Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / content_settings / content_settings_store.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_EXTENSIONS_API_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_
7
8 #include <map>
9 #include <string>
10
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"
21
22 namespace base {
23 class ListValue;
24 }
25
26 namespace content_settings {
27 class OriginIdentifierValueMap;
28 class RuleIterator;
29 }
30
31 namespace extensions {
32
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> {
39  public:
40   class Observer {
41    public:
42     virtual ~Observer() {}
43
44     // Called when a content setting changes in the
45     // ContentSettingsStore.
46     virtual void OnContentSettingChanged(
47         const std::string& extension_id,
48         bool incognito) = 0;
49   };
50
51   ContentSettingsStore();
52
53   // //////////////////////////////////////////////////////////////////////////
54
55   content_settings::RuleIterator* GetRuleIterator(
56       ContentSettingsType type,
57       const content_settings::ResourceIdentifier& identifier,
58       bool incognito) const;
59
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);
73
74   // Clears all contents settings set by the extension |ext_id|.
75   void ClearContentSettingsForExtension(const std::string& ext_id,
76                                         ExtensionPrefsScope scope);
77
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
80   // value.
81   base::ListValue* GetSettingsForExtension(const std::string& extension_id,
82                                            ExtensionPrefsScope scope) const;
83
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);
89
90   // //////////////////////////////////////////////////////////////////////////
91
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,
95                          bool is_enabled);
96
97   // Deletes all entries related to extension |ext_id|.
98   void UnregisterExtension(const std::string& ext_id);
99
100   // Hides or makes the extension content settings of the specified extension
101   // visible.
102   void SetExtensionState(const std::string& ext_id, bool is_enabled);
103
104   // Adds |observer|. This method should only be called on the UI thread.
105   void AddObserver(Observer* observer);
106
107   // Remove |observer|. This method should only be called on the UI thread.
108   void RemoveObserver(Observer* observer);
109
110  private:
111   friend class base::RefCountedThreadSafe<ContentSettingsStore>;
112
113   struct ExtensionEntry;
114
115   typedef std::multimap<base::Time, ExtensionEntry*> ExtensionEntryMap;
116
117   virtual ~ContentSettingsStore();
118
119   content_settings::OriginIdentifierValueMap* GetValueMap(
120       const std::string& ext_id,
121       ExtensionPrefsScope scope);
122
123   const content_settings::OriginIdentifierValueMap* GetValueMap(
124       const std::string& ext_id,
125       ExtensionPrefsScope scope) const;
126
127   void NotifyOfContentSettingChanged(const std::string& extension_id,
128                                      bool incognito);
129
130   bool OnCorrectThread();
131
132   ExtensionEntryMap::iterator FindEntry(const std::string& ext_id);
133   ExtensionEntryMap::const_iterator FindEntry(const std::string& ext_id) const;
134
135   ExtensionEntryMap entries_;
136
137   ObserverList<Observer, false> observers_;
138
139   mutable base::Lock lock_;
140
141   DISALLOW_COPY_AND_ASSIGN(ContentSettingsStore);
142 };
143
144 }  // namespace extensions
145
146 #endif  // CHROME_BROWSER_EXTENSIONS_API_CONTENT_SETTINGS_CONTENT_SETTINGS_STORE_H_