From 0d839af74446d979237ee7e8778c77eb23c643a3 Mon Sep 17 00:00:00 2001 From: Neil Deakin Date: Thu, 31 Oct 2019 10:11:40 +0000 Subject: [PATCH] Bug 1590752, convert ContentMetaHandler.jsm into JSWindowActors, r=mconley Differential Revision: https://phabricator.services.mozilla.com/D50584 --HG-- rename : browser/modules/ContentMetaHandler.jsm => browser/actors/ContentMetaChild.jsm extra : moz-landing-system : lando --- .../ContentMetaChild.jsm} | 55 ++++++++++--------- browser/actors/ContentMetaParent.jsm | 30 ++++++++++ browser/actors/moz.build | 2 + browser/base/content/browser.js | 11 ---- browser/base/content/content.js | 10 ---- .../performance/browser_startup_content.js | 1 - browser/components/BrowserGlue.jsm | 14 +++++ browser/modules/moz.build | 1 - 8 files changed, 76 insertions(+), 48 deletions(-) rename browser/{modules/ContentMetaHandler.jsm => actors/ContentMetaChild.jsm} (84%) create mode 100644 browser/actors/ContentMetaParent.jsm diff --git a/browser/modules/ContentMetaHandler.jsm b/browser/actors/ContentMetaChild.jsm similarity index 84% rename from browser/modules/ContentMetaHandler.jsm rename to browser/actors/ContentMetaChild.jsm index 8d8ebc18787a..9a3ebff0283d 100644 --- a/browser/modules/ContentMetaHandler.jsm +++ b/browser/actors/ContentMetaChild.jsm @@ -3,6 +3,9 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; + +const EXPORTED_SYMBOLS = ["ContentMetaChild"]; + const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); const { XPCOMUtils } = ChromeUtils.import( "resource://gre/modules/XPCOMUtils.jsm" @@ -75,32 +78,34 @@ function checkLoadURIStr(aURL) { return true; } -var EXPORTED_SYMBOLS = ["ContentMetaHandler"]; - /* * This listens to DOMMetaAdded events and collects relevant metadata about the * meta tag received. Then, it sends the metadata gathered from the meta tags * and the url of the page as it's payload to be inserted into moz_places. */ +class ContentMetaChild extends JSWindowActorChild { + constructor() { + super(); -var ContentMetaHandler = { - init(chromeGlobal) { - // Store a locally-scoped (for this chromeGlobal) mapping of the best - // description and preview image collected so far for a given URL - const metaTags = new Map(); - chromeGlobal.addEventListener("DOMMetaAdded", event => { - const metaTag = event.originalTarget; - const window = metaTag.ownerGlobal; + // Store a mapping of the best description and preview + // image collected so far for a given URL. + this.metaTags = new Map(); + } - // If there's no meta tag, or we're in a sub-frame, ignore this - if (!metaTag || !metaTag.ownerDocument || window != window.top) { - return; - } - this.handleMetaTag(metaTag, chromeGlobal, metaTags); - }); - }, + handleEvent(event) { + if (event.type != "DOMMetaAdded") { + return; + } + + const metaTag = event.originalTarget; + const window = metaTag.ownerGlobal; + + // If there's no meta tag, ignore this. Also verify that the window + // matches just to be safe. + if (!metaTag || !metaTag.ownerDocument || window != this.contentWindow) { + return; + } - handleMetaTag(metaTag, chromeGlobal, metaTags) { const url = metaTag.ownerDocument.documentURI; let name = metaTag.name; @@ -111,7 +116,7 @@ var ContentMetaHandler = { let tag = name || prop; - const entry = metaTags.get(url) || { + const entry = this.metaTags.get(url) || { description: { value: null, currMaxScore: -1 }, image: { value: null, currMaxScore: -1 }, timeout: null, @@ -144,8 +149,8 @@ var ContentMetaHandler = { return; } - if (!metaTags.has(url)) { - metaTags.set(url, entry); + if (!this.metaTags.has(url)) { + this.metaTags.set(url, entry); } if (entry.timeout) { @@ -159,7 +164,7 @@ var ContentMetaHandler = { entry.timeout = null; // Save description and preview image to moz_places - chromeGlobal.sendAsyncMessage("Meta:SetPageInfo", { + this.sendAsyncMessage("Meta:SetPageInfo", { url, description: entry.description.value, previewImageURL: entry.image.value, @@ -173,11 +178,11 @@ var ContentMetaHandler = { Services.telemetry .getHistogramById("PAGE_METADATA_SIZE") .add(metadataSize); - metaTags.delete(url); + this.metaTags.delete(url); }, TIMEOUT_DELAY, Ci.nsITimer.TYPE_ONE_SHOT ); } - }, -}; + } +} diff --git a/browser/actors/ContentMetaParent.jsm b/browser/actors/ContentMetaParent.jsm new file mode 100644 index 000000000000..5c839378f0b0 --- /dev/null +++ b/browser/actors/ContentMetaParent.jsm @@ -0,0 +1,30 @@ +/* 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/. */ + +"use strict"; + +const EXPORTED_SYMBOLS = ["ContentMetaParent"]; + +class ContentMetaParent extends JSWindowActorParent { + receiveMessage(message) { + if (message.name == "Meta:SetPageInfo") { + let browser = this.manager.browsingContext.top.embedderElement; + if (browser) { + if (browser.outerBrowser) { + // Responsive design mode check + browser = browser.outerBrowser; + } + + let gBrowser = browser.ownerGlobal.gBrowser; + if (gBrowser) { + gBrowser.setPageInfo( + message.data.url, + message.data.description, + message.data.previewImageURL + ); + } + } + } + } +} diff --git a/browser/actors/moz.build b/browser/actors/moz.build index 2f5f7c58ca0a..09f6b02b73d7 100644 --- a/browser/actors/moz.build +++ b/browser/actors/moz.build @@ -28,6 +28,8 @@ FINAL_TARGET_FILES.actors += [ 'BrowserTabChild.jsm', 'BrowserTabParent.jsm', 'ClickHandlerChild.jsm', + 'ContentMetaChild.jsm', + 'ContentMetaParent.jsm', 'ContentSearchChild.jsm', 'ContextMenuChild.jsm', 'ContextMenuParent.jsm', diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index 4996aecf723f..9831d094d79f 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -4155,7 +4155,6 @@ const DOMEventHandler = { mm.addMessageListener("Link:SetIcon", this); mm.addMessageListener("Link:SetFailedIcon", this); mm.addMessageListener("Link:AddSearch", this); - mm.addMessageListener("Meta:SetPageInfo", this); }, receiveMessage(aMsg) { @@ -4186,19 +4185,9 @@ const DOMEventHandler = { case "Link:AddSearch": this.addSearch(aMsg.target, aMsg.data.engine, aMsg.data.url); break; - - case "Meta:SetPageInfo": - this.setPageInfo(aMsg.data); - break; } }, - setPageInfo(aData) { - const { url, description, previewImageURL } = aData; - gBrowser.setPageInfo(url, description, previewImageURL); - return true; - }, - setPendingIcon(aBrowser) { let tab = gBrowser.getTabForBrowser(aBrowser); if (tab.hasAttribute("busy")) { diff --git a/browser/base/content/content.js b/browser/base/content/content.js index 79921481985a..7901ebd664dd 100644 --- a/browser/base/content/content.js +++ b/browser/base/content/content.js @@ -9,15 +9,5 @@ /* eslint-env mozilla/frame-script */ /* eslint no-unused-vars: ["error", {args: "none"}] */ -var { XPCOMUtils } = ChromeUtils.import( - "resource://gre/modules/XPCOMUtils.jsm" -); - -XPCOMUtils.defineLazyModuleGetters(this, { - ContentMetaHandler: "resource:///modules/ContentMetaHandler.jsm", -}); - -ContentMetaHandler.init(this); - // This is a temporary hack to prevent regressions (bug 1471327). void content; diff --git a/browser/base/content/test/performance/browser_startup_content.js b/browser/base/content/test/performance/browser_startup_content.js index 4b3db5e6d68d..9bb6d3338588 100644 --- a/browser/base/content/test/performance/browser_startup_content.js +++ b/browser/base/content/test/performance/browser_startup_content.js @@ -42,7 +42,6 @@ const whitelist = { // Browser front-end "resource:///actors/AboutReaderChild.jsm", "resource:///actors/BrowserTabChild.jsm", - "resource:///modules/ContentMetaHandler.jsm", "resource:///actors/LinkHandlerChild.jsm", "resource:///actors/SearchTelemetryChild.jsm", "resource://gre/actors/AutoCompleteChild.jsm", diff --git a/browser/components/BrowserGlue.jsm b/browser/components/BrowserGlue.jsm index c77bc2a46429..4d79c3ff6577 100644 --- a/browser/components/BrowserGlue.jsm +++ b/browser/components/BrowserGlue.jsm @@ -46,6 +46,20 @@ let ACTORS = { }, }, + // Collects description and icon information from meta tags. + ContentMeta: { + parent: { + moduleURI: "resource:///actors/ContentMetaParent.jsm", + }, + + child: { + moduleURI: "resource:///actors/ContentMetaChild.jsm", + events: { + DOMMetaAdded: {}, + }, + }, + }, + ContextMenu: { parent: { moduleURI: "resource:///actors/ContextMenuParent.jsm", diff --git a/browser/modules/moz.build b/browser/modules/moz.build index 0d295ac99a05..a52af9c8bb74 100644 --- a/browser/modules/moz.build +++ b/browser/modules/moz.build @@ -138,7 +138,6 @@ EXTRA_JS_MODULES += [ 'BrowserWindowTracker.jsm', 'ContentClick.jsm', 'ContentCrashHandlers.jsm', - 'ContentMetaHandler.jsm', 'ContentObservers.js', 'ContentSearch.jsm', 'Discovery.jsm',