BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / resources / hotword / base_session_manager.js
blobd1ee2bfbff9b975bcebc366684b01a28dbadfa47
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    * Base class for managing hotwording sessions.
10    * @param {!hotword.StateManager} stateManager Manager of global hotwording
11    *     state.
12    * @param {!hotword.constants.SessionSource} sessionSource Source of the
13    *     hotword session request.
14    * @constructor
15    */
16   function BaseSessionManager(stateManager, sessionSource) {
17     /**
18      * Manager of global hotwording state.
19      * @protected {!hotword.StateManager}
20      */
21     this.stateManager = stateManager;
23     /**
24      * Source of the hotword session request.
25      * @private {!hotword.constants.SessionSource}
26      */
27     this.sessionSource_ = sessionSource;
29     /**
30      * Chrome event listeners. Saved so that they can be de-registered when
31      * hotwording is disabled.
32      * @private
33      */
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();
44     }.bind(this));
45   }
47   BaseSessionManager.prototype = {
48     /**
49      * Return whether or not this session type is enabled.
50      * @protected
51      * @return {boolean}
52      */
53     enabled: assertNotReached,
55     /**
56      * Called when the hotwording session is stopped.
57      * @protected
58      */
59     onSessionStop: function() {
60     },
62     /**
63      * Starts a launcher hotwording session.
64      * @param {hotword.constants.TrainingMode=} opt_mode The mode to start the
65      *     recognizer in.
66      */
67     startSession: function(opt_mode) {
68       this.stateManager.startSession(
69           this.sessionSource_,
70           function() {
71             chrome.hotwordPrivate.setHotwordSessionState(true, function() {});
72           },
73           this.handleHotwordTrigger.bind(this),
74           opt_mode);
75     },
77     /**
78      * Stops a launcher hotwording session.
79      * @private
80      */
81     stopSession_: function() {
82       this.stateManager.stopSession(this.sessionSource_);
83       this.onSessionStop();
84     },
86     /**
87      * Handles a hotword triggered event.
88      * @param {?Object} log Audio log data, if audio logging is enabled.
89      * @protected
90      */
91     handleHotwordTrigger: function(log) {
92       hotword.debug('Hotword triggered: ' + this.sessionSource_, log);
93       chrome.hotwordPrivate.notifyHotwordRecognition('search',
94                                                      log,
95                                                      function() {});
96     },
98     /**
99      * Handles a hotwordPrivate.onHotwordSessionRequested event.
100      * @private
101      */
102     handleSessionRequested_: function() {
103       hotword.debug('handleSessionRequested_: ' + this.sessionSource_);
104       this.startSession();
105     },
107     /**
108      * Handles a hotwordPrivate.onHotwordSessionStopped event.
109      * @private
110      */
111     handleSessionStopped_: function() {
112       hotword.debug('handleSessionStopped_: ' + this.sessionSource_);
113       this.stopSession_();
114     },
116     /**
117      * Set up event listeners.
118      * @private
119      */
120     setupListeners_: function() {
121       if (chrome.hotwordPrivate.onHotwordSessionRequested.hasListener(
122               this.sessionRequestedListener_)) {
123         return;
124       }
126       chrome.hotwordPrivate.onHotwordSessionRequested.addListener(
127           this.sessionRequestedListener_);
128       chrome.hotwordPrivate.onHotwordSessionStopped.addListener(
129           this.sessionStoppedListener_);
130     },
132     /**
133      * Remove event listeners.
134      * @private
135      */
136     removeListeners_: function() {
137       chrome.hotwordPrivate.onHotwordSessionRequested.removeListener(
138           this.sessionRequestedListener_);
139       chrome.hotwordPrivate.onHotwordSessionStopped.removeListener(
140           this.sessionStoppedListener_);
141     },
143     /**
144      * Update event listeners based on the current hotwording state.
145      * @protected
146      */
147     updateListeners: function() {
148       if (this.enabled()) {
149         this.setupListeners_();
150       } else {
151         this.removeListeners_();
152         this.stopSession_();
153       }
154     }
155   };
157   return {
158     BaseSessionManager: BaseSessionManager
159   };