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
This commit is contained in:
Agi Sferro 2020-05-22 16:24:12 +00:00
Родитель 4c37a4d137
Коммит b96d141f13
6 изменённых файлов: 138 добавлений и 0 удалений

Просмотреть файл

@ -46,6 +46,7 @@ DIRS += [
'pocket',
'preferences',
'privatebrowsing',
'prompts',
'protections',
'protocolhandler',
'resistfingerprinting',

Просмотреть файл

@ -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,
]);

Просмотреть файл

@ -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',
},
]

Просмотреть файл

@ -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',
]

Просмотреть файл

@ -12,6 +12,7 @@ TEST_DIRS += ['test']
XPIDL_SOURCES += [
'nsIDialogParamBlock.idl',
'nsIOpenWindowInfo.idl',
'nsIPromptCollection.idl',
'nsIPromptFactory.idl',
'nsIPromptService.idl',
'nsIWindowWatcher.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);
};