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

Unified Diff: third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.js

Issue 2441933002: [DevTools] Refactor connection-related classes. (Closed)
Patch Set: test fixes 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.js
diff --git a/third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.js b/third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.js
index cd0d3c12599a6385d24cdeaa207f12cee69c52ef..b177768f5deae3c8c8edaac627d5ac59f84f725a 100644
--- a/third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.js
+++ b/third_party/WebKit/Source/devtools/front_end/sdk/InspectorBackend.js
@@ -202,10 +202,13 @@ InspectorBackendClass.prototype = {
/**
* @constructor
- * @extends {WebInspector.Object}
+ * @param {!InspectorBackendClass.Channel} channel
+ * @param {function(string)} disconnectedCallback
*/
-InspectorBackendClass.Connection = function()
+InspectorBackendClass.Connection = function(channel, disconnectedCallback)
{
+ this._channel = channel;
+ this._disconnectedCallback = disconnectedCallback;
this._lastMessageId = 1;
this._pendingResponsesCount = 0;
this._agents = {};
@@ -213,15 +216,39 @@ InspectorBackendClass.Connection = function()
this._callbacks = {};
this._initialize(InspectorBackend._agentPrototypes, InspectorBackend._dispatcherPrototypes);
this._isConnected = true;
+ this._eventListeners = [
+ this._channel.addEventListener(InspectorBackendClass.Channel.Events.ChannelClosed, this._channelClosed, this),
+ this._channel.addEventListener(InspectorBackendClass.Channel.Events.MessageReceived, this._messageReceived, this)
+ ];
+ if (!InspectorBackendClass.mainConnectionForTesting)
+ InspectorBackendClass.mainConnectionForTesting = this;
}
-/** @enum {symbol} */
-InspectorBackendClass.Connection.Events = {
- Disconnected: Symbol("Disconnected")
-}
+/** @type {?InspectorBackendClass.Connection} */
+InspectorBackendClass.mainConnectionForTesting = null;
InspectorBackendClass.Connection.prototype = {
/**
+ * @return {!Object.<string, !Object>}
+ */
+ agentsMap: function()
+ {
+ return this._agents;
+ },
+
+ /**
+ * @param {string} domain
+ * @param {!Object} dispatcher
+ */
+ registerDispatcher: function(domain, dispatcher)
+ {
+ if (!this._dispatchers[domain])
+ return;
+
+ this._dispatchers[domain].setDomainDispatcher(dispatcher);
+ },
+
+ /**
* @param {!Object.<string, !InspectorBackendClass.AgentPrototype>} agentPrototypes
* @param {!Object.<string, !InspectorBackendClass.DispatcherPrototype>} dispatcherPrototypes
*/
@@ -239,7 +266,7 @@ InspectorBackendClass.Connection.prototype = {
/**
* @return {number}
*/
- nextMessageId: function()
+ _nextMessageId: function()
{
return this._lastMessageId++;
},
@@ -248,20 +275,12 @@ InspectorBackendClass.Connection.prototype = {
* @param {string} domain
* @return {!InspectorBackendClass.AgentPrototype}
*/
- agent: function(domain)
+ _agent: function(domain)
{
return this._agents[domain];
},
/**
- * @return {!Object.<string, !Object>}
- */
- agentsMap: function()
- {
- return this._agents;
- },
-
- /**
* @param {string} domain
* @param {string} method
* @param {?Object} params
@@ -269,13 +288,14 @@ InspectorBackendClass.Connection.prototype = {
*/
_wrapCallbackAndSendMessageObject: function(domain, method, params, callback)
{
- if (!this._isConnected && callback) {
- this._dispatchConnectionErrorResponse(domain, method, callback);
+ if (!this._isConnected) {
+ if (callback)
+ this._dispatchConnectionErrorResponse(domain, method, callback);
return;
}
var messageObject = {};
- var messageId = this.nextMessageId();
+ var messageId = this._nextMessageId();
messageObject.id = messageId;
messageObject.method = method;
if (params)
@@ -286,7 +306,7 @@ InspectorBackendClass.Connection.prototype = {
if (InspectorBackendClass.Options.dumpInspectorProtocolMessages)
this._dumpProtocolMessage("frontend: " + JSON.stringify(messageObject));
- this.sendMessage(messageObject);
+ this._channel.sendMessage(JSON.stringify(messageObject));
++this._pendingResponsesCount;
this._callbacks[messageId] = wrappedCallback;
},
@@ -311,29 +331,22 @@ InspectorBackendClass.Connection.prototype = {
},
/**
- * @param {!Object} messageObject
- */
- sendMessage: function(messageObject)
- {
- throw "Not implemented";
- },
-
- /**
* @param {string} method
* @param {?Object} params
* @param {?function(...*)} callback
*/
- sendRawMessageForTesting: function(method, params, callback)
+ _sendRawMessageForTesting: function(method, params, callback)
{
var domain = method.split(".")[0];
this._wrapCallbackAndSendMessageObject(domain, method, params, callback);
},
/**
- * @param {!Object|string} message
+ * @param {!WebInspector.Event} event
*/
- dispatch: function(message)
+ _messageReceived: function(event)
{
+ var message = /** @type {!Object|string} */ (event.data);
if (InspectorBackendClass.Options.dumpInspectorProtocolMessages)
this._dumpProtocolMessage("backend: " + ((typeof message === "string") ? message : JSON.stringify(message)));
@@ -350,7 +363,7 @@ InspectorBackendClass.Connection.prototype = {
if (InspectorBackendClass.Options.dumpInspectorTimeStats)
processingStartTime = Date.now();
- this.agent(callback.domain).dispatchResponse(messageObject, callback.methodName, callback);
+ this._agent(callback.domain).dispatchResponse(messageObject, callback.methodName, callback);
--this._pendingResponsesCount;
delete this._callbacks[messageObject.id];
@@ -359,8 +372,12 @@ InspectorBackendClass.Connection.prototype = {
if (this._scripts && !this._pendingResponsesCount)
this.deprecatedRunAfterPendingDispatches();
- return;
} else {
+ if (!("method" in messageObject)) {
+ InspectorBackendClass.reportProtocolError("Protocol Error: the message without method", messageObject);
+ return;
+ }
+
var method = messageObject.method.split(".");
var domainName = method[0];
if (!(domainName in this._dispatchers)) {
@@ -370,19 +387,6 @@ InspectorBackendClass.Connection.prototype = {
this._dispatchers[domainName].dispatch(method[1], messageObject);
}
-
- },
-
- /**
- * @param {string} domain
- * @param {!Object} dispatcher
- */
- registerDispatcher: function(domain, dispatcher)
- {
- if (!this._dispatchers[domain])
- return;
-
- this._dispatchers[domain].setDomainDispatcher(dispatcher);
},
/**
@@ -415,33 +419,25 @@ InspectorBackendClass.Connection.prototype = {
}
},
- _dumpProtocolMessage: function(message)
- {
- console.log(message);
- },
-
- close: function()
- {
- this.forceClose();
- this.connectionClosed("force close");
- },
-
/**
- * @protected
+ * @param {!Object|string} message
*/
- forceClose: function()
+ _dumpProtocolMessage: function(message)
{
+ console.log(message);
},
/**
- * @protected
- * @param {string} reason
+ * @param {!WebInspector.Event} event
*/
- connectionClosed: function(reason)
+ _channelClosed: function(event)
{
+ WebInspector.EventTarget.removeEventListeners(this._eventListeners);
+ var reason = /** @type {string} */ (event.data);
this._isConnected = false;
this._runPendingCallbacks();
- this.dispatchEventToListeners(InspectorBackendClass.Connection.Events.Disconnected, {reason: reason});
+ this._disconnectedCallback.call(null, reason);
+ this._disconnectedCallback = null;
},
_runPendingCallbacks: function()
@@ -463,29 +459,38 @@ InspectorBackendClass.Connection.prototype = {
{
var error = { message: "Connection is closed, can't dispatch pending " + methodName, code: InspectorBackendClass._DevToolsErrorCode, data: null};
var messageObject = {error: error};
- setTimeout(InspectorBackendClass.AgentPrototype.prototype.dispatchResponse.bind(this.agent(domain), messageObject, methodName, callback), 0);
- },
+ setTimeout(InspectorBackendClass.AgentPrototype.prototype.dispatchResponse.bind(this._agent(domain), messageObject, methodName, callback), 0);
+ }
+}
+
+/**
+ * @interface
+ * @extends {WebInspector.EventTarget}
+ */
+InspectorBackendClass.Channel = function()
+{
+}
+
+/** @enum {symbol} */
+InspectorBackendClass.Channel.Events = {
+ MessageReceived: Symbol("MessageReceived"),
+ ChannelClosed: Symbol("ChannelClosed")
+}
+
+InspectorBackendClass.Channel.prototype = {
/**
- * @return {boolean}
+ * @param {string} message
*/
- isClosed: function()
- {
- return !this._isConnected;
- },
+ sendMessage: function(message) { },
/**
- * @param {!Array.<string>} domains
+ * @param {function()} callback
*/
- suppressErrorsForDomains: function(domains)
- {
- domains.forEach(function(domain) { this._agents[domain].suppressErrorLogging(); }, this);
- },
-
- __proto__: WebInspector.Object.prototype
-
+ reconnect: function(callback) { },
}
+
/**
* @constructor
* @param {string} domain
@@ -754,3 +759,20 @@ InspectorBackendClass.Options = {
}
InspectorBackend = new InspectorBackendClass();
+
+/**
+ * @param {string} method
+ * @param {?Object} params
+ * @return {!Promise}
+ */
+WebInspector.sendOverProtocol = function(method, params)
+{
+ var connection = InspectorBackendClass.mainConnectionForTesting;
+ return new Promise((resolve, reject) => {
+ connection._sendRawMessageForTesting(method, params, (err, result) => {
+ if (err)
+ return reject(err);
+ return resolve(result);
+ });
+ });
+}

Powered by Google App Engine
This is Rietveld 408576698