OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
16 #include "base/prefs/scoped_user_pref_update.h" | 16 #include "base/prefs/scoped_user_pref_update.h" |
| 17 #include "base/time/clock.h" |
| 18 #include "base/time/default_clock.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/content_settings/content_settings_rule.h" | 20 #include "chrome/browser/content_settings/content_settings_rule.h" |
19 #include "chrome/browser/content_settings/content_settings_utils.h" | 21 #include "chrome/browser/content_settings/content_settings_utils.h" |
20 #include "chrome/browser/content_settings/host_content_settings_map.h" | 22 #include "chrome/browser/content_settings/host_content_settings_map.h" |
21 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/content_settings.h" | 24 #include "chrome/common/content_settings.h" |
23 #include "chrome/common/content_settings_pattern.h" | 25 #include "chrome/common/content_settings_pattern.h" |
24 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
25 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
26 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/notification_details.h" | 29 #include "content/public/browser/notification_details.h" |
28 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
29 #include "content/public/browser/user_metrics.h" | 31 #include "content/public/browser/user_metrics.h" |
30 #include "url/gurl.h" | 32 #include "url/gurl.h" |
31 | 33 |
32 using base::UserMetricsAction; | 34 using base::UserMetricsAction; |
33 using content::BrowserThread; | 35 using content::BrowserThread; |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 typedef std::pair<std::string, std::string> StringPair; | 39 typedef std::pair<std::string, std::string> StringPair; |
38 typedef std::map<std::string, std::string> StringMap; | 40 typedef std::map<std::string, std::string> StringMap; |
39 | 41 |
40 const char kPerPluginPrefName[] = "per_plugin"; | 42 const char kPerPluginPrefName[] = "per_plugin"; |
41 const char kAudioKey[] = "audio"; | 43 const char kAudioKey[] = "audio"; |
42 const char kVideoKey[] = "video"; | 44 const char kVideoKey[] = "video"; |
| 45 const char kLastUsed[] = "last_used"; |
43 | 46 |
44 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, | 47 ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type, |
45 ContentSetting setting) { | 48 ContentSetting setting) { |
46 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && | 49 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES && |
47 setting == CONTENT_SETTING_ASK) { | 50 setting == CONTENT_SETTING_ASK) { |
48 return CONTENT_SETTING_BLOCK; | 51 return CONTENT_SETTING_BLOCK; |
49 } | 52 } |
50 return setting; | 53 return setting; |
51 } | 54 } |
52 | 55 |
(...skipping 23 matching lines...) Expand all Loading... |
76 user_prefs::PrefRegistrySyncable* registry) { | 79 user_prefs::PrefRegistrySyncable* registry) { |
77 registry->RegisterIntegerPref( | 80 registry->RegisterIntegerPref( |
78 prefs::kContentSettingsVersion, | 81 prefs::kContentSettingsVersion, |
79 ContentSettingsPattern::kContentSettingsPatternVersion, | 82 ContentSettingsPattern::kContentSettingsPatternVersion, |
80 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 83 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
81 registry->RegisterDictionaryPref( | 84 registry->RegisterDictionaryPref( |
82 prefs::kContentSettingsPatternPairs, | 85 prefs::kContentSettingsPatternPairs, |
83 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 86 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
84 } | 87 } |
85 | 88 |
86 PrefProvider::PrefProvider(PrefService* prefs, | 89 PrefProvider::PrefProvider(PrefService* prefs, bool incognito) |
87 bool incognito) | 90 : prefs_(prefs), |
88 : prefs_(prefs), | 91 clock_(new base::DefaultClock()), |
89 is_incognito_(incognito), | 92 is_incognito_(incognito), |
90 updating_preferences_(false) { | 93 owns_clock_(true), |
| 94 updating_preferences_(false) { |
91 DCHECK(prefs_); | 95 DCHECK(prefs_); |
92 // Verify preferences version. | 96 // Verify preferences version. |
93 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { | 97 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { |
94 prefs_->SetInteger(prefs::kContentSettingsVersion, | 98 prefs_->SetInteger(prefs::kContentSettingsVersion, |
95 ContentSettingsPattern::kContentSettingsPatternVersion); | 99 ContentSettingsPattern::kContentSettingsPatternVersion); |
96 } | 100 } |
97 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > | 101 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > |
98 ContentSettingsPattern::kContentSettingsPatternVersion) { | 102 ContentSettingsPattern::kContentSettingsPatternVersion) { |
99 return; | 103 return; |
100 } | 104 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 NULL); | 211 NULL); |
208 } | 212 } |
209 NotifyObservers(ContentSettingsPattern(), | 213 NotifyObservers(ContentSettingsPattern(), |
210 ContentSettingsPattern(), | 214 ContentSettingsPattern(), |
211 content_type, | 215 content_type, |
212 std::string()); | 216 std::string()); |
213 } | 217 } |
214 | 218 |
215 PrefProvider::~PrefProvider() { | 219 PrefProvider::~PrefProvider() { |
216 DCHECK(!prefs_); | 220 DCHECK(!prefs_); |
| 221 if (owns_clock_) |
| 222 delete clock_; |
217 } | 223 } |
218 | 224 |
219 RuleIterator* PrefProvider::GetRuleIterator( | 225 RuleIterator* PrefProvider::GetRuleIterator( |
220 ContentSettingsType content_type, | 226 ContentSettingsType content_type, |
221 const ResourceIdentifier& resource_identifier, | 227 const ResourceIdentifier& resource_identifier, |
222 bool incognito) const { | 228 bool incognito) const { |
223 if (incognito) | 229 if (incognito) |
224 return incognito_value_map_.GetRuleIterator(content_type, | 230 return incognito_value_map_.GetRuleIterator(content_type, |
225 resource_identifier, | 231 resource_identifier, |
226 &lock_); | 232 &lock_); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } else { | 289 } else { |
284 resource_dictionary->SetWithoutPathExpansion( | 290 resource_dictionary->SetWithoutPathExpansion( |
285 resource_identifier, value->DeepCopy()); | 291 resource_identifier, value->DeepCopy()); |
286 } | 292 } |
287 } else { | 293 } else { |
288 // Update settings dictionary. | 294 // Update settings dictionary. |
289 std::string setting_path = GetTypeName(content_type); | 295 std::string setting_path = GetTypeName(content_type); |
290 if (value == NULL) { | 296 if (value == NULL) { |
291 settings_dictionary->RemoveWithoutPathExpansion(setting_path, | 297 settings_dictionary->RemoveWithoutPathExpansion(setting_path, |
292 NULL); | 298 NULL); |
| 299 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); |
293 } else { | 300 } else { |
294 settings_dictionary->SetWithoutPathExpansion( | 301 settings_dictionary->SetWithoutPathExpansion( |
295 setting_path, value->DeepCopy()); | 302 setting_path, value->DeepCopy()); |
296 } | 303 } |
297 } | 304 } |
298 // Remove the settings dictionary if it is empty. | 305 // Remove the settings dictionary if it is empty. |
299 if (settings_dictionary->empty()) { | 306 if (settings_dictionary->empty()) { |
300 pattern_pairs_settings->RemoveWithoutPathExpansion( | 307 pattern_pairs_settings->RemoveWithoutPathExpansion( |
301 pattern_str, NULL); | 308 pattern_str, NULL); |
302 } | 309 } |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 } | 566 } |
560 | 567 |
561 void PrefProvider::ShutdownOnUIThread() { | 568 void PrefProvider::ShutdownOnUIThread() { |
562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
563 DCHECK(prefs_); | 570 DCHECK(prefs_); |
564 RemoveAllObservers(); | 571 RemoveAllObservers(); |
565 pref_change_registrar_.RemoveAll(); | 572 pref_change_registrar_.RemoveAll(); |
566 prefs_ = NULL; | 573 prefs_ = NULL; |
567 } | 574 } |
568 | 575 |
| 576 void PrefProvider::UpdateLastUsage( |
| 577 const ContentSettingsPattern& primary_pattern, |
| 578 const ContentSettingsPattern& secondary_pattern, |
| 579 ContentSettingsType content_type) { |
| 580 // Don't write if in incognito. |
| 581 if (is_incognito_) { |
| 582 return; |
| 583 } |
| 584 |
| 585 // Ensure that |lock_| is not held by this thread, since this function will |
| 586 // send out notifications (by |~DictionaryPrefUpdate|). |
| 587 AssertLockNotHeld(); |
| 588 |
| 589 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
| 590 { |
| 591 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
| 592 base::DictionaryValue* pattern_pairs_settings = update.Get(); |
| 593 |
| 594 std::string pattern_str( |
| 595 CreatePatternString(primary_pattern, secondary_pattern)); |
| 596 base::DictionaryValue* settings_dictionary = NULL; |
| 597 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
| 598 pattern_str, &settings_dictionary); |
| 599 |
| 600 if (!found) { |
| 601 settings_dictionary = new base::DictionaryValue; |
| 602 pattern_pairs_settings->SetWithoutPathExpansion(pattern_str, |
| 603 settings_dictionary); |
| 604 } |
| 605 |
| 606 if (settings_dictionary) { |
| 607 base::DictionaryValue* last_used_dictionary = NULL; |
| 608 found = settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 609 kLastUsed, &last_used_dictionary); |
| 610 |
| 611 if (!found) { |
| 612 last_used_dictionary = new base::DictionaryValue; |
| 613 settings_dictionary->SetWithoutPathExpansion(kLastUsed, |
| 614 last_used_dictionary); |
| 615 } |
| 616 |
| 617 std::string settings_path = GetTypeName(content_type); |
| 618 last_used_dictionary->Set( |
| 619 settings_path, new base::FundamentalValue(clock_->Now().ToDoubleT())); |
| 620 } |
| 621 } |
| 622 } |
| 623 |
| 624 base::Time PrefProvider::GetLastUsage( |
| 625 const ContentSettingsPattern& primary_pattern, |
| 626 const ContentSettingsPattern& secondary_pattern, |
| 627 ContentSettingsType content_type) { |
| 628 const base::DictionaryValue* pattern_pairs_settings = |
| 629 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
| 630 std::string pattern_str( |
| 631 CreatePatternString(primary_pattern, secondary_pattern)); |
| 632 |
| 633 const base::DictionaryValue* settings_dictionary = NULL; |
| 634 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
| 635 pattern_str, &settings_dictionary); |
| 636 |
| 637 if (!found) |
| 638 return base::Time(); |
| 639 |
| 640 const base::DictionaryValue* last_used_dictionary = NULL; |
| 641 found = settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 642 kLastUsed, &last_used_dictionary); |
| 643 |
| 644 if (!found) |
| 645 return base::Time(); |
| 646 |
| 647 double last_used_time; |
| 648 found = last_used_dictionary->GetDoubleWithoutPathExpansion( |
| 649 GetTypeName(content_type), &last_used_time); |
| 650 |
| 651 if (!found) |
| 652 return base::Time(); |
| 653 |
| 654 return base::Time::FromDoubleT(last_used_time); |
| 655 } |
| 656 |
569 void PrefProvider::AssertLockNotHeld() const { | 657 void PrefProvider::AssertLockNotHeld() const { |
570 #if !defined(NDEBUG) | 658 #if !defined(NDEBUG) |
571 // |Lock::Acquire()| will assert if the lock is held by this thread. | 659 // |Lock::Acquire()| will assert if the lock is held by this thread. |
572 lock_.Acquire(); | 660 lock_.Acquire(); |
573 lock_.Release(); | 661 lock_.Release(); |
574 #endif | 662 #endif |
575 } | 663 } |
576 | 664 |
| 665 void PrefProvider::SetClockForTesting(base::Clock* clock) { |
| 666 if (owns_clock_) |
| 667 delete clock_; |
| 668 clock_ = clock; |
| 669 owns_clock_ = false; |
| 670 } |
| 671 |
577 } // namespace content_settings | 672 } // namespace content_settings |
OLD | NEW |