Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / declarative / rules_registry.h
blobe94d143135bb22aca5fb3b47943adea31872d061
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 EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__
6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback_forward.h"
14 #include "base/compiler_specific.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "extensions/common/api/events.h"
22 #include "extensions/common/one_shot_event.h"
24 namespace content {
25 class BrowserContext;
28 namespace base {
29 class Value;
30 } // namespace base
32 namespace extensions {
34 class Extension;
35 class RulesCacheDelegate;
37 // A base class for RulesRegistries that takes care of storing the
38 // api::events::Rule objects. It contains all the methods that need to run
39 // on the registry thread; methods that need to run on the UI thread are
40 // separated in the RulesCacheDelegate object.
41 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
42 public:
43 enum Defaults { DEFAULT_PRIORITY = 100 };
44 // After the RulesCacheDelegate object (the part of the registry which runs on
45 // the UI thread) is created, a pointer to it is passed to |*ui_part|.
46 // In tests, |browser_context| and |ui_part| can be NULL (at the same time).
47 // In that case the storage functionality disabled (no RulesCacheDelegate
48 // object created).
49 RulesRegistry(content::BrowserContext* browser_context,
50 const std::string& event_name,
51 content::BrowserThread::ID owner_thread,
52 RulesCacheDelegate* cache_delegate,
53 int id);
55 const OneShotEvent& ready() const {
56 return ready_;
59 // RulesRegistry implementation:
61 // Registers |rules|, owned by |extension_id| to this RulesRegistry.
62 // If a concrete RuleRegistry does not support some of the rules,
63 // it may ignore them.
65 // |rules| is a list of Rule instances following the definition of the
66 // declarative extension APIs. It is guaranteed that each rule in |rules| has
67 // a unique name within the scope of |extension_id| that has not been
68 // registered before, unless it has been removed again.
69 // The ownership of rules remains with the caller.
71 // Returns an empty string if the function is successful or an error
72 // message otherwise.
74 // IMPORTANT: This function is atomic. Either all rules that are deemed
75 // relevant are added or none.
76 std::string AddRules(const std::string& extension_id,
77 const std::vector<linked_ptr<api::events::Rule>>& rules);
79 // Unregisters all rules listed in |rule_identifiers| and owned by
80 // |extension_id| from this RulesRegistry.
81 // Some or all IDs in |rule_identifiers| may not be stored in this
82 // RulesRegistry and are ignored.
84 // Returns an empty string if the function is successful or an error
85 // message otherwise.
87 // IMPORTANT: This function is atomic. Either all rules that are deemed
88 // relevant are removed or none.
89 std::string RemoveRules(
90 const std::string& extension_id,
91 const std::vector<std::string>& rule_identifiers);
93 // Same as RemoveAllRules but acts on all rules owned by |extension_id|.
94 std::string RemoveAllRules(const std::string& extension_id);
96 // Returns all rules listed in |rule_identifiers| and owned by |extension_id|
97 // registered in this RuleRegistry. Entries in |rule_identifiers| that
98 // are unknown are ignored.
100 // The returned rules are stored in |out|. Ownership is passed to the caller.
101 void GetRules(const std::string& extension_id,
102 const std::vector<std::string>& rule_identifiers,
103 std::vector<linked_ptr<api::events::Rule>>* out);
105 // Same as GetRules but returns all rules owned by |extension_id|.
106 void GetAllRules(const std::string& extension_id,
107 std::vector<linked_ptr<api::events::Rule>>* out);
109 // Called to notify the RulesRegistry that the extension availability has
110 // changed, so that the registry can update which rules are active.
111 void OnExtensionUnloaded(const Extension* extension);
112 void OnExtensionUninstalled(const Extension* extension);
113 void OnExtensionLoaded(const Extension* extension);
115 // Returns the number of entries in used_rule_identifiers_ for leak detection.
116 // Every ExtensionId counts as one entry, even if it contains no rules.
117 size_t GetNumberOfUsedRuleIdentifiersForTesting() const;
119 // Returns the RulesCacheDelegate. This is used for testing.
120 RulesCacheDelegate* rules_cache_delegate_for_testing() const {
121 return cache_delegate_.get();
124 // Returns the context where the rules registry lives.
125 content::BrowserContext* browser_context() const { return browser_context_; }
127 // Returns the ID of the thread on which the rules registry lives.
128 // It is safe to call this function from any thread.
129 content::BrowserThread::ID owner_thread() const { return owner_thread_; }
131 // The name of the event with which rules are registered.
132 const std::string& event_name() const { return event_name_; }
134 // The unique identifier for this RulesRegistry object.
135 int id() const { return id_; }
137 protected:
138 virtual ~RulesRegistry();
140 // These functions need to apply the rules to the browser, while the base
141 // class will handle defaulting empty fields before calling *Impl, and will
142 // automatically cache the rules and re-call *Impl on browser startup.
143 virtual std::string AddRulesImpl(
144 const std::string& extension_id,
145 const std::vector<linked_ptr<api::events::Rule>>& rules) = 0;
146 virtual std::string RemoveRulesImpl(
147 const std::string& extension_id,
148 const std::vector<std::string>& rule_identifiers) = 0;
149 virtual std::string RemoveAllRulesImpl(
150 const std::string& extension_id) = 0;
152 private:
153 friend class base::RefCountedThreadSafe<RulesRegistry>;
154 friend class RulesCacheDelegate;
156 typedef std::string ExtensionId;
157 typedef std::string RuleId;
158 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey;
159 typedef std::map<RulesDictionaryKey, linked_ptr<api::events::Rule>>
160 RulesDictionary;
161 enum ProcessChangedRulesState {
162 // ProcessChangedRules can never be called, |cache_delegate_| is NULL.
163 NEVER_PROCESS,
164 // A task to call ProcessChangedRules is scheduled for future execution.
165 SCHEDULED_FOR_PROCESSING,
166 // No task to call ProcessChangedRules is scheduled yet, but it is possible
167 // to schedule one.
168 NOT_SCHEDULED_FOR_PROCESSING
170 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap;
172 base::WeakPtr<RulesRegistry> GetWeakPtr() {
173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
174 return weak_ptr_factory_.GetWeakPtr();
177 // Internal implementation of the AddRules interface which adds
178 // |from_manifest| which is true when the source is the manifest.
179 std::string AddRulesInternal(
180 const std::string& extension_id,
181 const std::vector<linked_ptr<api::events::Rule>>& rules,
182 RulesDictionary* out);
184 // The precondition for calling this method is that all rules have unique IDs.
185 // AddRules establishes this precondition and calls into this method.
186 // Stored rules already meet this precondition and so they avoid calling
187 // CheckAndFillInOptionalRules for improved performance.
189 // Returns an empty string if the function is successful or an error
190 // message otherwise.
191 std::string AddRulesNoFill(
192 const std::string& extension_id,
193 const std::vector<linked_ptr<api::events::Rule>>& rules,
194 RulesDictionary* out);
196 // Same as GetRules but returns all rules owned by |extension_id| for a given
197 // |rules| dictionary.
198 void GetRules(const std::string& extension_id,
199 RulesDictionary& rules,
200 std::vector<linked_ptr<api::events::Rule>>* out);
202 // Common processing after extension's rules have changed.
203 void ProcessChangedRules(const std::string& extension_id);
205 // Calls ProcessChangedRules if
206 // |process_changed_rules_requested_(extension_id)| ==
207 // NOT_SCHEDULED_FOR_PROCESSING.
208 void MaybeProcessChangedRules(const std::string& extension_id);
210 // This method implements the functionality of RemoveAllRules, except for not
211 // calling MaybeProcessChangedRules. That way updating the rules store and
212 // extension prefs is avoided. This method is called when an extension is
213 // uninstalled, that way there is no clash with the preferences being wiped.
214 // Set |remove_manifest_rules| to true if |manifest_rules_| should be cleared
215 // along with |rules_|.
216 std::string RemoveAllRulesNoStoreUpdate(const std::string& extension_id,
217 bool remove_manifest_rules);
219 void MarkReady(base::Time storage_init_time);
221 // Deserialize the rules from the given Value object and add them to the
222 // RulesRegistry.
223 void DeserializeAndAddRules(const std::string& extension_id,
224 scoped_ptr<base::Value> rules);
226 // Reports an internal error with the specified params to the extensions
227 // client.
228 void ReportInternalError(const std::string& extension_id,
229 const std::string& error);
231 // The context to which this rules registry belongs.
232 content::BrowserContext* browser_context_;
234 // The ID of the thread on which the rules registry lives.
235 const content::BrowserThread::ID owner_thread_;
237 // The name of the event with which rules are registered.
238 const std::string event_name_;
240 // The key that identifies the context in which these rules apply.
241 int id_;
243 RulesDictionary rules_;
245 RulesDictionary manifest_rules_;
247 // Signaled when we have finished reading from storage for all extensions that
248 // are loaded on startup.
249 OneShotEvent ready_;
251 ProcessStateMap process_changed_rules_requested_;
253 // Returns whether any existing rule is registered with identifier |rule_id|
254 // for extension |extension_id|.
255 bool IsUniqueId(const std::string& extension_id,
256 const std::string& rule_id) const;
258 // Creates an ID that is unique within the scope of|extension_id|.
259 std::string GenerateUniqueId(const std::string& extension_id);
261 // Verifies that all |rules| have unique IDs or initializes them with
262 // unique IDs if they don't have one. In case of duplicate IDs, this function
263 // returns a non-empty error message.
264 std::string CheckAndFillInOptionalRules(
265 const std::string& extension_id,
266 const std::vector<linked_ptr<api::events::Rule>>& rules);
268 // Initializes the priority fields in case they have not been set.
269 void FillInOptionalPriorities(
270 const std::vector<linked_ptr<api::events::Rule>>& rules);
272 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|.
273 void RemoveUsedRuleIdentifiers(const std::string& extension_id,
274 const std::vector<std::string>& identifiers);
276 // Same as RemoveUsedRuleIdentifiers but operates on all rules of
277 // |extension_id|.
278 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id);
280 typedef std::string RuleIdentifier;
281 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap;
282 RuleIdentifiersMap used_rule_identifiers_;
283 int last_generated_rule_identifier_id_;
285 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is
286 // NULL, then the storage functionality is disabled (this is used in tests).
287 // This registry cannot own |cache_delegate_| because during the time after
288 // rules registry service shuts down on UI thread, and the registry is
289 // destroyed on its thread, the use of the |cache_delegate_| would not be
290 // safe. The registry only ever associates with one RulesCacheDelegate
291 // instance.
292 base::WeakPtr<RulesCacheDelegate> cache_delegate_;
294 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_;
296 DISALLOW_COPY_AND_ASSIGN(RulesRegistry);
299 } // namespace extensions
301 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__