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
;
22 namespace extensions
{
24 class WebstoreStandaloneInstaller
;
25 } // namespace extensions
27 namespace hotword_internal
{
28 // Constants for the hotword field trial.
29 extern const char kHotwordFieldTrialName
[];
30 extern const char kHotwordFieldTrialDisabledGroupName
[];
31 } // namespace hotword_internal
33 // Provides an interface for the Hotword component that does voice triggered
35 class HotwordService
: public content::NotificationObserver
,
36 public extensions::ExtensionRegistryObserver
,
39 // Returns true if the hotword supports the current system language.
40 static bool DoesHotwordSupportLanguage(Profile
* profile
);
42 // Returns true if the "enable-experimental-hotwording" flag is set.
43 static bool IsExperimentalHotwordingEnabled();
45 explicit HotwordService(Profile
* profile
);
46 virtual ~HotwordService();
48 // Overridden from content::NotificationObserver:
49 virtual void Observe(int type
,
50 const content::NotificationSource
& source
,
51 const content::NotificationDetails
& details
) override
;
53 // Overridden from ExtensionRegisterObserver:
54 virtual void OnExtensionInstalled(
55 content::BrowserContext
* browser_context
,
56 const extensions::Extension
* extension
,
57 bool is_update
) override
;
58 virtual void OnExtensionUninstalled(
59 content::BrowserContext
* browser_context
,
60 const extensions::Extension
* extension
,
61 extensions::UninstallReason reason
) override
;
63 // Checks for whether all the necessary files have downloaded to allow for
64 // using the extension.
65 virtual bool IsServiceAvailable();
67 // Determine if hotwording is allowed in this profile based on field trials
69 virtual bool IsHotwordAllowed();
71 // Checks if the user has opted into audio logging. Returns true if the user
72 // is opted in, false otherwise..
73 bool IsOptedIntoAudioLogging();
75 // Returns whether always-on hotwording is enabled.
76 bool IsAlwaysOnEnabled();
78 // Control the state of the hotword extension.
79 void EnableHotwordExtension(ExtensionService
* extension_service
);
80 void DisableHotwordExtension(ExtensionService
* extension_service
);
82 // Handles enabling/disabling the hotword extension when the user
83 // turns it off via the settings menu.
84 void OnHotwordSearchEnabledChanged(const std::string
& pref_name
);
86 // Called to handle the hotword session from |client|.
87 void RequestHotwordSession(HotwordClient
* client
);
88 void StopHotwordSession(HotwordClient
* client
);
89 HotwordClient
* client() { return client_
; }
91 // Checks if the current version of the hotword extension should be
92 // uninstalled in order to update to a different language version.
93 // Returns true if the extension was uninstalled.
94 bool MaybeReinstallHotwordExtension();
96 // Checks based on locale if the current version should be uninstalled so that
97 // a version with a different language can be installed.
98 bool ShouldReinstallHotwordExtension();
100 // Helper functions pulled out for testing purposes.
101 // UninstallHotwordExtension returns true if the extension was uninstalled.
102 virtual bool UninstallHotwordExtension(ExtensionService
* extension_service
);
103 virtual void InstallHotwordExtensionFromWebstore();
105 // Sets the pref value of the previous language.
106 void SetPreviousLanguagePref();
108 // Returns the current error message id. A value of 0 indicates
110 int error_message() { return error_message_
; }
112 // These methods are for launching, and getting and setting the launch mode of
113 // the Hotword Audio Verification App.
115 // TODO(kcarattini): Remove this when
116 // https://code.google.com/p/chromium/issues/detail?id=165573 is fixed,
117 // at which time we can simply launch the app in the given mode instead of
118 // having to check for it here.
122 HOTWORD_AND_AUDIO_HISTORY
,
125 void LaunchHotwordAudioVerificationApp(const LaunchMode
& launch_mode
);
126 virtual LaunchMode
GetHotwordAudioVerificationLaunchMode();
131 PrefChangeRegistrar pref_registrar_
;
133 content::NotificationRegistrar registrar_
;
135 // For observing the ExtensionRegistry.
136 ScopedObserver
<extensions::ExtensionRegistry
,
137 extensions::ExtensionRegistryObserver
>
138 extension_registry_observer_
;
140 scoped_refptr
<extensions::WebstoreStandaloneInstaller
> installer_
;
142 HotwordClient
* client_
;
144 bool reinstall_pending_
;
146 base::WeakPtrFactory
<HotwordService
> weak_factory_
;
148 // Stores the launch mode for the Hotword Audio Verification App.
149 LaunchMode hotword_audio_verification_launch_mode_
;
151 DISALLOW_COPY_AND_ASSIGN(HotwordService
);
154 #endif // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_