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 "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/scoped_observer.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_registry_observer.h"
18 class ExtensionService;
19 class HotwordAudioHistoryHandler;
23 namespace extensions {
25 class WebstoreStandaloneInstaller;
26 } // namespace extensions
28 namespace hotword_internal {
29 // Constants for the hotword field trial.
30 extern const char kHotwordFieldTrialName[];
31 extern const char kHotwordFieldTrialDisabledGroupName[];
32 // String passed to indicate the training state has changed.
33 extern const char kHotwordTrainingEnabled[];
34 } // namespace hotword_internal
36 // Provides an interface for the Hotword component that does voice triggered
38 class HotwordService : public extensions::ExtensionRegistryObserver,
41 // Returns true if the hotword supports the current system language.
42 static bool DoesHotwordSupportLanguage(Profile* profile);
44 // Returns true if the "enable-experimental-hotwording" flag is set.
45 static bool IsExperimentalHotwordingEnabled();
47 explicit HotwordService(Profile* profile);
48 ~HotwordService() override;
50 // Overridden from ExtensionRegisterObserver:
51 void OnExtensionInstalled(content::BrowserContext* browser_context,
52 const extensions::Extension* extension,
53 bool is_update) override;
54 void OnExtensionUninstalled(content::BrowserContext* browser_context,
55 const extensions::Extension* extension,
56 extensions::UninstallReason reason) override;
58 // Checks for whether all the necessary files have downloaded to allow for
59 // using the extension.
60 virtual bool IsServiceAvailable();
62 // Determine if hotwording is allowed in this profile based on field trials
64 virtual bool IsHotwordAllowed();
66 // Checks if the user has opted into audio logging. Returns true if the user
67 // is opted in, false otherwise..
68 bool IsOptedIntoAudioLogging();
70 // Returns whether always-on hotwording is enabled.
71 bool IsAlwaysOnEnabled();
73 // Control the state of the hotword extension.
74 void EnableHotwordExtension(ExtensionService* extension_service);
75 void DisableHotwordExtension(ExtensionService* extension_service);
77 // Handles enabling/disabling the hotword extension when the user
78 // turns it off via the settings menu.
79 void OnHotwordSearchEnabledChanged(const std::string& pref_name);
81 // Called to handle the hotword session from |client|.
82 void RequestHotwordSession(HotwordClient* client);
83 void StopHotwordSession(HotwordClient* client);
84 HotwordClient* client() { return client_; }
86 // Checks if the current version of the hotword extension should be
87 // uninstalled in order to update to a different language version.
88 // Returns true if the extension was uninstalled.
89 bool MaybeReinstallHotwordExtension();
91 // Checks based on locale if the current version should be uninstalled so that
92 // a version with a different language can be installed.
93 bool ShouldReinstallHotwordExtension();
95 // Helper functions pulled out for testing purposes.
96 // UninstallHotwordExtension returns true if the extension was uninstalled.
97 virtual bool UninstallHotwordExtension(ExtensionService* extension_service);
98 virtual void InstallHotwordExtensionFromWebstore();
100 // Sets the pref value of the previous language.
101 void SetPreviousLanguagePref();
103 // Returns the current error message id. A value of 0 indicates
105 int error_message() { return error_message_; }
107 // These methods are for launching, and getting and setting the launch mode of
108 // the Hotword Audio Verification App.
110 // TODO(kcarattini): Remove this when
111 // https://code.google.com/p/chromium/issues/detail?id=165573 is fixed,
112 // at which time we can simply launch the app in the given mode instead of
113 // having to check for it here.
116 HOTWORD_AND_AUDIO_HISTORY,
119 void LaunchHotwordAudioVerificationApp(const LaunchMode& launch_mode);
120 virtual LaunchMode GetHotwordAudioVerificationLaunchMode();
122 // These methods control the speaker training communication between
123 // the Hotword Audio Verification App and the Hotword Extension that
124 // contains the NaCl module.
125 void StartTraining();
126 void FinalizeSpeakerModel();
128 void NotifyHotwordTriggered();
130 // Returns true if speaker training is currently in progress.
134 // Returns the ID of the extension that may need to be reinstalled.
135 std::string ReinstalledExtensionId();
139 PrefChangeRegistrar pref_registrar_;
141 content::NotificationRegistrar registrar_;
143 // For observing the ExtensionRegistry.
144 ScopedObserver<extensions::ExtensionRegistry,
145 extensions::ExtensionRegistryObserver>
146 extension_registry_observer_;
148 scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
150 scoped_ptr<HotwordAudioHistoryHandler> audio_history_handler_;
152 HotwordClient* client_;
154 bool reinstall_pending_;
155 // Whether we are currently in the process of training the speaker model.
158 base::WeakPtrFactory<HotwordService> weak_factory_;
160 // Stores the launch mode for the Hotword Audio Verification App.
161 LaunchMode hotword_audio_verification_launch_mode_;
163 DISALLOW_COPY_AND_ASSIGN(HotwordService);
166 #endif // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_