/chromium-webcl/src/chrome/browser/speech/chrome_speech_recognition_preferences.cc

https://bitbucket.org/peixuan/chromium_r197479_base · C++ · 224 lines · 178 code · 37 blank · 9 comment · 7 complexity · 78d43117b31958600d445f12ac996c6e MD5 · raw file

  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.
  4. #include "chrome/browser/speech/chrome_speech_recognition_preferences.h"
  5. #include "base/bind.h"
  6. #include "base/prefs/pref_service.h"
  7. #include "base/values.h"
  8. #include "chrome/browser/profiles/profile.h"
  9. #include "chrome/browser/profiles/profile_dependency_manager.h"
  10. #include "chrome/common/chrome_notification_types.h"
  11. #include "chrome/common/pref_names.h"
  12. #include "components/user_prefs/pref_registry_syncable.h"
  13. #include "content/public/browser/browser_thread.h"
  14. #include "content/public/browser/notification_details.h"
  15. using base::ListValue;
  16. using content::BrowserThread;
  17. namespace {
  18. const bool kDefaultFilterProfanities = true;
  19. const bool kDefaultShownSecurityNotification = false;
  20. }
  21. ChromeSpeechRecognitionPreferences::Factory*
  22. ChromeSpeechRecognitionPreferences::Factory::GetInstance() {
  23. return Singleton<ChromeSpeechRecognitionPreferences::Factory>::get();
  24. }
  25. void ChromeSpeechRecognitionPreferences::InitializeFactory() {
  26. ChromeSpeechRecognitionPreferences::Factory::GetInstance();
  27. }
  28. scoped_refptr<ChromeSpeechRecognitionPreferences>
  29. ChromeSpeechRecognitionPreferences::Factory::GetForProfile(Profile* profile) {
  30. DCHECK(profile);
  31. // GetServiceForProfile will let us instantiate a new (if not already cached
  32. // for the profile) Service through BuildServiceInstanceFor method.
  33. ChromeSpeechRecognitionPreferences::Service* service =
  34. static_cast<ChromeSpeechRecognitionPreferences::Service*>(
  35. GetServiceForProfile(profile, true));
  36. if (!service) {
  37. // Incognito won't have this service.
  38. return NULL;
  39. }
  40. return service->GetPreferences();
  41. }
  42. ChromeSpeechRecognitionPreferences::Factory::Factory()
  43. : ProfileKeyedServiceFactory(
  44. "ChromeSpeechRecognitionPreferences",
  45. ProfileDependencyManager::GetInstance()) {
  46. }
  47. ChromeSpeechRecognitionPreferences::Factory::~Factory() {
  48. }
  49. ProfileKeyedService*
  50. ChromeSpeechRecognitionPreferences::Factory::BuildServiceInstanceFor(
  51. content::BrowserContext* profile) const {
  52. DCHECK(profile);
  53. return new ChromeSpeechRecognitionPreferences::Service(
  54. static_cast<Profile*>(profile));
  55. }
  56. void ChromeSpeechRecognitionPreferences::Factory::RegisterUserPrefs(
  57. PrefRegistrySyncable* prefs) {
  58. prefs->RegisterBooleanPref(
  59. prefs::kSpeechRecognitionFilterProfanities,
  60. kDefaultFilterProfanities,
  61. PrefRegistrySyncable::UNSYNCABLE_PREF);
  62. prefs->RegisterListPref(
  63. prefs::kSpeechRecognitionTrayNotificationShownContexts,
  64. PrefRegistrySyncable::UNSYNCABLE_PREF);
  65. }
  66. bool ChromeSpeechRecognitionPreferences::Factory::
  67. ServiceRedirectedInIncognito() const {
  68. return false;
  69. }
  70. bool ChromeSpeechRecognitionPreferences::Factory::
  71. ServiceIsNULLWhileTesting() const {
  72. return true;
  73. }
  74. bool ChromeSpeechRecognitionPreferences::Factory::
  75. ServiceIsCreatedWithProfile() const {
  76. return false;
  77. }
  78. ChromeSpeechRecognitionPreferences::Service::Service(
  79. Profile* profile)
  80. : preferences_(new ChromeSpeechRecognitionPreferences(profile)) {
  81. }
  82. ChromeSpeechRecognitionPreferences::Service::~Service() {
  83. }
  84. void ChromeSpeechRecognitionPreferences::Service::Shutdown() {
  85. DCHECK(preferences_);
  86. preferences_->DetachFromProfile();
  87. }
  88. scoped_refptr<ChromeSpeechRecognitionPreferences>
  89. ChromeSpeechRecognitionPreferences::Service::GetPreferences() const {
  90. return preferences_;
  91. }
  92. scoped_refptr<ChromeSpeechRecognitionPreferences>
  93. ChromeSpeechRecognitionPreferences::GetForProfile(Profile* profile) {
  94. scoped_refptr<ChromeSpeechRecognitionPreferences> ret;
  95. if (profile) {
  96. // Note that when in incognito, GetForProfile will return NULL.
  97. // We catch that case below and return the default preferences.
  98. ret = Factory::GetInstance()->GetForProfile(profile);
  99. }
  100. if (!ret) {
  101. // Create a detached preferences object if no profile is provided.
  102. ret = new ChromeSpeechRecognitionPreferences(NULL);
  103. }
  104. return ret;
  105. }
  106. ChromeSpeechRecognitionPreferences::ChromeSpeechRecognitionPreferences(
  107. Profile* profile)
  108. : profile_(profile),
  109. pref_change_registrar_(new PrefChangeRegistrar()),
  110. filter_profanities_(kDefaultFilterProfanities),
  111. notifications_shown_(new ListValue()) {
  112. if (!profile_)
  113. return;
  114. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  115. pref_change_registrar_->Init(profile_->GetPrefs());
  116. ReloadFilterProfanities();
  117. pref_change_registrar_->Add(
  118. prefs::kSpeechRecognitionFilterProfanities,
  119. base::Bind(&ChromeSpeechRecognitionPreferences::ReloadFilterProfanities,
  120. base::Unretained(this)));
  121. ReloadNotificationsShown();
  122. pref_change_registrar_->Add(
  123. prefs::kSpeechRecognitionTrayNotificationShownContexts,
  124. base::Bind(&ChromeSpeechRecognitionPreferences::ReloadNotificationsShown,
  125. base::Unretained(this)));
  126. }
  127. ChromeSpeechRecognitionPreferences::~ChromeSpeechRecognitionPreferences() {
  128. }
  129. void ChromeSpeechRecognitionPreferences::DetachFromProfile() {
  130. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  131. DCHECK(profile_);
  132. pref_change_registrar_.reset();
  133. profile_ = NULL;
  134. }
  135. bool ChromeSpeechRecognitionPreferences::FilterProfanities() const {
  136. base::AutoLock read_lock(preferences_lock_);
  137. return filter_profanities_;
  138. }
  139. void ChromeSpeechRecognitionPreferences::SetFilterProfanities(
  140. bool filter_profanities) {
  141. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  142. {
  143. base::AutoLock write_lock(preferences_lock_);
  144. filter_profanities_ = filter_profanities;
  145. }
  146. if (profile_) {
  147. profile_->GetPrefs()->SetBoolean(prefs::kSpeechRecognitionFilterProfanities,
  148. filter_profanities);
  149. }
  150. }
  151. void ChromeSpeechRecognitionPreferences::ToggleFilterProfanities() {
  152. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  153. SetFilterProfanities(!FilterProfanities());
  154. }
  155. bool ChromeSpeechRecognitionPreferences::ShouldShowSecurityNotification(
  156. const std::string& context_name) const {
  157. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  158. DCHECK(notifications_shown_.get());
  159. scoped_ptr<base::StringValue> match_name(
  160. base::Value::CreateStringValue(context_name));
  161. return notifications_shown_->Find(*match_name) == notifications_shown_->end();
  162. }
  163. void ChromeSpeechRecognitionPreferences::SetHasShownSecurityNotification(
  164. const std::string& context_name) {
  165. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  166. DCHECK(notifications_shown_.get());
  167. notifications_shown_->AppendIfNotPresent(
  168. base::Value::CreateStringValue(context_name));
  169. if (profile_) {
  170. profile_->GetPrefs()->Set(
  171. prefs::kSpeechRecognitionTrayNotificationShownContexts,
  172. *notifications_shown_);
  173. }
  174. }
  175. void ChromeSpeechRecognitionPreferences::ReloadFilterProfanities() {
  176. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  177. base::AutoLock write_lock(preferences_lock_);
  178. filter_profanities_ = profile_->GetPrefs()->GetBoolean(
  179. prefs::kSpeechRecognitionFilterProfanities);
  180. }
  181. void ChromeSpeechRecognitionPreferences::ReloadNotificationsShown() {
  182. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  183. base::AutoLock write_lock(preferences_lock_);
  184. const base::ListValue* pref_list = profile_->GetPrefs()->GetList(
  185. prefs::kSpeechRecognitionTrayNotificationShownContexts);
  186. notifications_shown_.reset(pref_list->DeepCopy());
  187. }