Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Bug 695143: Implement getTab and getBrowserWindow for getting high-level objects from low-level DOM windows. #880

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/sdk/deprecated/traits.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ function Composition(trait) {
return self._public;
}
defineProperties(Trait, {
prototype: { value: freeze(create(TraitProto, {
prototype: { value: create(TraitProto, {
constructor: { value: constructor, writable: true }
}))}, // writable is `true` to avoid getters in custom ES5
})}, // writable is `true` to avoid getters in custom ES5
displayName: { value: (trait.constructor || constructor).name },
compose: { value: compose, enumerable: true },
override: { value: override, enumerable: true },
Expand Down
19 changes: 18 additions & 1 deletion lib/sdk/tabs/tab-firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';

const { Ci } = require("chrome");
const { Trait } = require("../deprecated/traits");
const { EventEmitter } = require("../deprecated/events");
const { defer } = require("../lang/functional");
const { EVENTS } = require("./events");
const { getThumbnailURIForWindow } = require("../content/thumbnail");
const { getFaviconURIForLocation } = require("../io/data");
const { activateTab, getOwnerWindow, getBrowserForTab, getTabTitle, setTabTitle,
getTabURL, setTabURL, getTabContentType, getTabId } = require('./utils');
getTabURL, setTabURL, getTabContentType, getTabId, getTab } = require('./utils');
const { getOwnerWindow: getPBOwnerWindow } = require('../private-browsing/window/utils');
const viewNS = require('sdk/core/namespace').ns();
const { getDOMWindow } = require("sdk/window/utils");

// Array of the inner instances of all the wrapped tabs.
const TABS = [];
Expand Down Expand Up @@ -241,3 +243,18 @@ function Tab(options, existingOnly) {
}
Tab.prototype = TabTrait.prototype;
exports.Tab = Tab;

getTab.define(function(obj) {
if (obj instanceof Ci.nsIDOMWindow) {
for (let tab of TABS) {
if (tab._browser.contentWindow == obj)
return tab;
}
}

return null;
});

getDOMWindow.define(TabTrait, function(tab) {
return tab._browser.contentWindow;
});
2 changes: 2 additions & 0 deletions lib/sdk/tabs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { Ci } = require('chrome');
const { defer } = require("../lang/functional");
const { windows, isBrowser } = require('../window/utils');
const { isPrivateBrowsingSupported } = require('../self');
const { method } = require('method/core');

// Bug 834961: ignore private windows when they are not supported
function getWindows() windows(null, { includePrivate: isPrivateBrowsingSupported });
Expand Down Expand Up @@ -308,3 +309,4 @@ function getTabForBrowser(browser) {
}
exports.getTabForBrowser = getTabForBrowser;

exports.getTab = method("getTab");
17 changes: 13 additions & 4 deletions lib/sdk/window/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { Cc, Ci } = require('chrome');
const array = require('../util/array');
const observers = require('../deprecated/observer-service');
const { defer } = require('sdk/core/promise');
const { method } = require('method/core');

const windowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].
getService(Ci.nsIWindowWatcher);
Expand Down Expand Up @@ -94,10 +95,16 @@ function getXULWindow(window) {
};
exports.getXULWindow = getXULWindow;

function getDOMWindow(xulWindow) {
return xulWindow.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIDOMWindow);
}
let getDOMWindow = method("getDOMWindow");
getDOMWindow.define(function (obj) {
if (obj instanceof Ci.nsIXULWindow) {
return xulWindow.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIDOMWindow);
}

return null;
})

exports.getDOMWindow = getDOMWindow;

/**
Expand Down Expand Up @@ -331,3 +338,5 @@ function getFrames(window) {
}, [])
}
exports.getFrames = getFrames;

exports.getBrowserWindow = method("getBrowserWindow");
13 changes: 12 additions & 1 deletion lib/sdk/windows/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { Cc, Ci, Cr } = require('chrome'),
{ WindowTabs, WindowTabTracker } = require('./tabs-firefox'),
{ WindowDom } = require('./dom'),
{ WindowLoader } = require('./loader'),
{ isBrowser, getWindowDocShell, windows: windowIterator } = require('../window/utils'),
{ isBrowser, getWindowDocShell, windows: windowIterator, getBrowserWindow, getDOMWindow } = require('../window/utils'),
{ Options } = require('../tabs/common'),
apiUtils = require('../deprecated/api-utils'),
unload = require('../system/unload'),
Expand Down Expand Up @@ -122,6 +122,17 @@ function getRegisteredWindow(chromeWindow) {
return null;
}

getBrowserWindow.define(function(obj) {
if (obj instanceof Ci.nsIDOMWindow)
return getRegisteredWindow(obj);

return null;
});

getDOMWindow.define(BrowserWindow, function(browserWindow) {
return browserWindow._window;
});

/**
* Wrapper for `BrowserWindowTrait`. Creates new instance if wrapper for
* window doesn't exists yet. If wrapper already exists then returns it
Expand Down
21 changes: 20 additions & 1 deletion test/test-window-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const windowUtils = require("sdk/deprecated/window-utils");
const timer = require("sdk/timers");
const { Cc, Ci } = require("chrome");
const { Loader } = require("sdk/test/loader");
const { open, getFrames, getWindowTitle, onFocus } = require('sdk/window/utils');
const { open, getFrames, getWindowTitle, onFocus, getDOMWindow, getMostRecentBrowserWindow, getBrowserWindow } = require('sdk/window/utils');
const { close } = require('sdk/window/helpers');
const { fromIterator: toArray } = require('sdk/util/array');
const { browserWindows } = require('sdk/windows');

const WM = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);

Expand Down Expand Up @@ -301,6 +302,24 @@ exports.testWindowIterator = function(assert, done) {
}, false);
};

exports["test getBrowserWindow"] = function(assert) {
let window = browserWindows.activeWindow;
let chromeWindow = getMostRecentBrowserWindow();

assert.equal(getBrowserWindow(chromeWindow), window, "getBrowserWindow should have worked");
}

exports["test getDOMWindow"] = function(assert) {
let window = browserWindows.activeWindow;
let tab = window.tabs.activeTab;

let chromeWindow = getMostRecentBrowserWindow();
let contentWindow = chromeWindow.content;

assert.equal(getDOMWindow(window), chromeWindow, "getDOMWindow should have worked for a window");
assert.equal(getDOMWindow(tab), contentWindow, "getDOMWindow should have worked for a window");
}

if (require("sdk/system/xul-app").is("Fennec")) {
module.exports = {
"test Unsupported Test": function UnsupportedTest (assert) {
Expand Down