ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / resources / hotword / training_manager.js
blob61c68c2b89efa9af2d68560f546d61218196aac5
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
3 // found in the LICENSE file.
5 cr.define('hotword', function() {
6 'use strict';
8 /**
9 * Class used to manage speaker training. Starts a hotwording session
10 * if training is on, and automatically restarts the detector when a
11 * a hotword is triggered.
12 * @param {!hotword.StateManager} stateManager
13 * @constructor
14 * @extends {hotword.BaseSessionManager}
16 function TrainingManager(stateManager) {
17 /**
18 * Chrome event listeners. Saved so that they can be de-registered when
19 * hotwording is disabled.
20 * @private
22 this.finalizedSpeakerModelListener_ =
23 this.handleFinalizeSpeakerModel_.bind(this);
25 hotword.BaseSessionManager.call(this,
26 stateManager,
27 hotword.constants.SessionSource.TRAINING);
30 /**
31 * Handles a success event on mounting the file system event.
32 * @param {FileSystem} fs The FileSystem object.
33 * @private
35 TrainingManager.onRequestFileSystemSuccess_ = function(fs) {
36 fs.root.getFile(hotword.constants.SPEAKER_MODEL_FILE_NAME, {create: false},
37 TrainingManager.deleteFile_, TrainingManager.fileErrorHandler_);
39 for (var i = 0; i < hotword.constants.NUM_TRAINING_UTTERANCES; ++i) {
40 fs.root.getFile(hotword.constants.UTTERANCE_FILE_PREFIX + i +
41 hotword.constants.UTTERANCE_FILE_EXTENSION,
42 {create: false},
43 TrainingManager.deleteFile_, TrainingManager.fileErrorHandler_);
47 /**
48 * Deletes a file.
49 * @param {FileEntry} fileEntry The FileEntry object.
50 * @private
52 TrainingManager.deleteFile_ = function(fileEntry) {
53 if (fileEntry.isFile) {
54 hotword.debug('File found: ' + fileEntry.fullPath);
55 if (hotword.DEBUG || window.localStorage['hotword.DEBUG']) {
56 fileEntry.getMetadata(function(md) {
57 hotword.debug('File size: ' + md.size);
58 });
60 fileEntry.remove(function() {
61 hotword.debug('File removed: ' + fileEntry.fullPath);
62 }, TrainingManager.fileErrorHandler_);
66 /**
67 * Handles a failure event on mounting the file system event.
68 * @param {FileError} e The FileError object.
69 * @private
71 TrainingManager.fileErrorHandler_ = function(e) {
72 hotword.debug('File error: ' + e.code);
75 /**
76 * Handles a request to delete the speaker model.
78 TrainingManager.handleDeleteSpeakerModel = function() {
79 window.webkitRequestFileSystem(PERSISTENT,
80 hotword.constants.FILE_SYSTEM_SIZE_BYTES,
81 TrainingManager.onRequestFileSystemSuccess_,
82 TrainingManager.fileErrorHandler_);
85 TrainingManager.prototype = {
86 __proto__: hotword.BaseSessionManager.prototype,
88 /** @override */
89 enabled: function() {
90 return this.stateManager.isTrainingEnabled();
93 /** @override */
94 updateListeners: function() {
95 hotword.BaseSessionManager.prototype.updateListeners.call(this);
97 if (this.enabled()) {
98 // Detect when the speaker model needs to be finalized.
99 if (!chrome.hotwordPrivate.onFinalizeSpeakerModel.hasListener(
100 this.finalizedSpeakerModelListener_)) {
101 chrome.hotwordPrivate.onFinalizeSpeakerModel.addListener(
102 this.finalizedSpeakerModelListener_);
104 this.startSession(hotword.constants.RecognizerStartMode.NEW_MODEL);
105 } else {
106 chrome.hotwordPrivate.onFinalizeSpeakerModel.removeListener(
107 this.finalizedSpeakerModelListener_);
111 /** @override */
112 handleHotwordTrigger: function(log) {
113 if (this.enabled()) {
114 hotword.BaseSessionManager.prototype.handleHotwordTrigger.call(
115 this, log);
116 this.startSession(hotword.constants.RecognizerStartMode.ADAPT_MODEL);
120 /** @override */
121 startSession: function(opt_mode) {
122 this.stateManager.startSession(
123 this.sessionSource_,
124 function() {
125 chrome.hotwordPrivate.setHotwordSessionState(true, function() {});
127 this.handleHotwordTrigger.bind(this),
128 this.handleSpeakerModelSaved_.bind(this),
129 opt_mode);
133 * Handles a hotwordPrivate.onFinalizeSpeakerModel event.
134 * @private
136 handleFinalizeSpeakerModel_: function() {
137 if (this.enabled())
138 this.stateManager.finalizeSpeakerModel();
142 * Handles a hotwordPrivate.onFinalizeSpeakerModel event.
143 * @private
145 handleSpeakerModelSaved_: function() {
146 if (this.enabled())
147 chrome.hotwordPrivate.notifySpeakerModelSaved();
151 return {
152 TrainingManager: TrainingManager