OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @constructor | |
7 * @extends {WebInspector.SDKObject} | |
8 * @param {!WebInspector.Target} target | |
9 * @implements {BrowserAgent.Dispatcher} | |
10 */ | |
11 WebInspector.RemoteLocationManager = function(target) | |
12 { | |
13 WebInspector.SDKObject.call(this, target); | |
14 /** @type {!Map<string, ?WebInspector.Target>} */ | |
15 this._connectedTargets = new Map(); | |
16 target.registerBrowserDispatcher(this); | |
17 this._defaultLocations = [{host: "localhost", port: 9229}]; | |
18 | |
19 this._throttler = new WebInspector.Throttler(1000); | |
20 this._throttler.schedule(this._discoverTargets.bind(this)); | |
21 } | |
22 | |
23 WebInspector.RemoteLocationManager.prototype = { | |
24 /** | |
25 * @override | |
26 * @param {string} targetId | |
27 * @param {string} message | |
28 */ | |
29 dispatchMessage: function(targetId, message) | |
30 { | |
31 var target = this._connectedTargets.get(targetId); | |
32 if (target) | |
33 target.connection().dispatch(message); | |
34 }, | |
35 | |
36 /** | |
37 * @return {!Promise} | |
38 */ | |
39 _discoverTargets: function() | |
40 { | |
41 return this.target().browserAgent().setRemoteLocations(this._defaultLoca
tions, this._requestTargets.bind(this)); | |
42 }, | |
43 | |
44 /** | |
45 * @param {?Protocol.Error} error | |
46 * @return {!Promise} | |
47 */ | |
48 _requestTargets: function(error) | |
49 { | |
50 if (error) { | |
51 console.error(error); | |
52 return Promise.resolve(); | |
53 } | |
54 | |
55 return this.target().browserAgent().getTargets(this._processTargets.bind
(this)); | |
56 }, | |
57 | |
58 /** | |
59 * @param {?Protocol.Error} error | |
60 * @param {!Array<!BrowserAgent.TargetInfo>} targetInfos | |
61 * @return {!Promise} | |
62 */ | |
63 _processTargets: function(error, targetInfos) | |
64 { | |
65 if (error) { | |
66 console.error(error); | |
67 return Promise.resolve(); | |
68 } | |
69 /** @type {!Map<string, !BrowserAgent.TargetInfo>} */ | |
70 var newTargetInfos = new Map(); | |
71 for (var info of targetInfos) { | |
72 if (info.type !== "node") | |
73 continue; | |
74 newTargetInfos.set(info.targetId, info); | |
75 } | |
76 | |
77 for (var targetId of this._connectedTargets.keys()) { | |
78 if (!newTargetInfos.has(targetId)) { | |
79 var target = this._connectedTargets.get(targetId); | |
80 this._connectedTargets.delete(targetId); | |
81 if (target) | |
82 target.dispose(); | |
83 } | |
84 } | |
85 | |
86 var promises = []; | |
87 for (var targetId of newTargetInfos.keys()) { | |
88 if (this._connectedTargets.has(targetId)) | |
89 continue; | |
90 | |
91 this._connectedTargets.set(targetId, null); | |
92 promises.push(this.target().browserAgent().attach(targetId, this._cr
eateConnection.bind(this, targetId, newTargetInfos.get(targetId).title))); | |
93 } | |
94 return Promise.all(promises).then(() => this._throttler.schedule(this._d
iscoverTargets.bind(this))); | |
95 }, | |
96 | |
97 /** | |
98 * @param {string} targetId | |
99 * @param {string} title | |
100 * @param {?Protocol.Error} error | |
101 * @param {boolean} success | |
102 */ | |
103 _createConnection: function(targetId, title, error, success) | |
104 { | |
105 if (!success || !this._connectedTargets.has(targetId)) { | |
106 // Could not attach or target was deleted while we were connecting,
do not proceed. | |
107 return; | |
108 } | |
109 | |
110 var nodeConnection = new WebInspector.RemoteLocationConnection(this.targ
et().browserAgent(), targetId); | |
111 var nodeCapabilities = WebInspector.Target.Capability.JS; | |
112 var target = WebInspector.targetManager.createTarget(title, nodeCapabili
ties, nodeConnection, this.target()); | |
113 this._connectedTargets.set(targetId, target); | |
114 target.runtimeAgent().runIfWaitingForDebugger(); | |
115 }, | |
116 | |
117 __proto__: WebInspector.SDKObject.prototype | |
118 } | |
119 | |
120 /** | |
121 * @constructor | |
122 * @extends {InspectorBackendClass.Connection} | |
123 * @param {!Protocol.BrowserAgent} agent | |
124 * @param {string} targetId | |
125 */ | |
126 WebInspector.RemoteLocationConnection = function(agent, targetId) | |
127 { | |
128 InspectorBackendClass.Connection.call(this); | |
129 this._agent = agent; | |
130 this._targetId = targetId; | |
131 } | |
132 | |
133 WebInspector.RemoteLocationConnection.prototype = { | |
134 /** | |
135 * @override | |
136 * @param {!Object} messageObject | |
137 */ | |
138 sendMessage: function(messageObject) | |
139 { | |
140 this._agent.sendMessage(this._targetId, JSON.stringify(messageObject)); | |
141 }, | |
142 | |
143 /** | |
144 * @override | |
145 */ | |
146 forceClose: function() | |
147 { | |
148 this._agent.detach(this._targetId, () => {}); | |
149 }, | |
150 | |
151 __proto__: InspectorBackendClass.Connection.prototype | |
152 } | |
OLD | NEW |