|
1 /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 "use strict"; |
|
7 |
|
8 const { Cc, Ci, Cu, Cr } = require("chrome"); |
|
9 const { Promise: promise } = Cu.import("resource://gre/modules/Promise.jsm", {}); |
|
10 const EventEmitter = require("devtools/toolkit/event-emitter"); |
|
11 const { DevToolsUtils } = Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm", {}); |
|
12 |
|
13 function NetMonitorPanel(iframeWindow, toolbox) { |
|
14 this.panelWin = iframeWindow; |
|
15 this._toolbox = toolbox; |
|
16 this._destroyer = null; |
|
17 |
|
18 this._view = this.panelWin.NetMonitorView; |
|
19 this._controller = this.panelWin.NetMonitorController; |
|
20 this._controller._target = this.target; |
|
21 |
|
22 EventEmitter.decorate(this); |
|
23 }; |
|
24 |
|
25 exports.NetMonitorPanel = NetMonitorPanel; |
|
26 |
|
27 NetMonitorPanel.prototype = { |
|
28 /** |
|
29 * Open is effectively an asynchronous constructor. |
|
30 * |
|
31 * @return object |
|
32 * A promise that is resolved when the NetMonitor completes opening. |
|
33 */ |
|
34 open: function() { |
|
35 let targetPromise; |
|
36 |
|
37 // Local monitoring needs to make the target remote. |
|
38 if (!this.target.isRemote) { |
|
39 targetPromise = this.target.makeRemote(); |
|
40 } else { |
|
41 targetPromise = promise.resolve(this.target); |
|
42 } |
|
43 |
|
44 return targetPromise |
|
45 .then(() => this._controller.startupNetMonitor()) |
|
46 .then(() => this._controller.connect()) |
|
47 .then(() => { |
|
48 this.isReady = true; |
|
49 this.emit("ready"); |
|
50 return this; |
|
51 }) |
|
52 .then(null, function onError(aReason) { |
|
53 DevToolsUtils.reportException("NetMonitorPanel.prototype.open", aReason); |
|
54 }); |
|
55 }, |
|
56 |
|
57 // DevToolPanel API |
|
58 |
|
59 get target() this._toolbox.target, |
|
60 |
|
61 destroy: function() { |
|
62 // Make sure this panel is not already destroyed. |
|
63 if (this._destroyer) { |
|
64 return this._destroyer; |
|
65 } |
|
66 |
|
67 return this._destroyer = this._controller.shutdownNetMonitor().then(() => { |
|
68 this.emit("destroyed"); |
|
69 }); |
|
70 } |
|
71 }; |