|
| 1 | +/* See license.txt for terms of usage */ |
| 2 | + |
| 3 | +"use strict"; |
| 4 | + |
| 5 | +module.metadata = { |
| 6 | + "stability": "stable" |
| 7 | +}; |
| 8 | + |
| 9 | +// Add-on SDK |
| 10 | +const options = require("@loader/options"); |
| 11 | +const { Cu } = require("chrome"); |
| 12 | +const { Class } = require("sdk/core/heritage"); |
| 13 | +const { prefs } = require("sdk/simple-prefs"); |
| 14 | +const { defer, resolve } = require("sdk/core/promise"); |
| 15 | +const { emit } = require("sdk/event/core"); |
| 16 | + |
| 17 | +// Firebug.SDK |
| 18 | +const { DebuggerServer, DebuggerClient, devtools } = require("firebug.sdk/lib/core/devtools.js"); |
| 19 | +const { Trace, TraceError } = require("firebug.sdk/lib/core/trace.js").get(module.id); |
| 20 | +const { TriggerToolboxOverlay } = require("./trigger-toolbox-overlay.js"); |
| 21 | + |
| 22 | +// Platform |
| 23 | +const { HarAutomation } = devtools.require("devtools/client/netmonitor/har/har-automation"); |
| 24 | + |
| 25 | +// Constants |
| 26 | +const TargetFactory = devtools.TargetFactory; |
| 27 | + |
| 28 | +/** |
| 29 | + * TODO: docs |
| 30 | + */ |
| 31 | +const TriggerToolbox = |
| 32 | +/** @lends TriggerToolbox */ |
| 33 | +{ |
| 34 | + // Initialization |
| 35 | + |
| 36 | + initialize: function() { |
| 37 | + this.onTabListChanged = this.onTabListChanged.bind(this); |
| 38 | + |
| 39 | + // Map<tab, TriggerToolboxOverlay> |
| 40 | + this.overlays = new Map(); |
| 41 | + |
| 42 | + if (!prefs.autoConnect) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + this.connect().then(client => { |
| 47 | + this.onReady(client); |
| 48 | + }); |
| 49 | + }, |
| 50 | + |
| 51 | + onReady: function(client) { |
| 52 | + Trace.sysout("TriggerToolbox.onReady;", client); |
| 53 | + |
| 54 | + this.client = client; |
| 55 | + this.client.addListener("tabListChanged", this.onTabListChanged); |
| 56 | + |
| 57 | + // Ensure that initial connection for the default tab is created. |
| 58 | + this.onTabListChanged(); |
| 59 | + }, |
| 60 | + |
| 61 | + shutdown: function() { |
| 62 | + this.close(); |
| 63 | + }, |
| 64 | + |
| 65 | + // Connect/close |
| 66 | + |
| 67 | + connect: function() { |
| 68 | + let deferred = defer(); |
| 69 | + |
| 70 | + if (!DebuggerServer.initialized) { |
| 71 | + DebuggerServer.init(); |
| 72 | + DebuggerServer.addBrowserActors(); |
| 73 | + } |
| 74 | + |
| 75 | + let client = new DebuggerClient(DebuggerServer.connectPipe()); |
| 76 | + client.connect(() => { |
| 77 | + Trace.sysout("TriggerToolbox.connect; DONE", client); |
| 78 | + deferred.resolve(client); |
| 79 | + }); |
| 80 | + |
| 81 | + return deferred.promise; |
| 82 | + }, |
| 83 | + |
| 84 | + close: function() { |
| 85 | + Trace.sysout("TriggerToolbox.close;"); |
| 86 | + |
| 87 | + if (!this.target) { |
| 88 | + return resolve(); |
| 89 | + } |
| 90 | + |
| 91 | + if (this.destroyer) { |
| 92 | + return this.destroyer.promise; |
| 93 | + } |
| 94 | + |
| 95 | + this.destroyer = defer(); |
| 96 | + |
| 97 | + this.client.close(() => { |
| 98 | + this.destroyer.resolve(); |
| 99 | + }); |
| 100 | + |
| 101 | + return this.destroyer.promise; |
| 102 | + }, |
| 103 | + |
| 104 | + // Events |
| 105 | + |
| 106 | + /** |
| 107 | + * Handle 'tabListChanged' event and attach the selected tab. |
| 108 | + * Note that there is an extra connection created for each tab. |
| 109 | + * So, network events ('tabListChanged' and 'networkEventUpdate') |
| 110 | + * are sent only to the attached automation.collector object. |
| 111 | + * |
| 112 | + * xxxHonza: if we remove the check in HarCollector.onNetworkEventUpdate |
| 113 | + * method (labeled as: 'Skip events from unknown actors') we might |
| 114 | + * do everything through one connection. But this needs testing. |
| 115 | + */ |
| 116 | + onTabListChanged: function(eventId, packet) { |
| 117 | + Trace.sysout("TriggerToolbox.onTabListChanged;", arguments); |
| 118 | + |
| 119 | + // Execute 'listTabs' to make sure that 'tabListChanged' event |
| 120 | + // will be sent the next time (this is historical complexity |
| 121 | + // of the backend). This must be done after every 'tabListChanged'. |
| 122 | + this.client.listTabs(response => { |
| 123 | + if (response.error) { |
| 124 | + Trace.sysout("TriggerToolbox.onTabListChanged; ERROR " + |
| 125 | + response.message, response); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + let currentTab = response.tabs[response.selected]; |
| 130 | + Trace.sysout("TriggerToolbox.onTabListChanged; " + |
| 131 | + "(initial connection): " + currentTab.actor, response); |
| 132 | + |
| 133 | + // Bail out if the tab already has its own connection. |
| 134 | + if (this.overlays.has(currentTab.actor)) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + // Create new connection for the current tab. |
| 139 | + this.connect().then(client => { |
| 140 | + // Execute list of tabs for the new connection (it'll maintain |
| 141 | + // it's own tab actors on the backend). |
| 142 | + client.listTabs(response => { |
| 143 | + let tabForm = response.tabs[response.selected]; |
| 144 | + let tabActor = tabForm.actor; |
| 145 | + |
| 146 | + Trace.sysout("TriggerToolbox.onTabListChanged; " + |
| 147 | + "current tab: " + tabActor, tabForm); |
| 148 | + |
| 149 | + // Attach to the current tab using the new connection. |
| 150 | + this.attachTab(tabForm, client).then(result => { |
| 151 | + this.overlays.set(currentTab.actor, result); |
| 152 | + |
| 153 | + Trace.sysout("TriggerToolbox.onTabListChanged; tab attached: " + |
| 154 | + currentTab.actor, this.overlays); |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | + }, |
| 160 | + |
| 161 | + onTabNavigated: function(packet) { |
| 162 | + Trace.sysout("TriggerToolbox.onTabNavigated; " + packet.from, packet); |
| 163 | + }, |
| 164 | + |
| 165 | + onTabDetached: function(packet) { |
| 166 | + Trace.sysout("TriggerToolbox.onTabDetached; " + packet.from, packet); |
| 167 | + |
| 168 | + var tabActor = packet.from; |
| 169 | + |
| 170 | + // Destroy the automation object and close its connection. |
| 171 | + var entry = this.overlays.get(tabActor); |
| 172 | + if (entry) { |
| 173 | + entry.overlay.destroy(); |
| 174 | + entry.automation.destroy(); |
| 175 | + entry.client.close(); |
| 176 | + |
| 177 | + this.overlays.delete(tabActor); |
| 178 | + } |
| 179 | + }, |
| 180 | + |
| 181 | + /** |
| 182 | + * Attach to given tab. |
| 183 | + */ |
| 184 | + attachTab: function(tab, client) { |
| 185 | + Trace.sysout("TriggerToolbox.attachTab; " + tab.actor); |
| 186 | + |
| 187 | + let config = { |
| 188 | + form: tab, |
| 189 | + client: client, |
| 190 | + chrome: false, |
| 191 | + }; |
| 192 | + |
| 193 | + // Create target, automation object and the toolbox overlay object |
| 194 | + // This is what the real Toolbox does (but the Toolbox |
| 195 | + // isn't available at the moment). |
| 196 | + return TargetFactory.forRemoteTab(config).then(target => { |
| 197 | + Trace.sysout("TriggerToolbox.attachTab; target", target); |
| 198 | + |
| 199 | + // Simulate the Toolbox object since the TriggerToolboxOverlay |
| 200 | + // is based on it. |
| 201 | + // xxxHonza: If TriggerToolboxOverlay is based on the target |
| 202 | + // things would be easier. |
| 203 | + var toolbox = { |
| 204 | + target: target, |
| 205 | + getPanel: function() {}, |
| 206 | + on: function() {} |
| 207 | + }; |
| 208 | + |
| 209 | + var automation = new HarAutomation(toolbox); |
| 210 | + |
| 211 | + // Create toolbox overlay (just like for the real Toolbox). |
| 212 | + let options = { |
| 213 | + toolbox: toolbox, |
| 214 | + automation: automation, |
| 215 | + } |
| 216 | + |
| 217 | + // Instantiate the toolbox overlay and simulate onReady event. |
| 218 | + var overlay = new TriggerToolboxOverlay(options); |
| 219 | + overlay.onReady({}); |
| 220 | + |
| 221 | + Trace.sysout("TriggerToolbox.onTabSelected; New automation", options); |
| 222 | + |
| 223 | + return { |
| 224 | + overlay: overlay, |
| 225 | + automation: automation, |
| 226 | + client: client |
| 227 | + }; |
| 228 | + }); |
| 229 | + } |
| 230 | +}; |
| 231 | + |
| 232 | +// Exports from this module |
| 233 | +exports.TriggerToolbox = TriggerToolbox; |
0 commit comments