Bug 1629113 - Add nsIPromptCollection. r=johannh,pbz
authorAgi Sferro <agi@sferro.dev>
Fri, 22 May 2020 16:24:12 +0000
changeset 531741 67e3388a7051a7deef04868ee16b0f90e3ab33d7
parent 531740 e3fca4a4cc7f86732fc05404b170f23ba92cb982
child 531742 b4aa2007863153e3b39aef004689964d728b1038
push id37442
push userncsoregi@mozilla.com
push dateSat, 23 May 2020 09:21:24 +0000
treeherdermozilla-central@bbcc193fe0f0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh, pbz
bugs1629113
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1629113 - Add nsIPromptCollection. r=johannh,pbz This new prompt service will handle specialized prompts like beforeUnloadCheck. The plan is to eventually phase out generic prompts like confirmExBC and just have specialized prompts. Differential Revision: https://phabricator.services.mozilla.com/D72720
browser/components/moz.build
browser/components/prompts/PromptCollection.jsm
browser/components/prompts/components.conf
browser/components/prompts/moz.build
toolkit/components/windowwatcher/moz.build
toolkit/components/windowwatcher/nsIPromptCollection.idl
--- a/browser/components/moz.build
+++ b/browser/components/moz.build
@@ -41,16 +41,17 @@ DIRS += [
     'fxmonitor',
     'migration',
     'newtab',
     'originattributes',
     'places',
     'pocket',
     'preferences',
     'privatebrowsing',
+    'prompts',
     'protections',
     'protocolhandler',
     'resistfingerprinting',
     'search',
     'sessionstore',
     'shell',
     'ssb',
     'syncedtabs',
new file mode 100644
--- /dev/null
+++ b/browser/components/prompts/PromptCollection.jsm
@@ -0,0 +1,85 @@
+/* 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";
+
+var EXPORTED_SYMBOLS = ["PromptCollection"];
+
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+const { XPCOMUtils } = ChromeUtils.import(
+  "resource://gre/modules/XPCOMUtils.jsm"
+);
+
+/**
+ * Implements nsIPromptCollection
+ * @class PromptCollection
+ */
+class PromptCollection {
+  beforeUnloadCheck(browsingContext) {
+    let title;
+    let message;
+    let leaveLabel;
+    let stayLabel;
+
+    try {
+      title = this.domBundle.GetStringFromName("OnBeforeUnloadTitle");
+      message = this.domBundle.GetStringFromName("OnBeforeUnloadMessage");
+      leaveLabel = this.domBundle.GetStringFromName(
+        "OnBeforeUnloadLeaveButton"
+      );
+      stayLabel = this.domBundle.GetStringFromName("OnBeforeUnloadStayButton");
+    } catch (exception) {
+      Cu.reportError("Failed to get strings from dom.properties");
+      return false;
+    }
+
+    let contentViewer = browsingContext?.docShell?.contentViewer;
+    let modalType = contentViewer?.isTabModalPromptAllowed
+      ? Ci.nsIPromptService.MODAL_TYPE_CONTENT
+      : Ci.nsIPromptService.MODAL_TYPE_WINDOW;
+
+    let buttonFlags =
+      Ci.nsIPromptService.BUTTON_POS_0_DEFAULT |
+      (Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
+        Ci.nsIPromptService.BUTTON_POS_0) |
+      (Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
+        Ci.nsIPromptService.BUTTON_POS_1);
+
+    let buttonPressed = Services.prompt.confirmExBC(
+      browsingContext,
+      modalType,
+      title,
+      message,
+      buttonFlags,
+      leaveLabel,
+      stayLabel,
+      null,
+      null,
+      {}
+    );
+
+    return buttonPressed === 0;
+  }
+}
+
+XPCOMUtils.defineLazyGetter(
+  PromptCollection.prototype,
+  "domBundle",
+  function() {
+    let bundle = Services.strings.createBundle(
+      "chrome://global/locale/dom/dom.properties"
+    );
+    if (!bundle) {
+      throw new Error("String bundle for dom not present!");
+    }
+    return bundle;
+  }
+);
+
+PromptCollection.prototype.classID = Components.ID(
+  "{7913837c-9623-11ea-bb37-0242ac130002}"
+);
+PromptCollection.prototype.QueryInterface = ChromeUtils.generateQI([
+  Ci.nsIPromptCollection,
+]);
new file mode 100644
--- /dev/null
+++ b/browser/components/prompts/components.conf
@@ -0,0 +1,12 @@
+# 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/.
+
+Classes = [
+    {
+        'cid': '{7913837c-9623-11ea-bb37-0242ac130002}',
+        'contract_ids': ['@mozilla.org/embedcomp/prompt-collection;1'],
+        'jsm': 'resource:///modules/PromptCollection.jsm',
+        'constructor': 'PromptCollection',
+    },
+]
new file mode 100644
--- /dev/null
+++ b/browser/components/prompts/moz.build
@@ -0,0 +1,14 @@
+# 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/.
+
+with Files("**"):
+    BUG_COMPONENT = ("Toolkit", "Notifications and Alerts")
+
+EXTRA_JS_MODULES += [
+  'PromptCollection.jsm',
+]
+
+XPCOM_MANIFESTS += [
+  'components.conf',
+]
--- a/toolkit/components/windowwatcher/moz.build
+++ b/toolkit/components/windowwatcher/moz.build
@@ -7,16 +7,17 @@
 with Files('**'):
     BUG_COMPONENT = ('Core', 'Window Management')
 
 TEST_DIRS += ['test']
 
 XPIDL_SOURCES += [
     'nsIDialogParamBlock.idl',
     'nsIOpenWindowInfo.idl',
+    'nsIPromptCollection.idl',
     'nsIPromptFactory.idl',
     'nsIPromptService.idl',
     'nsIWindowWatcher.idl',
     'nsPIPromptService.idl',
     'nsPIWindowWatcher.idl',
 ]
 
 XPIDL_MODULE = 'windowwatcher'
new file mode 100644
--- /dev/null
+++ b/toolkit/components/windowwatcher/nsIPromptCollection.idl
@@ -0,0 +1,25 @@
+/* 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/. */
+
+#include "nsISupports.idl"
+
+webidl BrowsingContext;
+
+/**
+ * This interface contains various specialized prompts that the app can
+ * implement.
+ */
+[scriptable, uuid(7913837c-9623-11ea-bb37-0242ac130002)]
+interface nsIPromptCollection : nsISupports
+{
+  /**
+   * Puts up a dialog for the before unload prompt.
+   *
+   * @param aBrowsingContext
+   *        The browsing context the prompt should be opened for.
+   *
+   * @return true if the page should be allowed to navigate away
+   */
+  boolean beforeUnloadCheck(in BrowsingContext aBrowsingContext);
+};