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