Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/content_settings/content_settings_default_provider.cc

Issue 8539004: Replace SetContentSetting method of the content_settings::Provider interface with GetWebsiteSetting (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 replace with a |Value|.
Bernhard Bauer 2011/11/11 14:00:40 Nit: "replaced".
markusheintz_ 2011/11/14 11:15:10 Done.
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
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 int int_value;
115 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES]->GetAsInteger(&int_value);
Bernhard Bauer 2011/11/11 14:00:40 Can you use the ValueToContentSetting helper metho
markusheintz_ 2011/11/14 11:15:10 Done.
116 if (IntToContentSetting(int_value) == 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 void 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 const 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;
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;
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.
167 scoped_ptr<base::Value> default_value(
168 Value::CreateIntegerValue(kDefaultSettings[content_type]));
Bernhard Bauer 2011/11/11 14:00:40 If you get the integer value from |value| you can
markusheintz_ 2011/11/14 11:15:10 Done.
160 { 169 {
161 base::AutoLock lock(lock_); 170 base::AutoLock lock(lock_);
162 if (setting == CONTENT_SETTING_DEFAULT || 171 if (value == NULL ||
163 setting == kDefaultSettings[content_type]) { 172 value->Equals(default_value.get())) {
164 default_content_settings_.settings[content_type] = 173 // If |value| is NULL we need to reset the default setting the the
165 kDefaultSettings[content_type]; 174 // hardcoded default.
175 default_settings_[content_type].reset(
176 Value::CreateIntegerValue(kDefaultSettings[content_type]));
177
178 // If |value| is null or equal to the hardcoded default setting then
179 // remove the corresponding pref entry as it is not needed.
166 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, 180 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
167 NULL); 181 NULL);
168 } else { 182 } else {
169 default_content_settings_.settings[content_type] = setting; 183 default_settings_[content_type].reset(value->DeepCopy());
170 default_settings_dictionary->SetWithoutPathExpansion( 184 default_settings_dictionary->SetWithoutPathExpansion(
171 dictionary_path, Value::CreateIntegerValue(setting)); 185 dictionary_path, value->DeepCopy());
172 } 186 }
173 } 187 }
174 188
175 // Keep the obsolete pref in sync as long as backwards compatibility is 189 // Keep the obsolete pref in sync as long as backwards compatibility is
176 // required. This is required to keep sync working correctly. 190 // required. This is required to keep sync working correctly.
177 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) { 191 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
178 prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting, 192 prefs_->Set(prefs::kGeolocationDefaultContentSetting,
179 setting == CONTENT_SETTING_DEFAULT ? 193 value == NULL ? *default_value : *value);
180 kDefaultSettings[content_type] : setting);
181 } 194 }
182 } 195 }
183 196
184 NotifyObservers(ContentSettingsPattern(), 197 NotifyObservers(ContentSettingsPattern(),
185 ContentSettingsPattern(), 198 ContentSettingsPattern(),
186 content_type, 199 content_type,
187 std::string()); 200 std::string());
188 } 201 }
189 202
190 RuleIterator* DefaultProvider::GetRuleIterator( 203 RuleIterator* DefaultProvider::GetRuleIterator(
191 ContentSettingsType content_type, 204 ContentSettingsType content_type,
192 const ResourceIdentifier& resource_identifier, 205 const ResourceIdentifier& resource_identifier,
193 bool incognito) const { 206 bool incognito) const {
194 base::AutoLock lock(lock_); 207 base::AutoLock lock(lock_);
195 if (resource_identifier.empty()) { 208 if (resource_identifier.empty()) {
196 return new DefaultRuleIterator( 209 int int_value;
Bernhard Bauer 2011/11/11 14:00:40 Initialize this value please.
markusheintz_ 2011/11/14 11:15:10 Done.
197 default_content_settings_.settings[content_type]); 210 ValueMap::const_iterator it(default_settings_.find(content_type));
211 if (it != default_settings_.end()) {
212 it->second->GetAsInteger(&int_value);
213 } else {
214 NOTREACHED();
215 }
216 return new DefaultRuleIterator(ContentSetting(int_value));
198 } else { 217 } else {
199 return new EmptyRuleIterator(); 218 return new EmptyRuleIterator();
200 } 219 }
201 } 220 }
202 221
203 void DefaultProvider::ClearAllContentSettingsRules( 222 void DefaultProvider::ClearAllContentSettingsRules(
204 ContentSettingsType content_type) { 223 ContentSettingsType content_type) {
205 // TODO(markusheintz): This method is only called when the 224 // TODO(markusheintz): This method is only called when the
206 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the 225 // |DesktopNotificationService| calls |ClearAllSettingsForType| method on the
207 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the 226 // |HostContentSettingsMap|. Don't implement this method yet, otherwise the
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 NOTREACHED() << "Unexpected notification"; 267 NOTREACHED() << "Unexpected notification";
249 } 268 }
250 } 269 }
251 270
252 void DefaultProvider::ReadDefaultSettings(bool overwrite) { 271 void DefaultProvider::ReadDefaultSettings(bool overwrite) {
253 base::AutoLock lock(lock_); 272 base::AutoLock lock(lock_);
254 const DictionaryValue* default_settings_dictionary = 273 const DictionaryValue* default_settings_dictionary =
255 prefs_->GetDictionary(prefs::kDefaultContentSettings); 274 prefs_->GetDictionary(prefs::kDefaultContentSettings);
256 275
257 if (overwrite) 276 if (overwrite)
258 default_content_settings_ = ContentSettings(); 277 default_settings_.clear();
259 278
260 // Careful: The returned value could be NULL if the pref has never been set. 279 // Careful: The returned value could be NULL if the pref has never been set.
261 if (default_settings_dictionary) { 280 if (default_settings_dictionary)
262 GetSettingsFromDictionary(default_settings_dictionary, 281 GetSettingsFromDictionary(default_settings_dictionary);
263 &default_content_settings_); 282
264 }
265 ForceDefaultsToBeExplicit(); 283 ForceDefaultsToBeExplicit();
266 } 284 }
267 285
268 void DefaultProvider::ForceDefaultsToBeExplicit() { 286 void DefaultProvider::ForceDefaultsToBeExplicit() {
269 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 287 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
270 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) 288 ContentSettingsType type = ContentSettingsType(i);
271 default_content_settings_.settings[i] = kDefaultSettings[i]; 289 if (!default_settings_[type].get())
290 default_settings_[type].reset(
291 Value::CreateIntegerValue(kDefaultSettings[i]));
272 } 292 }
273 } 293 }
274 294
275 void DefaultProvider::GetSettingsFromDictionary( 295 void DefaultProvider::GetSettingsFromDictionary(
276 const DictionaryValue* dictionary, 296 const DictionaryValue* dictionary) {
277 ContentSettings* settings) {
278 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); 297 for (DictionaryValue::key_iterator i(dictionary->begin_keys());
279 i != dictionary->end_keys(); ++i) { 298 i != dictionary->end_keys(); ++i) {
280 const std::string& content_type(*i); 299 const std::string& content_type(*i);
281 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { 300 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
Bernhard Bauer 2011/11/11 14:00:40 Can you look up the |ContentSettingsType| for |con
markusheintz_ 2011/11/14 11:15:10 Yes I could.But I thought I keep the change small
282 if (content_type == GetTypeName(ContentSettingsType(type))) { 301 if (content_type == GetTypeName(ContentSettingsType(type))) {
283 int setting = CONTENT_SETTING_DEFAULT; 302 base::Value* value = NULL;
284 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, 303 bool found = dictionary->GetWithoutPathExpansion(content_type, &value);
285 &setting); 304
286 DCHECK(found); 305 DCHECK(found);
287 settings->settings[type] = IntToContentSetting(setting); 306 default_settings_[ContentSettingsType(type)].reset(value->DeepCopy());
Bernhard Bauer 2011/11/11 14:00:40 If we don't find a value, the |DeepCopy| will cras
markusheintz_ 2011/11/14 11:15:10 Done.
307
288 break; 308 break;
289 } 309 }
290 } 310 }
291 } 311 }
292 // Migrate obsolete cookie prompt mode/ 312 // Migrate obsolete cookie prompt mode/
Bernhard Bauer 2011/11/11 14:00:40 Nit: Slash at the end of the comment
markusheintz_ 2011/11/14 11:15:10 Done.
293 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == 313 if (ValueEqualsContentSetting(
Bernhard Bauer 2011/11/11 14:00:40 Oh, we have that method? Then you could use it in
markusheintz_ 2011/11/14 11:15:10 Obsolete as I removed the method ValueEqualsConten
294 CONTENT_SETTING_ASK) 314 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].get(),
295 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; 315 CONTENT_SETTING_ASK)) {
316 default_settings_[CONTENT_SETTINGS_TYPE_COOKIES].reset(
317 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
318 }
296 319
297 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = 320 if (default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].get()) {
298 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, 321 ContentSetting plugin_setting = ValueToContentSetting(
299 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); 322 default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].get());
323 plugin_setting =
324 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, plugin_setting);
325 default_settings_[CONTENT_SETTINGS_TYPE_PLUGINS].reset(
326 Value::CreateIntegerValue(plugin_setting));
327 }
300 } 328 }
301 329
302 void DefaultProvider::MigrateObsoleteNotificationPref() { 330 void DefaultProvider::MigrateObsoleteNotificationPref() {
303 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { 331 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) {
304 ContentSetting setting = IntToContentSetting( 332 const base::Value* value = prefs_->FindPreference(
305 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); 333 prefs::kDesktopNotificationDefaultContentSetting)->GetValue();
306 SetContentSetting( 334 // Do not clear the old preference yet as long as we need to maintain
335 // backward compatibility.
336 SetWebsiteSetting(
307 ContentSettingsPattern::Wildcard(), 337 ContentSettingsPattern::Wildcard(),
308 ContentSettingsPattern::Wildcard(), 338 ContentSettingsPattern::Wildcard(),
309 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 339 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
310 std::string(), 340 std::string(),
311 setting); 341 value);
312 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); 342 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting);
313 } 343 }
314 } 344 }
315 345
316 void DefaultProvider::MigrateObsoleteGeolocationPref() { 346 void DefaultProvider::MigrateObsoleteGeolocationPref() {
317 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) { 347 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) {
318 ContentSetting setting = IntToContentSetting( 348 const base::Value* value = prefs_->FindPreference(
319 prefs_->GetInteger(prefs::kGeolocationDefaultContentSetting)); 349 prefs::kGeolocationDefaultContentSetting)->GetValue();
320 // Do not clear the old preference yet as long as we need to maintain 350 // Do not clear the old preference yet as long as we need to maintain
321 // backward compatibility. 351 // backward compatibility.
322 SetContentSetting( 352 SetWebsiteSetting(
323 ContentSettingsPattern::Wildcard(), 353 ContentSettingsPattern::Wildcard(),
324 ContentSettingsPattern::Wildcard(), 354 ContentSettingsPattern::Wildcard(),
325 CONTENT_SETTINGS_TYPE_GEOLOCATION, 355 CONTENT_SETTINGS_TYPE_GEOLOCATION,
326 std::string(), 356 std::string(),
327 setting); 357 value);
328 } 358 }
329 } 359 }
330 360
331 } // namespace content_settings 361 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698