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