| OLD | NEW |
| (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 {InspectorBackendClass.Connection} | |
| 8 */ | |
| 9 WebInspector.MainConnection = function() | |
| 10 { | |
| 11 InspectorBackendClass.Connection.call(this); | |
| 12 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event
s.DispatchMessage, this._dispatchMessage, this); | |
| 13 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event
s.DispatchMessageChunk, this._dispatchMessageChunk, this); | |
| 14 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event
s.EvaluateForTestInFrontend, this._evaluateForTestInFrontend, this); | |
| 15 } | |
| 16 | |
| 17 WebInspector.MainConnection.prototype = { | |
| 18 /** | |
| 19 * @override | |
| 20 * @param {!Object} messageObject | |
| 21 */ | |
| 22 sendMessage: function(messageObject) | |
| 23 { | |
| 24 var message = JSON.stringify(messageObject); | |
| 25 InspectorFrontendHost.sendMessageToBackend(message); | |
| 26 }, | |
| 27 | |
| 28 /** | |
| 29 * @param {!WebInspector.Event} event | |
| 30 */ | |
| 31 _dispatchMessage: function(event) | |
| 32 { | |
| 33 this.dispatch(/** @type {string} */ (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.dispatch(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 this.deprecatedRunAfterPendingDispatches(invokeMethod); | |
| 80 }, | |
| 81 | |
| 82 /** | |
| 83 * @override | |
| 84 */ | |
| 85 forceClose: function() | |
| 86 { | |
| 87 InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAP
I.Events.DispatchMessage, this._dispatchMessage, this); | |
| 88 InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAP
I.Events.DispatchMessageChunk, this._dispatchMessageChunk, this); | |
| 89 InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAP
I.Events.EvaluateForTestInFrontend, this._evaluateForTestInFrontend, this); | |
| 90 }, | |
| 91 | |
| 92 __proto__: InspectorBackendClass.Connection.prototype | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * @constructor | |
| 97 * @extends {InspectorBackendClass.Connection} | |
| 98 * @param {string} url | |
| 99 * @param {function(!InspectorBackendClass.Connection)} onConnectionReady | |
| 100 */ | |
| 101 WebInspector.WebSocketConnection = function(url, onConnectionReady) | |
| 102 { | |
| 103 InspectorBackendClass.Connection.call(this); | |
| 104 this._socket = new WebSocket(url); | |
| 105 this._socket.onmessage = this._onMessage.bind(this); | |
| 106 this._socket.onerror = this._onError.bind(this); | |
| 107 this._socket.onopen = onConnectionReady.bind(null, this); | |
| 108 this._socket.onclose = this.connectionClosed.bind(this, "websocket_closed"); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * @param {string} url | |
| 113 * @return {!Promise<!InspectorBackendClass.Connection>} | |
| 114 */ | |
| 115 WebInspector.WebSocketConnection.Create = function(url) | |
| 116 { | |
| 117 var fulfill; | |
| 118 var result = new Promise(resolve => fulfill = resolve); | |
| 119 new WebInspector.WebSocketConnection(url, fulfill); | |
| 120 return result; | |
| 121 } | |
| 122 | |
| 123 WebInspector.WebSocketConnection.prototype = { | |
| 124 | |
| 125 /** | |
| 126 * @param {!MessageEvent} message | |
| 127 */ | |
| 128 _onMessage: function(message) | |
| 129 { | |
| 130 var data = /** @type {string} */ (message.data); | |
| 131 this.dispatch(data); | |
| 132 }, | |
| 133 | |
| 134 /** | |
| 135 * @param {!Event} error | |
| 136 */ | |
| 137 _onError: function(error) | |
| 138 { | |
| 139 console.error(error); | |
| 140 }, | |
| 141 | |
| 142 /** | |
| 143 * @override | |
| 144 */ | |
| 145 forceClose: function() | |
| 146 { | |
| 147 this._socket.close(); | |
| 148 }, | |
| 149 | |
| 150 /** | |
| 151 * @override | |
| 152 * @param {!Object} messageObject | |
| 153 */ | |
| 154 sendMessage: function(messageObject) | |
| 155 { | |
| 156 var message = JSON.stringify(messageObject); | |
| 157 this._socket.send(message); | |
| 158 }, | |
| 159 | |
| 160 __proto__: InspectorBackendClass.Connection.prototype | |
| 161 } | |
| 162 | |
| 163 /** | |
| 164 * @constructor | |
| 165 * @extends {InspectorBackendClass.Connection} | |
| 166 */ | |
| 167 WebInspector.StubConnection = function() | |
| 168 { | |
| 169 InspectorBackendClass.Connection.call(this); | |
| 170 } | |
| 171 | |
| 172 WebInspector.StubConnection.prototype = { | |
| 173 /** | |
| 174 * @override | |
| 175 * @param {!Object} messageObject | |
| 176 */ | |
| 177 sendMessage: function(messageObject) | |
| 178 { | |
| 179 setTimeout(this._respondWithError.bind(this, messageObject), 0); | |
| 180 }, | |
| 181 | |
| 182 /** | |
| 183 * @param {!Object} messageObject | |
| 184 */ | |
| 185 _respondWithError: function(messageObject) | |
| 186 { | |
| 187 var error = { message: "This is a stub connection, can't dispatch messag
e.", code: InspectorBackendClass.DevToolsStubErrorCode, data: messageObject }; | |
| 188 this.dispatch({ id: messageObject.id, error: error }); | |
| 189 }, | |
| 190 | |
| 191 __proto__: InspectorBackendClass.Connection.prototype | |
| 192 } | |
| 193 | |
| 194 | |
| 195 /** | |
| 196 * @constructor | |
| 197 * @param {function(string)} dispatchCallback | |
| 198 * @param {function()} yieldCallback | |
| 199 */ | |
| 200 WebInspector.RawProtocolConnection = function(dispatchCallback, yieldCallback) | |
| 201 { | |
| 202 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event
s.DispatchMessage, this._dispatchMessage, this); | |
| 203 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Event
s.DispatchMessageChunk, this._dispatchMessageChunk, this); | |
| 204 this._dispatchCallback = dispatchCallback; | |
| 205 this._yieldCallback = yieldCallback; | |
| 206 this._isClosed = false; | |
| 207 } | |
| 208 | |
| 209 WebInspector.RawProtocolConnection.prototype = { | |
| 210 /** | |
| 211 * @param {string} message | |
| 212 */ | |
| 213 send: function(message) | |
| 214 { | |
| 215 if (this._isClosed) | |
| 216 return; | |
| 217 InspectorFrontendHost.sendMessageToBackend(message); | |
| 218 }, | |
| 219 | |
| 220 /** | |
| 221 * @param {!WebInspector.Event} event | |
| 222 */ | |
| 223 _dispatchMessage: function(event) | |
| 224 { | |
| 225 this._dispatchCallback(/** @type {string} */ (event.data)); | |
| 226 }, | |
| 227 | |
| 228 /** | |
| 229 * @param {!WebInspector.Event} event | |
| 230 */ | |
| 231 _dispatchMessageChunk: function(event) | |
| 232 { | |
| 233 var messageChunk = /** @type {string} */ (event.data["messageChunk"]); | |
| 234 var messageSize = /** @type {number} */ (event.data["messageSize"]); | |
| 235 if (messageSize) { | |
| 236 this._messageBuffer = ""; | |
| 237 this._messageSize = messageSize; | |
| 238 } | |
| 239 this._messageBuffer += messageChunk; | |
| 240 if (this._messageBuffer.length === this._messageSize) { | |
| 241 this._dispatchCallback(this._messageBuffer); | |
| 242 this._messageBuffer = ""; | |
| 243 this._messageSize = 0; | |
| 244 } | |
| 245 }, | |
| 246 | |
| 247 yieldConnection: function() | |
| 248 { | |
| 249 InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAP
I.Events.DispatchMessage, this._dispatchMessage, this); | |
| 250 InspectorFrontendHost.events.removeEventListener(InspectorFrontendHostAP
I.Events.DispatchMessageChunk, this._dispatchMessageChunk, this); | |
| 251 this._isClosed = true; | |
| 252 delete this._dispatchCallback; | |
| 253 this._yieldCallback(); | |
| 254 } | |
| 255 } | |
| OLD | NEW |