| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 LOADING: 1, | 70 LOADING: 1, |
| 71 STOPPING: 2, | 71 STOPPING: 2, |
| 72 STOPPED: 3, | 72 STOPPED: 3, |
| 73 STARTING: 4, | 73 STARTING: 4, |
| 74 RUNNING: 5, | 74 RUNNING: 5, |
| 75 ERROR: 6, | 75 ERROR: 6, |
| 76 SHUTDOWN: 7, | 76 SHUTDOWN: 7, |
| 77 }; | 77 }; |
| 78 var ManagerState_ = NaClManager.ManagerState_; | 78 var ManagerState_ = NaClManager.ManagerState_; |
| 79 var Error_ = hotword.constants.Error; | 79 var Error_ = hotword.constants.Error; |
| 80 var UmaNaClMessageTimeout_ = hotword.constants.UmaNaClMessageTimeout; |
| 81 var UmaNaClPluginLoadResult_ = hotword.constants.UmaNaClPluginLoadResult; |
| 80 | 82 |
| 81 NaClManager.prototype.__proto__ = cr.EventTarget.prototype; | 83 NaClManager.prototype.__proto__ = cr.EventTarget.prototype; |
| 82 | 84 |
| 83 /** | 85 /** |
| 84 * Called when an error occurs. Dispatches an event. | 86 * Called when an error occurs. Dispatches an event. |
| 85 * @param {!hotword.constants.Error} error | 87 * @param {!hotword.constants.Error} error |
| 86 * @private | 88 * @private |
| 87 */ | 89 */ |
| 88 NaClManager.prototype.handleError_ = function(error) { | 90 NaClManager.prototype.handleError_ = function(error) { |
| 89 var event = new Event(hotword.constants.Event.ERROR); | 91 var event = new Event(hotword.constants.Event.ERROR); |
| 90 event.data = error; | 92 event.data = error; |
| 91 this.dispatchEvent(event); | 93 this.dispatchEvent(event); |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 /** | 96 /** |
| 97 * Record the result of loading the NaCl plugin to UMA. |
| 98 * @param {!hotword.constants.UmaNaClPluginLoadResult} error |
| 99 * @private |
| 100 */ |
| 101 NaClManager.prototype.logPluginLoadResult_ = function(error) { |
| 102 hotword.metrics.recordEnum( |
| 103 hotword.constants.UmaMetrics.NACL_PLUGIN_LOAD_RESULT, |
| 104 error, |
| 105 UmaNaClPluginLoadResult_.MAX); |
| 106 }; |
| 107 |
| 108 /** |
| 95 * @return {boolean} True if the recognizer is in a running state. | 109 * @return {boolean} True if the recognizer is in a running state. |
| 96 */ | 110 */ |
| 97 NaClManager.prototype.isRunning = function() { | 111 NaClManager.prototype.isRunning = function() { |
| 98 return this.recognizerState_ == ManagerState_.RUNNING; | 112 return this.recognizerState_ == ManagerState_.RUNNING; |
| 99 }; | 113 }; |
| 100 | 114 |
| 101 /** | 115 /** |
| 102 * Set a timeout. Only allow one timeout to exist at any given time. | 116 * Set a timeout. Only allow one timeout to exist at any given time. |
| 103 * @param {!function()} func | 117 * @param {!function()} func |
| 104 * @param {number} timeout | 118 * @param {number} timeout |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 } | 257 } |
| 244 this.modelUrl_ = chrome.extension.getURL(dataSrc); | 258 this.modelUrl_ = chrome.extension.getURL(dataSrc); |
| 245 this.stream_ = stream; | 259 this.stream_ = stream; |
| 246 this.recognizerState_ = ManagerState_.LOADING; | 260 this.recognizerState_ = ManagerState_.LOADING; |
| 247 | 261 |
| 248 plugin.addEventListener('message', | 262 plugin.addEventListener('message', |
| 249 this.handlePluginMessage_.bind(this), | 263 this.handlePluginMessage_.bind(this), |
| 250 false); | 264 false); |
| 251 | 265 |
| 252 plugin.addEventListener('crash', | 266 plugin.addEventListener('crash', |
| 253 this.handleError_.bind(this, Error_.NACL_CRASH), | 267 function() { |
| 268 this.handleError_(Error_.NACL_CRASH); |
| 269 this.logPluginLoadResult_( |
| 270 UmaNaClPluginLoadResult_.CRASH); |
| 271 }.bind(this), |
| 254 false); | 272 false); |
| 255 return true; | 273 return true; |
| 256 } | 274 } |
| 257 this.recognizerState_ = ManagerState_.ERROR; | 275 this.recognizerState_ = ManagerState_.ERROR; |
| 276 this.logPluginLoadResult_(UmaNaClPluginLoadResult_.NO_MODULE_FOUND); |
| 258 return false; | 277 return false; |
| 259 }; | 278 }; |
| 260 | 279 |
| 261 /** | 280 /** |
| 262 * Shuts down the NaCl plugin and frees all resources. | 281 * Shuts down the NaCl plugin and frees all resources. |
| 263 */ | 282 */ |
| 264 NaClManager.prototype.shutdown = function() { | 283 NaClManager.prototype.shutdown = function() { |
| 265 if (this.plugin_ != null) { | 284 if (this.plugin_ != null) { |
| 266 document.body.removeChild(this.plugin_); | 285 document.body.removeChild(this.plugin_); |
| 267 this.plugin_ = null; | 286 this.plugin_ = null; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 290 * @param {!string} message Message to wait for. | 309 * @param {!string} message Message to wait for. |
| 291 * @private | 310 * @private |
| 292 */ | 311 */ |
| 293 NaClManager.prototype.waitForMessage_ = function(timeout, message) { | 312 NaClManager.prototype.waitForMessage_ = function(timeout, message) { |
| 294 assert(this.expectingMessage_ == null, | 313 assert(this.expectingMessage_ == null, |
| 295 'Already waiting for message ' + this.expectingMessage_); | 314 'Already waiting for message ' + this.expectingMessage_); |
| 296 this.setTimeout_( | 315 this.setTimeout_( |
| 297 function() { | 316 function() { |
| 298 this.recognizerState_ = ManagerState_.ERROR; | 317 this.recognizerState_ = ManagerState_.ERROR; |
| 299 this.handleError_(Error_.TIMEOUT); | 318 this.handleError_(Error_.TIMEOUT); |
| 319 switch (this.expectingMessage_) { |
| 320 case hotword.constants.NaClPlugin.REQUEST_MODEL: |
| 321 var metricValue = UmaNaClMessageTimeout_.REQUEST_MODEL; |
| 322 break; |
| 323 case hotword.constants.NaClPlugin.MODEL_LOADED: |
| 324 var metricValue = UmaNaClMessageTimeout_.MODEL_LOADED; |
| 325 break; |
| 326 case hotword.constants.NaClPlugin.READY_FOR_AUDIO: |
| 327 var metricValue = UmaNaClMessageTimeout_.READY_FOR_AUDIO; |
| 328 break; |
| 329 case hotword.constants.NaClPlugin.STOPPED: |
| 330 var metricValue = UmaNaClMessageTimeout_.STOPPED; |
| 331 break; |
| 332 case hotword.constants.NaClPlugin.HOTWORD_DETECTED: |
| 333 var metricValue = UmaNaClMessageTimeout_.HOTWORD_DETECTED; |
| 334 break; |
| 335 case hotword.constants.NaClPlugin.MS_CONFIGURED: |
| 336 var metricValue = UmaNaClMessageTimeout_.MS_CONFIGURED; |
| 337 break; |
| 338 } |
| 339 hotword.metrics.recordEnum( |
| 340 hotword.constants.UmaMetrics.NACL_MESSAGE_TIMEOUT, |
| 341 metricValue, |
| 342 UmaNaClMessageTimeout_.MAX); |
| 300 }.bind(this), timeout); | 343 }.bind(this), timeout); |
| 301 this.expectingMessage_ = message; | 344 this.expectingMessage_ = message; |
| 302 }; | 345 }; |
| 303 | 346 |
| 304 /** | 347 /** |
| 305 * Called when a message is received from the plugin. If we're waiting for that | 348 * Called when a message is received from the plugin. If we're waiting for that |
| 306 * message, cancel the pending timeout. | 349 * message, cancel the pending timeout. |
| 307 * @param {string} message Message received. | 350 * @param {string} message Message received. |
| 308 * @private | 351 * @private |
| 309 */ | 352 */ |
| 310 NaClManager.prototype.receivedMessage_ = function(message) { | 353 NaClManager.prototype.receivedMessage_ = function(message) { |
| 311 if (message == this.expectingMessage_) { | 354 if (message == this.expectingMessage_) { |
| 312 this.clearTimeout_(); | 355 this.clearTimeout_(); |
| 313 this.expectingMessage_ = null; | 356 this.expectingMessage_ = null; |
| 314 } | 357 } |
| 315 }; | 358 }; |
| 316 | 359 |
| 317 /** | 360 /** |
| 318 * Handle a REQUEST_MODEL message from the plugin. | 361 * Handle a REQUEST_MODEL message from the plugin. |
| 319 * The plugin sends this message immediately after starting. | 362 * The plugin sends this message immediately after starting. |
| 320 * @private | 363 * @private |
| 321 */ | 364 */ |
| 322 NaClManager.prototype.handleRequestModel_ = function() { | 365 NaClManager.prototype.handleRequestModel_ = function() { |
| 323 if (this.recognizerState_ != ManagerState_.LOADING) { | 366 if (this.recognizerState_ != ManagerState_.LOADING) { |
| 324 return; | 367 return; |
| 325 } | 368 } |
| 369 this.logPluginLoadResult_(UmaNaClPluginLoadResult_.SUCCESS); |
| 326 this.sendDataToPlugin_( | 370 this.sendDataToPlugin_( |
| 327 hotword.constants.NaClPlugin.MODEL_PREFIX + this.modelUrl_); | 371 hotword.constants.NaClPlugin.MODEL_PREFIX + this.modelUrl_); |
| 328 this.waitForMessage_(hotword.constants.TimeoutMs.LONG, | 372 this.waitForMessage_(hotword.constants.TimeoutMs.LONG, |
| 329 hotword.constants.NaClPlugin.MODEL_LOADED); | 373 hotword.constants.NaClPlugin.MODEL_LOADED); |
| 330 }; | 374 }; |
| 331 | 375 |
| 332 /** | 376 /** |
| 333 * Handle a MODEL_LOADED message from the plugin. | 377 * Handle a MODEL_LOADED message from the plugin. |
| 334 * The plugin sends this message after successfully loading the language model. | 378 * The plugin sends this message after successfully loading the language model. |
| 335 * @private | 379 * @private |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 break; | 473 break; |
| 430 } | 474 } |
| 431 } | 475 } |
| 432 }; | 476 }; |
| 433 | 477 |
| 434 return { | 478 return { |
| 435 NaClManager: NaClManager | 479 NaClManager: NaClManager |
| 436 }; | 480 }; |
| 437 | 481 |
| 438 }); | 482 }); |
| OLD | NEW |