summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/hotword/base_session_manager.js
blob: d1ee2bfbff9b975bcebc366684b01a28dbadfa47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('hotword', function() {
  'use strict';

  /**
   * Base class for managing hotwording sessions.
   * @param {!hotword.StateManager} stateManager Manager of global hotwording
   *     state.
   * @param {!hotword.constants.SessionSource} sessionSource Source of the
   *     hotword session request.
   * @constructor
   */
  function BaseSessionManager(stateManager, sessionSource) {
    /**
     * Manager of global hotwording state.
     * @protected {!hotword.StateManager}
     */
    this.stateManager = stateManager;

    /**
     * Source of the hotword session request.
     * @private {!hotword.constants.SessionSource}
     */
    this.sessionSource_ = sessionSource;

    /**
     * Chrome event listeners. Saved so that they can be de-registered when
     * hotwording is disabled.
     * @private
     */
    this.sessionRequestedListener_ = this.handleSessionRequested_.bind(this);
    this.sessionStoppedListener_ = this.handleSessionStopped_.bind(this);

    // Need to setup listeners on startup, otherwise events that caused the
    // event page to start up, will be lost.
    this.setupListeners_();

    this.stateManager.onStatusChanged.addListener(function() {
      hotword.debug('onStatusChanged');
      this.updateListeners();
    }.bind(this));
  }

  BaseSessionManager.prototype = {
    /**
     * Return whether or not this session type is enabled.
     * @protected
     * @return {boolean}
     */
    enabled: assertNotReached,

    /**
     * Called when the hotwording session is stopped.
     * @protected
     */
    onSessionStop: function() {
    },

    /**
     * Starts a launcher hotwording session.
     * @param {hotword.constants.TrainingMode=} opt_mode The mode to start the
     *     recognizer in.
     */
    startSession: function(opt_mode) {
      this.stateManager.startSession(
          this.sessionSource_,
          function() {
            chrome.hotwordPrivate.setHotwordSessionState(true, function() {});
          },
          this.handleHotwordTrigger.bind(this),
          opt_mode);
    },

    /**
     * Stops a launcher hotwording session.
     * @private
     */
    stopSession_: function() {
      this.stateManager.stopSession(this.sessionSource_);
      this.onSessionStop();
    },

    /**
     * Handles a hotword triggered event.
     * @param {?Object} log Audio log data, if audio logging is enabled.
     * @protected
     */
    handleHotwordTrigger: function(log) {
      hotword.debug('Hotword triggered: ' + this.sessionSource_, log);
      chrome.hotwordPrivate.notifyHotwordRecognition('search',
                                                     log,
                                                     function() {});
    },

    /**
     * Handles a hotwordPrivate.onHotwordSessionRequested event.
     * @private
     */
    handleSessionRequested_: function() {
      hotword.debug('handleSessionRequested_: ' + this.sessionSource_);
      this.startSession();
    },

    /**
     * Handles a hotwordPrivate.onHotwordSessionStopped event.
     * @private
     */
    handleSessionStopped_: function() {
      hotword.debug('handleSessionStopped_: ' + this.sessionSource_);
      this.stopSession_();
    },

    /**
     * Set up event listeners.
     * @private
     */
    setupListeners_: function() {
      if (chrome.hotwordPrivate.onHotwordSessionRequested.hasListener(
              this.sessionRequestedListener_)) {
        return;
      }

      chrome.hotwordPrivate.onHotwordSessionRequested.addListener(
          this.sessionRequestedListener_);
      chrome.hotwordPrivate.onHotwordSessionStopped.addListener(
          this.sessionStoppedListener_);
    },

    /**
     * Remove event listeners.
     * @private
     */
    removeListeners_: function() {
      chrome.hotwordPrivate.onHotwordSessionRequested.removeListener(
          this.sessionRequestedListener_);
      chrome.hotwordPrivate.onHotwordSessionStopped.removeListener(
          this.sessionStoppedListener_);
    },

    /**
     * Update event listeners based on the current hotwording state.
     * @protected
     */
    updateListeners: function() {
      if (this.enabled()) {
        this.setupListeners_();
      } else {
        this.removeListeners_();
        this.stopSession_();
      }
    }
  };

  return {
    BaseSessionManager: BaseSessionManager
  };
});