Wed, 31 Dec 2014 06:09:35 +0100
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 { Cc, Ci } = require('chrome'); |
michael@0 | 7 | const { Class } = require('../core/heritage'); |
michael@0 | 8 | const { tabNS, rawTabNS } = require('./namespace'); |
michael@0 | 9 | const { EventTarget } = require('../event/target'); |
michael@0 | 10 | const { activateTab, getTabTitle, setTabTitle, closeTab, getTabURL, getTabContentWindow, |
michael@0 | 11 | getTabForBrowser, |
michael@0 | 12 | setTabURL, getOwnerWindow, getTabContentType, getTabId } = require('./utils'); |
michael@0 | 13 | const { emit } = require('../event/core'); |
michael@0 | 14 | const { getOwnerWindow: getPBOwnerWindow } = require('../private-browsing/window/utils'); |
michael@0 | 15 | const { when: unload } = require('../system/unload'); |
michael@0 | 16 | const { viewFor } = require('../view/core'); |
michael@0 | 17 | const { EVENTS } = require('./events'); |
michael@0 | 18 | |
michael@0 | 19 | const ERR_FENNEC_MSG = 'This method is not yet supported by Fennec'; |
michael@0 | 20 | |
michael@0 | 21 | const Tab = Class({ |
michael@0 | 22 | extends: EventTarget, |
michael@0 | 23 | initialize: function initialize(options) { |
michael@0 | 24 | options = options.tab ? options : { tab: options }; |
michael@0 | 25 | let tab = options.tab; |
michael@0 | 26 | |
michael@0 | 27 | EventTarget.prototype.initialize.call(this, options); |
michael@0 | 28 | let tabInternals = tabNS(this); |
michael@0 | 29 | rawTabNS(tab).tab = this; |
michael@0 | 30 | |
michael@0 | 31 | let window = tabInternals.window = options.window || getOwnerWindow(tab); |
michael@0 | 32 | tabInternals.tab = tab; |
michael@0 | 33 | |
michael@0 | 34 | // TabReady |
michael@0 | 35 | let onReady = tabInternals.onReady = onTabReady.bind(this); |
michael@0 | 36 | tab.browser.addEventListener(EVENTS.ready.dom, onReady, false); |
michael@0 | 37 | |
michael@0 | 38 | // TabPageShow |
michael@0 | 39 | let onPageShow = tabInternals.onPageShow = onTabPageShow.bind(this); |
michael@0 | 40 | tab.browser.addEventListener(EVENTS.pageshow.dom, onPageShow, false); |
michael@0 | 41 | |
michael@0 | 42 | // TabLoad |
michael@0 | 43 | let onLoad = tabInternals.onLoad = onTabLoad.bind(this); |
michael@0 | 44 | tab.browser.addEventListener(EVENTS.load.dom, onLoad, true); |
michael@0 | 45 | |
michael@0 | 46 | // TabClose |
michael@0 | 47 | let onClose = tabInternals.onClose = onTabClose.bind(this); |
michael@0 | 48 | window.BrowserApp.deck.addEventListener(EVENTS.close.dom, onClose, false); |
michael@0 | 49 | |
michael@0 | 50 | unload(cleanupTab.bind(null, this)); |
michael@0 | 51 | }, |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * The title of the page currently loaded in the tab. |
michael@0 | 55 | * Changing this property changes an actual title. |
michael@0 | 56 | * @type {String} |
michael@0 | 57 | */ |
michael@0 | 58 | get title() getTabTitle(tabNS(this).tab), |
michael@0 | 59 | set title(title) setTabTitle(tabNS(this).tab, title), |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Location of the page currently loaded in this tab. |
michael@0 | 63 | * Changing this property will loads page under under the specified location. |
michael@0 | 64 | * @type {String} |
michael@0 | 65 | */ |
michael@0 | 66 | get url() { |
michael@0 | 67 | return tabNS(this).closed ? undefined : getTabURL(tabNS(this).tab); |
michael@0 | 68 | }, |
michael@0 | 69 | set url(url) setTabURL(tabNS(this).tab, url), |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * URI of the favicon for the page currently loaded in this tab. |
michael@0 | 73 | * @type {String} |
michael@0 | 74 | */ |
michael@0 | 75 | get favicon() { |
michael@0 | 76 | /* |
michael@0 | 77 | * Synchronous favicon services were never supported on Fennec, |
michael@0 | 78 | * and as of FF22, are now deprecated. When/if favicon services |
michael@0 | 79 | * are supported for Fennec, this getter should reference |
michael@0 | 80 | * `require('sdk/places/favicon').getFavicon` |
michael@0 | 81 | */ |
michael@0 | 82 | console.error( |
michael@0 | 83 | 'tab.favicon is deprecated, and currently ' + |
michael@0 | 84 | 'favicon helpers are not yet supported by Fennec' |
michael@0 | 85 | ); |
michael@0 | 86 | |
michael@0 | 87 | // return 16x16 blank default |
michael@0 | 88 | return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAEklEQVQ4jWNgGAWjYBSMAggAAAQQAAF/TXiOAAAAAElFTkSuQmCC'; |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | getThumbnail: function() { |
michael@0 | 92 | // TODO: implement! |
michael@0 | 93 | console.error(ERR_FENNEC_MSG); |
michael@0 | 94 | |
michael@0 | 95 | // return 80x45 blank default |
michael@0 | 96 | return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAtCAYAAAA5reyyAAAAJElEQVRoge3BAQEAAACCIP+vbkhAAQAAAAAAAAAAAAAAAADXBjhtAAGQ0AF/AAAAAElFTkSuQmCC'; |
michael@0 | 97 | }, |
michael@0 | 98 | |
michael@0 | 99 | get id() { |
michael@0 | 100 | return getTabId(tabNS(this).tab); |
michael@0 | 101 | }, |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * The index of the tab relative to other tabs in the application window. |
michael@0 | 105 | * Changing this property will change order of the actual position of the tab. |
michael@0 | 106 | * @type {Number} |
michael@0 | 107 | */ |
michael@0 | 108 | get index() { |
michael@0 | 109 | if (tabNS(this).closed) return undefined; |
michael@0 | 110 | |
michael@0 | 111 | let tabs = tabNS(this).window.BrowserApp.tabs; |
michael@0 | 112 | let tab = tabNS(this).tab; |
michael@0 | 113 | for (var i = tabs.length; i >= 0; i--) { |
michael@0 | 114 | if (tabs[i] === tab) |
michael@0 | 115 | return i; |
michael@0 | 116 | } |
michael@0 | 117 | return null; |
michael@0 | 118 | }, |
michael@0 | 119 | set index(value) { |
michael@0 | 120 | console.error(ERR_FENNEC_MSG); // TODO |
michael@0 | 121 | }, |
michael@0 | 122 | |
michael@0 | 123 | /** |
michael@0 | 124 | * Whether or not tab is pinned (Is an app-tab). |
michael@0 | 125 | * @type {Boolean} |
michael@0 | 126 | */ |
michael@0 | 127 | get isPinned() { |
michael@0 | 128 | console.error(ERR_FENNEC_MSG); // TODO |
michael@0 | 129 | return false; // TODO |
michael@0 | 130 | }, |
michael@0 | 131 | pin: function pin() { |
michael@0 | 132 | console.error(ERR_FENNEC_MSG); // TODO |
michael@0 | 133 | }, |
michael@0 | 134 | unpin: function unpin() { |
michael@0 | 135 | console.error(ERR_FENNEC_MSG); // TODO |
michael@0 | 136 | }, |
michael@0 | 137 | |
michael@0 | 138 | /** |
michael@0 | 139 | * Returns the MIME type that the document loaded in the tab is being |
michael@0 | 140 | * rendered as. |
michael@0 | 141 | * @type {String} |
michael@0 | 142 | */ |
michael@0 | 143 | get contentType() getTabContentType(tabNS(this).tab), |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Create a worker for this tab, first argument is options given to Worker. |
michael@0 | 147 | * @type {Worker} |
michael@0 | 148 | */ |
michael@0 | 149 | attach: function attach(options) { |
michael@0 | 150 | // BUG 792946 https://bugzilla.mozilla.org/show_bug.cgi?id=792946 |
michael@0 | 151 | // TODO: fix this circular dependency |
michael@0 | 152 | let { Worker } = require('./worker'); |
michael@0 | 153 | return Worker(options, getTabContentWindow(tabNS(this).tab)); |
michael@0 | 154 | }, |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * Make this tab active. |
michael@0 | 158 | */ |
michael@0 | 159 | activate: function activate() { |
michael@0 | 160 | activateTab(tabNS(this).tab, tabNS(this).window); |
michael@0 | 161 | }, |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * Close the tab |
michael@0 | 165 | */ |
michael@0 | 166 | close: function close(callback) { |
michael@0 | 167 | let tab = this; |
michael@0 | 168 | this.once(EVENTS.close.name, function () { |
michael@0 | 169 | tabNS(tab).closed = true; |
michael@0 | 170 | if (callback) callback(); |
michael@0 | 171 | }); |
michael@0 | 172 | |
michael@0 | 173 | closeTab(tabNS(this).tab); |
michael@0 | 174 | }, |
michael@0 | 175 | |
michael@0 | 176 | /** |
michael@0 | 177 | * Reload the tab |
michael@0 | 178 | */ |
michael@0 | 179 | reload: function reload() { |
michael@0 | 180 | tabNS(this).tab.browser.reload(); |
michael@0 | 181 | } |
michael@0 | 182 | }); |
michael@0 | 183 | exports.Tab = Tab; |
michael@0 | 184 | |
michael@0 | 185 | // Implement `viewFor` polymorphic function for the Tab |
michael@0 | 186 | // instances. |
michael@0 | 187 | viewFor.define(Tab, x => tabNS(x).tab); |
michael@0 | 188 | |
michael@0 | 189 | function cleanupTab(tab) { |
michael@0 | 190 | let tabInternals = tabNS(tab); |
michael@0 | 191 | if (!tabInternals.tab) |
michael@0 | 192 | return; |
michael@0 | 193 | |
michael@0 | 194 | if (tabInternals.tab.browser) { |
michael@0 | 195 | tabInternals.tab.browser.removeEventListener(EVENTS.ready.dom, tabInternals.onReady, false); |
michael@0 | 196 | tabInternals.tab.browser.removeEventListener(EVENTS.pageshow.dom, tabInternals.onPageShow, false); |
michael@0 | 197 | tabInternals.tab.browser.removeEventListener(EVENTS.load.dom, tabInternals.onLoad, true); |
michael@0 | 198 | } |
michael@0 | 199 | tabInternals.onReady = null; |
michael@0 | 200 | tabInternals.onPageShow = null; |
michael@0 | 201 | tabInternals.onLoad = null; |
michael@0 | 202 | tabInternals.window.BrowserApp.deck.removeEventListener(EVENTS.close.dom, tabInternals.onClose, false); |
michael@0 | 203 | tabInternals.onClose = null; |
michael@0 | 204 | rawTabNS(tabInternals.tab).tab = null; |
michael@0 | 205 | tabInternals.tab = null; |
michael@0 | 206 | tabInternals.window = null; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | function onTabReady(event) { |
michael@0 | 210 | let win = event.target.defaultView; |
michael@0 | 211 | |
michael@0 | 212 | // ignore frames |
michael@0 | 213 | if (win === win.top) { |
michael@0 | 214 | emit(this, 'ready', this); |
michael@0 | 215 | } |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | function onTabLoad (event) { |
michael@0 | 219 | let win = event.target.defaultView; |
michael@0 | 220 | |
michael@0 | 221 | // ignore frames |
michael@0 | 222 | if (win === win.top) { |
michael@0 | 223 | emit(this, 'load', this); |
michael@0 | 224 | } |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | function onTabPageShow(event) { |
michael@0 | 228 | let win = event.target.defaultView; |
michael@0 | 229 | if (win === win.top) |
michael@0 | 230 | emit(this, 'pageshow', this, event.persisted); |
michael@0 | 231 | } |
michael@0 | 232 | |
michael@0 | 233 | // TabClose |
michael@0 | 234 | function onTabClose(event) { |
michael@0 | 235 | let rawTab = getTabForBrowser(event.target); |
michael@0 | 236 | if (tabNS(this).tab !== rawTab) |
michael@0 | 237 | return; |
michael@0 | 238 | |
michael@0 | 239 | emit(this, EVENTS.close.name, this); |
michael@0 | 240 | cleanupTab(this); |
michael@0 | 241 | }; |
michael@0 | 242 | |
michael@0 | 243 | getPBOwnerWindow.define(Tab, function(tab) { |
michael@0 | 244 | return getTabContentWindow(tabNS(tab).tab); |
michael@0 | 245 | }); |