fennec.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. const { Class } = require('../core/heritage');
  6. const { BrowserWindow } = require('../window/browser');
  7. const { WindowTracker } = require('../deprecated/window-utils');
  8. const { isBrowser, getMostRecentBrowserWindow } = require('../window/utils');
  9. const { windowNS } = require('../window/namespace');
  10. const { on, off, once, emit } = require('../event/core');
  11. const { method } = require('../lang/functional');
  12. const { EventTarget } = require('../event/target');
  13. const { List, addListItem } = require('../util/list');
  14. const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec, consider using require("sdk/tabs") instead';
  15. // NOTE: On Fennec there is only one window.
  16. let BrowserWindows = Class({
  17. implements: [ List ],
  18. extends: EventTarget,
  19. initialize: function() {
  20. List.prototype.initialize.apply(this);
  21. },
  22. get activeWindow() {
  23. let window = getMostRecentBrowserWindow();
  24. return window ? getBrowserWindow({window: window}) : null;
  25. },
  26. open: function open(options) {
  27. throw new Error(ERR_FENNEC_MSG);
  28. return null;
  29. }
  30. });
  31. const browserWindows = exports.browserWindows = BrowserWindows();
  32. /**
  33. * Gets a `BrowserWindow` for the given `chromeWindow` if previously
  34. * registered, `null` otherwise.
  35. */
  36. function getRegisteredWindow(chromeWindow) {
  37. for each (let window in browserWindows) {
  38. if (chromeWindow === windowNS(window).window)
  39. return window;
  40. }
  41. return null;
  42. }
  43. /**
  44. * Gets a `BrowserWindow` for the provided window options obj
  45. * @params {Object} options
  46. * Options that are passed to the the `BrowserWindowTrait`
  47. * @returns {BrowserWindow}
  48. */
  49. function getBrowserWindow(options) {
  50. let window = null;
  51. // if we have a BrowserWindow already then use it
  52. if ('window' in options)
  53. window = getRegisteredWindow(options.window);
  54. if (window)
  55. return window;
  56. // we don't have a BrowserWindow yet, so create one
  57. var window = BrowserWindow(options);
  58. addListItem(browserWindows, window);
  59. return window;
  60. }
  61. WindowTracker({
  62. onTrack: function onTrack(chromeWindow) {
  63. if (!isBrowser(chromeWindow)) return;
  64. let window = getBrowserWindow({ window: chromeWindow });
  65. emit(browserWindows, 'open', window);
  66. },
  67. onUntrack: function onUntrack(chromeWindow) {
  68. if (!isBrowser(chromeWindow)) return;
  69. let window = getBrowserWindow({ window: chromeWindow });
  70. emit(browserWindows, 'close', window);
  71. }
  72. });