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.
66 startSession_: function() {
67 this.stateManager.startSession(
70 chrome.hotwordPrivate.setHotwordSessionState(true, function() {});
72 this.handleHotwordTrigger.bind(this));
76 * Stops a launcher hotwording session.
79 stopSession_: function() {
80 this.stateManager.stopSession(this.sessionSource_);
85 * Handles a hotword triggered event.
88 handleHotwordTrigger: function() {
89 hotword.debug('Hotword triggered: ' + this.sessionSource_);
90 chrome.hotwordPrivate.notifyHotwordRecognition('search', function() {});
94 * Handles a hotwordPrivate.onHotwordSessionRequested event.
97 handleSessionRequested_: function() {
98 hotword.debug('handleSessionRequested_: ' + this.sessionSource_);
103 * Handles a hotwordPrivate.onHotwordSessionStopped event.
106 handleSessionStopped_: function() {
107 hotword.debug('handleSessionStopped_: ' + this.sessionSource_);
112 * Set up event listeners.
115 setupListeners_: function() {
116 if (chrome.hotwordPrivate.onHotwordSessionRequested.hasListener(
117 this.sessionRequestedListener_)) {
121 chrome.hotwordPrivate.onHotwordSessionRequested.addListener(
122 this.sessionRequestedListener_);
123 chrome.hotwordPrivate.onHotwordSessionStopped.addListener(
124 this.sessionStoppedListener_);
128 * Remove event listeners.
131 removeListeners_: function() {
132 chrome.hotwordPrivate.onHotwordSessionRequested.removeListener(
133 this.sessionRequestedListener_);
134 chrome.hotwordPrivate.onHotwordSessionStopped.removeListener(
135 this.sessionStoppedListener_);
139 * Update event listeners based on the current hotwording state.
142 updateListeners: function() {
143 if (this.enabled()) {
144 this.setupListeners_();
146 this.removeListeners_();
153 BaseSessionManager: BaseSessionManager