addon-sdk/source/lib/sdk/windows/fennec.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:e875aa09a897
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 'use strict';
5
6 const { Class } = require('../core/heritage');
7 const { BrowserWindow } = require('../window/browser');
8 const { WindowTracker } = require('../deprecated/window-utils');
9 const { isBrowser, getMostRecentBrowserWindow } = require('../window/utils');
10 const { windowNS } = require('../window/namespace');
11 const { on, off, once, emit } = require('../event/core');
12 const { method } = require('../lang/functional');
13 const { EventTarget } = require('../event/target');
14 const { List, addListItem } = require('../util/list');
15
16 const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec, consider using require("sdk/tabs") instead';
17
18 // NOTE: On Fennec there is only one window.
19
20 let BrowserWindows = Class({
21 implements: [ List ],
22 extends: EventTarget,
23 initialize: function() {
24 List.prototype.initialize.apply(this);
25 },
26 get activeWindow() {
27 let window = getMostRecentBrowserWindow();
28 return window ? getBrowserWindow({window: window}) : null;
29 },
30 open: function open(options) {
31 throw new Error(ERR_FENNEC_MSG);
32 return null;
33 }
34 });
35 const browserWindows = exports.browserWindows = BrowserWindows();
36
37
38 /**
39 * Gets a `BrowserWindow` for the given `chromeWindow` if previously
40 * registered, `null` otherwise.
41 */
42 function getRegisteredWindow(chromeWindow) {
43 for each (let window in browserWindows) {
44 if (chromeWindow === windowNS(window).window)
45 return window;
46 }
47
48 return null;
49 }
50
51 /**
52 * Gets a `BrowserWindow` for the provided window options obj
53 * @params {Object} options
54 * Options that are passed to the the `BrowserWindowTrait`
55 * @returns {BrowserWindow}
56 */
57 function getBrowserWindow(options) {
58 let window = null;
59
60 // if we have a BrowserWindow already then use it
61 if ('window' in options)
62 window = getRegisteredWindow(options.window);
63 if (window)
64 return window;
65
66 // we don't have a BrowserWindow yet, so create one
67 var window = BrowserWindow(options);
68 addListItem(browserWindows, window);
69 return window;
70 }
71
72 WindowTracker({
73 onTrack: function onTrack(chromeWindow) {
74 if (!isBrowser(chromeWindow)) return;
75 let window = getBrowserWindow({ window: chromeWindow });
76 emit(browserWindows, 'open', window);
77 },
78 onUntrack: function onUntrack(chromeWindow) {
79 if (!isBrowser(chromeWindow)) return;
80 let window = getBrowserWindow({ window: chromeWindow });
81 emit(browserWindows, 'close', window);
82 }
83 });

mercurial