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 | module.metadata = { |
michael@0 | 7 | 'stability': 'unstable', |
michael@0 | 8 | 'engines': { |
michael@0 | 9 | 'Firefox': '*' |
michael@0 | 10 | } |
michael@0 | 11 | }; |
michael@0 | 12 | |
michael@0 | 13 | const { models, buttons, views, viewsFor, modelFor } = require('./namespace'); |
michael@0 | 14 | const { isBrowser, getMostRecentBrowserWindow, windows, isWindowPrivate } = require('../../window/utils'); |
michael@0 | 15 | const { setStateFor } = require('../state'); |
michael@0 | 16 | const { defer } = require('../../core/promise'); |
michael@0 | 17 | const { isPrivateBrowsingSupported } = require('../../self'); |
michael@0 | 18 | |
michael@0 | 19 | const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; |
michael@0 | 20 | const WEB_PANEL_BROWSER_ID = 'web-panels-browser'; |
michael@0 | 21 | |
michael@0 | 22 | function create(window, details) { |
michael@0 | 23 | let id = makeID(details.id); |
michael@0 | 24 | let { document } = window; |
michael@0 | 25 | |
michael@0 | 26 | if (document.getElementById(id)) |
michael@0 | 27 | throw new Error('The ID "' + details.id + '" seems already used.'); |
michael@0 | 28 | |
michael@0 | 29 | let menuitem = document.createElementNS(XUL_NS, 'menuitem'); |
michael@0 | 30 | menuitem.setAttribute('id', id); |
michael@0 | 31 | menuitem.setAttribute('label', details.title); |
michael@0 | 32 | menuitem.setAttribute('sidebarurl', details.sidebarurl); |
michael@0 | 33 | menuitem.setAttribute('checked', 'false'); |
michael@0 | 34 | menuitem.setAttribute('type', 'checkbox'); |
michael@0 | 35 | menuitem.setAttribute('group', 'sidebar'); |
michael@0 | 36 | menuitem.setAttribute('autoCheck', 'false'); |
michael@0 | 37 | |
michael@0 | 38 | document.getElementById('viewSidebarMenu').appendChild(menuitem); |
michael@0 | 39 | |
michael@0 | 40 | return menuitem; |
michael@0 | 41 | } |
michael@0 | 42 | exports.create = create; |
michael@0 | 43 | |
michael@0 | 44 | function dispose(menuitem) { |
michael@0 | 45 | menuitem.parentNode.removeChild(menuitem); |
michael@0 | 46 | } |
michael@0 | 47 | exports.dispose = dispose; |
michael@0 | 48 | |
michael@0 | 49 | function updateTitle(sidebar, title) { |
michael@0 | 50 | let button = buttons.get(sidebar); |
michael@0 | 51 | |
michael@0 | 52 | for (let window of windows(null, { includePrivate: true })) { |
michael@0 | 53 | let { document } = window; |
michael@0 | 54 | |
michael@0 | 55 | // update the button |
michael@0 | 56 | if (button) { |
michael@0 | 57 | setStateFor(button, window, { label: title }); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // update the menuitem |
michael@0 | 61 | let mi = document.getElementById(makeID(sidebar.id)); |
michael@0 | 62 | if (mi) { |
michael@0 | 63 | mi.setAttribute('label', title) |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // update sidebar, if showing |
michael@0 | 67 | if (isSidebarShowing(window, sidebar)) { |
michael@0 | 68 | document.getElementById('sidebar-title').setAttribute('value', title); |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | exports.updateTitle = updateTitle; |
michael@0 | 73 | |
michael@0 | 74 | function updateURL(sidebar, url) { |
michael@0 | 75 | let eleID = makeID(sidebar.id); |
michael@0 | 76 | |
michael@0 | 77 | for (let window of windows(null, { includePrivate: true })) { |
michael@0 | 78 | // update the menuitem |
michael@0 | 79 | let mi = window.document.getElementById(eleID); |
michael@0 | 80 | if (mi) { |
michael@0 | 81 | mi.setAttribute('sidebarurl', url) |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | // update sidebar, if showing |
michael@0 | 85 | if (isSidebarShowing(window, sidebar)) { |
michael@0 | 86 | showSidebar(window, sidebar, url); |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | exports.updateURL = updateURL; |
michael@0 | 91 | |
michael@0 | 92 | function isSidebarShowing(window, sidebar) { |
michael@0 | 93 | let win = window || getMostRecentBrowserWindow(); |
michael@0 | 94 | |
michael@0 | 95 | // make sure there is a window |
michael@0 | 96 | if (!win) { |
michael@0 | 97 | return false; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // make sure there is a sidebar for the window |
michael@0 | 101 | let sb = win.document.getElementById('sidebar'); |
michael@0 | 102 | let sidebarTitle = win.document.getElementById('sidebar-title'); |
michael@0 | 103 | if (!(sb && sidebarTitle)) { |
michael@0 | 104 | return false; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // checks if the sidebar box is hidden |
michael@0 | 108 | let sbb = win.document.getElementById('sidebar-box'); |
michael@0 | 109 | if (!sbb || sbb.hidden) { |
michael@0 | 110 | return false; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | if (sidebarTitle.value == modelFor(sidebar).title) { |
michael@0 | 114 | // checks if the sidebar is loading |
michael@0 | 115 | if (win.gWebPanelURI == modelFor(sidebar).url) { |
michael@0 | 116 | return true; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // checks if the sidebar loaded already |
michael@0 | 120 | let ele = sb.contentDocument && sb.contentDocument.getElementById(WEB_PANEL_BROWSER_ID); |
michael@0 | 121 | if (!ele) { |
michael@0 | 122 | return false; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | if (ele.getAttribute('cachedurl') == modelFor(sidebar).url) { |
michael@0 | 126 | return true; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | if (ele && ele.contentWindow && ele.contentWindow.location == modelFor(sidebar).url) { |
michael@0 | 130 | return true; |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // default |
michael@0 | 135 | return false; |
michael@0 | 136 | } |
michael@0 | 137 | exports.isSidebarShowing = isSidebarShowing; |
michael@0 | 138 | |
michael@0 | 139 | function showSidebar(window, sidebar, newURL) { |
michael@0 | 140 | window = window || getMostRecentBrowserWindow(); |
michael@0 | 141 | |
michael@0 | 142 | let { promise, resolve, reject } = defer(); |
michael@0 | 143 | let model = modelFor(sidebar); |
michael@0 | 144 | |
michael@0 | 145 | if (!newURL && isSidebarShowing(window, sidebar)) { |
michael@0 | 146 | resolve({}); |
michael@0 | 147 | } |
michael@0 | 148 | else if (!isPrivateBrowsingSupported && isWindowPrivate(window)) { |
michael@0 | 149 | reject(Error('You cannot show a sidebar on private windows')); |
michael@0 | 150 | } |
michael@0 | 151 | else { |
michael@0 | 152 | sidebar.once('show', resolve); |
michael@0 | 153 | |
michael@0 | 154 | let menuitem = window.document.getElementById(makeID(model.id)); |
michael@0 | 155 | menuitem.setAttribute('checked', true); |
michael@0 | 156 | |
michael@0 | 157 | window.openWebPanel(model.title, newURL || model.url); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | return promise; |
michael@0 | 161 | } |
michael@0 | 162 | exports.showSidebar = showSidebar; |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | function hideSidebar(window, sidebar) { |
michael@0 | 166 | window = window || getMostRecentBrowserWindow(); |
michael@0 | 167 | |
michael@0 | 168 | let { promise, resolve, reject } = defer(); |
michael@0 | 169 | |
michael@0 | 170 | if (!isSidebarShowing(window, sidebar)) { |
michael@0 | 171 | reject(Error('The sidebar is already hidden')); |
michael@0 | 172 | } |
michael@0 | 173 | else { |
michael@0 | 174 | sidebar.once('hide', resolve); |
michael@0 | 175 | |
michael@0 | 176 | // Below was taken from http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#4775 |
michael@0 | 177 | // the code for window.todggleSideBar().. |
michael@0 | 178 | let { document } = window; |
michael@0 | 179 | let sidebarEle = document.getElementById('sidebar'); |
michael@0 | 180 | let sidebarTitle = document.getElementById('sidebar-title'); |
michael@0 | 181 | let sidebarBox = document.getElementById('sidebar-box'); |
michael@0 | 182 | let sidebarSplitter = document.getElementById('sidebar-splitter'); |
michael@0 | 183 | let commandID = sidebarBox.getAttribute('sidebarcommand'); |
michael@0 | 184 | let sidebarBroadcaster = document.getElementById(commandID); |
michael@0 | 185 | |
michael@0 | 186 | sidebarBox.hidden = true; |
michael@0 | 187 | sidebarSplitter.hidden = true; |
michael@0 | 188 | |
michael@0 | 189 | sidebarEle.setAttribute('src', 'about:blank'); |
michael@0 | 190 | //sidebarEle.docShell.createAboutBlankContentViewer(null); |
michael@0 | 191 | |
michael@0 | 192 | sidebarBroadcaster.removeAttribute('checked'); |
michael@0 | 193 | sidebarBox.setAttribute('sidebarcommand', ''); |
michael@0 | 194 | sidebarTitle.value = ''; |
michael@0 | 195 | sidebarBox.hidden = true; |
michael@0 | 196 | sidebarSplitter.hidden = true; |
michael@0 | 197 | |
michael@0 | 198 | // TODO: perhaps this isn't necessary if the window is not most recent? |
michael@0 | 199 | window.gBrowser.selectedBrowser.focus(); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | return promise; |
michael@0 | 203 | } |
michael@0 | 204 | exports.hideSidebar = hideSidebar; |
michael@0 | 205 | |
michael@0 | 206 | function makeID(id) { |
michael@0 | 207 | return 'jetpack-sidebar-' + id; |
michael@0 | 208 | } |