From 5de2d8377f1b6675ffd742186b200320ef6346bf Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Thu, 6 Apr 2017 21:42:21 -0500 Subject: [PATCH] Bug 1353045 - Remove SDK usage from RDM. r=pbro MozReview-Commit-ID: I2dodBb9NHh --HG-- extra : rebase_source : 0ce62635307c19e1d449f2c773bacaaf998a5970 --- .../responsive.html/actions/screenshot.js | 2 +- .../responsive.html/components/browser.js | 2 +- devtools/client/responsive.html/index.js | 9 ++-- devtools/client/responsive.html/manager.js | 7 ++- .../test/browser/browser_menu_item_01.js | 8 +-- .../test/browser/browser_menu_item_02.js | 6 +-- .../responsive.html/test/browser/head.js | 5 +- devtools/client/responsive.html/utils/css.js | 18 +++++++ .../client/responsive.html/utils/moz.build | 2 + .../client/responsive.html/utils/window.js | 51 +++++++++++++++++++ 10 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 devtools/client/responsive.html/utils/css.js create mode 100644 devtools/client/responsive.html/utils/window.js diff --git a/devtools/client/responsive.html/actions/screenshot.js b/devtools/client/responsive.html/actions/screenshot.js index 42bc234e0a8d..2ef4171b0ba9 100644 --- a/devtools/client/responsive.html/actions/screenshot.js +++ b/devtools/client/responsive.html/actions/screenshot.js @@ -12,7 +12,7 @@ const { } = require("./index"); const { getFormatStr } = require("../utils/l10n"); -const { getToplevelWindow } = require("sdk/window/utils"); +const { getToplevelWindow } = require("../utils/window"); const { Task: { spawn } } = require("devtools/shared/task"); const e10s = require("../utils/e10s"); const Services = require("Services"); diff --git a/devtools/client/responsive.html/components/browser.js b/devtools/client/responsive.html/components/browser.js index 257580f59ad5..56259e177ab3 100644 --- a/devtools/client/responsive.html/components/browser.js +++ b/devtools/client/responsive.html/components/browser.js @@ -8,7 +8,7 @@ const { Task } = require("devtools/shared/task"); const flags = require("devtools/shared/flags"); -const { getToplevelWindow } = require("sdk/window/utils"); +const { getToplevelWindow } = require("../utils/window"); const { DOM: dom, createClass, addons, PropTypes } = require("devtools/client/shared/vendor/react"); diff --git a/devtools/client/responsive.html/index.js b/devtools/client/responsive.html/index.js index bb3ca7d252df..340de569f3f7 100644 --- a/devtools/client/responsive.html/index.js +++ b/devtools/client/responsive.html/index.js @@ -15,7 +15,7 @@ const { require } = BrowserLoader({ }); const { Task } = require("devtools/shared/task"); const Telemetry = require("devtools/client/shared/telemetry"); -const { loadSheet } = require("sdk/stylesheet/utils"); +const { loadAgentSheet } = require("./utils/css"); const { createFactory, createElement } = require("devtools/client/shared/vendor/react"); @@ -42,9 +42,10 @@ let bootstrap = { init: Task.async(function* () { // Load a special UA stylesheet to reset certain styles such as dropdown // lists. - loadSheet(window, - "resource://devtools/client/responsive.html/responsive-ua.css", - "agent"); + loadAgentSheet( + window, + "resource://devtools/client/responsive.html/responsive-ua.css" + ); this.telemetry.toolOpened("responsive"); let store = this.store = Store(); let provider = createElement(Provider, { store }, App()); diff --git a/devtools/client/responsive.html/manager.js b/devtools/client/responsive.html/manager.js index 8f37e6812c2a..e99cae787d65 100644 --- a/devtools/client/responsive.html/manager.js +++ b/devtools/client/responsive.html/manager.js @@ -8,8 +8,7 @@ const { Ci } = require("chrome"); const promise = require("promise"); const { Task } = require("devtools/shared/task"); const EventEmitter = require("devtools/shared/event-emitter"); -const { getOwnerWindow } = require("sdk/tabs/utils"); -const { startup } = require("sdk/window/helpers"); +const { startup } = require("./utils/window"); const message = require("./utils/message"); const { swapToInnerBrowser } = require("./browser/swap"); const { EmulationFront } = require("devtools/shared/fronts/emulation"); @@ -145,7 +144,7 @@ const ResponsiveUIManager = exports.ResponsiveUIManager = { * @return boolean */ isActiveForWindow(window) { - return [...this.activeTabs.keys()].some(t => getOwnerWindow(t) === window); + return [...this.activeTabs.keys()].some(t => t.ownerGlobal === window); }, /** @@ -209,7 +208,7 @@ const ResponsiveUIManager = exports.ResponsiveUIManager = { } }, - setMenuCheckFor: Task.async(function* (tab, window = getOwnerWindow(tab)) { + setMenuCheckFor: Task.async(function* (tab, window = tab.ownerGlobal) { yield startup(window); let menu = window.document.getElementById("menu_responsiveUI"); diff --git a/devtools/client/responsive.html/test/browser/browser_menu_item_01.js b/devtools/client/responsive.html/test/browser/browser_menu_item_01.js index 8e1c1c4cdb3c..26e6dc287f9b 100644 --- a/devtools/client/responsive.html/test/browser/browser_menu_item_01.js +++ b/devtools/client/responsive.html/test/browser/browser_menu_item_01.js @@ -7,18 +7,18 @@ const TEST_URL = "data:text/html;charset=utf-8,"; -const tabUtils = require("sdk/tabs/utils"); -const { startup } = require("sdk/window/helpers"); +const { startup } = require("devtools/client/responsive.html/utils/window"); const activateTab = (tab) => new Promise(resolve => { - let { tabContainer } = tabUtils.getOwnerWindow(tab).gBrowser; + let { gBrowser } = tab.ownerGlobal; + let { tabContainer } = gBrowser; tabContainer.addEventListener("TabSelect", function listener({type}) { tabContainer.removeEventListener(type, listener); resolve(); }); - tabUtils.activateTab(tab); + gBrowser.selectedTab = tab; }); const isMenuChecked = () => { diff --git a/devtools/client/responsive.html/test/browser/browser_menu_item_02.js b/devtools/client/responsive.html/test/browser/browser_menu_item_02.js index 166ecb8ae506..4315525618a3 100644 --- a/devtools/client/responsive.html/test/browser/browser_menu_item_02.js +++ b/devtools/client/responsive.html/test/browser/browser_menu_item_02.js @@ -7,8 +7,6 @@ const TEST_URL = "data:text/html;charset=utf-8,"; -const { getMostRecentBrowserWindow } = require("sdk/window/utils"); - const isMenuCheckedFor = ({document}) => { let menu = document.getElementById("menu_responsiveUI"); return menu.getAttribute("checked") === "true"; @@ -22,7 +20,7 @@ add_task(function* () { function* (browser) { let tab = gBrowser.getTabForBrowser(browser); - is(window1, getMostRecentBrowserWindow(), + is(window1, Services.wm.getMostRecentWindow("navigator:browser"), "The new window is the active one"); ok(!isMenuCheckedFor(window1), @@ -41,7 +39,7 @@ add_task(function* () { yield BrowserTestUtils.closeWindow(window1); - is(window, getMostRecentBrowserWindow(), + is(window, Services.wm.getMostRecentWindow("navigator:browser"), "The original window is the active one"); ok(!isMenuCheckedFor(window), diff --git a/devtools/client/responsive.html/test/browser/head.js b/devtools/client/responsive.html/test/browser/head.js index c458bccf29e1..322c7cb62b35 100644 --- a/devtools/client/responsive.html/test/browser/head.js +++ b/devtools/client/responsive.html/test/browser/head.js @@ -36,7 +36,6 @@ const TEST_URI_ROOT = "http://example.com/browser/devtools/client/responsive.htm const OPEN_DEVICE_MODAL_VALUE = "OPEN_DEVICE_MODAL"; const { _loadPreferredDevices } = require("devtools/client/responsive.html/actions/devices"); -const { getOwnerWindow } = require("sdk/tabs/utils"); const asyncStorage = require("devtools/shared/async-storage"); const { addDevice, removeDevice } = require("devtools/client/shared/devices"); @@ -72,7 +71,7 @@ const { ResponsiveUIManager } = require("resource://devtools/client/responsivede var openRDM = Task.async(function* (tab) { info("Opening responsive design mode"); let manager = ResponsiveUIManager; - let ui = yield manager.openIfNeeded(getOwnerWindow(tab), tab); + let ui = yield manager.openIfNeeded(tab.ownerGlobal, tab); info("Responsive design mode opened"); return { ui, manager }; }); @@ -83,7 +82,7 @@ var openRDM = Task.async(function* (tab) { var closeRDM = Task.async(function* (tab, options) { info("Closing responsive design mode"); let manager = ResponsiveUIManager; - yield manager.closeIfNeeded(getOwnerWindow(tab), tab, options); + yield manager.closeIfNeeded(tab.ownerGlobal, tab, options); info("Responsive design mode closed"); }); diff --git a/devtools/client/responsive.html/utils/css.js b/devtools/client/responsive.html/utils/css.js new file mode 100644 index 000000000000..cbea686f3ca3 --- /dev/null +++ b/devtools/client/responsive.html/utils/css.js @@ -0,0 +1,18 @@ +/* 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 { getDOMWindowUtils } = require("./window"); + +/** + * Synchronously loads an agent style sheet from `uri` and adds it to the list of + * additional style sheets of the document. The sheets added takes effect immediately, + * and only on the document of the `window` given. + */ +function loadAgentSheet(window, url) { + let winUtils = getDOMWindowUtils(window); + winUtils.loadSheetUsingURIString(url, winUtils.AGENT_SHEET); +} +exports.loadAgentSheet = loadAgentSheet; diff --git a/devtools/client/responsive.html/utils/moz.build b/devtools/client/responsive.html/utils/moz.build index 98af886d5c11..56be03cfb13c 100644 --- a/devtools/client/responsive.html/utils/moz.build +++ b/devtools/client/responsive.html/utils/moz.build @@ -5,7 +5,9 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. DevToolsModules( + 'css.js', 'e10s.js', 'l10n.js', 'message.js', + 'window.js', ) diff --git a/devtools/client/responsive.html/utils/window.js b/devtools/client/responsive.html/utils/window.js new file mode 100644 index 000000000000..de7b80c62f09 --- /dev/null +++ b/devtools/client/responsive.html/utils/window.js @@ -0,0 +1,51 @@ +/* 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 { Ci } = require("chrome"); +const Services = require("Services"); + +/** + * Returns the `nsIDOMWindow` toplevel window for any child/inner window + */ +function getToplevelWindow(window) { + return window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsIDocShellTreeItem) + .rootTreeItem + .QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindow); +} +exports.getToplevelWindow = getToplevelWindow; + +function getDOMWindowUtils(window) { + return window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindowUtils); +} +exports.getDOMWindowUtils = getDOMWindowUtils; + +/** + * Check if the given browser window has finished the startup. + * @params {nsIDOMWindow} window + */ +const isStartupFinished = (window) => + window.gBrowserInit && + window.gBrowserInit.delayedStartupFinished; + +function startup(window) { + return new Promise(resolve => { + if (isStartupFinished(window)) { + resolve(window); + return; + } + Services.obs.addObserver(function listener({ subject }) { + if (subject === window) { + Services.obs.removeObserver(listener, "browser-delayed-startup-finished"); + resolve(window); + } + }, "browser-delayed-startup-finished", false); + }); +} +exports.startup = startup;