1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/* 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/. */
var { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
var { BrowserUtils } = ChromeUtils.importESModule(
"resource://gre/modules/BrowserUtils.sys.mjs"
);
var { ExtensionParent } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionParent.sys.mjs"
);
var { MailE10SUtils } = ChromeUtils.importESModule(
"resource:///modules/MailE10SUtils.sys.mjs"
);
var { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
XPCOMUtils.defineLazyScriptGetter(
this,
"PrintUtils",
"chrome://messenger/content/printUtils.js"
);
var gContextMenu;
/* globals reporterListener */
/**
* @implements {nsICommandController}
*/
var contentController = {
commands: {
cmd_reload: {
isEnabled() {
return !contentProgress.busy;
},
doCommand() {
document.getElementById("requestFrame").reload();
},
},
cmd_stop: {
isEnabled() {
return contentProgress.busy;
},
doCommand() {
document.getElementById("requestFrame").stop();
},
},
"Browser:Back": {
isEnabled() {
return gBrowser.canGoBack;
},
doCommand() {
gBrowser.goBack();
},
},
"Browser:Forward": {
isEnabled() {
return gBrowser.canGoForward;
},
doCommand() {
gBrowser.goForward();
},
},
},
supportsCommand(command) {
return command in this.commands;
},
isCommandEnabled(command) {
if (!this.supportsCommand(command)) {
return false;
}
const cmd = this.commands[command];
return cmd.isEnabled();
},
doCommand(command) {
if (!this.supportsCommand(command)) {
return;
}
const cmd = this.commands[command];
if (!cmd.isEnabled()) {
return;
}
cmd.doCommand();
},
onEvent() {},
};
/**
* @implements {nsIBrowserDOMWindow}
*/
class nsBrowserAccess {
QueryInterface = ChromeUtils.generateQI(["nsIBrowserDOMWindow"]);
_openURIInNewTab(
aURI,
aReferrerInfo,
aIsExternal,
aOpenWindowInfo = null,
aTriggeringPrincipal = null,
aCsp = null,
aSkipLoad = false,
aMessageManagerGroup = null
) {
// This is a popup which must not have more than one tab, so open the new tab
// in the most recent mail window.
const win = Services.wm.getMostRecentWindow("mail:3pane", true);
if (!win) {
// We couldn't find a suitable window, a new one needs to be opened.
return null;
}
const loadInBackground = Services.prefs.getBoolPref(
"browser.tabs.loadDivertedInBackground"
);
const tabmail = win.document.getElementById("tabmail");
const newTab = tabmail.openTab("contentTab", {
background: loadInBackground,
csp: aCsp,
linkHandler: aMessageManagerGroup,
openWindowInfo: aOpenWindowInfo,
referrerInfo: aReferrerInfo,
skipLoad: aSkipLoad,
triggeringPrincipal: aTriggeringPrincipal,
url: aURI ? aURI.spec : "about:blank",
});
win.focus();
return newTab.browser;
}
createContentWindow() {
throw Components.Exception("Not implemented", Cr.NS_ERROR_NOT_IMPLEMENTED);
}
createContentWindowInFrame(aURI, aParams, aWhere, aFlags, aName) {
// Passing a null-URI to only create the content window,
// and pass true for aSkipLoad to prevent loading of
// about:blank
return this.getContentWindowOrOpenURIInFrame(
null,
aParams,
aWhere,
aFlags,
aName,
true
);
}
openURI() {
throw Components.Exception("Not implemented", Cr.NS_ERROR_NOT_IMPLEMENTED);
}
openURIInFrame() {
throw Components.Exception("Not implemented", Cr.NS_ERROR_NOT_IMPLEMENTED);
}
getContentWindowOrOpenURI() {
throw Components.Exception("Not implemented", Cr.NS_ERROR_NOT_IMPLEMENTED);
}
getContentWindowOrOpenURIInFrame(
aURI,
aParams,
aWhere,
aFlags,
aName,
aSkipLoad
) {
if (aWhere == Ci.nsIBrowserDOMWindow.OPEN_PRINT_BROWSER) {
return PrintUtils.handleStaticCloneCreatedForPrint(
aParams.openWindowInfo
);
}
if (aWhere != Ci.nsIBrowserDOMWindow.OPEN_NEWTAB) {
Services.console.logStringMessage(
"Error: openURIInFrame can only open in new tabs or print"
);
return null;
}
const isExternal = !!(aFlags & Ci.nsIBrowserDOMWindow.OPEN_EXTERNAL);
return this._openURIInNewTab(
aURI,
aParams.referrerInfo,
isExternal,
aParams.openWindowInfo,
aParams.triggeringPrincipal,
aParams.csp,
aSkipLoad,
aParams.openerBrowser?.getAttribute("messagemanagergroup")
);
}
canClose() {
return true;
}
get tabCount() {
return 1;
}
}
function loadRequestedUrl() {
const browser = document.getElementById("requestFrame");
browser.addProgressListener(reporterListener, Ci.nsIWebProgress.NOTIFY_ALL);
browser.addEventListener(
"DOMWindowClose",
() => {
if (browser.getAttribute("allowscriptstoclose") == "true") {
window.close();
}
},
true
);
browser.addEventListener(
"pagetitlechanged",
() => gBrowser.updateTitlebar(),
true
);
// This window does double duty. If window.arguments[0] is a string, it's
// probably being called by browser.identity.launchWebAuthFlowInParent.
// Otherwise, it's probably being called by browser.windows.create, with an
// array of URLs to open in tabs. We'll only attempt to open the first,
// which is consistent with Firefox behaviour.
if (typeof window.arguments[0] == "string") {
MailE10SUtils.loadURI(browser, window.arguments[0]);
} else {
const createData = window.arguments[1].wrappedJSObject;
const tabParams = createData.tabs[0].tabParams;
const url = new URL(tabParams.url);
// moz-extension://s urls default to allowScriptsToClose = true
const defaultScriptsToClose = url.protocol == "moz-extension:";
if (createData.allowScriptsToClose ?? defaultScriptsToClose) {
browser.setAttribute("allowscriptstoclose", "true");
}
if (tabParams.userContextId) {
browser.setAttribute("usercontextid", tabParams.userContextId);
// The usercontextid is only read on frame creation, so recreate it.
browser.replaceWith(browser);
}
ExtensionParent.apiManager.emit("extension-browser-inserted", browser);
MailE10SUtils.loadURI(browser, tabParams.url);
}
}
// Fake it 'til you make it.
var gBrowser = {
get canGoBack() {
return this.selectedBrowser.canGoBack;
},
get canGoForward() {
return this.selectedBrowser.canGoForward;
},
goForward(requireUserInteraction) {
return this.selectedBrowser.goForward(requireUserInteraction);
},
goBack(requireUserInteraction) {
return this.selectedBrowser.goBack(requireUserInteraction);
},
get selectedBrowser() {
return document.getElementById("requestFrame");
},
_getAndMaybeCreateDateTimePickerPanel() {
return this.selectedBrowser.dateTimePicker;
},
get webNavigation() {
return this.selectedBrowser.webNavigation;
},
async updateTitlebar() {
let docTitle =
browser.browsingContext?.currentWindowGlobal?.documentTitle?.trim() || "";
if (!docTitle) {
// If the document title is blank, use the default title.
docTitle = await document.l10n.formatValue(
"extension-popup-default-title"
);
} else {
// Let l10n handle the addition of separator and modifier.
docTitle = await document.l10n.formatValue("extension-popup-title", {
title: docTitle,
});
}
// Add preface, if defined.
const docElement = document.documentElement;
if (docElement.hasAttribute("titlepreface")) {
docTitle = docElement.getAttribute("titlepreface") + docTitle;
}
document.title = docTitle;
},
getTabForBrowser() {
return null;
},
};
this.__defineGetter__("browser", getBrowser);
function getBrowser() {
return gBrowser.selectedBrowser;
}
var gBrowserInit = {
onDOMContentLoaded() {
// This needs setting up before we create the first remote browser.
window.docShell.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIAppWindow).XULBrowserWindow = window.XULBrowserWindow;
window.tryToClose = () => {
if (window.onclose()) {
window.close();
}
};
window.onclose = () => {
const { permitUnload } = gBrowser.selectedBrowser.permitUnload();
return permitUnload;
};
window.browserDOMWindow = new nsBrowserAccess();
const initiallyFocusedElement = document.commandDispatcher.focusedElement;
const promise = gBrowser.selectedBrowser.isRemoteBrowser
? Promise.withResolvers().promise
: Promise.resolve();
contentProgress.addListener({
onStateChange(browser, webProgress, request, stateFlags, statusCode) {
if (!webProgress.isTopLevel) {
return;
}
let status;
if (stateFlags & Ci.nsIWebProgressListener.STATE_IS_WINDOW) {
if (stateFlags & Ci.nsIWebProgressListener.STATE_START) {
status = "loading";
} else if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
status = "complete";
}
} else if (
stateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
statusCode == Cr.NS_BINDING_ABORTED
) {
status = "complete";
}
contentProgress.busy = status == "loading";
},
});
contentProgress.addProgressListenerToBrowser(gBrowser.selectedBrowser);
top.controllers.appendController(contentController);
promise.then(() => {
// If focus didn't move while we were waiting, we're okay to move to
// the browser.
if (
document.commandDispatcher.focusedElement == initiallyFocusedElement
) {
gBrowser.selectedBrowser.focus();
}
loadRequestedUrl();
});
},
isAdoptingTab() {
// Required for compatibility with toolkit's ext-webNavigation.js
return false;
},
};
/**
* @implements {nsIXULBrowserWindow}
*/
var XULBrowserWindow = {
// Used in mailWindows to show the link in the status bar, but popup windows
// do not have one. Do nothing here.
setOverLink() {},
// Called before links are navigated to to allow us to retarget them if needed.
onBeforeLinkTraversal(originalTarget) {
return originalTarget;
},
// Called by BrowserParent::RecvShowTooltip.
showTooltip(xDevPix, yDevPix, tooltip, direction) {
if (
Cc["@mozilla.org/widget/dragservice;1"]
.getService(Ci.nsIDragService)
.getCurrentSession()
) {
return;
}
const elt = document.getElementById("remoteBrowserTooltip");
elt.label = tooltip;
elt.style.direction = direction;
elt.openPopupAtScreen(
xDevPix / window.devicePixelRatio,
yDevPix / window.devicePixelRatio,
false,
null
);
},
// Called by BrowserParent::RecvHideTooltip.
hideTooltip() {
const elt = document.getElementById("remoteBrowserTooltip");
elt.hidePopup();
},
getTabCount() {
// Popup windows have a single tab.
return 1;
},
};
/**
* Combines all nsIWebProgress notifications from all content browsers in this
* window and reports them to the registered listeners.
*
* @see WindowTracker (ext-mail.js)
* @see StatusListener, WindowTrackerBase (ext-tabs-base.js)
*/
var contentProgress = {
_listeners: new Set(),
busy: false,
addListener(listener) {
this._listeners.add(listener);
},
removeListener(listener) {
this._listeners.delete(listener);
},
callListeners(method, args) {
for (const listener of this._listeners.values()) {
if (method in listener) {
try {
listener[method](...args);
} catch (e) {
console.error(e);
}
}
}
},
/**
* Ensure that `browser` has a ProgressListener attached to it.
*
* @param {Browser} browser
*/
addProgressListenerToBrowser(browser) {
if (browser?.webProgress && !browser._progressListener) {
browser._progressListener = new contentProgress.ProgressListener(browser);
browser.webProgress.addProgressListener(
browser._progressListener,
Ci.nsIWebProgress.NOTIFY_ALL
);
}
},
// @implements {nsIWebProgressListener}
// @implements {nsIWebProgressListener2}
ProgressListener: class {
QueryInterface = ChromeUtils.generateQI([
"nsIWebProgressListener",
"nsIWebProgressListener2",
"nsISupportsWeakReference",
]);
constructor(browser) {
this.browser = browser;
}
callListeners(method, args) {
args.unshift(this.browser);
contentProgress.callListeners(method, args);
}
onProgressChange(...args) {
this.callListeners("onProgressChange", args);
}
onProgressChange64(...args) {
this.callListeners("onProgressChange64", args);
}
onLocationChange(...args) {
this.callListeners("onLocationChange", args);
}
onStateChange(...args) {
this.callListeners("onStateChange", args);
}
onStatusChange(...args) {
this.callListeners("onStatusChange", args);
}
onSecurityChange(...args) {
this.callListeners("onSecurityChange", args);
}
onContentBlockingEvent(...args) {
this.callListeners("onContentBlockingEvent", args);
}
onRefreshAttempted(...args) {
return this.callListeners("onRefreshAttempted", args);
}
},
};
// The listener of DOMContentLoaded must be set on window, rather than
// document, because the window can go away before the event is fired.
// In that case, we don't want to initialize anything, otherwise we
// may be leaking things because they will never be destroyed after.
window.addEventListener(
"DOMContentLoaded",
gBrowserInit.onDOMContentLoaded.bind(gBrowserInit),
{ once: true }
);
|