[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chrome / browser / search / hotword_service.h
blob9b22c84310ec015e1eeac5b8d5dba5f5ba2c10ae
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/common/extensions/webstore_install_result.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_registry_observer.h"
22 class ExtensionService;
23 class HotwordAudioHistoryHandler;
24 class HotwordClient;
25 class Profile;
27 namespace extensions {
28 class Extension;
29 } // namespace extensions
31 namespace hotword_internal {
32 // Constants for the hotword field trial.
33 extern const char kHotwordFieldTrialName[];
34 extern const char kHotwordFieldTrialDisabledGroupName[];
35 // String passed to indicate the training state has changed.
36 extern const char kHotwordTrainingEnabled[];
37 } // namespace hotword_internal
39 // Provides an interface for the Hotword component that does voice triggered
40 // search.
41 class HotwordService : public extensions::ExtensionRegistryObserver,
42 public KeyedService {
43 public:
44 // A simple subclass to allow for aborting an install during shutdown.
45 // HotwordWebstoreInstaller class is public for testing.
46 class HotwordWebstoreInstaller : public extensions::WebstoreStartupInstaller {
47 public:
48 HotwordWebstoreInstaller(const std::string& webstore_item_id,
49 Profile* profile,
50 const Callback& callback)
51 : extensions::WebstoreStartupInstaller(webstore_item_id,
52 profile,
53 false,
54 callback) {}
55 void Shutdown();
56 protected:
57 ~HotwordWebstoreInstaller() override {}
60 // Returns true if the hotword supports the current system language.
61 static bool DoesHotwordSupportLanguage(Profile* profile);
63 // Returns true if hotwording hardware is available.
64 static bool IsHotwordHardwareAvailable();
66 explicit HotwordService(Profile* profile);
67 ~HotwordService() override;
69 // Overridden from ExtensionRegisterObserver:
70 void OnExtensionInstalled(content::BrowserContext* browser_context,
71 const extensions::Extension* extension,
72 bool is_update) override;
73 void OnExtensionUninstalled(content::BrowserContext* browser_context,
74 const extensions::Extension* extension,
75 extensions::UninstallReason reason) override;
77 // Overriden from KeyedService
78 void Shutdown() override;
80 // Checks for whether all the necessary files have downloaded to allow for
81 // using the extension.
82 virtual bool IsServiceAvailable();
84 // Determine if hotwording is allowed in this profile based on field trials
85 // and language.
86 virtual bool IsHotwordAllowed();
88 // Checks if the user has opted into audio logging. Returns true if the user
89 // is opted in, false otherwise..
90 bool IsOptedIntoAudioLogging();
92 // Returns whether always-on hotwording is enabled.
93 bool IsAlwaysOnEnabled();
95 // Returns whether google.com/NTP/launcher hotwording is enabled.
96 bool IsSometimesOnEnabled();
98 // Handles enabling/disabling the hotword notification when the user
99 // changes the always on search settings.
100 void OnHotwordAlwaysOnSearchEnabledChanged(const std::string& pref_name);
102 // Called to handle the hotword session from |client|.
103 void RequestHotwordSession(HotwordClient* client);
104 void StopHotwordSession(HotwordClient* client);
105 HotwordClient* client() { return client_; }
107 // Checks if the current version of the hotword extension should be
108 // uninstalled in order to update to a different language version.
109 // Returns true if the extension was uninstalled.
110 bool MaybeReinstallHotwordExtension();
112 // Checks based on locale if the current version should be uninstalled so that
113 // a version with a different language can be installed.
114 bool ShouldReinstallHotwordExtension();
116 // Helper functions pulled out for testing purposes.
117 // UninstallHotwordExtension returns true if the extension was uninstalled.
118 virtual bool UninstallHotwordExtension(ExtensionService* extension_service);
119 virtual void InstallHotwordExtensionFromWebstore(int num_tries);
121 // Sets the pref value of the previous language.
122 void SetPreviousLanguagePref();
124 // Returns the current error message id. A value of 0 indicates
125 // no error.
126 int error_message() { return error_message_; }
128 // These methods are for launching, and getting and setting the launch mode of
129 // the Hotword Audio Verification App.
131 // OptIntoHotwording first determines if the app needs to be launched, and if
132 // so, launches the app (if Audio History is on and a speaker model exists,
133 // then we don't need to launch the app).
135 // LaunchHotwordAudioVerificationApp launches the app without the above
136 // check in the specified |launch_mode|.
137 enum LaunchMode {
138 HOTWORD_ONLY,
139 HOTWORD_AND_AUDIO_HISTORY,
140 RETRAIN
142 void OptIntoHotwording(const LaunchMode& launch_mode);
143 void LaunchHotwordAudioVerificationApp(const LaunchMode& launch_mode);
144 virtual LaunchMode GetHotwordAudioVerificationLaunchMode();
146 // Called when the SpeakerModelExists request is complete. Either
147 // sets the always-on hotword pref to true, or launches the Hotword
148 // Audio Verification App, depending on the value of |exists|.
149 void SpeakerModelExistsComplete(bool exists);
151 // These methods control the speaker training communication between
152 // the Hotword Audio Verification App and the Hotword Extension that
153 // contains the NaCl module.
154 void StartTraining();
155 void FinalizeSpeakerModel();
156 void StopTraining();
157 void NotifyHotwordTriggered();
159 // Returns true if speaker training is currently in progress.
160 bool IsTraining();
162 // Indicate that the currently active user has changed.
163 void ActiveUserChanged();
165 // Return true if this profile corresponds to the currently active user.
166 bool UserIsActive();
168 // Returns a pointer to the audio history handler.
169 HotwordAudioHistoryHandler* GetAudioHistoryHandler();
171 // Sets the audio history handler. Used for tests.
172 void SetAudioHistoryHandler(HotwordAudioHistoryHandler* handler);
174 // Turn off the currently enabled version of hotwording if one exists.
175 void DisableHotwordPreferences();
177 protected:
178 // Used in test subclasses.
179 scoped_refptr<HotwordWebstoreInstaller> installer_;
181 private:
182 class HotwordUserSessionStateObserver;
184 // Callback for webstore extension installer.
185 void InstalledFromWebstoreCallback(
186 int num_tries,
187 bool success,
188 const std::string& error,
189 extensions::webstore_install::Result result);
191 // Returns the ID of the extension that may need to be reinstalled.
192 std::string ReinstalledExtensionId();
194 // Creates a notification for always-on hotwording.
195 void ShowHotwordNotification();
197 Profile* profile_;
199 PrefChangeRegistrar pref_registrar_;
201 content::NotificationRegistrar registrar_;
203 // For observing the ExtensionRegistry.
204 ScopedObserver<extensions::ExtensionRegistry,
205 extensions::ExtensionRegistryObserver>
206 extension_registry_observer_;
208 scoped_ptr<HotwordAudioHistoryHandler> audio_history_handler_;
210 HotwordClient* client_;
211 int error_message_;
212 bool reinstall_pending_;
213 // Whether we are currently in the process of training the speaker model.
214 bool training_;
215 scoped_ptr<HotwordUserSessionStateObserver> session_observer_;
217 // Stores the launch mode for the Hotword Audio Verification App.
218 LaunchMode hotword_audio_verification_launch_mode_;
220 // The WeakPtrFactory should be the last member, so the weak pointer
221 // gets invalidated before the destructors for other members run,
222 // to avoid callbacks into a half-destroyed object.
223 base::WeakPtrFactory<HotwordService> weak_factory_;
225 DISALLOW_COPY_AND_ASSIGN(HotwordService);
228 #endif // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_