| 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/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/common/pref_service.h" | 11 #include "chrome/common/pref_service.h" |
| 12 | 12 |
| 13 // static | 13 // static |
| 14 const wchar_t* HostContentSettingsMap::kTypeNames[] = { | 14 const wchar_t* HostContentSettingsMap::kTypeNames[] = { |
| 15 L"cookies", | 15 L"cookies", |
| 16 L"images", | 16 L"images", |
| 17 L"javascript", | 17 L"javascript", |
| 18 L"plugins", | 18 L"plugins", |
| 19 L"popups", | 19 L"popups", |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) | 22 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) |
| 23 : profile_(profile) { | 23 : profile_(profile), |
| 24 block_third_party_cookies_(false) { |
| 24 DCHECK_EQ(arraysize(kTypeNames), | 25 DCHECK_EQ(arraysize(kTypeNames), |
| 25 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | 26 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); |
| 26 | 27 |
| 27 const DictionaryValue* default_settings_dictionary = | 28 const DictionaryValue* default_settings_dictionary = |
| 28 profile_->GetPrefs()->GetDictionary(prefs::kDefaultContentSettings); | 29 profile_->GetPrefs()->GetDictionary(prefs::kDefaultContentSettings); |
| 29 // Careful: The returned value could be NULL if the pref has never been set. | 30 // Careful: The returned value could be NULL if the pref has never been set. |
| 30 if (default_settings_dictionary != NULL) { | 31 if (default_settings_dictionary != NULL) { |
| 31 GetSettingsFromDictionary(default_settings_dictionary, | 32 GetSettingsFromDictionary(default_settings_dictionary, |
| 32 &default_content_settings_); | 33 &default_content_settings_); |
| 33 } | 34 } |
| 34 | 35 |
| 35 const DictionaryValue* all_settings_dictionary = | 36 const DictionaryValue* all_settings_dictionary = |
| 36 profile_->GetPrefs()->GetDictionary(prefs::kPerHostContentSettings); | 37 profile_->GetPrefs()->GetDictionary(prefs::kPerHostContentSettings); |
| 37 // Careful: The returned value could be NULL if the pref has never been set. | 38 // Careful: The returned value could be NULL if the pref has never been set. |
| 38 if (all_settings_dictionary != NULL) { | 39 if (all_settings_dictionary != NULL) { |
| 39 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 40 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| 40 i != all_settings_dictionary->end_keys(); ++i) { | 41 i != all_settings_dictionary->end_keys(); ++i) { |
| 41 std::wstring wide_host(*i); | 42 std::wstring wide_host(*i); |
| 42 DictionaryValue* host_settings_dictionary = NULL; | 43 DictionaryValue* host_settings_dictionary = NULL; |
| 43 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 44 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 44 wide_host, &host_settings_dictionary); | 45 wide_host, &host_settings_dictionary); |
| 45 DCHECK(found); | 46 DCHECK(found); |
| 46 ContentSettings settings; | 47 ContentSettings settings; |
| 47 GetSettingsFromDictionary(host_settings_dictionary, &settings); | 48 GetSettingsFromDictionary(host_settings_dictionary, &settings); |
| 48 host_content_settings_[WideToUTF8(wide_host)] = settings; | 49 host_content_settings_[WideToUTF8(wide_host)] = settings; |
| 49 } | 50 } |
| 50 } | 51 } |
| 52 |
| 53 // TODO(darin): init third-party cookie pref |
| 51 } | 54 } |
| 52 | 55 |
| 53 // static | 56 // static |
| 54 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 57 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 55 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); | 58 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); |
| 56 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); | 59 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); |
| 60 |
| 61 // TODO(darin): register third-party cookie pref |
| 57 } | 62 } |
| 58 | 63 |
| 59 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( | 64 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( |
| 60 ContentSettingsType content_type) const { | 65 ContentSettingsType content_type) const { |
| 61 AutoLock auto_lock(lock_); | 66 AutoLock auto_lock(lock_); |
| 62 return default_content_settings_.settings[content_type]; | 67 return default_content_settings_.settings[content_type]; |
| 63 } | 68 } |
| 64 | 69 |
| 65 ContentSetting HostContentSettingsMap::GetContentSetting( | 70 ContentSetting HostContentSettingsMap::GetContentSetting( |
| 66 const std::string& host, | 71 const std::string& host, |
| 67 ContentSettingsType content_type) const { | 72 ContentSettingsType content_type) const { |
| 68 AutoLock auto_lock(lock_); | 73 AutoLock auto_lock(lock_); |
| 69 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | 74 HostContentSettings::const_iterator i(host_content_settings_.find(host)); |
| 70 return (i == host_content_settings_.end()) ? | 75 return (i == host_content_settings_.end()) ? |
| 71 CONTENT_SETTING_DEFAULT : i->second.settings[content_type]; | 76 default_content_settings_.settings[content_type] : |
| 77 i->second.settings[content_type]; |
| 72 } | 78 } |
| 73 | 79 |
| 74 ContentSettings HostContentSettingsMap::GetContentSettings( | 80 ContentSettings HostContentSettingsMap::GetContentSettings( |
| 75 const std::string& host) const { | 81 const std::string& host) const { |
| 76 AutoLock auto_lock(lock_); | 82 AutoLock auto_lock(lock_); |
| 77 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | 83 HostContentSettings::const_iterator i(host_content_settings_.find(host)); |
| 78 return (i == host_content_settings_.end()) ? ContentSettings() : i->second; | 84 if (i == host_content_settings_.end()) |
| 85 return default_content_settings_; |
| 86 |
| 87 ContentSettings output; |
| 88 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
| 89 if (i->second.settings[i] == CONTENT_SETTING_DEFAULT) { |
| 90 output.settings[i] = default_content_settings_.settings[i]; |
| 91 } else { |
| 92 output.settings[i] = i->second.settings[i]; |
| 93 } |
| 94 } |
| 95 return output; |
| 79 } | 96 } |
| 80 | 97 |
| 81 void HostContentSettingsMap::GetHostContentSettingsForOneType( | 98 void HostContentSettingsMap::GetHostContentSettingsForOneType( |
| 82 ContentSettingsType content_type, | 99 ContentSettingsType content_type, |
| 83 HostContentSettingsForOneType* settings) const { | 100 HostContentSettingsForOneType* settings) const { |
| 84 DCHECK(settings); | 101 DCHECK(settings); |
| 85 settings->clear(); | 102 settings->clear(); |
| 86 | 103 |
| 87 AutoLock auto_lock(lock_); | 104 AutoLock auto_lock(lock_); |
| 88 for (HostContentSettings::const_iterator i(host_content_settings_.begin()); | 105 for (HostContentSettings::const_iterator i(host_content_settings_.begin()); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 173 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 157 | 174 |
| 158 { | 175 { |
| 159 AutoLock auto_lock(lock_); | 176 AutoLock auto_lock(lock_); |
| 160 default_content_settings_ = ContentSettings(); | 177 default_content_settings_ = ContentSettings(); |
| 161 host_content_settings_.clear(); | 178 host_content_settings_.clear(); |
| 162 } | 179 } |
| 163 | 180 |
| 164 profile_->GetPrefs()->ClearPref(prefs::kDefaultContentSettings); | 181 profile_->GetPrefs()->ClearPref(prefs::kDefaultContentSettings); |
| 165 profile_->GetPrefs()->ClearPref(prefs::kPerHostContentSettings); | 182 profile_->GetPrefs()->ClearPref(prefs::kPerHostContentSettings); |
| 183 |
| 184 // TODO(darin): clear third-party cookie pref |
| 166 } | 185 } |
| 167 | 186 |
| 168 HostContentSettingsMap::~HostContentSettingsMap() { | 187 HostContentSettingsMap::~HostContentSettingsMap() { |
| 169 } | 188 } |
| 170 | 189 |
| 171 void HostContentSettingsMap::GetSettingsFromDictionary( | 190 void HostContentSettingsMap::GetSettingsFromDictionary( |
| 172 const DictionaryValue* dictionary, | 191 const DictionaryValue* dictionary, |
| 173 ContentSettings* settings) { | 192 ContentSettings* settings) { |
| 174 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 193 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
| 175 i != dictionary->end_keys(); ++i) { | 194 i != dictionary->end_keys(); ++i) { |
| 176 std::wstring content_type(*i); | 195 std::wstring content_type(*i); |
| 177 int setting = CONTENT_SETTING_DEFAULT; | 196 int setting = CONTENT_SETTING_DEFAULT; |
| 178 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | 197 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, |
| 179 &setting); | 198 &setting); |
| 180 DCHECK(found); | 199 DCHECK(found); |
| 181 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { | 200 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { |
| 182 if (std::wstring(kTypeNames[type]) == content_type) { | 201 if (std::wstring(kTypeNames[type]) == content_type) { |
| 183 settings->settings[type] = static_cast<ContentSetting>(setting); | 202 settings->settings[type] = static_cast<ContentSetting>(setting); |
| 184 break; | 203 break; |
| 185 } | 204 } |
| 186 } | 205 } |
| 187 } | 206 } |
| 188 } | 207 } |
| OLD | NEW |