RemotePrompt.jsm 3.65 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
9
10
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
11
12
13

this.EXPORTED_SYMBOLS = [ "RemotePrompt" ];

14
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
15

16
17
18
19
20
21
22
23
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUIUtils",
                                  "resource:///modules/PlacesUIUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
                                  "resource://gre/modules/PrivateBrowsingUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PromptUtils",
                                  "resource://gre/modules/SharedPromptUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
                                  "resource://gre/modules/Services.jsm");
24

25
26
var RemotePrompt = {
  // Listeners are added in nsBrowserGlue.js
27
  receiveMessage(message) {
28
29
30
31
32
    switch (message.name) {
      case "Prompt:Open":
        if (message.data.uri) {
          this.openModalWindow(message.data, message.target);
        } else {
33
          this.openTabPrompt(message.data, message.target);
34
35
36
37
38
        }
        break;
    }
  },

39
  openTabPrompt(args, browser) {
40
    let window = browser.ownerGlobal;
41
    let tabPrompt = window.gBrowser.getTabModalPromptBox(browser);
42
    let newPrompt;
43
    let needRemove = false;
44
45
46
    let promptId = args._remoteId;

    function onPromptClose(forceCleanup) {
47
48
49
50
      // 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.
51
52
      if (newPrompt)
        tabPrompt.removePrompt(newPrompt);
53
54
      else
        needRemove = true;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

      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 {
74
75
76
77
78
      let eventDetail = {
        tabPrompt: true,
        promptPrincipal: args.promptPrincipal,
        inPermitUnload: args.inPermitUnload,
      };
79
      PromptUtils.fireDialogEvent(window, "DOMWillOpenModalDialog", browser, eventDetail);
80
81
82
83
84

      args.promptActive = true;

      newPrompt = tabPrompt.appendPrompt(args, onPromptClose);

85
86
87
88
      if (needRemove) {
        tabPrompt.removePrompt(newPrompt);
      }

89
90
91
92
93
94
95
96
      // 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);
    }
  },

97
  openModalWindow(args, browser) {
98
    let window = browser.ownerGlobal;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    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);
    }
  }
};