Chromium Code Reviews| 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 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 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 this.logPluginLoadResult_(UmaNaClPluginLoadResult_.MESSAGE_TIMEOUT); | |
| 320 switch (this.expectingMessage_) { | |
| 321 case hotword.constants.NaClPlugin.REQUEST_MODEL: | |
| 322 var metricValue = UmaNaClMessageTimeout_.REQUEST_MODEL; | |
| 323 break; | |
| 324 case hotword.constants.NaClPlugin.MODEL_LOADED: | |
| 325 var metricValue = UmaNaClMessageTimeout_.MODEL_LOADED; | |
| 326 break; | |
| 327 case hotword.constants.NaClPlugin.READY_FOR_AUDIO: | |
| 328 var metricValue = UmaNaClMessageTimeout_.READY_FOR_AUDIO; | |
| 329 break; | |
| 330 case hotword.constants.NaClPlugin.STOPPED: | |
| 331 var metricValue = UmaNaClMessageTimeout_.STOPPED; | |
| 332 break; | |
| 333 case hotword.constants.NaClPlugin.HOTWORD_DETECTED: | |
| 334 var metricValue = UmaNaClMessageTimeout_.HOTWORD_DETECTED; | |
| 335 break; | |
| 336 case hotword.constants.NaClPlugin.MS_CONFIGURED: | |
| 337 var metricValue = UmaNaClMessageTimeout_.MS_CONFIGURED; | |
| 338 break; | |
| 339 } | |
| 340 hotword.metrics.recordEnum( | |
| 341 hotword.constants.UmaMetrics.NACL_MESSAGE_TIMEOUT, | |
| 342 metricValue, | |
|
rpetterson
2014/10/23 19:03:36
Perhaps success should simply be one of the cases
Anand Mistry (off Chromium)
2014/10/23 21:38:16
I don't understand. I don't think we want to track
| |
| 343 UmaNaClMessageTimeout_.MAX); | |
| 300 }.bind(this), timeout); | 344 }.bind(this), timeout); |
| 301 this.expectingMessage_ = message; | 345 this.expectingMessage_ = message; |
| 302 }; | 346 }; |
| 303 | 347 |
| 304 /** | 348 /** |
| 305 * Called when a message is received from the plugin. If we're waiting for that | 349 * Called when a message is received from the plugin. If we're waiting for that |
| 306 * message, cancel the pending timeout. | 350 * message, cancel the pending timeout. |
| 307 * @param {string} message Message received. | 351 * @param {string} message Message received. |
| 308 * @private | 352 * @private |
| 309 */ | 353 */ |
| 310 NaClManager.prototype.receivedMessage_ = function(message) { | 354 NaClManager.prototype.receivedMessage_ = function(message) { |
| 311 if (message == this.expectingMessage_) { | 355 if (message == this.expectingMessage_) { |
| 312 this.clearTimeout_(); | 356 this.clearTimeout_(); |
| 313 this.expectingMessage_ = null; | 357 this.expectingMessage_ = null; |
| 314 } | 358 } |
| 315 }; | 359 }; |
| 316 | 360 |
| 317 /** | 361 /** |
| 318 * Handle a REQUEST_MODEL message from the plugin. | 362 * Handle a REQUEST_MODEL message from the plugin. |
| 319 * The plugin sends this message immediately after starting. | 363 * The plugin sends this message immediately after starting. |
| 320 * @private | 364 * @private |
| 321 */ | 365 */ |
| 322 NaClManager.prototype.handleRequestModel_ = function() { | 366 NaClManager.prototype.handleRequestModel_ = function() { |
| 323 if (this.recognizerState_ != ManagerState_.LOADING) { | 367 if (this.recognizerState_ != ManagerState_.LOADING) { |
| 324 return; | 368 return; |
| 325 } | 369 } |
| 370 this.logPluginLoadResult_(UmaNaClPluginLoadResult_.SUCCESS); | |
| 326 this.sendDataToPlugin_( | 371 this.sendDataToPlugin_( |
| 327 hotword.constants.NaClPlugin.MODEL_PREFIX + this.modelUrl_); | 372 hotword.constants.NaClPlugin.MODEL_PREFIX + this.modelUrl_); |
| 328 this.waitForMessage_(hotword.constants.TimeoutMs.LONG, | 373 this.waitForMessage_(hotword.constants.TimeoutMs.LONG, |
| 329 hotword.constants.NaClPlugin.MODEL_LOADED); | 374 hotword.constants.NaClPlugin.MODEL_LOADED); |
| 330 }; | 375 }; |
| 331 | 376 |
| 332 /** | 377 /** |
| 333 * Handle a MODEL_LOADED message from the plugin. | 378 * Handle a MODEL_LOADED message from the plugin. |
| 334 * The plugin sends this message after successfully loading the language model. | 379 * The plugin sends this message after successfully loading the language model. |
| 335 * @private | 380 * @private |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 break; | 474 break; |
| 430 } | 475 } |
| 431 } | 476 } |
| 432 }; | 477 }; |
| 433 | 478 |
| 434 return { | 479 return { |
| 435 NaClManager: NaClManager | 480 NaClManager: NaClManager |
| 436 }; | 481 }; |
| 437 | 482 |
| 438 }); | 483 }); |
| OLD | NEW |