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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/time/time.h"
16 #include "base/tuple.h"
17 #include "chrome/browser/content_settings/content_settings_provider.h"
18 #include "chrome/common/content_settings.h"
19 #include "chrome/common/content_settings_pattern.h"
20 #include "extensions/browser/extension_prefs_scope.h"
26 namespace content_settings {
27 class OriginIdentifierValueMap;
31 namespace extensions {
33 // This class is the backend for extension-defined content settings. It is used
34 // by the content_settings::CustomExtensionProvider to integrate its settings
35 // into the HostContentSettingsMap and by the content settings extension API to
36 // provide extensions with access to content settings.
37 class ContentSettingsStore
38 : public base::RefCountedThreadSafe<ContentSettingsStore> {
42 virtual ~Observer() {}
44 // Called when a content setting changes in the
45 // ContentSettingsStore.
46 virtual void OnContentSettingChanged(
47 const std::string& extension_id,
51 ContentSettingsStore();
53 // //////////////////////////////////////////////////////////////////////////
55 content_settings::RuleIterator* GetRuleIterator(
56 ContentSettingsType type,
57 const content_settings::ResourceIdentifier& identifier,
58 bool incognito) const;
60 // Sets the content |setting| for |pattern| of extension |ext_id|. The
61 // |incognito| flag allow to set whether the provided setting is for
62 // incognito mode only.
63 // Precondition: the extension must be registered.
64 // This method should only be called on the UI thread.
65 void SetExtensionContentSetting(
66 const std::string& ext_id,
67 const ContentSettingsPattern& embedded_pattern,
68 const ContentSettingsPattern& top_level_pattern,
69 ContentSettingsType type,
70 const content_settings::ResourceIdentifier& identifier,
71 ContentSetting setting,
72 ExtensionPrefsScope scope);
74 // Clears all contents settings set by the extension |ext_id|.
75 void ClearContentSettingsForExtension(const std::string& ext_id,
76 ExtensionPrefsScope scope);
78 // Serializes all content settings set by the extension with ID |extension_id|
79 // and returns them as a ListValue. The caller takes ownership of the returned
81 base::ListValue* GetSettingsForExtension(const std::string& extension_id,
82 ExtensionPrefsScope scope) const;
84 // Deserializes content settings rules from |list| and applies them as set by
85 // the extension with ID |extension_id|.
86 void SetExtensionContentSettingFromList(const std::string& extension_id,
87 const base::ListValue* list,
88 ExtensionPrefsScope scope);
90 // //////////////////////////////////////////////////////////////////////////
92 // Registers the time when an extension |ext_id| is installed.
93 void RegisterExtension(const std::string& ext_id,
94 const base::Time& install_time,
97 // Deletes all entries related to extension |ext_id|.
98 void UnregisterExtension(const std::string& ext_id);
100 // Hides or makes the extension content settings of the specified extension
102 void SetExtensionState(const std::string& ext_id, bool is_enabled);
104 // Adds |observer|. This method should only be called on the UI thread.
105 void AddObserver(Observer* observer);
107 // Remove |observer|. This method should only be called on the UI thread.
108 void RemoveObserver(Observer* observer);
111 friend class base::RefCountedThreadSafe<ContentSettingsStore>;
113 struct ExtensionEntry;
115 typedef std::multimap<base::Time, ExtensionEntry*> ExtensionEntryMap;
117 virtual ~ContentSettingsStore();
119 content_settings::OriginIdentifierValueMap* GetValueMap(
120 const std::string& ext_id,
121 ExtensionPrefsScope scope);
123 const content_settings::OriginIdentifierValueMap* GetValueMap(
124 const std::string& ext_id,
125 ExtensionPrefsScope scope) const;
127 void NotifyOfContentSettingChanged(const std::string& extension_id,
130 bool OnCorrectThread();
132 ExtensionEntryMap::iterator FindEntry(const std::string& ext_id);
133 ExtensionEntryMap::const_iterator FindEntry(const std::string& ext_id) const;
135 ExtensionEntryMap entries_;
137 ObserverList<Observer, false> observers_;
139 mutable base::Lock lock_;
141 DISALLOW_COPY_AND_ASSIGN(ContentSettingsStore);
144 } // namespace extensions
146 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_