| 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 UmaNaClPluginError_ = hotword.constants.UmaNaClPluginError; |
| 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 event = new Event(hotword.constants.Event.ERROR); | 91 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 a plugin error to UMA. |
| 98 * @param {!hotword.constants.UmaNaClPluginError} error |
| 99 * @private |
| 100 */ |
| 101 NaClManager.prototype.logPluginError_ = function(error) { |
| 102 hotword.metrics.recordEnum( |
| 103 hotword.constants.UmaMetrics.NACL_PLUGIN_ERROR, |
| 104 error, |
| 105 UmaNaClPluginError_.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.logPluginError_(UmaNaClPluginError_.CRASH); |
| 270 }.bind(this), |
| 254 false); | 271 false); |
| 255 return true; | 272 return true; |
| 256 } | 273 } |
| 257 this.recognizerState_ = ManagerState_.ERROR; | 274 this.recognizerState_ = ManagerState_.ERROR; |
| 275 this.logPluginError_(UmaNaClPluginError_.NO_MODULE_FOUND); |
| 258 return false; | 276 return false; |
| 259 }; | 277 }; |
| 260 | 278 |
| 261 /** | 279 /** |
| 262 * Shuts down the NaCl plugin and frees all resources. | 280 * Shuts down the NaCl plugin and frees all resources. |
| 263 */ | 281 */ |
| 264 NaClManager.prototype.shutdown = function() { | 282 NaClManager.prototype.shutdown = function() { |
| 265 if (this.plugin_ != null) { | 283 if (this.plugin_ != null) { |
| 266 document.body.removeChild(this.plugin_); | 284 document.body.removeChild(this.plugin_); |
| 267 this.plugin_ = null; | 285 this.plugin_ = null; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 290 * @param {!string} message Message to wait for. | 308 * @param {!string} message Message to wait for. |
| 291 * @private | 309 * @private |
| 292 */ | 310 */ |
| 293 NaClManager.prototype.waitForMessage_ = function(timeout, message) { | 311 NaClManager.prototype.waitForMessage_ = function(timeout, message) { |
| 294 assert(this.expectingMessage_ == null, | 312 assert(this.expectingMessage_ == null, |
| 295 'Already waiting for message ' + this.expectingMessage_); | 313 'Already waiting for message ' + this.expectingMessage_); |
| 296 this.setTimeout_( | 314 this.setTimeout_( |
| 297 function() { | 315 function() { |
| 298 this.recognizerState_ = ManagerState_.ERROR; | 316 this.recognizerState_ = ManagerState_.ERROR; |
| 299 this.handleError_(Error_.TIMEOUT); | 317 this.handleError_(Error_.TIMEOUT); |
| 318 this.logPluginError_(UmaNaClPluginError_.MESSAGE_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 */ |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 break; | 472 break; |
| 430 } | 473 } |
| 431 } | 474 } |
| 432 }; | 475 }; |
| 433 | 476 |
| 434 return { | 477 return { |
| 435 NaClManager: NaClManager | 478 NaClManager: NaClManager |
| 436 }; | 479 }; |
| 437 | 480 |
| 438 }); | 481 }); |
| OLD | NEW |