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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6 * @constructor
7 * @extends {WebInspector.Object}
8 * @implements {InspectorBackendClass.Channel}
9 */
10 WebInspector.MainChannel = function()
11 {
12 WebInspector.Object.call(this);
13 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event s.DispatchMessage, this._dispatchMessage, this);
14 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event s.DispatchMessageChunk, this._dispatchMessageChunk, this);
15 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event s.EvaluateForTestInFrontend, this._evaluateForTestInFrontend, this);
16 }
17
18 WebInspector.MainChannel.prototype = {
19 /**
20 * @override
21 * @param {string} message
22 */
23 sendMessage: function(message)
24 {
25 InspectorFrontendHost.sendMessageToBackend(message);
26 },
27
28 /**
29 * @param {!WebInspector.Event} event
30 */
31 _dispatchMessage: function(event)
32 {
33 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Messa geReceived, event.data);
34 },
35
36 /**
37 * @param {!WebInspector.Event} event
38 */
39 _dispatchMessageChunk: function(event)
40 {
41 var messageChunk = /** @type {string} */ (event.data["messageChunk"]);
42 var messageSize = /** @type {number} */ (event.data["messageSize"]);
43 if (messageSize) {
44 this._messageBuffer = "";
45 this._messageSize = messageSize;
46 }
47 this._messageBuffer += messageChunk;
48 if (this._messageBuffer.length === this._messageSize) {
49 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.M essageReceived, this._messageBuffer);
50 this._messageBuffer = "";
51 this._messageSize = 0;
52 }
53 },
54
55 /**
56 * @param {!WebInspector.Event} event
57 */
58 _evaluateForTestInFrontend: function(event)
59 {
60 if (!InspectorFrontendHost.isUnderTest())
61 return;
62
63 var callId = /** @type {number} */ (event.data["callId"]);
64 var script = /** @type {number} */ (event.data["script"]);
65
66 /**
67 * @suppressGlobalPropertiesCheck
68 */
69 function invokeMethod()
70 {
71 try {
72 script = script + "//# sourceURL=evaluateInWebInspector" + callI d + ".js";
73 window.eval(script);
74 } catch (e) {
75 console.error(e.stack);
76 }
77 }
78
79 InspectorBackendClass.mainConnectionForTesting.deprecatedRunAfterPending Dispatches(invokeMethod);
80 },
81
82 /**
83 * @override
84 * @param {function()} callback
85 */
86 reconnect: function(callback)
87 {
88 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Chann elClosed, "reconnecting");
89 InspectorFrontendHost.reattach(callback);
90 },
91
92 __proto__: WebInspector.Object.prototype
93 }
94
95 /**
96 * @constructor
97 * @extends {WebInspector.Object}
98 * @implements {InspectorBackendClass.Channel}
99 * @param {string} url
100 * @param {function()} onChannelReady
101 */
102 WebInspector.WebSocketChannel = function(url, onChannelReady)
103 {
104 WebInspector.Object.call(this);
105 this._url = url;
106 this._connect(onChannelReady);
107 }
108
109 /**
110 * @param {string} url
111 * @return {!Promise<!InspectorBackendClass.Channel>}
112 */
113 WebInspector.WebSocketChannel.Create = function(url)
114 {
115 var fulfill;
116 var result = new Promise(resolve => fulfill = resolve);
117 var channel = new WebInspector.WebSocketChannel(url, () => fulfill(channel)) ;
118 return result;
119 }
120
121 WebInspector.WebSocketChannel.prototype = {
122 /**
123 * @param {function()} onChannelReady
124 */
125 _connect: function(onChannelReady)
126 {
127 this._socket = new WebSocket(this._url);
128 this._socket.onmessage = this._onMessage.bind(this);
129 this._socket.onerror = this._onError.bind(this);
130 this._socket.onopen = onChannelReady.bind(null);
131 this._socket.onclose = this._onClosed.bind(this);
132 },
133
134 /**
135 * @param {!MessageEvent} message
136 */
137 _onMessage: function(message)
138 {
139 var data = /** @type {string} */ (message.data);
140 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Messa geReceived, data);
141 },
142
143 /**
144 * @param {!Event} error
145 */
146 _onError: function(error)
147 {
148 console.error(error);
149 },
150
151 _onClosed: function()
152 {
153 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Chann elClosed, "websocket closed");
154 },
155
156 /**
157 * @override
158 * @param {string} message
159 */
160 sendMessage: function(message)
161 {
162 this._socket.send(message);
163 },
164
165 /**
166 * @override
167 * @param {function()} callback
168 */
169 reconnect: function(callback)
170 {
171 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Chann elClosed, "reconnecting");
172 this._socket.onclose = null;
173 this._socket.close();
174 this._connect(callback);
175 },
176
177 __proto__: WebInspector.Object.prototype
178 }
179
180 /**
181 * @constructor
182 * @extends {WebInspector.Object}
183 * @implements {InspectorBackendClass.Channel}
184 */
185 WebInspector.StubChannel = function()
186 {
187 WebInspector.Object.call(this);
188 }
189
190 WebInspector.StubChannel.prototype = {
191 /**
192 * @override
193 * @param {string} message
194 */
195 sendMessage: function(message)
196 {
197 setTimeout(this._respondWithError.bind(this, message), 0);
198 },
199
200 /**
201 * @override
202 * @param {function()} callback
203 */
204 reconnect: function(callback)
205 {
206 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Chann elClosed, "reconnecting");
207 setTimeout(callback, 0);
208 },
209
210 /**
211 * @param {string} message
212 */
213 _respondWithError: function(message)
214 {
215 var messageObject = JSON.parse(message);
216 var error = { message: "This is a stub connection, can't dispatch messag e.", code: InspectorBackendClass.DevToolsStubErrorCode, data: messageObject };
217 this.dispatchEventToListeners(InspectorBackendClass.Channel.Events.Messa geReceived, { id: messageObject.id, error: error });
218 },
219
220 __proto__: WebInspector.Object.prototype
221 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698