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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 if (constructor) 194 if (constructor)
195 clientCallback(new constructor(value)); 195 clientCallback(new constructor(value));
196 else 196 else
197 clientCallback(value); 197 clientCallback(value);
198 } 198 }
199 return callbackWrapper; 199 return callbackWrapper;
200 } 200 }
201 }; 201 };
202 202
203 /** 203 /**
204 * @constructor 204 * @interface
205 * @extends {WebInspector.Object}
206 */ 205 */
207 InspectorBackendClass.Connection = function() 206 InspectorBackendClass.Connection = function()
208 { 207 {
208 };
209
210 InspectorBackendClass.Connection.prototype = {
211 /**
212 * @param {string} message
213 */
214 sendMessage: function(message) { },
215
216 /**
217 * @return {!Promise}
218 */
219 disconnect: function() { },
220 };
221
222 /**
223 * @typedef {!{
224 * onMessage: function((!Object|string)),
225 * onDisconnect: function(string)
226 * }}
227 */
228 InspectorBackendClass.Connection.Params;
229
230 /**
231 * @typedef {function(!InspectorBackendClass.Connection.Params):!InspectorBacken dClass.Connection}
232 */
233 InspectorBackendClass.Connection.Factory;
234
235 /**
236 * @constructor
237 * @param {!InspectorBackendClass.Connection.Factory} connectionFactory
238 * @param {function(string)} disconnectedCallback
239 */
240 InspectorBackendClass.TargetPrototype = function(connectionFactory, disconnected Callback)
241 {
242 this._connection = connectionFactory({onMessage: this.dispatch.bind(this), o nDisconnect: this._disconnected.bind(this)});
243 this._disconnectedCallback = disconnectedCallback;
209 this._lastMessageId = 1; 244 this._lastMessageId = 1;
210 this._pendingResponsesCount = 0; 245 this._pendingResponsesCount = 0;
211 this._agents = {}; 246 this._agents = {};
212 this._dispatchers = {}; 247 this._dispatchers = {};
213 this._callbacks = {}; 248 this._callbacks = {};
214 this._initialize(InspectorBackend._agentPrototypes, InspectorBackend._dispat cherPrototypes); 249 this._initialize(InspectorBackend._agentPrototypes, InspectorBackend._dispat cherPrototypes);
215 this._isConnected = true; 250 if (!InspectorBackendClass.deprecatedRunAfterPendingDispatches)
251 InspectorBackendClass.deprecatedRunAfterPendingDispatches = this._deprec atedRunAfterPendingDispatches.bind(this);
252 if (!InspectorBackendClass.sendRawMessageForTesting)
253 InspectorBackendClass.sendRawMessageForTesting = this._sendRawMessageFor Testing.bind(this);
216 }; 254 };
217 255
218 /** @enum {symbol} */ 256 InspectorBackendClass.TargetPrototype.prototype = {
219 InspectorBackendClass.Connection.Events = {
220 Disconnected: Symbol("Disconnected")
221 };
222
223 InspectorBackendClass.Connection.prototype = {
224 /** 257 /**
225 * @param {!Object.<string, !InspectorBackendClass.AgentPrototype>} agentPro totypes 258 * @param {!Object.<string, !InspectorBackendClass.AgentPrototype>} agentPro totypes
226 * @param {!Object.<string, !InspectorBackendClass.DispatcherPrototype>} dis patcherPrototypes 259 * @param {!Object.<string, !InspectorBackendClass.DispatcherPrototype>} dis patcherPrototypes
227 */ 260 */
228 _initialize: function(agentPrototypes, dispatcherPrototypes) 261 _initialize: function(agentPrototypes, dispatcherPrototypes)
229 { 262 {
230 for (var domain in agentPrototypes) { 263 for (var domain in agentPrototypes) {
231 this._agents[domain] = Object.create(agentPrototypes[domain]); 264 this._agents[domain] = Object.create(agentPrototypes[domain]);
232 this._agents[domain].setConnection(this); 265 this._agents[domain].setConnection(this);
233 } 266 }
(...skipping 28 matching lines...) Expand all
262 }, 295 },
263 296
264 /** 297 /**
265 * @param {string} domain 298 * @param {string} domain
266 * @param {string} method 299 * @param {string} method
267 * @param {?Object} params 300 * @param {?Object} params
268 * @param {?function(*)} callback 301 * @param {?function(*)} callback
269 */ 302 */
270 _wrapCallbackAndSendMessageObject: function(domain, method, params, callback ) 303 _wrapCallbackAndSendMessageObject: function(domain, method, params, callback )
271 { 304 {
272 if (!this._isConnected && callback) { 305 if (!this._connection) {
273 this._dispatchConnectionErrorResponse(domain, method, callback); 306 if (callback)
307 this._dispatchConnectionErrorResponse(domain, method, callback);
274 return; 308 return;
275 } 309 }
276 310
277 var messageObject = {}; 311 var messageObject = {};
278 var messageId = this.nextMessageId(); 312 var messageId = this.nextMessageId();
279 messageObject.id = messageId; 313 messageObject.id = messageId;
280 messageObject.method = method; 314 messageObject.method = method;
281 if (params) 315 if (params)
282 messageObject.params = params; 316 messageObject.params = params;
283 317
284 var wrappedCallback = this._wrap(callback, domain, method); 318 var wrappedCallback = this._wrap(callback, domain, method);
319 var message = JSON.stringify(messageObject);
285 320
286 if (InspectorBackendClass.Options.dumpInspectorProtocolMessages) 321 if (InspectorBackendClass.Options.dumpInspectorProtocolMessages)
287 this._dumpProtocolMessage("frontend: " + JSON.stringify(messageObjec t)); 322 this._dumpProtocolMessage("frontend: " + message);
288 323
289 this.sendMessage(messageObject); 324 this._connection.sendMessage(message);
290 ++this._pendingResponsesCount; 325 ++this._pendingResponsesCount;
291 this._callbacks[messageId] = wrappedCallback; 326 this._callbacks[messageId] = wrappedCallback;
292 }, 327 },
293 328
294 /** 329 /**
295 * @param {?function(*)} callback 330 * @param {?function(*)} callback
296 * @param {string} method 331 * @param {string} method
297 * @param {string} domain 332 * @param {string} domain
298 * @return {function(*)} 333 * @return {function(*)}
299 */ 334 */
300 _wrap: function(callback, domain, method) 335 _wrap: function(callback, domain, method)
301 { 336 {
302 if (!callback) 337 if (!callback)
303 callback = function() {}; 338 callback = function() {};
304 339
305 callback.methodName = method; 340 callback.methodName = method;
306 callback.domain = domain; 341 callback.domain = domain;
307 if (InspectorBackendClass.Options.dumpInspectorTimeStats) 342 if (InspectorBackendClass.Options.dumpInspectorTimeStats)
308 callback.sendRequestTime = Date.now(); 343 callback.sendRequestTime = Date.now();
309 344
310 return callback; 345 return callback;
311 }, 346 },
312 347
313 /** 348 /**
314 * @param {!Object} messageObject
315 */
316 sendMessage: function(messageObject)
317 {
318 throw "Not implemented";
319 },
320
321 /**
322 * @param {string} method 349 * @param {string} method
323 * @param {?Object} params 350 * @param {?Object} params
324 * @param {?function(...*)} callback 351 * @param {?function(...*)} callback
325 */ 352 */
326 sendRawMessageForTesting: function(method, params, callback) 353 _sendRawMessageForTesting: function(method, params, callback)
327 { 354 {
328 var domain = method.split(".")[0]; 355 var domain = method.split(".")[0];
329 this._wrapCallbackAndSendMessageObject(domain, method, params, callback) ; 356 this._wrapCallbackAndSendMessageObject(domain, method, params, callback) ;
330 }, 357 },
331 358
332 /** 359 /**
333 * @param {!Object|string} message 360 * @param {!Object|string} message
334 */ 361 */
335 dispatch: function(message) 362 dispatch: function(message)
336 { 363 {
(...skipping 14 matching lines...) Expand all
351 processingStartTime = Date.now(); 378 processingStartTime = Date.now();
352 379
353 this.agent(callback.domain).dispatchResponse(messageObject, callback .methodName, callback); 380 this.agent(callback.domain).dispatchResponse(messageObject, callback .methodName, callback);
354 --this._pendingResponsesCount; 381 --this._pendingResponsesCount;
355 delete this._callbacks[messageObject.id]; 382 delete this._callbacks[messageObject.id];
356 383
357 if (InspectorBackendClass.Options.dumpInspectorTimeStats) 384 if (InspectorBackendClass.Options.dumpInspectorTimeStats)
358 console.log("time-stats: " + callback.methodName + " = " + (proc essingStartTime - callback.sendRequestTime) + " + " + (Date.now() - processingSt artTime)); 385 console.log("time-stats: " + callback.methodName + " = " + (proc essingStartTime - callback.sendRequestTime) + " + " + (Date.now() - processingSt artTime));
359 386
360 if (this._scripts && !this._pendingResponsesCount) 387 if (this._scripts && !this._pendingResponsesCount)
361 this.deprecatedRunAfterPendingDispatches(); 388 this._deprecatedRunAfterPendingDispatches();
362 return;
363 } else { 389 } else {
390 if (!("method" in messageObject)) {
391 InspectorBackendClass.reportProtocolError("Protocol Error: the m essage without method", messageObject);
392 return;
393 }
394
364 var method = messageObject.method.split("."); 395 var method = messageObject.method.split(".");
365 var domainName = method[0]; 396 var domainName = method[0];
366 if (!(domainName in this._dispatchers)) { 397 if (!(domainName in this._dispatchers)) {
367 InspectorBackendClass.reportProtocolError("Protocol Error: the m essage " + messageObject.method + " is for non-existing domain '" + domainName + "'", messageObject); 398 InspectorBackendClass.reportProtocolError("Protocol Error: the m essage " + messageObject.method + " is for non-existing domain '" + domainName + "'", messageObject);
368 return; 399 return;
369 } 400 }
370 401
371 this._dispatchers[domainName].dispatch(method[1], messageObject); 402 this._dispatchers[domainName].dispatch(method[1], messageObject);
372 } 403 }
373
374 }, 404 },
375 405
376 /** 406 /**
377 * @param {string} domain 407 * @param {string} domain
378 * @param {!Object} dispatcher 408 * @param {!Object} dispatcher
379 */ 409 */
380 registerDispatcher: function(domain, dispatcher) 410 registerDispatcher: function(domain, dispatcher)
381 { 411 {
382 if (!this._dispatchers[domain]) 412 if (!this._dispatchers[domain])
383 return; 413 return;
384 414
385 this._dispatchers[domain].setDomainDispatcher(dispatcher); 415 this._dispatchers[domain].setDomainDispatcher(dispatcher);
386 }, 416 },
387 417
388 /** 418 /**
389 * @param {function()=} script 419 * @param {function()=} script
390 */ 420 */
391 deprecatedRunAfterPendingDispatches: function(script) 421 _deprecatedRunAfterPendingDispatches: function(script)
392 { 422 {
393 if (!this._scripts) 423 if (!this._scripts)
394 this._scripts = []; 424 this._scripts = [];
395 425
396 if (script) 426 if (script)
397 this._scripts.push(script); 427 this._scripts.push(script);
398 428
399 // Execute all promises. 429 // Execute all promises.
400 setTimeout(function() { 430 setTimeout(function() {
401 if (!this._pendingResponsesCount) 431 if (!this._pendingResponsesCount)
402 this._executeAfterPendingDispatches(); 432 this._executeAfterPendingDispatches();
403 else 433 else
404 this.deprecatedRunAfterPendingDispatches(); 434 this._deprecatedRunAfterPendingDispatches();
405 }.bind(this), 0); 435 }.bind(this), 0);
406 }, 436 },
407 437
408 _executeAfterPendingDispatches: function() 438 _executeAfterPendingDispatches: function()
409 { 439 {
410 if (!this._pendingResponsesCount) { 440 if (!this._pendingResponsesCount) {
411 var scripts = this._scripts; 441 var scripts = this._scripts;
412 this._scripts = []; 442 this._scripts = [];
413 for (var id = 0; id < scripts.length; ++id) 443 for (var id = 0; id < scripts.length; ++id)
414 scripts[id].call(this); 444 scripts[id].call(this);
415 } 445 }
416 }, 446 },
417 447
448 /**
449 * @param {string} message
450 */
418 _dumpProtocolMessage: function(message) 451 _dumpProtocolMessage: function(message)
419 { 452 {
420 console.log(message); 453 console.log(message);
421 }, 454 },
422 455
423 close: function()
424 {
425 this.forceClose();
426 this.connectionClosed("force close");
427 },
428
429 /** 456 /**
430 * @protected
431 */
432 forceClose: function()
433 {
434 },
435
436 /**
437 * @protected
438 * @param {string} reason 457 * @param {string} reason
439 */ 458 */
440 connectionClosed: function(reason) 459 _disconnected: function(reason)
441 { 460 {
442 this._isConnected = false; 461 this._connection = null;
443 this._runPendingCallbacks(); 462 this._runPendingCallbacks();
444 this.dispatchEventToListeners(InspectorBackendClass.Connection.Events.Di sconnected, {reason: reason}); 463 this._disconnectedCallback(reason);
464 this._disconnectedCallback = null;
445 }, 465 },
446 466
447 _runPendingCallbacks: function() 467 _runPendingCallbacks: function()
448 { 468 {
449 var keys = Object.keys(this._callbacks).map(function(num) { return parse Int(num, 10); }); 469 var keys = Object.keys(this._callbacks).map(function(num) { return parse Int(num, 10); });
450 for (var i = 0; i < keys.length; ++i) { 470 for (var i = 0; i < keys.length; ++i) {
451 var callback = this._callbacks[keys[i]]; 471 var callback = this._callbacks[keys[i]];
452 this._dispatchConnectionErrorResponse(callback.domain, callback.meth odName, callback); 472 this._dispatchConnectionErrorResponse(callback.domain, callback.meth odName, callback);
453 } 473 }
454 this._callbacks = {}; 474 this._callbacks = {};
455 }, 475 },
456 476
457 /** 477 /**
458 * @param {string} domain 478 * @param {string} domain
459 * @param {string} methodName 479 * @param {string} methodName
460 * @param {function(*)} callback 480 * @param {function(*)} callback
461 */ 481 */
462 _dispatchConnectionErrorResponse: function(domain, methodName, callback) 482 _dispatchConnectionErrorResponse: function(domain, methodName, callback)
463 { 483 {
464 var error = { message: "Connection is closed, can't dispatch pending " + methodName, code: InspectorBackendClass._DevToolsErrorCode, data: null}; 484 var error = { message: "Connection is closed, can't dispatch pending " + methodName, code: InspectorBackendClass._DevToolsErrorCode, data: null};
465 var messageObject = {error: error}; 485 var messageObject = {error: error};
466 setTimeout(InspectorBackendClass.AgentPrototype.prototype.dispatchRespon se.bind(this.agent(domain), messageObject, methodName, callback), 0); 486 setTimeout(InspectorBackendClass.AgentPrototype.prototype.dispatchRespon se.bind(this.agent(domain), messageObject, methodName, callback), 0);
467 }, 487 },
468
469 /**
470 * @return {boolean}
471 */
472 isClosed: function()
473 {
474 return !this._isConnected;
475 },
476
477 /**
478 * @param {!Array.<string>} domains
479 */
480 suppressErrorsForDomains: function(domains)
481 {
482 domains.forEach(function(domain) { this._agents[domain].suppressErrorLog ging(); }, this);
483 },
484
485 __proto__: WebInspector.Object.prototype
486
487 }; 488 };
488 489
489 /** 490 /**
490 * @constructor 491 * @constructor
491 * @param {string} domain 492 * @param {string} domain
492 */ 493 */
493 InspectorBackendClass.AgentPrototype = function(domain) 494 InspectorBackendClass.AgentPrototype = function(domain)
494 { 495 {
495 this._replyArgs = {}; 496 this._replyArgs = {};
496 this._hasErrorData = {}; 497 this._hasErrorData = {};
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 } 748 }
748 }; 749 };
749 750
750 InspectorBackendClass.Options = { 751 InspectorBackendClass.Options = {
751 dumpInspectorTimeStats: false, 752 dumpInspectorTimeStats: false,
752 dumpInspectorProtocolMessages: false, 753 dumpInspectorProtocolMessages: false,
753 suppressRequestErrors: false 754 suppressRequestErrors: false
754 }; 755 };
755 756
756 InspectorBackend = new InspectorBackendClass(); 757 InspectorBackend = new InspectorBackendClass();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698