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_FACTORY_H_
      6 #define CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_FACTORY_H_
      7 
      8 #include "base/memory/singleton.h"
      9 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
     10 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
     11 
     12 class HotwordService;
     13 class Profile;
     14 
     15 // Singleton that owns all HotwordServices and associates them with Profiles.
     16 class HotwordServiceFactory : public MediaCaptureDevicesDispatcher::Observer,
     17                               public BrowserContextKeyedServiceFactory {
     18  public:
     19   // Returns the HotwordService for |context|.
     20   static HotwordService* GetForProfile(content::BrowserContext* context);
     21 
     22   static HotwordServiceFactory* GetInstance();
     23 
     24   // Returns true if the hotwording service is available for |context|.
     25   static bool IsServiceAvailable(content::BrowserContext* context);
     26 
     27   // Returns true if hotwording is allowed for |context|.
     28   static bool IsHotwordAllowed(content::BrowserContext* context);
     29 
     30   // Returns the current error message for the service for |context|.
     31   // A value of 0 indicates no error.
     32   static int GetCurrentError(content::BrowserContext* context);
     33 
     34   // Returns the current known state of the microphone. Since this state
     35   // is browser (not profile) specific, it resides in the factory.
     36   static bool IsMicrophoneAvailable();
     37 
     38   // Overridden from MediaCaptureDevicesDispatcher::Observer
     39   virtual void OnUpdateAudioDevices(
     40       const content::MediaStreamDevices& devices) OVERRIDE;
     41 
     42   // This will kick off the monitor that calls OnUpdateAudioDevices when the
     43   // number of audio devices changes (or is initialized). It needs to be a
     44   // separate function so it can be called after the service is initialized
     45   // (i.e., after startup). The monitor can't be initialized during startup
     46   // because it would slow down startup too much so it is delayed and not
     47   // called until it's needed by the webui in browser_options_handler.
     48   void UpdateMicrophoneState();
     49 
     50  private:
     51   friend struct DefaultSingletonTraits<HotwordServiceFactory>;
     52 
     53   HotwordServiceFactory();
     54   virtual ~HotwordServiceFactory();
     55 
     56   // Overrides from BrowserContextKeyedServiceFactory:
     57   virtual void RegisterProfilePrefs(
     58       user_prefs::PrefRegistrySyncable* registry) OVERRIDE;
     59   virtual KeyedService* BuildServiceInstanceFor(
     60       content::BrowserContext* context) const OVERRIDE;
     61 
     62   // Must be called from the UI thread since the instance of
     63   // MediaCaptureDevicesDispatcher can only be accessed on the UI thread.
     64   void InitializeMicrophoneObserver();
     65 
     66   bool microphone_available() { return microphone_available_; }
     67 
     68   bool microphone_available_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(HotwordServiceFactory);
     71 };
     72 
     73 #endif  // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_FACTORY_H_
     74