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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 'use strict';
michael@0 5
michael@0 6 const { Class } = require('../core/heritage');
michael@0 7 const { Tab } = require('../tabs/tab');
michael@0 8 const { browserWindows } = require('./fennec');
michael@0 9 const { windowNS } = require('../window/namespace');
michael@0 10 const { tabsNS, tabNS } = require('../tabs/namespace');
michael@0 11 const { openTab, getTabs, getSelectedTab, getTabForBrowser: getRawTabForBrowser,
michael@0 12 getTabContentWindow } = require('../tabs/utils');
michael@0 13 const { Options } = require('../tabs/common');
michael@0 14 const { getTabForBrowser, getTabForRawTab } = require('../tabs/helpers');
michael@0 15 const { on, once, off, emit } = require('../event/core');
michael@0 16 const { method } = require('../lang/functional');
michael@0 17 const { EVENTS } = require('../tabs/events');
michael@0 18 const { EventTarget } = require('../event/target');
michael@0 19 const { when: unload } = require('../system/unload');
michael@0 20 const { windowIterator } = require('../deprecated/window-utils');
michael@0 21 const { List, addListItem, removeListItem } = require('../util/list');
michael@0 22 const { isPrivateBrowsingSupported } = require('../self');
michael@0 23 const { isTabPBSupported, ignoreWindow } = require('../private-browsing/utils');
michael@0 24
michael@0 25 const mainWindow = windowNS(browserWindows.activeWindow).window;
michael@0 26
michael@0 27 const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec';
michael@0 28
michael@0 29 const supportPrivateTabs = isPrivateBrowsingSupported && isTabPBSupported;
michael@0 30
michael@0 31 const Tabs = Class({
michael@0 32 implements: [ List ],
michael@0 33 extends: EventTarget,
michael@0 34 initialize: function initialize(options) {
michael@0 35 let tabsInternals = tabsNS(this);
michael@0 36 let window = tabsNS(this).window = options.window || mainWindow;
michael@0 37
michael@0 38 EventTarget.prototype.initialize.call(this, options);
michael@0 39 List.prototype.initialize.apply(this, getTabs(window).map(Tab));
michael@0 40
michael@0 41 // TabOpen event
michael@0 42 window.BrowserApp.deck.addEventListener(EVENTS.open.dom, onTabOpen, false);
michael@0 43
michael@0 44 // TabSelect
michael@0 45 window.BrowserApp.deck.addEventListener(EVENTS.activate.dom, onTabSelect, false);
michael@0 46 },
michael@0 47 get activeTab() {
michael@0 48 return getTabForRawTab(getSelectedTab(tabsNS(this).window));
michael@0 49 },
michael@0 50 open: function(options) {
michael@0 51 options = Options(options);
michael@0 52 let activeWin = browserWindows.activeWindow;
michael@0 53
michael@0 54 if (options.isPinned) {
michael@0 55 console.error(ERR_FENNEC_MSG); // TODO
michael@0 56 }
michael@0 57
michael@0 58 let rawTab = openTab(windowNS(activeWin).window, options.url, {
michael@0 59 inBackground: options.inBackground,
michael@0 60 isPrivate: supportPrivateTabs && options.isPrivate
michael@0 61 });
michael@0 62
michael@0 63 // by now the tab has been created
michael@0 64 let tab = getTabForRawTab(rawTab);
michael@0 65
michael@0 66 if (options.onClose)
michael@0 67 tab.on('close', options.onClose);
michael@0 68
michael@0 69 if (options.onOpen) {
michael@0 70 // NOTE: on Fennec this will be true
michael@0 71 if (tabNS(tab).opened)
michael@0 72 options.onOpen(tab);
michael@0 73
michael@0 74 tab.on('open', options.onOpen);
michael@0 75 }
michael@0 76
michael@0 77 if (options.onReady)
michael@0 78 tab.on('ready', options.onReady);
michael@0 79
michael@0 80 if (options.onLoad)
michael@0 81 tab.on('load', options.onLoad);
michael@0 82
michael@0 83 if (options.onPageShow)
michael@0 84 tab.on('pageshow', options.onPageShow);
michael@0 85
michael@0 86 if (options.onActivate)
michael@0 87 tab.on('activate', options.onActivate);
michael@0 88
michael@0 89 return tab;
michael@0 90 }
michael@0 91 });
michael@0 92 let gTabs = exports.tabs = Tabs(mainWindow);
michael@0 93
michael@0 94 function tabsUnloader(event, window) {
michael@0 95 window = window || (event && event.target);
michael@0 96 if (!(window && window.BrowserApp))
michael@0 97 return;
michael@0 98 window.BrowserApp.deck.removeEventListener(EVENTS.open.dom, onTabOpen, false);
michael@0 99 window.BrowserApp.deck.removeEventListener(EVENTS.activate.dom, onTabSelect, false);
michael@0 100 }
michael@0 101
michael@0 102 // unload handler
michael@0 103 unload(function() {
michael@0 104 for (let window in windowIterator()) {
michael@0 105 tabsUnloader(null, window);
michael@0 106 }
michael@0 107 });
michael@0 108
michael@0 109 function addTab(tab) {
michael@0 110 addListItem(gTabs, tab);
michael@0 111 return tab;
michael@0 112 }
michael@0 113
michael@0 114 function removeTab(tab) {
michael@0 115 removeListItem(gTabs, tab);
michael@0 116 return tab;
michael@0 117 }
michael@0 118
michael@0 119 // TabOpen
michael@0 120 function onTabOpen(event) {
michael@0 121 let browser = event.target;
michael@0 122
michael@0 123 // Eventually ignore private tabs
michael@0 124 if (ignoreWindow(browser.contentWindow))
michael@0 125 return;
michael@0 126
michael@0 127 let tab = getTabForBrowser(browser);
michael@0 128 if (tab === null) {
michael@0 129 let rawTab = getRawTabForBrowser(browser);
michael@0 130
michael@0 131 // create a Tab instance for this new tab
michael@0 132 tab = addTab(Tab(rawTab));
michael@0 133 }
michael@0 134
michael@0 135 tabNS(tab).opened = true;
michael@0 136
michael@0 137 tab.on('ready', function() emit(gTabs, 'ready', tab));
michael@0 138 tab.once('close', onTabClose);
michael@0 139
michael@0 140 tab.on('pageshow', function(_tab, persisted)
michael@0 141 emit(gTabs, 'pageshow', tab, persisted));
michael@0 142
michael@0 143 emit(tab, 'open', tab);
michael@0 144 emit(gTabs, 'open', tab);
michael@0 145 }
michael@0 146
michael@0 147 // TabSelect
michael@0 148 function onTabSelect(event) {
michael@0 149 let browser = event.target;
michael@0 150
michael@0 151 // Eventually ignore private tabs
michael@0 152 if (ignoreWindow(browser.contentWindow))
michael@0 153 return;
michael@0 154
michael@0 155 // Set value whenever new tab becomes active.
michael@0 156 let tab = getTabForBrowser(browser);
michael@0 157 emit(tab, 'activate', tab);
michael@0 158 emit(gTabs, 'activate', tab);
michael@0 159
michael@0 160 for each (let t in gTabs) {
michael@0 161 if (t === tab) continue;
michael@0 162 emit(t, 'deactivate', t);
michael@0 163 emit(gTabs, 'deactivate', t);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 // TabClose
michael@0 168 function onTabClose(tab) {
michael@0 169 removeTab(tab);
michael@0 170 emit(gTabs, EVENTS.close.name, tab);
michael@0 171 }

mercurial