RemotePrompt.jsm 3.23 KB
Newer Older
1
2
3
4
5
6
7
/* vim: set ts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

8
var EXPORTED_SYMBOLS = [ "RemotePrompt" ];
9

10
11
12
13
ChromeUtils.defineModuleGetter(this, "PromptUtils",
                               "resource://gre/modules/SharedPromptUtils.jsm");
ChromeUtils.defineModuleGetter(this, "Services",
                               "resource://gre/modules/Services.jsm");
14

15
var RemotePrompt = {
16
  // Listeners are added in BrowserGlue.jsm
17
  receiveMessage(message) {
18
19
20
21
22
    switch (message.name) {
      case "Prompt:Open":
        if (message.data.uri) {
          this.openModalWindow(message.data, message.target);
        } else {
23
          this.openTabPrompt(message.data, message.target);
24
25
26
27
28
        }
        break;
    }
  },

29
  openTabPrompt(args, browser) {
30
    let window = browser.ownerGlobal;
31
    let tabPrompt = window.gBrowser.getTabModalPromptBox(browser);
32
    let newPrompt;
33
    let needRemove = false;
34
35
36
    let promptId = args._remoteId;

    function onPromptClose(forceCleanup) {
37
38
39
40
      // It's possible that we removed the prompt during the
      // appendPrompt call below. In that case, newPrompt will be
      // undefined. We set the needRemove flag to remember to remove
      // it right after we've finished adding it.
41
42
      if (newPrompt)
        tabPrompt.removePrompt(newPrompt);
43
44
      else
        needRemove = true;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

      PromptUtils.fireDialogEvent(window, "DOMModalDialogClosed", browser);
      browser.messageManager.sendAsyncMessage("Prompt:Close", args);
    }

    browser.messageManager.addMessageListener("Prompt:ForceClose", function listener(message) {
      // If this was for another prompt in the same tab, ignore it.
      if (message.data._remoteId !== promptId) {
        return;
      }

      browser.messageManager.removeMessageListener("Prompt:ForceClose", listener);

      if (newPrompt) {
        newPrompt.abortPrompt();
      }
    });

    try {
64
65
66
67
68
      let eventDetail = {
        tabPrompt: true,
        promptPrincipal: args.promptPrincipal,
        inPermitUnload: args.inPermitUnload,
      };
69
      PromptUtils.fireDialogEvent(window, "DOMWillOpenModalDialog", browser, eventDetail);
70
71
72
73
74

      args.promptActive = true;

      newPrompt = tabPrompt.appendPrompt(args, onPromptClose);

75
76
77
78
      if (needRemove) {
        tabPrompt.removePrompt(newPrompt);
      }

79
80
81
82
83
84
85
86
      // TODO since we don't actually open a window, need to check if
      // there's other stuff in nsWindowWatcher::OpenWindowInternal
      // that we might need to do here as well.
    } catch (ex) {
      onPromptClose(true);
    }
  },

87
  openModalWindow(args, browser) {
88
    let window = browser.ownerGlobal;
89
90
91
92
93
94
95
96
97
98
99
100
    try {
      PromptUtils.fireDialogEvent(window, "DOMWillOpenModalDialog", browser);
      let bag = PromptUtils.objectToPropBag(args);

      Services.ww.openWindow(window, args.uri, "_blank",
                             "centerscreen,chrome,modal,titlebar", bag);

      PromptUtils.propBagToObject(bag, args);
    } finally {
      PromptUtils.fireDialogEvent(window, "DOMModalDialogClosed", browser);
      browser.messageManager.sendAsyncMessage("Prompt:Close", args);
    }
101
  },
102
};