OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview JavaScript shim for the liblouis Native Client wrapper. | 6 * @fileoverview JavaScript shim for the liblouis Native Client wrapper. |
7 */ | 7 */ |
8 | 8 |
9 goog.provide('cvox.LibLouis'); | 9 goog.provide('cvox.LibLouis'); |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 /** | 44 /** |
45 * Next message ID to be used. Incremented with each sent message. | 45 * Next message ID to be used. Incremented with each sent message. |
46 * @private {number} | 46 * @private {number} |
47 */ | 47 */ |
48 this.nextMessageId_ = 1; | 48 this.nextMessageId_ = 1; |
49 }; | 49 }; |
50 | 50 |
51 | 51 |
52 /** | 52 /** |
| 53 * Set to {@code true} to enable debug logging of RPC messages. |
| 54 * @type {boolean} |
| 55 */ |
| 56 cvox.LibLouis.DEBUG = false; |
| 57 |
| 58 |
| 59 /** |
53 * Attaches the Native Client wrapper to the DOM as a child of the provided | 60 * Attaches the Native Client wrapper to the DOM as a child of the provided |
54 * element, assumed to already be in the document. | 61 * element, assumed to already be in the document. |
55 * @param {!Element} elem Desired parent element of the instance. | 62 * @param {!Element} elem Desired parent element of the instance. |
56 */ | 63 */ |
57 cvox.LibLouis.prototype.attachToElement = function(elem) { | 64 cvox.LibLouis.prototype.attachToElement = function(elem) { |
58 if (this.isAttached()) { | 65 if (this.isAttached()) { |
59 throw Error('Instance already attached'); | 66 throw Error('Instance already attached'); |
60 } | 67 } |
61 | 68 |
62 var embed = document.createElement('embed'); | 69 var embed = document.createElement('embed'); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 */ | 145 */ |
139 cvox.LibLouis.prototype.rpc_ = | 146 cvox.LibLouis.prototype.rpc_ = |
140 function(command, message, callback) { | 147 function(command, message, callback) { |
141 if (!this.isAttached()) { | 148 if (!this.isAttached()) { |
142 throw Error('Cannot send RPC: liblouis instance not loaded'); | 149 throw Error('Cannot send RPC: liblouis instance not loaded'); |
143 } | 150 } |
144 var messageId = '' + this.nextMessageId_++; | 151 var messageId = '' + this.nextMessageId_++; |
145 message['message_id'] = messageId; | 152 message['message_id'] = messageId; |
146 message['command'] = command; | 153 message['command'] = command; |
147 var json = JSON.stringify(message); | 154 var json = JSON.stringify(message); |
148 if (goog.DEBUG) { | 155 if (cvox.LibLouis.DEBUG) { |
149 window.console.debug('RPC -> ' + json); | 156 window.console.debug('RPC -> ' + json); |
150 } | 157 } |
151 this.embedElement_.postMessage(json); | 158 this.embedElement_.postMessage(json); |
152 this.pendingRpcCallbacks_[messageId] = callback; | 159 this.pendingRpcCallbacks_[messageId] = callback; |
153 }; | 160 }; |
154 | 161 |
155 | 162 |
156 /** | 163 /** |
157 * Invoked when the Native Client instance successfully loads. | 164 * Invoked when the Native Client instance successfully loads. |
158 * @param {Event} e Event dispatched after loading. | 165 * @param {Event} e Event dispatched after loading. |
(...skipping 14 matching lines...) Expand all Loading... |
173 this.detach(); | 180 this.detach(); |
174 }; | 181 }; |
175 | 182 |
176 | 183 |
177 /** | 184 /** |
178 * Invoked when the Native Client instance posts a message. | 185 * Invoked when the Native Client instance posts a message. |
179 * @param {Event} e Event dispatched after the message was posted. | 186 * @param {Event} e Event dispatched after the message was posted. |
180 * @private | 187 * @private |
181 */ | 188 */ |
182 cvox.LibLouis.prototype.onInstanceMessage_ = function(e) { | 189 cvox.LibLouis.prototype.onInstanceMessage_ = function(e) { |
183 if (goog.DEBUG) { | 190 if (cvox.LibLouis.DEBUG) { |
184 window.console.debug('RPC <- ' + e.data); | 191 window.console.debug('RPC <- ' + e.data); |
185 } | 192 } |
186 var message = /** @type {!Object} */ (JSON.parse(e.data)); | 193 var message = /** @type {!Object} */ (JSON.parse(e.data)); |
187 var messageId = message['in_reply_to']; | 194 var messageId = message['in_reply_to']; |
188 if (!goog.isDef(messageId)) { | 195 if (!goog.isDef(messageId)) { |
189 window.console.warn('liblouis Native Client module sent message with no ID', | 196 window.console.warn('liblouis Native Client module sent message with no ID', |
190 message); | 197 message); |
191 return; | 198 return; |
192 } | 199 } |
193 if (goog.isDef(message['error'])) { | 200 if (goog.isDef(message['error'])) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 */ | 325 */ |
319 cvox.LibLouis.Translator.encodeHexString_ = function(arrayBuffer) { | 326 cvox.LibLouis.Translator.encodeHexString_ = function(arrayBuffer) { |
320 var array = new Uint8Array(arrayBuffer); | 327 var array = new Uint8Array(arrayBuffer); |
321 var hex = ''; | 328 var hex = ''; |
322 for (var i = 0; i < array.length; i++) { | 329 for (var i = 0; i < array.length; i++) { |
323 var b = array[i]; | 330 var b = array[i]; |
324 hex += (b < 0x10 ? '0' : '') + b.toString(16); | 331 hex += (b < 0x10 ? '0' : '') + b.toString(16); |
325 } | 332 } |
326 return hex; | 333 return hex; |
327 }; | 334 }; |
OLD | NEW |