michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { Class } = require('../core/heritage'); michael@0: const { BrowserWindow } = require('../window/browser'); michael@0: const { WindowTracker } = require('../deprecated/window-utils'); michael@0: const { isBrowser, getMostRecentBrowserWindow } = require('../window/utils'); michael@0: const { windowNS } = require('../window/namespace'); michael@0: const { on, off, once, emit } = require('../event/core'); michael@0: const { method } = require('../lang/functional'); michael@0: const { EventTarget } = require('../event/target'); michael@0: const { List, addListItem } = require('../util/list'); michael@0: michael@0: const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec, consider using require("sdk/tabs") instead'; michael@0: michael@0: // NOTE: On Fennec there is only one window. michael@0: michael@0: let BrowserWindows = Class({ michael@0: implements: [ List ], michael@0: extends: EventTarget, michael@0: initialize: function() { michael@0: List.prototype.initialize.apply(this); michael@0: }, michael@0: get activeWindow() { michael@0: let window = getMostRecentBrowserWindow(); michael@0: return window ? getBrowserWindow({window: window}) : null; michael@0: }, michael@0: open: function open(options) { michael@0: throw new Error(ERR_FENNEC_MSG); michael@0: return null; michael@0: } michael@0: }); michael@0: const browserWindows = exports.browserWindows = BrowserWindows(); michael@0: michael@0: michael@0: /** michael@0: * Gets a `BrowserWindow` for the given `chromeWindow` if previously michael@0: * registered, `null` otherwise. michael@0: */ michael@0: function getRegisteredWindow(chromeWindow) { michael@0: for each (let window in browserWindows) { michael@0: if (chromeWindow === windowNS(window).window) michael@0: return window; michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: /** michael@0: * Gets a `BrowserWindow` for the provided window options obj michael@0: * @params {Object} options michael@0: * Options that are passed to the the `BrowserWindowTrait` michael@0: * @returns {BrowserWindow} michael@0: */ michael@0: function getBrowserWindow(options) { michael@0: let window = null; michael@0: michael@0: // if we have a BrowserWindow already then use it michael@0: if ('window' in options) michael@0: window = getRegisteredWindow(options.window); michael@0: if (window) michael@0: return window; michael@0: michael@0: // we don't have a BrowserWindow yet, so create one michael@0: var window = BrowserWindow(options); michael@0: addListItem(browserWindows, window); michael@0: return window; michael@0: } michael@0: michael@0: WindowTracker({ michael@0: onTrack: function onTrack(chromeWindow) { michael@0: if (!isBrowser(chromeWindow)) return; michael@0: let window = getBrowserWindow({ window: chromeWindow }); michael@0: emit(browserWindows, 'open', window); michael@0: }, michael@0: onUntrack: function onUntrack(chromeWindow) { michael@0: if (!isBrowser(chromeWindow)) return; michael@0: let window = getBrowserWindow({ window: chromeWindow }); michael@0: emit(browserWindows, 'close', window); michael@0: } michael@0: });