Bug 1891710: part 2) Enable <Element-outerHTML.html> WPT for Trusted Types. r=smaug
[gecko.git] / mobile / android / actors / GeckoViewContentParent.sys.mjs
blobe666040b5eeb75b147b9798b65c16fb2b1fb56a4
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";
6 import { GeckoViewActorParent } from "resource://gre/modules/GeckoViewActorParent.sys.mjs";
8 const lazy = {};
10 ChromeUtils.defineESModuleGetters(lazy, {
11   SessionHistory: "resource://gre/modules/sessionstore/SessionHistory.sys.mjs",
12 });
14 const { debug, warn } = GeckoViewUtils.initLogging("GeckoViewContentParent");
16 export class GeckoViewContentParent extends GeckoViewActorParent {
17   async collectState() {
18     return this.sendQuery("CollectSessionState");
19   }
21   async containsFormData() {
22     return this.sendQuery("ContainsFormData");
23   }
25   restoreState({ history, switchId, formdata, scrolldata }) {
26     if (Services.appinfo.sessionHistoryInParent) {
27       const { browsingContext } = this.browser;
28       lazy.SessionHistory.restoreFromParent(
29         browsingContext.sessionHistory,
30         history
31       );
33       // TODO Bug 1648158 this should include scroll, form history, etc
34       return SessionStoreUtils.initializeRestore(
35         browsingContext,
36         SessionStoreUtils.constructSessionStoreRestoreData()
37       );
38     }
40     // Restoring is made of two parts. First we need to restore the history
41     // of the tab and navigating to the current page, after the page
42     // navigates to the current page we need to restore the state of the
43     // page (scroll position, form data, etc).
44     //
45     // We can't do everything in one step inside the child actor because
46     // the actor is recreated when navigating, so we need to keep the state
47     // on the parent side until we navigate.
48     this.sendAsyncMessage("RestoreHistoryAndNavigate", {
49       history,
50       switchId,
51     });
53     if (!formdata && !scrolldata) {
54       return null;
55     }
57     const progressFilter = Cc[
58       "@mozilla.org/appshell/component/browser-status-filter;1"
59     ].createInstance(Ci.nsIWebProgress);
61     const { browser } = this;
62     const progressListener = {
63       QueryInterface: ChromeUtils.generateQI(["nsIWebProgressListener"]),
65       onLocationChange(aWebProgress) {
66         if (!aWebProgress.isTopLevel) {
67           return;
68         }
69         // The actor might get recreated between navigations so we need to
70         // query it again for the up-to-date instance.
71         browser.browsingContext.currentWindowGlobal
72           .getActor("GeckoViewContent")
73           .sendAsyncMessage("RestoreSessionState", { formdata, scrolldata });
74         progressFilter.removeProgressListener(this);
75         browser.removeProgressListener(progressFilter);
76       },
77     };
79     const flags = Ci.nsIWebProgress.NOTIFY_LOCATION;
80     progressFilter.addProgressListener(progressListener, flags);
81     browser.addProgressListener(progressFilter, flags);
82     return null;
83   }