Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / content_settings_pref_provider.h
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
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PREF_PROVIDER_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PREF_PROVIDER_H_
7
8 // A content settings provider that takes its settings out of the pref service.
9
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/synchronization/lock.h"
15 #include "chrome/browser/content_settings/content_settings_utils.h"
16 #include "components/content_settings/core/browser/content_settings_observable_provider.h"
17 #include "components/content_settings/core/browser/content_settings_origin_identifier_value_map.h"
18
19 class PrefService;
20
21 namespace base {
22 class Clock;
23 class DictionaryValue;
24 }
25
26 namespace user_prefs {
27 class PrefRegistrySyncable;
28 }
29
30 namespace content_settings {
31
32 // Content settings provider that provides content settings from the user
33 // preference.
34 class PrefProvider : public ObservableProvider {
35  public:
36   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
37
38   PrefProvider(PrefService* prefs, bool incognito);
39   virtual ~PrefProvider();
40
41   // ProviderInterface implementations.
42   virtual RuleIterator* GetRuleIterator(
43       ContentSettingsType content_type,
44       const ResourceIdentifier& resource_identifier,
45       bool incognito) const OVERRIDE;
46
47   virtual bool SetWebsiteSetting(
48       const ContentSettingsPattern& primary_pattern,
49       const ContentSettingsPattern& secondary_pattern,
50       ContentSettingsType content_type,
51       const ResourceIdentifier& resource_identifier,
52       base::Value* value) OVERRIDE;
53
54   virtual void ClearAllContentSettingsRules(
55       ContentSettingsType content_type) OVERRIDE;
56
57   virtual void ShutdownOnUIThread() OVERRIDE;
58
59   // Records the last time the given pattern has used a certain content setting.
60   void UpdateLastUsage(const ContentSettingsPattern& primary_pattern,
61                        const ContentSettingsPattern& secondary_pattern,
62                        ContentSettingsType content_type);
63
64   base::Time GetLastUsage(const ContentSettingsPattern& primary_pattern,
65                           const ContentSettingsPattern& secondary_pattern,
66                           ContentSettingsType content_type);
67
68   // Gains ownership of |clock|.
69   void SetClockForTesting(scoped_ptr<base::Clock> clock);
70
71  private:
72   friend class DeadlockCheckerThread;  // For testing.
73   // Reads all content settings exceptions from the preference and load them
74   // into the |value_map_|. The |value_map_| is cleared first if |overwrite| is
75   // true.
76   void ReadContentSettingsFromPref(bool overwrite);
77
78   // Callback for changes in the pref with the same name.
79   void OnContentSettingsPatternPairsChanged();
80
81   // Update the preference that stores content settings exceptions and syncs the
82   // value to the obsolete preference. When calling this function, |lock_|
83   // should not be held, since this function will send out notifications of
84   // preference changes.
85   void UpdatePref(
86       const ContentSettingsPattern& primary_pattern,
87       const ContentSettingsPattern& secondary_pattern,
88       ContentSettingsType content_type,
89       const ResourceIdentifier& resource_identifier,
90       const base::Value* value);
91
92   // Migrate the old media setting into new mic/camera content settings.
93   void MigrateObsoleteMediaContentSetting();
94
95   static void CanonicalizeContentSettingsExceptions(
96       base::DictionaryValue* all_settings_dictionary);
97
98   // In the debug mode, asserts that |lock_| is not held by this thread. It's
99   // ok if some other thread holds |lock_|, as long as it will eventually
100   // release it.
101   void AssertLockNotHeld() const;
102
103   // Weak; owned by the Profile and reset in ShutdownOnUIThread.
104   PrefService* prefs_;
105
106   // Can be set for testing.
107   scoped_ptr<base::Clock> clock_;
108
109   bool is_incognito_;
110
111   PrefChangeRegistrar pref_change_registrar_;
112
113   // Whether we are currently updating preferences, this is used to ignore
114   // notifications from the preferences service that we triggered ourself.
115   bool updating_preferences_;
116
117   OriginIdentifierValueMap value_map_;
118
119   OriginIdentifierValueMap incognito_value_map_;
120
121   // Used around accesses to the value map objects to guarantee thread safety.
122   mutable base::Lock lock_;
123
124   DISALLOW_COPY_AND_ASSIGN(PrefProvider);
125 };
126
127 }  // namespace content_settings
128
129 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_PREF_PROVIDER_H_