Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(614)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/SubTargetsManager.js

Issue 2354973003: [DevTools] Move subtargets functionality from ServiceWorker to Target domain. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.SDKModel}
8 * @param {!WebInspector.Target} target
9 */
10 WebInspector.SubTargetsManager = function(target)
11 {
12 WebInspector.SDKModel.call(this, WebInspector.SubTargetsManager, target);
13 target.registerTargetDispatcher(new WebInspector.SubTargetsDispatcher(this)) ;
14 this._lastAnonymousTargetId = 0;
15 this._agent = target.targetAgent();
16
17 /** @type {!Map<string, !WebInspector.Target>} */
18 this._targets = new Map();
19 /** @type {!Map<string, !WebInspector.SubTargetConnection>} */
20 this._connections = new Map();
21
22 this._agent.setWaitForDebuggerOnStart(true);
23 this._agent.enable();
24 }
25
26 /** @enum {symbol} */
27 WebInspector.SubTargetsManager.Events = {
28 SubTargetsUpdated: Symbol("SubTargetsUpdated"),
29 }
30
31 WebInspector.SubTargetsManager._TypeSymbol = Symbol("SubTargetType");
32 WebInspector.SubTargetsManager._IdSymbol = Symbol("SubTargetId");
33
34 WebInspector.SubTargetsManager.prototype = {
35 /**
36 * @override
37 * @return {!Promise}
38 */
39 suspendModel: function()
40 {
41 var fulfill;
42 var promise = new Promise(f => fulfill = f);
43 this._agent.setWaitForDebuggerOnStart(false, fulfill);
44 return promise;
45 },
46
47 /**
48 * @override
49 * @return {!Promise}
50 */
51 resumeModel: function()
52 {
53 var fulfill;
54 var promise = new Promise(f => fulfill = f);
55 this._agent.setWaitForDebuggerOnStart(true, fulfill);
56 return promise;
57 },
58
59 /**
60 * @override
61 */
62 dispose: function()
63 {
64 for (var connection of this._connections.values())
65 connection._close();
66 this._connections.clear();
67 this._targets.clear();
68 },
69
70 /**
71 * @param {!TargetAgent.TargetID} targetId
72 */
73 activateTarget: function(targetId)
74 {
75 this._agent.activateTarget(targetId);
76 },
77
78 /**
79 * @param {!TargetAgent.TargetID} targetId
80 * @param {function(?WebInspector.TargetInfo)=} callback
81 */
82 getTargetInfo: function(targetId, callback)
83 {
84 /**
85 * @param {?Protocol.Error} error
86 * @param {?TargetAgent.TargetInfo} targetInfo
87 */
88 function innerCallback(error, targetInfo)
89 {
90 if (error) {
91 console.error(error);
92 callback(null);
93 return;
94 }
95 if (targetInfo)
96 callback(new WebInspector.TargetInfo(targetInfo));
97 else
98 callback(null)
99 }
100 this._agent.getTargetInfo(targetId, innerCallback);
101 },
102
103 /**
104 * @param {string} targetId
105 * @return {?WebInspector.Target}
106 */
107 targetForId: function(targetId)
108 {
109 return this._targets.get(targetId) || null;
110 },
111
112 /**
113 * @param {!WebInspector.Target} target
114 * @return {?string}
115 */
116 targetId: function(target)
117 {
118 return target[WebInspector.SubTargetsManager._IdSymbol] || null;
119 },
120
121 /**
122 * @param {!WebInspector.Target} target
123 * @return {?string}
124 */
125 targetType: function(target)
126 {
127 return target[WebInspector.SubTargetsManager._TypeSymbol] || null;
128 },
129
130 /**
131 * @param {string} type
132 * @return {number}
133 */
134 _capabilitiesForType: function(type)
135 {
136 if (type === TargetAgent.TargetType.Worker)
137 return WebInspector.Target.Capability.JS | WebInspector.Target.Capab ility.Log;
138 if (type === TargetAgent.TargetType.Service_worker)
139 return WebInspector.Target.Capability.Log | WebInspector.Target.Capa bility.Network | WebInspector.Target.Capability.Worker;
140 return 0;
141 },
142
143 /**
144 * @param {string} targetId
145 * @param {string} type
146 * @param {string} url
147 * @param {boolean} waitingForDebugger
148 */
149 _targetCreated: function(targetId, type, url, waitingForDebugger)
150 {
151 var connection = new WebInspector.SubTargetConnection(this._agent, targe tId);
152 this._connections.set(targetId, connection);
153
154 var parsedURL = url.asParsedURL();
155 var targetName = parsedURL ? parsedURL.lastPathComponentWithFragment() : "#" + (++this._lastAnonymousTargetId);
156 var target = WebInspector.targetManager.createTarget(targetName, this._c apabilitiesForType(type), connection, this.target());
157 target[WebInspector.SubTargetsManager._TypeSymbol] = type;
158 target[WebInspector.SubTargetsManager._IdSymbol] = targetId;
159 this._targets.set(targetId, target);
160
161 // Only pause new worker if debugging SW - we are going through the paus e on start checkbox.
162 var mainIsServiceWorker = !this.target().parentTarget() && this.target() .hasWorkerCapability() && !this.target().hasBrowserCapability();
163 if (mainIsServiceWorker && waitingForDebugger)
164 target.debuggerAgent().pause();
165 target.runtimeAgent().runIfWaitingForDebugger();
166
167 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetsUpdated);
168 },
169
170 /**
171 * @param {string} targetId
172 */
173 _targetRemoved: function(targetId)
174 {
175 var connection = this._connections.get(targetId);
176 if (connection)
177 connection._close();
178 this._connections.delete(targetId);
179 this._targets.delete(targetId);
180 this.dispatchEventToListeners(WebInspector.SubTargetsManager.Events.SubT argetsUpdated);
181 },
182
183 /**
184 * @param {string} targetId
185 * @param {string} message
186 */
187 _receivedMessageFromTarget: function(targetId, message)
188 {
189 var connection = this._connections.get(targetId);
190 if (connection)
191 connection.dispatch(message);
192 },
193
194 __proto__: WebInspector.SDKModel.prototype
195 }
196
197 /**
198 * @constructor
199 * @implements {TargetAgent.Dispatcher}
200 * @param {!WebInspector.SubTargetsManager} manager
201 */
202 WebInspector.SubTargetsDispatcher = function(manager)
203 {
204 this._manager = manager;
205 }
206
207 WebInspector.SubTargetsDispatcher.prototype = {
208 /**
209 * @override
210 * @param {string} targetId
211 * @param {string} type
212 * @param {string} url
213 * @param {boolean} waitingForDebugger
214 */
215 targetCreated: function(targetId, type, url, waitingForDebugger)
216 {
217 this._manager._targetCreated(targetId, type, url, waitingForDebugger);
218 },
219
220 /**
221 * @override
222 * @param {string} targetId
223 */
224 targetRemoved: function(targetId)
225 {
226 this._manager._targetRemoved(targetId);
227 },
228
229 /**
230 * @override
231 * @param {string} targetId
232 * @param {string} message
233 */
234 receivedMessageFromTarget: function(targetId, message)
235 {
236 this._manager._receivedMessageFromTarget(targetId, message);
237 }
238 }
239
240 /**
241 * @constructor
242 * @extends {InspectorBackendClass.Connection}
243 * @param {!Protocol.TargetAgent} agent
244 * @param {string} targetId
245 */
246 WebInspector.SubTargetConnection = function(agent, targetId)
247 {
248 InspectorBackendClass.Connection.call(this);
249 this._agent = agent;
250 this._targetId = targetId;
251 }
252
253 WebInspector.SubTargetConnection.prototype = {
254 /**
255 * @override
256 * @param {!Object} messageObject
257 */
258 sendMessage: function(messageObject)
259 {
260 this._agent.sendMessageToTarget(this._targetId, JSON.stringify(messageOb ject));
261 },
262
263 _close: function()
264 {
265 this.connectionClosed("target_terminated");
266 },
267
268 __proto__: InspectorBackendClass.Connection.prototype
269 }
270
271 /**
272 * @constructor
273 * @param {!TargetAgent.TargetInfo} payload
274 */
275 WebInspector.TargetInfo = function(payload)
276 {
277 this.id = payload.targetId;
278 this.type = payload.type;
279 this.title = payload.title;
280 this.url = payload.url;
281 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698