| OLD | NEW |
| 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 10 matching lines...) Expand all Loading... |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 var Protocol = {}; | |
| 32 | |
| 33 /** @typedef {string} */ | 31 /** @typedef {string} */ |
| 34 Protocol.Error; | 32 Protocol.Error; |
| 35 | 33 |
| 36 /** | 34 /** |
| 37 * @unrestricted | 35 * @unrestricted |
| 38 */ | 36 */ |
| 39 var InspectorBackendClass = class { | 37 Protocol.InspectorBackend = class { |
| 40 constructor() { | 38 constructor() { |
| 41 this._agentPrototypes = {}; | 39 this._agentPrototypes = {}; |
| 42 this._dispatcherPrototypes = {}; | 40 this._dispatcherPrototypes = {}; |
| 43 this._initialized = false; | 41 this._initialized = false; |
| 44 } | 42 } |
| 45 | 43 |
| 46 /** | 44 /** |
| 47 * @param {string} error | 45 * @param {string} error |
| 48 * @param {!Object} messageObject | 46 * @param {!Object} messageObject |
| 49 */ | 47 */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 */ | 80 */ |
| 83 function registerDispatcher(dispatcher) { | 81 function registerDispatcher(dispatcher) { |
| 84 this.registerDispatcher(domain, dispatcher); | 82 this.registerDispatcher(domain, dispatcher); |
| 85 } | 83 } |
| 86 | 84 |
| 87 Protocol.TargetBase.prototype['register' + domain + 'Dispatcher'] = register
Dispatcher; | 85 Protocol.TargetBase.prototype['register' + domain + 'Dispatcher'] = register
Dispatcher; |
| 88 } | 86 } |
| 89 | 87 |
| 90 /** | 88 /** |
| 91 * @param {string} domain | 89 * @param {string} domain |
| 92 * @return {!InspectorBackendClass._AgentPrototype} | 90 * @return {!Protocol.InspectorBackend._AgentPrototype} |
| 93 */ | 91 */ |
| 94 _agentPrototype(domain) { | 92 _agentPrototype(domain) { |
| 95 if (!this._agentPrototypes[domain]) { | 93 if (!this._agentPrototypes[domain]) { |
| 96 this._agentPrototypes[domain] = new InspectorBackendClass._AgentPrototype(
domain); | 94 this._agentPrototypes[domain] = new Protocol.InspectorBackend._AgentProtot
ype(domain); |
| 97 this._addAgentGetterMethodToProtocolTargetPrototype(domain); | 95 this._addAgentGetterMethodToProtocolTargetPrototype(domain); |
| 98 } | 96 } |
| 99 | 97 |
| 100 return this._agentPrototypes[domain]; | 98 return this._agentPrototypes[domain]; |
| 101 } | 99 } |
| 102 | 100 |
| 103 /** | 101 /** |
| 104 * @param {string} domain | 102 * @param {string} domain |
| 105 * @return {!InspectorBackendClass._DispatcherPrototype} | 103 * @return {!Protocol.InspectorBackend._DispatcherPrototype} |
| 106 */ | 104 */ |
| 107 _dispatcherPrototype(domain) { | 105 _dispatcherPrototype(domain) { |
| 108 if (!this._dispatcherPrototypes[domain]) | 106 if (!this._dispatcherPrototypes[domain]) |
| 109 this._dispatcherPrototypes[domain] = new InspectorBackendClass._Dispatcher
Prototype(); | 107 this._dispatcherPrototypes[domain] = new Protocol.InspectorBackend._Dispat
cherPrototype(); |
| 110 return this._dispatcherPrototypes[domain]; | 108 return this._dispatcherPrototypes[domain]; |
| 111 } | 109 } |
| 112 | 110 |
| 113 /** | 111 /** |
| 114 * @param {string} method | 112 * @param {string} method |
| 115 * @param {!Array.<!Object>} signature | 113 * @param {!Array.<!Object>} signature |
| 116 * @param {!Array.<string>} replyArgs | 114 * @param {!Array.<string>} replyArgs |
| 117 * @param {boolean} hasErrorData | 115 * @param {boolean} hasErrorData |
| 118 */ | 116 */ |
| 119 registerCommand(method, signature, replyArgs, hasErrorData) { | 117 registerCommand(method, signature, replyArgs, hasErrorData) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 166 } |
| 169 if (constructor) | 167 if (constructor) |
| 170 clientCallback(new constructor(value)); | 168 clientCallback(new constructor(value)); |
| 171 else | 169 else |
| 172 clientCallback(value); | 170 clientCallback(value); |
| 173 } | 171 } |
| 174 return callbackWrapper; | 172 return callbackWrapper; |
| 175 } | 173 } |
| 176 }; | 174 }; |
| 177 | 175 |
| 178 InspectorBackendClass._ConnectionClosedErrorCode = -32000; | 176 Protocol.InspectorBackend._ConnectionClosedErrorCode = -32000; |
| 179 InspectorBackendClass.DevToolsStubErrorCode = -32015; | 177 Protocol.InspectorBackend.DevToolsStubErrorCode = -32015; |
| 180 | 178 |
| 181 | 179 |
| 182 var InspectorBackend = new InspectorBackendClass(); | 180 Protocol.inspectorBackend = new Protocol.InspectorBackend(); |
| 183 | 181 |
| 184 /** | 182 /** |
| 185 * @interface | 183 * @interface |
| 186 */ | 184 */ |
| 187 InspectorBackendClass.Connection = function() {}; | 185 Protocol.InspectorBackend.Connection = function() {}; |
| 188 | 186 |
| 189 InspectorBackendClass.Connection.prototype = { | 187 Protocol.InspectorBackend.Connection.prototype = { |
| 190 /** | 188 /** |
| 191 * @param {string} message | 189 * @param {string} message |
| 192 */ | 190 */ |
| 193 sendMessage(message) {}, | 191 sendMessage(message) {}, |
| 194 | 192 |
| 195 /** | 193 /** |
| 196 * @return {!Promise} | 194 * @return {!Promise} |
| 197 */ | 195 */ |
| 198 disconnect() {}, | 196 disconnect() {}, |
| 199 }; | 197 }; |
| 200 | 198 |
| 201 /** | 199 /** |
| 202 * @typedef {!{ | 200 * @typedef {!{ |
| 203 * onMessage: function((!Object|string)), | 201 * onMessage: function((!Object|string)), |
| 204 * onDisconnect: function(string) | 202 * onDisconnect: function(string) |
| 205 * }} | 203 * }} |
| 206 */ | 204 */ |
| 207 InspectorBackendClass.Connection.Params; | 205 Protocol.InspectorBackend.Connection.Params; |
| 208 | 206 |
| 209 /** | 207 /** |
| 210 * @typedef {function(!InspectorBackendClass.Connection.Params):!InspectorBacken
dClass.Connection} | 208 * @typedef {function(!Protocol.InspectorBackend.Connection.Params):!Protocol.In
spectorBackend.Connection} |
| 211 */ | 209 */ |
| 212 InspectorBackendClass.Connection.Factory; | 210 Protocol.InspectorBackend.Connection.Factory; |
| 213 | 211 |
| 214 /** | 212 /** |
| 215 * @unrestricted | 213 * @unrestricted |
| 216 */ | 214 */ |
| 217 Protocol.TargetBase = class { | 215 Protocol.TargetBase = class { |
| 218 /** | 216 /** |
| 219 * @param {!InspectorBackendClass.Connection.Factory} connectionFactory | 217 * @param {!Protocol.InspectorBackend.Connection.Factory} connectionFactory |
| 220 */ | 218 */ |
| 221 constructor(connectionFactory) { | 219 constructor(connectionFactory) { |
| 222 this._connection = | 220 this._connection = |
| 223 connectionFactory({onMessage: this._onMessage.bind(this), onDisconnect:
this._onDisconnect.bind(this)}); | 221 connectionFactory({onMessage: this._onMessage.bind(this), onDisconnect:
this._onDisconnect.bind(this)}); |
| 224 this._lastMessageId = 1; | 222 this._lastMessageId = 1; |
| 225 this._pendingResponsesCount = 0; | 223 this._pendingResponsesCount = 0; |
| 226 this._agents = {}; | 224 this._agents = {}; |
| 227 this._dispatchers = {}; | 225 this._dispatchers = {}; |
| 228 this._callbacks = {}; | 226 this._callbacks = {}; |
| 229 this._initialize(InspectorBackend._agentPrototypes, InspectorBackend._dispat
cherPrototypes); | 227 this._initialize(Protocol.inspectorBackend._agentPrototypes, Protocol.inspec
torBackend._dispatcherPrototypes); |
| 230 if (!InspectorBackendClass.deprecatedRunAfterPendingDispatches) | 228 if (!Protocol.InspectorBackend.deprecatedRunAfterPendingDispatches) { |
| 231 InspectorBackendClass.deprecatedRunAfterPendingDispatches = this._deprecat
edRunAfterPendingDispatches.bind(this); | 229 Protocol.InspectorBackend.deprecatedRunAfterPendingDispatches = |
| 232 if (!InspectorBackendClass.sendRawMessageForTesting) | 230 this._deprecatedRunAfterPendingDispatches.bind(this); |
| 233 InspectorBackendClass.sendRawMessageForTesting = this._sendRawMessageForTe
sting.bind(this); | 231 } |
| 232 if (!Protocol.InspectorBackend.sendRawMessageForTesting) |
| 233 Protocol.InspectorBackend.sendRawMessageForTesting = this._sendRawMessageF
orTesting.bind(this); |
| 234 } | 234 } |
| 235 | 235 |
| 236 /** | 236 /** |
| 237 * @param {!Object.<string, !InspectorBackendClass._AgentPrototype>} agentProt
otypes | 237 * @param {!Object.<string, !Protocol.InspectorBackend._AgentPrototype>} agent
Prototypes |
| 238 * @param {!Object.<string, !InspectorBackendClass._DispatcherPrototype>} disp
atcherPrototypes | 238 * @param {!Object.<string, !Protocol.InspectorBackend._DispatcherPrototype>}
dispatcherPrototypes |
| 239 */ | 239 */ |
| 240 _initialize(agentPrototypes, dispatcherPrototypes) { | 240 _initialize(agentPrototypes, dispatcherPrototypes) { |
| 241 for (var domain in agentPrototypes) { | 241 for (var domain in agentPrototypes) { |
| 242 this._agents[domain] = Object.create(agentPrototypes[domain]); | 242 this._agents[domain] = Object.create(agentPrototypes[domain]); |
| 243 this._agents[domain].setTarget(this); | 243 this._agents[domain].setTarget(this); |
| 244 } | 244 } |
| 245 | 245 |
| 246 for (var domain in dispatcherPrototypes) | 246 for (var domain in dispatcherPrototypes) |
| 247 this._dispatchers[domain] = Object.create(dispatcherPrototypes[domain]); | 247 this._dispatchers[domain] = Object.create(dispatcherPrototypes[domain]); |
| 248 } | 248 } |
| 249 | 249 |
| 250 /** | 250 /** |
| 251 * @return {number} | 251 * @return {number} |
| 252 */ | 252 */ |
| 253 _nextMessageId() { | 253 _nextMessageId() { |
| 254 return this._lastMessageId++; | 254 return this._lastMessageId++; |
| 255 } | 255 } |
| 256 | 256 |
| 257 /** | 257 /** |
| 258 * @param {string} domain | 258 * @param {string} domain |
| 259 * @return {!InspectorBackendClass._AgentPrototype} | 259 * @return {!Protocol.InspectorBackend._AgentPrototype} |
| 260 */ | 260 */ |
| 261 _agent(domain) { | 261 _agent(domain) { |
| 262 return this._agents[domain]; | 262 return this._agents[domain]; |
| 263 } | 263 } |
| 264 | 264 |
| 265 /** | 265 /** |
| 266 * @param {string} domain | 266 * @param {string} domain |
| 267 * @param {string} method | 267 * @param {string} method |
| 268 * @param {?Object} params | 268 * @param {?Object} params |
| 269 * @param {?function(*)} callback | 269 * @param {?function(*)} callback |
| 270 */ | 270 */ |
| 271 _wrapCallbackAndSendMessageObject(domain, method, params, callback) { | 271 _wrapCallbackAndSendMessageObject(domain, method, params, callback) { |
| 272 if (!this._connection) { | 272 if (!this._connection) { |
| 273 if (callback) | 273 if (callback) |
| 274 this._dispatchConnectionErrorResponse(domain, method, callback); | 274 this._dispatchConnectionErrorResponse(domain, method, callback); |
| 275 return; | 275 return; |
| 276 } | 276 } |
| 277 | 277 |
| 278 var messageObject = {}; | 278 var messageObject = {}; |
| 279 var messageId = this._nextMessageId(); | 279 var messageId = this._nextMessageId(); |
| 280 messageObject.id = messageId; | 280 messageObject.id = messageId; |
| 281 messageObject.method = method; | 281 messageObject.method = method; |
| 282 if (params) | 282 if (params) |
| 283 messageObject.params = params; | 283 messageObject.params = params; |
| 284 | 284 |
| 285 var wrappedCallback = this._wrap(callback, domain, method); | 285 var wrappedCallback = this._wrap(callback, domain, method); |
| 286 var message = JSON.stringify(messageObject); | 286 var message = JSON.stringify(messageObject); |
| 287 | 287 |
| 288 if (InspectorBackendClass.Options.dumpInspectorProtocolMessages) | 288 if (Protocol.InspectorBackend.Options.dumpInspectorProtocolMessages) |
| 289 this._dumpProtocolMessage('frontend: ' + message); | 289 this._dumpProtocolMessage('frontend: ' + message); |
| 290 | 290 |
| 291 this._connection.sendMessage(message); | 291 this._connection.sendMessage(message); |
| 292 ++this._pendingResponsesCount; | 292 ++this._pendingResponsesCount; |
| 293 this._callbacks[messageId] = wrappedCallback; | 293 this._callbacks[messageId] = wrappedCallback; |
| 294 } | 294 } |
| 295 | 295 |
| 296 /** | 296 /** |
| 297 * @param {?function(*)} callback | 297 * @param {?function(*)} callback |
| 298 * @param {string} method | 298 * @param {string} method |
| 299 * @param {string} domain | 299 * @param {string} domain |
| 300 * @return {function(*)} | 300 * @return {function(*)} |
| 301 */ | 301 */ |
| 302 _wrap(callback, domain, method) { | 302 _wrap(callback, domain, method) { |
| 303 if (!callback) | 303 if (!callback) |
| 304 callback = function() {}; | 304 callback = function() {}; |
| 305 | 305 |
| 306 callback.methodName = method; | 306 callback.methodName = method; |
| 307 callback.domain = domain; | 307 callback.domain = domain; |
| 308 if (InspectorBackendClass.Options.dumpInspectorTimeStats) | 308 if (Protocol.InspectorBackend.Options.dumpInspectorTimeStats) |
| 309 callback.sendRequestTime = Date.now(); | 309 callback.sendRequestTime = Date.now(); |
| 310 | 310 |
| 311 return callback; | 311 return callback; |
| 312 } | 312 } |
| 313 | 313 |
| 314 /** | 314 /** |
| 315 * @param {string} method | 315 * @param {string} method |
| 316 * @param {?Object} params | 316 * @param {?Object} params |
| 317 * @param {?function(...*)} callback | 317 * @param {?function(...*)} callback |
| 318 */ | 318 */ |
| 319 _sendRawMessageForTesting(method, params, callback) { | 319 _sendRawMessageForTesting(method, params, callback) { |
| 320 var domain = method.split('.')[0]; | 320 var domain = method.split('.')[0]; |
| 321 this._wrapCallbackAndSendMessageObject(domain, method, params, callback); | 321 this._wrapCallbackAndSendMessageObject(domain, method, params, callback); |
| 322 } | 322 } |
| 323 | 323 |
| 324 /** | 324 /** |
| 325 * @param {!Object|string} message | 325 * @param {!Object|string} message |
| 326 */ | 326 */ |
| 327 _onMessage(message) { | 327 _onMessage(message) { |
| 328 if (InspectorBackendClass.Options.dumpInspectorProtocolMessages) | 328 if (Protocol.InspectorBackend.Options.dumpInspectorProtocolMessages) |
| 329 this._dumpProtocolMessage('backend: ' + ((typeof message === 'string') ? m
essage : JSON.stringify(message))); | 329 this._dumpProtocolMessage('backend: ' + ((typeof message === 'string') ? m
essage : JSON.stringify(message))); |
| 330 | 330 |
| 331 var messageObject = /** @type {!Object} */ ((typeof message === 'string') ?
JSON.parse(message) : message); | 331 var messageObject = /** @type {!Object} */ ((typeof message === 'string') ?
JSON.parse(message) : message); |
| 332 | 332 |
| 333 if ('id' in messageObject) { // just a response for some request | 333 if ('id' in messageObject) { // just a response for some request |
| 334 var callback = this._callbacks[messageObject.id]; | 334 var callback = this._callbacks[messageObject.id]; |
| 335 if (!callback) { | 335 if (!callback) { |
| 336 InspectorBackendClass.reportProtocolError('Protocol Error: the message w
ith wrong id', messageObject); | 336 Protocol.InspectorBackend.reportProtocolError('Protocol Error: the messa
ge with wrong id', messageObject); |
| 337 return; | 337 return; |
| 338 } | 338 } |
| 339 | 339 |
| 340 var timingLabel = 'time-stats: ' + callback.methodName; | 340 var timingLabel = 'time-stats: ' + callback.methodName; |
| 341 if (InspectorBackendClass.Options.dumpInspectorTimeStats) | 341 if (Protocol.InspectorBackend.Options.dumpInspectorTimeStats) |
| 342 console.time(timingLabel); | 342 console.time(timingLabel); |
| 343 | 343 |
| 344 this._agent(callback.domain).dispatchResponse(messageObject, callback.meth
odName, callback); | 344 this._agent(callback.domain).dispatchResponse(messageObject, callback.meth
odName, callback); |
| 345 --this._pendingResponsesCount; | 345 --this._pendingResponsesCount; |
| 346 delete this._callbacks[messageObject.id]; | 346 delete this._callbacks[messageObject.id]; |
| 347 | 347 |
| 348 if (InspectorBackendClass.Options.dumpInspectorTimeStats) | 348 if (Protocol.InspectorBackend.Options.dumpInspectorTimeStats) |
| 349 console.timeEnd(timingLabel); | 349 console.timeEnd(timingLabel); |
| 350 | 350 |
| 351 if (this._scripts && !this._pendingResponsesCount) | 351 if (this._scripts && !this._pendingResponsesCount) |
| 352 this._deprecatedRunAfterPendingDispatches(); | 352 this._deprecatedRunAfterPendingDispatches(); |
| 353 } else { | 353 } else { |
| 354 if (!('method' in messageObject)) { | 354 if (!('method' in messageObject)) { |
| 355 InspectorBackendClass.reportProtocolError('Protocol Error: the message w
ithout method', messageObject); | 355 Protocol.InspectorBackend.reportProtocolError('Protocol Error: the messa
ge without method', messageObject); |
| 356 return; | 356 return; |
| 357 } | 357 } |
| 358 | 358 |
| 359 var method = messageObject.method.split('.'); | 359 var method = messageObject.method.split('.'); |
| 360 var domainName = method[0]; | 360 var domainName = method[0]; |
| 361 if (!(domainName in this._dispatchers)) { | 361 if (!(domainName in this._dispatchers)) { |
| 362 InspectorBackendClass.reportProtocolError( | 362 Protocol.InspectorBackend.reportProtocolError( |
| 363 'Protocol Error: the message ' + messageObject.method + ' is for non
-existing domain \'' + domainName + | 363 'Protocol Error: the message ' + messageObject.method + ' is for non
-existing domain \'' + domainName + |
| 364 '\'', | 364 '\'', |
| 365 messageObject); | 365 messageObject); |
| 366 return; | 366 return; |
| 367 } | 367 } |
| 368 | 368 |
| 369 this._dispatchers[domainName].dispatch(method[1], messageObject); | 369 this._dispatchers[domainName].dispatch(method[1], messageObject); |
| 370 } | 370 } |
| 371 } | 371 } |
| 372 | 372 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 } | 450 } |
| 451 | 451 |
| 452 /** | 452 /** |
| 453 * @param {string} domain | 453 * @param {string} domain |
| 454 * @param {string} methodName | 454 * @param {string} methodName |
| 455 * @param {function(*)} callback | 455 * @param {function(*)} callback |
| 456 */ | 456 */ |
| 457 _dispatchConnectionErrorResponse(domain, methodName, callback) { | 457 _dispatchConnectionErrorResponse(domain, methodName, callback) { |
| 458 var error = { | 458 var error = { |
| 459 message: 'Connection is closed, can\'t dispatch pending ' + methodName, | 459 message: 'Connection is closed, can\'t dispatch pending ' + methodName, |
| 460 code: InspectorBackendClass._ConnectionClosedErrorCode, | 460 code: Protocol.InspectorBackend._ConnectionClosedErrorCode, |
| 461 data: null | 461 data: null |
| 462 }; | 462 }; |
| 463 var messageObject = {error: error}; | 463 var messageObject = {error: error}; |
| 464 setTimeout( | 464 setTimeout( |
| 465 InspectorBackendClass._AgentPrototype.prototype.dispatchResponse.bind( | 465 Protocol.InspectorBackend._AgentPrototype.prototype.dispatchResponse.bin
d( |
| 466 this._agent(domain), messageObject, methodName, callback), | 466 this._agent(domain), messageObject, methodName, callback), |
| 467 0); | 467 0); |
| 468 } | 468 } |
| 469 }; | 469 }; |
| 470 | 470 |
| 471 /** | 471 /** |
| 472 * @unrestricted | 472 * @unrestricted |
| 473 */ | 473 */ |
| 474 InspectorBackendClass._AgentPrototype = class { | 474 Protocol.InspectorBackend._AgentPrototype = class { |
| 475 /** | 475 /** |
| 476 * @param {string} domain | 476 * @param {string} domain |
| 477 */ | 477 */ |
| 478 constructor(domain) { | 478 constructor(domain) { |
| 479 this._replyArgs = {}; | 479 this._replyArgs = {}; |
| 480 this._hasErrorData = {}; | 480 this._hasErrorData = {}; |
| 481 this._domain = domain; | 481 this._domain = domain; |
| 482 } | 482 } |
| 483 | 483 |
| 484 /** | 484 /** |
| 485 * @param {!Protocol.TargetBase} target | 485 * @param {!Protocol.TargetBase} target |
| 486 */ | 486 */ |
| 487 setTarget(target) { | 487 setTarget(target) { |
| 488 this._target = target; | 488 this._target = target; |
| 489 } | 489 } |
| 490 | 490 |
| 491 /** | 491 /** |
| 492 * @param {string} methodName | 492 * @param {string} methodName |
| 493 * @param {!Array.<!Object>} signature | 493 * @param {!Array.<!Object>} signature |
| 494 * @param {!Array.<string>} replyArgs | 494 * @param {!Array.<string>} replyArgs |
| 495 * @param {boolean} hasErrorData | 495 * @param {boolean} hasErrorData |
| 496 */ | 496 */ |
| 497 registerCommand(methodName, signature, replyArgs, hasErrorData) { | 497 registerCommand(methodName, signature, replyArgs, hasErrorData) { |
| 498 var domainAndMethod = this._domain + '.' + methodName; | 498 var domainAndMethod = this._domain + '.' + methodName; |
| 499 | 499 |
| 500 /** | 500 /** |
| 501 * @param {...*} vararg | 501 * @param {...*} vararg |
| 502 * @this {InspectorBackendClass._AgentPrototype} | 502 * @this {Protocol.InspectorBackend._AgentPrototype} |
| 503 * @return {!Promise.<*>} | 503 * @return {!Promise.<*>} |
| 504 */ | 504 */ |
| 505 function sendMessagePromise(vararg) { | 505 function sendMessagePromise(vararg) { |
| 506 var params = Array.prototype.slice.call(arguments); | 506 var params = Array.prototype.slice.call(arguments); |
| 507 return InspectorBackendClass._AgentPrototype.prototype._sendMessageToBacke
ndPromise.call( | 507 return Protocol.InspectorBackend._AgentPrototype.prototype._sendMessageToB
ackendPromise.call( |
| 508 this, domainAndMethod, signature, params); | 508 this, domainAndMethod, signature, params); |
| 509 } | 509 } |
| 510 | 510 |
| 511 this[methodName] = sendMessagePromise; | 511 this[methodName] = sendMessagePromise; |
| 512 | 512 |
| 513 /** | 513 /** |
| 514 * @param {...*} vararg | 514 * @param {...*} vararg |
| 515 * @this {InspectorBackendClass._AgentPrototype} | 515 * @this {Protocol.InspectorBackend._AgentPrototype} |
| 516 */ | 516 */ |
| 517 function invoke(vararg) { | 517 function invoke(vararg) { |
| 518 var params = [domainAndMethod].concat(Array.prototype.slice.call(arguments
)); | 518 var params = [domainAndMethod].concat(Array.prototype.slice.call(arguments
)); |
| 519 InspectorBackendClass._AgentPrototype.prototype._invoke.apply(this, params
); | 519 Protocol.InspectorBackend._AgentPrototype.prototype._invoke.apply(this, pa
rams); |
| 520 } | 520 } |
| 521 | 521 |
| 522 this['invoke_' + methodName] = invoke; | 522 this['invoke_' + methodName] = invoke; |
| 523 | 523 |
| 524 this._replyArgs[domainAndMethod] = replyArgs; | 524 this._replyArgs[domainAndMethod] = replyArgs; |
| 525 if (hasErrorData) | 525 if (hasErrorData) |
| 526 this._hasErrorData[domainAndMethod] = true; | 526 this._hasErrorData[domainAndMethod] = true; |
| 527 } | 527 } |
| 528 | 528 |
| 529 /** | 529 /** |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 var userCallback = (args.length && typeof args.peekLast() === 'function') ?
args.pop() : null; | 598 var userCallback = (args.length && typeof args.peekLast() === 'function') ?
args.pop() : null; |
| 599 var params = this._prepareParameters(method, signature, args, !userCallback,
onError); | 599 var params = this._prepareParameters(method, signature, args, !userCallback,
onError); |
| 600 if (errorMessage) | 600 if (errorMessage) |
| 601 return Promise.reject(new Error(errorMessage)); | 601 return Promise.reject(new Error(errorMessage)); |
| 602 else | 602 else |
| 603 return new Promise(promiseAction.bind(this)); | 603 return new Promise(promiseAction.bind(this)); |
| 604 | 604 |
| 605 /** | 605 /** |
| 606 * @param {function(?)} resolve | 606 * @param {function(?)} resolve |
| 607 * @param {function(!Error)} reject | 607 * @param {function(!Error)} reject |
| 608 * @this {InspectorBackendClass._AgentPrototype} | 608 * @this {Protocol.InspectorBackend._AgentPrototype} |
| 609 */ | 609 */ |
| 610 function promiseAction(resolve, reject) { | 610 function promiseAction(resolve, reject) { |
| 611 /** | 611 /** |
| 612 * @param {...*} vararg | 612 * @param {...*} vararg |
| 613 */ | 613 */ |
| 614 function callback(vararg) { | 614 function callback(vararg) { |
| 615 var result = userCallback ? userCallback.apply(null, arguments) : undefi
ned; | 615 var result = userCallback ? userCallback.apply(null, arguments) : undefi
ned; |
| 616 resolve(result); | 616 resolve(result); |
| 617 } | 617 } |
| 618 this._target._wrapCallbackAndSendMessageObject(this._domain, method, param
s, callback); | 618 this._target._wrapCallbackAndSendMessageObject(this._domain, method, param
s, callback); |
| 619 } | 619 } |
| 620 } | 620 } |
| 621 | 621 |
| 622 /** | 622 /** |
| 623 * @param {string} method | 623 * @param {string} method |
| 624 * @param {?Object} args | 624 * @param {?Object} args |
| 625 * @param {?function(*)} callback | 625 * @param {?function(*)} callback |
| 626 */ | 626 */ |
| 627 _invoke(method, args, callback) { | 627 _invoke(method, args, callback) { |
| 628 this._target._wrapCallbackAndSendMessageObject(this._domain, method, args, c
allback); | 628 this._target._wrapCallbackAndSendMessageObject(this._domain, method, args, c
allback); |
| 629 } | 629 } |
| 630 | 630 |
| 631 /** | 631 /** |
| 632 * @param {!Object} messageObject | 632 * @param {!Object} messageObject |
| 633 * @param {string} methodName | 633 * @param {string} methodName |
| 634 * @param {function(*)|function(?Protocol.Error, ?Object)} callback | 634 * @param {function(*)|function(?Protocol.Error, ?Object)} callback |
| 635 */ | 635 */ |
| 636 dispatchResponse(messageObject, methodName, callback) { | 636 dispatchResponse(messageObject, methodName, callback) { |
| 637 if (messageObject.error && messageObject.error.code !== InspectorBackendClas
s._ConnectionClosedErrorCode && | 637 if (messageObject.error && messageObject.error.code !== Protocol.InspectorBa
ckend._ConnectionClosedErrorCode && |
| 638 messageObject.error.code !== InspectorBackendClass.DevToolsStubErrorCode
&& | 638 messageObject.error.code !== Protocol.InspectorBackend.DevToolsStubError
Code && |
| 639 !InspectorBackendClass.Options.suppressRequestErrors) { | 639 !Protocol.InspectorBackend.Options.suppressRequestErrors) { |
| 640 var id = InspectorBackendClass.Options.dumpInspectorProtocolMessages ? ' w
ith id = ' + messageObject.id : ''; | 640 var id = Protocol.InspectorBackend.Options.dumpInspectorProtocolMessages ?
' with id = ' + messageObject.id : ''; |
| 641 console.error('Request ' + methodName + id + ' failed. ' + JSON.stringify(
messageObject.error)); | 641 console.error('Request ' + methodName + id + ' failed. ' + JSON.stringify(
messageObject.error)); |
| 642 } | 642 } |
| 643 | 643 |
| 644 var argumentsArray = []; | 644 var argumentsArray = []; |
| 645 argumentsArray[0] = messageObject.error ? messageObject.error.message : null
; | 645 argumentsArray[0] = messageObject.error ? messageObject.error.message : null
; |
| 646 | 646 |
| 647 if (this._hasErrorData[methodName]) | 647 if (this._hasErrorData[methodName]) |
| 648 argumentsArray[1] = messageObject.error ? messageObject.error.data : null; | 648 argumentsArray[1] = messageObject.error ? messageObject.error.data : null; |
| 649 | 649 |
| 650 if (messageObject.result) { | 650 if (messageObject.result) { |
| 651 var paramNames = this._replyArgs[methodName] || []; | 651 var paramNames = this._replyArgs[methodName] || []; |
| 652 for (var i = 0; i < paramNames.length; ++i) | 652 for (var i = 0; i < paramNames.length; ++i) |
| 653 argumentsArray.push(messageObject.result[paramNames[i]]); | 653 argumentsArray.push(messageObject.result[paramNames[i]]); |
| 654 } | 654 } |
| 655 | 655 |
| 656 callback.apply(null, argumentsArray); | 656 callback.apply(null, argumentsArray); |
| 657 } | 657 } |
| 658 }; | 658 }; |
| 659 | 659 |
| 660 /** | 660 /** |
| 661 * @unrestricted | 661 * @unrestricted |
| 662 */ | 662 */ |
| 663 InspectorBackendClass._DispatcherPrototype = class { | 663 Protocol.InspectorBackend._DispatcherPrototype = class { |
| 664 constructor() { | 664 constructor() { |
| 665 this._eventArgs = {}; | 665 this._eventArgs = {}; |
| 666 this._dispatcher = null; | 666 this._dispatcher = null; |
| 667 } | 667 } |
| 668 | 668 |
| 669 /** | 669 /** |
| 670 * @param {string} eventName | 670 * @param {string} eventName |
| 671 * @param {!Object} params | 671 * @param {!Object} params |
| 672 */ | 672 */ |
| 673 registerEvent(eventName, params) { | 673 registerEvent(eventName, params) { |
| 674 this._eventArgs[eventName] = params; | 674 this._eventArgs[eventName] = params; |
| 675 } | 675 } |
| 676 | 676 |
| 677 /** | 677 /** |
| 678 * @param {!Object} dispatcher | 678 * @param {!Object} dispatcher |
| 679 */ | 679 */ |
| 680 setDomainDispatcher(dispatcher) { | 680 setDomainDispatcher(dispatcher) { |
| 681 this._dispatcher = dispatcher; | 681 this._dispatcher = dispatcher; |
| 682 } | 682 } |
| 683 | 683 |
| 684 /** | 684 /** |
| 685 * @param {string} functionName | 685 * @param {string} functionName |
| 686 * @param {!Object} messageObject | 686 * @param {!Object} messageObject |
| 687 */ | 687 */ |
| 688 dispatch(functionName, messageObject) { | 688 dispatch(functionName, messageObject) { |
| 689 if (!this._dispatcher) | 689 if (!this._dispatcher) |
| 690 return; | 690 return; |
| 691 | 691 |
| 692 if (!(functionName in this._dispatcher)) { | 692 if (!(functionName in this._dispatcher)) { |
| 693 InspectorBackendClass.reportProtocolError( | 693 Protocol.InspectorBackend.reportProtocolError( |
| 694 'Protocol Error: Attempted to dispatch an unimplemented method \'' + m
essageObject.method + '\'', | 694 'Protocol Error: Attempted to dispatch an unimplemented method \'' + m
essageObject.method + '\'', |
| 695 messageObject); | 695 messageObject); |
| 696 return; | 696 return; |
| 697 } | 697 } |
| 698 | 698 |
| 699 if (!this._eventArgs[messageObject.method]) { | 699 if (!this._eventArgs[messageObject.method]) { |
| 700 InspectorBackendClass.reportProtocolError( | 700 Protocol.InspectorBackend.reportProtocolError( |
| 701 'Protocol Error: Attempted to dispatch an unspecified method \'' + mes
sageObject.method + '\'', | 701 'Protocol Error: Attempted to dispatch an unspecified method \'' + mes
sageObject.method + '\'', |
| 702 messageObject); | 702 messageObject); |
| 703 return; | 703 return; |
| 704 } | 704 } |
| 705 | 705 |
| 706 var params = []; | 706 var params = []; |
| 707 if (messageObject.params) { | 707 if (messageObject.params) { |
| 708 var paramNames = this._eventArgs[messageObject.method]; | 708 var paramNames = this._eventArgs[messageObject.method]; |
| 709 for (var i = 0; i < paramNames.length; ++i) | 709 for (var i = 0; i < paramNames.length; ++i) |
| 710 params.push(messageObject.params[paramNames[i]]); | 710 params.push(messageObject.params[paramNames[i]]); |
| 711 } | 711 } |
| 712 | 712 |
| 713 var timingLabel = 'time-stats: ' + messageObject.method; | 713 var timingLabel = 'time-stats: ' + messageObject.method; |
| 714 if (InspectorBackendClass.Options.dumpInspectorTimeStats) | 714 if (Protocol.InspectorBackend.Options.dumpInspectorTimeStats) |
| 715 console.time(timingLabel); | 715 console.time(timingLabel); |
| 716 | 716 |
| 717 this._dispatcher[functionName].apply(this._dispatcher, params); | 717 this._dispatcher[functionName].apply(this._dispatcher, params); |
| 718 | 718 |
| 719 if (InspectorBackendClass.Options.dumpInspectorTimeStats) | 719 if (Protocol.InspectorBackend.Options.dumpInspectorTimeStats) |
| 720 console.timeEnd(timingLabel); | 720 console.timeEnd(timingLabel); |
| 721 } | 721 } |
| 722 }; | 722 }; |
| 723 | 723 |
| 724 InspectorBackendClass.Options = { | 724 Protocol.InspectorBackend.Options = { |
| 725 dumpInspectorTimeStats: false, | 725 dumpInspectorTimeStats: false, |
| 726 dumpInspectorProtocolMessages: false, | 726 dumpInspectorProtocolMessages: false, |
| 727 suppressRequestErrors: false | 727 suppressRequestErrors: false |
| 728 }; | 728 }; |
| OLD | NEW |