diff options
Diffstat (limited to 'tools/addon-sdk-1.12/lib/sdk/windows')
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/dom.js | 33 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/fennec.js | 84 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/firefox.js | 241 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/loader.js | 120 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/observer.js | 52 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/tabs-fennec.js | 190 | ||||
-rw-r--r-- | tools/addon-sdk-1.12/lib/sdk/windows/tabs-firefox.js | 188 |
7 files changed, 908 insertions, 0 deletions
diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/dom.js b/tools/addon-sdk-1.12/lib/sdk/windows/dom.js new file mode 100644 index 0000000..ac2b86b --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/dom.js @@ -0,0 +1,33 @@ +/* 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 { Trait } = require('../deprecated/traits'); +const { getWindowTitle } = require('../window/utils'); +const { getMode } = require('../private-browsing/utils'); + +module.metadata = { + "stability": "unstable" +}; + +const WindowDom = Trait.compose({ + _window: Trait.required, + get title() { + return getWindowTitle(this._window); + }, + close: function close() { + let window = this._window; + if (window) window.close(); + return this._public; + }, + activate: function activate() { + let window = this._window; + if (window) window.focus(); + return this._public; + }, + get isPrivateBrowsing() { + return getMode(this._window); + } +}); +exports.WindowDom = WindowDom; diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/fennec.js b/tools/addon-sdk-1.12/lib/sdk/windows/fennec.js new file mode 100644 index 0000000..a497ea9 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/fennec.js @@ -0,0 +1,84 @@ +/* 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 { Class } = require('../core/heritage'); +const { BrowserWindow } = require('../window/browser'); +const windowUtils = require('../deprecated/window-utils'); +const { WindowTracker } = windowUtils; +const { isBrowser } = require('../window/utils'); +const { windowNS } = require('../window/namespace'); +const { on, off, once, emit } = require('../event/core'); +const { method } = require('../lang/functional'); +const { EventTarget } = require('../event/target'); +const { List, addListItem } = require('../util/list'); + +const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec, consider using require("tabs") instead'; + +// NOTE: On Fennec there is only one window. + +let BrowserWindows = Class({ + implements: [ List ], + extends: EventTarget, + initialize: function() { + List.prototype.initialize.apply(this); + }, + get activeWindow() { + let window = windowUtils.activeBrowserWindow; + return window ? getBrowserWindow({window: window}) : null; + }, + open: function open(options) { + throw new Error(ERR_FENNEC_MSG); + return null; + } +}); +const browserWindows = exports.browserWindows = BrowserWindows(); + + +/** + * Gets a `BrowserWindow` for the given `chromeWindow` if previously + * registered, `null` otherwise. + */ +function getRegisteredWindow(chromeWindow) { + for each (let window in browserWindows) { + if (chromeWindow === windowNS(window).window) + return window; + } + + return null; +} + +/** + * Gets a `BrowserWindow` for the provided window options obj + * @params {Object} options + * Options that are passed to the the `BrowserWindowTrait` + * @returns {BrowserWindow} + */ +function getBrowserWindow(options) { + let window = null; + + // if we have a BrowserWindow already then use it + if ('window' in options) + window = getRegisteredWindow(options.window); + if (window) + return window; + + // we don't have a BrowserWindow yet, so create one + var window = BrowserWindow(options); + addListItem(browserWindows, window); + return window; +} + +WindowTracker({ + onTrack: function onTrack(chromeWindow) { + if (!isBrowser(chromeWindow)) return; + let window = getBrowserWindow({ window: chromeWindow }); + emit(browserWindows, 'open', window); + }, + onUntrack: function onUntrack(chromeWindow) { + if (!isBrowser(chromeWindow)) return; + let window = getBrowserWindow({ window: chromeWindow }); + emit(browserWindows, 'close', window); + } +}); diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/firefox.js b/tools/addon-sdk-1.12/lib/sdk/windows/firefox.js new file mode 100644 index 0000000..9a55a08 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/firefox.js @@ -0,0 +1,241 @@ +/* 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 { Cc, Ci, Cr } = require('chrome'), + { Trait } = require('../deprecated/traits'), + { List } = require('../deprecated/list'), + { EventEmitter } = require('../deprecated/events'), + { WindowTabs, WindowTabTracker } = require('./tabs-firefox'), + { WindowDom } = require('./dom'), + { WindowLoader } = require('./loader'), + { isBrowser, getWindowDocShell } = require('../window/utils'), + { Options } = require('../tabs/common'), + apiUtils = require('../deprecated/api-utils'), + unload = require('../system/unload'), + windowUtils = require('../deprecated/window-utils'), + { WindowTrackerTrait } = windowUtils, + { ns } = require('../core/namespace'), + { observer: windowObserver } = require('./observer'), + { isWindowPBEnabled } = require('../private-browsing/utils'); + +/** + * Window trait composes safe wrappers for browser window that are E10S + * compatible. + */ +const BrowserWindowTrait = Trait.compose( + EventEmitter, + WindowDom.resolve({ close: '_close' }), + WindowTabs, + WindowTabTracker, + WindowLoader, + /* WindowSidebars, */ + Trait.compose({ + _emit: Trait.required, + _close: Trait.required, + _load: Trait.required, + /** + * Constructor returns wrapper of the specified chrome window. + * @param {nsIWindow} window + */ + constructor: function BrowserWindow(options) { + // Register this window ASAP, in order to avoid loop that would try + // to create this window instance over and over (see bug 648244) + windows.push(this); + + // make sure we don't have unhandled errors + this.on('error', console.exception.bind(console)); + + if ('onOpen' in options) + this.on('open', options.onOpen); + if ('onClose' in options) + this.on('close', options.onClose); + if ('onActivate' in options) + this.on('activate', options.onActivate); + if ('onDeactivate' in options) + this.on('deactivate', options.onDeactivate); + if ('window' in options) + this._window = options.window; + + if ('tabs' in options) { + this._tabOptions = Array.isArray(options.tabs) ? + options.tabs.map(Options) : + [ Options(options.tabs) ]; + } + else if ('url' in options) { + this._tabOptions = [ Options(options.url) ]; + } + + this._load(); + return this; + }, + destroy: function () this._onUnload(), + _tabOptions: [], + _onLoad: function() { + try { + this._initWindowTabTracker(); + } + catch(e) { + this._emit('error', e); + } + + this._emitOnObject(browserWindows, 'open', this._public); + }, + _onUnload: function() { + if (!this._window) + return; + this._destroyWindowTabTracker(); + this._emitOnObject(browserWindows, 'close', this._public); + this._window = null; + // Removing reference from the windows array. + windows.splice(windows.indexOf(this), 1); + this._removeAllListeners(); + }, + close: function close(callback) { + // maybe we should deprecate this with message ? + if (callback) this.on('close', callback); + return this._close(); + } + }) +); + +/** + * Gets a `BrowserWindowTrait` for the given `chromeWindow` if previously + * registered, `null` otherwise. + */ +function getRegisteredWindow(chromeWindow) { + for each (let window in windows) { + if (chromeWindow === window._window) + return window; + } + + return null; +} + +/** + * Wrapper for `BrowserWindowTrait`. Creates new instance if wrapper for + * window doesn't exists yet. If wrapper already exists then returns it + * instead. + * @params {Object} options + * Options that are passed to the the `BrowserWindowTrait` + * @returns {BrowserWindow} + * @see BrowserWindowTrait + */ +function BrowserWindow(options) { + let window = null; + + if ("window" in options) + window = getRegisteredWindow(options.window); + + return (window || BrowserWindowTrait(options))._public; +} +// to have proper `instanceof` behavior will go away when #596248 is fixed. +BrowserWindow.prototype = BrowserWindowTrait.prototype; +exports.BrowserWindow = BrowserWindow; + +const windows = []; + +const browser = ns(); + +function onWindowActivation (chromeWindow, event) { + if (!isBrowser(chromeWindow)) return; // Ignore if it's not a browser window. + + let window = getRegisteredWindow(chromeWindow); + + if (window) + window._emit(event.type, window._public); + else + window = BrowserWindowTrait({ window: chromeWindow }); + + browser(browserWindows).internals._emit(event.type, window._public); +} + +windowObserver.on("activate", onWindowActivation); +windowObserver.on("deactivate", onWindowActivation); + +/** + * `BrowserWindows` trait is composed out of `List` trait and it represents + * "live" list of currently open browser windows. Instance mutates itself + * whenever new browser window gets opened / closed. + */ +// Very stupid to resolve all `toStrings` but this will be fixed by #596248 +const browserWindows = Trait.resolve({ toString: null }).compose( + List.resolve({ constructor: '_initList' }), + EventEmitter.resolve({ toString: null }), + WindowTrackerTrait.resolve({ constructor: '_initTracker', toString: null }), + Trait.compose({ + _emit: Trait.required, + _add: Trait.required, + _remove: Trait.required, + + // public API + + /** + * Constructor creates instance of `Windows` that represents live list of open + * windows. + */ + constructor: function BrowserWindows() { + browser(this._public).internals = this; + + this._trackedWindows = []; + this._initList(); + this._initTracker(); + unload.ensure(this, "_destructor"); + }, + _destructor: function _destructor() { + this._removeAllListeners('open'); + this._removeAllListeners('close'); + this._removeAllListeners('activate'); + this._removeAllListeners('deactivate'); + this._clear(); + + delete browser(this._public).internals; + }, + /** + * This property represents currently active window. + * Property is non-enumerable, in order to preserve array like enumeration. + * @type {Window|null} + */ + get activeWindow() { + let window = windowUtils.activeBrowserWindow; + return window ? BrowserWindow({window: window}) : null; + }, + open: function open(options) { + if (typeof options === "string") + // `tabs` option is under review and may be removed. + options = { tabs: [Options(options)] }; + return BrowserWindow(options); + }, + + /** + * Internal listener which is called whenever new window gets open. + * Creates wrapper and adds to this list. + * @param {nsIWindow} chromeWindow + */ + _onTrack: function _onTrack(chromeWindow) { + if (!isBrowser(chromeWindow)) return; + let window = BrowserWindow({ window: chromeWindow }); + this._add(window); + this._emit('open', window); + }, + /** + * Internal listener which is called whenever window gets closed. + * Cleans up references and removes wrapper from this list. + * @param {nsIWindow} window + */ + _onUntrack: function _onUntrack(chromeWindow) { + if (!isBrowser(chromeWindow)) return; + let window = BrowserWindow({ window: chromeWindow }); + this._remove(window); + this._emit('close', window); + + // Bug 724404: do not leak this module and linked windows: + // We have to do it on untrack and not only when `_onUnload` is called + // when windows are closed, otherwise, we will leak on addon disabling. + window.destroy(); + } + }).resolve({ toString: null }) +)(); + +exports.browserWindows = browserWindows; diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/loader.js b/tools/addon-sdk-1.12/lib/sdk/windows/loader.js new file mode 100644 index 0000000..be3d84c --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/loader.js @@ -0,0 +1,120 @@ +/* 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'; + +module.metadata = { + "stability": "unstable" +}; + +const { Cc, Ci } = require('chrome'), + { setTimeout } = require('../timers'), + { Trait } = require('../deprecated/traits'), + { openDialog } = require('../window/utils'), + + ON_LOAD = 'load', + ON_UNLOAD = 'unload', + STATE_LOADED = 'complete'; + +/** + * Trait provides private `_window` property and requires `_onLoad` property + * that will be called when `_window` is loaded. If `_window` property value + * is changed with already loaded window `_onLoad` still will be called. + */ +const WindowLoader = Trait.compose({ + /** + * Internal listener that is called when window is loaded. + * Please keep in mind that this trait will not handle exceptions that may + * be thrown by this method so method itself should take care of + * handling them. + * @param {nsIWindow} window + */ + _onLoad: Trait.required, + _tabOptions: Trait.required, + /** + * Internal listener that is called when `_window`'s DOM 'unload' event + * is dispatched. Please note that this trait will not handle exceptions that + * may be thrown by this method so method itself should take care of + * handling them. + */ + _onUnload: Trait.required, + _load: function _load() { + if (this.__window) return; + this._window = openDialog({ + args: this._tabOptions.map(function(options) options.url).join("|") + }); + }, + /** + * Private window who's load event is being tracked. Once window is loaded + * `_onLoad` is called. + * @type {nsIWindow} + */ + get _window() this.__window, + set _window(window) { + let _window = this.__window; + if (!window) window = null; + + if (window !== _window) { + if (_window) { + _window.removeEventListener(ON_UNLOAD, this.__unloadListener, false); + _window.removeEventListener(ON_LOAD, this.__loadListener, false); + } + + if (window) { + window.addEventListener( + ON_UNLOAD, + this.__unloadListener || + (this.__unloadListener = this._unloadListener.bind(this)) + , + false + ); + + this.__window = window; + + // If window is not loaded yet setting up a listener. + if (STATE_LOADED != window.document.readyState) { + window.addEventListener( + ON_LOAD, + this.__loadListener || + (this.__loadListener = this._loadListener.bind(this)) + , + false + ); + } + else { // If window is loaded calling listener next turn of event loop. + this._onLoad(window) + } + } + } + }, + __window: null, + /** + * Internal method used for listening 'load' event on the `_window`. + * Method takes care of removing itself from 'load' event listeners once + * event is being handled. + */ + _loadListener: function _loadListener(event) { + let window = this._window; + if (!event.target || event.target.defaultView != window) return; + window.removeEventListener(ON_LOAD, this.__loadListener, false); + this._onLoad(window); + }, + __loadListener: null, + /** + * Internal method used for listening 'unload' event on the `_window`. + * Method takes care of removing itself from 'unload' event listeners once + * event is being handled. + */ + _unloadListener: function _unloadListener(event) { + let window = this._window; + if (!event.target + || event.target.defaultView != window + || STATE_LOADED != window.document.readyState + ) return; + window.removeEventListener(ON_UNLOAD, this.__unloadListener, false); + this._onUnload(window); + }, + __unloadListener: null +}); +exports.WindowLoader = WindowLoader; + diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/observer.js b/tools/addon-sdk-1.12/lib/sdk/windows/observer.js new file mode 100644 index 0000000..1097bf3 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/observer.js @@ -0,0 +1,52 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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"; + +module.metadata = { + "stability": "unstable" +}; + +const { EventEmitterTrait: EventEmitter } = require("../deprecated/events"); +const { WindowTracker, windowIterator } = require("../deprecated/window-utils"); +const { DOMEventAssembler } = require("../deprecated/events/assembler"); +const { Trait } = require("../deprecated/light-traits"); + +// Event emitter objects used to register listeners and emit events on them +// when they occur. +const observer = Trait.compose(DOMEventAssembler, EventEmitter).create({ + /** + * Method is implemented by `EventEmitter` and is used just for emitting + * events on registered listeners. + */ + _emit: Trait.required, + /** + * Events that are supported and emitted by the module. + */ + supportedEventsTypes: [ "activate", "deactivate" ], + /** + * Function handles all the supported events on all the windows that are + * observed. Method is used to proxy events to the listeners registered on + * this event emitter. + * @param {Event} event + * Keyboard event being emitted. + */ + handleEvent: function handleEvent(event) { + this._emit(event.type, event.target, event); + } +}); + +// Using `WindowTracker` to track window events. +WindowTracker({ + onTrack: function onTrack(chromeWindow) { + observer._emit("open", chromeWindow); + observer.observe(chromeWindow); + }, + onUntrack: function onUntrack(chromeWindow) { + observer._emit("close", chromeWindow); + observer.ignore(chromeWindow); + } +}); + +exports.observer = observer; diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/tabs-fennec.js b/tools/addon-sdk-1.12/lib/sdk/windows/tabs-fennec.js new file mode 100644 index 0000000..ddf4aeb --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/tabs-fennec.js @@ -0,0 +1,190 @@ +/* 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 { Class } = require('../core/heritage'); +const { Tab } = require('../tabs/tab'); +const { browserWindows } = require('./fennec'); +const { windowNS } = require('../window/namespace'); +const { tabsNS, tabNS } = require('../tabs/namespace'); +const { openTab, getTabs, getSelectedTab } = require('../tabs/utils'); +const { Options } = require('../tabs/common'); +const { on, once, off, emit } = require('../event/core'); +const { method } = require('../lang/functional'); +const { EVENTS } = require('../tabs/events'); +const { EventTarget } = require('../event/target'); +const { when: unload } = require('../system/unload'); +const { windowIterator } = require('../deprecated/window-utils'); +const { List, addListItem, removeListItem } = require('../util/list'); + +const mainWindow = windowNS(browserWindows.activeWindow).window; + +const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec'; + +const Tabs = Class({ + implements: [ List ], + extends: EventTarget, + initialize: function initialize(options) { + let tabsInternals = tabsNS(this); + let window = tabsNS(this).window = options.window || mainWindow; + + EventTarget.prototype.initialize.call(this, options); + List.prototype.initialize.apply(this, getTabs(window).map(Tab)); + + // TabOpen event + window.BrowserApp.deck.addEventListener(EVENTS.open.dom, onTabOpen, false); + + // TabSelect + window.BrowserApp.deck.addEventListener(EVENTS.activate.dom, onTabSelect, false); + + // TabClose + window.BrowserApp.deck.addEventListener(EVENTS.close.dom, onTabClose, false); + }, + get activeTab() { + return getTabForRawTab(getSelectedTab(tabsNS(this).window)); + }, + open: function(options) { + options = Options(options); + let activeWin = browserWindows.activeWindow; + + if (options.isPinned) { + console.error(ERR_FENNEC_MSG); // TODO + } + + let rawTab = openTab(windowNS(activeWin).window, options.url, { + inBackground: options.inBackground + }); + + // by now the tab has been created + let tab = getTabForRawTab(rawTab); + + if (options.onClose) + tab.on('close', options.onClose); + + if (options.onOpen) { + // NOTE: on Fennec this will be true + if (tabNS(tab).opened) + options.onOpen(tab); + + tab.on('open', options.onOpen); + } + + if (options.onReady) + tab.on('ready', options.onReady); + + if (options.onActivate) + tab.on('activate', options.onActivate); + + return tab; + } +}); +let gTabs = exports.tabs = Tabs(mainWindow); + +function tabsUnloader(event, window) { + window = window || (event && event.target); + if (!(window && window.BrowserApp)) + return; + window.BrowserApp.deck.removeEventListener(EVENTS.open.dom, onTabOpen, false); + window.BrowserApp.deck.removeEventListener(EVENTS.activate.dom, onTabSelect, false); + window.BrowserApp.deck.removeEventListener(EVENTS.close.dom, onTabClose, false); +} + +// unload handler +unload(function() { + for (let window in windowIterator()) { + tabsUnloader(null, window); + } +}); + +function addTab(tab) { + addListItem(gTabs, tab); + return tab; +} + +function removeTab(tab) { + removeListItem(gTabs, tab); + return tab; +} + +function getTabForBrowser(browser) { + return getTabForRawTab(getRawTabForBrowser(browser)); +} + +function getRawTabForBrowser(browser) { + let tabs = mainWindow.BrowserApp.tabs; + for (let i = 0; i < tabs.length; i++) { + let tab = tabs[i]; + if (tab.browser === browser) + return tab + } + return null; +} + +// TabOpen +function onTabOpen(event) { + let browser = event.target; + + let tab = getTabForBrowser(browser); + if (tab === null) { + let rawTab = getRawTabForBrowser(browser); + + // create a Tab instance for this new tab + tab = addTab(Tab(rawTab)); + } + + tabNS(tab).opened = true; + + // TabReady + let onReady = tabNS(tab).onReady = onTabReady.bind(tab); + browser.addEventListener(EVENTS.ready.dom, onReady, false); + + emit(tab, 'open', tab); + emit(gTabs, 'open', tab); +}; + +function onTabReady() { + emit(this, 'ready', this); + emit(gTabs, 'ready', this); +} + +// TabSelect +function onTabSelect(event) { + // Set value whenever new tab becomes active. + let tab = getTabForBrowser(event.target); + emit(tab, 'activate', tab); + emit(gTabs, 'activate', tab); + + for each (let t in gTabs) { + if (t === tab) continue; + emit(t, 'deactivate', t); + emit(gTabs, 'deactivate', t); + } +}; + +// TabClose +function onTabClose(event) { + let tab = getTabForBrowser(event.target); + removeTab(tab); + + emit(gTabs, 'close', tab); + emit(tab, 'close', tab); +}; + +function getTabForRawTab(rawTab) { + for each (let tab in gTabs) { + if (tabNS(tab).tab === rawTab) + return tab; + } + return null; +} + +unload(function() { + for each (let tab in gTabs) { + let tabInternals = tabNS(tab); + tabInternals.tab.browser.removeEventListener(EVENTS.ready.dom, tabInternals.onReady, false); + tabInternals.onReady = null; + tabInternals.tab = null; + tabInternals.window = null; + } +}); diff --git a/tools/addon-sdk-1.12/lib/sdk/windows/tabs-firefox.js b/tools/addon-sdk-1.12/lib/sdk/windows/tabs-firefox.js new file mode 100644 index 0000000..f41e752 --- /dev/null +++ b/tools/addon-sdk-1.12/lib/sdk/windows/tabs-firefox.js @@ -0,0 +1,188 @@ +/* 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"; + +module.metadata = { + "stability": "unstable" +}; + +const { Trait } = require("../deprecated/traits"); +const { List } = require("../deprecated/list"); +const { Tab } = require("../tabs/tab"); +const { EventEmitter } = require("../deprecated/events"); +const { EVENTS } = require("../tabs/events"); +const { getOwnerWindow, getActiveTab, getTabs, + openTab, activateTab } = require("../tabs/utils"); +const { Options } = require("../tabs/common"); +const { observer: tabsObserver } = require("../tabs/observer"); + +const TAB_BROWSER = "tabbrowser"; + +/** + * This is a trait that is used in composition of window wrapper. Trait tracks + * tab related events of the wrapped window in order to keep track of open + * tabs and maintain their wrappers. Every new tab gets wrapped and jetpack + * type event is emitted. + */ +const WindowTabTracker = Trait.compose({ + /** + * Chrome window whose tabs are tracked. + */ + _window: Trait.required, + /** + * Function used to emit events. + */ + _emit: EventEmitter.required, + _tabOptions: Trait.required, + /** + * Function to add event listeners. + */ + on: EventEmitter.required, + removeListener: EventEmitter.required, + /** + * Initializes tab tracker for a browser window. + */ + _initWindowTabTracker: function _initWindowTabTracker() { + // Ugly hack that we have to remove at some point (see Bug 658059). At this + // point it is necessary to invoke lazy `tabs` getter on the windows object + // which creates a `TabList` instance. + this.tabs; + + // Binding all methods used as event listeners to the instance. + this._onTabReady = this._emitEvent.bind(this, "ready"); + this._onTabOpen = this._onTabEvent.bind(this, "open"); + this._onTabClose = this._onTabEvent.bind(this, "close"); + this._onTabActivate = this._onTabEvent.bind(this, "activate"); + this._onTabDeactivate = this._onTabEvent.bind(this, "deactivate"); + this._onTabPinned = this._onTabEvent.bind(this, "pinned"); + this._onTabUnpinned = this._onTabEvent.bind(this, "unpinned"); + + for each (let tab in getTabs(this._window)) { + // We emulate "open" events for all open tabs since gecko does not emits + // them on the tabs that new windows are open with. Also this is + // necessary to synchronize tabs lists with an actual state. + this._onTabOpen(tab); + } + + // We also emulate "activate" event so that it's picked up by a tab list. + this._onTabActivate(getActiveTab(this._window)); + + // Setting up event listeners + tabsObserver.on("open", this._onTabOpen); + tabsObserver.on("close", this._onTabClose); + tabsObserver.on("activate", this._onTabActivate); + tabsObserver.on("deactivate", this._onTabDeactivate); + tabsObserver.on("pinned", this._onTabPinned); + tabsObserver.on("unpinned", this._onTabUnpinned); + }, + _destroyWindowTabTracker: function _destroyWindowTabTracker() { + // We emulate close events on all tabs, since gecko does not emits such + // events by itself. + for each (let tab in this.tabs) + this._emitEvent("close", tab); + + this._tabs._clear(); + + tabsObserver.removeListener("open", this._onTabOpen); + tabsObserver.removeListener("close", this._onTabClose); + tabsObserver.removeListener("activate", this._onTabActivate); + tabsObserver.removeListener("deactivate", this._onTabDeactivate); + }, + _onTabEvent: function _onTabEvent(type, tab) { + if (this._window === getOwnerWindow(tab)) { + let options = this._tabOptions.shift() || {}; + options.tab = tab; + options.window = this._public; + + // creating tab wrapper and adding listener to "ready" events. + let wrappedTab = Tab(options); + + // Setting up an event listener for ready events. + if (type === "open") + wrappedTab.on("ready", this._onTabReady); + + this._emitEvent(type, wrappedTab); + } + }, + _emitEvent: function _emitEvent(type, tab) { + // Notifies combined tab list that tab was added / removed. + tabs._emit(type, tab); + // Notifies contained tab list that window was added / removed. + this._tabs._emit(type, tab); + } +}); +exports.WindowTabTracker = WindowTabTracker; + +/** + * This trait is used to create live representation of open tab lists. Each + * window wrapper's tab list is represented by an object created from this + * trait. It is also used to represent list of all the open windows. Trait is + * composed out of `EventEmitter` in order to emit 'TabOpen', 'TabClose' events. + * **Please note** that objects created by this trait can't be exposed outside + * instead you should expose it's `_public` property, see comments in + * constructor for details. + */ +const TabList = List.resolve({ constructor: "_init" }).compose( + // This is ugly, but necessary. Will be removed by #596248 + EventEmitter.resolve({ toString: null }), + Trait.compose({ + on: Trait.required, + _emit: Trait.required, + constructor: function TabList(options) { + this._window = options.window; + // Add new items to the list + this.on(EVENTS.open.name, this._add.bind(this)); + + // Remove closed items from the list + this.on(EVENTS.close.name, this._remove.bind(this)); + + // Set value whenever new tab becomes active. + this.on("activate", function onTabActivate(tab) { + this._activeTab = tab; + }.bind(this)); + + // Initialize list. + this._init(); + // This list is not going to emit any events, object holding this list + // will do it instead, to make that possible we return a private API. + return this; + }, + get activeTab() this._activeTab, + _activeTab: null, + + open: function open(options) { + options = Options(options); + this._window._tabOptions.push(options); + let tab = openTab(this._window._window, options.url); + if (!options.inBackground) + activateTab(tab); + } + // This is ugly, but necessary. Will be removed by #596248 + }).resolve({ toString: null }) +); + +/** + * Combined list of all open tabs on all the windows. + * type {TabList} + */ +var tabs = TabList({ window: null }); +exports.tabs = tabs._public; + +/** + * Trait is a part of composition that represents window wrapper. This trait is + * composed out of `WindowTabTracker` that allows it to keep track of open tabs + * on the window being wrapped. + */ +const WindowTabs = Trait.compose( + WindowTabTracker, + Trait.compose({ + _window: Trait.required, + /** + * List of tabs + */ + get tabs() (this._tabs || (this._tabs = TabList({ window: this })))._public, + _tabs: null, + }) +); +exports.WindowTabs = WindowTabs; |