Skip to content

Commit fd1c813

Browse files
committed
Bug 1871187: prompt() goes through content analysis r=Gijs
Adds another text-paste case to content analysis, this time for the prompt() command, which is not covered by the other clipboard cases in bug 1871135. Differential Revision: https://phabricator.services.mozilla.com/D197018
1 parent 397f71b commit fd1c813

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

browser/actors/PromptParent.sys.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ export class PromptParent extends JSWindowActorParent {
301301

302302
args.promptAborted = false;
303303
args.openedWithTabDialog = true;
304+
args.owningBrowsingContext = this.browsingContext;
304305

305306
// Convert args object to a prop bag for the dialog to consume.
306307
let bag;

browser/components/contentanalysis/content/ContentAnalysis.sys.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export const ContentAnalysis = {
221221
}
222222
case "dlp-request-made":
223223
{
224-
const request = aSubj;
224+
const request = aSubj.QueryInterface(Ci.nsIContentAnalysisRequest);
225225
if (!request) {
226226
console.error(
227227
"Showing in-browser Content Analysis notification but no request was passed"
@@ -272,7 +272,7 @@ export const ContentAnalysis = {
272272
}
273273
break;
274274
case "dlp-response":
275-
const request = aSubj;
275+
const request = aSubj.QueryInterface(Ci.nsIContentAnalysisResponse);
276276
// Cancels timer or slow message UI,
277277
// if present, and possibly presents the CA verdict.
278278
if (!request) {

toolkit/components/prompts/content/commonDialog.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
const { CommonDialog } = ChromeUtils.importESModule(
66
"resource://gre/modules/CommonDialog.sys.mjs"
77
);
8+
const { XPCOMUtils } = ChromeUtils.importESModule(
9+
"resource://gre/modules/XPCOMUtils.sys.mjs"
10+
);
11+
12+
const lazy = {};
13+
14+
XPCOMUtils.defineLazyServiceGetter(
15+
lazy,
16+
"gContentAnalysis",
17+
"@mozilla.org/contentanalysis;1",
18+
Ci.nsIContentAnalysis
19+
);
820

921
// imported by adjustableTitle.js loaded in the same context:
1022
/* globals PromptUtils */
@@ -123,6 +135,58 @@ function commonDialogOnLoad() {
123135
// If the icon hasn't loaded yet, size the window to the content again when
124136
// it does, as its layout can change.
125137
ui.infoIcon.addEventListener("load", () => window.sizeToContent());
138+
if (lazy.gContentAnalysis.isActive && args.owningBrowsingContext?.isContent) {
139+
ui.loginTextbox?.addEventListener("paste", async event => {
140+
let data = event.clipboardData.getData("text/plain");
141+
if (data?.length > 0) {
142+
// Prevent the paste from happening until content analysis returns a response
143+
event.preventDefault();
144+
// Selections can be forward or backward, so use min/max
145+
const startIndex = Math.min(
146+
ui.loginTextbox.selectionStart,
147+
ui.loginTextbox.selectionEnd
148+
);
149+
const endIndex = Math.max(
150+
ui.loginTextbox.selectionStart,
151+
ui.loginTextbox.selectionEnd
152+
);
153+
const selectionDirection =
154+
endIndex < startIndex ? "backward" : "forward";
155+
try {
156+
const response = await lazy.gContentAnalysis.analyzeContentRequest(
157+
{
158+
requestToken: Services.uuid.generateUUID().toString(),
159+
resources: [],
160+
analysisType: Ci.nsIContentAnalysis.eBulkDataEntry,
161+
operationTypeForDisplay: Ci.nsIContentAnalysisRequest.eClipboard,
162+
url: args.owningBrowsingContext.currentURI.spec,
163+
textContent: data,
164+
windowGlobalParent:
165+
args.owningBrowsingContext.currentWindowContext,
166+
},
167+
true
168+
);
169+
if (response.shouldAllowContent) {
170+
ui.loginTextbox.value =
171+
ui.loginTextbox.value.slice(0, startIndex) +
172+
data +
173+
ui.loginTextbox.value.slice(endIndex);
174+
ui.loginTextbox.focus();
175+
if (startIndex !== endIndex) {
176+
// Select the pasted text
177+
ui.loginTextbox.setSelectionRange(
178+
startIndex,
179+
startIndex + data.length,
180+
selectionDirection
181+
);
182+
}
183+
}
184+
} catch (error) {
185+
console.error("Content analysis request returned error: ", error);
186+
}
187+
}
188+
});
189+
}
126190

127191
window.getAttention();
128192
}

0 commit comments

Comments
 (0)