 Chromium Code Reviews
 Chromium Code Reviews Issue 6253012:
  Add ContentSettingsProvider Interface.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 6253012:
  Add ContentSettingsProvider Interface.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/content_settings/pref_content_settings_provider.h" | 5 #include "chrome/browser/content_settings/pref_content_settings_provider.h" | 
| 6 | 6 | 
| 7 #include <string> | |
| 8 | |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" | 
| 8 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" | 
| 9 #include "chrome/browser/content_settings/content_settings_details.h" | 11 #include "chrome/browser/content_settings/content_settings_details.h" | 
| 10 #include "chrome/browser/content_settings/content_settings_pattern.h" | 12 #include "chrome/browser/content_settings/content_settings_pattern.h" | 
| 11 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" | 
| 12 #include "chrome/browser/prefs/scoped_pref_update.h" | 14 #include "chrome/browser/prefs/scoped_pref_update.h" | 
| 13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" | 
| 14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" | 
| 15 #include "chrome/common/notification_details.h" | 17 #include "chrome/common/notification_details.h" | 
| 16 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && | 52 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && | 
| 51 !CommandLine::ForCurrentProcess()->HasSwitch( | 53 !CommandLine::ForCurrentProcess()->HasSwitch( | 
| 52 switches::kEnableClickToPlay)) { | 54 switches::kEnableClickToPlay)) { | 
| 53 return CONTENT_SETTING_BLOCK; | 55 return CONTENT_SETTING_BLOCK; | 
| 54 } | 56 } | 
| 55 return setting; | 57 return setting; | 
| 56 } | 58 } | 
| 57 | 59 | 
| 58 } // namespace | 60 } // namespace | 
| 59 | 61 | 
| 60 PrefContentSettingsProvider::PrefContentSettingsProvider(Profile* profile) | 62 namespace content_settings { | 
| 61 : profile_(profile), | 63 | 
| 62 is_off_the_record_(profile_->IsOffTheRecord()), | 64 PrefDefaultContentSettingsProvider:: | 
| 63 updating_preferences_(false) { | 65 PrefDefaultContentSettingsProvider(Profile* profile) | 
| 66 : profile_(profile), | |
| 
Bernhard Bauer
2011/01/27 09:54:26
And here.
 
markusheintz_
2011/01/27 10:24:37
Done.
 | |
| 67 is_off_the_record_(profile_->IsOffTheRecord()), | |
| 68 updating_preferences_(false) { | |
| 64 PrefService* prefs = profile->GetPrefs(); | 69 PrefService* prefs = profile->GetPrefs(); | 
| 65 | 70 | 
| 66 // Read global defaults. | 71 // Read global defaults. | 
| 67 DCHECK_EQ(arraysize(kTypeNames), | 72 DCHECK_EQ(arraysize(kTypeNames), | 
| 68 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | 73 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | 
| 69 ReadDefaultSettings(true); | 74 ReadDefaultSettings(true); | 
| 70 | 75 | 
| 71 pref_change_registrar_.Init(prefs); | 76 pref_change_registrar_.Init(prefs); | 
| 72 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); | 77 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); | 
| 73 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 78 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 
| 74 Source<Profile>(profile_)); | 79 Source<Profile>(profile_)); | 
| 75 } | 80 } | 
| 76 | 81 | 
| 77 PrefContentSettingsProvider::~PrefContentSettingsProvider() { | 82 PrefDefaultContentSettingsProvider::~PrefDefaultContentSettingsProvider() { | 
| 78 UnregisterObservers(); | 83 UnregisterObservers(); | 
| 79 } | 84 } | 
| 80 | 85 | 
| 81 bool PrefContentSettingsProvider::CanProvideDefaultSetting( | 86 bool PrefDefaultContentSettingsProvider::CanProvideDefaultSetting( | 
| 82 ContentSettingsType content_type) const { | 87 ContentSettingsType content_type) const { | 
| 83 return true; | 88 return true; | 
| 84 } | 89 } | 
| 85 | 90 | 
| 86 ContentSetting PrefContentSettingsProvider::ProvideDefaultSetting( | 91 ContentSetting PrefDefaultContentSettingsProvider::ProvideDefaultSetting( | 
| 87 ContentSettingsType content_type) const { | 92 ContentSettingsType content_type) const { | 
| 88 base::AutoLock lock(lock_); | 93 base::AutoLock lock(lock_); | 
| 89 return default_content_settings_.settings[content_type]; | 94 return default_content_settings_.settings[content_type]; | 
| 90 } | 95 } | 
| 91 | 96 | 
| 92 void PrefContentSettingsProvider::UpdateDefaultSetting( | 97 void PrefDefaultContentSettingsProvider::UpdateDefaultSetting( | 
| 93 ContentSettingsType content_type, | 98 ContentSettingsType content_type, | 
| 94 ContentSetting setting) { | 99 ContentSetting setting) { | 
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 96 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 101 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 
| 97 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 102 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 
| 98 setting != CONTENT_SETTING_ASK || | 103 setting != CONTENT_SETTING_ASK || | 
| 99 CommandLine::ForCurrentProcess()->HasSwitch( | 104 CommandLine::ForCurrentProcess()->HasSwitch( | 
| 100 switches::kEnableClickToPlay)); | 105 switches::kEnableClickToPlay)); | 
| 101 | 106 | 
| 102 // The default settings may not be directly modified for OTR sessions. | 107 // The default settings may not be directly modified for OTR sessions. | 
| (...skipping 21 matching lines...) Expand all Loading... | |
| 124 default_settings_dictionary->SetWithoutPathExpansion( | 129 default_settings_dictionary->SetWithoutPathExpansion( | 
| 125 dictionary_path, Value::CreateIntegerValue(setting)); | 130 dictionary_path, Value::CreateIntegerValue(setting)); | 
| 126 } | 131 } | 
| 127 } | 132 } | 
| 128 updating_preferences_ = false; | 133 updating_preferences_ = false; | 
| 129 | 134 | 
| 130 NotifyObservers( | 135 NotifyObservers( | 
| 131 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); | 136 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); | 
| 132 } | 137 } | 
| 133 | 138 | 
| 134 bool PrefContentSettingsProvider::DefaultSettingIsManaged( | 139 bool PrefDefaultContentSettingsProvider::DefaultSettingIsManaged( | 
| 135 ContentSettingsType content_type) const { | 140 ContentSettingsType content_type) const { | 
| 136 return false; | 141 return false; | 
| 137 } | 142 } | 
| 138 | 143 | 
| 139 void PrefContentSettingsProvider::ResetToDefaults() { | 144 void PrefDefaultContentSettingsProvider::ResetToDefaults() { | 
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 141 base::AutoLock lock(lock_); | 146 base::AutoLock lock(lock_); | 
| 142 default_content_settings_ = ContentSettings(); | 147 default_content_settings_ = ContentSettings(); | 
| 143 ForceDefaultsToBeExplicit(); | 148 ForceDefaultsToBeExplicit(); | 
| 144 | 149 | 
| 145 if (!is_off_the_record_) { | 150 if (!is_off_the_record_) { | 
| 146 PrefService* prefs = profile_->GetPrefs(); | 151 PrefService* prefs = profile_->GetPrefs(); | 
| 147 updating_preferences_ = true; | 152 updating_preferences_ = true; | 
| 148 prefs->ClearPref(prefs::kDefaultContentSettings); | 153 prefs->ClearPref(prefs::kDefaultContentSettings); | 
| 149 updating_preferences_ = false; | 154 updating_preferences_ = false; | 
| 150 } | 155 } | 
| 151 } | 156 } | 
| 152 | 157 | 
| 153 void PrefContentSettingsProvider::Observe(NotificationType type, | 158 void PrefDefaultContentSettingsProvider::Observe(NotificationType type, | 
| 154 const NotificationSource& source, | 159 const NotificationSource& source, | 
| 155 const NotificationDetails& details) { | 160 const NotificationDetails& details) { | 
| 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 157 | 162 | 
| 158 if (type == NotificationType::PREF_CHANGED) { | 163 if (type == NotificationType::PREF_CHANGED) { | 
| 159 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 164 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 
| 160 if (updating_preferences_) | 165 if (updating_preferences_) | 
| 161 return; | 166 return; | 
| 162 | 167 | 
| 163 std::string* name = Details<std::string>(details).ptr(); | 168 std::string* name = Details<std::string>(details).ptr(); | 
| 164 if (*name == prefs::kDefaultContentSettings) { | 169 if (*name == prefs::kDefaultContentSettings) { | 
| 165 ReadDefaultSettings(true); | 170 ReadDefaultSettings(true); | 
| 166 } else { | 171 } else { | 
| 167 NOTREACHED() << "Unexpected preference observed"; | 172 NOTREACHED() << "Unexpected preference observed"; | 
| 168 return; | 173 return; | 
| 169 } | 174 } | 
| 170 | 175 | 
| 171 if (!is_off_the_record_) { | 176 if (!is_off_the_record_) { | 
| 172 NotifyObservers(ContentSettingsDetails( | 177 NotifyObservers(ContentSettingsDetails( | 
| 173 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); | 178 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); | 
| 174 } | 179 } | 
| 175 } else if (type == NotificationType::PROFILE_DESTROYED) { | 180 } else if (type == NotificationType::PROFILE_DESTROYED) { | 
| 176 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | 181 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | 
| 177 UnregisterObservers(); | 182 UnregisterObservers(); | 
| 178 } else { | 183 } else { | 
| 179 NOTREACHED() << "Unexpected notification"; | 184 NOTREACHED() << "Unexpected notification"; | 
| 180 } | 185 } | 
| 181 } | 186 } | 
| 182 | 187 | 
| 183 void PrefContentSettingsProvider::UnregisterObservers() { | 188 void PrefDefaultContentSettingsProvider::UnregisterObservers() { | 
| 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 185 if (!profile_) | 190 if (!profile_) | 
| 186 return; | 191 return; | 
| 187 pref_change_registrar_.RemoveAll(); | 192 pref_change_registrar_.RemoveAll(); | 
| 188 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | 193 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | 
| 189 Source<Profile>(profile_)); | 194 Source<Profile>(profile_)); | 
| 190 profile_ = NULL; | 195 profile_ = NULL; | 
| 191 } | 196 } | 
| 192 | 197 | 
| 193 void PrefContentSettingsProvider::ReadDefaultSettings(bool overwrite) { | 198 void PrefDefaultContentSettingsProvider::ReadDefaultSettings(bool overwrite) { | 
| 194 PrefService* prefs = profile_->GetPrefs(); | 199 PrefService* prefs = profile_->GetPrefs(); | 
| 195 const DictionaryValue* default_settings_dictionary = | 200 const DictionaryValue* default_settings_dictionary = | 
| 196 prefs->GetDictionary(prefs::kDefaultContentSettings); | 201 prefs->GetDictionary(prefs::kDefaultContentSettings); | 
| 197 | 202 | 
| 198 base::AutoLock lock(lock_); | 203 base::AutoLock lock(lock_); | 
| 199 | 204 | 
| 200 if (overwrite) | 205 if (overwrite) | 
| 201 default_content_settings_ = ContentSettings(); | 206 default_content_settings_ = ContentSettings(); | 
| 202 | 207 | 
| 203 // Careful: The returned value could be NULL if the pref has never been set. | 208 // Careful: The returned value could be NULL if the pref has never been set. | 
| 204 if (default_settings_dictionary != NULL) { | 209 if (default_settings_dictionary != NULL) { | 
| 205 GetSettingsFromDictionary(default_settings_dictionary, | 210 GetSettingsFromDictionary(default_settings_dictionary, | 
| 206 &default_content_settings_); | 211 &default_content_settings_); | 
| 207 } | 212 } | 
| 208 ForceDefaultsToBeExplicit(); | 213 ForceDefaultsToBeExplicit(); | 
| 209 } | 214 } | 
| 210 | 215 | 
| 211 void PrefContentSettingsProvider::ForceDefaultsToBeExplicit() { | 216 void PrefDefaultContentSettingsProvider::ForceDefaultsToBeExplicit() { | 
| 212 DCHECK_EQ(arraysize(kDefaultSettings), | 217 DCHECK_EQ(arraysize(kDefaultSettings), | 
| 213 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | 218 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | 
| 214 | 219 | 
| 215 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 220 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 
| 216 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) | 221 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) | 
| 217 default_content_settings_.settings[i] = kDefaultSettings[i]; | 222 default_content_settings_.settings[i] = kDefaultSettings[i]; | 
| 218 } | 223 } | 
| 219 } | 224 } | 
| 220 | 225 | 
| 221 void PrefContentSettingsProvider::GetSettingsFromDictionary( | 226 void PrefDefaultContentSettingsProvider::GetSettingsFromDictionary( | 
| 222 const DictionaryValue* dictionary, | 227 const DictionaryValue* dictionary, | 
| 223 ContentSettings* settings) { | 228 ContentSettings* settings) { | 
| 224 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 229 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 
| 225 i != dictionary->end_keys(); ++i) { | 230 i != dictionary->end_keys(); ++i) { | 
| 226 const std::string& content_type(*i); | 231 const std::string& content_type(*i); | 
| 227 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { | 232 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { | 
| 228 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type)) { | 233 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type)) { | 
| 229 int setting = CONTENT_SETTING_DEFAULT; | 234 int setting = CONTENT_SETTING_DEFAULT; | 
| 230 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | 235 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | 
| 231 &setting); | 236 &setting); | 
| 232 DCHECK(found); | 237 DCHECK(found); | 
| 233 settings->settings[type] = IntToContentSetting(setting); | 238 settings->settings[type] = IntToContentSetting(setting); | 
| 234 break; | 239 break; | 
| 235 } | 240 } | 
| 236 } | 241 } | 
| 237 } | 242 } | 
| 238 // Migrate obsolete cookie prompt mode. | 243 // Migrate obsolete cookie prompt mode. | 
| 239 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | 244 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | 
| 240 CONTENT_SETTING_ASK) | 245 CONTENT_SETTING_ASK) | 
| 241 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | 246 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | 
| 242 | 247 | 
| 243 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | 248 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | 
| 244 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | 249 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | 
| 245 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | 250 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | 
| 246 } | 251 } | 
| 247 | 252 | 
| 248 void PrefContentSettingsProvider::NotifyObservers( | 253 void PrefDefaultContentSettingsProvider::NotifyObservers( | 
| 249 const ContentSettingsDetails& details) { | 254 const ContentSettingsDetails& details) { | 
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
| 251 if (profile_ == NULL) | 256 if (profile_ == NULL) | 
| 252 return; | 257 return; | 
| 253 NotificationService::current()->Notify( | 258 NotificationService::current()->Notify( | 
| 254 NotificationType::CONTENT_SETTINGS_CHANGED, | 259 NotificationType::CONTENT_SETTINGS_CHANGED, | 
| 255 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), | 260 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), | 
| 256 Details<const ContentSettingsDetails>(&details)); | 261 Details<const ContentSettingsDetails>(&details)); | 
| 257 } | 262 } | 
| 258 | 263 | 
| 259 | 264 | 
| 260 // static | 265 // static | 
| 261 void PrefContentSettingsProvider::RegisterUserPrefs(PrefService* prefs) { | 266 void PrefDefaultContentSettingsProvider::RegisterUserPrefs(PrefService* prefs) { | 
| 262 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); | 267 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); | 
| 263 } | 268 } | 
| 269 | |
| 270 } // namespace content_settings | |
| OLD | NEW |