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() {
9 * Base class for managing hotwording sessions.
10 * @param {!hotword.StateManager} stateManager Manager of global hotwording
12 * @param {!hotword.constants.SessionSource} sessionSource Source of the
13 * hotword session request.
16 function BaseSessionManager(stateManager, sessionSource) {
18 * Manager of global hotwording state.
19 * @protected {!hotword.StateManager}
21 this.stateManager = stateManager;
24 * Source of the hotword session request.
25 * @private {!hotword.constants.SessionSource}
27 this.sessionSource_ = sessionSource;
30 * Chrome event listeners. Saved so that they can be de-registered when
31 * hotwording is disabled.
34 this.sessionRequestedListener_ = this.handleSessionRequested_.bind(this);
35 this.sessionStoppedListener_ = this.handleSessionStopped_.bind(this);
37 // Need to setup listeners on startup, otherwise events that caused the
38 // event page to start up, will be lost.
39 this.setupListeners_();
41 this.stateManager.onStatusChanged.addListener(function() {
42 hotword.debug('onStatusChanged');
43 this.updateListeners();
47 BaseSessionManager.prototype = {
49 * Return whether or not this session type is enabled.
53 enabled: assertNotReached,
56 * Called when the hotwording session is stopped.
59 onSessionStop: function() {
63 * Starts a launcher hotwording session.
64 * @param {hotword.constants.TrainingMode=} opt_mode The mode to start the
67 startSession: function(opt_mode) {
68 this.stateManager.startSession(
71 chrome.hotwordPrivate.setHotwordSessionState(true, function() {});
73 this.handleHotwordTrigger.bind(this),
78 * Stops a launcher hotwording session.
81 stopSession_: function() {
82 this.stateManager.stopSession(this.sessionSource_);
87 * Handles a hotword triggered event.
88 * @param {?Object} log Audio log data, if audio logging is enabled.
91 handleHotwordTrigger: function(log) {
92 hotword.debug('Hotword triggered: ' + this.sessionSource_, log);
93 chrome.hotwordPrivate.notifyHotwordRecognition('search',
99 * Handles a hotwordPrivate.onHotwordSessionRequested event.
102 handleSessionRequested_: function() {
103 hotword.debug('handleSessionRequested_: ' + this.sessionSource_);
108 * Handles a hotwordPrivate.onHotwordSessionStopped event.
111 handleSessionStopped_: function() {
112 hotword.debug('handleSessionStopped_: ' + this.sessionSource_);
117 * Set up event listeners.
120 setupListeners_: function() {
121 if (chrome.hotwordPrivate.onHotwordSessionRequested.hasListener(
122 this.sessionRequestedListener_)) {
126 chrome.hotwordPrivate.onHotwordSessionRequested.addListener(
127 this.sessionRequestedListener_);
128 chrome.hotwordPrivate.onHotwordSessionStopped.addListener(
129 this.sessionStoppedListener_);
133 * Remove event listeners.
136 removeListeners_: function() {
137 chrome.hotwordPrivate.onHotwordSessionRequested.removeListener(
138 this.sessionRequestedListener_);
139 chrome.hotwordPrivate.onHotwordSessionStopped.removeListener(
140 this.sessionStoppedListener_);
144 * Update event listeners based on the current hotwording state.
147 updateListeners: function() {
148 if (this.enabled()) {
149 this.setupListeners_();
151 this.removeListeners_();
158 BaseSessionManager: BaseSessionManager