Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / speech / chrome_speech_recognition_preferences.cc
blobfc057fe6aff632b0a2fdf646dabde4bcfc1c39b6
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"
7 #include "base/bind.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;
20 namespace {
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) {
36 DCHECK(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() {
52 ProfileKeyedService*
53 ChromeSpeechRecognitionPreferences::Factory::BuildServiceInstanceFor(
54 Profile* profile) const {
55 DCHECK(profile);
56 return new ChromeSpeechRecognitionPreferences::Service(profile);
59 void ChromeSpeechRecognitionPreferences::Factory::RegisterUserPrefs(
60 PrefService* prefs) {
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 {
73 return false;
76 bool ChromeSpeechRecognitionPreferences::Factory::
77 ServiceIsNULLWhileTesting() const {
78 return true;
81 bool ChromeSpeechRecognitionPreferences::Factory::
82 ServiceIsCreatedWithProfile() const {
83 return false;
86 ChromeSpeechRecognitionPreferences::Service::Service(
87 Profile* profile)
88 : preferences_(new ChromeSpeechRecognitionPreferences(profile)) {
91 ChromeSpeechRecognitionPreferences::Service::~Service() {
94 void ChromeSpeechRecognitionPreferences::Service::Shutdown() {
95 DCHECK(preferences_);
96 preferences_->DetachFromProfile();
99 scoped_refptr<ChromeSpeechRecognitionPreferences>
100 ChromeSpeechRecognitionPreferences::Service::GetPreferences() const {
101 return preferences_;
104 scoped_refptr<ChromeSpeechRecognitionPreferences>
105 ChromeSpeechRecognitionPreferences::GetForProfile(Profile* profile) {
106 if (profile) {
107 return Factory::GetInstance()->GetForProfile(profile);
108 } else {
109 // Create a detached preferences object if no profile is provided.
110 return new ChromeSpeechRecognitionPreferences(NULL);
114 ChromeSpeechRecognitionPreferences::ChromeSpeechRecognitionPreferences(
115 Profile* profile)
116 : profile_(profile),
117 pref_change_registrar_(new PrefChangeRegistrar()),
118 filter_profanities_(kDefaultFilterProfanities),
119 notifications_shown_(new ListValue()) {
120 if (!profile_)
121 return;
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));
139 DCHECK(profile_);
140 pref_change_registrar_.reset();
141 profile_ = NULL;
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;
163 if (profile_) {
164 profile_->GetPrefs()->SetBoolean(prefs::kSpeechRecognitionFilterProfanities,
165 filter_profanities);
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));
189 if (profile_) {
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));
199 DCHECK(profile_);
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);
208 DCHECK(pref_list);
209 notifications_shown_.reset(pref_list->DeepCopy());