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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 Rule Next() { | 63 Rule Next() { |
64 DCHECK(setting_ != CONTENT_SETTING_DEFAULT); | 64 DCHECK(setting_ != CONTENT_SETTING_DEFAULT); |
65 ContentSetting setting_to_return = setting_; | 65 ContentSetting setting_to_return = setting_; |
66 setting_ = CONTENT_SETTING_DEFAULT; | 66 setting_ = CONTENT_SETTING_DEFAULT; |
67 return Rule(ContentSettingsPattern::Wildcard(), | 67 return Rule(ContentSettingsPattern::Wildcard(), |
68 ContentSettingsPattern::Wildcard(), | 68 ContentSettingsPattern::Wildcard(), |
69 Value::CreateIntegerValue(setting_to_return)); | 69 Value::CreateIntegerValue(setting_to_return)); |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
| 73 // TODO(markusheintz): |ContentSetting| should be replaced with a |Value|. |
73 ContentSetting setting_; | 74 ContentSetting setting_; |
74 }; | 75 }; |
75 | 76 |
76 } // namespace | 77 } // namespace |
77 | 78 |
78 // static | 79 // static |
79 void DefaultProvider::RegisterUserPrefs(PrefService* prefs) { | 80 void DefaultProvider::RegisterUserPrefs(PrefService* prefs) { |
80 // The registration of the preference prefs::kDefaultContentSettings should | 81 // The registration of the preference prefs::kDefaultContentSettings should |
81 // also include the default values for default content settings. This allows | 82 // also include the default values for default content settings. This allows |
82 // functional tests to get default content settings by reading the preference | 83 // functional tests to get default content settings by reading the preference |
(...skipping 19 matching lines...) Expand all Loading... |
102 DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito) | 103 DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito) |
103 : prefs_(prefs), | 104 : prefs_(prefs), |
104 is_incognito_(incognito), | 105 is_incognito_(incognito), |
105 updating_preferences_(false) { | 106 updating_preferences_(false) { |
106 DCHECK(prefs_); | 107 DCHECK(prefs_); |
107 MigrateObsoleteNotificationPref(); | 108 MigrateObsoleteNotificationPref(); |
108 MigrateObsoleteGeolocationPref(); | 109 MigrateObsoleteGeolocationPref(); |
109 | 110 |
110 // Read global defaults. | 111 // Read global defaults. |
111 ReadDefaultSettings(true); | 112 ReadDefaultSettings(true); |
112 if (default_content_settings_[CONTENT_SETTINGS_TYPE_COOKIES] == | 113 |
113 CONTENT_SETTING_BLOCK) { | 114 ContentSetting cookie_setting = ValueToContentSetting( |
| 115 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].get()); |
| 116 if (cookie_setting == CONTENT_SETTING_BLOCK) { |
114 UserMetrics::RecordAction( | 117 UserMetrics::RecordAction( |
115 UserMetricsAction("CookieBlockingEnabledPerDefault")); | 118 UserMetricsAction("CookieBlockingEnabledPerDefault")); |
116 } else { | 119 } else { |
117 UserMetrics::RecordAction( | 120 UserMetrics::RecordAction( |
118 UserMetricsAction("CookieBlockingDisabledPerDefault")); | 121 UserMetricsAction("CookieBlockingDisabledPerDefault")); |
119 } | 122 } |
120 | 123 |
121 pref_change_registrar_.Init(prefs_); | 124 pref_change_registrar_.Init(prefs_); |
122 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); | 125 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); |
123 pref_change_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this); | 126 pref_change_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this); |
124 } | 127 } |
125 | 128 |
126 DefaultProvider::~DefaultProvider() { | 129 DefaultProvider::~DefaultProvider() { |
127 } | 130 } |
128 | 131 |
129 void DefaultProvider::SetContentSetting( | 132 bool DefaultProvider::SetWebsiteSetting( |
130 const ContentSettingsPattern& primary_pattern, | 133 const ContentSettingsPattern& primary_pattern, |
131 const ContentSettingsPattern& secondary_pattern, | 134 const ContentSettingsPattern& secondary_pattern, |
132 ContentSettingsType content_type, | 135 ContentSettingsType content_type, |
133 const ResourceIdentifier& resource_identifier, | 136 const ResourceIdentifier& resource_identifier, |
134 ContentSetting setting) { | 137 Value* value) { |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
136 DCHECK(prefs_); | 139 DCHECK(prefs_); |
137 | 140 |
138 // Ignore non default settings | 141 // Ignore non default settings |
139 if (primary_pattern != ContentSettingsPattern::Wildcard() || | 142 if (primary_pattern != ContentSettingsPattern::Wildcard() || |
140 secondary_pattern != ContentSettingsPattern::Wildcard()) { | 143 secondary_pattern != ContentSettingsPattern::Wildcard()) { |
141 return; | 144 return false; |
142 } | 145 } |
143 | 146 |
144 // The default settings may not be directly modified for OTR sessions. | 147 // The default settings may not be directly modified for OTR sessions. |
145 // Instead, they are synced to the main profile's setting. | 148 // Instead, they are synced to the main profile's setting. |
146 if (is_incognito_) | 149 if (is_incognito_) |
147 return; | 150 return false; |
148 | 151 |
149 std::string dictionary_path = GetTypeName(content_type); | |
150 { | 152 { |
151 AutoReset<bool> auto_reset(&updating_preferences_, true); | 153 AutoReset<bool> auto_reset(&updating_preferences_, true); |
152 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings); | 154 // Keep the obsolete pref in sync as long as backwards compatibility is |
153 DictionaryValue* default_settings_dictionary = update.Get(); | 155 // required. This is required to keep sync working correctly. |
| 156 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { |
| 157 if (value) { |
| 158 prefs_->Set(prefs::kGeolocationDefaultContentSetting, *value); |
| 159 } else { |
| 160 prefs_->ClearPref(prefs::kGeolocationDefaultContentSetting); |
| 161 } |
| 162 } |
154 | 163 |
155 // |DefaultProvider| should not send any notifications when holding | 164 // |DefaultProvider| should not send any notifications when holding |
156 // |lock_|. |DictionaryPrefUpdate| destructor and | 165 // |lock_|. |DictionaryPrefUpdate| destructor and |
157 // |PrefService::SetInteger()| send out notifications. As a response, the | 166 // |PrefService::SetInteger()| send out notifications. As a response, the |
158 // upper layers may call |GetAllContentSettingRules| which acquires |lock_| | 167 // upper layers may call |GetAllContentSettingRules| which acquires |lock_| |
159 // again. | 168 // again. |
160 { | 169 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings); |
161 base::AutoLock lock(lock_); | 170 DictionaryValue* default_settings_dictionary = update.Get(); |
162 if (setting == CONTENT_SETTING_DEFAULT || | 171 base::AutoLock lock(lock_); |
163 setting == kDefaultSettings[content_type]) { | 172 if (value == NULL || |
164 default_content_settings_[content_type] = | 173 ValueToContentSetting(value) == kDefaultSettings[content_type]) { |
165 kDefaultSettings[content_type]; | 174 // If |value| is NULL we need to reset the default setting the the |
166 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | 175 // hardcoded default. |
167 NULL); | 176 default_settings_[content_type].reset( |
168 } else { | 177 Value::CreateIntegerValue(kDefaultSettings[content_type])); |
169 default_content_settings_[content_type] = setting; | |
170 default_settings_dictionary->SetWithoutPathExpansion( | |
171 dictionary_path, Value::CreateIntegerValue(setting)); | |
172 } | |
173 } | |
174 | 178 |
175 // Keep the obsolete pref in sync as long as backwards compatibility is | 179 // Remove the corresponding pref entry since the hardcoded default value |
176 // required. This is required to keep sync working correctly. | 180 // is used. |
177 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { | 181 default_settings_dictionary->RemoveWithoutPathExpansion( |
178 prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting, | 182 GetTypeName(content_type), NULL); |
179 setting == CONTENT_SETTING_DEFAULT ? | 183 } else { |
180 kDefaultSettings[content_type] : setting); | 184 default_settings_[content_type].reset(value->DeepCopy()); |
| 185 // Transfer ownership of |value| to the |default_settings_dictionary|. |
| 186 default_settings_dictionary->SetWithoutPathExpansion( |
| 187 GetTypeName(content_type), value); |
181 } | 188 } |
182 } | 189 } |
183 | 190 |
184 NotifyObservers(ContentSettingsPattern(), | 191 NotifyObservers(ContentSettingsPattern(), |
185 ContentSettingsPattern(), | 192 ContentSettingsPattern(), |
186 content_type, | 193 content_type, |
187 std::string()); | 194 std::string()); |
| 195 |
| 196 return true; |
188 } | 197 } |
189 | 198 |
190 RuleIterator* DefaultProvider::GetRuleIterator( | 199 RuleIterator* DefaultProvider::GetRuleIterator( |
191 ContentSettingsType content_type, | 200 ContentSettingsType content_type, |
192 const ResourceIdentifier& resource_identifier, | 201 const ResourceIdentifier& resource_identifier, |
193 bool incognito) const { | 202 bool incognito) const { |
194 base::AutoLock lock(lock_); | 203 base::AutoLock lock(lock_); |
195 if (resource_identifier.empty()) { | 204 if (resource_identifier.empty()) { |
196 return new DefaultRuleIterator(default_content_settings_[content_type]); | 205 int int_value = 0; |
| 206 ValueMap::const_iterator it(default_settings_.find(content_type)); |
| 207 if (it != default_settings_.end()) { |
| 208 it->second->GetAsInteger(&int_value); |
| 209 } else { |
| 210 NOTREACHED(); |
| 211 } |
| 212 return new DefaultRuleIterator(ContentSetting(int_value)); |
197 } else { | 213 } else { |
198 return new EmptyRuleIterator(); | 214 return new EmptyRuleIterator(); |
199 } | 215 } |
200 } | 216 } |
201 | 217 |
202 void DefaultProvider::ClearAllContentSettingsRules( | 218 void DefaultProvider::ClearAllContentSettingsRules( |
203 ContentSettingsType content_type) { | 219 ContentSettingsType content_type) { |
204 // TODO(markusheintz): This method is only called when the | 220 // TODO(markusheintz): This method is only called when the |
205 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the | 221 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the |
206 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the | 222 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 } else { | 262 } else { |
247 NOTREACHED() << "Unexpected notification"; | 263 NOTREACHED() << "Unexpected notification"; |
248 } | 264 } |
249 } | 265 } |
250 | 266 |
251 void DefaultProvider::ReadDefaultSettings(bool overwrite) { | 267 void DefaultProvider::ReadDefaultSettings(bool overwrite) { |
252 base::AutoLock lock(lock_); | 268 base::AutoLock lock(lock_); |
253 const DictionaryValue* default_settings_dictionary = | 269 const DictionaryValue* default_settings_dictionary = |
254 prefs_->GetDictionary(prefs::kDefaultContentSettings); | 270 prefs_->GetDictionary(prefs::kDefaultContentSettings); |
255 | 271 |
256 if (overwrite) { | 272 if (overwrite) |
257 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) | 273 default_settings_.clear(); |
258 default_content_settings_[i] = CONTENT_SETTING_DEFAULT; | |
259 } | |
260 | 274 |
261 // Careful: The returned value could be NULL if the pref has never been set. | 275 // Careful: The returned value could be NULL if the pref has never been set. |
262 if (default_settings_dictionary) { | 276 if (default_settings_dictionary) |
263 GetSettingsFromDictionary(default_settings_dictionary, | 277 GetSettingsFromDictionary(default_settings_dictionary); |
264 default_content_settings_); | 278 |
265 } | |
266 ForceDefaultsToBeExplicit(); | 279 ForceDefaultsToBeExplicit(); |
267 } | 280 } |
268 | 281 |
269 void DefaultProvider::ForceDefaultsToBeExplicit() { | 282 void DefaultProvider::ForceDefaultsToBeExplicit() { |
270 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 283 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
271 if (default_content_settings_[i] == CONTENT_SETTING_DEFAULT) | 284 ContentSettingsType type = ContentSettingsType(i); |
272 default_content_settings_[i] = kDefaultSettings[i]; | 285 if (!default_settings_[type].get()) |
| 286 default_settings_[type].reset( |
| 287 Value::CreateIntegerValue(kDefaultSettings[i])); |
273 } | 288 } |
274 } | 289 } |
275 | 290 |
276 void DefaultProvider::GetSettingsFromDictionary( | 291 void DefaultProvider::GetSettingsFromDictionary( |
277 const DictionaryValue* dictionary, | 292 const DictionaryValue* dictionary) { |
278 ContentSetting* settings) { | |
279 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 293 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
280 i != dictionary->end_keys(); ++i) { | 294 i != dictionary->end_keys(); ++i) { |
281 const std::string& content_type(*i); | 295 const std::string& content_type(*i); |
282 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { | 296 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { |
283 if (content_type == GetTypeName(ContentSettingsType(type))) { | 297 if (content_type == GetTypeName(ContentSettingsType(type))) { |
284 int setting = CONTENT_SETTING_DEFAULT; | 298 int int_value = CONTENT_SETTING_DEFAULT; |
285 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | 299 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, |
286 &setting); | 300 &int_value); |
287 DCHECK(found); | 301 DCHECK(found); |
288 settings[type] = IntToContentSetting(setting); | 302 default_settings_[ContentSettingsType(type)].reset( |
| 303 Value::CreateIntegerValue(int_value)); |
289 break; | 304 break; |
290 } | 305 } |
291 } | 306 } |
292 } | 307 } |
293 // Migrate obsolete cookie prompt mode/ | 308 // Migrate obsolete cookie prompt mode. |
294 if (settings[CONTENT_SETTINGS_TYPE_COOKIES] == CONTENT_SETTING_ASK) | 309 if (ValueToContentSetting( |
295 settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | 310 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].get()) == |
| 311 CONTENT_SETTING_ASK) { |
| 312 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].reset( |
| 313 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); |
| 314 } |
296 | 315 |
297 settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | 316 if (default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].get()) { |
298 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | 317 ContentSetting plugin_setting = ValueToContentSetting( |
299 settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | 318 default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].get()); |
| 319 plugin_setting = |
| 320 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, plugin_setting); |
| 321 default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].reset( |
| 322 Value::CreateIntegerValue(plugin_setting)); |
| 323 } |
300 } | 324 } |
301 | 325 |
302 void DefaultProvider::MigrateObsoleteNotificationPref() { | 326 void DefaultProvider::MigrateObsoleteNotificationPref() { |
303 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { | 327 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { |
304 ContentSetting setting = IntToContentSetting( | 328 const base::Value* value = prefs_->FindPreference( |
305 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | 329 prefs::kDesktopNotificationDefaultContentSetting)->GetValue(); |
306 SetContentSetting( | 330 // Do not clear the old preference yet as long as we need to maintain |
| 331 // backward compatibility. |
| 332 SetWebsiteSetting( |
307 ContentSettingsPattern::Wildcard(), | 333 ContentSettingsPattern::Wildcard(), |
308 ContentSettingsPattern::Wildcard(), | 334 ContentSettingsPattern::Wildcard(), |
309 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 335 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
310 std::string(), | 336 std::string(), |
311 setting); | 337 value->DeepCopy()); |
312 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); | 338 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); |
313 } | 339 } |
314 } | 340 } |
315 | 341 |
316 void DefaultProvider::MigrateObsoleteGeolocationPref() { | 342 void DefaultProvider::MigrateObsoleteGeolocationPref() { |
317 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) { | 343 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) { |
318 ContentSetting setting = IntToContentSetting( | 344 const base::Value* value = prefs_->FindPreference( |
319 prefs_->GetInteger(prefs::kGeolocationDefaultContentSetting)); | 345 prefs::kGeolocationDefaultContentSetting)->GetValue(); |
320 // Do not clear the old preference yet as long as we need to maintain | 346 // Do not clear the old preference yet as long as we need to maintain |
321 // backward compatibility. | 347 // backward compatibility. |
322 SetContentSetting( | 348 SetWebsiteSetting( |
323 ContentSettingsPattern::Wildcard(), | 349 ContentSettingsPattern::Wildcard(), |
324 ContentSettingsPattern::Wildcard(), | 350 ContentSettingsPattern::Wildcard(), |
325 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 351 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
326 std::string(), | 352 std::string(), |
327 setting); | 353 value->DeepCopy()); |
328 } | 354 } |
329 } | 355 } |
330 | 356 |
331 } // namespace content_settings | 357 } // namespace content_settings |
OLD | NEW |