summaryrefslogtreecommitdiffstats
path: root/chrome/browser/speech/chrome_speech_recognition_preferences.cc
blob: d25cbc186158e9e0c10d7508b3db2c12c21f8ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/speech/chrome_speech_recognition_preferences.h"

#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_dependency_manager.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"

using base::ListValue;
using content::BrowserThread;

namespace {
const bool kDefaultFilterProfanities = true;
const bool kDefaultShownSecurityNotification = false;
}

ChromeSpeechRecognitionPreferences::Factory*
ChromeSpeechRecognitionPreferences::Factory::GetInstance() {
  return Singleton<ChromeSpeechRecognitionPreferences::Factory>::get();
}

void ChromeSpeechRecognitionPreferences::InitializeFactory() {
  ChromeSpeechRecognitionPreferences::Factory::GetInstance();
}

scoped_refptr<ChromeSpeechRecognitionPreferences>
ChromeSpeechRecognitionPreferences::Factory::GetForProfile(Profile* profile) {
  DCHECK(profile);
  // GetServiceForProfile will let us instantiate a new (if not already cached
  // for the profile) Service through BuildServiceInstanceFor method.
  ChromeSpeechRecognitionPreferences::Service* service =
      static_cast<ChromeSpeechRecognitionPreferences::Service*>(
          GetServiceForProfile(profile, true));

  if (!service) {
    // Incognito won't have this service.
    return NULL;
  }

  return service->GetPreferences();
}

ChromeSpeechRecognitionPreferences::Factory::Factory()
    : ProfileKeyedServiceFactory(
        "ChromeSpeechRecognitionPreferences",
        ProfileDependencyManager::GetInstance()) {
}

ChromeSpeechRecognitionPreferences::Factory::~Factory() {
}

ProfileKeyedService*
ChromeSpeechRecognitionPreferences::Factory::BuildServiceInstanceFor(
    content::BrowserContext* profile) const {
  DCHECK(profile);
  return new ChromeSpeechRecognitionPreferences::Service(
      static_cast<Profile*>(profile));
}

void ChromeSpeechRecognitionPreferences::Factory::RegisterUserPrefs(
    user_prefs::PrefRegistrySyncable* prefs) {
  prefs->RegisterBooleanPref(
      prefs::kSpeechRecognitionFilterProfanities,
      kDefaultFilterProfanities,
      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);

  prefs->RegisterListPref(
      prefs::kSpeechRecognitionTrayNotificationShownContexts,
      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}

bool ChromeSpeechRecognitionPreferences::Factory::
ServiceIsNULLWhileTesting() const {
  return true;
}

bool ChromeSpeechRecognitionPreferences::Factory::
ServiceIsCreatedWithProfile() const {
  return false;
}

ChromeSpeechRecognitionPreferences::Service::Service(
    Profile* profile)
    : preferences_(new ChromeSpeechRecognitionPreferences(profile)) {
}

ChromeSpeechRecognitionPreferences::Service::~Service() {
}

void ChromeSpeechRecognitionPreferences::Service::Shutdown() {
  DCHECK(preferences_);
  preferences_->DetachFromProfile();
}

scoped_refptr<ChromeSpeechRecognitionPreferences>
ChromeSpeechRecognitionPreferences::Service::GetPreferences() const {
  return preferences_;
}

scoped_refptr<ChromeSpeechRecognitionPreferences>
ChromeSpeechRecognitionPreferences::GetForProfile(Profile* profile) {
  scoped_refptr<ChromeSpeechRecognitionPreferences> ret;
  if (profile) {
    // Note that when in incognito, GetForProfile will return NULL.
    // We catch that case below and return the default preferences.
    ret = Factory::GetInstance()->GetForProfile(profile);
  }

  if (!ret) {
    // Create a detached preferences object if no profile is provided.
    ret = new ChromeSpeechRecognitionPreferences(NULL);
  }

  return ret;
}

ChromeSpeechRecognitionPreferences::ChromeSpeechRecognitionPreferences(
    Profile* profile)
    : profile_(profile),
      pref_change_registrar_(new PrefChangeRegistrar()),
      filter_profanities_(kDefaultFilterProfanities),
      notifications_shown_(new ListValue()) {
  if (!profile_)
    return;

  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  pref_change_registrar_->Init(profile_->GetPrefs());

  ReloadFilterProfanities();
  pref_change_registrar_->Add(
      prefs::kSpeechRecognitionFilterProfanities,
      base::Bind(&ChromeSpeechRecognitionPreferences::ReloadFilterProfanities,
                 base::Unretained(this)));

  ReloadNotificationsShown();
  pref_change_registrar_->Add(
      prefs::kSpeechRecognitionTrayNotificationShownContexts,
      base::Bind(&ChromeSpeechRecognitionPreferences::ReloadNotificationsShown,
                 base::Unretained(this)));
}

ChromeSpeechRecognitionPreferences::~ChromeSpeechRecognitionPreferences() {
}

void ChromeSpeechRecognitionPreferences::DetachFromProfile() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(profile_);
  pref_change_registrar_.reset();
  profile_ = NULL;
}

bool ChromeSpeechRecognitionPreferences::FilterProfanities() const {
  base::AutoLock read_lock(preferences_lock_);
  return filter_profanities_;
}

void ChromeSpeechRecognitionPreferences::SetFilterProfanities(
    bool filter_profanities) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  {
    base::AutoLock write_lock(preferences_lock_);
    filter_profanities_ = filter_profanities;
  }
  if (profile_) {
    profile_->GetPrefs()->SetBoolean(prefs::kSpeechRecognitionFilterProfanities,
                                     filter_profanities);
  }
}

void ChromeSpeechRecognitionPreferences::ToggleFilterProfanities() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SetFilterProfanities(!FilterProfanities());
}

bool ChromeSpeechRecognitionPreferences::ShouldShowSecurityNotification(
    const std::string& context_name) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(notifications_shown_.get());
  scoped_ptr<base::StringValue> match_name(
      base::Value::CreateStringValue(context_name));
  return notifications_shown_->Find(*match_name) == notifications_shown_->end();
}

void ChromeSpeechRecognitionPreferences::SetHasShownSecurityNotification(
    const std::string& context_name) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(notifications_shown_.get());
  notifications_shown_->AppendIfNotPresent(
      base::Value::CreateStringValue(context_name));
  if (profile_) {
    profile_->GetPrefs()->Set(
        prefs::kSpeechRecognitionTrayNotificationShownContexts,
        *notifications_shown_);
  }
}

void ChromeSpeechRecognitionPreferences::ReloadFilterProfanities() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  base::AutoLock write_lock(preferences_lock_);
  filter_profanities_ = profile_->GetPrefs()->GetBoolean(
      prefs::kSpeechRecognitionFilterProfanities);
}

void ChromeSpeechRecognitionPreferences::ReloadNotificationsShown() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  base::AutoLock write_lock(preferences_lock_);
  const base::ListValue* pref_list = profile_->GetPrefs()->GetList(
      prefs::kSpeechRecognitionTrayNotificationShownContexts);
  notifications_shown_.reset(pref_list->DeepCopy());
}