Bug 1833753 [wpt PR 40065] - Allow newly-added test to also pass when mutation events...
[gecko.git] / toolkit / actors / BackgroundThumbnailsChild.sys.mjs
blob3af51b3b80b3348483d86c7b16b747951727d42f
1 /* vim: set ts=2 sw=2 sts=2 et tw=80: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 const lazy = {};
8 ChromeUtils.defineESModuleGetters(lazy, {
9   PageThumbUtils: "resource://gre/modules/PageThumbUtils.sys.mjs",
10 });
12 // NOTE: Copied from nsSandboxFlags.h
13 /**
14  * This flag prevents content from creating new auxiliary browsing contexts,
15  * e.g. using the target attribute, or the window.open() method.
16  */
17 const SANDBOXED_AUXILIARY_NAVIGATION = 0x2;
19 export class BackgroundThumbnailsChild extends JSWindowActorChild {
20   receiveMessage(message) {
21     switch (message.name) {
22       case "Browser:Thumbnail:ContentInfo": {
23         if (
24           message.data.isImage ||
25           this.contentWindow.ImageDocument.isInstance(this.document)
26         ) {
27           // To avoid sending additional messages between processes, we return
28           // the image data directly with the size info.
29           return lazy.PageThumbUtils.createImageThumbnailCanvas(
30             this.contentWindow,
31             this.document.location,
32             message.data.targetWidth,
33             message.data.backgroundColor
34           );
35         }
37         let [width, height] = lazy.PageThumbUtils.getContentSize(
38           this.contentWindow
39         );
40         return { width, height };
41       }
43       case "Browser:Thumbnail:LoadURL": {
44         let docShell = this.docShell.QueryInterface(Ci.nsIWebNavigation);
46         // We want a low network priority for this service - lower than b/g tabs
47         // etc - so set it to the lowest priority available.
48         docShell
49           .QueryInterface(Ci.nsIDocumentLoader)
50           .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority =
51           Ci.nsISupportsPriority.PRIORITY_LOWEST;
53         docShell.allowMedia = false;
54         docShell.allowPlugins = false;
55         docShell.allowContentRetargeting = false;
56         let defaultFlags =
57           Ci.nsIRequest.LOAD_ANONYMOUS |
58           Ci.nsIRequest.LOAD_BYPASS_CACHE |
59           Ci.nsIRequest.INHIBIT_CACHING |
60           Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_HISTORY;
61         docShell.defaultLoadFlags = defaultFlags;
62         this.browsingContext.sandboxFlags |= SANDBOXED_AUXILIARY_NAVIGATION;
63         docShell.useTrackingProtection = true;
65         // Get the document to force a content viewer to be created, otherwise
66         // the first load can fail.
67         if (!this.document) {
68           return false;
69         }
71         let loadURIOptions = {
72           // Bug 1498603 verify usages of systemPrincipal here
73           triggeringPrincipal:
74             Services.scriptSecurityManager.getSystemPrincipal(),
75           loadFlags: Ci.nsIWebNavigation.LOAD_FLAGS_STOP_CONTENT,
76         };
77         try {
78           docShell.loadURI(
79             Services.io.newURI(message.data.url),
80             loadURIOptions
81           );
82         } catch (ex) {
83           return false;
84         }
86         return true;
87       }
88     }
90     return undefined;
91   }
93   handleEvent(event) {
94     if (event.type == "DOMDocElementInserted") {
95       // Arrange to prevent (most) popup dialogs for this window - popups done
96       // in the parent (eg, auth) aren't prevented, but alert() etc are.
97       // disableDialogs only works on the current inner window, so it has
98       // to be called every page load, but before scripts run.
99       this.contentWindow.windowUtils.disableDialogs();
100     }
101   }