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__
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"
32 namespace extensions
{
35 class RulesCacheDelegate
;
37 // A base class for RulesRegistries that takes care of storing the
38 // core_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
> {
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
49 RulesRegistry(content::BrowserContext
* browser_context
,
50 const std::string
& event_name
,
51 content::BrowserThread::ID owner_thread
,
52 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
<core_api::events::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.
102 void GetRules(const std::string
& extension_id
,
103 const std::vector
<std::string
>& rule_identifiers
,
104 std::vector
<linked_ptr
<core_api::events::Rule
>>* out
);
106 // Same as GetRules but returns all rules owned by |extension_id|.
107 void GetAllRules(const std::string
& extension_id
,
108 std::vector
<linked_ptr
<core_api::events::Rule
>>* out
);
110 // Called to notify the RulesRegistry that the extension availability has
111 // changed, so that the registry can update which rules are active.
112 void OnExtensionUnloaded(const Extension
* extension
);
113 void OnExtensionUninstalled(const Extension
* extension
);
114 void OnExtensionLoaded(const Extension
* extension
);
116 // Returns the number of entries in used_rule_identifiers_ for leak detection.
117 // Every ExtensionId counts as one entry, even if it contains no rules.
118 size_t GetNumberOfUsedRuleIdentifiersForTesting() const;
120 // Returns the RulesCacheDelegate. This is used for testing.
121 RulesCacheDelegate
* rules_cache_delegate_for_testing() const {
122 return cache_delegate_
.get();
125 // Returns the context where the rules registry lives.
126 content::BrowserContext
* browser_context() const { return browser_context_
; }
128 // Returns the ID of the thread on which the rules registry lives.
129 // It is safe to call this function from any thread.
130 content::BrowserThread::ID
owner_thread() const { return owner_thread_
; }
132 // The name of the event with which rules are registered.
133 const std::string
& event_name() const { return event_name_
; }
135 // The unique identifier for this RulesRegistry object.
136 int id() const { return id_
; }
139 virtual ~RulesRegistry();
141 // These functions need to apply the rules to the browser, while the base
142 // class will handle defaulting empty fields before calling *Impl, and will
143 // automatically cache the rules and re-call *Impl on browser startup.
144 virtual std::string
AddRulesImpl(
145 const std::string
& extension_id
,
146 const std::vector
<linked_ptr
<core_api::events::Rule
>>& rules
) = 0;
147 virtual std::string
RemoveRulesImpl(
148 const std::string
& extension_id
,
149 const std::vector
<std::string
>& rule_identifiers
) = 0;
150 virtual std::string
RemoveAllRulesImpl(
151 const std::string
& extension_id
) = 0;
154 friend class base::RefCountedThreadSafe
<RulesRegistry
>;
155 friend class RulesCacheDelegate
;
157 typedef std::string ExtensionId
;
158 typedef std::string RuleId
;
159 typedef std::pair
<ExtensionId
, RuleId
> RulesDictionaryKey
;
160 typedef std::map
<RulesDictionaryKey
, linked_ptr
<core_api::events::Rule
>>
162 enum ProcessChangedRulesState
{
163 // ProcessChangedRules can never be called, |cache_delegate_| is NULL.
165 // A task to call ProcessChangedRules is scheduled for future execution.
166 SCHEDULED_FOR_PROCESSING
,
167 // No task to call ProcessChangedRules is scheduled yet, but it is possible
169 NOT_SCHEDULED_FOR_PROCESSING
171 typedef std::map
<ExtensionId
, ProcessChangedRulesState
> ProcessStateMap
;
173 base::WeakPtr
<RulesRegistry
> GetWeakPtr() {
174 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
175 return weak_ptr_factory_
.GetWeakPtr();
178 // Internal implementation of the AddRules interface which adds
179 // |from_manifest| which is true when the source is the manifest.
180 std::string
AddRulesInternal(
181 const std::string
& extension_id
,
182 const std::vector
<linked_ptr
<core_api::events::Rule
>>& rules
,
183 RulesDictionary
* out
);
185 // The precondition for calling this method is that all rules have unique IDs.
186 // AddRules establishes this precondition and calls into this method.
187 // Stored rules already meet this precondition and so they avoid calling
188 // CheckAndFillInOptionalRules for improved performance.
190 // Returns an empty string if the function is successful or an error
191 // message otherwise.
192 std::string
AddRulesNoFill(
193 const std::string
& extension_id
,
194 const std::vector
<linked_ptr
<core_api::events::Rule
>>& rules
,
195 RulesDictionary
* out
);
197 // Same as GetRules but returns all rules owned by |extension_id| for a given
198 // |rules| dictionary.
199 void GetRules(const std::string
& extension_id
,
200 RulesDictionary
& rules
,
201 std::vector
<linked_ptr
<core_api::events::Rule
>>* out
);
203 // Common processing after extension's rules have changed.
204 void ProcessChangedRules(const std::string
& extension_id
);
206 // Calls ProcessChangedRules if
207 // |process_changed_rules_requested_(extension_id)| ==
208 // NOT_SCHEDULED_FOR_PROCESSING.
209 void MaybeProcessChangedRules(const std::string
& extension_id
);
211 // This method implements the functionality of RemoveAllRules, except for not
212 // calling MaybeProcessChangedRules. That way updating the rules store and
213 // extension prefs is avoided. This method is called when an extension is
214 // uninstalled, that way there is no clash with the preferences being wiped.
215 // Set |remove_manifest_rules| to true if |manifest_rules_| should be cleared
216 // along with |rules_|.
217 std::string
RemoveAllRulesNoStoreUpdate(const std::string
& extension_id
,
218 bool remove_manifest_rules
);
220 void MarkReady(base::Time storage_init_time
);
222 // Deserialize the rules from the given Value object and add them to the
224 void DeserializeAndAddRules(const std::string
& extension_id
,
225 scoped_ptr
<base::Value
> rules
);
227 // Reports an internal error with the specified params to the extensions
229 void ReportInternalError(const std::string
& extension_id
,
230 const std::string
& error
);
232 // The context to which this rules registry belongs.
233 content::BrowserContext
* browser_context_
;
235 // The ID of the thread on which the rules registry lives.
236 const content::BrowserThread::ID owner_thread_
;
238 // The name of the event with which rules are registered.
239 const std::string event_name_
;
241 // The key that identifies the context in which these rules apply.
244 RulesDictionary rules_
;
246 RulesDictionary manifest_rules_
;
248 // Signaled when we have finished reading from storage for all extensions that
249 // are loaded on startup.
252 ProcessStateMap process_changed_rules_requested_
;
254 // Returns whether any existing rule is registered with identifier |rule_id|
255 // for extension |extension_id|.
256 bool IsUniqueId(const std::string
& extension_id
,
257 const std::string
& rule_id
) const;
259 // Creates an ID that is unique within the scope of|extension_id|.
260 std::string
GenerateUniqueId(const std::string
& extension_id
);
262 // Verifies that all |rules| have unique IDs or initializes them with
263 // unique IDs if they don't have one. In case of duplicate IDs, this function
264 // returns a non-empty error message.
265 std::string
CheckAndFillInOptionalRules(
266 const std::string
& extension_id
,
267 const std::vector
<linked_ptr
<core_api::events::Rule
>>& rules
);
269 // Initializes the priority fields in case they have not been set.
270 void FillInOptionalPriorities(
271 const std::vector
<linked_ptr
<core_api::events::Rule
>>& rules
);
273 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|.
274 void RemoveUsedRuleIdentifiers(const std::string
& extension_id
,
275 const std::vector
<std::string
>& identifiers
);
277 // Same as RemoveUsedRuleIdentifiers but operates on all rules of
279 void RemoveAllUsedRuleIdentifiers(const std::string
& extension_id
);
281 typedef std::string RuleIdentifier
;
282 typedef std::map
<ExtensionId
, std::set
<RuleIdentifier
> > RuleIdentifiersMap
;
283 RuleIdentifiersMap used_rule_identifiers_
;
284 int last_generated_rule_identifier_id_
;
286 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is
287 // NULL, then the storage functionality is disabled (this is used in tests).
288 // This registry cannot own |cache_delegate_| because during the time after
289 // rules registry service shuts down on UI thread, and the registry is
290 // destroyed on its thread, the use of the |cache_delegate_| would not be
291 // safe. The registry only ever associates with one RulesCacheDelegate
293 base::WeakPtr
<RulesCacheDelegate
> cache_delegate_
;
295 base::WeakPtrFactory
<RulesRegistry
> weak_ptr_factory_
;
297 DISALLOW_COPY_AND_ASSIGN(RulesRegistry
);
300 } // namespace extensions
302 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__