Skip to content

Commit c978775

Browse files
committed
Bug 1659073 - Add confirmRepost to nsIPromptCollection. r=pbz,smaug
Differential Revision: https://phabricator.services.mozilla.com/D89657
1 parent f3a7f78 commit c978775

File tree

3 files changed

+97
-84
lines changed

3 files changed

+97
-84
lines changed

browser/components/prompts/PromptCollection.jsm

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,78 @@ const { XPCOMUtils } = ChromeUtils.import(
1616
* @class PromptCollection
1717
*/
1818
class PromptCollection {
19+
confirmRepost(browsingContext) {
20+
let brandName;
21+
try {
22+
brandName = this.stringBundles.brand.GetStringFromName("brandShortName");
23+
} catch (exception) {
24+
// That's ok, we'll use a generic version of the prompt
25+
}
26+
27+
let message;
28+
let resendLabel;
29+
try {
30+
if (brandName) {
31+
message = this.stringBundles.app.formatStringFromName(
32+
"confirmRepostPrompt",
33+
[brandName]
34+
);
35+
} else {
36+
// Use a generic version of this prompt.
37+
message = this.stringBundles.app.GetStringFromName(
38+
"confirmRepostPrompt"
39+
);
40+
}
41+
resendLabel = this.stringBundles.app.GetStringFromName(
42+
"resendButton.label"
43+
);
44+
} catch (exception) {
45+
Cu.reportError("Failed to get strings from appstrings.properties");
46+
return false;
47+
}
48+
49+
let contentViewer = browsingContext?.docShell?.contentViewer;
50+
let modalType = contentViewer?.isTabModalPromptAllowed
51+
? Ci.nsIPromptService.MODAL_TYPE_CONTENT
52+
: Ci.nsIPromptService.MODAL_TYPE_WINDOW;
53+
let buttonFlags =
54+
(Ci.nsIPromptService.BUTTON_TITLE_IS_STRING *
55+
Ci.nsIPromptService.BUTTON_POS_0) |
56+
(Ci.nsIPromptService.BUTTON_TITLE_CANCEL *
57+
Ci.nsIPromptService.BUTTON_POS_1);
58+
let buttonPressed = Services.prompt.confirmExBC(
59+
browsingContext,
60+
modalType,
61+
null,
62+
message,
63+
buttonFlags,
64+
resendLabel,
65+
null,
66+
null,
67+
null,
68+
{}
69+
);
70+
71+
return buttonPressed === 0;
72+
}
73+
1974
beforeUnloadCheck(browsingContext) {
2075
let title;
2176
let message;
2277
let leaveLabel;
2378
let stayLabel;
2479

2580
try {
26-
title = this.domBundle.GetStringFromName("OnBeforeUnloadTitle");
27-
message = this.domBundle.GetStringFromName("OnBeforeUnloadMessage");
28-
leaveLabel = this.domBundle.GetStringFromName(
81+
title = this.stringBundles.dom.GetStringFromName("OnBeforeUnloadTitle");
82+
message = this.stringBundles.dom.GetStringFromName(
83+
"OnBeforeUnloadMessage"
84+
);
85+
leaveLabel = this.stringBundles.dom.GetStringFromName(
2986
"OnBeforeUnloadLeaveButton"
3087
);
31-
stayLabel = this.domBundle.GetStringFromName("OnBeforeUnloadStayButton");
88+
stayLabel = this.stringBundles.dom.GetStringFromName(
89+
"OnBeforeUnloadStayButton"
90+
);
3291
} catch (exception) {
3392
Cu.reportError("Failed to get strings from dom.properties");
3493
return false;
@@ -63,19 +122,27 @@ class PromptCollection {
63122
}
64123
}
65124

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!");
125+
const BUNDLES = {
126+
dom: "chrome://global/locale/dom/dom.properties",
127+
app: "chrome://global/locale/appstrings.properties",
128+
brand: "chrome://branding/locale/brand.properties",
129+
};
130+
131+
PromptCollection.prototype.stringBundles = {};
132+
133+
for (const [bundleName, bundleUrl] of Object.entries(BUNDLES)) {
134+
XPCOMUtils.defineLazyGetter(
135+
PromptCollection.prototype.stringBundles,
136+
bundleName,
137+
function() {
138+
let bundle = Services.strings.createBundle(bundleUrl);
139+
if (!bundle) {
140+
throw new Error("String bundle for dom not present!");
141+
}
142+
return bundle;
75143
}
76-
return bundle;
77-
}
78-
);
144+
);
145+
}
79146

80147
PromptCollection.prototype.classID = Components.ID(
81148
"{7913837c-9623-11ea-bb37-0242ac130002}"

docshell/base/nsDocShell.cpp

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
#include "nsIPrincipal.h"
129129
#include "nsIPrivacyTransitionObserver.h"
130130
#include "nsIPrompt.h"
131+
#include "nsIPromptCollection.h"
131132
#include "nsIPromptFactory.h"
132133
#include "nsIReflowObserver.h"
133134
#include "nsIScriptChannel.h"
@@ -11709,78 +11710,13 @@ nsresult nsDocShell::ConfirmRepost(bool* aRepost) {
1170911710
return NS_OK;
1171011711
}
1171111712

11712-
nsCOMPtr<nsIPrompt> prompter;
11713-
CallGetInterface(this, static_cast<nsIPrompt**>(getter_AddRefs(prompter)));
11713+
nsCOMPtr<nsIPromptCollection> prompter =
11714+
do_GetService("@mozilla.org/embedcomp/prompt-collection;1");
1171411715
if (!prompter) {
1171511716
return NS_ERROR_NOT_AVAILABLE;
1171611717
}
1171711718

11718-
nsCOMPtr<nsIStringBundleService> stringBundleService =
11719-
mozilla::services::GetStringBundleService();
11720-
if (!stringBundleService) {
11721-
return NS_ERROR_FAILURE;
11722-
}
11723-
11724-
nsCOMPtr<nsIStringBundle> appBundle;
11725-
nsresult rv = stringBundleService->CreateBundle(kAppstringsBundleURL,
11726-
getter_AddRefs(appBundle));
11727-
NS_ENSURE_SUCCESS(rv, rv);
11728-
11729-
nsCOMPtr<nsIStringBundle> brandBundle;
11730-
rv = stringBundleService->CreateBundle(kBrandBundleURL,
11731-
getter_AddRefs(brandBundle));
11732-
NS_ENSURE_SUCCESS(rv, rv);
11733-
11734-
NS_ASSERTION(prompter && brandBundle && appBundle,
11735-
"Unable to set up repost prompter.");
11736-
11737-
AutoTArray<nsString, 1> formatStrings;
11738-
rv = brandBundle->GetStringFromName("brandShortName",
11739-
*formatStrings.AppendElement());
11740-
11741-
nsAutoString msgString, button0Title;
11742-
if (NS_FAILED(rv)) { // No brand, use the generic version.
11743-
rv = appBundle->GetStringFromName("confirmRepostPrompt", msgString);
11744-
} else {
11745-
// Brand available - if the app has an override file with formatting, the
11746-
// app name will be included. Without an override, the prompt will look
11747-
// like the generic version.
11748-
rv = appBundle->FormatStringFromName("confirmRepostPrompt", formatStrings,
11749-
msgString);
11750-
}
11751-
if (NS_FAILED(rv)) {
11752-
return rv;
11753-
}
11754-
11755-
rv = appBundle->GetStringFromName("resendButton.label", button0Title);
11756-
if (NS_FAILED(rv)) {
11757-
return rv;
11758-
}
11759-
11760-
// Make the repost prompt content modal to prevent malicious pages from
11761-
// locking up the browser, see bug 1412559 for an example.
11762-
if (nsCOMPtr<nsIWritablePropertyBag2> promptBag =
11763-
do_QueryInterface(prompter)) {
11764-
promptBag->SetPropertyAsUint32(u"modalType"_ns,
11765-
nsIPrompt::MODAL_TYPE_CONTENT);
11766-
}
11767-
11768-
int32_t buttonPressed;
11769-
// The actual value here is irrelevant, but we can't pass an invalid
11770-
// bool through XPConnect.
11771-
bool checkState = false;
11772-
rv = prompter->ConfirmEx(
11773-
nullptr, msgString.get(),
11774-
(nsIPrompt::BUTTON_POS_0 * nsIPrompt::BUTTON_TITLE_IS_STRING) +
11775-
(nsIPrompt::BUTTON_POS_1 * nsIPrompt::BUTTON_TITLE_CANCEL),
11776-
button0Title.get(), nullptr, nullptr, nullptr, &checkState,
11777-
&buttonPressed);
11778-
if (NS_FAILED(rv)) {
11779-
return rv;
11780-
}
11781-
11782-
*aRepost = (buttonPressed == 0);
11783-
return NS_OK;
11719+
return prompter->ConfirmRepost(mBrowsingContext, aRepost);
1178411720
}
1178511721

1178611722
nsresult nsDocShell::GetPromptAndStringBundle(nsIPrompt** aPrompt,

toolkit/components/windowwatcher/nsIPromptCollection.idl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ interface nsIPromptCollection : nsISupports
2222
* @return true if the page should be allowed to navigate away
2323
*/
2424
boolean beforeUnloadCheck(in BrowsingContext aBrowsingContext);
25+
26+
/**
27+
* Puts up a dialog for the confirm repost prompt.
28+
*
29+
* @param aBrowsingContext
30+
* The browsing context the prompt should be opened for.
31+
*
32+
* @return true if the page should be allowed to repost data.
33+
*/
34+
boolean confirmRepost(in BrowsingContext aBrowsingContext);
2535
};

0 commit comments

Comments
 (0)