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

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

Powered by Google App Engine
This is Rietveld 408576698