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 updating_preferences_(false) { |
91 DCHECK(prefs_); | 94 DCHECK(prefs_); |
92 // Verify preferences version. | 95 // Verify preferences version. |
93 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { | 96 if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) { |
94 prefs_->SetInteger(prefs::kContentSettingsVersion, | 97 prefs_->SetInteger(prefs::kContentSettingsVersion, |
95 ContentSettingsPattern::kContentSettingsPatternVersion); | 98 ContentSettingsPattern::kContentSettingsPatternVersion); |
96 } | 99 } |
97 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > | 100 if (prefs_->GetInteger(prefs::kContentSettingsVersion) > |
98 ContentSettingsPattern::kContentSettingsPatternVersion) { | 101 ContentSettingsPattern::kContentSettingsPatternVersion) { |
99 return; | 102 return; |
100 } | 103 } |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 } else { | 286 } else { |
284 resource_dictionary->SetWithoutPathExpansion( | 287 resource_dictionary->SetWithoutPathExpansion( |
285 resource_identifier, value->DeepCopy()); | 288 resource_identifier, value->DeepCopy()); |
286 } | 289 } |
287 } else { | 290 } else { |
288 // Update settings dictionary. | 291 // Update settings dictionary. |
289 std::string setting_path = GetTypeName(content_type); | 292 std::string setting_path = GetTypeName(content_type); |
290 if (value == NULL) { | 293 if (value == NULL) { |
291 settings_dictionary->RemoveWithoutPathExpansion(setting_path, | 294 settings_dictionary->RemoveWithoutPathExpansion(setting_path, |
292 NULL); | 295 NULL); |
| 296 settings_dictionary->RemoveWithoutPathExpansion(kLastUsed, NULL); |
293 } else { | 297 } else { |
294 settings_dictionary->SetWithoutPathExpansion( | 298 settings_dictionary->SetWithoutPathExpansion( |
295 setting_path, value->DeepCopy()); | 299 setting_path, value->DeepCopy()); |
296 } | 300 } |
297 } | 301 } |
298 // Remove the settings dictionary if it is empty. | 302 // Remove the settings dictionary if it is empty. |
299 if (settings_dictionary->empty()) { | 303 if (settings_dictionary->empty()) { |
300 pattern_pairs_settings->RemoveWithoutPathExpansion( | 304 pattern_pairs_settings->RemoveWithoutPathExpansion( |
301 pattern_str, NULL); | 305 pattern_str, NULL); |
302 } | 306 } |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 } | 563 } |
560 | 564 |
561 void PrefProvider::ShutdownOnUIThread() { | 565 void PrefProvider::ShutdownOnUIThread() { |
562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 566 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
563 DCHECK(prefs_); | 567 DCHECK(prefs_); |
564 RemoveAllObservers(); | 568 RemoveAllObservers(); |
565 pref_change_registrar_.RemoveAll(); | 569 pref_change_registrar_.RemoveAll(); |
566 prefs_ = NULL; | 570 prefs_ = NULL; |
567 } | 571 } |
568 | 572 |
| 573 void PrefProvider::UpdateLastUsage( |
| 574 const ContentSettingsPattern& primary_pattern, |
| 575 const ContentSettingsPattern& secondary_pattern, |
| 576 ContentSettingsType content_type) { |
| 577 // Don't write if in incognito. |
| 578 if (is_incognito_) { |
| 579 return; |
| 580 } |
| 581 |
| 582 // Ensure that |lock_| is not held by this thread, since this function will |
| 583 // send out notifications (by |~DictionaryPrefUpdate|). |
| 584 AssertLockNotHeld(); |
| 585 |
| 586 base::AutoReset<bool> auto_reset(&updating_preferences_, true); |
| 587 { |
| 588 DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs); |
| 589 base::DictionaryValue* pattern_pairs_settings = update.Get(); |
| 590 |
| 591 std::string pattern_str( |
| 592 CreatePatternString(primary_pattern, secondary_pattern)); |
| 593 base::DictionaryValue* settings_dictionary = NULL; |
| 594 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
| 595 pattern_str, &settings_dictionary); |
| 596 |
| 597 if (!found) { |
| 598 settings_dictionary = new base::DictionaryValue; |
| 599 pattern_pairs_settings->SetWithoutPathExpansion(pattern_str, |
| 600 settings_dictionary); |
| 601 } |
| 602 |
| 603 base::DictionaryValue* last_used_dictionary = NULL; |
| 604 found = settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 605 kLastUsed, &last_used_dictionary); |
| 606 |
| 607 if (!found) { |
| 608 last_used_dictionary = new base::DictionaryValue; |
| 609 settings_dictionary->SetWithoutPathExpansion(kLastUsed, |
| 610 last_used_dictionary); |
| 611 } |
| 612 |
| 613 std::string settings_path = GetTypeName(content_type); |
| 614 last_used_dictionary->Set( |
| 615 settings_path, new base::FundamentalValue(clock_->Now().ToDoubleT())); |
| 616 } |
| 617 } |
| 618 |
| 619 base::Time PrefProvider::GetLastUsage( |
| 620 const ContentSettingsPattern& primary_pattern, |
| 621 const ContentSettingsPattern& secondary_pattern, |
| 622 ContentSettingsType content_type) { |
| 623 const base::DictionaryValue* pattern_pairs_settings = |
| 624 prefs_->GetDictionary(prefs::kContentSettingsPatternPairs); |
| 625 std::string pattern_str( |
| 626 CreatePatternString(primary_pattern, secondary_pattern)); |
| 627 |
| 628 const base::DictionaryValue* settings_dictionary = NULL; |
| 629 bool found = pattern_pairs_settings->GetDictionaryWithoutPathExpansion( |
| 630 pattern_str, &settings_dictionary); |
| 631 |
| 632 if (!found) |
| 633 return base::Time(); |
| 634 |
| 635 const base::DictionaryValue* last_used_dictionary = NULL; |
| 636 found = settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 637 kLastUsed, &last_used_dictionary); |
| 638 |
| 639 if (!found) |
| 640 return base::Time(); |
| 641 |
| 642 double last_used_time; |
| 643 found = last_used_dictionary->GetDoubleWithoutPathExpansion( |
| 644 GetTypeName(content_type), &last_used_time); |
| 645 |
| 646 if (!found) |
| 647 return base::Time(); |
| 648 |
| 649 return base::Time::FromDoubleT(last_used_time); |
| 650 } |
| 651 |
569 void PrefProvider::AssertLockNotHeld() const { | 652 void PrefProvider::AssertLockNotHeld() const { |
570 #if !defined(NDEBUG) | 653 #if !defined(NDEBUG) |
571 // |Lock::Acquire()| will assert if the lock is held by this thread. | 654 // |Lock::Acquire()| will assert if the lock is held by this thread. |
572 lock_.Acquire(); | 655 lock_.Acquire(); |
573 lock_.Release(); | 656 lock_.Release(); |
574 #endif | 657 #endif |
575 } | 658 } |
576 | 659 |
| 660 void PrefProvider::SetClockForTesting(scoped_ptr<base::Clock> clock) { |
| 661 clock_ = clock.Pass(); |
| 662 } |
| 663 |
577 } // namespace content_settings | 664 } // namespace content_settings |
OLD | NEW |