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

Unified Diff: third_party/WebKit/Source/devtools/front_end/sdk/Channels.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/Channels.js
diff --git a/third_party/WebKit/Source/devtools/front_end/sdk/Channels.js b/third_party/WebKit/Source/devtools/front_end/sdk/Channels.js
new file mode 100644
index 0000000000000000000000000000000000000000..0291d98adc91116f9087e5805f0ac770757fab14
--- /dev/null
+++ b/third_party/WebKit/Source/devtools/front_end/sdk/Channels.js
@@ -0,0 +1,221 @@
+// Copyright (c) 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @constructor
+ * @extends {WebInspector.Object}
+ * @implements {InspectorBackendClass.Channel}
+ */
+WebInspector.MainChannel = function()
+{
+ WebInspector.Object.call(this);
+ InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.DispatchMessage, this._dispatchMessage, this);
+ InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.DispatchMessageChunk, this._dispatchMessageChunk, this);
+ InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.EvaluateForTestInFrontend, this._evaluateForTestInFrontend, this);
+}
+
+WebInspector.MainChannel.prototype = {
+ /**
+ * @override
+ * @param {string} message
+ */
+ sendMessage: function(message)
+ {
+ InspectorFrontendHost.sendMessageToBackend(message);
+ },
+
+ /**
+ * @param {!WebInspector.Event} event
+ */
+ _dispatchMessage: function(event)
+ {
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.MessageReceived, event.data);
+ },
+
+ /**
+ * @param {!WebInspector.Event} event
+ */
+ _dispatchMessageChunk: function(event)
+ {
+ var messageChunk = /** @type {string} */ (event.data["messageChunk"]);
+ var messageSize = /** @type {number} */ (event.data["messageSize"]);
+ if (messageSize) {
+ this._messageBuffer = "";
+ this._messageSize = messageSize;
+ }
+ this._messageBuffer += messageChunk;
+ if (this._messageBuffer.length === this._messageSize) {
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.MessageReceived, this._messageBuffer);
+ this._messageBuffer = "";
+ this._messageSize = 0;
+ }
+ },
+
+ /**
+ * @param {!WebInspector.Event} event
+ */
+ _evaluateForTestInFrontend: function(event)
+ {
+ if (!InspectorFrontendHost.isUnderTest())
+ return;
+
+ var callId = /** @type {number} */ (event.data["callId"]);
+ var script = /** @type {number} */ (event.data["script"]);
+
+ /**
+ * @suppressGlobalPropertiesCheck
+ */
+ function invokeMethod()
+ {
+ try {
+ script = script + "//# sourceURL=evaluateInWebInspector" + callId + ".js";
+ window.eval(script);
+ } catch (e) {
+ console.error(e.stack);
+ }
+ }
+
+ InspectorBackendClass.mainConnectionForTesting.deprecatedRunAfterPendingDispatches(invokeMethod);
+ },
+
+ /**
+ * @override
+ * @param {function()} callback
+ */
+ reconnect: function(callback)
+ {
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.ChannelClosed, "reconnecting");
+ InspectorFrontendHost.reattach(callback);
+ },
+
+ __proto__: WebInspector.Object.prototype
+}
+
+/**
+ * @constructor
+ * @extends {WebInspector.Object}
+ * @implements {InspectorBackendClass.Channel}
+ * @param {string} url
+ * @param {function()} onChannelReady
+ */
+WebInspector.WebSocketChannel = function(url, onChannelReady)
+{
+ WebInspector.Object.call(this);
+ this._url = url;
+ this._connect(onChannelReady);
+}
+
+/**
+ * @param {string} url
+ * @return {!Promise<!InspectorBackendClass.Channel>}
+ */
+WebInspector.WebSocketChannel.Create = function(url)
+{
+ var fulfill;
+ var result = new Promise(resolve => fulfill = resolve);
+ var channel = new WebInspector.WebSocketChannel(url, () => fulfill(channel));
+ return result;
+}
+
+WebInspector.WebSocketChannel.prototype = {
+ /**
+ * @param {function()} onChannelReady
+ */
+ _connect: function(onChannelReady)
+ {
+ this._socket = new WebSocket(this._url);
+ this._socket.onmessage = this._onMessage.bind(this);
+ this._socket.onerror = this._onError.bind(this);
+ this._socket.onopen = onChannelReady.bind(null);
+ this._socket.onclose = this._onClosed.bind(this);
+ },
+
+ /**
+ * @param {!MessageEvent} message
+ */
+ _onMessage: function(message)
+ {
+ var data = /** @type {string} */ (message.data);
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.MessageReceived, data);
+ },
+
+ /**
+ * @param {!Event} error
+ */
+ _onError: function(error)
+ {
+ console.error(error);
+ },
+
+ _onClosed: function()
+ {
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.ChannelClosed, "websocket closed");
+ },
+
+ /**
+ * @override
+ * @param {string} message
+ */
+ sendMessage: function(message)
+ {
+ this._socket.send(message);
+ },
+
+ /**
+ * @override
+ * @param {function()} callback
+ */
+ reconnect: function(callback)
+ {
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.ChannelClosed, "reconnecting");
+ this._socket.onclose = null;
+ this._socket.close();
+ this._connect(callback);
+ },
+
+ __proto__: WebInspector.Object.prototype
+}
+
+/**
+ * @constructor
+ * @extends {WebInspector.Object}
+ * @implements {InspectorBackendClass.Channel}
+ */
+WebInspector.StubChannel = function()
+{
+ WebInspector.Object.call(this);
+}
+
+WebInspector.StubChannel.prototype = {
+ /**
+ * @override
+ * @param {string} message
+ */
+ sendMessage: function(message)
+ {
+ setTimeout(this._respondWithError.bind(this, message), 0);
+ },
+
+ /**
+ * @override
+ * @param {function()} callback
+ */
+ reconnect: function(callback)
+ {
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.ChannelClosed, "reconnecting");
+ setTimeout(callback, 0);
+ },
+
+ /**
+ * @param {string} message
+ */
+ _respondWithError: function(message)
+ {
+ var messageObject = JSON.parse(message);
+ var error = { message: "This is a stub connection, can't dispatch message.", code: InspectorBackendClass.DevToolsStubErrorCode, data: messageObject };
+ this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.MessageReceived, { id: messageObject.id, error: error });
+ },
+
+ __proto__: WebInspector.Object.prototype
+}

Powered by Google App Engine
This is Rietveld 408576698