Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / declarative / rules_registry.h
blob34ad7acfc8191ed0b54d42d50238965c2bc0a7fd
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 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> {
41 public:
42 typedef extensions::core_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, |browser_context| and |ui_part| can be NULL (at the same time).
48 // In that case the storage functionality disabled (no RulesCacheDelegate
49 // object created).
50 RulesRegistry(content::BrowserContext* browser_context,
51 const std::string& event_name,
52 content::BrowserThread::ID owner_thread,
53 RulesCacheDelegate* cache_delegate,
54 int id);
56 const OneShotEvent& ready() const {
57 return ready_;
60 // RulesRegistry implementation:
62 // Registers |rules|, owned by |extension_id| to this RulesRegistry.
63 // If a concrete RuleRegistry does not support some of the rules,
64 // it may ignore them.
66 // |rules| is a list of Rule instances following the definition of the
67 // declarative extension APIs. It is guaranteed that each rule in |rules| has
68 // a unique name within the scope of |extension_id| that has not been
69 // registered before, unless it has been removed again.
70 // The ownership of rules remains with the caller.
72 // Returns an empty string if the function is successful or an error
73 // message otherwise.
75 // IMPORTANT: This function is atomic. Either all rules that are deemed
76 // relevant are added or none.
77 std::string AddRules(
78 const std::string& extension_id,
79 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
81 // Unregisters all rules listed in |rule_identifiers| and owned by
82 // |extension_id| from this RulesRegistry.
83 // Some or all IDs in |rule_identifiers| may not be stored in this
84 // RulesRegistry and are ignored.
86 // Returns an empty string if the function is successful or an error
87 // message otherwise.
89 // IMPORTANT: This function is atomic. Either all rules that are deemed
90 // relevant are removed or none.
91 std::string RemoveRules(
92 const std::string& extension_id,
93 const std::vector<std::string>& rule_identifiers);
95 // Same as RemoveAllRules but acts on all rules owned by |extension_id|.
96 std::string RemoveAllRules(const std::string& extension_id);
98 // Returns all rules listed in |rule_identifiers| and owned by |extension_id|
99 // registered in this RuleRegistry. Entries in |rule_identifiers| that
100 // are unknown are ignored.
102 // The returned rules are stored in |out|. Ownership is passed to the caller.
103 void GetRules(const std::string& extension_id,
104 const std::vector<std::string>& rule_identifiers,
105 std::vector<linked_ptr<RulesRegistry::Rule> >* out);
107 // Same as GetRules but returns all rules owned by |extension_id|.
108 void GetAllRules(const std::string& extension_id,
109 std::vector<linked_ptr<RulesRegistry::Rule> >* out);
111 // Called to notify the RulesRegistry that the extension availability has
112 // changed, so that the registry can update which rules are active.
113 void OnExtensionUnloaded(const std::string& extension_id);
114 void OnExtensionUninstalled(const std::string& extension_id);
115 void OnExtensionLoaded(const std::string& extension_id);
117 // Returns the number of entries in used_rule_identifiers_ for leak detection.
118 // Every ExtensionId counts as one entry, even if it contains no rules.
119 size_t GetNumberOfUsedRuleIdentifiersForTesting() const;
121 // Returns the RulesCacheDelegate. This is used for testing.
122 RulesCacheDelegate* rules_cache_delegate_for_testing() const {
123 return cache_delegate_.get();
126 // Returns the context where the rules registry lives.
127 content::BrowserContext* browser_context() const { return browser_context_; }
129 // Returns the ID of the thread on which the rules registry lives.
130 // It is safe to call this function from any thread.
131 content::BrowserThread::ID owner_thread() const { return owner_thread_; }
133 // The name of the event with which rules are registered.
134 const std::string& event_name() const { return event_name_; }
136 // The unique identifier for this RulesRegistry object.
137 int id() const { return id_; }
139 protected:
140 virtual ~RulesRegistry();
142 // The precondition for calling this method is that all rules have unique IDs.
143 // AddRules establishes this precondition and calls into this method.
144 // Stored rules already meet this precondition and so they avoid calling
145 // CheckAndFillInOptionalRules for improved performance.
147 // Returns an empty string if the function is successful or an error
148 // message otherwise.
149 std::string AddRulesNoFill(
150 const std::string& extension_id,
151 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
153 // These functions need to apply the rules to the browser, while the base
154 // class will handle defaulting empty fields before calling *Impl, and will
155 // automatically cache the rules and re-call *Impl on browser startup.
156 virtual std::string AddRulesImpl(
157 const std::string& extension_id,
158 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0;
159 virtual std::string RemoveRulesImpl(
160 const std::string& extension_id,
161 const std::vector<std::string>& rule_identifiers) = 0;
162 virtual std::string RemoveAllRulesImpl(
163 const std::string& extension_id) = 0;
165 private:
166 friend class base::RefCountedThreadSafe<RulesRegistry>;
167 friend class RulesCacheDelegate;
169 typedef std::string ExtensionId;
170 typedef std::string RuleId;
171 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey;
172 typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> >
173 RulesDictionary;
174 enum ProcessChangedRulesState {
175 // ProcessChangedRules can never be called, |cache_delegate_| is NULL.
176 NEVER_PROCESS,
177 // A task to call ProcessChangedRules is scheduled for future execution.
178 SCHEDULED_FOR_PROCESSING,
179 // No task to call ProcessChangedRules is scheduled yet, but it is possible
180 // to schedule one.
181 NOT_SCHEDULED_FOR_PROCESSING
183 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap;
185 base::WeakPtr<RulesRegistry> GetWeakPtr() {
186 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
187 return weak_ptr_factory_.GetWeakPtr();
190 // Common processing after extension's rules have changed.
191 void ProcessChangedRules(const std::string& extension_id);
193 // Calls ProcessChangedRules if
194 // |process_changed_rules_requested_(extension_id)| ==
195 // NOT_SCHEDULED_FOR_PROCESSING.
196 void MaybeProcessChangedRules(const std::string& extension_id);
198 // This method implements the functionality of RemoveAllRules, except for not
199 // calling MaybeProcessChangedRules. That way updating the rules store and
200 // extension prefs is avoided. This method is called when an extension is
201 // uninstalled, that way there is no clash with the preferences being wiped.
202 std::string RemoveAllRulesNoStoreUpdate(const std::string& extension_id);
204 void MarkReady(base::Time storage_init_time);
206 // Deserialize the rules from the given Value object and add them to the
207 // RulesRegistry.
208 void DeserializeAndAddRules(const std::string& extension_id,
209 scoped_ptr<base::Value> rules);
211 // The context to which this rules registry belongs.
212 content::BrowserContext* browser_context_;
214 // The ID of the thread on which the rules registry lives.
215 const content::BrowserThread::ID owner_thread_;
217 // The name of the event with which rules are registered.
218 const std::string event_name_;
220 // The key that identifies the context in which these rules apply.
221 int id_;
223 RulesDictionary rules_;
225 // Signaled when we have finished reading from storage for all extensions that
226 // are loaded on startup.
227 OneShotEvent ready_;
229 ProcessStateMap process_changed_rules_requested_;
231 // Returns whether any existing rule is registered with identifier |rule_id|
232 // for extension |extension_id|.
233 bool IsUniqueId(const std::string& extension_id,
234 const std::string& rule_id) const;
236 // Creates an ID that is unique within the scope of|extension_id|.
237 std::string GenerateUniqueId(const std::string& extension_id);
239 // Verifies that all |rules| have unique IDs or initializes them with
240 // unique IDs if they don't have one. In case of duplicate IDs, this function
241 // returns a non-empty error message.
242 std::string CheckAndFillInOptionalRules(
243 const std::string& extension_id,
244 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
246 // Initializes the priority fields in case they have not been set.
247 void FillInOptionalPriorities(
248 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules);
250 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|.
251 void RemoveUsedRuleIdentifiers(const std::string& extension_id,
252 const std::vector<std::string>& identifiers);
254 // Same as RemoveUsedRuleIdentifiers but operates on all rules of
255 // |extension_id|.
256 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id);
258 typedef std::string RuleIdentifier;
259 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap;
260 RuleIdentifiersMap used_rule_identifiers_;
261 int last_generated_rule_identifier_id_;
263 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is
264 // NULL, then the storage functionality is disabled (this is used in tests).
265 // This registry cannot own |cache_delegate_| because during the time after
266 // rules registry service shuts down on UI thread, and the registry is
267 // destroyed on its thread, the use of the |cache_delegate_| would not be
268 // safe. The registry only ever associates with one RulesCacheDelegate
269 // instance.
270 base::WeakPtr<RulesCacheDelegate> cache_delegate_;
272 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_;
274 DISALLOW_COPY_AND_ASSIGN(RulesRegistry);
277 } // namespace extensions
279 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__