Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | const Ci = Components.interfaces; |
michael@0 | 7 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 8 | Cu.import("resource:///modules/devtools/gDevTools.jsm"); |
michael@0 | 9 | |
michael@0 | 10 | const {Simulator} = Cu.import("resource://gre/modules/devtools/Simulator.jsm") |
michael@0 | 11 | const {Devices} = Cu.import("resource://gre/modules/devtools/Devices.jsm"); |
michael@0 | 12 | const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}); |
michael@0 | 13 | const {require} = devtools; |
michael@0 | 14 | |
michael@0 | 15 | const {ConnectionManager, Connection} = require("devtools/client/connection-manager"); |
michael@0 | 16 | const {getDeviceFront} = require("devtools/server/actors/device"); |
michael@0 | 17 | const ConnectionStore = require("devtools/app-manager/connection-store"); |
michael@0 | 18 | const DeviceStore = require("devtools/app-manager/device-store"); |
michael@0 | 19 | const simulatorsStore = require("devtools/app-manager/simulators-store"); |
michael@0 | 20 | const adbStore = require("devtools/app-manager/builtin-adb-store"); |
michael@0 | 21 | |
michael@0 | 22 | window.addEventListener("unload", function onUnload() { |
michael@0 | 23 | window.removeEventListener("unload", onUnload); |
michael@0 | 24 | UI.destroy(); |
michael@0 | 25 | }); |
michael@0 | 26 | |
michael@0 | 27 | let UI = { |
michael@0 | 28 | init: function() { |
michael@0 | 29 | this.useFloatingScrollbarsIfNeeded(); |
michael@0 | 30 | let connections = ConnectionManager.connections; |
michael@0 | 31 | if (connections.length > 0) { |
michael@0 | 32 | let hash = window.location.hash; |
michael@0 | 33 | if (hash) { |
michael@0 | 34 | let res = (/cid=([^&]+)/).exec(hash) |
michael@0 | 35 | if (res) { |
michael@0 | 36 | let [,cid] = res; |
michael@0 | 37 | this.connection = connections.filter((({uid}) => uid == cid))[0]; |
michael@0 | 38 | } |
michael@0 | 39 | } |
michael@0 | 40 | if (!this.connection) { |
michael@0 | 41 | // We take the first connection available. |
michael@0 | 42 | this.connection = connections[0]; |
michael@0 | 43 | } |
michael@0 | 44 | } else { |
michael@0 | 45 | let host = Services.prefs.getCharPref("devtools.debugger.remote-host"); |
michael@0 | 46 | let port = Services.prefs.getIntPref("devtools.debugger.remote-port"); |
michael@0 | 47 | this.connection = ConnectionManager.createConnection(host, port); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | window.location.hash = "cid=" + this.connection.uid; |
michael@0 | 51 | window.parent.postMessage(JSON.stringify({name:"connection",cid:this.connection.uid}), "*"); |
michael@0 | 52 | |
michael@0 | 53 | this.store = Utils.mergeStores({ |
michael@0 | 54 | "device": new DeviceStore(this.connection), |
michael@0 | 55 | "connection": new ConnectionStore(this.connection), |
michael@0 | 56 | "simulators": simulatorsStore, |
michael@0 | 57 | "adb": adbStore |
michael@0 | 58 | }); |
michael@0 | 59 | |
michael@0 | 60 | let pre = document.querySelector("#logs > pre"); |
michael@0 | 61 | pre.textContent = this.connection.logs; |
michael@0 | 62 | pre.scrollTop = pre.scrollTopMax; |
michael@0 | 63 | this.connection.on(Connection.Events.NEW_LOG, this._onNewLog); |
michael@0 | 64 | |
michael@0 | 65 | this.template = new Template(document.body, this.store, Utils.l10n); |
michael@0 | 66 | this.template.start(); |
michael@0 | 67 | |
michael@0 | 68 | this._onSimulatorConnected = this._onSimulatorConnected.bind(this); |
michael@0 | 69 | this._onSimulatorDisconnected = this._onSimulatorDisconnected.bind(this); |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | destroy: function() { |
michael@0 | 73 | this.store.destroy(); |
michael@0 | 74 | this.connection.off(Connection.Events.NEW_LOG, this._onNewLog); |
michael@0 | 75 | this.template.destroy(); |
michael@0 | 76 | }, |
michael@0 | 77 | |
michael@0 | 78 | _onNewLog: function(event, str) { |
michael@0 | 79 | let pre = document.querySelector("#logs > pre"); |
michael@0 | 80 | pre.textContent += "\n" + str; |
michael@0 | 81 | pre.scrollTop = pre.scrollTopMax; |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | useFloatingScrollbarsIfNeeded: function() { |
michael@0 | 85 | if (Services.appinfo.OS == "Darwin") { |
michael@0 | 86 | return; |
michael@0 | 87 | } |
michael@0 | 88 | let scrollbarsUrl = Services.io.newURI("chrome://browser/skin/devtools/floating-scrollbars-light.css", null, null); |
michael@0 | 89 | let winUtils = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); |
michael@0 | 90 | winUtils.loadSheet(scrollbarsUrl, winUtils.AGENT_SHEET); |
michael@0 | 91 | let computedStyle = window.getComputedStyle(document.documentElement); |
michael@0 | 92 | if (computedStyle) { // Force a reflow to take the new css into account |
michael@0 | 93 | let display = computedStyle.display; // Save display value |
michael@0 | 94 | document.documentElement.style.display = "none"; |
michael@0 | 95 | window.getComputedStyle(document.documentElement).display; // Flush |
michael@0 | 96 | document.documentElement.style.display = display; // Restore |
michael@0 | 97 | } |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | disconnect: function() { |
michael@0 | 101 | this.connection.disconnect(); |
michael@0 | 102 | }, |
michael@0 | 103 | |
michael@0 | 104 | connect: function() { |
michael@0 | 105 | this.connection.connect(); |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | editConnectionParameters: function() { |
michael@0 | 109 | document.body.classList.add("edit-connection"); |
michael@0 | 110 | document.querySelector("input.host").focus(); |
michael@0 | 111 | }, |
michael@0 | 112 | |
michael@0 | 113 | saveConnectionInfo: function() { |
michael@0 | 114 | document.body.classList.remove("edit-connection"); |
michael@0 | 115 | document.querySelector("#connect-button").focus(); |
michael@0 | 116 | let host = document.querySelector("input.host").value; |
michael@0 | 117 | let port = document.querySelector("input.port").value; |
michael@0 | 118 | this.connection.port = port; |
michael@0 | 119 | this.connection.host = host; |
michael@0 | 120 | Services.prefs.setCharPref("devtools.debugger.remote-host", host); |
michael@0 | 121 | Services.prefs.setIntPref("devtools.debugger.remote-port", port); |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | showSimulatorList: function() { |
michael@0 | 125 | document.body.classList.add("show-simulators"); |
michael@0 | 126 | }, |
michael@0 | 127 | |
michael@0 | 128 | cancelShowSimulatorList: function() { |
michael@0 | 129 | document.body.classList.remove("show-simulators"); |
michael@0 | 130 | }, |
michael@0 | 131 | |
michael@0 | 132 | installSimulator: function() { |
michael@0 | 133 | let url = "https://developer.mozilla.org/docs/Mozilla/Firefox_OS/Using_the_App_Manager#Simulator"; |
michael@0 | 134 | window.open(url); |
michael@0 | 135 | }, |
michael@0 | 136 | |
michael@0 | 137 | startSimulator: function(version) { |
michael@0 | 138 | this._portBeforeSimulatorStarted = this.connection.port; |
michael@0 | 139 | let port = ConnectionManager.getFreeTCPPort(); |
michael@0 | 140 | let simulator = Simulator.getByVersion(version); |
michael@0 | 141 | if (!simulator) { |
michael@0 | 142 | this.connection.log("Error: can't find simulator: " + version); |
michael@0 | 143 | return; |
michael@0 | 144 | } |
michael@0 | 145 | if (!simulator.launch) { |
michael@0 | 146 | this.connection.log("Error: invalid simulator: " + version); |
michael@0 | 147 | return; |
michael@0 | 148 | } |
michael@0 | 149 | this.connection.log("Found simulator: " + version); |
michael@0 | 150 | this.connection.log("Starting simulator..."); |
michael@0 | 151 | |
michael@0 | 152 | this.simulator = simulator; |
michael@0 | 153 | this.simulator.launch({ port: port }) |
michael@0 | 154 | .then(() => { |
michael@0 | 155 | this.connection.log("Simulator ready. Connecting."); |
michael@0 | 156 | this.connection.port = port; |
michael@0 | 157 | this.connection.host = "localhost"; |
michael@0 | 158 | this.connection.once("connected", |
michael@0 | 159 | this._onSimulatorConnected); |
michael@0 | 160 | this.connection.once("disconnected", |
michael@0 | 161 | this._onSimulatorDisconnected); |
michael@0 | 162 | this.connection.keepConnecting = true; |
michael@0 | 163 | this.connection.connect(); |
michael@0 | 164 | }); |
michael@0 | 165 | document.body.classList.remove("show-simulators"); |
michael@0 | 166 | }, |
michael@0 | 167 | |
michael@0 | 168 | _onSimulatorConnected: function() { |
michael@0 | 169 | this.connection.log("Connected to simulator."); |
michael@0 | 170 | this.connection.keepConnecting = false; |
michael@0 | 171 | |
michael@0 | 172 | // This doesn't change the current (successful) connection, |
michael@0 | 173 | // but makes sure that when the simulator is disconnected, the |
michael@0 | 174 | // connection doesn't end up with a random port number (from |
michael@0 | 175 | // getFreeTCPPort). |
michael@0 | 176 | this.connection.port = this._portBeforeSimulatorStarted; |
michael@0 | 177 | }, |
michael@0 | 178 | |
michael@0 | 179 | _onSimulatorDisconnected: function() { |
michael@0 | 180 | this.connection.off("connected", this._onSimulatorConnected); |
michael@0 | 181 | }, |
michael@0 | 182 | |
michael@0 | 183 | connectToAdbDevice: function(name) { |
michael@0 | 184 | let device = Devices.getByName(name); |
michael@0 | 185 | device.connect().then((port) => { |
michael@0 | 186 | this.connection.host = "localhost"; |
michael@0 | 187 | this.connection.port = port; |
michael@0 | 188 | this.connect(); |
michael@0 | 189 | }); |
michael@0 | 190 | }, |
michael@0 | 191 | |
michael@0 | 192 | screenshot: function() { |
michael@0 | 193 | this.connection.client.listTabs( |
michael@0 | 194 | response => { |
michael@0 | 195 | let front = getDeviceFront(this.connection.client, response); |
michael@0 | 196 | front.screenshotToBlob().then(blob => { |
michael@0 | 197 | let topWindow = Services.wm.getMostRecentWindow("navigator:browser"); |
michael@0 | 198 | let gBrowser = topWindow.gBrowser; |
michael@0 | 199 | let url = topWindow.URL.createObjectURL(blob); |
michael@0 | 200 | let tab = gBrowser.selectedTab = gBrowser.addTab(url); |
michael@0 | 201 | tab.addEventListener("TabClose", function onTabClose() { |
michael@0 | 202 | tab.removeEventListener("TabClose", onTabClose, false); |
michael@0 | 203 | topWindow.URL.revokeObjectURL(url); |
michael@0 | 204 | }, false); |
michael@0 | 205 | }).then(null, console.error); |
michael@0 | 206 | } |
michael@0 | 207 | ); |
michael@0 | 208 | }, |
michael@0 | 209 | } |