| 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 cr.define('hotword', function() { | 5 cr.define('hotword', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Class used to manage the state of the NaCl recognizer plugin. Handles all | 9 * Class used to manage the state of the NaCl recognizer plugin. Handles all |
| 10 * control of the NaCl plugin, including creation, start, stop, trigger, and | 10 * control of the NaCl plugin, including creation, start, stop, trigger, and |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 if (this.recognizerState_ == ManagerState_.UNINITIALIZED || | 221 if (this.recognizerState_ == ManagerState_.UNINITIALIZED || |
| 222 this.recognizerState_ == ManagerState_.ERROR || | 222 this.recognizerState_ == ManagerState_.ERROR || |
| 223 this.recognizerState_ == ManagerState_.SHUTDOWN || | 223 this.recognizerState_ == ManagerState_.SHUTDOWN || |
| 224 this.recognizerState_ == ManagerState_.LOADING) { | 224 this.recognizerState_ == ManagerState_.LOADING) { |
| 225 return; | 225 return; |
| 226 } | 226 } |
| 227 this.sendDataToPlugin_(hotword.constants.NaClPlugin.FINISH_SPEAKER_MODEL); | 227 this.sendDataToPlugin_(hotword.constants.NaClPlugin.FINISH_SPEAKER_MODEL); |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 /** | 230 /** |
| 231 * Checks whether the file at the given path exists. | 231 * Checks whether the file at the given path exists. |
| 232 * @param {!string} path Path to a file. Can be any valid URL. | 232 * @param {!string} path Path to a file. Can be any valid URL. |
| 233 * @return {boolean} True if the patch exists. | 233 * @return {boolean} True if the patch exists. |
| 234 * @private | 234 * @private |
| 235 */ | 235 */ |
| 236 NaClManager.prototype.fileExists_ = function(path) { | 236 NaClManager.prototype.fileExists_ = function(path) { |
| 237 var xhr = new XMLHttpRequest(); | 237 var xhr = new XMLHttpRequest(); |
| 238 xhr.open('HEAD', path, false); | 238 xhr.open('HEAD', path, false); |
| 239 try { | 239 try { |
| 240 xhr.send(); | 240 xhr.send(); |
| 241 } catch (err) { | 241 } catch (err) { |
| 242 return false; | 242 return false; |
| 243 } | 243 } |
| 244 if (xhr.readyState != xhr.DONE || xhr.status != 200) { | 244 if (xhr.readyState != xhr.DONE || xhr.status != 200) { |
| 245 return false; | 245 return false; |
| 246 } | 246 } |
| 247 return true; | 247 return true; |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 /** | 250 /** |
| 251 * Creates and returns a list of possible languages to check for hotword | 251 * Creates and returns a list of possible languages to check for hotword |
| 252 * support. | 252 * support. |
| 253 * @return {!Array<string>} Array of languages. | 253 * @return {!Array<string>} Array of languages. |
| 254 * @private | 254 * @private |
| 255 */ | 255 */ |
| 256 NaClManager.prototype.getPossibleLanguages_ = function() { | 256 NaClManager.prototype.getPossibleLanguages_ = function() { |
| 257 // Create array used to search first for language-country, if not found then | 257 // Create array used to search first for language-country, if not found then |
| 258 // search for language, if not found then no language (empty string). | 258 // search for language, if not found then no language (empty string). |
| 259 // For example, search for 'en-us', then 'en', then ''. | 259 // For example, search for 'en-us', then 'en', then ''. |
| 260 var langs = new Array(); | 260 var langs = new Array(); |
| 261 if (hotword.constants.UI_LANGUAGE) { | 261 if (hotword.constants.UI_LANGUAGE) { |
| 262 // Chrome webstore doesn't support uppercase path: crbug.com/353407 | 262 // Chrome webstore doesn't support uppercase path: crbug.com/353407 |
| 263 var language = hotword.constants.UI_LANGUAGE.toLowerCase(); | 263 var language = hotword.constants.UI_LANGUAGE.toLowerCase(); |
| 264 langs.push(language); // Example: 'en-us'. | 264 langs.push(language); // Example: 'en-us'. |
| 265 // Remove country to add just the language to array. | 265 // Remove country to add just the language to array. |
| 266 var hyphen = language.lastIndexOf('-'); | 266 var hyphen = language.lastIndexOf('-'); |
| 267 if (hyphen >= 0) { | 267 if (hyphen >= 0) { |
| 268 langs.push(language.substr(0, hyphen)); // Example: 'en'. | 268 langs.push(language.substr(0, hyphen)); // Example: 'en'. |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 langs.push(''); | 271 langs.push(''); |
| 272 return langs; | 272 return langs; |
| 273 }; | 273 }; |
| 274 | 274 |
| 275 /** | 275 /** |
| 276 * Creates a NaCl plugin object and attaches it to the page. | 276 * Creates a NaCl plugin object and attaches it to the page. |
| 277 * @param {!string} src Location of the plugin. | 277 * @param {!string} src Location of the plugin. |
| 278 * @return {!HTMLEmbedElement} NaCl plugin DOM object. | 278 * @return {!HTMLEmbedElement} NaCl plugin DOM object. |
| 279 * @private | 279 * @private |
| 280 */ | 280 */ |
| 281 NaClManager.prototype.createPlugin_ = function(src) { | 281 NaClManager.prototype.createPlugin_ = function(src) { |
| 282 var plugin = | 282 var plugin = |
| 283 /** @type {HTMLEmbedElement} */ (document.createElement('embed')); | 283 /** @type {HTMLEmbedElement} */ (document.createElement('embed')); |
| 284 plugin.src = src; | 284 plugin.src = src; |
| 285 plugin.type = 'application/x-nacl'; | 285 plugin.type = 'application/x-nacl'; |
| 286 document.body.appendChild(plugin); | 286 document.body.appendChild(plugin); |
| 287 return plugin; | 287 return plugin; |
| 288 }; | 288 }; |
| 289 | 289 |
| 290 /** | 290 /** |
| 291 * Initializes the NaCl manager. | 291 * Initializes the NaCl manager. |
| 292 * @param {!string} naclArch Either 'arm', 'x86-32' or 'x86-64'. | 292 * @param {!string} naclArch Either 'arm', 'x86-32' or 'x86-64'. |
| 293 * @param {!MediaStream} stream A stream containing an audio source track. | 293 * @param {!MediaStream} stream A stream containing an audio source track. |
| 294 * @return {boolean} True if the successful. | 294 * @return {boolean} True if the successful. |
| 295 */ | 295 */ |
| 296 NaClManager.prototype.initialize = function(naclArch, stream) { | 296 NaClManager.prototype.initialize = function(naclArch, stream) { |
| 297 assert( | 297 assert( |
| 298 this.recognizerState_ == ManagerState_.UNINITIALIZED, | 298 this.recognizerState_ == ManagerState_.UNINITIALIZED, |
| 299 'Recognizer not in uninitialized state. State: ' + | 299 'Recognizer not in uninitialized state. State: ' + |
| 300 this.recognizerState_); | 300 this.recognizerState_); |
| 301 assert(this.plugin_ == null); | 301 assert(this.plugin_ == null); |
| 302 var langs = this.getPossibleLanguages_(); | 302 var langs = this.getPossibleLanguages_(); |
| 303 var i, j; | 303 var i, j; |
| 304 // For country-lang variations. For example, when combined with path it will | 304 // For country-lang variations. For example, when combined with path it will |
| 305 // attempt to find: '/x86-32_en-gb/', else '/x86-32_en/', else '/x86-32_/'. | 305 // attempt to find: '/x86-32_en-gb/', else '/x86-32_en/', else '/x86-32_/'. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 * Waits, with a timeout, for a message to be received from the plugin. If the | 370 * Waits, with a timeout, for a message to be received from the plugin. If the |
| 371 * message is not seen within the timeout, dispatch an 'error' event and go | 371 * message is not seen within the timeout, dispatch an 'error' event and go |
| 372 * into | 372 * into |
| 373 * the ERROR state. | 373 * the ERROR state. |
| 374 * @param {number} timeout Timeout, in milliseconds, to wait for the message. | 374 * @param {number} timeout Timeout, in milliseconds, to wait for the message. |
| 375 * @param {!string} message Message to wait for. | 375 * @param {!string} message Message to wait for. |
| 376 * @private | 376 * @private |
| 377 */ | 377 */ |
| 378 NaClManager.prototype.waitForMessage_ = function(timeout, message) { | 378 NaClManager.prototype.waitForMessage_ = function(timeout, message) { |
| 379 assert( | 379 assert( |
| 380 this.expectingMessage_ == null, 'Cannot wait for message: ' + message + | 380 this.expectingMessage_ == null, |
| 381 'Cannot wait for message: ' + message + |
| 381 ', already waiting for message ' + this.expectingMessage_); | 382 ', already waiting for message ' + this.expectingMessage_); |
| 382 this.setTimeout_(function() { | 383 this.setTimeout_(function() { |
| 383 this.recognizerState_ = ManagerState_.ERROR; | 384 this.recognizerState_ = ManagerState_.ERROR; |
| 384 this.handleError_(Error_.TIMEOUT); | 385 this.handleError_(Error_.TIMEOUT); |
| 385 switch (this.expectingMessage_) { | 386 switch (this.expectingMessage_) { |
| 386 case hotword.constants.NaClPlugin.REQUEST_MODEL: | 387 case hotword.constants.NaClPlugin.REQUEST_MODEL: |
| 387 var metricValue = UmaNaClMessageTimeout_.REQUEST_MODEL; | 388 var metricValue = UmaNaClMessageTimeout_.REQUEST_MODEL; |
| 388 break; | 389 break; |
| 389 case hotword.constants.NaClPlugin.MODEL_LOADED: | 390 case hotword.constants.NaClPlugin.MODEL_LOADED: |
| 390 var metricValue = UmaNaClMessageTimeout_.MODEL_LOADED; | 391 var metricValue = UmaNaClMessageTimeout_.MODEL_LOADED; |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 case hotword.constants.NaClPlugin.SPEAKER_MODEL_SAVED: | 603 case hotword.constants.NaClPlugin.SPEAKER_MODEL_SAVED: |
| 603 this.handleSpeakerModelSaved_(); | 604 this.handleSpeakerModelSaved_(); |
| 604 break; | 605 break; |
| 605 } | 606 } |
| 606 } | 607 } |
| 607 }; | 608 }; |
| 608 | 609 |
| 609 return {NaClManager: NaClManager}; | 610 return {NaClManager: NaClManager}; |
| 610 | 611 |
| 611 }); | 612 }); |
| OLD | NEW |