| OLD | NEW |
| (Empty) |
| 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_DECLARATIVE_RULES_REGISTRY_H__ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 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 "chrome/common/extensions/api/events.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/notification_observer.h" | |
| 21 #include "content/public/browser/notification_registrar.h" | |
| 22 #include "extensions/common/one_shot_event.h" | |
| 23 | |
| 24 namespace content { | |
| 25 class BrowserContext; | |
| 26 } | |
| 27 | |
| 28 namespace base { | |
| 29 class Value; | |
| 30 } // namespace base | |
| 31 | |
| 32 namespace extensions { | |
| 33 | |
| 34 class RulesCacheDelegate; | |
| 35 | |
| 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::api::events::Rule Rule; | |
| 43 struct WebViewKey { | |
| 44 int embedder_process_id; | |
| 45 int webview_instance_id; | |
| 46 WebViewKey(int embedder_process_id, int webview_instance_id) | |
| 47 : embedder_process_id(embedder_process_id), | |
| 48 webview_instance_id(webview_instance_id) {} | |
| 49 bool operator<(const WebViewKey& other) const { | |
| 50 return embedder_process_id < other.embedder_process_id || | |
| 51 ((embedder_process_id == other.embedder_process_id) && | |
| 52 (webview_instance_id < other.webview_instance_id)); | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 enum Defaults { DEFAULT_PRIORITY = 100 }; | |
| 57 // After the RulesCacheDelegate object (the part of the registry which runs on | |
| 58 // the UI thread) is created, a pointer to it is passed to |*ui_part|. | |
| 59 // In tests, |browser_context| and |ui_part| can be NULL (at the same time). | |
| 60 // In that case the storage functionality disabled (no RulesCacheDelegate | |
| 61 // object created). | |
| 62 RulesRegistry(content::BrowserContext* browser_context, | |
| 63 const std::string& event_name, | |
| 64 content::BrowserThread::ID owner_thread, | |
| 65 RulesCacheDelegate* cache_delegate, | |
| 66 const WebViewKey& webview_key); | |
| 67 | |
| 68 const OneShotEvent& ready() const { | |
| 69 return ready_; | |
| 70 } | |
| 71 | |
| 72 // RulesRegistry implementation: | |
| 73 | |
| 74 // Registers |rules|, owned by |extension_id| to this RulesRegistry. | |
| 75 // If a concrete RuleRegistry does not support some of the rules, | |
| 76 // it may ignore them. | |
| 77 // | |
| 78 // |rules| is a list of Rule instances following the definition of the | |
| 79 // declarative extension APIs. It is guaranteed that each rule in |rules| has | |
| 80 // a unique name within the scope of |extension_id| that has not been | |
| 81 // registered before, unless it has been removed again. | |
| 82 // The ownership of rules remains with the caller. | |
| 83 // | |
| 84 // Returns an empty string if the function is successful or an error | |
| 85 // message otherwise. | |
| 86 // | |
| 87 // IMPORTANT: This function is atomic. Either all rules that are deemed | |
| 88 // relevant are added or none. | |
| 89 std::string AddRules( | |
| 90 const std::string& extension_id, | |
| 91 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
| 92 | |
| 93 // Unregisters all rules listed in |rule_identifiers| and owned by | |
| 94 // |extension_id| from this RulesRegistry. | |
| 95 // Some or all IDs in |rule_identifiers| may not be stored in this | |
| 96 // RulesRegistry and are ignored. | |
| 97 // | |
| 98 // Returns an empty string if the function is successful or an error | |
| 99 // message otherwise. | |
| 100 // | |
| 101 // IMPORTANT: This function is atomic. Either all rules that are deemed | |
| 102 // relevant are removed or none. | |
| 103 std::string RemoveRules( | |
| 104 const std::string& extension_id, | |
| 105 const std::vector<std::string>& rule_identifiers); | |
| 106 | |
| 107 // Same as RemoveAllRules but acts on all rules owned by |extension_id|. | |
| 108 std::string RemoveAllRules(const std::string& extension_id); | |
| 109 | |
| 110 // Returns all rules listed in |rule_identifiers| and owned by |extension_id| | |
| 111 // registered in this RuleRegistry. Entries in |rule_identifiers| that | |
| 112 // are unknown are ignored. | |
| 113 // | |
| 114 // The returned rules are stored in |out|. Ownership is passed to the caller. | |
| 115 void GetRules(const std::string& extension_id, | |
| 116 const std::vector<std::string>& rule_identifiers, | |
| 117 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | |
| 118 | |
| 119 // Same as GetRules but returns all rules owned by |extension_id|. | |
| 120 void GetAllRules(const std::string& extension_id, | |
| 121 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | |
| 122 | |
| 123 // Called to notify the RulesRegistry that the extension availability has | |
| 124 // changed, so that the registry can update which rules are active. | |
| 125 void OnExtensionUnloaded(const std::string& extension_id); | |
| 126 void OnExtensionUninstalled(const std::string& extension_id); | |
| 127 void OnExtensionLoaded(const std::string& extension_id); | |
| 128 | |
| 129 // Returns the number of entries in used_rule_identifiers_ for leak detection. | |
| 130 // Every ExtensionId counts as one entry, even if it contains no rules. | |
| 131 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; | |
| 132 | |
| 133 // Returns the RulesCacheDelegate. This is used for testing. | |
| 134 RulesCacheDelegate* rules_cache_delegate_for_testing() const { | |
| 135 return cache_delegate_.get(); | |
| 136 } | |
| 137 | |
| 138 // Returns the context where the rules registry lives. | |
| 139 content::BrowserContext* browser_context() { return browser_context_; } | |
| 140 | |
| 141 // Returns the ID of the thread on which the rules registry lives. | |
| 142 // It is safe to call this function from any thread. | |
| 143 content::BrowserThread::ID owner_thread() const { return owner_thread_; } | |
| 144 | |
| 145 // The name of the event with which rules are registered. | |
| 146 const std::string& event_name() const { return event_name_; } | |
| 147 | |
| 148 // The key that identifies the webview (or tabs) in which these rules apply. | |
| 149 // If the rules apply to the main browser, then this returns the tuple (0, 0). | |
| 150 const WebViewKey& webview_key() const { | |
| 151 return webview_key_; | |
| 152 } | |
| 153 | |
| 154 protected: | |
| 155 virtual ~RulesRegistry(); | |
| 156 | |
| 157 // The precondition for calling this method is that all rules have unique IDs. | |
| 158 // AddRules establishes this precondition and calls into this method. | |
| 159 // Stored rules already meet this precondition and so they avoid calling | |
| 160 // CheckAndFillInOptionalRules for improved performance. | |
| 161 // | |
| 162 // Returns an empty string if the function is successful or an error | |
| 163 // message otherwise. | |
| 164 std::string AddRulesNoFill( | |
| 165 const std::string& extension_id, | |
| 166 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
| 167 | |
| 168 // These functions need to apply the rules to the browser, while the base | |
| 169 // class will handle defaulting empty fields before calling *Impl, and will | |
| 170 // automatically cache the rules and re-call *Impl on browser startup. | |
| 171 virtual std::string AddRulesImpl( | |
| 172 const std::string& extension_id, | |
| 173 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; | |
| 174 virtual std::string RemoveRulesImpl( | |
| 175 const std::string& extension_id, | |
| 176 const std::vector<std::string>& rule_identifiers) = 0; | |
| 177 virtual std::string RemoveAllRulesImpl( | |
| 178 const std::string& extension_id) = 0; | |
| 179 | |
| 180 private: | |
| 181 friend class base::RefCountedThreadSafe<RulesRegistry>; | |
| 182 friend class RulesCacheDelegate; | |
| 183 | |
| 184 typedef std::string ExtensionId; | |
| 185 typedef std::string RuleId; | |
| 186 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey; | |
| 187 typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> > | |
| 188 RulesDictionary; | |
| 189 enum ProcessChangedRulesState { | |
| 190 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. | |
| 191 NEVER_PROCESS, | |
| 192 // A task to call ProcessChangedRules is scheduled for future execution. | |
| 193 SCHEDULED_FOR_PROCESSING, | |
| 194 // No task to call ProcessChangedRules is scheduled yet, but it is possible | |
| 195 // to schedule one. | |
| 196 NOT_SCHEDULED_FOR_PROCESSING | |
| 197 }; | |
| 198 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap; | |
| 199 | |
| 200 base::WeakPtr<RulesRegistry> GetWeakPtr() { | |
| 201 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 202 return weak_ptr_factory_.GetWeakPtr(); | |
| 203 } | |
| 204 | |
| 205 // Common processing after extension's rules have changed. | |
| 206 void ProcessChangedRules(const std::string& extension_id); | |
| 207 | |
| 208 // Calls ProcessChangedRules if | |
| 209 // |process_changed_rules_requested_(extension_id)| == | |
| 210 // NOT_SCHEDULED_FOR_PROCESSING. | |
| 211 void MaybeProcessChangedRules(const std::string& extension_id); | |
| 212 | |
| 213 // This method implements the functionality of RemoveAllRules, except for not | |
| 214 // calling MaybeProcessChangedRules. That way updating the rules store and | |
| 215 // extension prefs is avoided. This method is called when an extension is | |
| 216 // uninstalled, that way there is no clash with the preferences being wiped. | |
| 217 std::string RemoveAllRulesNoStoreUpdate(const std::string& extension_id); | |
| 218 | |
| 219 void MarkReady(base::Time storage_init_time); | |
| 220 | |
| 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); | |
| 225 | |
| 226 // The context to which this rules registry belongs. | |
| 227 content::BrowserContext* browser_context_; | |
| 228 | |
| 229 // The ID of the thread on which the rules registry lives. | |
| 230 const content::BrowserThread::ID owner_thread_; | |
| 231 | |
| 232 // The name of the event with which rules are registered. | |
| 233 const std::string event_name_; | |
| 234 | |
| 235 // The key that identifies the context in which these rules apply. | |
| 236 WebViewKey webview_key_; | |
| 237 | |
| 238 RulesDictionary rules_; | |
| 239 | |
| 240 // Signaled when we have finished reading from storage for all extensions that | |
| 241 // are loaded on startup. | |
| 242 OneShotEvent ready_; | |
| 243 | |
| 244 // The factory needs to be declared before |cache_delegate_|, so that it can | |
| 245 // produce a pointer as a construction argument for |cache_delegate_|. | |
| 246 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; | |
| 247 | |
| 248 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is | |
| 249 // NULL, then the storage functionality is disabled (this is used in tests). | |
| 250 // This registry cannot own |cache_delegate_| because during the time after | |
| 251 // rules registry service shuts down on UI thread, and the registry is | |
| 252 // destroyed on its thread, the use of the |cache_delegate_| would not be | |
| 253 // safe. The registry only ever associates with one RulesCacheDelegate | |
| 254 // instance. | |
| 255 base::WeakPtr<RulesCacheDelegate> cache_delegate_; | |
| 256 | |
| 257 ProcessStateMap process_changed_rules_requested_; | |
| 258 | |
| 259 // Returns whether any existing rule is registered with identifier |rule_id| | |
| 260 // for extension |extension_id|. | |
| 261 bool IsUniqueId(const std::string& extension_id, | |
| 262 const std::string& rule_id) const; | |
| 263 | |
| 264 // Creates an ID that is unique within the scope of|extension_id|. | |
| 265 std::string GenerateUniqueId(const std::string& extension_id); | |
| 266 | |
| 267 // Verifies that all |rules| have unique IDs or initializes them with | |
| 268 // unique IDs if they don't have one. In case of duplicate IDs, this function | |
| 269 // returns a non-empty error message. | |
| 270 std::string CheckAndFillInOptionalRules( | |
| 271 const std::string& extension_id, | |
| 272 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
| 273 | |
| 274 // Initializes the priority fields in case they have not been set. | |
| 275 void FillInOptionalPriorities( | |
| 276 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
| 277 | |
| 278 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. | |
| 279 void RemoveUsedRuleIdentifiers(const std::string& extension_id, | |
| 280 const std::vector<std::string>& identifiers); | |
| 281 | |
| 282 // Same as RemoveUsedRuleIdentifiers but operates on all rules of | |
| 283 // |extension_id|. | |
| 284 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); | |
| 285 | |
| 286 typedef std::string RuleIdentifier; | |
| 287 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; | |
| 288 RuleIdentifiersMap used_rule_identifiers_; | |
| 289 int last_generated_rule_identifier_id_; | |
| 290 | |
| 291 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | |
| 292 }; | |
| 293 | |
| 294 } // namespace extensions | |
| 295 | |
| 296 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | |
| OLD | NEW |