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