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_DECLARATIVE_RULES_REGISTRY_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__
8 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
15 #include "base/callback_forward.h"
16 #include "base/compiler_specific.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "chrome/common/extensions/api/events.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "extensions/common/one_shot_event.h"
32 namespace extensions {
34 class RulesCacheDelegate;
36 // A base class for RulesRegistries that takes care of storing the
37 // RulesRegistry::Rule objects. It contains all the methods that need to run on
38 // the registry thread; methods that need to run on the UI thread are separated
39 // in the RulesCacheDelegate object.
40 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
42 typedef extensions::api::events::Rule Rule;
44 enum Defaults { DEFAULT_PRIORITY = 100 };
45 // After the RulesCacheDelegate object (the part of the registry which runs on
46 // the UI thread) is created, a pointer to it is passed to |*ui_part|.
47 // In tests, |profile| and |ui_part| can be NULL (at the same time). In that
48 // case the storage functionality disabled (no RulesCacheDelegate object
50 RulesRegistry(Profile* profile,
51 const std::string& event_name,
52 content::BrowserThread::ID owner_thread,
53 RulesCacheDelegate* cache_delegate);
55 const OneShotEvent& ready() const {
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
74 // IMPORTANT: This function is atomic. Either all rules that are deemed
75 // relevant are added or none.
77 const std::string& extension_id,
78 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
80 // Unregisters all rules listed in |rule_identifiers| and owned by
81 // |extension_id| from this RulesRegistry.
82 // Some or all IDs in |rule_identifiers| may not be stored in this
83 // RulesRegistry and are ignored.
85 // Returns an empty string if the function is successful or an error
88 // IMPORTANT: This function is atomic. Either all rules that are deemed
89 // relevant are removed or none.
90 std::string RemoveRules(
91 const std::string& extension_id,
92 const std::vector<std::string>& rule_identifiers);
94 // Same as RemoveAllRules but acts on all rules owned by |extension_id|.
95 std::string RemoveAllRules(const std::string& extension_id);
97 // Returns all rules listed in |rule_identifiers| and owned by |extension_id|
98 // registered in this RuleRegistry. Entries in |rule_identifiers| that
99 // are unknown are ignored.
101 // The returned rules are stored in |out|. Ownership is passed to the caller.
103 // Returns an empty string if the function is successful or an error
104 // message otherwise.
105 std::string GetRules(
106 const std::string& extension_id,
107 const std::vector<std::string>& rule_identifiers,
108 std::vector<linked_ptr<RulesRegistry::Rule> >* out);
110 // Same as GetRules but returns all rules owned by |extension_id|.
111 std::string GetAllRules(
112 const std::string& extension_id,
113 std::vector<linked_ptr<RulesRegistry::Rule> >* out);
115 // Called to notify the RulesRegistry that an extension has been unloaded
116 // and all rules of this extension need to be removed.
117 void OnExtensionUnloaded(const std::string& extension_id);
119 // Returns the number of entries in used_rule_identifiers_ for leak detection.
120 // Every ExtensionId counts as one entry, even if it contains no rules.
121 size_t GetNumberOfUsedRuleIdentifiersForTesting() const;
123 // Returns the profile where the rules registry lives.
124 Profile* profile() const { return profile_; }
126 // Returns the ID of the thread on which the rules registry lives.
127 // It is safe to call this function from any thread.
128 content::BrowserThread::ID owner_thread() const { return owner_thread_; }
130 // The name of the event with which rules are registered.
131 const std::string& event_name() const { return event_name_; }
134 virtual ~RulesRegistry();
136 // These functions need to apply the rules to the browser, while the base
137 // class will handle defaulting empty fields before calling *Impl, and will
138 // automatically cache the rules and re-call *Impl on browser startup.
139 virtual std::string AddRulesImpl(
140 const std::string& extension_id,
141 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0;
142 virtual std::string RemoveRulesImpl(
143 const std::string& extension_id,
144 const std::vector<std::string>& rule_identifiers) = 0;
145 virtual std::string RemoveAllRulesImpl(
146 const std::string& extension_id) = 0;
149 friend class base::RefCountedThreadSafe<RulesRegistry>;
150 friend class RulesCacheDelegate;
152 typedef std::string ExtensionId;
153 typedef std::string RuleId;
154 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey;
155 typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> >
157 enum ProcessChangedRulesState {
158 // ProcessChangedRules can never be called, |cache_delegate_| is NULL.
160 // A task to call ProcessChangedRules is scheduled for future execution.
161 SCHEDULED_FOR_PROCESSING,
162 // No task to call ProcessChangedRules is scheduled yet, but it is possible
164 NOT_SCHEDULED_FOR_PROCESSING
167 base::WeakPtr<RulesRegistry> GetWeakPtr() {
168 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
169 return weak_ptr_factory_.GetWeakPtr();
172 // Common processing after extension's rules have changed.
173 void ProcessChangedRules(const std::string& extension_id);
175 // Calls ProcessChangedRules if |process_changed_rules_requested_| ==
176 // NOT_SCHEDULED_FOR_PROCESSING.
177 void MaybeProcessChangedRules(const std::string& extension_id);
179 void MarkReady(base::Time storage_init_time);
181 // Deserialize the rules from the given Value object and add them to the
183 void DeserializeAndAddRules(const std::string& extension_id,
184 scoped_ptr<base::Value> rules);
186 // The profile to which this rules registry belongs.
189 // The ID of the thread on which the rules registry lives.
190 const content::BrowserThread::ID owner_thread_;
192 // The name of the event with which rules are registered.
193 const std::string event_name_;
195 RulesDictionary rules_;
197 // Signaled when we have finished reading from storage for all extensions that
198 // are loaded on startup.
201 // The factory needs to be declared before |cache_delegate_|, so that it can
202 // produce a pointer as a construction argument for |cache_delegate_|.
203 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_;
205 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is
206 // NULL, then the storage functionality is disabled (this is used in tests).
207 // This registry cannot own |cache_delegate_| because during the time after
208 // rules registry service shuts down on UI thread, and the registry is
209 // destroyed on its thread, the use of the |cache_delegate_| would not be
210 // safe. The registry only ever associates with one RulesCacheDelegate
212 base::WeakPtr<RulesCacheDelegate> cache_delegate_;
214 ProcessChangedRulesState process_changed_rules_requested_;
216 // Returns whether any existing rule is registered with identifier |rule_id|
217 // for extension |extension_id|.
218 bool IsUniqueId(const std::string& extension_id,
219 const std::string& rule_id) const;
221 // Creates an ID that is unique within the scope of|extension_id|.
222 std::string GenerateUniqueId(const std::string& extension_id);
224 // Verifies that all |rules| have unique IDs or initializes them with
225 // unique IDs if they don't have one. In case of duplicate IDs, this function
226 // returns a non-empty error message.
227 std::string CheckAndFillInOptionalRules(
228 const std::string& extension_id,
229 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
231 // Initializes the priority fields in case they have not been set.
232 void FillInOptionalPriorities(
233 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
235 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|.
236 void RemoveUsedRuleIdentifiers(const std::string& extension_id,
237 const std::vector<std::string>& identifiers);
239 // Same as RemoveUsedRuleIdentifiers but operates on all rules of
241 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id);
243 typedef std::string RuleIdentifier;
244 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap;
245 RuleIdentifiersMap used_rule_identifiers_;
246 int last_generated_rule_identifier_id_;
248 DISALLOW_COPY_AND_ASSIGN(RulesRegistry);
251 } // namespace extensions
253 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__