| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/time/clock.h" | |
| 14 #include "components/content_settings/core/browser/content_settings_info.h" | 13 #include "components/content_settings/core/browser/content_settings_info.h" |
| 15 #include "components/content_settings/core/browser/content_settings_registry.h" | 14 #include "components/content_settings/core/browser/content_settings_registry.h" |
| 16 #include "components/content_settings/core/browser/content_settings_rule.h" | 15 #include "components/content_settings/core/browser/content_settings_rule.h" |
| 17 #include "components/content_settings/core/browser/content_settings_utils.h" | 16 #include "components/content_settings/core/browser/content_settings_utils.h" |
| 18 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 19 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
| 20 #include "components/content_settings/core/common/content_settings_pattern.h" | 19 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 21 #include "components/content_settings/core/common/pref_names.h" | 20 #include "components/content_settings/core/common/pref_names.h" |
| 22 #include "components/prefs/scoped_user_pref_update.h" | 21 #include "components/prefs/scoped_user_pref_update.h" |
| 23 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 const char kSettingPath[] = "setting"; | 26 const char kSettingPath[] = "setting"; |
| 28 const char kPerResourceIdentifierPrefName[] = "per_resource"; | 27 const char kPerResourceIdentifierPrefName[] = "per_resource"; |
| 29 const char kLastUsed[] = "last_used"; | |
| 30 | 28 |
| 31 // If the given content type supports resource identifiers in user preferences, | 29 // If the given content type supports resource identifiers in user preferences, |
| 32 // returns true and sets |pref_key| to the key in the content settings | 30 // returns true and sets |pref_key| to the key in the content settings |
| 33 // dictionary under which per-resource content settings are stored. | 31 // dictionary under which per-resource content settings are stored. |
| 34 // Otherwise, returns false. | 32 // Otherwise, returns false. |
| 35 bool SupportsResourceIdentifiers(ContentSettingsType content_type) { | 33 bool SupportsResourceIdentifiers(ContentSettingsType content_type) { |
| 36 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; | 34 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; |
| 37 } | 35 } |
| 38 | 36 |
| 39 bool IsValueAllowedForType(const base::Value* value, ContentSettingsType type) { | 37 bool IsValueAllowedForType(const base::Value* value, ContentSettingsType type) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } else { | 169 } else { |
| 172 ClearPref(); | 170 ClearPref(); |
| 173 } | 171 } |
| 174 | 172 |
| 175 notify_callback_.Run(ContentSettingsPattern(), | 173 notify_callback_.Run(ContentSettingsPattern(), |
| 176 ContentSettingsPattern(), | 174 ContentSettingsPattern(), |
| 177 content_type_, | 175 content_type_, |
| 178 ResourceIdentifier()); | 176 ResourceIdentifier()); |
| 179 } | 177 } |
| 180 | 178 |
| 181 void ContentSettingsPref::UpdateLastUsage( | |
| 182 const ContentSettingsPattern& primary_pattern, | |
| 183 const ContentSettingsPattern& secondary_pattern, | |
| 184 base::Clock* clock) { | |
| 185 // Don't write if in incognito. | |
| 186 if (is_incognito_) { | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 // Ensure that |lock_| is not held by this thread, since this function will | |
| 191 // send out notifications (by |~DictionaryPrefUpdate|). | |
| 192 AssertLockNotHeld(); | |
| 193 | |
| 194 base::AutoReset<bool> auto_reset(&updating_preferences_, true); | |
| 195 { | |
| 196 DictionaryPrefUpdate update(prefs_, pref_name_); | |
| 197 base::DictionaryValue* pattern_pairs_settings = update.Get(); | |
| 198 | |
| 199 std::string pattern_str( | |
| 200 CreatePatternString(primary_pattern, secondary_pattern)); | |
| 201 base::DictionaryValue* settings_dictionary = NULL; | |
| 202 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | |
| 203 pattern_str, &settings_dictionary); | |
| 204 | |
| 205 if (!found) { | |
| 206 settings_dictionary = new base::DictionaryValue; | |
| 207 pattern_pairs_settings->SetWithoutPathExpansion(pattern_str, | |
| 208 settings_dictionary); | |
| 209 } | |
| 210 | |
| 211 settings_dictionary->SetWithoutPathExpansion( | |
| 212 kLastUsed, new base::FundamentalValue(clock->Now().ToDoubleT())); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 base::Time ContentSettingsPref::GetLastUsage( | |
| 217 const ContentSettingsPattern& primary_pattern, | |
| 218 const ContentSettingsPattern& secondary_pattern) { | |
| 219 const base::DictionaryValue* pattern_pairs_settings = | |
| 220 prefs_->GetDictionary(pref_name_); | |
| 221 std::string pattern_str( | |
| 222 CreatePatternString(primary_pattern, secondary_pattern)); | |
| 223 | |
| 224 const base::DictionaryValue* settings_dictionary = NULL; | |
| 225 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( | |
| 226 pattern_str, &settings_dictionary); | |
| 227 | |
| 228 if (!found) | |
| 229 return base::Time(); | |
| 230 | |
| 231 double last_used_time; | |
| 232 found = settings_dictionary->GetDoubleWithoutPathExpansion( | |
| 233 kLastUsed, &last_used_time); | |
| 234 | |
| 235 if (!found) | |
| 236 return base::Time(); | |
| 237 | |
| 238 return base::Time::FromDoubleT(last_used_time); | |
| 239 } | |
| 240 | |
| 241 size_t ContentSettingsPref::GetNumExceptions() { | 179 size_t ContentSettingsPref::GetNumExceptions() { |
| 242 return value_map_.size(); | 180 return value_map_.size(); |
| 243 } | 181 } |
| 244 | 182 |
| 245 bool ContentSettingsPref::TryLockForTesting() const { | 183 bool ContentSettingsPref::TryLockForTesting() const { |
| 246 if (!lock_.Try()) | 184 if (!lock_.Try()) |
| 247 return false; | 185 return false; |
| 248 lock_.Release(); | 186 lock_.Release(); |
| 249 return true; | 187 return true; |
| 250 } | 188 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 kPerResourceIdentifierPrefName, NULL); | 367 kPerResourceIdentifierPrefName, NULL); |
| 430 } | 368 } |
| 431 } else { | 369 } else { |
| 432 resource_dictionary->SetWithoutPathExpansion( | 370 resource_dictionary->SetWithoutPathExpansion( |
| 433 resource_identifier, value->DeepCopy()); | 371 resource_identifier, value->DeepCopy()); |
| 434 } | 372 } |
| 435 } else { | 373 } else { |
| 436 // Update settings dictionary. | 374 // Update settings dictionary. |
| 437 if (value == NULL) { | 375 if (value == NULL) { |
| 438 settings_dictionary->RemoveWithoutPathExpansion(kSettingPath, NULL); | 376 settings_dictionary->RemoveWithoutPathExpansion(kSettingPath, NULL); |
| 439 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); | |
| 440 } else { | 377 } else { |
| 441 settings_dictionary->SetWithoutPathExpansion( | 378 settings_dictionary->SetWithoutPathExpansion( |
| 442 kSettingPath, value->DeepCopy()); | 379 kSettingPath, value->DeepCopy()); |
| 443 } | 380 } |
| 444 } | 381 } |
| 445 // Remove the settings dictionary if it is empty. | 382 // Remove the settings dictionary if it is empty. |
| 446 if (settings_dictionary->empty()) { | 383 if (settings_dictionary->empty()) { |
| 447 pattern_pairs_settings->RemoveWithoutPathExpansion( | 384 pattern_pairs_settings->RemoveWithoutPathExpansion( |
| 448 pattern_str, NULL); | 385 pattern_str, NULL); |
| 449 } | 386 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 | 446 |
| 510 void ContentSettingsPref::AssertLockNotHeld() const { | 447 void ContentSettingsPref::AssertLockNotHeld() const { |
| 511 #if !defined(NDEBUG) | 448 #if !defined(NDEBUG) |
| 512 // |Lock::Acquire()| will assert if the lock is held by this thread. | 449 // |Lock::Acquire()| will assert if the lock is held by this thread. |
| 513 lock_.Acquire(); | 450 lock_.Acquire(); |
| 514 lock_.Release(); | 451 lock_.Release(); |
| 515 #endif | 452 #endif |
| 516 } | 453 } |
| 517 | 454 |
| 518 } // namespace content_settings | 455 } // namespace content_settings |
| OLD | NEW |