Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 goog.provide('__crWeb.mojoApi'); | |
| 6 | |
| 7 // Note: The API should stay in sync with the IDL definitions in | |
| 8 // third_party/WebKit/Source/core/mojo | |
| 9 | |
| 10 var Mojo = Mojo || {}; | |
|
Eugene But (OOO till 7-30)
2017/06/24 01:09:51
Does this global variable also exist on other plat
yzshen1
2017/06/26 20:10:00
Yes. This is exposed to the webpage as the Mojo pu
| |
| 11 | |
| 12 /** | |
| 13 * MojoResult {number}: Result codes for Mojo operations. | |
| 14 */ | |
| 15 Mojo.RESULT_OK = 0; | |
| 16 Mojo.RESULT_CANCELLED = 1; | |
| 17 Mojo.RESULT_UNKNOWN = 2; | |
| 18 Mojo.RESULT_INVALID_ARGUMENT = 3; | |
| 19 Mojo.RESULT_DEADLINE_EXCEEDED = 4; | |
| 20 Mojo.RESULT_NOT_FOUND = 5; | |
| 21 Mojo.RESULT_ALREADY_EXISTS = 6; | |
| 22 Mojo.RESULT_PERMISSION_DENIED = 7; | |
| 23 Mojo.RESULT_RESOURCE_EXHAUSTED = 8; | |
| 24 Mojo.RESULT_FAILED_PRECONDITION = 9; | |
| 25 Mojo.RESULT_ABORTED = 10; | |
| 26 Mojo.RESULT_OUT_OF_RANGE = 11; | |
| 27 Mojo.RESULT_UNIMPLEMENTED = 12; | |
| 28 Mojo.RESULT_INTERNAL = 13; | |
| 29 Mojo.RESULT_UNAVAILABLE = 14; | |
| 30 Mojo.RESULT_DATA_LOSS = 15; | |
| 31 Mojo.RESULT_BUSY = 16; | |
| 32 Mojo.RESULT_SHOULD_WAIT = 17; | |
| 33 | |
| 34 /** | |
| 35 * Creates a message pipe. | |
| 36 * | |
| 37 * @return {result: !MojoResult, handle0: !MojoHandle=, handle1: !MojoHandle=} | |
| 38 * Result code and (on success) the two message pipe handles. | |
| 39 */ | |
| 40 Mojo.createMessagePipe = function() { | |
| 41 var result = Mojo.internal.sendMessage({ | |
| 42 name: "Mojo.createMessagePipe", | |
| 43 args: {} | |
| 44 }); | |
| 45 if (result.result == Mojo.RESULT_OK) { | |
| 46 result.handle0 = new MojoHandle(result.handle0); | |
| 47 result.handle1 = new MojoHandle(result.handle1); | |
| 48 } | |
| 49 return result; | |
| 50 }; | |
| 51 | |
| 52 /** | |
| 53 * Binds to the specified Mojo interface. | |
| 54 * @param {string} interfaceName The interface name to connect. | |
| 55 * @param {!MojoHandle} requestHandle The interface request handle. | |
| 56 */ | |
| 57 Mojo.bindInterface = function(interfaceName, requestHandle) { | |
| 58 Mojo.internal.sendMessage({ | |
| 59 name: "Mojo.bindInterface", | |
| 60 args: { | |
| 61 interfaceName: interfaceName, | |
| 62 requestHandle: requestHandle.takeNativeHandle_() | |
| 63 } | |
| 64 }); | |
| 65 }; | |
| 66 | |
| 67 /** | |
| 68 * @constructor | |
| 69 * @param {?number=} nativeHandle An opaque number representing the underlying | |
| 70 * Mojo system resource. | |
| 71 */ | |
| 72 var MojoHandle = function(nativeHandle) { | |
| 73 if (nativeHandle === undefined) | |
| 74 nativeHandle = null; | |
| 75 | |
| 76 /** | |
| 77 * @type {number|null} | |
| 78 */ | |
| 79 this.nativeHandle_ = nativeHandle; | |
| 80 }; | |
| 81 | |
| 82 /** | |
| 83 * Takes the native handle value. This is not part of the public API. | |
| 84 * @return {?number} | |
| 85 */ | |
| 86 MojoHandle.prototype.takeNativeHandle_ = function() { | |
| 87 var nativeHandle = this.nativeHandle_; | |
| 88 this.nativeHandle_ = null; | |
| 89 return nativeHandle; | |
| 90 }; | |
| 91 | |
| 92 /** | |
| 93 * Closes the handle. | |
| 94 */ | |
| 95 MojoHandle.prototype.close = function() { | |
| 96 if (this.nativeHandle_ === null) | |
| 97 return; | |
| 98 | |
| 99 var nativeHandle = this.nativeHandle_; | |
| 100 this.nativeHandle_ = null; | |
| 101 Mojo.internal.sendMessage({ | |
| 102 name: "MojoHandle.close", | |
| 103 args: { | |
| 104 handle: nativeHandle | |
| 105 } | |
| 106 }); | |
| 107 }; | |
| 108 | |
| 109 /** | |
| 110 * Begins watching the handle for |signals| to be satisfied or unsatisfiable. | |
| 111 * | |
| 112 * @param {readable: boolean=, writable: boolean=, peerClosed: boolean=} signals | |
| 113 * The signals to watch. | |
| 114 * @param {!function(!MojoResult)} calback Called with a result any time | |
| 115 * the watched signals become satisfied or unsatisfiable. | |
| 116 * | |
| 117 * @return {!MojoWatcher} A MojoWatcher instance that could be used to cancel | |
| 118 * the watch. | |
| 119 */ | |
| 120 MojoHandle.prototype.watch = function(signals, callback) { | |
| 121 var HANDLE_SIGNAL_NONE = 0; | |
| 122 var HANDLE_SIGNAL_READABLE = 1; | |
| 123 var HANDLE_SIGNAL_WRITABLE = 2; | |
| 124 var HANDLE_SIGNAL_PEER_CLOSED = 4; | |
| 125 | |
| 126 var signalsValue = HANDLE_SIGNAL_NONE; | |
| 127 if (signals.readable) { | |
| 128 signalsValue |= HANDLE_SIGNAL_READABLE; | |
| 129 } | |
| 130 if (signals.writable) { | |
| 131 signalsValue |= HANDLE_SIGNAL_WRITABLE; | |
| 132 } | |
| 133 if (signalsValue.peerClosed) { | |
| 134 signalsValue |= HANDLE_SIGNAL_PEER_CLOSED; | |
| 135 } | |
| 136 | |
| 137 var watchId = Mojo.internal.sendMessage({ | |
| 138 name: "MojoHandle.watch", | |
| 139 args: { | |
| 140 handle: this.nativeHandle_, | |
| 141 signals: signalsValue, | |
| 142 callbackId: Mojo.internal.watchCallbacksHolder.getNextCallbackId() | |
| 143 } | |
| 144 }); | |
| 145 Mojo.internal.watchCallbacksHolder.addWatchCallback(watchId, callback); | |
| 146 | |
| 147 return new MojoWatcher(watchId); | |
| 148 }; | |
| 149 | |
| 150 /** | |
| 151 * Writes a message to the message pipe. | |
| 152 * | |
| 153 * @param {!ArrayBufferView} buffer The message data. May be empty. | |
| 154 * @param {!Array<!MojoHandle>} handles Any handles to attach. Handles are | |
| 155 * transferred and will no longer be valid. May be empty. | |
| 156 * @return {!MojoResult} Result code. | |
| 157 */ | |
| 158 MojoHandle.prototype.writeMessage = function(buffer, handles) { | |
| 159 var nativeHandles = handles.map(function(handle) { | |
| 160 return handle.takeNativeHandle_(); | |
| 161 }); | |
| 162 return Mojo.internal.sendMessage({ | |
| 163 name: "MojoHandle.writeMessage", | |
| 164 args: { | |
| 165 handle: this.nativeHandle_, | |
| 166 buffer: buffer, | |
| 167 handles: nativeHandles, | |
| 168 } | |
| 169 }); | |
| 170 }; | |
| 171 | |
| 172 /** | |
| 173 * Reads a message from the message pipe. | |
| 174 * | |
| 175 * @return {result: !MojoResult, | |
| 176 * buffer: !ArrayBufferView=, | |
| 177 * handles: !Array<!MojoHandle>=} | |
| 178 * Result code and (on success) the data and handles recevied. | |
| 179 */ | |
| 180 MojoHandle.prototype.readMessage = function() { | |
| 181 var result = Mojo.internal.sendMessage({ | |
| 182 name: "MojoHandle.readMessage", | |
| 183 args: { | |
| 184 handle: this.nativeHandle_ | |
| 185 } | |
| 186 }); | |
| 187 | |
| 188 if (result.result == Mojo.RESULT_OK) { | |
| 189 result.buffer = new Uint8Array(result.buffer).buffer; | |
| 190 result.handles = result.handles.map(function(handle) { | |
| 191 return new MojoHandle(handle); | |
| 192 }); | |
| 193 } | |
| 194 return result; | |
| 195 }; | |
| 196 | |
| 197 /** | |
| 198 * MojoWatcher identifies a watch on a MojoHandle and can be used to cancel the | |
| 199 * watch. | |
| 200 * | |
| 201 * @constructor | |
| 202 * @param {number} An opaque id representing the watch. | |
| 203 */ | |
| 204 var MojoWatcher = function(watchId) { | |
| 205 this.watchId_ = watchId; | |
| 206 }; | |
| 207 | |
| 208 /* | |
| 209 * Cancels a handle watch. | |
| 210 */ | |
| 211 MojoWatcher.prototype.cancel = function() { | |
| 212 Mojo.internal.sendMessage({ | |
| 213 name: "MojoWatcher.cancel", | |
| 214 args: { | |
| 215 watchId: this.watchId_ | |
| 216 } | |
| 217 }); | |
| 218 Mojo.internal.watchCallbacksHolder.removeWatchCallback(watchId); | |
| 219 }; | |
| 220 | |
| 221 // ----------------------------------------------------------------------------- | |
| 222 // Mojo API implementation details. It is not part of the public API. | |
| 223 | |
| 224 Mojo.internal = Mojo.internal || {}; | |
| 225 | |
| 226 /** | |
| 227 * Synchronously sends a message to Mojo backend. | |
| 228 * @param {!Object} message The message to send. | |
| 229 * @return {Object=} Response from Mojo backend. | |
| 230 */ | |
| 231 Mojo.internal.sendMessage = function(message) { | |
| 232 var response = window.prompt(__gCrWeb.common.JSONStringify(message)); | |
| 233 return response ? JSON.parse(response) : undefined; | |
| 234 }; | |
| 235 | |
| 236 /** | |
| 237 * Holds callbacks for all currently active watches. | |
| 238 */ | |
| 239 Mojo.internal.watchCallbacksHolder = (function() { | |
| 240 /** | |
| 241 * Next callback id to be used for watch. | |
| 242 * @type{number} | |
| 243 */ | |
| 244 var nextCallbackId = 0; | |
| 245 | |
| 246 /** | |
| 247 * Map where keys are callbacks ids and values are callbacks. | |
| 248 * @type {!Map<number, !function(!MojoResult)>} | |
| 249 */ | |
| 250 var callbacks = new Map(); | |
| 251 | |
| 252 /** | |
| 253 * Map where keys are watch ids and values are callback ids. | |
| 254 * @type {!Map<number, number>} | |
| 255 */ | |
| 256 var callbackIds = new Map(); | |
| 257 | |
| 258 /** | |
| 259 * Calls watch callback. | |
| 260 * | |
| 261 * @param {number} callbackId Callback id previously returned from | |
| 262 {@code getNextCallbackId}. | |
| 263 * @param {!MojoResult} mojoResult The result code to call the callback with. | |
| 264 */ | |
| 265 var callCallback = function(callbackId, mojoResult) { | |
| 266 var callback = callbacks.get(callbackId); | |
| 267 | |
| 268 // Signalling the watch is asynchronous operation and this function may be | |
| 269 // called for already removed watch. | |
| 270 if (callback) | |
| 271 callback(mojoResult); | |
| 272 }; | |
| 273 | |
| 274 /** | |
| 275 * Returns next callback id to be used for watch (idempotent). | |
| 276 * | |
| 277 * @return {number} callback id. | |
| 278 */ | |
| 279 var getNextCallbackId = function() { | |
| 280 return nextCallbackId; | |
| 281 }; | |
| 282 | |
| 283 /** | |
| 284 * Adds callback which must be executed when the watch fires. | |
| 285 * | |
| 286 * @param {number} watchId The value returned from "MojoHandle.watch" Mojo | |
| 287 * backend. | |
| 288 * @param {!function(!MojoResult)} callback The callback which should be | |
| 289 * executed when the watch fires. | |
| 290 */ | |
| 291 var addWatchCallback = function(watchId, callback) { | |
| 292 callbackIds.set(watchId, nextCallbackId); | |
| 293 callbacks.set(nextCallbackId, callback); | |
| 294 ++nextCallbackId; | |
| 295 }; | |
| 296 | |
| 297 /** | |
| 298 * Removes callback which should no longer be executed. | |
| 299 * | |
| 300 * @param {!number} watchId The id to remove callback for. | |
| 301 */ | |
| 302 var removeWatchCallback = function (watchId) { | |
| 303 callbacks.delete(callbackIds.get(watchId)); | |
| 304 callbackIds.delete(watchId); | |
| 305 }; | |
| 306 | |
| 307 return { | |
| 308 callCallback: callCallback, | |
| 309 getNextCallbackId: getNextCallbackId, | |
| 310 addWatchCallback: addWatchCallback, | |
| 311 removeWatchCallback: removeWatchCallback | |
| 312 }; | |
| 313 })(); | |
| 314 | |
| OLD | NEW |