Skip to content

Commit b96d141

Browse files
committed
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
1 parent 4c37a4d commit b96d141

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

browser/components/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ DIRS += [
4646
'pocket',
4747
'preferences',
4848
'privatebrowsing',
49+
'prompts',
4950
'protections',
5051
'protocolhandler',
5152
'resistfingerprinting',
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
"use strict";
6+
7+
var EXPORTED_SYMBOLS = ["PromptCollection"];
8+
9+
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
10+
const { XPCOMUtils } = ChromeUtils.import(
11+
"resource://gre/modules/XPCOMUtils.jsm"
12+
);
13+
14+
/**
15+
* Implements nsIPromptCollection
16+
* @class PromptCollection
17+
*/
18+
class PromptCollection {
19+
beforeUnloadCheck(browsingContext) {
20+
let title;
21+
let message;
22+
let leaveLabel;
23+
let stayLabel;
24+
25+
try {
26+
title = this.domBundle.GetStringFromName("OnBeforeUnloadTitle");
27+
message = this.domBundle.GetStringFromName("OnBeforeUnloadMessage");
28+
leaveLabel = this.domBundle.GetStringFromName(
29+
"OnBeforeUnloadLeaveButton"
30+
);
31+
stayLabel = this.domBundle.GetStringFromName("OnBeforeUnloadStayButton");
32+
} catch (exception) {
33+
Cu.reportError("Failed to get strings from dom.properties");
34+
return false;
35+
}
36+
37+
let contentViewer = browsingContext?.docShell?.contentViewer;
38+
let modalType = contentViewer?.isTabModalPromptAllowed
39+
? Ci.nsIPromptService.MODAL_TYPE_CONTENT
40+
: Ci.nsIPromptService.MODAL_TYPE_WINDOW;
41+
42+
let buttonFlags =
43+
Ci.nsIPromptService.BUTTON_POS_0_DEFAULT |
44+
(Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
45+
Ci.nsIPromptService.BUTTON_POS_0) |
46+
(Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
47+
Ci.nsIPromptService.BUTTON_POS_1);
48+
49+
let buttonPressed = Services.prompt.confirmExBC(
50+
browsingContext,
51+
modalType,
52+
title,
53+
message,
54+
buttonFlags,
55+
leaveLabel,
56+
stayLabel,
57+
null,
58+
null,
59+
{}
60+
);
61+
62+
return buttonPressed === 0;
63+
}
64+
}
65+
66+
XPCOMUtils.defineLazyGetter(
67+
PromptCollection.prototype,
68+
"domBundle",
69+
function() {
70+
let bundle = Services.strings.createBundle(
71+
"chrome://global/locale/dom/dom.properties"
72+
);
73+
if (!bundle) {
74+
throw new Error("String bundle for dom not present!");
75+
}
76+
return bundle;
77+
}
78+
);
79+
80+
PromptCollection.prototype.classID = Components.ID(
81+
"{7913837c-9623-11ea-bb37-0242ac130002}"
82+
);
83+
PromptCollection.prototype.QueryInterface = ChromeUtils.generateQI([
84+
Ci.nsIPromptCollection,
85+
]);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
Classes = [
6+
{
7+
'cid': '{7913837c-9623-11ea-bb37-0242ac130002}',
8+
'contract_ids': ['@mozilla.org/embedcomp/prompt-collection;1'],
9+
'jsm': 'resource:///modules/PromptCollection.jsm',
10+
'constructor': 'PromptCollection',
11+
},
12+
]

browser/components/prompts/moz.build

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
with Files("**"):
6+
BUG_COMPONENT = ("Toolkit", "Notifications and Alerts")
7+
8+
EXTRA_JS_MODULES += [
9+
'PromptCollection.jsm',
10+
]
11+
12+
XPCOM_MANIFESTS += [
13+
'components.conf',
14+
]

toolkit/components/windowwatcher/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TEST_DIRS += ['test']
1212
XPIDL_SOURCES += [
1313
'nsIDialogParamBlock.idl',
1414
'nsIOpenWindowInfo.idl',
15+
'nsIPromptCollection.idl',
1516
'nsIPromptFactory.idl',
1617
'nsIPromptService.idl',
1718
'nsIWindowWatcher.idl',
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "nsISupports.idl"
6+
7+
webidl BrowsingContext;
8+
9+
/**
10+
* This interface contains various specialized prompts that the app can
11+
* implement.
12+
*/
13+
[scriptable, uuid(7913837c-9623-11ea-bb37-0242ac130002)]
14+
interface nsIPromptCollection : nsISupports
15+
{
16+
/**
17+
* Puts up a dialog for the before unload prompt.
18+
*
19+
* @param aBrowsingContext
20+
* The browsing context the prompt should be opened for.
21+
*
22+
* @return true if the page should be allowed to navigate away
23+
*/
24+
boolean beforeUnloadCheck(in BrowsingContext aBrowsingContext);
25+
};

0 commit comments

Comments
 (0)