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_.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); | 152 std::string dictionary_path = GetTypeName(content_type); |
150 { | 153 { |
151 AutoReset<bool> auto_reset(&updating_preferences_, true); | 154 AutoReset<bool> auto_reset(&updating_preferences_, true); |
152 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings); | 155 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings); |
153 DictionaryValue* default_settings_dictionary = update.Get(); | 156 DictionaryValue* default_settings_dictionary = update.Get(); |
154 | 157 |
155 // |DefaultProvider| should not send any notifications when holding | 158 // |DefaultProvider| should not send any notifications when holding |
156 // |lock_|. |DictionaryPrefUpdate| destructor and | 159 // |lock_|. |DictionaryPrefUpdate| destructor and |
157 // |PrefService::SetInteger()| send out notifications. As a response, the | 160 // |PrefService::SetInteger()| send out notifications. As a response, the |
158 // upper layers may call |GetAllContentSettingRules| which acquires |lock_| | 161 // upper layers may call |GetAllContentSettingRules| which acquires |lock_| |
159 // again. | 162 // again. |
163 // TODO(markusheintz): Hardcoded default settings should be handled by the | |
164 // HostContentSettingsMap itself. The DefaultProvider should only deal with | |
165 // user set default values and should return NULL if no user defined | |
166 // settings are avaliable. | |
160 { | 167 { |
161 base::AutoLock lock(lock_); | 168 base::AutoLock lock(lock_); |
162 if (setting == CONTENT_SETTING_DEFAULT || | 169 if (value == NULL || |
163 setting == kDefaultSettings[content_type]) { | 170 ValueToContentSetting(value) == kDefaultSettings[content_type]) { |
164 default_content_settings_.settings[content_type] = | 171 // If |value| is NULL we need to reset the default setting the the |
165 kDefaultSettings[content_type]; | 172 // hardcoded default. |
173 default_settings_[content_type].reset( | |
174 Value::CreateIntegerValue(kDefaultSettings[content_type])); | |
175 | |
176 // If |value| is null or equal to the hardcoded default setting then | |
177 // remove the corresponding pref entry as it is not needed. | |
166 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | 178 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, |
167 NULL); | 179 NULL); |
168 } else { | 180 } else { |
169 default_content_settings_.settings[content_type] = setting; | 181 default_settings_[content_type].reset(value->DeepCopy()); |
170 default_settings_dictionary->SetWithoutPathExpansion( | 182 default_settings_dictionary->SetWithoutPathExpansion( |
171 dictionary_path, Value::CreateIntegerValue(setting)); | 183 dictionary_path, value); |
172 } | 184 } |
173 } | 185 } |
174 | 186 |
175 // Keep the obsolete pref in sync as long as backwards compatibility is | 187 // Keep the obsolete pref in sync as long as backwards compatibility is |
176 // required. This is required to keep sync working correctly. | 188 // required. This is required to keep sync working correctly. |
177 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { | 189 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { |
178 prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting, | 190 if (value) { |
179 setting == CONTENT_SETTING_DEFAULT ? | 191 prefs_->Set(prefs::kGeolocationDefaultContentSetting, *value); |
Bernhard Bauer
2011/11/15 15:40:28
I'm not happy that we pass ownership of |value| to
markusheintz_
2011/11/15 17:36:58
Done.
| |
180 kDefaultSettings[content_type] : setting); | 192 } else { |
193 prefs_->ClearPref(prefs::kGeolocationDefaultContentSetting); | |
194 } | |
181 } | 195 } |
182 } | 196 } |
183 | 197 |
184 NotifyObservers(ContentSettingsPattern(), | 198 NotifyObservers(ContentSettingsPattern(), |
185 ContentSettingsPattern(), | 199 ContentSettingsPattern(), |
186 content_type, | 200 content_type, |
187 std::string()); | 201 std::string()); |
202 | |
203 return true; | |
188 } | 204 } |
189 | 205 |
190 RuleIterator* DefaultProvider::GetRuleIterator( | 206 RuleIterator* DefaultProvider::GetRuleIterator( |
191 ContentSettingsType content_type, | 207 ContentSettingsType content_type, |
192 const ResourceIdentifier& resource_identifier, | 208 const ResourceIdentifier& resource_identifier, |
193 bool incognito) const { | 209 bool incognito) const { |
194 base::AutoLock lock(lock_); | 210 base::AutoLock lock(lock_); |
195 if (resource_identifier.empty()) { | 211 if (resource_identifier.empty()) { |
196 return new DefaultRuleIterator( | 212 int int_value = 0; |
197 default_content_settings_.settings[content_type]); | 213 ValueMap::const_iterator it(default_settings_.find(content_type)); |
214 if (it != default_settings_.end()) { | |
215 it->second->GetAsInteger(&int_value); | |
216 } else { | |
217 NOTREACHED(); | |
218 } | |
219 return new DefaultRuleIterator(ContentSetting(int_value)); | |
198 } else { | 220 } else { |
199 return new EmptyRuleIterator(); | 221 return new EmptyRuleIterator(); |
200 } | 222 } |
201 } | 223 } |
202 | 224 |
203 void DefaultProvider::ClearAllContentSettingsRules( | 225 void DefaultProvider::ClearAllContentSettingsRules( |
204 ContentSettingsType content_type) { | 226 ContentSettingsType content_type) { |
205 // TODO(markusheintz): This method is only called when the | 227 // TODO(markusheintz): This method is only called when the |
206 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the | 228 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the |
207 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the | 229 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 NOTREACHED() << "Unexpected notification"; | 270 NOTREACHED() << "Unexpected notification"; |
249 } | 271 } |
250 } | 272 } |
251 | 273 |
252 void DefaultProvider::ReadDefaultSettings(bool overwrite) { | 274 void DefaultProvider::ReadDefaultSettings(bool overwrite) { |
253 base::AutoLock lock(lock_); | 275 base::AutoLock lock(lock_); |
254 const DictionaryValue* default_settings_dictionary = | 276 const DictionaryValue* default_settings_dictionary = |
255 prefs_->GetDictionary(prefs::kDefaultContentSettings); | 277 prefs_->GetDictionary(prefs::kDefaultContentSettings); |
256 | 278 |
257 if (overwrite) | 279 if (overwrite) |
258 default_content_settings_ = ContentSettings(); | 280 default_settings_.clear(); |
259 | 281 |
260 // Careful: The returned value could be NULL if the pref has never been set. | 282 // Careful: The returned value could be NULL if the pref has never been set. |
261 if (default_settings_dictionary) { | 283 if (default_settings_dictionary) |
262 GetSettingsFromDictionary(default_settings_dictionary, | 284 GetSettingsFromDictionary(default_settings_dictionary); |
263 &default_content_settings_); | 285 |
264 } | |
265 ForceDefaultsToBeExplicit(); | 286 ForceDefaultsToBeExplicit(); |
266 } | 287 } |
267 | 288 |
268 void DefaultProvider::ForceDefaultsToBeExplicit() { | 289 void DefaultProvider::ForceDefaultsToBeExplicit() { |
269 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 290 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
270 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) | 291 ContentSettingsType type = ContentSettingsType(i); |
271 default_content_settings_.settings[i] = kDefaultSettings[i]; | 292 if (!default_settings_[type].get()) |
293 default_settings_[type].reset( | |
294 Value::CreateIntegerValue(kDefaultSettings[i])); | |
272 } | 295 } |
273 } | 296 } |
274 | 297 |
275 void DefaultProvider::GetSettingsFromDictionary( | 298 void DefaultProvider::GetSettingsFromDictionary( |
276 const DictionaryValue* dictionary, | 299 const DictionaryValue* dictionary) { |
277 ContentSettings* settings) { | |
278 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | 300 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
279 i != dictionary->end_keys(); ++i) { | 301 i != dictionary->end_keys(); ++i) { |
280 const std::string& content_type(*i); | 302 const std::string& content_type(*i); |
281 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { | 303 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { |
282 if (content_type == GetTypeName(ContentSettingsType(type))) { | 304 if (content_type == GetTypeName(ContentSettingsType(type))) { |
283 int setting = CONTENT_SETTING_DEFAULT; | 305 int int_value = CONTENT_SETTING_DEFAULT; |
284 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | 306 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, |
285 &setting); | 307 &int_value); |
286 DCHECK(found); | 308 DCHECK(found); |
287 settings->settings[type] = IntToContentSetting(setting); | 309 default_settings_[ContentSettingsType(type)].reset( |
310 Value::CreateIntegerValue(int_value)); | |
288 break; | 311 break; |
289 } | 312 } |
290 } | 313 } |
291 } | 314 } |
292 // Migrate obsolete cookie prompt mode/ | 315 // Migrate obsolete cookie prompt mode. |
293 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | 316 if (ValueToContentSetting( |
294 CONTENT_SETTING_ASK) | 317 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].get()) == |
295 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | 318 CONTENT_SETTING_ASK) { |
319 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].reset( | |
320 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK)); | |
321 } | |
296 | 322 |
297 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | 323 if (default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].get()) { |
298 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | 324 ContentSetting plugin_setting = ValueToContentSetting( |
299 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | 325 default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].get()); |
326 plugin_setting = | |
327 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, plugin_setting); | |
328 default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].reset( | |
329 Value::CreateIntegerValue(plugin_setting)); | |
330 } | |
300 } | 331 } |
301 | 332 |
302 void DefaultProvider::MigrateObsoleteNotificationPref() { | 333 void DefaultProvider::MigrateObsoleteNotificationPref() { |
303 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { | 334 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { |
304 ContentSetting setting = IntToContentSetting( | 335 const base::Value* value = prefs_->FindPreference( |
305 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | 336 prefs::kDesktopNotificationDefaultContentSetting)->GetValue(); |
306 SetContentSetting( | 337 // Do not clear the old preference yet as long as we need to maintain |
338 // backward compatibility. | |
339 SetWebsiteSetting( | |
307 ContentSettingsPattern::Wildcard(), | 340 ContentSettingsPattern::Wildcard(), |
308 ContentSettingsPattern::Wildcard(), | 341 ContentSettingsPattern::Wildcard(), |
309 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 342 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
310 std::string(), | 343 std::string(), |
311 setting); | 344 value->DeepCopy()); |
312 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); | 345 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); |
313 } | 346 } |
314 } | 347 } |
315 | 348 |
316 void DefaultProvider::MigrateObsoleteGeolocationPref() { | 349 void DefaultProvider::MigrateObsoleteGeolocationPref() { |
317 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) { | 350 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) { |
318 ContentSetting setting = IntToContentSetting( | 351 const base::Value* value = prefs_->FindPreference( |
319 prefs_->GetInteger(prefs::kGeolocationDefaultContentSetting)); | 352 prefs::kGeolocationDefaultContentSetting)->GetValue(); |
320 // Do not clear the old preference yet as long as we need to maintain | 353 // Do not clear the old preference yet as long as we need to maintain |
321 // backward compatibility. | 354 // backward compatibility. |
322 SetContentSetting( | 355 SetWebsiteSetting( |
323 ContentSettingsPattern::Wildcard(), | 356 ContentSettingsPattern::Wildcard(), |
324 ContentSettingsPattern::Wildcard(), | 357 ContentSettingsPattern::Wildcard(), |
325 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 358 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
326 std::string(), | 359 std::string(), |
327 setting); | 360 value->DeepCopy()); |
328 } | 361 } |
329 } | 362 } |
330 | 363 |
331 } // namespace content_settings | 364 } // namespace content_settings |
OLD | NEW |