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/values.h"
9 #include "chrome/browser/prefs/pref_service.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 "content/public/browser/browser_thread.h"
15 #include "content/public/browser/notification_details.h"
17 using base::ListValue
;
18 using content::BrowserThread
;
21 const bool kDefaultFilterProfanities
= true;
22 const bool kDefaultShownSecurityNotification
= false;
25 ChromeSpeechRecognitionPreferences::Factory
*
26 ChromeSpeechRecognitionPreferences::Factory::GetInstance() {
27 return Singleton
<ChromeSpeechRecognitionPreferences::Factory
>::get();
30 void ChromeSpeechRecognitionPreferences::InitializeFactory() {
31 ChromeSpeechRecognitionPreferences::Factory::GetInstance();
34 scoped_refptr
<ChromeSpeechRecognitionPreferences
>
35 ChromeSpeechRecognitionPreferences::Factory::GetForProfile(Profile
* profile
) {
37 // GetServiceForProfile will let us instantiate a new (if not already cached
38 // for the profile) Service through BuildServiceInstanceFor method.
39 return static_cast<ChromeSpeechRecognitionPreferences::Service
*>(
40 GetServiceForProfile(profile
, true))->GetPreferences();
43 ChromeSpeechRecognitionPreferences::Factory::Factory()
44 : ProfileKeyedServiceFactory(
45 "ChromeSpeechRecognitionPreferences",
46 ProfileDependencyManager::GetInstance()) {
49 ChromeSpeechRecognitionPreferences::Factory::~Factory() {
53 ChromeSpeechRecognitionPreferences::Factory::BuildServiceInstanceFor(
54 Profile
* profile
) const {
56 return new ChromeSpeechRecognitionPreferences::Service(profile
);
59 void ChromeSpeechRecognitionPreferences::Factory::RegisterUserPrefs(
61 prefs
->RegisterBooleanPref(
62 prefs::kSpeechRecognitionFilterProfanities
,
63 kDefaultFilterProfanities
,
64 PrefService::UNSYNCABLE_PREF
);
66 prefs
->RegisterListPref(
67 prefs::kSpeechRecognitionTrayNotificationShownContexts
,
68 PrefService::UNSYNCABLE_PREF
);
71 bool ChromeSpeechRecognitionPreferences::Factory::
72 ServiceRedirectedInIncognito() const {
76 bool ChromeSpeechRecognitionPreferences::Factory::
77 ServiceIsNULLWhileTesting() const {
81 bool ChromeSpeechRecognitionPreferences::Factory::
82 ServiceIsCreatedWithProfile() const {
86 ChromeSpeechRecognitionPreferences::Service::Service(
88 : preferences_(new ChromeSpeechRecognitionPreferences(profile
)) {
91 ChromeSpeechRecognitionPreferences::Service::~Service() {
94 void ChromeSpeechRecognitionPreferences::Service::Shutdown() {
96 preferences_
->DetachFromProfile();
99 scoped_refptr
<ChromeSpeechRecognitionPreferences
>
100 ChromeSpeechRecognitionPreferences::Service::GetPreferences() const {
104 scoped_refptr
<ChromeSpeechRecognitionPreferences
>
105 ChromeSpeechRecognitionPreferences::GetForProfile(Profile
* profile
) {
107 return Factory::GetInstance()->GetForProfile(profile
);
109 // Create a detached preferences object if no profile is provided.
110 return new ChromeSpeechRecognitionPreferences(NULL
);
114 ChromeSpeechRecognitionPreferences::ChromeSpeechRecognitionPreferences(
117 pref_change_registrar_(new PrefChangeRegistrar()),
118 filter_profanities_(kDefaultFilterProfanities
),
119 notifications_shown_(new ListValue()) {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
124 pref_change_registrar_
->Init(profile_
->GetPrefs());
126 ReloadPreference(prefs::kSpeechRecognitionFilterProfanities
);
127 pref_change_registrar_
->Add(prefs::kSpeechRecognitionFilterProfanities
, this);
129 ReloadPreference(prefs::kSpeechRecognitionTrayNotificationShownContexts
);
130 pref_change_registrar_
->Add(
131 prefs::kSpeechRecognitionTrayNotificationShownContexts
, this);
134 ChromeSpeechRecognitionPreferences::~ChromeSpeechRecognitionPreferences() {
137 void ChromeSpeechRecognitionPreferences::DetachFromProfile() {
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
140 pref_change_registrar_
.reset();
144 void ChromeSpeechRecognitionPreferences::OnPreferenceChanged(
145 PrefServiceBase
* service
,
146 const std::string
& pref_name
) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
148 ReloadPreference(pref_name
);
151 bool ChromeSpeechRecognitionPreferences::FilterProfanities() const {
152 base::AutoLock
read_lock(preferences_lock_
);
153 return filter_profanities_
;
156 void ChromeSpeechRecognitionPreferences::SetFilterProfanities(
157 bool filter_profanities
) {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
160 base::AutoLock
write_lock(preferences_lock_
);
161 filter_profanities_
= filter_profanities
;
164 profile_
->GetPrefs()->SetBoolean(prefs::kSpeechRecognitionFilterProfanities
,
169 void ChromeSpeechRecognitionPreferences::ToggleFilterProfanities() {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
171 SetFilterProfanities(!FilterProfanities());
174 bool ChromeSpeechRecognitionPreferences::ShouldShowSecurityNotification(
175 const std::string
& context_name
) const {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
177 DCHECK(notifications_shown_
.get());
178 scoped_ptr
<base::StringValue
> match_name(
179 base::Value::CreateStringValue(context_name
));
180 return notifications_shown_
->Find(*match_name
) == notifications_shown_
->end();
183 void ChromeSpeechRecognitionPreferences::SetHasShownSecurityNotification(
184 const std::string
& context_name
) {
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
186 DCHECK(notifications_shown_
.get());
187 notifications_shown_
->AppendIfNotPresent(
188 base::Value::CreateStringValue(context_name
));
190 profile_
->GetPrefs()->Set(
191 prefs::kSpeechRecognitionTrayNotificationShownContexts
,
192 *notifications_shown_
);
196 void ChromeSpeechRecognitionPreferences::ReloadPreference(
197 const std::string
& key
) {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
200 base::AutoLock
write_lock(preferences_lock_
);
201 PrefService
* pref_service
= profile_
->GetPrefs();
202 if (key
== prefs::kSpeechRecognitionFilterProfanities
) {
203 filter_profanities_
=
204 pref_service
->GetBoolean(prefs::kSpeechRecognitionFilterProfanities
);
205 } else if (key
== prefs::kSpeechRecognitionTrayNotificationShownContexts
) {
206 const base::ListValue
* pref_list
= profile_
->GetPrefs()->GetList(
207 prefs::kSpeechRecognitionTrayNotificationShownContexts
);
209 notifications_shown_
.reset(pref_list
->DeepCopy());