CanonicalURLParent.sys.mjs (2099B)
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.defineLazyGetter(lazy, "logConsole", function () { 8 return console.createInstance({ 9 prefix: "CanonicalURL", 10 maxLogLevel: Services.prefs.getBoolPref("browser.tabs.notes.debug", false) 11 ? "Debug" 12 : "Warn", 13 }); 14 }); 15 16 /** 17 * Receives canonical URL identifications from CanonicalURLChild and dispatches 18 * event notifications on the <browser>. 19 */ 20 export class CanonicalURLParent extends JSWindowActorParent { 21 /** 22 * Called when a message is received from the content process. 23 * 24 * @param {ReceiveMessageArgument} msg 25 */ 26 receiveMessage(msg) { 27 switch (msg.name) { 28 case "CanonicalURL:Identified": 29 { 30 const browser = this.browsingContext?.embedderElement; 31 32 // If we don't have a browser then it went away before we could record, 33 // so we don't know where the data came from. 34 if (!browser) { 35 lazy.logConsole.debug( 36 "CanonicalURL:Identified: reject due to missing browser" 37 ); 38 return; 39 } 40 41 if (!browser.ownerGlobal.gBrowser?.getTabForBrowser(browser)) { 42 lazy.logConsole.debug( 43 "CanonicalURL:Identified: reject due to the browser not being a tab browser" 44 ); 45 return; 46 } 47 48 const { canonicalUrl, canonicalUrlSources } = msg.data; 49 50 let event = new browser.ownerGlobal.CustomEvent( 51 "CanonicalURL:Identified", 52 { 53 bubbles: true, 54 cancelable: false, 55 detail: { 56 canonicalUrl, 57 canonicalUrlSources, 58 }, 59 } 60 ); 61 browser.dispatchEvent(event); 62 lazy.logConsole.info("CanonicalURL:Identified", { 63 canonicalUrl, 64 canonicalUrlSources, 65 }); 66 } 67 break; 68 } 69 } 70 }