/chromium-webcl/src/chrome/browser/speech/chrome_speech_recognition_preferences.h

https://bitbucket.org/peixuan/chromium_r197479_base · C Header · 123 lines · 69 code · 25 blank · 29 comment · 0 complexity · 32caf8c7ad765142ac9202a226fcec66 MD5 · raw file

  1. // Copyright (c) 2012 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. #ifndef CHROME_BROWSER_SPEECH_CHROME_SPEECH_RECOGNITION_PREFERENCES_H_
  5. #define CHROME_BROWSER_SPEECH_CHROME_SPEECH_RECOGNITION_PREFERENCES_H_
  6. #include "base/basictypes.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/scoped_ptr.h"
  9. #include "base/memory/singleton.h"
  10. #include "base/prefs/pref_change_registrar.h"
  11. #include "base/synchronization/lock.h"
  12. #include "base/threading/non_thread_safe.h"
  13. #include "chrome/browser/profiles/profile_keyed_service.h"
  14. #include "chrome/browser/profiles/profile_keyed_service_factory.h"
  15. #include "content/public/browser/speech_recognition_preferences.h"
  16. class PrefRegistrySyncable;
  17. class PrefService;
  18. namespace base {
  19. class ListValue;
  20. }
  21. // Utility class that handles persistent storage of speech recognition
  22. // preferences. Instances of this class must be obtained exclusively through the
  23. // method ChromeSpeechRecognitionPreferences::GetForProfile(...), so that
  24. // preferences are handled within the Profile (if not NULL). The internal
  25. // factory class also handles ownership and lifetime of the
  26. // ChromeSpeechRecognitionPreferences instances (through the inner Service
  27. // class), allowing them to be used in a detached mode (no persistent storage)
  28. // even when a Profile is not available (or has been shutdown).
  29. class ChromeSpeechRecognitionPreferences
  30. : public content::SpeechRecognitionPreferences {
  31. public:
  32. static void InitializeFactory();
  33. static scoped_refptr<ChromeSpeechRecognitionPreferences> GetForProfile(
  34. Profile* profile);
  35. // content::SpeechRecognitionPreferences implementation.
  36. // Called by both Content (on IO thread) and Chrome (on UI thread).
  37. virtual bool FilterProfanities() const OVERRIDE;
  38. // Called only by Chrome (on UI thread).
  39. void SetFilterProfanities(bool filter_profanities);
  40. void ToggleFilterProfanities();
  41. bool ShouldShowSecurityNotification(const std::string& context_name) const;
  42. void SetHasShownSecurityNotification(const std::string& context_name);
  43. private:
  44. // The two classes below are needed to handle storage of speech recognition
  45. // preferences in profile preferences, according to the Chromium Profile
  46. // Architecture document entitled "The New Way: ProfileKeyedServiceFactory".
  47. // Singleton that manages instantiation of ChromeSpeechRecognitionPreferences
  48. // handling its association with Profiles.
  49. class Factory : public ProfileKeyedServiceFactory {
  50. public:
  51. static Factory* GetInstance();
  52. scoped_refptr<ChromeSpeechRecognitionPreferences> GetForProfile(
  53. Profile* profile);
  54. private:
  55. friend struct DefaultSingletonTraits<Factory>;
  56. Factory();
  57. virtual ~Factory();
  58. // ProfileKeyedServiceFactory methods:
  59. virtual ProfileKeyedService* BuildServiceInstanceFor(
  60. content::BrowserContext* profile) const OVERRIDE;
  61. virtual void RegisterUserPrefs(PrefRegistrySyncable* registry) OVERRIDE;
  62. virtual bool ServiceRedirectedInIncognito() const OVERRIDE;
  63. virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
  64. virtual bool ServiceIsCreatedWithProfile() const OVERRIDE;
  65. DISALLOW_COPY_AND_ASSIGN(Factory);
  66. };
  67. // This wrapper handles the binding between ChromeSpeechRecognitionPreferences
  68. // instances (which can have a longer lifetime, since they are refcounted) and
  69. // ProfileKeyedService (which lifetime depends on the Profile service). Upon
  70. // profile shutdown, the ChromeSpeechRecognitionPreferences instance is
  71. // detached from profile, meaning that after that point its clients can still
  72. // use it, but preferences will no longer be kept in sync with the profile.
  73. class Service : public ProfileKeyedService {
  74. public:
  75. explicit Service(Profile* profile);
  76. virtual ~Service();
  77. // ProfileKeyedService implementation.
  78. virtual void Shutdown() OVERRIDE;
  79. scoped_refptr<ChromeSpeechRecognitionPreferences> GetPreferences() const;
  80. private:
  81. scoped_refptr<ChromeSpeechRecognitionPreferences> preferences_;
  82. DISALLOW_COPY_AND_ASSIGN(Service);
  83. };
  84. explicit ChromeSpeechRecognitionPreferences(Profile* profile);
  85. virtual ~ChromeSpeechRecognitionPreferences();
  86. void DetachFromProfile();
  87. void ReloadFilterProfanities();
  88. void ReloadNotificationsShown();
  89. Profile* profile_; // NULL when detached.
  90. scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
  91. bool filter_profanities_;
  92. scoped_ptr<base::ListValue> notifications_shown_;
  93. // Lock used to ensure exclusive access to preference variables that are
  94. // accessed by both threads (note: mutable is required to keep getters const).
  95. mutable base::Lock preferences_lock_;
  96. DISALLOW_COPY_AND_ASSIGN(ChromeSpeechRecognitionPreferences);
  97. };
  98. #endif // CHROME_BROWSER_SPEECH_CHROME_SPEECH_RECOGNITION_PREFERENCES_H_