| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/content_settings/core/browser/content_settings_pref.h" | 5 #include "components/content_settings/core/browser/content_settings_pref.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 DCHECK(!in_value || IsValueAllowedForType(in_value, content_type_)); | 100 DCHECK(!in_value || IsValueAllowedForType(in_value, content_type_)); |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
| 102 DCHECK(prefs_); | 102 DCHECK(prefs_); |
| 103 DCHECK(primary_pattern != ContentSettingsPattern::Wildcard() || | 103 DCHECK(primary_pattern != ContentSettingsPattern::Wildcard() || |
| 104 secondary_pattern != ContentSettingsPattern::Wildcard() || | 104 secondary_pattern != ContentSettingsPattern::Wildcard() || |
| 105 !resource_identifier.empty()); | 105 !resource_identifier.empty()); |
| 106 | 106 |
| 107 // At this point take the ownership of the |in_value|. | 107 // At this point take the ownership of the |in_value|. |
| 108 scoped_ptr<base::Value> value(in_value); | 108 scoped_ptr<base::Value> value(in_value); |
| 109 | 109 |
| 110 ContentSetting previous_value = CONTENT_SETTING_DEFAULT; |
| 111 |
| 110 // Update in memory value map. | 112 // Update in memory value map. |
| 111 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; | 113 OriginIdentifierValueMap* map_to_modify = &incognito_value_map_; |
| 112 if (!is_incognito_) | 114 if (!is_incognito_) |
| 113 map_to_modify = &value_map_; | 115 map_to_modify = &value_map_; |
| 114 | 116 |
| 115 { | 117 { |
| 116 base::AutoLock auto_lock(lock_); | 118 base::AutoLock auto_lock(lock_); |
| 117 if (value.get()) { | 119 if (value.get()) { |
| 118 map_to_modify->SetValue( | 120 map_to_modify->SetValue( |
| 119 primary_pattern, | 121 primary_pattern, |
| 120 secondary_pattern, | 122 secondary_pattern, |
| 121 content_type_, | 123 content_type_, |
| 122 resource_identifier, | 124 resource_identifier, |
| 123 value->DeepCopy()); | 125 value->DeepCopy()); |
| 124 } else { | 126 } else { |
| 125 map_to_modify->DeleteValue( | 127 map_to_modify->DeleteValue( |
| 126 primary_pattern, | 128 primary_pattern, |
| 127 secondary_pattern, | 129 secondary_pattern, |
| 128 content_type_, | 130 content_type_, |
| 129 resource_identifier); | 131 resource_identifier); |
| 130 } | 132 } |
| 131 } | 133 } |
| 132 // Update the content settings preference. | 134 // Update the content settings preference. |
| 133 if (!is_incognito_) { | 135 if (!is_incognito_) { |
| 134 UpdatePref(primary_pattern, | 136 previous_value = UpdatePref(primary_pattern, |
| 135 secondary_pattern, | 137 secondary_pattern, |
| 136 resource_identifier, | 138 resource_identifier, |
| 137 value.get()); | 139 value.get()); |
| 138 } | 140 } |
| 139 | 141 |
| 140 notify_callback_.Run( | 142 notify_callback_.Run(primary_pattern, secondary_pattern, content_type_, |
| 141 primary_pattern, secondary_pattern, content_type_, resource_identifier); | 143 previous_value, resource_identifier); |
| 142 | 144 |
| 143 return true; | 145 return true; |
| 144 } | 146 } |
| 145 | 147 |
| 146 void ContentSettingsPref::ClearPref() { | 148 void ContentSettingsPref::ClearPref() { |
| 147 DCHECK(thread_checker_.CalledOnValidThread()); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 148 DCHECK(prefs_); | 150 DCHECK(prefs_); |
| 149 | 151 |
| 150 { | 152 { |
| 151 base::AutoLock auto_lock(lock_); | 153 base::AutoLock auto_lock(lock_); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 167 if (is_incognito_) { | 169 if (is_incognito_) { |
| 168 base::AutoLock auto_lock(lock_); | 170 base::AutoLock auto_lock(lock_); |
| 169 incognito_value_map_.clear(); | 171 incognito_value_map_.clear(); |
| 170 } else { | 172 } else { |
| 171 ClearPref(); | 173 ClearPref(); |
| 172 } | 174 } |
| 173 | 175 |
| 174 notify_callback_.Run(ContentSettingsPattern(), | 176 notify_callback_.Run(ContentSettingsPattern(), |
| 175 ContentSettingsPattern(), | 177 ContentSettingsPattern(), |
| 176 content_type_, | 178 content_type_, |
| 179 CONTENT_SETTING_DEFAULT, |
| 177 ResourceIdentifier()); | 180 ResourceIdentifier()); |
| 178 } | 181 } |
| 179 | 182 |
| 180 void ContentSettingsPref::UpdateLastUsage( | 183 void ContentSettingsPref::UpdateLastUsage( |
| 181 const ContentSettingsPattern& primary_pattern, | 184 const ContentSettingsPattern& primary_pattern, |
| 182 const ContentSettingsPattern& secondary_pattern, | 185 const ContentSettingsPattern& secondary_pattern, |
| 183 base::Clock* clock) { | 186 base::Clock* clock) { |
| 184 // Don't write if in incognito. | 187 // Don't write if in incognito. |
| 185 if (is_incognito_) { | 188 if (is_incognito_) { |
| 186 return; | 189 return; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 DCHECK(thread_checker_.CalledOnValidThread()); | 372 DCHECK(thread_checker_.CalledOnValidThread()); |
| 370 | 373 |
| 371 if (updating_preferences_) | 374 if (updating_preferences_) |
| 372 return; | 375 return; |
| 373 | 376 |
| 374 ReadContentSettingsFromPref(); | 377 ReadContentSettingsFromPref(); |
| 375 | 378 |
| 376 notify_callback_.Run(ContentSettingsPattern(), | 379 notify_callback_.Run(ContentSettingsPattern(), |
| 377 ContentSettingsPattern(), | 380 ContentSettingsPattern(), |
| 378 content_type_, | 381 content_type_, |
| 382 CONTENT_SETTING_DEFAULT, |
| 379 ResourceIdentifier()); | 383 ResourceIdentifier()); |
| 380 } | 384 } |
| 381 | 385 |
| 382 void ContentSettingsPref::UpdatePref( | 386 ContentSetting ContentSettingsPref::UpdatePref( |
| 383 const ContentSettingsPattern& primary_pattern, | 387 const ContentSettingsPattern& primary_pattern, |
| 384 const ContentSettingsPattern& secondary_pattern, | 388 const ContentSettingsPattern& secondary_pattern, |
| 385 const ResourceIdentifier& resource_identifier, | 389 const ResourceIdentifier& resource_identifier, |
| 386 const base::Value* value) { | 390 const base::Value* value) { |
| 387 // Ensure that |lock_| is not held by this thread, since this function will | 391 // Ensure that |lock_| is not held by this thread, since this function will |
| 388 // send out notifications (by |~DictionaryPrefUpdate|). | 392 // send out notifications (by |~DictionaryPrefUpdate|). |
| 389 AssertLockNotHeld(); | 393 AssertLockNotHeld(); |
| 390 | 394 |
| 395 ContentSetting old_content_setting = CONTENT_SETTING_DEFAULT; |
| 396 |
| 391 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | 397 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
| 392 { | 398 { |
| 393 DictionaryPrefUpdate update(prefs_, pref_name_); | 399 DictionaryPrefUpdate update(prefs_, pref_name_); |
| 394 base::DictionaryValue* pattern_pairs_settings = update.Get(); | 400 base::DictionaryValue* pattern_pairs_settings = update.Get(); |
| 395 | 401 |
| 396 // Get settings dictionary for the given patterns. | 402 // Get settings dictionary for the given patterns. |
| 397 std::string pattern_str(CreatePatternString(primary_pattern, | 403 std::string pattern_str(CreatePatternString(primary_pattern, |
| 398 secondary_pattern)); | 404 secondary_pattern)); |
| 399 base::DictionaryValue* settings_dictionary = NULL; | 405 base::DictionaryValue* settings_dictionary = NULL; |
| 400 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | 406 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
| 401 pattern_str, &settings_dictionary); | 407 pattern_str, &settings_dictionary); |
| 402 | 408 |
| 403 if (!found && value) { | 409 if (!found && value) { |
| 404 settings_dictionary = new base::DictionaryValue; | 410 settings_dictionary = new base::DictionaryValue; |
| 405 pattern_pairs_settings->SetWithoutPathExpansion( | 411 pattern_pairs_settings->SetWithoutPathExpansion( |
| 406 pattern_str, settings_dictionary); | 412 pattern_str, settings_dictionary); |
| 407 } | 413 } |
| 408 | 414 |
| 409 if (settings_dictionary) { | 415 if (settings_dictionary) { |
| 416 int old_setting = CONTENT_SETTING_DEFAULT; |
| 417 settings_dictionary->GetIntegerWithoutPathExpansion(kSettingPath, |
| 418 &old_setting); |
| 419 old_content_setting = static_cast<ContentSetting>(old_setting); |
| 420 |
| 410 if (SupportsResourceIdentifiers(content_type_) && | 421 if (SupportsResourceIdentifiers(content_type_) && |
| 411 !resource_identifier.empty()) { | 422 !resource_identifier.empty()) { |
| 412 base::DictionaryValue* resource_dictionary = NULL; | 423 base::DictionaryValue* resource_dictionary = NULL; |
| 413 found = settings_dictionary->GetDictionary( | 424 found = settings_dictionary->GetDictionary( |
| 414 kPerResourceIdentifierPrefName, &resource_dictionary); | 425 kPerResourceIdentifierPrefName, &resource_dictionary); |
| 415 if (!found) { | 426 if (!found) { |
| 416 if (value == NULL) | 427 if (value == NULL) |
| 417 return; // Nothing to remove. Exit early. | 428 return old_content_setting; // Nothing to remove. Exit early. |
| 418 resource_dictionary = new base::DictionaryValue; | 429 resource_dictionary = new base::DictionaryValue; |
| 419 settings_dictionary->Set( | 430 settings_dictionary->Set( |
| 420 kPerResourceIdentifierPrefName, resource_dictionary); | 431 kPerResourceIdentifierPrefName, resource_dictionary); |
| 421 } | 432 } |
| 422 // Update resource dictionary. | 433 // Update resource dictionary. |
| 423 if (value == NULL) { | 434 if (value == NULL) { |
| 424 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, | 435 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, |
| 425 NULL); | 436 NULL); |
| 426 if (resource_dictionary->empty()) { | 437 if (resource_dictionary->empty()) { |
| 427 settings_dictionary->RemoveWithoutPathExpansion( | 438 settings_dictionary->RemoveWithoutPathExpansion( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 441 kSettingPath, value->DeepCopy()); | 452 kSettingPath, value->DeepCopy()); |
| 442 } | 453 } |
| 443 } | 454 } |
| 444 // Remove the settings dictionary if it is empty. | 455 // Remove the settings dictionary if it is empty. |
| 445 if (settings_dictionary->empty()) { | 456 if (settings_dictionary->empty()) { |
| 446 pattern_pairs_settings->RemoveWithoutPathExpansion( | 457 pattern_pairs_settings->RemoveWithoutPathExpansion( |
| 447 pattern_str, NULL); | 458 pattern_str, NULL); |
| 448 } | 459 } |
| 449 } | 460 } |
| 450 } | 461 } |
| 462 |
| 463 return old_content_setting; |
| 451 } | 464 } |
| 452 | 465 |
| 453 // static | 466 // static |
| 454 void ContentSettingsPref::CanonicalizeContentSettingsExceptions( | 467 void ContentSettingsPref::CanonicalizeContentSettingsExceptions( |
| 455 base::DictionaryValue* all_settings_dictionary) { | 468 base::DictionaryValue* all_settings_dictionary) { |
| 456 DCHECK(all_settings_dictionary); | 469 DCHECK(all_settings_dictionary); |
| 457 | 470 |
| 458 std::vector<std::string> remove_items; | 471 std::vector<std::string> remove_items; |
| 459 base::StringPairs move_items; | 472 base::StringPairs move_items; |
| 460 for (base::DictionaryValue::Iterator i(*all_settings_dictionary); | 473 for (base::DictionaryValue::Iterator i(*all_settings_dictionary); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 | 521 |
| 509 void ContentSettingsPref::AssertLockNotHeld() const { | 522 void ContentSettingsPref::AssertLockNotHeld() const { |
| 510 #if !defined(NDEBUG) | 523 #if !defined(NDEBUG) |
| 511 // |Lock::Acquire()| will assert if the lock is held by this thread. | 524 // |Lock::Acquire()| will assert if the lock is held by this thread. |
| 512 lock_.Acquire(); | 525 lock_.Acquire(); |
| 513 lock_.Release(); | 526 lock_.Release(); |
| 514 #endif | 527 #endif |
| 515 } | 528 } |
| 516 | 529 |
| 517 } // namespace content_settings | 530 } // namespace content_settings |
| OLD | NEW |