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

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

Issue 7049007: Origin Identifier Value Map. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 6 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 <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
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
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.get());
477 DCHECK(setting != CONTENT_SETTING_DEFAULT);
478 Rule new_rule(entry->item_pattern,
479 entry->top_level_frame_pattern,
480 setting);
481 content_setting_rules->push_back(new_rule);
482 }
483 }
484 }
485
389 void PrefProvider::SetContentSetting( 486 void PrefProvider::SetContentSetting(
390 const ContentSettingsPattern& requesting_pattern, 487 const ContentSettingsPattern& requesting_pattern,
391 const ContentSettingsPattern& embedding_pattern, 488 const ContentSettingsPattern& embedding_pattern,
392 ContentSettingsType content_type, 489 ContentSettingsType content_type,
393 const ResourceIdentifier& resource_identifier, 490 const ResourceIdentifier& resource_identifier,
394 ContentSetting setting) { 491 ContentSetting setting) {
395 // Support for embedding_patterns is not implemented yet. 492 // Support for embedding_patterns is not implemented yet.
396 DCHECK(requesting_pattern == embedding_pattern); 493 DCHECK(requesting_pattern == embedding_pattern);
397 494
398 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 495 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
400 DCHECK_NE(RequiresResourceIdentifier(content_type), 497 DCHECK_NE(RequiresResourceIdentifier(content_type),
401 resource_identifier.empty()); 498 resource_identifier.empty());
402 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || 499 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS ||
403 setting != CONTENT_SETTING_ASK || 500 setting != CONTENT_SETTING_ASK ||
404 CommandLine::ForCurrentProcess()->HasSwitch( 501 CommandLine::ForCurrentProcess()->HasSwitch(
405 switches::kEnableClickToPlay)); 502 switches::kEnableClickToPlay));
406 503
407 bool early_exit = false;
408 std::string pattern_str(requesting_pattern.ToString());
409 DictionaryValue* all_settings_dictionary = NULL;
410
411 updating_preferences_ = true; 504 updating_preferences_ = true;
412 { 505 {
413 // Begin scope of update.
414 // profile_ may be NULL in unit tests.
415 DictionaryPrefUpdate update(profile_ ? profile_->GetPrefs() : NULL, 506 DictionaryPrefUpdate update(profile_ ? profile_->GetPrefs() : NULL,
416 prefs::kContentSettingsPatterns); 507 prefs::kContentSettingsPatterns);
508 DictionaryValue* all_settings_dictionary = NULL;
417 509
418 // Select content-settings-map to write to. 510 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
419 HostContentSettings* map_to_modify = incognito_settings(); 511 if (!is_incognito_) {
420 if (!is_incognito()) { 512 map_to_modify = &value_map_;
421 all_settings_dictionary = update.Get(); 513 all_settings_dictionary = update.Get();
422
423 map_to_modify = host_content_settings();
424 } 514 }
425 515
426 // Update content-settings-map. 516 // Update in memory value map.
427 { 517 {
428 base::AutoLock auto_lock(lock()); 518 base::AutoLock auto_lock(lock_);
429 if (!map_to_modify->count(pattern_str)) 519 if (setting == CONTENT_SETTING_DEFAULT) {
430 (*map_to_modify)[pattern_str].content_settings = ContentSettings(); 520 map_to_modify->DeleteValue(
431 HostContentSettings::iterator i(map_to_modify->find(pattern_str)); 521 requesting_pattern,
432 ContentSettings& settings = i->second.content_settings; 522 embedding_pattern,
433 if (RequiresResourceIdentifier(content_type)) { 523 content_type,
434 settings.settings[content_type] = CONTENT_SETTING_DEFAULT; 524 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 { 525 } else {
445 settings.settings[content_type] = setting; 526 map_to_modify->SetValue(
446 } 527 requesting_pattern,
447 if (AllDefault(i->second)) { 528 embedding_pattern,
448 map_to_modify->erase(i); 529 content_type,
449 if (all_settings_dictionary) 530 resource_identifier,
450 all_settings_dictionary->RemoveWithoutPathExpansion( 531 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 } 532 }
457 } 533 }
458 534
459 // Update the content_settings preference. 535 // Update the content settings preference.
460 if (!early_exit && all_settings_dictionary) { 536 std::string pattern_str(CreatePatternString(requesting_pattern,
461 DictionaryValue* host_settings_dictionary = NULL; 537 embedding_pattern));
538 if (all_settings_dictionary) {
539 // Get settings dictionary for the pattern string (key).
540 DictionaryValue* settings_dictionary = NULL;
462 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 541 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
463 pattern_str, &host_settings_dictionary); 542 pattern_str, &settings_dictionary);
464 if (!found) { 543
465 host_settings_dictionary = new DictionaryValue; 544 if (!found && (setting != CONTENT_SETTING_DEFAULT)) {
545 settings_dictionary = new DictionaryValue;
466 all_settings_dictionary->SetWithoutPathExpansion( 546 all_settings_dictionary->SetWithoutPathExpansion(
467 pattern_str, host_settings_dictionary); 547 pattern_str, settings_dictionary);
468 DCHECK_NE(setting, CONTENT_SETTING_DEFAULT);
469 } 548 }
470 if (RequiresResourceIdentifier(content_type)) { 549
471 std::string dictionary_path(kResourceTypeNames[content_type]); 550 if (settings_dictionary) {
472 DictionaryValue* resource_dictionary = NULL; 551 if (RequiresResourceIdentifier(content_type)) {
473 found = host_settings_dictionary->GetDictionary( 552 // Get resource dictionary.
474 dictionary_path, &resource_dictionary); 553 std::string res_dictionary_path(kResourceTypeNames[content_type]);
475 if (!found) { 554 DictionaryValue* resource_dictionary = NULL;
476 resource_dictionary = new DictionaryValue; 555 found = settings_dictionary->GetDictionary(
477 host_settings_dictionary->Set(dictionary_path, resource_dictionary); 556 res_dictionary_path, &resource_dictionary);
557 if (!found) {
558 if (setting == CONTENT_SETTING_DEFAULT)
559 return; // Nothing to remove. Exit early.
560 resource_dictionary = new DictionaryValue;
561 settings_dictionary->Set(res_dictionary_path, resource_dictionary);
562 }
563 // Update resource dictionary.
564 if (setting == CONTENT_SETTING_DEFAULT) {
565 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier,
566 NULL);
567 if (resource_dictionary->empty()) {
568 settings_dictionary->RemoveWithoutPathExpansion(
569 res_dictionary_path, NULL);
570 }
571 } else {
572 resource_dictionary->SetWithoutPathExpansion(
573 resource_identifier, Value::CreateIntegerValue(setting));
574 }
575 } else {
576 // Update settings dictionary.
577 std::string setting_path(kTypeNames[content_type]);
578 if (setting == CONTENT_SETTING_DEFAULT) {
579 settings_dictionary->RemoveWithoutPathExpansion(setting_path,
580 NULL);
581 } else {
582 settings_dictionary->SetWithoutPathExpansion(
583 setting_path, Value::CreateIntegerValue(setting));
584 }
478 } 585 }
479 if (setting == CONTENT_SETTING_DEFAULT) { 586 // Remove the settings dictionary if it is empty.
480 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, 587 if (settings_dictionary->empty()) {
481 NULL); 588 all_settings_dictionary->RemoveWithoutPathExpansion(
482 } else { 589 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 } 590 }
495 } 591 }
496 } 592 }
497 } // End scope of update. 593 }
498 updating_preferences_ = false; 594 updating_preferences_ = false;
499 595
500 NotifyObservers(ContentSettingsDetails(requesting_pattern, content_type, "")); 596 NotifyObservers(ContentSettingsDetails(requesting_pattern, content_type, ""));
501 } 597 }
502 598
503 void PrefProvider::ResetToDefaults() { 599 void PrefProvider::ResetToDefaults() {
504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 600 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
505 601
506 { 602 {
507 base::AutoLock auto_lock(lock()); 603 base::AutoLock auto_lock(lock_);
508 host_content_settings()->clear(); 604 value_map_.Clear();
509 incognito_settings()->clear(); 605 incognito_value_map_.Clear();
510 } 606 }
511 607
512 if (!is_incognito()) { 608 if (!is_incognito_) {
513 PrefService* prefs = profile_->GetPrefs(); 609 PrefService* prefs = profile_->GetPrefs();
514 updating_preferences_ = true; 610 updating_preferences_ = true;
515 prefs->ClearPref(prefs::kContentSettingsPatterns); 611 prefs->ClearPref(prefs::kContentSettingsPatterns);
516 updating_preferences_ = false; 612 updating_preferences_ = false;
517 } 613 }
518 } 614 }
519 615
520 void PrefProvider::ClearAllContentSettingsRules( 616 void PrefProvider::ClearAllContentSettingsRules(
521 ContentSettingsType content_type) { 617 ContentSettingsType content_type) {
522 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 618 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
523 619
524 DictionaryValue* all_settings_dictionary = NULL;
525 HostContentSettings* map_to_modify = incognito_settings();
526
527 updating_preferences_ = true; 620 updating_preferences_ = true;
528 { // Begin scope of update. 621 { // Begin scope of update.
529 DictionaryPrefUpdate update(profile_->GetPrefs(), 622 DictionaryPrefUpdate update(profile_->GetPrefs(),
530 prefs::kContentSettingsPatterns); 623 prefs::kContentSettingsPatterns);
531 624
532 if (!is_incognito()) { 625 DictionaryValue* all_settings_dictionary = NULL;
626 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
627 if (!is_incognito_) {
533 all_settings_dictionary = update.Get(); 628 all_settings_dictionary = update.Get();
534 map_to_modify = host_content_settings(); 629 map_to_modify = &value_map_;
535 } 630 }
536 631
537 { 632 {
538 base::AutoLock auto_lock(lock()); 633 base::AutoLock auto_lock(lock_);
539 for (HostContentSettings::iterator i(map_to_modify->begin()); 634
540 i != map_to_modify->end(); ) { 635 OriginIdentifierValueMap::iterator entry(map_to_modify->begin());
541 if (RequiresResourceIdentifier(content_type) || 636 while (entry != map_to_modify->end()) {
542 i->second.content_settings.settings[content_type] != 637 if (entry->content_type == content_type) {
543 CONTENT_SETTING_DEFAULT) { 638 std::string pattern_str = CreatePatternString(
544 if (RequiresResourceIdentifier(content_type)) 639 entry->item_pattern, entry->top_level_frame_pattern);
545 i->second.content_settings_for_resources.clear(); 640 // Delete current |entry| and set |entry| to the next value map entry.
546 i->second.content_settings.settings[content_type] = 641 entry = map_to_modify->DeleteValue(entry);
547 CONTENT_SETTING_DEFAULT; 642
548 std::string host(i->first); 643 // Update the content settings preference.
549 if (AllDefault(i->second)) { 644 if (all_settings_dictionary) {
550 if (all_settings_dictionary) 645 // Update the settings dictionary.
551 all_settings_dictionary->RemoveWithoutPathExpansion(host, NULL); 646 DictionaryValue* settings_dictionary;
552 map_to_modify->erase(i++);
553 } else if (all_settings_dictionary) {
554 DictionaryValue* host_settings_dictionary;
555 bool found = 647 bool found =
556 all_settings_dictionary->GetDictionaryWithoutPathExpansion( 648 all_settings_dictionary->GetDictionaryWithoutPathExpansion(
557 host, &host_settings_dictionary); 649 pattern_str, &settings_dictionary);
558 DCHECK(found); 650 DCHECK(found);
559 host_settings_dictionary->RemoveWithoutPathExpansion( 651 settings_dictionary->RemoveWithoutPathExpansion(
560 kTypeNames[content_type], NULL); 652 kTypeNames[content_type], NULL);
561 ++i; 653 // Remove empty dictionaries.
654 if (settings_dictionary->empty())
655 all_settings_dictionary->RemoveWithoutPathExpansion(pattern_str,
656 NULL);
562 } 657 }
563 } else { 658 } else {
564 ++i; 659 // No action is required, so move to the next value map entry.
660 ++entry;
565 } 661 }
566 } 662 }
567 } 663 }
568 } // End scope of update. 664 } // End scope of update.
569 updating_preferences_ = false; 665 updating_preferences_ = false;
570 666
571 NotifyObservers( 667 NotifyObservers(
572 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); 668 ContentSettingsDetails(ContentSettingsPattern(), content_type, ""));
573 } 669 }
574 670
575 void PrefProvider::Observe( 671 void PrefProvider::Observe(
576 NotificationType type, 672 NotificationType type,
577 const NotificationSource& source, 673 const NotificationSource& source,
578 const NotificationDetails& details) { 674 const NotificationDetails& details) {
579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
580 676
581 if (type == NotificationType::PREF_CHANGED) { 677 if (type == NotificationType::PREF_CHANGED) {
582 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); 678 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr());
583 if (updating_preferences_) 679 if (updating_preferences_)
584 return; 680 return;
585 681
586 std::string* name = Details<std::string>(details).ptr(); 682 std::string* name = Details<std::string>(details).ptr();
587 if (*name == prefs::kContentSettingsPatterns) { 683 if (*name == prefs::kContentSettingsPatterns) {
588 ReadExceptions(true); 684 ReadExceptions(true);
589 } else { 685 } else {
590 NOTREACHED() << "Unexpected preference observed"; 686 NOTREACHED() << "Unexpected preference observed";
591 return; 687 return;
592 } 688 }
593 689
594 if (!is_incognito()) { 690 if (!is_incognito_) {
595 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), 691 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(),
596 CONTENT_SETTINGS_TYPE_DEFAULT, 692 CONTENT_SETTINGS_TYPE_DEFAULT,
597 "")); 693 ""));
598 } 694 }
599 } else if (type == NotificationType::PROFILE_DESTROYED) { 695 } else if (type == NotificationType::PROFILE_DESTROYED) {
600 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); 696 DCHECK_EQ(profile_, Source<Profile>(source).ptr());
601 UnregisterObservers(); 697 UnregisterObservers();
602 } else { 698 } else {
603 NOTREACHED() << "Unexpected notification"; 699 NOTREACHED() << "Unexpected notification";
604 } 700 }
605 } 701 }
606 702
607 PrefProvider::~PrefProvider() { 703 PrefProvider::~PrefProvider() {
608 UnregisterObservers(); 704 UnregisterObservers();
609 } 705 }
610 706
611 // //////////////////////////////////////////////////////////////////////////// 707 // ////////////////////////////////////////////////////////////////////////////
612 // Private 708 // Private
613 709
614 void PrefProvider::ReadExceptions(bool overwrite) { 710 void PrefProvider::ReadExceptions(bool overwrite) {
615 base::AutoLock auto_lock(lock()); 711 base::AutoLock auto_lock(lock_);
616 712
617 PrefService* prefs = profile_->GetPrefs(); 713 PrefService* prefs = profile_->GetPrefs();
618 const DictionaryValue* all_settings_dictionary = 714 const DictionaryValue* all_settings_dictionary =
619 prefs->GetDictionary(prefs::kContentSettingsPatterns); 715 prefs->GetDictionary(prefs::kContentSettingsPatterns);
620 716
621 if (overwrite) 717 if (overwrite)
622 host_content_settings()->clear(); 718 value_map_.Clear();
623 719
624 updating_preferences_ = true; 720 updating_preferences_ = true;
625
626 // Careful: The returned value could be NULL if the pref has never been set. 721 // Careful: The returned value could be NULL if the pref has never been set.
627 if (all_settings_dictionary != NULL) { 722 if (all_settings_dictionary != NULL) {
628 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns); 723 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns);
629 DictionaryValue* mutable_settings; 724 DictionaryValue* mutable_settings;
630 scoped_ptr<DictionaryValue> mutable_settings_scope; 725 scoped_ptr<DictionaryValue> mutable_settings_scope;
631 726
632 if (!is_incognito()) { 727 if (!is_incognito_) {
633 mutable_settings = update.Get(); 728 mutable_settings = update.Get();
634 } else { 729 } else {
635 // Create copy as we do not want to persist anything in OTR prefs. 730 // Create copy as we do not want to persist anything in OTR prefs.
636 mutable_settings = all_settings_dictionary->DeepCopy(); 731 mutable_settings = all_settings_dictionary->DeepCopy();
637 mutable_settings_scope.reset(mutable_settings); 732 mutable_settings_scope.reset(mutable_settings);
638 } 733 }
639
640 // Convert all Unicode patterns into punycode form, then read. 734 // Convert all Unicode patterns into punycode form, then read.
641 CanonicalizeContentSettingsExceptions(mutable_settings); 735 CanonicalizeContentSettingsExceptions(mutable_settings);
642 736
643 for (DictionaryValue::key_iterator i(mutable_settings->begin_keys()); 737 for (DictionaryValue::key_iterator i(mutable_settings->begin_keys());
644 i != mutable_settings->end_keys(); ++i) { 738 i != mutable_settings->end_keys(); ++i) {
645 const std::string& pattern(*i); 739 const std::string& pattern_str(*i);
646 if (!ContentSettingsPattern::FromString(pattern).IsValid()) 740 ContentSettingsPattern pattern =
741 ContentSettingsPattern::FromString(pattern_str);
742 if (!pattern.IsValid())
647 LOG(WARNING) << "Invalid pattern stored in content settings"; 743 LOG(WARNING) << "Invalid pattern stored in content settings";
648 DictionaryValue* pattern_settings_dictionary = NULL; 744
745 // Get settings dictionary for the current pattern string, and read
746 // settings from the dictionary.
747 DictionaryValue* settings_dictionary = NULL;
649 bool found = mutable_settings->GetDictionaryWithoutPathExpansion( 748 bool found = mutable_settings->GetDictionaryWithoutPathExpansion(
650 pattern, &pattern_settings_dictionary); 749 pattern_str, &settings_dictionary);
651 DCHECK(found); 750 DCHECK(found);
652 751
653 ExtendedContentSettings extended_settings; 752 for (DictionaryValue::key_iterator i(settings_dictionary->begin_keys());
654 GetSettingsFromDictionary(pattern_settings_dictionary, 753 i != settings_dictionary->end_keys();
655 &extended_settings.content_settings); 754 ++i) {
656 GetResourceSettingsFromDictionary( 755 const std::string& content_type_str(*i);
657 pattern_settings_dictionary, 756 ContentSettingsType content_type = StringToContentSettingsType(
658 &extended_settings.content_settings_for_resources); 757 content_type_str);
758 if (content_type == CONTENT_SETTINGS_TYPE_DEFAULT) {
759 NOTREACHED();
760 LOG(WARNING) << "Skip settings for invalid content settings type '"
761 << content_type_str << "'";
762 continue;
763 }
659 764
660 (*host_content_settings())[pattern] = extended_settings; 765 if (RequiresResourceIdentifier(content_type)) {
766 DictionaryValue* resource_dictionary = NULL;
767 bool found = settings_dictionary->GetDictionary(
768 content_type_str, &resource_dictionary);
769 DCHECK(found);
770 for (DictionaryValue::key_iterator j(
771 resource_dictionary->begin_keys());
772 j != resource_dictionary->end_keys();
773 ++j) {
774 const std::string& resource_identifier(*j);
775 int setting = CONTENT_SETTING_DEFAULT;
776 found = resource_dictionary->GetIntegerWithoutPathExpansion(
777 resource_identifier, &setting);
778 DCHECK(found);
779 setting = ClickToPlayFixup(content_type,
780 ContentSetting(setting));
781 value_map_.SetValue(pattern,
782 pattern,
783 content_type,
784 resource_identifier,
785 Value::CreateIntegerValue(setting));
786 }
787 } else {
788 int setting = CONTENT_SETTING_DEFAULT;
789 bool found = settings_dictionary->GetIntegerWithoutPathExpansion(
790 content_type_str, &setting);
791 DCHECK(found);
792 setting = FixObsoleteCookiePromptMode(content_type,
793 ContentSetting(setting));
794 setting = ClickToPlayFixup(content_type,
795 ContentSetting(setting));
796 value_map_.SetValue(pattern,
797 pattern,
798 content_type,
799 ResourceIdentifier(""),
800 Value::CreateIntegerValue(setting));
801 }
802 }
661 } 803 }
662 } 804 }
663 updating_preferences_ = false; 805 updating_preferences_ = false;
664 } 806 }
665 807
666 void PrefProvider::CanonicalizeContentSettingsExceptions( 808 void PrefProvider::CanonicalizeContentSettingsExceptions(
667 DictionaryValue* all_settings_dictionary) { 809 DictionaryValue* all_settings_dictionary) {
668 DCHECK(all_settings_dictionary); 810 DCHECK(all_settings_dictionary);
669 811
670 std::vector<std::string> remove_items; 812 std::vector<std::string> remove_items;
(...skipping 29 matching lines...) Expand all
700 842
701 for (size_t i = 0; i < move_items.size(); ++i) { 843 for (size_t i = 0; i < move_items.size(); ++i) {
702 Value* pattern_settings_dictionary = NULL; 844 Value* pattern_settings_dictionary = NULL;
703 all_settings_dictionary->RemoveWithoutPathExpansion( 845 all_settings_dictionary->RemoveWithoutPathExpansion(
704 move_items[i].first, &pattern_settings_dictionary); 846 move_items[i].first, &pattern_settings_dictionary);
705 all_settings_dictionary->SetWithoutPathExpansion( 847 all_settings_dictionary->SetWithoutPathExpansion(
706 move_items[i].second, pattern_settings_dictionary); 848 move_items[i].second, pattern_settings_dictionary);
707 } 849 }
708 } 850 }
709 851
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( 852 void PrefProvider::NotifyObservers(
770 const ContentSettingsDetails& details) { 853 const ContentSettingsDetails& details) {
771 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 854 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
772 if (initializing_ || profile_ == NULL) 855 if (initializing_ || profile_ == NULL)
773 return; 856 return;
774 NotificationService::current()->Notify( 857 NotificationService::current()->Notify(
775 NotificationType::CONTENT_SETTINGS_CHANGED, 858 NotificationType::CONTENT_SETTINGS_CHANGED,
776 Source<HostContentSettingsMap>( 859 Source<HostContentSettingsMap>(
777 profile_->GetHostContentSettingsMap()), 860 profile_->GetHostContentSettingsMap()),
778 Details<const ContentSettingsDetails>(&details)); 861 Details<const ContentSettingsDetails>(&details));
(...skipping 17 matching lines...) Expand all
796 for (DictionaryValue::key_iterator 879 for (DictionaryValue::key_iterator
797 i(all_settings_dictionary->begin_keys()); 880 i(all_settings_dictionary->begin_keys());
798 i != all_settings_dictionary->end_keys(); ++i) { 881 i != all_settings_dictionary->end_keys(); ++i) {
799 const std::string& host(*i); 882 const std::string& host(*i);
800 ContentSettingsPattern pattern = ContentSettingsPattern::FromString( 883 ContentSettingsPattern pattern = ContentSettingsPattern::FromString(
801 std::string(ContentSettingsPattern::kDomainWildcard) + host); 884 std::string(ContentSettingsPattern::kDomainWildcard) + host);
802 DictionaryValue* host_settings_dictionary = NULL; 885 DictionaryValue* host_settings_dictionary = NULL;
803 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( 886 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
804 host, &host_settings_dictionary); 887 host, &host_settings_dictionary);
805 DCHECK(found); 888 DCHECK(found);
806 ContentSettings settings; 889
807 GetSettingsFromDictionary(host_settings_dictionary, &settings); 890 for (DictionaryValue::key_iterator i(
808 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { 891 host_settings_dictionary->begin_keys());
809 if (settings.settings[j] != CONTENT_SETTING_DEFAULT && 892 i != host_settings_dictionary->end_keys();
810 !RequiresResourceIdentifier(ContentSettingsType(j))) { 893 ++i) {
811 SetContentSetting( 894 const std::string& content_type_str(*i);
812 pattern, 895 ContentSettingsType content_type =
813 pattern, 896 StringToContentSettingsType(content_type_str);
814 ContentSettingsType(j), 897
815 "", 898 if (content_type == CONTENT_SETTINGS_TYPE_DEFAULT) {
816 settings.settings[j]); 899 NOTREACHED();
900 LOG(DFATAL) << "Skip settings for invalid content settings type '"
901 << content_type_str << "'";
902 continue;
903 }
904
905 if (!RequiresResourceIdentifier(content_type)) {
906 int setting_int_value = CONTENT_SETTING_DEFAULT;
907 bool found =
908 host_settings_dictionary->GetIntegerWithoutPathExpansion(
909 content_type_str, &setting_int_value);
910 DCHECK(found);
911 ContentSetting setting = IntToContentSetting(setting_int_value);
912
913 setting = FixObsoleteCookiePromptMode(content_type, setting);
914 setting = ClickToPlayFixup(content_type, setting);
915
916 // TODO(markusheintz): Maybe this check can be removed.
917 if (setting != CONTENT_SETTING_DEFAULT) {
918 SetContentSetting(
919 pattern,
920 pattern,
921 content_type,
922 "",
923 setting);
924 }
817 } 925 }
818 } 926 }
819 } 927 }
820 prefs->ClearPref(prefs::kPerHostContentSettings); 928 prefs->ClearPref(prefs::kPerHostContentSettings);
821 } 929 }
822 } 930 }
823 931
824 void PrefProvider::MigrateObsoletePopupsPref(PrefService* prefs) { 932 void PrefProvider::MigrateObsoletePopupsPref(PrefService* prefs) {
825 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { 933 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) {
826 const ListValue* whitelist_pref = 934 const ListValue* whitelist_pref =
827 prefs->GetList(prefs::kPopupWhitelistedHosts); 935 prefs->GetList(prefs::kPopupWhitelistedHosts);
828 for (ListValue::const_iterator i(whitelist_pref->begin()); 936 for (ListValue::const_iterator i(whitelist_pref->begin());
829 i != whitelist_pref->end(); ++i) { 937 i != whitelist_pref->end(); ++i) {
830 std::string host; 938 std::string host;
831 (*i)->GetAsString(&host); 939 (*i)->GetAsString(&host);
832 SetContentSetting(ContentSettingsPattern::FromString(host), 940 SetContentSetting(ContentSettingsPattern::FromString(host),
833 ContentSettingsPattern::FromString(host), 941 ContentSettingsPattern::FromString(host),
834 CONTENT_SETTINGS_TYPE_POPUPS, 942 CONTENT_SETTINGS_TYPE_POPUPS,
835 "", 943 "",
836 CONTENT_SETTING_ALLOW); 944 CONTENT_SETTING_ALLOW);
837 } 945 }
838 prefs->ClearPref(prefs::kPopupWhitelistedHosts); 946 prefs->ClearPref(prefs::kPopupWhitelistedHosts);
839 } 947 }
840 } 948 }
841 949
842 } // namespace content_settings 950 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698