Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / hotword_service.h
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.
4
5 #ifndef CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
7
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"
17
18 class ExtensionService;
19 class HotwordAudioHistoryHandler;
20 class HotwordClient;
21 class Profile;
22
23 namespace extensions {
24 class Extension;
25 class WebstoreStandaloneInstaller;
26 }  // namespace extensions
27
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
35
36 // Provides an interface for the Hotword component that does voice triggered
37 // search.
38 class HotwordService : public extensions::ExtensionRegistryObserver,
39                        public KeyedService {
40  public:
41   // Returns true if the hotword supports the current system language.
42   static bool DoesHotwordSupportLanguage(Profile* profile);
43
44   // Returns true if the "enable-experimental-hotwording" flag is set.
45   static bool IsExperimentalHotwordingEnabled();
46
47   explicit HotwordService(Profile* profile);
48   ~HotwordService() override;
49
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;
57
58   // Checks for whether all the necessary files have downloaded to allow for
59   // using the extension.
60   virtual bool IsServiceAvailable();
61
62   // Determine if hotwording is allowed in this profile based on field trials
63   // and language.
64   virtual bool IsHotwordAllowed();
65
66   // Checks if the user has opted into audio logging. Returns true if the user
67   // is opted in, false otherwise..
68   bool IsOptedIntoAudioLogging();
69
70   // Returns whether always-on hotwording is enabled.
71   bool IsAlwaysOnEnabled();
72
73   // Control the state of the hotword extension.
74   void EnableHotwordExtension(ExtensionService* extension_service);
75   void DisableHotwordExtension(ExtensionService* extension_service);
76
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);
80
81   // Called to handle the hotword session from |client|.
82   void RequestHotwordSession(HotwordClient* client);
83   void StopHotwordSession(HotwordClient* client);
84   HotwordClient* client() { return client_; }
85
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();
90
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();
94
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();
99
100   // Sets the pref value of the previous language.
101   void SetPreviousLanguagePref();
102
103   // Returns the current error message id. A value of 0 indicates
104   // no error.
105   int error_message() { return error_message_; }
106
107   // These methods are for launching, and getting and setting the launch mode of
108   // the Hotword Audio Verification App.
109   //
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.
114   enum LaunchMode {
115     HOTWORD_ONLY,
116     HOTWORD_AND_AUDIO_HISTORY,
117     RETRAIN
118   };
119   void LaunchHotwordAudioVerificationApp(const LaunchMode& launch_mode);
120   virtual LaunchMode GetHotwordAudioVerificationLaunchMode();
121
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();
127   void StopTraining();
128   void NotifyHotwordTriggered();
129
130   // Returns true if speaker training is currently in progress.
131   bool IsTraining();
132
133  private:
134   // Returns the ID of the extension that may need to be reinstalled.
135   std::string ReinstalledExtensionId();
136
137   Profile* profile_;
138
139   PrefChangeRegistrar pref_registrar_;
140
141   content::NotificationRegistrar registrar_;
142
143   // For observing the ExtensionRegistry.
144   ScopedObserver<extensions::ExtensionRegistry,
145                  extensions::ExtensionRegistryObserver>
146       extension_registry_observer_;
147
148   scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
149
150   scoped_ptr<HotwordAudioHistoryHandler> audio_history_handler_;
151
152   HotwordClient* client_;
153   int error_message_;
154   bool reinstall_pending_;
155   // Whether we are currently in the process of training the speaker model.
156   bool training_;
157
158   base::WeakPtrFactory<HotwordService> weak_factory_;
159
160   // Stores the launch mode for the Hotword Audio Verification App.
161   LaunchMode hotword_audio_verification_launch_mode_;
162
163   DISALLOW_COPY_AND_ASSIGN(HotwordService);
164 };
165
166 #endif  // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_