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

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

Issue 7328018: Migrate default geolocation content setting to host content settings map. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 5 months 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_pref_provider.h" 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
6 6
7 #include <list> 7 #include <list>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES, 44 COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
45 resource_type_names_incorrect_size); 45 resource_type_names_incorrect_size);
46 46
47 // The default setting for each content type. 47 // The default setting for each content type.
48 const ContentSetting kDefaultSettings[] = { 48 const ContentSetting kDefaultSettings[] = {
49 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES 49 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES
50 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES 50 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES
51 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT 51 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
52 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS 52 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS
53 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS 53 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS
54 CONTENT_SETTING_ASK, // Not used for Geolocation 54 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_GEOLOCATION
55 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS 55 CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS
56 }; 56 };
57 COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES, 57 COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES,
58 default_settings_incorrect_size); 58 default_settings_incorrect_size);
59 59
60 // The names of the ContentSettingsType values, for use with dictionary prefs. 60 // The names of the ContentSettingsType values, for use with dictionary prefs.
61 const char* kTypeNames[] = { 61 const char* kTypeNames[] = {
62 "cookies", 62 "cookies",
63 "images", 63 "images",
64 "javascript", 64 "javascript",
65 "plugins", 65 "plugins",
66 "popups", 66 "popups",
67 NULL, // Not used for Geolocation
68 // TODO(markusheintz): Refactoring in progress. Content settings exceptions 67 // TODO(markusheintz): Refactoring in progress. Content settings exceptions
69 // for notifications will be added next. 68 // for notifications and geolocation will be added next.
69 "geolocation", // Only used for default Geolocation settings
70 "notifications", // Only used for default Notifications settings. 70 "notifications", // Only used for default Notifications settings.
71 }; 71 };
72 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES, 72 COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
73 type_names_incorrect_size); 73 type_names_incorrect_size);
74 74
75 void SetDefaultContentSettings(DictionaryValue* default_settings) { 75 void SetDefaultContentSettings(DictionaryValue* default_settings) {
76 default_settings->Clear(); 76 default_settings->Clear();
77 77
78 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 78 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
79 if (kTypeNames[i] != NULL) { 79 if (kTypeNames[i] != NULL) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 148
149 namespace content_settings { 149 namespace content_settings {
150 150
151 PrefDefaultProvider::PrefDefaultProvider(PrefService* prefs, 151 PrefDefaultProvider::PrefDefaultProvider(PrefService* prefs,
152 bool incognito) 152 bool incognito)
153 : prefs_(prefs), 153 : prefs_(prefs),
154 is_incognito_(incognito), 154 is_incognito_(incognito),
155 updating_preferences_(false) { 155 updating_preferences_(false) {
156 DCHECK(prefs_); 156 DCHECK(prefs_);
157 MigrateObsoleteNotificationPref(); 157 MigrateObsoleteNotificationPref();
158 MigrateObsoleteGeolocationPref();
158 159
159 // Read global defaults. 160 // Read global defaults.
160 DCHECK_EQ(arraysize(kTypeNames), 161 DCHECK_EQ(arraysize(kTypeNames),
161 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); 162 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
162 ReadDefaultSettings(true); 163 ReadDefaultSettings(true);
163 if (default_content_settings_.settings[CONTENT_SETTINGS_TYPE_COOKIES] == 164 if (default_content_settings_.settings[CONTENT_SETTINGS_TYPE_COOKIES] ==
164 CONTENT_SETTING_BLOCK) { 165 CONTENT_SETTING_BLOCK) {
165 UserMetrics::RecordAction( 166 UserMetrics::RecordAction(
166 UserMetricsAction("CookieBlockingEnabledPerDefault")); 167 UserMetricsAction("CookieBlockingEnabledPerDefault"));
167 } else { 168 } else {
168 UserMetrics::RecordAction( 169 UserMetrics::RecordAction(
169 UserMetricsAction("CookieBlockingDisabledPerDefault")); 170 UserMetricsAction("CookieBlockingDisabledPerDefault"));
170 } 171 }
171 172
172 pref_change_registrar_.Init(prefs_); 173 pref_change_registrar_.Init(prefs_);
173 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); 174 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this);
175 pref_change_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this);
174 } 176 }
175 177
176 PrefDefaultProvider::~PrefDefaultProvider() { 178 PrefDefaultProvider::~PrefDefaultProvider() {
177 DCHECK(!prefs_); 179 DCHECK(!prefs_);
178 } 180 }
179 181
180 ContentSetting PrefDefaultProvider::ProvideDefaultSetting( 182 ContentSetting PrefDefaultProvider::ProvideDefaultSetting(
181 ContentSettingsType content_type) const { 183 ContentSettingsType content_type) const {
182 base::AutoLock lock(lock_); 184 base::AutoLock lock(lock_);
183 return default_content_settings_.settings[content_type]; 185 return default_content_settings_.settings[content_type];
184 } 186 }
185 187
186 void PrefDefaultProvider::UpdateDefaultSetting( 188 void PrefDefaultProvider::UpdateDefaultSetting(
187 ContentSettingsType content_type, 189 ContentSettingsType content_type,
188 ContentSetting setting) { 190 ContentSetting setting) {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 DCHECK(prefs_); 192 DCHECK(prefs_);
191 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 193 DCHECK(kTypeNames[content_type] != NULL);
192 194
193 // The default settings may not be directly modified for OTR sessions. 195 // The default settings may not be directly modified for OTR sessions.
194 // Instead, they are synced to the main profile's setting. 196 // Instead, they are synced to the main profile's setting.
195 if (is_incognito_) 197 if (is_incognito_)
196 return; 198 return;
197 199
198 std::string dictionary_path(kTypeNames[content_type]); 200 std::string dictionary_path(kTypeNames[content_type]);
199 { 201 {
200 AutoReset<bool> auto_reset(&updating_preferences_, true); 202 AutoReset<bool> auto_reset(&updating_preferences_, true);
201 base::AutoLock lock(lock_); 203 base::AutoLock lock(lock_);
202 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings); 204 DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings);
203 DictionaryValue* default_settings_dictionary = update.Get(); 205 DictionaryValue* default_settings_dictionary = update.Get();
204 if ((setting == CONTENT_SETTING_DEFAULT) || 206 if ((setting == CONTENT_SETTING_DEFAULT) ||
205 (setting == kDefaultSettings[content_type])) { 207 (setting == kDefaultSettings[content_type])) {
206 default_content_settings_.settings[content_type] = 208 default_content_settings_.settings[content_type] =
207 kDefaultSettings[content_type]; 209 kDefaultSettings[content_type];
208 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, 210 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
209 NULL); 211 NULL);
210 } else { 212 } else {
211 default_content_settings_.settings[content_type] = setting; 213 default_content_settings_.settings[content_type] = setting;
212 default_settings_dictionary->SetWithoutPathExpansion( 214 default_settings_dictionary->SetWithoutPathExpansion(
213 dictionary_path, Value::CreateIntegerValue(setting)); 215 dictionary_path, Value::CreateIntegerValue(setting));
214 } 216 }
217
218 // Keep the obsolete pref in sync as long as backwards compatibility is
219 // required. This is required to keep sync working correctly.
220 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
221 prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting,
222 setting == CONTENT_SETTING_DEFAULT ?
223 kDefaultSettings[content_type] : setting);
224 }
215 } 225 }
216 226
217 NotifyObservers(ContentSettingsPattern(), 227 NotifyObservers(ContentSettingsPattern(),
218 ContentSettingsPattern(), 228 ContentSettingsPattern(),
219 content_type, 229 content_type,
220 std::string()); 230 std::string());
221 } 231 }
222 232
223 bool PrefDefaultProvider::DefaultSettingIsManaged( 233 bool PrefDefaultProvider::DefaultSettingIsManaged(
224 ContentSettingsType content_type) const { 234 ContentSettingsType content_type) const {
225 return false; 235 return false;
226 } 236 }
227 237
228 void PrefDefaultProvider::Observe(int type, 238 void PrefDefaultProvider::Observe(int type,
229 const NotificationSource& source, 239 const NotificationSource& source,
230 const NotificationDetails& details) { 240 const NotificationDetails& details) {
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
232 242
233 if (type == chrome::NOTIFICATION_PREF_CHANGED) { 243 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
234 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); 244 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
235 if (updating_preferences_) 245 if (updating_preferences_)
236 return; 246 return;
237 247
238 std::string* name = Details<std::string>(details).ptr(); 248 std::string* name = Details<std::string>(details).ptr();
239 if (*name == prefs::kDefaultContentSettings) { 249 if (*name == prefs::kDefaultContentSettings) {
240 ReadDefaultSettings(true); 250 ReadDefaultSettings(true);
251 } else if (*name == prefs::kGeolocationDefaultContentSetting) {
252 MigrateObsoleteGeolocationPref();
253 // Return and don't send a notifications. Migrating the obsolete
254 // geolocation pref will change the prefs::kDefaultContentSettings and
255 // cause the notification to be fired.
256 return;
241 } else { 257 } else {
242 NOTREACHED() << "Unexpected preference observed"; 258 NOTREACHED() << "Unexpected preference observed";
243 return; 259 return;
244 } 260 }
245 261
246 NotifyObservers(ContentSettingsPattern(), 262 NotifyObservers(ContentSettingsPattern(),
247 ContentSettingsPattern(), 263 ContentSettingsPattern(),
248 CONTENT_SETTINGS_TYPE_DEFAULT, 264 CONTENT_SETTINGS_TYPE_DEFAULT,
249 std::string()); 265 std::string());
250 } else { 266 } else {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 332
317 void PrefDefaultProvider::MigrateObsoleteNotificationPref() { 333 void PrefDefaultProvider::MigrateObsoleteNotificationPref() {
318 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) { 334 if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) {
319 ContentSetting setting = IntToContentSetting( 335 ContentSetting setting = IntToContentSetting(
320 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); 336 prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting));
321 UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); 337 UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
322 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting); 338 prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting);
323 } 339 }
324 } 340 }
325 341
342 void PrefDefaultProvider::MigrateObsoleteGeolocationPref() {
343 if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) {
344 ContentSetting setting = IntToContentSetting(
345 prefs_->GetInteger(prefs::kGeolocationDefaultContentSetting));
346 UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION, setting);
347 // Do not clear the old preference yet as long as we need to maintain
348 // backward compatibility.
349 }
350 }
351
326 // static 352 // static
327 void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) { 353 void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) {
328 // The registration of the preference prefs::kDefaultContentSettings should 354 // The registration of the preference prefs::kDefaultContentSettings should
329 // also include the default values for default content settings. This allows 355 // also include the default values for default content settings. This allows
330 // functional tests to get default content settings by reading the preference 356 // functional tests to get default content settings by reading the preference
331 // prefs::kDefaultContentSettings via pyauto. 357 // prefs::kDefaultContentSettings via pyauto.
332 // TODO(markusheintz): Write pyauto hooks for the content settings map as 358 // TODO(markusheintz): Write pyauto hooks for the content settings map as
333 // content settings should be read from the host content settings map. 359 // content settings should be read from the host content settings map.
334 DictionaryValue* default_content_settings = new DictionaryValue(); 360 DictionaryValue* default_content_settings = new DictionaryValue();
335 SetDefaultContentSettings(default_content_settings); 361 SetDefaultContentSettings(default_content_settings);
336 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings, 362 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings,
337 default_content_settings, 363 default_content_settings,
338 PrefService::SYNCABLE_PREF); 364 PrefService::SYNCABLE_PREF);
339 365
340 // Obsolete prefs, for migrations: 366 // Obsolete prefs, for migrations:
341 prefs->RegisterIntegerPref( 367 prefs->RegisterIntegerPref(
342 prefs::kDesktopNotificationDefaultContentSetting, 368 prefs::kDesktopNotificationDefaultContentSetting,
343 kDefaultSettings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS], 369 kDefaultSettings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS],
344 PrefService::SYNCABLE_PREF); 370 PrefService::SYNCABLE_PREF);
371 prefs->RegisterIntegerPref(
372 prefs::kGeolocationDefaultContentSetting,
373 kDefaultSettings[CONTENT_SETTINGS_TYPE_GEOLOCATION],
374 PrefService::UNSYNCABLE_PREF);
345 } 375 }
346 376
347 // //////////////////////////////////////////////////////////////////////////// 377 // ////////////////////////////////////////////////////////////////////////////
348 // PrefProvider: 378 // PrefProvider:
349 // 379 //
350 380
351 // static 381 // static
352 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { 382 void PrefProvider::RegisterUserPrefs(PrefService* prefs) {
353 prefs->RegisterIntegerPref( 383 prefs->RegisterIntegerPref(
354 prefs::kContentSettingsVersion, 384 prefs::kContentSettingsVersion,
(...skipping 24 matching lines...) Expand all
379 MigrateObsoleteContentSettingsPatternPref(); 409 MigrateObsoleteContentSettingsPatternPref();
380 } 410 }
381 411
382 // Verify preferences version. 412 // Verify preferences version.
383 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { 413 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
384 prefs_->SetInteger(prefs::kContentSettingsVersion, 414 prefs_->SetInteger(prefs::kContentSettingsVersion,
385 ContentSettingsPattern::kContentSettingsPatternVersion); 415 ContentSettingsPattern::kContentSettingsPatternVersion);
386 } 416 }
387 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > 417 if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
388 ContentSettingsPattern::kContentSettingsPatternVersion) { 418 ContentSettingsPattern::kContentSettingsPatternVersion) {
389 LOG(ERROR) << "Unknown content settings version in preferences.";
390 return; 419 return;
391 } 420 }
392 421
393 // Read content settings exceptions. 422 // Read content settings exceptions.
394 ReadContentSettingsFromPref(false); 423 ReadContentSettingsFromPref(false);
395 424
396 if (!is_incognito_) { 425 if (!is_incognito_) {
397 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", 426 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
398 value_map_.size()); 427 value_map_.size());
399 } 428 }
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 DCHECK(found); 1054 DCHECK(found);
1026 std::string new_key = pattern_pair.first.ToString(); 1055 std::string new_key = pattern_pair.first.ToString();
1027 // Existing values are overwritten. 1056 // Existing values are overwritten.
1028 obsolete_settings_dictionary->SetWithoutPathExpansion( 1057 obsolete_settings_dictionary->SetWithoutPathExpansion(
1029 new_key, dictionary->DeepCopy()); 1058 new_key, dictionary->DeepCopy());
1030 } 1059 }
1031 } 1060 }
1032 } 1061 }
1033 1062
1034 } // namespace content_settings 1063 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698