OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/content_settings_default_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_default_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito) | 102 DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito) |
103 : prefs_(prefs), | 103 : prefs_(prefs), |
104 is_incognito_(incognito), | 104 is_incognito_(incognito), |
105 updating_preferences_(false) { | 105 updating_preferences_(false) { |
106 DCHECK(prefs_); | 106 DCHECK(prefs_); |
107 MigrateObsoleteNotificationPref(); | 107 MigrateObsoleteNotificationPref(); |
108 MigrateObsoleteGeolocationPref(); | 108 MigrateObsoleteGeolocationPref(); |
109 | 109 |
110 // Read global defaults. | 110 // Read global defaults. |
111 ReadDefaultSettings(true); | 111 ReadDefaultSettings(true); |
112 if (default_content_settings_.settings[CONTENT_SETTINGS_TYPE_COOKIES] == | 112 if (default_content_settings_[CONTENT_SETTINGS_TYPE_COOKIES] == |
113 CONTENT_SETTING_BLOCK) { | 113 CONTENT_SETTING_BLOCK) { |
114 UserMetrics::RecordAction( | 114 UserMetrics::RecordAction( |
115 UserMetricsAction("CookieBlockingEnabledPerDefault")); | 115 UserMetricsAction("CookieBlockingEnabledPerDefault")); |
116 } else { | 116 } else { |
117 UserMetrics::RecordAction( | 117 UserMetrics::RecordAction( |
118 UserMetricsAction("CookieBlockingDisabledPerDefault")); | 118 UserMetricsAction("CookieBlockingDisabledPerDefault")); |
119 } | 119 } |
120 | 120 |
121 pref_change_registrar_.Init(prefs_); | 121 pref_change_registrar_.Init(prefs_); |
122 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); | 122 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 // |DefaultProvider| should not send any notifications when holding | 155 // |DefaultProvider| should not send any notifications when holding |
156 // |lock_|. |DictionaryPrefUpdate| destructor and | 156 // |lock_|. |DictionaryPrefUpdate| destructor and |
157 // |PrefService::SetInteger()| send out notifications. As a response, the | 157 // |PrefService::SetInteger()| send out notifications. As a response, the |
158 // upper layers may call |GetAllContentSettingRules| which acquires |lock_| | 158 // upper layers may call |GetAllContentSettingRules| which acquires |lock_| |
159 // again. | 159 // again. |
160 { | 160 { |
161 base::AutoLock lock(lock_); | 161 base::AutoLock lock(lock_); |
162 if (setting == CONTENT_SETTING_DEFAULT || | 162 if (setting == CONTENT_SETTING_DEFAULT || |
163 setting == kDefaultSettings[content_type]) { | 163 setting == kDefaultSettings[content_type]) { |
164 default_content_settings_.settings[content_type] = | 164 default_content_settings_[content_type] = |
165 kDefaultSettings[content_type]; | 165 kDefaultSettings[content_type]; |
166 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | 166 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, |
167 NULL); | 167 NULL); |
168 } else { | 168 } else { |
169 default_content_settings_.settings[content_type] = setting; | 169 default_content_settings_[content_type] = setting; |
170 default_settings_dictionary->SetWithoutPathExpansion( | 170 default_settings_dictionary->SetWithoutPathExpansion( |
171 dictionary_path, Value::CreateIntegerValue(setting)); | 171 dictionary_path, Value::CreateIntegerValue(setting)); |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 // Keep the obsolete pref in sync as long as backwards compatibility is | 175 // Keep the obsolete pref in sync as long as backwards compatibility is |
176 // required. This is required to keep sync working correctly. | 176 // required. This is required to keep sync working correctly. |
177 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { | 177 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { |
178 prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting, | 178 prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting, |
179 setting == CONTENT_SETTING_DEFAULT ? | 179 setting == CONTENT_SETTING_DEFAULT ? |
180 kDefaultSettings[content_type] : setting); | 180 kDefaultSettings[content_type] : setting); |
181 } | 181 } |
182 } | 182 } |
183 | 183 |
184 NotifyObservers(ContentSettingsPattern(), | 184 NotifyObservers(ContentSettingsPattern(), |
185 ContentSettingsPattern(), | 185 ContentSettingsPattern(), |
186 content_type, | 186 content_type, |
187 std::string()); | 187 std::string()); |
188 } | 188 } |
189 | 189 |
190 RuleIterator* DefaultProvider::GetRuleIterator( | 190 RuleIterator* DefaultProvider::GetRuleIterator( |
191 ContentSettingsType content_type, | 191 ContentSettingsType content_type, |
192 const ResourceIdentifier& resource_identifier, | 192 const ResourceIdentifier& resource_identifier, |
193 bool incognito) const { | 193 bool incognito) const { |
194 base::AutoLock lock(lock_); | 194 base::AutoLock lock(lock_); |
195 if (resource_identifier.empty()) { | 195 if (resource_identifier.empty()) { |
196 return new DefaultRuleIterator( | 196 return new DefaultRuleIterator(default_content_settings_[content_type]); |
197 default_content_settings_.settings[content_type]); | |
198 } else { | 197 } else { |
199 return new EmptyRuleIterator(); | 198 return new EmptyRuleIterator(); |
200 } | 199 } |
201 } | 200 } |
202 | 201 |
203 void DefaultProvider::ClearAllContentSettingsRules( | 202 void DefaultProvider::ClearAllContentSettingsRules( |
204 ContentSettingsType content_type) { | 203 ContentSettingsType content_type) { |
205 // TODO(markusheintz): This method is only called when the | 204 // TODO(markusheintz): This method is only called when the |
206 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the | 205 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the |
207 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the | 206 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } else { | 246 } else { |
248 NOTREACHED() << "Unexpected notification"; | 247 NOTREACHED() << "Unexpected notification"; |
249 } | 248 } |
250 } | 249 } |
251 | 250 |
252 void DefaultProvider::ReadDefaultSettings(bool overwrite) { | 251 void DefaultProvider::ReadDefaultSettings(bool overwrite) { |
253 base::AutoLock lock(lock_); | 252 base::AutoLock lock(lock_); |
254 const DictionaryValue* default_settings_dictionary = | 253 const DictionaryValue* default_settings_dictionary = |
255 prefs_->GetDictionary(prefs::kDefaultContentSettings); | 254 prefs_->GetDictionary(prefs::kDefaultContentSettings); |
256 | 255 |
257 if (overwrite) | 256 if (overwrite) { |
258 default_content_settings_ = ContentSettings(); | 257 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) |
| 258 default_content_settings_[i] = CONTENT_SETTING_DEFAULT; |
| 259 } |
259 | 260 |
260 // Careful: The returned value could be NULL if the pref has never been set. | 261 // Careful: The returned value could be NULL if the pref has never been set. |
261 if (default_settings_dictionary) { | 262 if (default_settings_dictionary) { |
262 GetSettingsFromDictionary(default_settings_dictionary, | 263 GetSettingsFromDictionary(default_settings_dictionary, |
263 &default_content_settings_); | 264 default_content_settings_); |
264 } | 265 } |
265 ForceDefaultsToBeExplicit(); | 266 ForceDefaultsToBeExplicit(); |
266 } | 267 } |
267 | 268 |
268 void DefaultProvider::ForceDefaultsToBeExplicit() { | 269 void DefaultProvider::ForceDefaultsToBeExplicit() { |
269 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 270 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
270 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) | 271 if (default_content_settings_[i] == CONTENT_SETTING_DEFAULT) |
271 default_content_settings_.settings[i] = kDefaultSettings[i]; | 272 default_content_settings_[i] = kDefaultSettings[i]; |
272 } | 273 } |
273 } | 274 } |
274 | 275 |
275 void DefaultProvider::GetSettingsFromDictionary( | 276 void DefaultProvider::GetSettingsFromDictionary( |
276 const DictionaryValue* dictionary, | 277 const DictionaryValue* dictionary, |
277 ContentSettings* settings) { | 278 ContentSetting* settings) { |
278 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 279 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
279 i != dictionary->end_keys(); ++i) { | 280 i != dictionary->end_keys(); ++i) { |
280 const std::string& content_type(*i); | 281 const std::string& content_type(*i); |
281 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { | 282 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { |
282 if (content_type == GetTypeName(ContentSettingsType(type))) { | 283 if (content_type == GetTypeName(ContentSettingsType(type))) { |
283 int setting = CONTENT_SETTING_DEFAULT; | 284 int setting = CONTENT_SETTING_DEFAULT; |
284 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | 285 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, |
285 &setting); | 286 &setting); |
286 DCHECK(found); | 287 DCHECK(found); |
287 settings->settings[type] = IntToContentSetting(setting); | 288 settings[type] = IntToContentSetting(setting); |
288 break; | 289 break; |
289 } | 290 } |
290 } | 291 } |
291 } | 292 } |
292 // Migrate obsolete cookie prompt mode/ | 293 // Migrate obsolete cookie prompt mode/ |
293 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | 294 if (settings[CONTENT_SETTINGS_TYPE_COOKIES] == CONTENT_SETTING_ASK) |
294 CONTENT_SETTING_ASK) | 295 settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; |
295 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | |
296 | 296 |
297 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | 297 settings[CONTENT_SETTINGS_TYPE_PLUGINS] = |
298 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | 298 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, |
299 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | 299 settings[CONTENT_SETTINGS_TYPE_PLUGINS]); |
300 } | 300 } |
301 | 301 |
302 void DefaultProvider::MigrateObsoleteNotificationPref() { | 302 void DefaultProvider::MigrateObsoleteNotificationPref() { |
303 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { | 303 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { |
304 ContentSetting setting = IntToContentSetting( | 304 ContentSetting setting = IntToContentSetting( |
305 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | 305 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); |
306 SetContentSetting( | 306 SetContentSetting( |
307 ContentSettingsPattern::Wildcard(), | 307 ContentSettingsPattern::Wildcard(), |
308 ContentSettingsPattern::Wildcard(), | 308 ContentSettingsPattern::Wildcard(), |
309 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 309 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
(...skipping 12 matching lines...) Expand all Loading... |
322 SetContentSetting( | 322 SetContentSetting( |
323 ContentSettingsPattern::Wildcard(), | 323 ContentSettingsPattern::Wildcard(), |
324 ContentSettingsPattern::Wildcard(), | 324 ContentSettingsPattern::Wildcard(), |
325 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 325 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
326 std::string(), | 326 std::string(), |
327 setting); | 327 setting); |
328 } | 328 } |
329 } | 329 } |
330 | 330 |
331 } // namespace content_settings | 331 } // namespace content_settings |
OLD | NEW |