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 #include "chrome/browser/speech/chrome_speech_recognition_preferences.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_dependency_manager.h"
12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/user_prefs/pref_registry_syncable.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_details.h"
18 using base::ListValue
;
19 using content::BrowserThread
;
22 const bool kDefaultFilterProfanities
= true;
23 const bool kDefaultShownSecurityNotification
= false;
26 ChromeSpeechRecognitionPreferences::Factory
*
27 ChromeSpeechRecognitionPreferences::Factory::GetInstance() {
28 return Singleton
<ChromeSpeechRecognitionPreferences::Factory
>::get();
31 void ChromeSpeechRecognitionPreferences::InitializeFactory() {
32 ChromeSpeechRecognitionPreferences::Factory::GetInstance();
35 scoped_refptr
<ChromeSpeechRecognitionPreferences
>
36 ChromeSpeechRecognitionPreferences::Factory::GetForProfile(Profile
* profile
) {
38 // GetServiceForProfile will let us instantiate a new (if not already cached
39 // for the profile) Service through BuildServiceInstanceFor method.
40 ChromeSpeechRecognitionPreferences::Service
* service
=
41 static_cast<ChromeSpeechRecognitionPreferences::Service
*>(
42 GetServiceForProfile(profile
, true));
45 // Incognito won't have this service.
49 return service
->GetPreferences();
52 ChromeSpeechRecognitionPreferences::Factory::Factory()
53 : ProfileKeyedServiceFactory(
54 "ChromeSpeechRecognitionPreferences",
55 ProfileDependencyManager::GetInstance()) {
58 ChromeSpeechRecognitionPreferences::Factory::~Factory() {
62 ChromeSpeechRecognitionPreferences::Factory::BuildServiceInstanceFor(
63 Profile
* profile
) const {
65 return new ChromeSpeechRecognitionPreferences::Service(profile
);
68 void ChromeSpeechRecognitionPreferences::Factory::RegisterUserPrefs(
69 PrefRegistrySyncable
* prefs
) {
70 prefs
->RegisterBooleanPref(
71 prefs::kSpeechRecognitionFilterProfanities
,
72 kDefaultFilterProfanities
,
73 PrefRegistrySyncable::UNSYNCABLE_PREF
);
75 prefs
->RegisterListPref(
76 prefs::kSpeechRecognitionTrayNotificationShownContexts
,
77 PrefRegistrySyncable::UNSYNCABLE_PREF
);
80 bool ChromeSpeechRecognitionPreferences::Factory::
81 ServiceRedirectedInIncognito() const {
85 bool ChromeSpeechRecognitionPreferences::Factory::
86 ServiceIsNULLWhileTesting() const {
90 bool ChromeSpeechRecognitionPreferences::Factory::
91 ServiceIsCreatedWithProfile() const {
95 ChromeSpeechRecognitionPreferences::Service::Service(
97 : preferences_(new ChromeSpeechRecognitionPreferences(profile
)) {
100 ChromeSpeechRecognitionPreferences::Service::~Service() {
103 void ChromeSpeechRecognitionPreferences::Service::Shutdown() {
104 DCHECK(preferences_
);
105 preferences_
->DetachFromProfile();
108 scoped_refptr
<ChromeSpeechRecognitionPreferences
>
109 ChromeSpeechRecognitionPreferences::Service::GetPreferences() const {
113 scoped_refptr
<ChromeSpeechRecognitionPreferences
>
114 ChromeSpeechRecognitionPreferences::GetForProfile(Profile
* profile
) {
115 scoped_refptr
<ChromeSpeechRecognitionPreferences
> ret
;
117 // Note that when in incognito, GetForProfile will return NULL.
118 // We catch that case below and return the default preferences.
119 ret
= Factory::GetInstance()->GetForProfile(profile
);
123 // Create a detached preferences object if no profile is provided.
124 ret
= new ChromeSpeechRecognitionPreferences(NULL
);
130 ChromeSpeechRecognitionPreferences::ChromeSpeechRecognitionPreferences(
133 pref_change_registrar_(new PrefChangeRegistrar()),
134 filter_profanities_(kDefaultFilterProfanities
),
135 notifications_shown_(new ListValue()) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
140 pref_change_registrar_
->Init(profile_
->GetPrefs());
142 ReloadFilterProfanities();
143 pref_change_registrar_
->Add(
144 prefs::kSpeechRecognitionFilterProfanities
,
145 base::Bind(&ChromeSpeechRecognitionPreferences::ReloadFilterProfanities
,
146 base::Unretained(this)));
148 ReloadNotificationsShown();
149 pref_change_registrar_
->Add(
150 prefs::kSpeechRecognitionTrayNotificationShownContexts
,
151 base::Bind(&ChromeSpeechRecognitionPreferences::ReloadNotificationsShown
,
152 base::Unretained(this)));
155 ChromeSpeechRecognitionPreferences::~ChromeSpeechRecognitionPreferences() {
158 void ChromeSpeechRecognitionPreferences::DetachFromProfile() {
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
161 pref_change_registrar_
.reset();
165 bool ChromeSpeechRecognitionPreferences::FilterProfanities() const {
166 base::AutoLock
read_lock(preferences_lock_
);
167 return filter_profanities_
;
170 void ChromeSpeechRecognitionPreferences::SetFilterProfanities(
171 bool filter_profanities
) {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
174 base::AutoLock
write_lock(preferences_lock_
);
175 filter_profanities_
= filter_profanities
;
178 profile_
->GetPrefs()->SetBoolean(prefs::kSpeechRecognitionFilterProfanities
,
183 void ChromeSpeechRecognitionPreferences::ToggleFilterProfanities() {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
185 SetFilterProfanities(!FilterProfanities());
188 bool ChromeSpeechRecognitionPreferences::ShouldShowSecurityNotification(
189 const std::string
& context_name
) const {
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
191 DCHECK(notifications_shown_
.get());
192 scoped_ptr
<base::StringValue
> match_name(
193 base::Value::CreateStringValue(context_name
));
194 return notifications_shown_
->Find(*match_name
) == notifications_shown_
->end();
197 void ChromeSpeechRecognitionPreferences::SetHasShownSecurityNotification(
198 const std::string
& context_name
) {
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
200 DCHECK(notifications_shown_
.get());
201 notifications_shown_
->AppendIfNotPresent(
202 base::Value::CreateStringValue(context_name
));
204 profile_
->GetPrefs()->Set(
205 prefs::kSpeechRecognitionTrayNotificationShownContexts
,
206 *notifications_shown_
);
210 void ChromeSpeechRecognitionPreferences::ReloadFilterProfanities() {
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
212 base::AutoLock
write_lock(preferences_lock_
);
213 filter_profanities_
= profile_
->GetPrefs()->GetBoolean(
214 prefs::kSpeechRecognitionFilterProfanities
);
217 void ChromeSpeechRecognitionPreferences::ReloadNotificationsShown() {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
219 base::AutoLock
write_lock(preferences_lock_
);
220 const base::ListValue
* pref_list
= profile_
->GetPrefs()->GetList(
221 prefs::kSpeechRecognitionTrayNotificationShownContexts
);
222 notifications_shown_
.reset(pref_list
->DeepCopy());