| 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/host_content_settings_map.h" | 5 #include "chrome/browser/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/chrome_thread.h" | 8 #include "chrome/browser/chrome_thread.h" |
| 9 #include "chrome/browser/profile.h" | 9 #include "chrome/browser/profile.h" |
| 10 #include "chrome/common/notification_service.h" |
| 11 #include "chrome/common/notification_type.h" |
| 10 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/common/pref_service.h" | 13 #include "chrome/common/pref_service.h" |
| 12 #include "net/base/static_cookie_policy.h" | 14 #include "net/base/static_cookie_policy.h" |
| 13 | 15 |
| 14 // static | 16 // static |
| 15 const wchar_t* | 17 const wchar_t* |
| 16 HostContentSettingsMap::kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | 18 HostContentSettingsMap::kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { |
| 17 L"cookies", | 19 L"cookies", |
| 18 L"images", | 20 L"images", |
| 19 L"javascript", | 21 L"javascript", |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 default_content_settings_.settings[content_type] = | 161 default_content_settings_.settings[content_type] = |
| 160 kDefaultSettings[content_type]; | 162 kDefaultSettings[content_type]; |
| 161 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | 163 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, |
| 162 NULL); | 164 NULL); |
| 163 } else { | 165 } else { |
| 164 default_content_settings_.settings[content_type] = setting; | 166 default_content_settings_.settings[content_type] = setting; |
| 165 default_settings_dictionary->SetWithoutPathExpansion( | 167 default_settings_dictionary->SetWithoutPathExpansion( |
| 166 dictionary_path, Value::CreateIntegerValue(setting)); | 168 dictionary_path, Value::CreateIntegerValue(setting)); |
| 167 } | 169 } |
| 168 } | 170 } |
| 171 |
| 172 NotifyObservers(std::string()); |
| 169 } | 173 } |
| 170 | 174 |
| 171 void HostContentSettingsMap::SetContentSetting(const std::string& host, | 175 void HostContentSettingsMap::SetContentSetting(const std::string& host, |
| 172 ContentSettingsType content_type, | 176 ContentSettingsType content_type, |
| 173 ContentSetting setting) { | 177 ContentSetting setting) { |
| 174 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 178 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 175 | 179 |
| 176 std::wstring wide_host(UTF8ToWide(host)); | 180 std::wstring wide_host(UTF8ToWide(host)); |
| 177 DictionaryValue* all_settings_dictionary = | 181 DictionaryValue* all_settings_dictionary = |
| 178 profile_->GetPrefs()->GetMutableDictionary( | 182 profile_->GetPrefs()->GetMutableDictionary( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 200 wide_host, host_settings_dictionary); | 204 wide_host, host_settings_dictionary); |
| 201 DCHECK_NE(setting, CONTENT_SETTING_DEFAULT); | 205 DCHECK_NE(setting, CONTENT_SETTING_DEFAULT); |
| 202 } | 206 } |
| 203 std::wstring dictionary_path(kTypeNames[content_type]); | 207 std::wstring dictionary_path(kTypeNames[content_type]); |
| 204 if (setting == CONTENT_SETTING_DEFAULT) { | 208 if (setting == CONTENT_SETTING_DEFAULT) { |
| 205 host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, NULL); | 209 host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, NULL); |
| 206 } else { | 210 } else { |
| 207 host_settings_dictionary->SetWithoutPathExpansion( | 211 host_settings_dictionary->SetWithoutPathExpansion( |
| 208 dictionary_path, Value::CreateIntegerValue(setting)); | 212 dictionary_path, Value::CreateIntegerValue(setting)); |
| 209 } | 213 } |
| 214 |
| 215 NotifyObservers(host); |
| 210 } | 216 } |
| 211 | 217 |
| 212 void HostContentSettingsMap::ClearSettingsForOneType( | 218 void HostContentSettingsMap::ClearSettingsForOneType( |
| 213 ContentSettingsType content_type) { | 219 ContentSettingsType content_type) { |
| 214 AutoLock auto_lock(lock_); | 220 { |
| 215 for (HostContentSettings::iterator i(host_content_settings_.begin()); | 221 AutoLock auto_lock(lock_); |
| 216 i != host_content_settings_.end(); ) { | 222 for (HostContentSettings::iterator i(host_content_settings_.begin()); |
| 217 if (i->second.settings[content_type] != CONTENT_SETTING_DEFAULT) { | 223 i != host_content_settings_.end(); ) { |
| 218 i->second.settings[content_type] = CONTENT_SETTING_DEFAULT; | 224 if (i->second.settings[content_type] != CONTENT_SETTING_DEFAULT) { |
| 219 std::wstring wide_host(UTF8ToWide(i->first)); | 225 i->second.settings[content_type] = CONTENT_SETTING_DEFAULT; |
| 220 DictionaryValue* all_settings_dictionary = | 226 std::wstring wide_host(UTF8ToWide(i->first)); |
| 221 profile_->GetPrefs()->GetMutableDictionary( | 227 DictionaryValue* all_settings_dictionary = |
| 222 prefs::kPerHostContentSettings); | 228 profile_->GetPrefs()->GetMutableDictionary( |
| 223 if (AllDefault(i->second)) { | 229 prefs::kPerHostContentSettings); |
| 224 all_settings_dictionary->RemoveWithoutPathExpansion(wide_host, NULL); | 230 if (AllDefault(i->second)) { |
| 225 host_content_settings_.erase(i++); | 231 all_settings_dictionary->RemoveWithoutPathExpansion(wide_host, NULL); |
| 232 host_content_settings_.erase(i++); |
| 233 } else { |
| 234 DictionaryValue* host_settings_dictionary; |
| 235 bool found = |
| 236 all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 237 wide_host, &host_settings_dictionary); |
| 238 DCHECK(found); |
| 239 host_settings_dictionary->RemoveWithoutPathExpansion( |
| 240 kTypeNames[content_type], NULL); |
| 241 ++i; |
| 242 } |
| 226 } else { | 243 } else { |
| 227 DictionaryValue* host_settings_dictionary; | |
| 228 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 229 wide_host, &host_settings_dictionary); | |
| 230 DCHECK(found); | |
| 231 host_settings_dictionary->RemoveWithoutPathExpansion( | |
| 232 kTypeNames[content_type], NULL); | |
| 233 ++i; | 244 ++i; |
| 234 } | 245 } |
| 235 } else { | |
| 236 ++i; | |
| 237 } | 246 } |
| 238 } | 247 } |
| 248 |
| 249 NotifyObservers(std::string()); |
| 239 } | 250 } |
| 240 | 251 |
| 241 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { | 252 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { |
| 242 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 253 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 243 | 254 |
| 244 { | 255 { |
| 245 AutoLock auto_lock(lock_); | 256 AutoLock auto_lock(lock_); |
| 246 block_third_party_cookies_ = block; | 257 block_third_party_cookies_ = block; |
| 247 } | 258 } |
| 248 | 259 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 261 default_content_settings_ = ContentSettings(); | 272 default_content_settings_ = ContentSettings(); |
| 262 ForceDefaultsToBeExplicit(); | 273 ForceDefaultsToBeExplicit(); |
| 263 host_content_settings_.clear(); | 274 host_content_settings_.clear(); |
| 264 block_third_party_cookies_ = false; | 275 block_third_party_cookies_ = false; |
| 265 } | 276 } |
| 266 | 277 |
| 267 PrefService* prefs = profile_->GetPrefs(); | 278 PrefService* prefs = profile_->GetPrefs(); |
| 268 prefs->ClearPref(prefs::kDefaultContentSettings); | 279 prefs->ClearPref(prefs::kDefaultContentSettings); |
| 269 prefs->ClearPref(prefs::kPerHostContentSettings); | 280 prefs->ClearPref(prefs::kPerHostContentSettings); |
| 270 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 281 prefs->ClearPref(prefs::kBlockThirdPartyCookies); |
| 282 |
| 283 NotifyObservers(std::string()); |
| 271 } | 284 } |
| 272 | 285 |
| 273 HostContentSettingsMap::~HostContentSettingsMap() { | 286 HostContentSettingsMap::~HostContentSettingsMap() { |
| 274 } | 287 } |
| 275 | 288 |
| 276 void HostContentSettingsMap::GetSettingsFromDictionary( | 289 void HostContentSettingsMap::GetSettingsFromDictionary( |
| 277 const DictionaryValue* dictionary, | 290 const DictionaryValue* dictionary, |
| 278 ContentSettings* settings) { | 291 ContentSettings* settings) { |
| 279 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 292 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
| 280 i != dictionary->end_keys(); ++i) { | 293 i != dictionary->end_keys(); ++i) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 300 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) | 313 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) |
| 301 default_content_settings_.settings[i] = kDefaultSettings[i]; | 314 default_content_settings_.settings[i] = kDefaultSettings[i]; |
| 302 } | 315 } |
| 303 } | 316 } |
| 304 | 317 |
| 305 bool HostContentSettingsMap::AllDefault(const ContentSettings& settings) const { | 318 bool HostContentSettingsMap::AllDefault(const ContentSettings& settings) const { |
| 306 for (size_t i = 0; i < arraysize(settings.settings); ++i) { | 319 for (size_t i = 0; i < arraysize(settings.settings); ++i) { |
| 307 if (settings.settings[i] != CONTENT_SETTING_DEFAULT) | 320 if (settings.settings[i] != CONTENT_SETTING_DEFAULT) |
| 308 return false; | 321 return false; |
| 309 } | 322 } |
| 310 | |
| 311 return true; | 323 return true; |
| 312 } | 324 } |
| 325 |
| 326 void HostContentSettingsMap::NotifyObservers(const std::string& host) { |
| 327 ContentSettingsDetails details(host); |
| 328 NotificationService::current()->Notify( |
| 329 NotificationType::CONTENT_SETTINGS_CHANGED, |
| 330 Source<HostContentSettingsMap>(this), |
| 331 Details<ContentSettingsDetails>(&details)); |
| 332 } |
| OLD | NEW |