PageDataParent.sys.mjs (1523B)
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/. */ 4 5 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 PageDataService: 9 "moz-src:///browser/components/pagedata/PageDataService.sys.mjs", 10 }); 11 12 /** 13 * Receives messages from PageDataChild and passes them to the PageData service. 14 */ 15 export class PageDataParent extends JSWindowActorParent { 16 #deferredCollection = null; 17 18 /** 19 * Starts data collection in the child process. Returns a promise that 20 * resolves to the page data or null if the page is closed before data 21 * collection completes. 22 * 23 * @returns {Promise<PageData|null>} 24 */ 25 collectPageData() { 26 if (!this.#deferredCollection) { 27 this.#deferredCollection = Promise.withResolvers(); 28 this.sendQuery("PageData:Collect").then( 29 this.#deferredCollection.resolve, 30 this.#deferredCollection.reject 31 ); 32 } 33 34 return this.#deferredCollection.promise; 35 } 36 37 /** 38 * Called when the page is destroyed. 39 */ 40 didDestroy() { 41 this.#deferredCollection?.resolve(null); 42 } 43 44 /** 45 * Called when a message is received from the content process. 46 * 47 * @param {ReceiveMessageArgument} msg 48 * The received message. 49 */ 50 receiveMessage(msg) { 51 switch (msg.name) { 52 case "PageData:DocumentReady": 53 lazy.PageDataService.pageLoaded(this, msg.data.url); 54 break; 55 } 56 } 57 }