Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / search / hotword_service.h
blob2d2394a38b1597de9d2323b4fd60fa80cdc67dff
1 // Copyright 2013 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 #ifndef CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/webstore_startup_installer.h"
15 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
16 #include "chrome/common/extensions/webstore_install_result.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/browser/extension_registry_observer.h"
23 class ExtensionService;
24 class HotwordAudioHistoryHandler;
25 class HotwordClient;
26 class Profile;
28 namespace extensions {
29 class Extension;
30 } // namespace extensions
32 namespace hotword_internal {
33 // Constants for the hotword field trial.
34 extern const char kHotwordFieldTrialName[];
35 extern const char kHotwordFieldTrialDisabledGroupName[];
36 // String passed to indicate the training state has changed.
37 extern const char kHotwordTrainingEnabled[];
38 } // namespace hotword_internal
40 // Provides an interface for the Hotword component that does voice triggered
41 // search.
42 class HotwordService : public MediaCaptureDevicesDispatcher::Observer,
43 public extensions::ExtensionRegistryObserver,
44 public KeyedService {
45 public:
46 // A simple subclass to allow for aborting an install during shutdown.
47 // HotwordWebstoreInstaller class is public for testing.
48 class HotwordWebstoreInstaller : public extensions::WebstoreStartupInstaller {
49 public:
50 HotwordWebstoreInstaller(const std::string& webstore_item_id,
51 Profile* profile,
52 const Callback& callback)
53 : extensions::WebstoreStartupInstaller(webstore_item_id,
54 profile,
55 false,
56 callback) {}
57 void Shutdown();
58 protected:
59 ~HotwordWebstoreInstaller() override {}
62 // Returns true if the hotword supports the current system language.
63 static bool DoesHotwordSupportLanguage(Profile* profile);
65 // Returns true if hotwording hardware is available.
66 static bool IsHotwordHardwareAvailable();
68 explicit HotwordService(Profile* profile);
69 ~HotwordService() override;
71 // Overridden from ExtensionRegisterObserver:
72 void OnExtensionInstalled(content::BrowserContext* browser_context,
73 const extensions::Extension* extension,
74 bool is_update) override;
75 void OnExtensionUninstalled(content::BrowserContext* browser_context,
76 const extensions::Extension* extension,
77 extensions::UninstallReason reason) override;
79 // Overriden from KeyedService
80 void Shutdown() override;
82 // Checks for whether all the necessary files have downloaded to allow for
83 // using the extension.
84 virtual bool IsServiceAvailable();
86 // Determine if hotwording is allowed in this profile based on field trials
87 // and language.
88 virtual bool IsHotwordAllowed();
90 // Checks if the user has opted into audio logging. Returns true if the user
91 // is opted in, false otherwise..
92 bool IsOptedIntoAudioLogging();
94 // Returns whether always-on hotwording is enabled.
95 bool IsAlwaysOnEnabled();
97 // Returns whether google.com/NTP/launcher hotwording is enabled.
98 bool IsSometimesOnEnabled();
100 // Handles enabling/disabling the hotword notification when the user
101 // changes the always on search settings.
102 void OnHotwordAlwaysOnSearchEnabledChanged(const std::string& pref_name);
104 // Called to handle the hotword session from |client|.
105 void RequestHotwordSession(HotwordClient* client);
106 void StopHotwordSession(HotwordClient* client);
107 HotwordClient* client() { return client_; }
109 // Checks if the current version of the hotword extension should be
110 // uninstalled in order to update to a different language version.
111 // Returns true if the extension was uninstalled.
112 bool MaybeReinstallHotwordExtension();
114 // Checks based on locale if the current version should be uninstalled so that
115 // a version with a different language can be installed.
116 bool ShouldReinstallHotwordExtension();
118 // Helper functions pulled out for testing purposes.
119 // UninstallHotwordExtension returns true if the extension was uninstalled.
120 virtual bool UninstallHotwordExtension(ExtensionService* extension_service);
121 virtual void InstallHotwordExtensionFromWebstore(int num_tries);
123 // Sets the pref value of the previous language.
124 void SetPreviousLanguagePref();
126 // Returns the current error message id. A value of 0 indicates
127 // no error.
128 int error_message() { return error_message_; }
130 bool microphone_available() { return microphone_available_; }
132 // These methods are for launching, and getting and setting the launch mode of
133 // the Hotword Audio Verification App.
135 // OptIntoHotwording first determines if the app needs to be launched, and if
136 // so, launches the app (if Audio History is on and a speaker model exists,
137 // then we don't need to launch the app).
139 // LaunchHotwordAudioVerificationApp launches the app without the above
140 // check in the specified |launch_mode|.
141 enum LaunchMode {
142 HOTWORD_ONLY,
143 HOTWORD_AND_AUDIO_HISTORY,
144 RETRAIN
146 void OptIntoHotwording(const LaunchMode& launch_mode);
147 void LaunchHotwordAudioVerificationApp(const LaunchMode& launch_mode);
148 virtual LaunchMode GetHotwordAudioVerificationLaunchMode();
150 // Called when the SpeakerModelExists request is complete. Either
151 // sets the always-on hotword pref to true, or launches the Hotword
152 // Audio Verification App, depending on the value of |exists|.
153 void SpeakerModelExistsComplete(bool exists);
155 // These methods control the speaker training communication between
156 // the Hotword Audio Verification App and the Hotword Extension that
157 // contains the NaCl module.
158 void StartTraining();
159 void FinalizeSpeakerModel();
160 void StopTraining();
161 void NotifyHotwordTriggered();
163 // Returns true if speaker training is currently in progress.
164 bool IsTraining();
166 // Indicate that the currently active user has changed.
167 void ActiveUserChanged();
169 // Return true if this profile corresponds to the currently active user.
170 bool UserIsActive();
172 // Returns a pointer to the audio history handler.
173 HotwordAudioHistoryHandler* GetAudioHistoryHandler();
175 // Sets the audio history handler. Used for tests.
176 void SetAudioHistoryHandler(HotwordAudioHistoryHandler* handler);
178 // Turn off the currently enabled version of hotwording if one exists.
179 void DisableHotwordPreferences();
181 // Overridden from MediaCaptureDevicesDispatcher::Observer
182 void OnUpdateAudioDevices(
183 const content::MediaStreamDevices& devices) override;
185 protected:
186 // Used in test subclasses.
187 scoped_refptr<HotwordWebstoreInstaller> installer_;
189 private:
190 class HotwordUserSessionStateObserver;
192 // Must be called from the UI thread since the instance of
193 // MediaCaptureDevicesDispatcher can only be accessed on the UI thread.
194 void InitializeMicrophoneObserver();
196 // Callback for webstore extension installer.
197 void InstalledFromWebstoreCallback(
198 int num_tries,
199 bool success,
200 const std::string& error,
201 extensions::webstore_install::Result result);
203 // Returns the ID of the extension that may need to be reinstalled.
204 std::string ReinstalledExtensionId();
206 // Creates a notification for always-on hotwording.
207 void ShowHotwordNotification();
209 Profile* profile_;
211 PrefChangeRegistrar pref_registrar_;
213 content::NotificationRegistrar registrar_;
215 // For observing the ExtensionRegistry.
216 ScopedObserver<extensions::ExtensionRegistry,
217 extensions::ExtensionRegistryObserver>
218 extension_registry_observer_;
220 scoped_ptr<HotwordAudioHistoryHandler> audio_history_handler_;
222 bool microphone_available_;
224 // Indicates if the check for audio devices has been run such that it can be
225 // included in the error checking. Audio checking is not done immediately
226 // upon start up because of the negative impact on performance.
227 bool audio_device_state_updated_;
229 HotwordClient* client_;
230 int error_message_;
231 bool reinstall_pending_;
232 // Whether we are currently in the process of training the speaker model.
233 bool training_;
234 scoped_ptr<HotwordUserSessionStateObserver> session_observer_;
236 // Stores the launch mode for the Hotword Audio Verification App.
237 LaunchMode hotword_audio_verification_launch_mode_;
239 // The WeakPtrFactory should be the last member, so the weak pointer
240 // gets invalidated before the destructors for other members run,
241 // to avoid callbacks into a half-destroyed object.
242 base::WeakPtrFactory<HotwordService> weak_factory_;
244 DISALLOW_COPY_AND_ASSIGN(HotwordService);
247 #endif // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_