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
This commit is contained in:
Greg Stoll
2024-01-09 13:53:31 +00:00
parent 7a70ac7c9b
commit b7eef69b9f
3 changed files with 67 additions and 2 deletions

View File

@@ -301,6 +301,7 @@ export class PromptParent extends JSWindowActorParent {
args.promptAborted = false; args.promptAborted = false;
args.openedWithTabDialog = true; args.openedWithTabDialog = true;
args.owningBrowsingContext = this.browsingContext;
// Convert args object to a prop bag for the dialog to consume. // Convert args object to a prop bag for the dialog to consume.
let bag; let bag;

View File

@@ -221,7 +221,7 @@ export const ContentAnalysis = {
} }
case "dlp-request-made": case "dlp-request-made":
{ {
const request = aSubj; const request = aSubj.QueryInterface(Ci.nsIContentAnalysisRequest);
if (!request) { if (!request) {
console.error( console.error(
"Showing in-browser Content Analysis notification but no request was passed" "Showing in-browser Content Analysis notification but no request was passed"
@@ -272,7 +272,7 @@ export const ContentAnalysis = {
} }
break; break;
case "dlp-response": case "dlp-response":
const request = aSubj; const request = aSubj.QueryInterface(Ci.nsIContentAnalysisResponse);
// Cancels timer or slow message UI, // Cancels timer or slow message UI,
// if present, and possibly presents the CA verdict. // if present, and possibly presents the CA verdict.
if (!request) { if (!request) {

View File

@@ -5,6 +5,18 @@
const { CommonDialog } = ChromeUtils.importESModule( const { CommonDialog } = ChromeUtils.importESModule(
"resource://gre/modules/CommonDialog.sys.mjs" "resource://gre/modules/CommonDialog.sys.mjs"
); );
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const lazy = {};
XPCOMUtils.defineLazyServiceGetter(
lazy,
"gContentAnalysis",
"@mozilla.org/contentanalysis;1",
Ci.nsIContentAnalysis
);
// imported by adjustableTitle.js loaded in the same context: // imported by adjustableTitle.js loaded in the same context:
/* globals PromptUtils */ /* globals PromptUtils */
@@ -123,6 +135,58 @@ function commonDialogOnLoad() {
// If the icon hasn't loaded yet, size the window to the content again when // If the icon hasn't loaded yet, size the window to the content again when
// it does, as its layout can change. // it does, as its layout can change.
ui.infoIcon.addEventListener("load", () => window.sizeToContent()); ui.infoIcon.addEventListener("load", () => window.sizeToContent());
if (lazy.gContentAnalysis.isActive && args.owningBrowsingContext?.isContent) {
ui.loginTextbox?.addEventListener("paste", async event => {
let data = event.clipboardData.getData("text/plain");
if (data?.length > 0) {
// Prevent the paste from happening until content analysis returns a response
event.preventDefault();
// Selections can be forward or backward, so use min/max
const startIndex = Math.min(
ui.loginTextbox.selectionStart,
ui.loginTextbox.selectionEnd
);
const endIndex = Math.max(
ui.loginTextbox.selectionStart,
ui.loginTextbox.selectionEnd
);
const selectionDirection =
endIndex < startIndex ? "backward" : "forward";
try {
const response = await lazy.gContentAnalysis.analyzeContentRequest(
{
requestToken: Services.uuid.generateUUID().toString(),
resources: [],
analysisType: Ci.nsIContentAnalysis.eBulkDataEntry,
operationTypeForDisplay: Ci.nsIContentAnalysisRequest.eClipboard,
url: args.owningBrowsingContext.currentURI.spec,
textContent: data,
windowGlobalParent:
args.owningBrowsingContext.currentWindowContext,
},
true
);
if (response.shouldAllowContent) {
ui.loginTextbox.value =
ui.loginTextbox.value.slice(0, startIndex) +
data +
ui.loginTextbox.value.slice(endIndex);
ui.loginTextbox.focus();
if (startIndex !== endIndex) {
// Select the pasted text
ui.loginTextbox.setSelectionRange(
startIndex,
startIndex + data.length,
selectionDirection
);
}
}
} catch (error) {
console.error("Content analysis request returned error: ", error);
}
}
});
}
window.getAttention(); window.getAttention();
} }