hotword_service.h revision 116680a4aac90f2aa7413d9095a592090648e557
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
18class ExtensionService;
19class HotwordClient;
20class Profile;
21
22namespace extensions {
23class Extension;
24class WebstoreStandaloneInstaller;
25}  // namespace extensions
26
27namespace hotword_internal {
28// Constants for the hotword field trial.
29extern const char kHotwordFieldTrialName[];
30extern const char kHotwordFieldTrialDisabledGroupName[];
31}  // namespace hotword_internal
32
33// Provides an interface for the Hotword component that does voice triggered
34// search.
35class HotwordService : public content::NotificationObserver,
36                       public extensions::ExtensionRegistryObserver,
37                       public KeyedService {
38 public:
39  // Returns true if the hotword supports the current system language.
40  static bool DoesHotwordSupportLanguage(Profile* profile);
41
42  explicit HotwordService(Profile* profile);
43  virtual ~HotwordService();
44
45  // Overridden from content::NotificationObserver:
46  virtual void Observe(int type,
47                       const content::NotificationSource& source,
48                       const content::NotificationDetails& details) OVERRIDE;
49
50  // Overridden from ExtensionRegisterObserver:
51  virtual void OnExtensionInstalled(
52      content::BrowserContext* browser_context,
53      const extensions::Extension* extension,
54      bool is_update) OVERRIDE;
55  virtual void OnExtensionUninstalled(
56      content::BrowserContext* browser_context,
57      const extensions::Extension* extension) OVERRIDE;
58
59  // Checks for whether all the necessary files have downloaded to allow for
60  // using the extension.
61  virtual bool IsServiceAvailable();
62
63  // Determine if hotwording is allowed in this profile based on field trials
64  // and language.
65  virtual bool IsHotwordAllowed();
66
67  // Checks if the user has opted into audio logging. Returns true if the user
68  // is opted in, false otherwise..
69  bool IsOptedIntoAudioLogging();
70
71  // Control the state of the hotword extension.
72  void EnableHotwordExtension(ExtensionService* extension_service);
73  void DisableHotwordExtension(ExtensionService* extension_service);
74
75  // Handles enabling/disabling the hotword extension when the user
76  // turns it off via the settings menu.
77  void OnHotwordSearchEnabledChanged(const std::string& pref_name);
78
79  // Called to handle the hotword session from |client|.
80  void RequestHotwordSession(HotwordClient* client);
81  void StopHotwordSession(HotwordClient* client);
82  HotwordClient* client() { return client_; }
83
84  // Checks if the current version of the hotword extension should be
85  // uninstalled in order to update to a different language version.
86  // Returns true if the extension was uninstalled.
87  bool MaybeReinstallHotwordExtension();
88
89  // Checks based on locale if the current version should be uninstalled so that
90  // a version with a different language can be installed.
91  bool ShouldReinstallHotwordExtension();
92
93  // Helper functions pulled out for testing purposes.
94  // UninstallHotwordExtension returns true if the extension was uninstalled.
95  virtual bool UninstallHotwordExtension(ExtensionService* extension_service);
96  virtual void InstallHotwordExtensionFromWebstore();
97
98  // Sets the pref value of the previous language.
99  void SetPreviousLanguagePref();
100
101  // Returns the current error message id. A value of 0 indicates
102  // no error.
103  int error_message() { return error_message_; }
104
105 private:
106  Profile* profile_;
107
108  PrefChangeRegistrar pref_registrar_;
109
110  content::NotificationRegistrar registrar_;
111
112  // For observing the ExtensionRegistry.
113  ScopedObserver<extensions::ExtensionRegistry,
114                 extensions::ExtensionRegistryObserver>
115      extension_registry_observer_;
116
117  scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
118
119  HotwordClient* client_;
120  int error_message_;
121  bool reinstall_pending_;
122
123  base::WeakPtrFactory<HotwordService> weak_factory_;
124
125  DISALLOW_COPY_AND_ASSIGN(HotwordService);
126};
127
128#endif  // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
129