2015-03-20 18:46:46 +03:00
|
|
|
/* 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";
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2015-03-20 18:46:46 +03:00
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = ["modal"];
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
const COMMON_DIALOG = "chrome://global/content/commonDialog.xul";
|
|
|
|
|
2017-10-12 14:21:17 +03:00
|
|
|
const isFirefox = () =>
|
|
|
|
Services.appinfo.ID == "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
|
2015-03-20 18:46:46 +03:00
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/** @namespace */
|
|
|
|
this.modal = {
|
2015-03-20 18:46:46 +03:00
|
|
|
COMMON_DIALOG_LOADED: "common-dialog-loaded",
|
|
|
|
TABMODAL_DIALOG_LOADED: "tabmodal-dialog-loaded",
|
|
|
|
handlers: {
|
|
|
|
"common-dialog-loaded": new Set(),
|
2017-06-30 02:40:24 +03:00
|
|
|
"tabmodal-dialog-loaded": new Set(),
|
|
|
|
},
|
2015-03-20 18:46:46 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add handler that will be called when a global- or tab modal dialogue
|
|
|
|
* appears.
|
|
|
|
*
|
|
|
|
* This is achieved by installing observers for common-
|
|
|
|
* and tab modal loaded events.
|
|
|
|
*
|
|
|
|
* This function is a no-op if called on any other product than Firefox.
|
|
|
|
*
|
|
|
|
* @param {function(Object, string)} handler
|
|
|
|
* The handler to be called, which is passed the
|
|
|
|
* subject (e.g. ChromeWindow) and the topic (one of
|
|
|
|
* {@code modal.COMMON_DIALOG_LOADED} or
|
|
|
|
* {@code modal.TABMODAL_DIALOG_LOADED}.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
modal.addHandler = function(handler) {
|
2015-03-20 18:46:46 +03:00
|
|
|
if (!isFirefox()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(this.handlers).map(topic => {
|
|
|
|
this.handlers[topic].add(handler);
|
2017-04-14 22:51:38 +03:00
|
|
|
Services.obs.addObserver(handler, topic);
|
2015-03-20 18:46:46 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-28 23:47:57 +03:00
|
|
|
/**
|
|
|
|
* Check for already existing modal or tab modal dialogs
|
|
|
|
*
|
|
|
|
* @param {browser.Context} context
|
|
|
|
* Reference to the browser context to check for existent dialogs.
|
|
|
|
*
|
|
|
|
* @return {modal.Dialog}
|
2017-06-30 02:40:24 +03:00
|
|
|
* Returns instance of the Dialog class, or `null` if no modal dialog
|
|
|
|
* is present.
|
2017-03-28 23:47:57 +03:00
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
modal.findModalDialogs = function(context) {
|
|
|
|
// First check if there is a modal dialog already present for the
|
|
|
|
// current browser window.
|
2018-08-19 04:13:14 +03:00
|
|
|
for (let win of Services.wm.getEnumerator(null)) {
|
2017-06-30 02:40:24 +03:00
|
|
|
// Modal dialogs which do not have an opener set, we cannot detect
|
|
|
|
// as long as GetZOrderDOMWindowEnumerator doesn't work on Linux
|
|
|
|
// (Bug 156333).
|
|
|
|
if (win.document.documentURI === COMMON_DIALOG &&
|
2017-03-28 23:47:57 +03:00
|
|
|
win.opener && win.opener === context.window) {
|
|
|
|
return new modal.Dialog(() => context, Cu.getWeakReference(win));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:40:24 +03:00
|
|
|
// If no modal dialog has been found, also check if there is an open
|
|
|
|
// tab modal dialog present for the current tab.
|
2017-03-28 23:47:57 +03:00
|
|
|
// TODO: Find an adequate implementation for Fennec.
|
|
|
|
if (context.tab && context.tabBrowser.getTabModalPromptBox) {
|
|
|
|
let contentBrowser = context.contentBrowser;
|
2017-06-30 02:40:24 +03:00
|
|
|
let promptManager =
|
|
|
|
context.tabBrowser.getTabModalPromptBox(contentBrowser);
|
2017-03-28 23:47:57 +03:00
|
|
|
let prompts = promptManager.listPrompts();
|
|
|
|
|
|
|
|
if (prompts.length) {
|
|
|
|
return new modal.Dialog(() => context, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2015-03-20 18:46:46 +03:00
|
|
|
/**
|
|
|
|
* Remove modal dialogue handler by function reference.
|
|
|
|
*
|
|
|
|
* This function is a no-op if called on any other product than Firefox.
|
|
|
|
*
|
|
|
|
* @param {function} toRemove
|
|
|
|
* The handler previously passed to modal.addHandler which will now
|
|
|
|
* be removed.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
modal.removeHandler = function(toRemove) {
|
2015-03-20 18:46:46 +03:00
|
|
|
if (!isFirefox()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let topic of Object.keys(this.handlers)) {
|
|
|
|
let handlers = this.handlers[topic];
|
|
|
|
for (let handler of handlers) {
|
|
|
|
if (handler == toRemove) {
|
|
|
|
Services.obs.removeObserver(handler, topic);
|
|
|
|
handlers.delete(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the current modal dialogue.
|
|
|
|
*
|
2016-05-10 15:29:21 +03:00
|
|
|
* @param {function(): browser.Context} curBrowserFn
|
|
|
|
* Function that returns the current |browser.Context|.
|
2016-03-08 17:10:32 +03:00
|
|
|
* @param {nsIWeakReference=} winRef
|
|
|
|
* A weak reference to the current |ChromeWindow|.
|
2015-03-20 18:46:46 +03:00
|
|
|
*/
|
2016-03-08 17:10:32 +03:00
|
|
|
modal.Dialog = class {
|
|
|
|
constructor(curBrowserFn, winRef = undefined) {
|
|
|
|
this.curBrowserFn_ = curBrowserFn;
|
|
|
|
this.win_ = winRef;
|
|
|
|
}
|
2015-03-20 18:46:46 +03:00
|
|
|
|
2016-03-08 17:10:32 +03:00
|
|
|
get curBrowser_() { return this.curBrowserFn_(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ChromeWindow associated with an open dialog window if
|
|
|
|
* it is currently attached to the DOM.
|
|
|
|
*/
|
|
|
|
get window() {
|
|
|
|
if (this.win_) {
|
2015-03-20 18:46:46 +03:00
|
|
|
let win = this.win_.get();
|
2015-03-23 23:43:18 +03:00
|
|
|
if (win && win.parent) {
|
2015-03-20 18:46:46 +03:00
|
|
|
return win;
|
2015-03-23 23:43:18 +03:00
|
|
|
}
|
2015-03-20 18:46:46 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-08 17:10:32 +03:00
|
|
|
get ui() {
|
2015-03-20 18:46:46 +03:00
|
|
|
let win = this.window;
|
2015-03-23 23:43:18 +03:00
|
|
|
if (win) {
|
2015-03-20 18:46:46 +03:00
|
|
|
return win.Dialog.ui;
|
2015-03-23 23:43:18 +03:00
|
|
|
}
|
2016-03-08 17:10:32 +03:00
|
|
|
return this.curBrowser_.getTabModalUI();
|
2015-03-20 18:46:46 +03:00
|
|
|
}
|
2016-03-08 17:10:32 +03:00
|
|
|
};
|