Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_WITH_CACHE_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_WITH_CACHE_H__ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_WITH_CACHE_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_WITH_CACHE_H__ |
| 7 | 7 |
| 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 class Profile; | 22 class Profile; |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 // A base class for RulesRegistries that takes care of storing the | 26 // A base class for RulesRegistries that takes care of storing the |
| 27 // RulesRegistry::Rule objects. It contains all the methods that need to run on | 27 // RulesRegistry::Rule objects. It contains all the methods that need to run on |
| 28 // the registry thread; methods that need to run on the UI thread are separated | 28 // the registry thread; methods that need to run on the UI thread are separated |
| 29 // in the RuleStorageOnUI object. | 29 // in the RuleStorageOnUI object. |
| 30 class RulesRegistryWithCache : public RulesRegistry { | 30 class RulesRegistryWithCache : public RulesRegistry { |
| 31 public: | 31 public: |
| 32 typedef std::vector<linked_ptr<RulesRegistry::Rule> > RulesVector; | |
| 32 // RuleStorageOnUI implements the part of the RulesRegistry which works on | 33 // RuleStorageOnUI implements the part of the RulesRegistry which works on |
| 33 // the UI thread. It should only be used on the UI thread. It gets created | 34 // the UI thread. It should only be used on the UI thread. It gets created |
| 34 // by the RulesRegistry, but right after that it changes owner to the | 35 // by the RulesRegistry, but right after that it changes owner to the |
| 35 // RulesRegistryService, and is deleted by the service. | 36 // RulesRegistryService, and is deleted by the service. |
| 36 // If |log_storage_init_delay| is set, the delay caused by loading and | 37 // If |log_storage_init_delay| is set, the delay caused by loading and |
| 37 // registering rules on initialization will be logged with UMA. | 38 // registering rules on initialization will be logged with UMA. |
| 38 class RuleStorageOnUI : public content::NotificationObserver { | 39 class RuleStorageOnUI : public content::NotificationObserver { |
| 39 public: | 40 public: |
| 40 RuleStorageOnUI(Profile* profile, | 41 RuleStorageOnUI(Profile* profile, |
| 41 const std::string& storage_key, | 42 const std::string& storage_key, |
| 42 content::BrowserThread::ID rules_registry_thread, | 43 content::BrowserThread::ID rules_registry_thread, |
| 43 base::WeakPtr<RulesRegistryWithCache> registry, | 44 base::WeakPtr<RulesRegistryWithCache> registry, |
| 44 bool log_storage_init_delay); | 45 bool log_storage_init_delay); |
| 45 | 46 |
| 46 virtual ~RuleStorageOnUI(); | 47 virtual ~RuleStorageOnUI(); |
| 47 | 48 |
| 48 // Initialize the storage functionality. | 49 // Initialize the storage functionality. |
| 49 void Init(); | 50 void Init(); |
| 50 | 51 |
| 51 void WriteToStorage(const std::string& extension_id, | 52 void WriteToStorage(const std::string& extension_id, |
| 52 scoped_ptr<base::Value> value); | 53 scoped_ptr<RulesVector> new_rules); |
| 53 | 54 |
| 54 base::WeakPtr<RuleStorageOnUI> GetWeakPtr() { | 55 base::WeakPtr<RuleStorageOnUI> GetWeakPtr() { |
| 55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 56 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 56 return weak_ptr_factory_.GetWeakPtr(); | 57 return weak_ptr_factory_.GetWeakPtr(); |
| 57 } | 58 } |
| 58 | 59 |
| 59 private: | 60 private: |
| 60 enum ReadyState { // Our increasing states of readiness. | 61 enum ReadyState { // Our increasing states of readiness. |
| 61 NOT_READY, // Waiting for NOTIFICATION_EXTENSIONS_READY. | 62 NOT_READY, // Waiting for NOTIFICATION_EXTENSIONS_READY. |
| 62 EXTENSIONS_READY, // Observed NOTIFICATION_EXTENSIONS_READY. | 63 EXTENSIONS_READY, // Observed NOTIFICATION_EXTENSIONS_READY. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; | 156 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; |
| 156 virtual std::string RemoveRulesImpl( | 157 virtual std::string RemoveRulesImpl( |
| 157 const std::string& extension_id, | 158 const std::string& extension_id, |
| 158 const std::vector<std::string>& rule_identifiers) = 0; | 159 const std::vector<std::string>& rule_identifiers) = 0; |
| 159 virtual std::string RemoveAllRulesImpl( | 160 virtual std::string RemoveAllRulesImpl( |
| 160 const std::string& extension_id) = 0; | 161 const std::string& extension_id) = 0; |
| 161 | 162 |
| 162 private: | 163 private: |
| 163 typedef std::string ExtensionId; | 164 typedef std::string ExtensionId; |
| 164 typedef std::string RuleId; | 165 typedef std::string RuleId; |
| 165 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey; | 166 typedef std::map<RuleId, linked_ptr<RulesRegistry::Rule> > RulesDictionary; |
| 166 typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> > | |
| 167 RulesDictionary; | |
| 168 | 167 |
| 169 // Common processing after extension's rules have changed. | 168 // Common processing after extension's rules have changed. |
| 170 void ProcessChangedRules(const std::string& extension_id); | 169 void ProcessChangedRules(const std::string& extension_id); |
| 171 | 170 |
| 172 // Process the callbacks once the registry gets ready. | 171 // Process the callbacks once the registry gets ready. |
| 173 void OnReady(base::Time storage_init_time); | 172 void OnReady(base::Time storage_init_time); |
| 174 | 173 |
| 175 // Deserialize the rules from the given Value object and add them to the | 174 // Deserialize the rules from the given Value object and add them to the |
| 176 // RulesRegistry. | 175 // RulesRegistry. |
| 177 void DeserializeAndAddRules(const std::string& extension_id, | 176 void DeserializeAndAddRules(const std::string& extension_id, |
| 178 scoped_ptr<base::Value> rules); | 177 scoped_ptr<base::Value> rules); |
| 179 | 178 |
| 180 | 179 |
| 181 RulesDictionary rules_; | 180 std::map<ExtensionId, RulesDictionary> rules_; |
|
battre
2013/05/07 16:22:09
This variable is going to be deleted with my CL. I
| |
| 182 | 181 |
| 183 std::vector<base::Closure> ready_callbacks_; | 182 std::vector<base::Closure> ready_callbacks_; |
| 184 | 183 |
| 185 // True when we have finished reading from storage for all extensions that | 184 // True when we have finished reading from storage for all extensions that |
| 186 // are loaded on startup. | 185 // are loaded on startup. |
| 187 bool ready_; | 186 bool ready_; |
| 188 | 187 |
| 189 // The factory needs to be declared before |storage_on_ui_|, so that it can | 188 // The factory needs to be declared before |storage_on_ui_|, so that it can |
| 190 // produce a pointer as a construction argument for |storage_on_ui_|. | 189 // produce a pointer as a construction argument for |storage_on_ui_|. |
| 191 base::WeakPtrFactory<RulesRegistryWithCache> weak_ptr_factory_; | 190 base::WeakPtrFactory<RulesRegistryWithCache> weak_ptr_factory_; |
| 192 | 191 |
| 193 // |storage_on_ui_| is owned by the registry service. If |storage_on_ui_| is | 192 // |storage_on_ui_| is owned by the registry service. If |storage_on_ui_| is |
| 194 // NULL, then the storage functionality is disabled (this is used in tests). | 193 // NULL, then the storage functionality is disabled (this is used in tests). |
| 195 // This registry cannot own |storage_on_ui_| because during the time after | 194 // This registry cannot own |storage_on_ui_| because during the time after |
| 196 // rules registry service shuts down on UI thread, and the registry is | 195 // rules registry service shuts down on UI thread, and the registry is |
| 197 // destroyed on its thread, the use of the |storage_on_ui_| would not be | 196 // destroyed on its thread, the use of the |storage_on_ui_| would not be |
| 198 // safe. The registry only ever associates with one RuleStorageOnUI instance. | 197 // safe. The registry only ever associates with one RuleStorageOnUI instance. |
| 199 const base::WeakPtr<RuleStorageOnUI> storage_on_ui_; | 198 const base::WeakPtr<RuleStorageOnUI> storage_on_ui_; |
| 200 | 199 |
| 201 DISALLOW_COPY_AND_ASSIGN(RulesRegistryWithCache); | 200 DISALLOW_COPY_AND_ASSIGN(RulesRegistryWithCache); |
| 202 }; | 201 }; |
| 203 | 202 |
| 204 } // namespace extensions | 203 } // namespace extensions |
| 205 | 204 |
| 206 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_WITH_CACHE_H __ | 205 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_WITH_CACHE_H __ |
| OLD | NEW |