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 | |
michael@0 | 5 | const Cu = Components.utils; |
michael@0 | 6 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 7 | Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); |
michael@0 | 8 | const {gDevTools} = Cu.import("resource:///modules/devtools/gDevTools.jsm", {}); |
michael@0 | 9 | |
michael@0 | 10 | const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); |
michael@0 | 11 | const {require} = devtools; |
michael@0 | 12 | |
michael@0 | 13 | const {ConnectionManager, Connection} |
michael@0 | 14 | = require("devtools/client/connection-manager"); |
michael@0 | 15 | const {getDeviceFront} = require("devtools/server/actors/device"); |
michael@0 | 16 | const {getTargetForApp, launchApp, closeApp} |
michael@0 | 17 | = require("devtools/app-actor-front"); |
michael@0 | 18 | const DeviceStore = require("devtools/app-manager/device-store"); |
michael@0 | 19 | const WebappsStore = require("devtools/app-manager/webapps-store"); |
michael@0 | 20 | const promise = require("devtools/toolkit/deprecated-sync-thenables"); |
michael@0 | 21 | const DEFAULT_APP_ICON = "chrome://browser/skin/devtools/app-manager/default-app-icon.png"; |
michael@0 | 22 | |
michael@0 | 23 | window.addEventListener("message", function(event) { |
michael@0 | 24 | try { |
michael@0 | 25 | let message = JSON.parse(event.data); |
michael@0 | 26 | if (message.name == "connection") { |
michael@0 | 27 | let cid = parseInt(message.cid); |
michael@0 | 28 | for (let c of ConnectionManager.connections) { |
michael@0 | 29 | if (c.uid == cid) { |
michael@0 | 30 | UI.connection = c; |
michael@0 | 31 | UI.onNewConnection(); |
michael@0 | 32 | break; |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | } |
michael@0 | 36 | } catch(e) { |
michael@0 | 37 | Cu.reportError(e); |
michael@0 | 38 | } |
michael@0 | 39 | }); |
michael@0 | 40 | |
michael@0 | 41 | window.addEventListener("unload", function onUnload() { |
michael@0 | 42 | window.removeEventListener("unload", onUnload); |
michael@0 | 43 | UI.destroy(); |
michael@0 | 44 | }); |
michael@0 | 45 | |
michael@0 | 46 | let UI = { |
michael@0 | 47 | init: function() { |
michael@0 | 48 | this.showFooterIfNeeded(); |
michael@0 | 49 | this.setTab("apps"); |
michael@0 | 50 | if (this.connection) { |
michael@0 | 51 | this.onNewConnection(); |
michael@0 | 52 | } else { |
michael@0 | 53 | this.hide(); |
michael@0 | 54 | } |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | destroy: function() { |
michael@0 | 58 | if (this.connection) { |
michael@0 | 59 | this.connection.off(Connection.Events.STATUS_CHANGED, this._onConnectionStatusChange); |
michael@0 | 60 | } |
michael@0 | 61 | if (this.store) { |
michael@0 | 62 | this.store.destroy(); |
michael@0 | 63 | } |
michael@0 | 64 | if (this.template) { |
michael@0 | 65 | this.template.destroy(); |
michael@0 | 66 | } |
michael@0 | 67 | }, |
michael@0 | 68 | |
michael@0 | 69 | showFooterIfNeeded: function() { |
michael@0 | 70 | let footer = document.querySelector("#connection-footer"); |
michael@0 | 71 | if (window.parent == window) { |
michael@0 | 72 | // We're alone. Let's add a footer. |
michael@0 | 73 | footer.removeAttribute("hidden"); |
michael@0 | 74 | footer.src = "chrome://browser/content/devtools/app-manager/connection-footer.xhtml"; |
michael@0 | 75 | } else { |
michael@0 | 76 | footer.setAttribute("hidden", "true"); |
michael@0 | 77 | } |
michael@0 | 78 | }, |
michael@0 | 79 | |
michael@0 | 80 | hide: function() { |
michael@0 | 81 | document.body.classList.add("notconnected"); |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | show: function() { |
michael@0 | 85 | document.body.classList.remove("notconnected"); |
michael@0 | 86 | }, |
michael@0 | 87 | |
michael@0 | 88 | onNewConnection: function() { |
michael@0 | 89 | this.connection.on(Connection.Events.STATUS_CHANGED, this._onConnectionStatusChange); |
michael@0 | 90 | |
michael@0 | 91 | this.store = Utils.mergeStores({ |
michael@0 | 92 | "device": new DeviceStore(this.connection), |
michael@0 | 93 | "apps": new WebappsStore(this.connection), |
michael@0 | 94 | }); |
michael@0 | 95 | |
michael@0 | 96 | if (this.template) { |
michael@0 | 97 | this.template.destroy(); |
michael@0 | 98 | } |
michael@0 | 99 | this.template = new Template(document.body, this.store, Utils.l10n); |
michael@0 | 100 | |
michael@0 | 101 | this.template.start(); |
michael@0 | 102 | this._onConnectionStatusChange(); |
michael@0 | 103 | }, |
michael@0 | 104 | |
michael@0 | 105 | setWallpaper: function(dataurl) { |
michael@0 | 106 | document.getElementById("meta").style.backgroundImage = "url(" + dataurl + ")"; |
michael@0 | 107 | }, |
michael@0 | 108 | |
michael@0 | 109 | _onConnectionStatusChange: function() { |
michael@0 | 110 | if (this.connection.status != Connection.Status.CONNECTED) { |
michael@0 | 111 | this.hide(); |
michael@0 | 112 | this.listTabsResponse = null; |
michael@0 | 113 | } else { |
michael@0 | 114 | this.show(); |
michael@0 | 115 | this.connection.client.listTabs( |
michael@0 | 116 | response => { |
michael@0 | 117 | this.listTabsResponse = response; |
michael@0 | 118 | let front = getDeviceFront(this.connection.client, this.listTabsResponse); |
michael@0 | 119 | front.getWallpaper().then(longstr => { |
michael@0 | 120 | longstr.string().then(dataURL => { |
michael@0 | 121 | longstr.release().then(null, Cu.reportError); |
michael@0 | 122 | this.setWallpaper(dataURL); |
michael@0 | 123 | }); |
michael@0 | 124 | }); |
michael@0 | 125 | if (Services.prefs.getBoolPref("devtools.chrome.enabled")) { |
michael@0 | 126 | let rootButton = document.getElementById("root-actor-debug"); |
michael@0 | 127 | if (response.consoleActor) { |
michael@0 | 128 | rootButton.removeAttribute("hidden"); |
michael@0 | 129 | } else { |
michael@0 | 130 | rootButton.setAttribute("hidden", "true"); |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | ); |
michael@0 | 135 | } |
michael@0 | 136 | }, |
michael@0 | 137 | |
michael@0 | 138 | get connected() { return !!this.listTabsResponse; }, |
michael@0 | 139 | |
michael@0 | 140 | setTab: function(name) { |
michael@0 | 141 | var tab = document.querySelector(".tab.selected"); |
michael@0 | 142 | var panel = document.querySelector(".tabpanel.selected"); |
michael@0 | 143 | |
michael@0 | 144 | if (tab) tab.classList.remove("selected"); |
michael@0 | 145 | if (panel) panel.classList.remove("selected"); |
michael@0 | 146 | |
michael@0 | 147 | var tab = document.querySelector(".tab." + name); |
michael@0 | 148 | var panel = document.querySelector(".tabpanel." + name); |
michael@0 | 149 | |
michael@0 | 150 | if (tab) tab.classList.add("selected"); |
michael@0 | 151 | if (panel) panel.classList.add("selected"); |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | openToolboxForRootActor: function() { |
michael@0 | 155 | if (!this.connected) { |
michael@0 | 156 | return; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | let options = { |
michael@0 | 160 | form: this.listTabsResponse, |
michael@0 | 161 | client: this.connection.client, |
michael@0 | 162 | chrome: true |
michael@0 | 163 | }; |
michael@0 | 164 | devtools.TargetFactory.forRemoteTab(options).then((target) => { |
michael@0 | 165 | top.UI.openAndShowToolboxForTarget(target, "Main process", null); |
michael@0 | 166 | }); |
michael@0 | 167 | }, |
michael@0 | 168 | |
michael@0 | 169 | openToolboxForApp: function(manifest) { |
michael@0 | 170 | if (!this.connected) { |
michael@0 | 171 | return; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | let app = this.store.object.apps.all.filter(a => a.manifestURL == manifest)[0]; |
michael@0 | 175 | getTargetForApp(this.connection.client, |
michael@0 | 176 | this.listTabsResponse.webappsActor, |
michael@0 | 177 | manifest).then((target) => { |
michael@0 | 178 | |
michael@0 | 179 | top.UI.openAndShowToolboxForTarget(target, app.name, app.iconURL); |
michael@0 | 180 | }, console.error); |
michael@0 | 181 | }, |
michael@0 | 182 | |
michael@0 | 183 | _getTargetForTab: function (form) { |
michael@0 | 184 | let options = { |
michael@0 | 185 | form: form, |
michael@0 | 186 | client: this.connection.client, |
michael@0 | 187 | chrome: false |
michael@0 | 188 | }; |
michael@0 | 189 | let deferred = promise.defer(); |
michael@0 | 190 | return devtools.TargetFactory.forRemoteTab(options); |
michael@0 | 191 | }, |
michael@0 | 192 | |
michael@0 | 193 | openToolboxForTab: function (aNode) { |
michael@0 | 194 | let index = Array.prototype.indexOf.apply( |
michael@0 | 195 | aNode.parentNode.parentNode.parentNode.children, |
michael@0 | 196 | [aNode.parentNode.parentNode]); |
michael@0 | 197 | this.connection.client.listTabs( |
michael@0 | 198 | response => { |
michael@0 | 199 | let tab = response.tabs[index]; |
michael@0 | 200 | this._getTargetForTab(tab).then(target => { |
michael@0 | 201 | top.UI.openAndShowToolboxForTarget( |
michael@0 | 202 | target, tab.title, DEFAULT_APP_ICON); |
michael@0 | 203 | }, console.error); |
michael@0 | 204 | } |
michael@0 | 205 | ); |
michael@0 | 206 | }, |
michael@0 | 207 | |
michael@0 | 208 | startApp: function(manifest) { |
michael@0 | 209 | if (!this.connected) { |
michael@0 | 210 | return promise.reject(); |
michael@0 | 211 | } |
michael@0 | 212 | return launchApp(this.connection.client, |
michael@0 | 213 | this.listTabsResponse.webappsActor, |
michael@0 | 214 | manifest); |
michael@0 | 215 | }, |
michael@0 | 216 | |
michael@0 | 217 | stopApp: function(manifest) { |
michael@0 | 218 | if (!this.connected) { |
michael@0 | 219 | return promise.reject(); |
michael@0 | 220 | } |
michael@0 | 221 | return closeApp(this.connection.client, |
michael@0 | 222 | this.listTabsResponse.webappsActor, |
michael@0 | 223 | manifest); |
michael@0 | 224 | }, |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | // This must be bound immediately, as it might be used via the message listener |
michael@0 | 228 | // before UI.init() has been called. |
michael@0 | 229 | UI._onConnectionStatusChange = UI._onConnectionStatusChange.bind(UI); |