Home | History | Annotate | Download | only in search
      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 HotwordClient;
     20 class Profile;
     21 
     22 namespace extensions {
     23 class Extension;
     24 class WebstoreStandaloneInstaller;
     25 }  // namespace extensions
     26 
     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
     32 
     33 // Provides an interface for the Hotword component that does voice triggered
     34 // search.
     35 class 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) OVERRIDE;
     54   virtual void OnExtensionUninstalled(
     55       content::BrowserContext* browser_context,
     56       const extensions::Extension* extension) 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   // Control the state of the hotword extension.
     71   void EnableHotwordExtension(ExtensionService* extension_service);
     72   void DisableHotwordExtension(ExtensionService* extension_service);
     73 
     74   // Handles enabling/disabling the hotword extension when the user
     75   // turns it off via the settings menu.
     76   void OnHotwordSearchEnabledChanged(const std::string& pref_name);
     77 
     78   // Called to handle the hotword session from |client|.
     79   void RequestHotwordSession(HotwordClient* client);
     80   void StopHotwordSession(HotwordClient* client);
     81   HotwordClient* client() { return client_; }
     82 
     83   // Checks if the current version of the hotword extension should be
     84   // uninstalled in order to update to a different language version.
     85   // Returns true if the extension was uninstalled.
     86   bool MaybeReinstallHotwordExtension();
     87 
     88   // Checks based on locale if the current version should be uninstalled so that
     89   // a version with a different language can be installed.
     90   bool ShouldReinstallHotwordExtension();
     91 
     92   // Helper functions pulled out for testing purposes.
     93   // UninstallHotwordExtension returns true if the extension was uninstalled.
     94   virtual bool UninstallHotwordExtension(ExtensionService* extension_service);
     95   virtual void InstallHotwordExtensionFromWebstore();
     96 
     97   // Sets the pref value of the previous language.
     98   void SetPreviousLanguagePref();
     99 
    100   // Returns the current error message id. A value of 0 indicates
    101   // no error.
    102   int error_message() { return error_message_; }
    103 
    104  private:
    105   Profile* profile_;
    106 
    107   PrefChangeRegistrar pref_registrar_;
    108 
    109   content::NotificationRegistrar registrar_;
    110 
    111   // For observing the ExtensionRegistry.
    112   ScopedObserver<extensions::ExtensionRegistry,
    113                  extensions::ExtensionRegistryObserver>
    114       extension_registry_observer_;
    115 
    116   scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
    117 
    118   HotwordClient* client_;
    119   int error_message_;
    120   bool reinstall_pending_;
    121 
    122   base::WeakPtrFactory<HotwordService> weak_factory_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(HotwordService);
    125 };
    126 
    127 #endif  // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
    128