Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / hotword / base_session_manager.js
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.
4
5 cr.define('hotword', function() {
6   'use strict';
7
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;
22
23     /**
24      * Source of the hotword session request.
25      * @private {!hotword.constants.SessionSource}
26      */
27     this.sessionSource_ = sessionSource;
28
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);
36
37     // Need to setup listeners on startup, otherwise events that caused the
38     // event page to start up, will be lost.
39     this.setupListeners_();
40
41     this.stateManager.onStatusChanged.addListener(function() {
42       hotword.debug('onStatusChanged');
43       this.updateListeners();
44     }.bind(this));
45   }
46
47   BaseSessionManager.prototype = {
48     /**
49      * Return whether or not this session type is enabled.
50      * @protected
51      * @return {boolean}
52      */
53     enabled: assertNotReached,
54
55     /**
56      * Called when the hotwording session is stopped.
57      * @protected
58      */
59     onSessionStop: function() {
60     },
61
62     /**
63      * Starts a launcher hotwording session.
64      * @private
65      */
66     startSession_: function() {
67       this.stateManager.startSession(
68           this.sessionSource_,
69           function() {
70             chrome.hotwordPrivate.setHotwordSessionState(true, function() {});
71           },
72           this.handleHotwordTrigger.bind(this));
73     },
74
75     /**
76      * Stops a launcher hotwording session.
77      * @private
78      */
79     stopSession_: function() {
80       this.stateManager.stopSession(this.sessionSource_);
81       this.onSessionStop();
82     },
83
84     /**
85      * Handles a hotword triggered event.
86      * @protected
87      */
88     handleHotwordTrigger: function() {
89       hotword.debug('Hotword triggered: ' + this.sessionSource_);
90       chrome.hotwordPrivate.notifyHotwordRecognition('search', function() {});
91     },
92
93     /**
94      * Handles a hotwordPrivate.onHotwordSessionRequested event.
95      * @private
96      */
97     handleSessionRequested_: function() {
98       hotword.debug('handleSessionRequested_: ' + this.sessionSource_);
99       this.startSession_();
100     },
101
102     /**
103      * Handles a hotwordPrivate.onHotwordSessionStopped event.
104      * @private
105      */
106     handleSessionStopped_: function() {
107       hotword.debug('handleSessionStopped_: ' + this.sessionSource_);
108       this.stopSession_();
109     },
110
111     /**
112      * Set up event listeners.
113      * @private
114      */
115     setupListeners_: function() {
116       if (chrome.hotwordPrivate.onHotwordSessionRequested.hasListener(
117               this.sessionRequestedListener_)) {
118         return;
119       }
120
121       chrome.hotwordPrivate.onHotwordSessionRequested.addListener(
122           this.sessionRequestedListener_);
123       chrome.hotwordPrivate.onHotwordSessionStopped.addListener(
124           this.sessionStoppedListener_);
125     },
126
127     /**
128      * Remove event listeners.
129      * @private
130      */
131     removeListeners_: function() {
132       chrome.hotwordPrivate.onHotwordSessionRequested.removeListener(
133           this.sessionRequestedListener_);
134       chrome.hotwordPrivate.onHotwordSessionStopped.removeListener(
135           this.sessionStoppedListener_);
136     },
137
138     /**
139      * Update event listeners based on the current hotwording state.
140      * @protected
141      */
142     updateListeners: function() {
143       if (this.enabled()) {
144         this.setupListeners_();
145       } else {
146         this.removeListeners_();
147         this.stopSession_();
148       }
149     }
150   };
151
152   return {
153     BaseSessionManager: BaseSessionManager
154   };
155 });