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

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

Issue 2441933002: [DevTools] Refactor connection-related classes. (Closed)
Patch Set: tests.js Created 4 years, 1 month 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
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 }, 71 },
72 72
73 dispose: function() 73 dispose: function()
74 { 74 {
75 this._reset(); 75 this._reset();
76 }, 76 },
77 77
78 _reset: function() 78 _reset: function()
79 { 79 {
80 for (var connection of this._connections.values()) 80 for (var connection of this._connections.values())
81 connection.close(); 81 connection._onDisconnect.call(null, "reset");
82 this._connections.clear(); 82 this._connections.clear();
83 this._targetsByWorkerId.clear(); 83 this._targetsByWorkerId.clear();
84 }, 84 },
85 85
86 _onSuspendStateChanged: function() 86 _onSuspendStateChanged: function()
87 { 87 {
88 var suspended = WebInspector.targetManager.allTargetsSuspended(); 88 var suspended = WebInspector.targetManager.allTargetsSuspended();
89 this.target().workerAgent().setWaitForDebuggerOnStart(!suspended); 89 this.target().workerAgent().setWaitForDebuggerOnStart(!suspended);
90 }, 90 },
91 91
92 /** 92 /**
93 * @param {string} workerId 93 * @param {string} workerId
94 * @param {string} url 94 * @param {string} url
95 * @param {boolean} waitingForDebugger 95 * @param {boolean} waitingForDebugger
96 */ 96 */
97 _workerCreated: function(workerId, url, waitingForDebugger) 97 _workerCreated: function(workerId, url, waitingForDebugger)
98 { 98 {
99 var connection = new WebInspector.WorkerConnection(this, workerId); 99 var capabilities = WebInspector.Target.Capability.JS | WebInspector.Targ et.Capability.Log;
100 this._connections.set(workerId, connection);
101
102 var parsedURL = url.asParsedURL(); 100 var parsedURL = url.asParsedURL();
103 var workerName = parsedURL ? parsedURL.lastPathComponentWithFragment() : "#" + (++this._lastAnonymousTargetId); 101 var workerName = parsedURL ? parsedURL.lastPathComponentWithFragment() : "#" + (++this._lastAnonymousTargetId);
104 var target = WebInspector.targetManager.createTarget(workerName, WebInsp ector.Target.Capability.JS | WebInspector.Target.Capability.Log, connection, thi s.target()); 102 var target = WebInspector.targetManager.createTarget(workerName, capabil ities, this._createConnection.bind(this, workerId), this.target());
105 this._targetsByWorkerId.set(workerId, target); 103 this._targetsByWorkerId.set(workerId, target);
106 104
107 // Only pause new worker if debugging SW - we are going through the 105 // Only pause new worker if debugging SW - we are going through the
108 // pause on start checkbox. 106 // pause on start checkbox.
109 var mainIsServiceWorker = WebInspector.targetManager.mainTarget().hasWor kerCapability() && !WebInspector.targetManager.mainTarget().hasBrowserCapability (); 107 var mainIsServiceWorker = WebInspector.targetManager.mainTarget().hasWor kerCapability() && !WebInspector.targetManager.mainTarget().hasBrowserCapability ();
110 if (mainIsServiceWorker && waitingForDebugger) 108 if (mainIsServiceWorker && waitingForDebugger)
111 target.debuggerAgent().pause(); 109 target.debuggerAgent().pause();
112 target.runtimeAgent().runIfWaitingForDebugger(); 110 target.runtimeAgent().runIfWaitingForDebugger();
113 }, 111 },
114 112
115 /** 113 /**
116 * @param {string} workerId 114 * @param {string} workerId
115 * @param {!InspectorBackendClass.Connection.Params} params
116 * @return {!InspectorBackendClass.Connection}
117 */
118 _createConnection: function(workerId, params)
119 {
120 var connection = new WebInspector.WorkerConnection(this, workerId, param s);
121 this._connections.set(workerId, connection);
122 return connection;
123 },
124
125 /**
126 * @param {string} workerId
117 */ 127 */
118 _workerTerminated: function(workerId) 128 _workerTerminated: function(workerId)
119 { 129 {
120 var connection = this._connections.get(workerId); 130 var connection = this._connections.get(workerId);
121 if (connection) 131 if (connection)
122 connection._reportClosed(); 132 connection._onDisconnect.call(null, "worker terminated");
123 this._connections.delete(workerId); 133 this._connections.delete(workerId);
124 this._targetsByWorkerId.delete(workerId); 134 this._targetsByWorkerId.delete(workerId);
125 }, 135 },
126 136
127 /** 137 /**
128 * @param {string} workerId 138 * @param {string} workerId
129 * @param {string} message 139 * @param {string} message
130 */ 140 */
131 _dispatchMessageFromWorker: function(workerId, message) 141 _dispatchMessageFromWorker: function(workerId, message)
132 { 142 {
133 var connection = this._connections.get(workerId); 143 var connection = this._connections.get(workerId);
134 if (connection) 144 if (connection)
135 connection.dispatch(message); 145 connection._onMessage.call(null, message);
136 }, 146 },
137 147
138 /** 148 /**
139 * @param {!WebInspector.Event} event 149 * @param {!WebInspector.Event} event
140 */ 150 */
141 _mainFrameNavigated: function(event) 151 _mainFrameNavigated: function(event)
142 { 152 {
143 if (event.data.target() !== this.target()) 153 if (event.data.target() !== this.target())
144 return; 154 return;
145 this._reset(); // TODO (dgozman): Probably, unnecessary. Consider remova l. 155 this._reset(); // TODO (dgozman): Probably, unnecessary. Consider remova l.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 * @param {string} message 203 * @param {string} message
194 */ 204 */
195 dispatchMessageFromWorker: function(workerId, message) 205 dispatchMessageFromWorker: function(workerId, message)
196 { 206 {
197 this._workerManager._dispatchMessageFromWorker(workerId, message); 207 this._workerManager._dispatchMessageFromWorker(workerId, message);
198 } 208 }
199 }; 209 };
200 210
201 /** 211 /**
202 * @constructor 212 * @constructor
203 * @extends {InspectorBackendClass.Connection} 213 * @implements {InspectorBackendClass.Connection}
204 * @param {!WebInspector.WorkerManager} workerManager 214 * @param {!WebInspector.WorkerManager} workerManager
205 * @param {string} workerId 215 * @param {string} workerId
216 * @param {!InspectorBackendClass.Connection.Params} params
206 */ 217 */
207 WebInspector.WorkerConnection = function(workerManager, workerId) 218 WebInspector.WorkerConnection = function(workerManager, workerId, params)
208 { 219 {
209 InspectorBackendClass.Connection.call(this);
210 // FIXME: remove resourceTreeModel and others from worker targets
211 this.suppressErrorsForDomains(["Worker", "Page", "CSS", "DOM", "DOMStorage", "Database", "Network", "IndexedDB"]);
212 this._agent = workerManager.target().workerAgent(); 220 this._agent = workerManager.target().workerAgent();
213 this._workerId = workerId; 221 this._workerId = workerId;
222 this._onMessage = params.onMessage;
223 this._onDisconnect = params.onDisconnect;
214 }; 224 };
215 225
216 WebInspector.WorkerConnection.prototype = { 226 WebInspector.WorkerConnection.prototype = {
217 /** 227 /**
218 * @override 228 * @override
219 * @param {!Object} messageObject 229 * @param {string} message
220 */ 230 */
221 sendMessage: function(messageObject) 231 sendMessage: function(message)
222 { 232 {
223 this._agent.sendMessageToWorker(this._workerId, JSON.stringify(messageOb ject)); 233 this._agent.sendMessageToWorker(this._workerId, message);
224 }, 234 },
225 235
226 _reportClosed: function() 236 /**
237 * @override
238 * @return {!Promise}
239 */
240 disconnect: function()
227 { 241 {
228 this.connectionClosed("worker_terminated"); 242 throw new Error("Not implemented");
229 }, 243 },
230
231 __proto__: InspectorBackendClass.Connection.prototype
232 }; 244 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698