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

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

Powered by Google App Engine
This is Rietveld 408576698