| 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_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 default_settings->Clear(); | 78 default_settings->Clear(); |
| 79 | 79 |
| 80 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | 80 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { |
| 81 if (kTypeNames[i] != NULL) { | 81 if (kTypeNames[i] != NULL) { |
| 82 default_settings->SetInteger(kTypeNames[i], | 82 default_settings->SetInteger(kTypeNames[i], |
| 83 kDefaultSettings[i]); | 83 kDefaultSettings[i]); |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 std::string CreatePatternString( |
| 89 const ContentSettingsPattern& requesting_pattern, |
| 90 const ContentSettingsPattern& embedding_pattern) { |
| 91 DCHECK(requesting_pattern == embedding_pattern); |
| 92 return requesting_pattern.ToString(); |
| 93 } |
| 94 |
| 95 ContentSetting ValueToContentSetting(Value* value) { |
| 96 int int_value; |
| 97 value->GetAsInteger(&int_value); |
| 98 return IntToContentSetting(int_value); |
| 99 } |
| 100 |
| 101 ContentSettingsType StringToContentSettingsType( |
| 102 const std::string& content_type_str) { |
| 103 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { |
| 104 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type_str)) |
| 105 return ContentSettingsType(type); |
| 106 } |
| 107 for (size_t type = 0; type < arraysize(kResourceTypeNames); ++type) { |
| 108 if ((kResourceTypeNames[type] != NULL) && |
| 109 (kResourceTypeNames[type] == content_type_str)) { |
| 110 return ContentSettingsType(type); |
| 111 } |
| 112 } |
| 113 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 114 } |
| 115 |
| 116 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, |
| 117 ContentSetting setting) { |
| 118 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && |
| 119 setting == CONTENT_SETTING_ASK) { |
| 120 return CONTENT_SETTING_BLOCK; |
| 121 } |
| 122 return setting; |
| 123 } |
| 124 |
| 88 } // namespace | 125 } // namespace |
| 89 | 126 |
| 90 namespace content_settings { | 127 namespace content_settings { |
| 91 | 128 |
| 92 PrefDefaultProvider::PrefDefaultProvider(Profile* profile) | 129 PrefDefaultProvider::PrefDefaultProvider(Profile* profile) |
| 93 : profile_(profile), | 130 : profile_(profile), |
| 94 is_incognito_(profile_->IsOffTheRecord()), | 131 is_incognito_(profile_->IsOffTheRecord()), |
| 95 updating_preferences_(false) { | 132 updating_preferences_(false) { |
| 96 initializing_ = true; | 133 initializing_ = true; |
| 97 PrefService* prefs = profile->GetPrefs(); | 134 PrefService* prefs = profile->GetPrefs(); |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 PrefService::SYNCABLE_PREF); | 375 PrefService::SYNCABLE_PREF); |
| 339 | 376 |
| 340 // Obsolete prefs, for migration: | 377 // Obsolete prefs, for migration: |
| 341 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, | 378 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts, |
| 342 PrefService::UNSYNCABLE_PREF); | 379 PrefService::UNSYNCABLE_PREF); |
| 343 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, | 380 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings, |
| 344 PrefService::UNSYNCABLE_PREF); | 381 PrefService::UNSYNCABLE_PREF); |
| 345 } | 382 } |
| 346 | 383 |
| 347 PrefProvider::PrefProvider(Profile* profile) | 384 PrefProvider::PrefProvider(Profile* profile) |
| 348 : BaseProvider(profile->IsOffTheRecord()), | 385 : profile_(profile), |
| 349 profile_(profile), | 386 is_incognito_(profile_->IsOffTheRecord()), |
| 350 updating_preferences_(false) { | 387 updating_preferences_(false) { |
| 351 Init(); | 388 Init(); |
| 352 } | 389 } |
| 353 | 390 |
| 354 void PrefProvider::Init() { | 391 void PrefProvider::Init() { |
| 355 initializing_ = true; | 392 initializing_ = true; |
| 356 PrefService* prefs = profile_->GetPrefs(); | 393 PrefService* prefs = profile_->GetPrefs(); |
| 357 | 394 |
| 358 // Migrate obsolete preferences. | 395 // Migrate obsolete preferences. |
| 359 MigrateObsoletePerhostPref(prefs); | 396 MigrateObsoletePerhostPref(prefs); |
| 360 MigrateObsoletePopupsPref(prefs); | 397 MigrateObsoletePopupsPref(prefs); |
| 361 | 398 |
| 362 // Verify preferences version. | 399 // Verify preferences version. |
| 363 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { | 400 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { |
| 364 prefs->SetInteger(prefs::kContentSettingsVersion, | 401 prefs->SetInteger(prefs::kContentSettingsVersion, |
| 365 ContentSettingsPattern::kContentSettingsPatternVersion); | 402 ContentSettingsPattern::kContentSettingsPatternVersion); |
| 366 } | 403 } |
| 367 if (prefs->GetInteger(prefs::kContentSettingsVersion) > | 404 if (prefs->GetInteger(prefs::kContentSettingsVersion) > |
| 368 ContentSettingsPattern::kContentSettingsPatternVersion) { | 405 ContentSettingsPattern::kContentSettingsPatternVersion) { |
| 369 LOG(ERROR) << "Unknown content settings version in preferences."; | 406 LOG(ERROR) << "Unknown content settings version in preferences."; |
| 370 return; | 407 return; |
| 371 } | 408 } |
| 372 | 409 |
| 373 // Read exceptions. | 410 // Read exceptions. |
| 374 ReadExceptions(false); | 411 ReadExceptions(false); |
| 375 | 412 |
| 376 if (!is_incognito()) { | 413 if (!is_incognito_) { |
| 377 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", | 414 UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions", |
| 378 host_content_settings()->size()); | 415 value_map_.size()); |
| 379 } | 416 } |
| 380 | 417 |
| 381 pref_change_registrar_.Init(prefs); | 418 pref_change_registrar_.Init(prefs); |
| 382 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); | 419 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); |
| 383 | 420 |
| 384 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 421 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 385 Source<Profile>(profile_)); | 422 Source<Profile>(profile_)); |
| 386 initializing_ = false; | 423 initializing_ = false; |
| 387 } | 424 } |
| 388 | 425 |
| 426 ContentSetting PrefProvider::GetContentSetting( |
| 427 const GURL& requesting_url, |
| 428 const GURL& embedding_url, |
| 429 ContentSettingsType content_type, |
| 430 const ResourceIdentifier& resource_identifier) const { |
| 431 // Support for item, top-level-frame URLs are not enabled yet. |
| 432 DCHECK(requesting_url == embedding_url); |
| 433 |
| 434 // For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito |
| 435 // profile, this will always return NULL. |
| 436 // TODO(markusheintz): I don't like this. I'd like to have an |
| 437 // IncognitoProviderWrapper that wrapps the pref provider for a host content |
| 438 // settings map of an incognito profile. |
| 439 Value* incognito_value = incognito_value_map_.GetValue( |
| 440 requesting_url, |
| 441 embedding_url, |
| 442 content_type, |
| 443 resource_identifier); |
| 444 if (incognito_value) |
| 445 return ValueToContentSetting(incognito_value); |
| 446 |
| 447 Value* value = value_map_.GetValue( |
| 448 requesting_url, |
| 449 embedding_url, |
| 450 content_type, |
| 451 resource_identifier); |
| 452 if (value) |
| 453 return ValueToContentSetting(value); |
| 454 |
| 455 return CONTENT_SETTING_DEFAULT; |
| 456 } |
| 457 |
| 458 void PrefProvider::GetAllContentSettingsRules( |
| 459 ContentSettingsType content_type, |
| 460 const ResourceIdentifier& resource_identifier, |
| 461 Rules* content_setting_rules) const { |
| 462 DCHECK_NE(RequiresResourceIdentifier(content_type), |
| 463 resource_identifier.empty()); |
| 464 DCHECK(content_setting_rules); |
| 465 content_setting_rules->clear(); |
| 466 |
| 467 const OriginIdentifierValueMap* map_to_return = |
| 468 is_incognito_ ? &incognito_value_map_ : &value_map_; |
| 469 |
| 470 base::AutoLock auto_lock(lock_); |
| 471 for (OriginIdentifierValueMap::const_iterator entry = map_to_return->begin(); |
| 472 entry != map_to_return->end(); |
| 473 ++entry) { |
| 474 if (entry->content_type == content_type && |
| 475 entry->identifier == resource_identifier) { |
| 476 ContentSetting setting = ValueToContentSetting(entry->value); |
| 477 DCHECK(setting != CONTENT_SETTING_DEFAULT); |
| 478 content_setting_rules->push_back( |
| 479 Rule(entry->item_pattern, entry->top_level_frame_pattern, setting)); |
| 480 } |
| 481 } |
| 482 } |
| 483 |
| 389 void PrefProvider::SetContentSetting( | 484 void PrefProvider::SetContentSetting( |
| 390 const ContentSettingsPattern& requesting_pattern, | 485 const ContentSettingsPattern& requesting_pattern, |
| 391 const ContentSettingsPattern& embedding_pattern, | 486 const ContentSettingsPattern& embedding_pattern, |
| 392 ContentSettingsType content_type, | 487 ContentSettingsType content_type, |
| 393 const ResourceIdentifier& resource_identifier, | 488 const ResourceIdentifier& resource_identifier, |
| 394 ContentSetting setting) { | 489 ContentSetting setting) { |
| 395 // Support for embedding_patterns is not implemented yet. | 490 // Support for embedding_patterns is not implemented yet. |
| 396 DCHECK(requesting_pattern == embedding_pattern); | 491 DCHECK(requesting_pattern == embedding_pattern); |
| 397 | 492 |
| 398 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 493 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 494 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 400 DCHECK_NE(RequiresResourceIdentifier(content_type), | 495 DCHECK_NE(RequiresResourceIdentifier(content_type), |
| 401 resource_identifier.empty()); | 496 resource_identifier.empty()); |
| 402 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 497 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || |
| 403 setting != CONTENT_SETTING_ASK || | 498 setting != CONTENT_SETTING_ASK || |
| 404 CommandLine::ForCurrentProcess()->HasSwitch( | 499 CommandLine::ForCurrentProcess()->HasSwitch( |
| 405 switches::kEnableClickToPlay)); | 500 switches::kEnableClickToPlay)); |
| 406 | 501 |
| 407 bool early_exit = false; | |
| 408 std::string pattern_str(requesting_pattern.ToString()); | |
| 409 DictionaryValue* all_settings_dictionary = NULL; | |
| 410 | |
| 411 updating_preferences_ = true; | 502 updating_preferences_ = true; |
| 412 { | 503 { |
| 413 // Begin scope of update. | |
| 414 // profile_ may be NULL in unit tests. | |
| 415 DictionaryPrefUpdate update(profile_ ? profile_->GetPrefs() : NULL, | 504 DictionaryPrefUpdate update(profile_ ? profile_->GetPrefs() : NULL, |
| 416 prefs::kContentSettingsPatterns); | 505 prefs::kContentSettingsPatterns); |
| 506 DictionaryValue* all_settings_dictionary = NULL; |
| 417 | 507 |
| 418 // Select content-settings-map to write to. | 508 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
| 419 HostContentSettings* map_to_modify = incognito_settings(); | 509 if (!is_incognito_) { |
| 420 if (!is_incognito()) { | 510 map_to_modify = &value_map_; |
| 421 all_settings_dictionary = update.Get(); | 511 all_settings_dictionary = update.Get(); |
| 422 | |
| 423 map_to_modify = host_content_settings(); | |
| 424 } | 512 } |
| 425 | 513 |
| 426 // Update content-settings-map. | 514 // Update in memory value map. |
| 427 { | 515 { |
| 428 base::AutoLock auto_lock(lock()); | 516 base::AutoLock auto_lock(lock_); |
| 429 if (!map_to_modify->count(pattern_str)) | 517 if (setting == CONTENT_SETTING_DEFAULT) { |
| 430 (*map_to_modify)[pattern_str].content_settings = ContentSettings(); | 518 map_to_modify->DeleteValue( |
| 431 HostContentSettings::iterator i(map_to_modify->find(pattern_str)); | 519 requesting_pattern, |
| 432 ContentSettings& settings = i->second.content_settings; | 520 embedding_pattern, |
| 433 if (RequiresResourceIdentifier(content_type)) { | 521 content_type, |
| 434 settings.settings[content_type] = CONTENT_SETTING_DEFAULT; | 522 resource_identifier); |
| 435 if (setting != CONTENT_SETTING_DEFAULT) { | |
| 436 i->second.content_settings_for_resources[ | |
| 437 ContentSettingsTypeResourceIdentifierPair(content_type, | |
| 438 resource_identifier)] = setting; | |
| 439 } else { | |
| 440 i->second.content_settings_for_resources.erase( | |
| 441 ContentSettingsTypeResourceIdentifierPair(content_type, | |
| 442 resource_identifier)); | |
| 443 } | |
| 444 } else { | 523 } else { |
| 445 settings.settings[content_type] = setting; | 524 map_to_modify->SetValue( |
| 446 } | 525 requesting_pattern, |
| 447 if (AllDefault(i->second)) { | 526 embedding_pattern, |
| 448 map_to_modify->erase(i); | 527 content_type, |
| 449 if (all_settings_dictionary) | 528 resource_identifier, |
| 450 all_settings_dictionary->RemoveWithoutPathExpansion( | 529 Value::CreateIntegerValue(setting)); |
| 451 pattern_str, NULL); | |
| 452 | |
| 453 // We can't just return because |NotifyObservers()| needs to be called, | |
| 454 // without lock() being held. | |
| 455 early_exit = true; | |
| 456 } | 530 } |
| 457 } | 531 } |
| 458 | 532 |
| 459 // Update the content_settings preference. | 533 // Update the content settings preference. |
| 460 if (!early_exit && all_settings_dictionary) { | 534 std::string pattern_str(CreatePatternString(requesting_pattern, |
| 461 DictionaryValue* host_settings_dictionary = NULL; | 535 embedding_pattern)); |
| 536 if (all_settings_dictionary) { |
| 537 // Get settings dictionary for the pattern string (key). |
| 538 DictionaryValue* settings_dictionary = NULL; |
| 462 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 539 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 463 pattern_str, &host_settings_dictionary); | 540 pattern_str, &settings_dictionary); |
| 464 if (!found) { | 541 |
| 465 host_settings_dictionary = new DictionaryValue; | 542 if (!found && (setting != CONTENT_SETTING_DEFAULT)) { |
| 543 settings_dictionary = new DictionaryValue; |
| 466 all_settings_dictionary->SetWithoutPathExpansion( | 544 all_settings_dictionary->SetWithoutPathExpansion( |
| 467 pattern_str, host_settings_dictionary); | 545 pattern_str, settings_dictionary); |
| 468 DCHECK_NE(setting, CONTENT_SETTING_DEFAULT); | |
| 469 } | 546 } |
| 470 if (RequiresResourceIdentifier(content_type)) { | 547 |
| 471 std::string dictionary_path(kResourceTypeNames[content_type]); | 548 if (settings_dictionary) { |
| 472 DictionaryValue* resource_dictionary = NULL; | 549 if (RequiresResourceIdentifier(content_type)) { |
| 473 found = host_settings_dictionary->GetDictionary( | 550 // Get resource dictionary. |
| 474 dictionary_path, &resource_dictionary); | 551 std::string res_dictionary_path(kResourceTypeNames[content_type]); |
| 475 if (!found) { | 552 DictionaryValue* resource_dictionary = NULL; |
| 476 resource_dictionary = new DictionaryValue; | 553 found = settings_dictionary->GetDictionary( |
| 477 host_settings_dictionary->Set(dictionary_path, resource_dictionary); | 554 res_dictionary_path, &resource_dictionary); |
| 555 if (!found) { |
| 556 if (setting == CONTENT_SETTING_DEFAULT) |
| 557 return; // Nothing to remove. Exit early. |
| 558 resource_dictionary = new DictionaryValue; |
| 559 settings_dictionary->Set(res_dictionary_path, resource_dictionary); |
| 560 } |
| 561 // Update resource dictionary. |
| 562 if (setting == CONTENT_SETTING_DEFAULT) { |
| 563 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, |
| 564 NULL); |
| 565 if (resource_dictionary->empty()) { |
| 566 settings_dictionary->RemoveWithoutPathExpansion( |
| 567 res_dictionary_path, NULL); |
| 568 } |
| 569 } else { |
| 570 resource_dictionary->SetWithoutPathExpansion( |
| 571 resource_identifier, Value::CreateIntegerValue(setting)); |
| 572 } |
| 573 } else { |
| 574 // Update settings dictionary. |
| 575 std::string setting_path(kTypeNames[content_type]); |
| 576 if (setting == CONTENT_SETTING_DEFAULT) { |
| 577 settings_dictionary->RemoveWithoutPathExpansion(setting_path, |
| 578 NULL); |
| 579 } else { |
| 580 settings_dictionary->SetWithoutPathExpansion( |
| 581 setting_path, Value::CreateIntegerValue(setting)); |
| 582 } |
| 478 } | 583 } |
| 479 if (setting == CONTENT_SETTING_DEFAULT) { | 584 // Remove the settings dictionary if it is empty. |
| 480 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, | 585 if (settings_dictionary->empty()) { |
| 481 NULL); | 586 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 482 } else { | 587 pattern_str, NULL); |
| 483 resource_dictionary->SetWithoutPathExpansion( | |
| 484 resource_identifier, Value::CreateIntegerValue(setting)); | |
| 485 } | |
| 486 } else { | |
| 487 std::string dictionary_path(kTypeNames[content_type]); | |
| 488 if (setting == CONTENT_SETTING_DEFAULT) { | |
| 489 host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | |
| 490 NULL); | |
| 491 } else { | |
| 492 host_settings_dictionary->SetWithoutPathExpansion( | |
| 493 dictionary_path, Value::CreateIntegerValue(setting)); | |
| 494 } | 588 } |
| 495 } | 589 } |
| 496 } | 590 } |
| 497 } // End scope of update. | 591 } |
| 498 updating_preferences_ = false; | 592 updating_preferences_ = false; |
| 499 | 593 |
| 500 NotifyObservers(ContentSettingsDetails(requesting_pattern, content_type, "")); | 594 NotifyObservers(ContentSettingsDetails(requesting_pattern, content_type, "")); |
| 501 } | 595 } |
| 502 | 596 |
| 503 void PrefProvider::ResetToDefaults() { | 597 void PrefProvider::ResetToDefaults() { |
| 504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 598 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 505 | 599 |
| 506 { | 600 { |
| 507 base::AutoLock auto_lock(lock()); | 601 base::AutoLock auto_lock(lock_); |
| 508 host_content_settings()->clear(); | 602 value_map_.Clear(); |
| 509 incognito_settings()->clear(); | 603 incognito_value_map_.Clear(); |
| 510 } | 604 } |
| 511 | 605 |
| 512 if (!is_incognito()) { | 606 if (!is_incognito_) { |
| 513 PrefService* prefs = profile_->GetPrefs(); | 607 PrefService* prefs = profile_->GetPrefs(); |
| 514 updating_preferences_ = true; | 608 updating_preferences_ = true; |
| 515 prefs->ClearPref(prefs::kContentSettingsPatterns); | 609 prefs->ClearPref(prefs::kContentSettingsPatterns); |
| 516 updating_preferences_ = false; | 610 updating_preferences_ = false; |
| 517 } | 611 } |
| 518 } | 612 } |
| 519 | 613 |
| 520 void PrefProvider::ClearAllContentSettingsRules( | 614 void PrefProvider::ClearAllContentSettingsRules( |
| 521 ContentSettingsType content_type) { | 615 ContentSettingsType content_type) { |
| 522 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 616 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 523 | 617 |
| 524 DictionaryValue* all_settings_dictionary = NULL; | |
| 525 HostContentSettings* map_to_modify = incognito_settings(); | |
| 526 | |
| 527 updating_preferences_ = true; | 618 updating_preferences_ = true; |
| 528 { // Begin scope of update. | 619 { // Begin scope of update. |
| 529 DictionaryPrefUpdate update(profile_->GetPrefs(), | 620 DictionaryPrefUpdate update(profile_->GetPrefs(), |
| 530 prefs::kContentSettingsPatterns); | 621 prefs::kContentSettingsPatterns); |
| 531 | 622 |
| 532 if (!is_incognito()) { | 623 DictionaryValue* all_settings_dictionary = NULL; |
| 624 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
| 625 if (!is_incognito_) { |
| 533 all_settings_dictionary = update.Get(); | 626 all_settings_dictionary = update.Get(); |
| 534 map_to_modify = host_content_settings(); | 627 map_to_modify = &value_map_; |
| 535 } | 628 } |
| 536 | 629 |
| 537 { | 630 { |
| 538 base::AutoLock auto_lock(lock()); | 631 base::AutoLock auto_lock(lock_); |
| 539 for (HostContentSettings::iterator i(map_to_modify->begin()); | 632 |
| 540 i != map_to_modify->end(); ) { | 633 OriginIdentifierValueMap::iterator entry(map_to_modify->begin()); |
| 541 if (RequiresResourceIdentifier(content_type) || | 634 while (entry != map_to_modify->end()) { |
| 542 i->second.content_settings.settings[content_type] != | 635 if (entry->content_type == content_type) { |
| 543 CONTENT_SETTING_DEFAULT) { | 636 std::string pattern_str = CreatePatternString( |
| 544 if (RequiresResourceIdentifier(content_type)) | 637 entry->item_pattern, entry->top_level_frame_pattern); |
| 545 i->second.content_settings_for_resources.clear(); | 638 // Delete current |entry| and set |entry| to the next value map entry. |
| 546 i->second.content_settings.settings[content_type] = | 639 entry = map_to_modify->DeleteValue(entry); |
| 547 CONTENT_SETTING_DEFAULT; | 640 |
| 548 std::string host(i->first); | 641 // Update the content settings preference. |
| 549 if (AllDefault(i->second)) { | 642 if (all_settings_dictionary) { |
| 550 if (all_settings_dictionary) | 643 // Update the settings dictionary. |
| 551 all_settings_dictionary->RemoveWithoutPathExpansion(host, NULL); | 644 DictionaryValue* settings_dictionary; |
| 552 map_to_modify->erase(i++); | |
| 553 } else if (all_settings_dictionary) { | |
| 554 DictionaryValue* host_settings_dictionary; | |
| 555 bool found = | 645 bool found = |
| 556 all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 646 all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 557 host, &host_settings_dictionary); | 647 pattern_str, &settings_dictionary); |
| 558 DCHECK(found); | 648 DCHECK(found); |
| 559 host_settings_dictionary->RemoveWithoutPathExpansion( | 649 settings_dictionary->RemoveWithoutPathExpansion( |
| 560 kTypeNames[content_type], NULL); | 650 kTypeNames[content_type], NULL); |
| 561 ++i; | 651 // Remove empty dictionaries. |
| 652 if (settings_dictionary->empty()) |
| 653 all_settings_dictionary->RemoveWithoutPathExpansion(pattern_str, |
| 654 NULL); |
| 562 } | 655 } |
| 563 } else { | 656 } else { |
| 564 ++i; | 657 // No action is required, so move to the next value map entry. |
| 658 ++entry; |
| 565 } | 659 } |
| 566 } | 660 } |
| 567 } | 661 } |
| 568 } // End scope of update. | 662 } // End scope of update. |
| 569 updating_preferences_ = false; | 663 updating_preferences_ = false; |
| 570 | 664 |
| 571 NotifyObservers( | 665 NotifyObservers( |
| 572 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); | 666 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); |
| 573 } | 667 } |
| 574 | 668 |
| 575 void PrefProvider::Observe( | 669 void PrefProvider::Observe( |
| 576 NotificationType type, | 670 NotificationType type, |
| 577 const NotificationSource& source, | 671 const NotificationSource& source, |
| 578 const NotificationDetails& details) { | 672 const NotificationDetails& details) { |
| 579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 580 | 674 |
| 581 if (type == NotificationType::PREF_CHANGED) { | 675 if (type == NotificationType::PREF_CHANGED) { |
| 582 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 676 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |
| 583 if (updating_preferences_) | 677 if (updating_preferences_) |
| 584 return; | 678 return; |
| 585 | 679 |
| 586 std::string* name = Details<std::string>(details).ptr(); | 680 std::string* name = Details<std::string>(details).ptr(); |
| 587 if (*name == prefs::kContentSettingsPatterns) { | 681 if (*name == prefs::kContentSettingsPatterns) { |
| 588 ReadExceptions(true); | 682 ReadExceptions(true); |
| 589 } else { | 683 } else { |
| 590 NOTREACHED() << "Unexpected preference observed"; | 684 NOTREACHED() << "Unexpected preference observed"; |
| 591 return; | 685 return; |
| 592 } | 686 } |
| 593 | 687 |
| 594 if (!is_incognito()) { | 688 if (!is_incognito_) { |
| 595 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 689 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 596 CONTENT_SETTINGS_TYPE_DEFAULT, | 690 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 597 "")); | 691 "")); |
| 598 } | 692 } |
| 599 } else if (type == NotificationType::PROFILE_DESTROYED) { | 693 } else if (type == NotificationType::PROFILE_DESTROYED) { |
| 600 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | 694 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
| 601 UnregisterObservers(); | 695 UnregisterObservers(); |
| 602 } else { | 696 } else { |
| 603 NOTREACHED() << "Unexpected notification"; | 697 NOTREACHED() << "Unexpected notification"; |
| 604 } | 698 } |
| 605 } | 699 } |
| 606 | 700 |
| 607 PrefProvider::~PrefProvider() { | 701 PrefProvider::~PrefProvider() { |
| 608 UnregisterObservers(); | 702 UnregisterObservers(); |
| 609 } | 703 } |
| 610 | 704 |
| 611 // //////////////////////////////////////////////////////////////////////////// | 705 // //////////////////////////////////////////////////////////////////////////// |
| 612 // Private | 706 // Private |
| 613 | 707 |
| 614 void PrefProvider::ReadExceptions(bool overwrite) { | 708 void PrefProvider::ReadExceptions(bool overwrite) { |
| 615 base::AutoLock auto_lock(lock()); | 709 base::AutoLock auto_lock(lock_); |
| 616 | 710 |
| 617 PrefService* prefs = profile_->GetPrefs(); | 711 PrefService* prefs = profile_->GetPrefs(); |
| 618 const DictionaryValue* all_settings_dictionary = | 712 const DictionaryValue* all_settings_dictionary = |
| 619 prefs->GetDictionary(prefs::kContentSettingsPatterns); | 713 prefs->GetDictionary(prefs::kContentSettingsPatterns); |
| 620 | 714 |
| 621 if (overwrite) | 715 if (overwrite) |
| 622 host_content_settings()->clear(); | 716 value_map_.Clear(); |
| 623 | 717 |
| 624 updating_preferences_ = true; | 718 updating_preferences_ = true; |
| 625 | |
| 626 // Careful: The returned value could be NULL if the pref has never been set. | 719 // Careful: The returned value could be NULL if the pref has never been set. |
| 627 if (all_settings_dictionary != NULL) { | 720 if (all_settings_dictionary != NULL) { |
| 628 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | 721 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns); |
| 629 DictionaryValue* mutable_settings; | 722 DictionaryValue* mutable_settings; |
| 630 scoped_ptr<DictionaryValue> mutable_settings_scope; | 723 scoped_ptr<DictionaryValue> mutable_settings_scope; |
| 631 | 724 |
| 632 if (!is_incognito()) { | 725 if (!is_incognito_) { |
| 633 mutable_settings = update.Get(); | 726 mutable_settings = update.Get(); |
| 634 } else { | 727 } else { |
| 635 // Create copy as we do not want to persist anything in OTR prefs. | 728 // Create copy as we do not want to persist anything in OTR prefs. |
| 636 mutable_settings = all_settings_dictionary->DeepCopy(); | 729 mutable_settings = all_settings_dictionary->DeepCopy(); |
| 637 mutable_settings_scope.reset(mutable_settings); | 730 mutable_settings_scope.reset(mutable_settings); |
| 638 } | 731 } |
| 639 | |
| 640 // Convert all Unicode patterns into punycode form, then read. | 732 // Convert all Unicode patterns into punycode form, then read. |
| 641 CanonicalizeContentSettingsExceptions(mutable_settings); | 733 CanonicalizeContentSettingsExceptions(mutable_settings); |
| 642 | 734 |
| 643 for (DictionaryValue::key_iterator i(mutable_settings->begin_keys()); | 735 for (DictionaryValue::key_iterator i(mutable_settings->begin_keys()); |
| 644 i != mutable_settings->end_keys(); ++i) { | 736 i != mutable_settings->end_keys(); ++i) { |
| 645 const std::string& pattern(*i); | 737 const std::string& pattern_str(*i); |
| 646 if (!ContentSettingsPattern::FromString(pattern).IsValid()) | 738 ContentSettingsPattern pattern = |
| 739 ContentSettingsPattern::FromString(pattern_str); |
| 740 if (!pattern.IsValid()) |
| 647 LOG(WARNING) << "Invalid pattern stored in content settings"; | 741 LOG(WARNING) << "Invalid pattern stored in content settings"; |
| 648 DictionaryValue* pattern_settings_dictionary = NULL; | 742 |
| 743 // Get settings dictionary for the current pattern string, and read |
| 744 // settings from the dictionary. |
| 745 DictionaryValue* settings_dictionary = NULL; |
| 649 bool found = mutable_settings->GetDictionaryWithoutPathExpansion( | 746 bool found = mutable_settings->GetDictionaryWithoutPathExpansion( |
| 650 pattern, &pattern_settings_dictionary); | 747 pattern_str, &settings_dictionary); |
| 651 DCHECK(found); | 748 DCHECK(found); |
| 652 | 749 |
| 653 ExtendedContentSettings extended_settings; | 750 for (DictionaryValue::key_iterator i(settings_dictionary->begin_keys()); |
| 654 GetSettingsFromDictionary(pattern_settings_dictionary, | 751 i != settings_dictionary->end_keys(); |
| 655 &extended_settings.content_settings); | 752 ++i) { |
| 656 GetResourceSettingsFromDictionary( | 753 const std::string& content_type_str(*i); |
| 657 pattern_settings_dictionary, | 754 ContentSettingsType content_type = StringToContentSettingsType( |
| 658 &extended_settings.content_settings_for_resources); | 755 content_type_str); |
| 756 if (content_type == CONTENT_SETTINGS_TYPE_DEFAULT) { |
| 757 NOTREACHED(); |
| 758 LOG(WARNING) << "Skip settings for invalid content settings type '" |
| 759 << content_type_str << "'"; |
| 760 continue; |
| 761 } |
| 659 | 762 |
| 660 (*host_content_settings())[pattern] = extended_settings; | 763 if (RequiresResourceIdentifier(content_type)) { |
| 764 DictionaryValue* resource_dictionary = NULL; |
| 765 bool found = settings_dictionary->GetDictionary( |
| 766 content_type_str, &resource_dictionary); |
| 767 DCHECK(found); |
| 768 for (DictionaryValue::key_iterator j( |
| 769 resource_dictionary->begin_keys()); |
| 770 j != resource_dictionary->end_keys(); |
| 771 ++j) { |
| 772 const std::string& resource_identifier(*j); |
| 773 int setting = CONTENT_SETTING_DEFAULT; |
| 774 found = resource_dictionary->GetIntegerWithoutPathExpansion( |
| 775 resource_identifier, &setting); |
| 776 DCHECK(found); |
| 777 setting = ClickToPlayFixup(content_type, |
| 778 ContentSetting(setting)); |
| 779 value_map_.SetValue(pattern, |
| 780 pattern, |
| 781 content_type, |
| 782 resource_identifier, |
| 783 Value::CreateIntegerValue(setting)); |
| 784 } |
| 785 } else { |
| 786 int setting = CONTENT_SETTING_DEFAULT; |
| 787 bool found = settings_dictionary->GetIntegerWithoutPathExpansion( |
| 788 content_type_str, &setting); |
| 789 DCHECK(found); |
| 790 setting = FixObsoleteCookiePromptMode(content_type, |
| 791 ContentSetting(setting)); |
| 792 setting = ClickToPlayFixup(content_type, |
| 793 ContentSetting(setting)); |
| 794 value_map_.SetValue(pattern, |
| 795 pattern, |
| 796 content_type, |
| 797 ResourceIdentifier(""), |
| 798 Value::CreateIntegerValue(setting)); |
| 799 } |
| 800 } |
| 661 } | 801 } |
| 662 } | 802 } |
| 663 updating_preferences_ = false; | 803 updating_preferences_ = false; |
| 664 } | 804 } |
| 665 | 805 |
| 666 void PrefProvider::CanonicalizeContentSettingsExceptions( | 806 void PrefProvider::CanonicalizeContentSettingsExceptions( |
| 667 DictionaryValue* all_settings_dictionary) { | 807 DictionaryValue* all_settings_dictionary) { |
| 668 DCHECK(all_settings_dictionary); | 808 DCHECK(all_settings_dictionary); |
| 669 | 809 |
| 670 std::vector<std::string> remove_items; | 810 std::vector<std::string> remove_items; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 700 | 840 |
| 701 for (size_t i = 0; i < move_items.size(); ++i) { | 841 for (size_t i = 0; i < move_items.size(); ++i) { |
| 702 Value* pattern_settings_dictionary = NULL; | 842 Value* pattern_settings_dictionary = NULL; |
| 703 all_settings_dictionary->RemoveWithoutPathExpansion( | 843 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 704 move_items[i].first, &pattern_settings_dictionary); | 844 move_items[i].first, &pattern_settings_dictionary); |
| 705 all_settings_dictionary->SetWithoutPathExpansion( | 845 all_settings_dictionary->SetWithoutPathExpansion( |
| 706 move_items[i].second, pattern_settings_dictionary); | 846 move_items[i].second, pattern_settings_dictionary); |
| 707 } | 847 } |
| 708 } | 848 } |
| 709 | 849 |
| 710 void PrefProvider::GetSettingsFromDictionary( | |
| 711 const DictionaryValue* dictionary, | |
| 712 ContentSettings* settings) { | |
| 713 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | |
| 714 i != dictionary->end_keys(); ++i) { | |
| 715 const std::string& content_type(*i); | |
| 716 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { | |
| 717 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type)) { | |
| 718 int setting = CONTENT_SETTING_DEFAULT; | |
| 719 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | |
| 720 &setting); | |
| 721 DCHECK(found); | |
| 722 settings->settings[type] = IntToContentSetting(setting); | |
| 723 break; | |
| 724 } | |
| 725 } | |
| 726 } | |
| 727 // Migrate obsolete cookie prompt mode. | |
| 728 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | |
| 729 CONTENT_SETTING_ASK) | |
| 730 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | |
| 731 | |
| 732 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | |
| 733 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 734 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | |
| 735 } | |
| 736 | |
| 737 void PrefProvider::GetResourceSettingsFromDictionary( | |
| 738 const DictionaryValue* dictionary, | |
| 739 ResourceContentSettings* settings) { | |
| 740 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | |
| 741 i != dictionary->end_keys(); ++i) { | |
| 742 const std::string& content_type(*i); | |
| 743 for (size_t type = 0; type < arraysize(kResourceTypeNames); ++type) { | |
| 744 if ((kResourceTypeNames[type] != NULL) && | |
| 745 (kResourceTypeNames[type] == content_type)) { | |
| 746 DictionaryValue* resource_dictionary = NULL; | |
| 747 bool found = dictionary->GetDictionary(content_type, | |
| 748 &resource_dictionary); | |
| 749 DCHECK(found); | |
| 750 for (DictionaryValue::key_iterator j(resource_dictionary->begin_keys()); | |
| 751 j != resource_dictionary->end_keys(); ++j) { | |
| 752 const std::string& resource_identifier(*j); | |
| 753 int setting = CONTENT_SETTING_DEFAULT; | |
| 754 bool found = resource_dictionary->GetIntegerWithoutPathExpansion( | |
| 755 resource_identifier, &setting); | |
| 756 DCHECK(found); | |
| 757 (*settings)[ContentSettingsTypeResourceIdentifierPair( | |
| 758 ContentSettingsType(type), resource_identifier)] = | |
| 759 ClickToPlayFixup(ContentSettingsType(type), | |
| 760 ContentSetting(setting)); | |
| 761 } | |
| 762 | |
| 763 break; | |
| 764 } | |
| 765 } | |
| 766 } | |
| 767 } | |
| 768 | |
| 769 void PrefProvider::NotifyObservers( | 850 void PrefProvider::NotifyObservers( |
| 770 const ContentSettingsDetails& details) { | 851 const ContentSettingsDetails& details) { |
| 771 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 852 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 772 if (initializing_ || profile_ == NULL) | 853 if (initializing_ || profile_ == NULL) |
| 773 return; | 854 return; |
| 774 NotificationService::current()->Notify( | 855 NotificationService::current()->Notify( |
| 775 NotificationType::CONTENT_SETTINGS_CHANGED, | 856 NotificationType::CONTENT_SETTINGS_CHANGED, |
| 776 Source<HostContentSettingsMap>( | 857 Source<HostContentSettingsMap>( |
| 777 profile_->GetHostContentSettingsMap()), | 858 profile_->GetHostContentSettingsMap()), |
| 778 Details<const ContentSettingsDetails>(&details)); | 859 Details<const ContentSettingsDetails>(&details)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 796 for (DictionaryValue::key_iterator | 877 for (DictionaryValue::key_iterator |
| 797 i(all_settings_dictionary->begin_keys()); | 878 i(all_settings_dictionary->begin_keys()); |
| 798 i != all_settings_dictionary->end_keys(); ++i) { | 879 i != all_settings_dictionary->end_keys(); ++i) { |
| 799 const std::string& host(*i); | 880 const std::string& host(*i); |
| 800 ContentSettingsPattern pattern = ContentSettingsPattern::FromString( | 881 ContentSettingsPattern pattern = ContentSettingsPattern::FromString( |
| 801 std::string(ContentSettingsPattern::kDomainWildcard) + host); | 882 std::string(ContentSettingsPattern::kDomainWildcard) + host); |
| 802 DictionaryValue* host_settings_dictionary = NULL; | 883 DictionaryValue* host_settings_dictionary = NULL; |
| 803 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 884 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 804 host, &host_settings_dictionary); | 885 host, &host_settings_dictionary); |
| 805 DCHECK(found); | 886 DCHECK(found); |
| 806 ContentSettings settings; | 887 |
| 807 GetSettingsFromDictionary(host_settings_dictionary, &settings); | 888 for (DictionaryValue::key_iterator i( |
| 808 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 889 host_settings_dictionary->begin_keys()); |
| 809 if (settings.settings[j] != CONTENT_SETTING_DEFAULT && | 890 i != host_settings_dictionary->end_keys(); |
| 810 !RequiresResourceIdentifier(ContentSettingsType(j))) { | 891 ++i) { |
| 811 SetContentSetting( | 892 const std::string& content_type_str(*i); |
| 812 pattern, | 893 ContentSettingsType content_type = |
| 813 pattern, | 894 StringToContentSettingsType(content_type_str); |
| 814 ContentSettingsType(j), | 895 |
| 815 "", | 896 if (content_type == CONTENT_SETTINGS_TYPE_DEFAULT) { |
| 816 settings.settings[j]); | 897 NOTREACHED(); |
| 898 LOG(DFATAL) << "Skip settings for invalid content settings type '" |
| 899 << content_type_str << "'"; |
| 900 continue; |
| 901 } |
| 902 |
| 903 if (!RequiresResourceIdentifier(content_type)) { |
| 904 int setting_int_value = CONTENT_SETTING_DEFAULT; |
| 905 bool found = |
| 906 host_settings_dictionary->GetIntegerWithoutPathExpansion( |
| 907 content_type_str, &setting_int_value); |
| 908 DCHECK(found); |
| 909 ContentSetting setting = IntToContentSetting(setting_int_value); |
| 910 |
| 911 setting = FixObsoleteCookiePromptMode(content_type, setting); |
| 912 setting = ClickToPlayFixup(content_type, setting); |
| 913 |
| 914 // TODO(markusheintz): Maybe this check can be removed. |
| 915 if (setting != CONTENT_SETTING_DEFAULT) { |
| 916 SetContentSetting( |
| 917 pattern, |
| 918 pattern, |
| 919 content_type, |
| 920 "", |
| 921 setting); |
| 922 } |
| 817 } | 923 } |
| 818 } | 924 } |
| 819 } | 925 } |
| 820 prefs->ClearPref(prefs::kPerHostContentSettings); | 926 prefs->ClearPref(prefs::kPerHostContentSettings); |
| 821 } | 927 } |
| 822 } | 928 } |
| 823 | 929 |
| 824 void PrefProvider::MigrateObsoletePopupsPref(PrefService* prefs) { | 930 void PrefProvider::MigrateObsoletePopupsPref(PrefService* prefs) { |
| 825 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { | 931 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { |
| 826 const ListValue* whitelist_pref = | 932 const ListValue* whitelist_pref = |
| 827 prefs->GetList(prefs::kPopupWhitelistedHosts); | 933 prefs->GetList(prefs::kPopupWhitelistedHosts); |
| 828 for (ListValue::const_iterator i(whitelist_pref->begin()); | 934 for (ListValue::const_iterator i(whitelist_pref->begin()); |
| 829 i != whitelist_pref->end(); ++i) { | 935 i != whitelist_pref->end(); ++i) { |
| 830 std::string host; | 936 std::string host; |
| 831 (*i)->GetAsString(&host); | 937 (*i)->GetAsString(&host); |
| 832 SetContentSetting(ContentSettingsPattern::FromString(host), | 938 SetContentSetting(ContentSettingsPattern::FromString(host), |
| 833 ContentSettingsPattern::FromString(host), | 939 ContentSettingsPattern::FromString(host), |
| 834 CONTENT_SETTINGS_TYPE_POPUPS, | 940 CONTENT_SETTINGS_TYPE_POPUPS, |
| 835 "", | 941 "", |
| 836 CONTENT_SETTING_ALLOW); | 942 CONTENT_SETTING_ALLOW); |
| 837 } | 943 } |
| 838 prefs->ClearPref(prefs::kPopupWhitelistedHosts); | 944 prefs->ClearPref(prefs::kPopupWhitelistedHosts); |
| 839 } | 945 } |
| 840 } | 946 } |
| 841 | 947 |
| 842 } // namespace content_settings | 948 } // namespace content_settings |
| OLD | NEW |