| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_CACHE_DELEGATE_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 16 | 16 |
| 17 class Profile; | 17 class Profile; |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 | 20 |
| 21 class RulesRegistry; | 21 class RulesRegistry; |
| 22 | 22 |
| 23 // RulesCacheDelegate implements the part of the RulesRegistry which works on | 23 // RulesCacheDelegate implements the part of the RulesRegistry which works on |
| 24 // the UI thread. It should only be used on the UI thread. It gets created | 24 // the UI thread. It should only be used on the UI thread. |
| 25 // by the RulesRegistry, but right after that it changes owner to the | |
| 26 // RulesRegistryService, and is deleted by the service. | |
| 27 // If |log_storage_init_delay| is set, the delay caused by loading and | 25 // If |log_storage_init_delay| is set, the delay caused by loading and |
| 28 // registering rules on initialization will be logged with UMA. | 26 // registering rules on initialization will be logged with UMA. |
| 29 class RulesCacheDelegate : public content::NotificationObserver { | 27 class RulesCacheDelegate : public content::NotificationObserver { |
| 30 public: | 28 public: |
| 31 // |event_name| identifies the JavaScript event for which rules are | 29 |
| 32 // registered. For example, for WebRequestRulesRegistry the name is | 30 explicit RulesCacheDelegate(bool log_storage_init_delay); |
| 33 // "declarativeWebRequest.onRequest". | |
| 34 RulesCacheDelegate(Profile* profile, | |
| 35 const std::string& event_name, | |
| 36 content::BrowserThread::ID rules_registry_thread, | |
| 37 base::WeakPtr<RulesRegistry> registry, | |
| 38 bool log_storage_init_delay); | |
| 39 | 31 |
| 40 virtual ~RulesCacheDelegate(); | 32 virtual ~RulesCacheDelegate(); |
| 41 | 33 |
| 42 // Returns a key for the state store. The associated preference is a boolean | 34 // Returns a key for the state store. The associated preference is a boolean |
| 43 // indicating whether there are some declarative rules stored in the rule | 35 // indicating whether there are some declarative rules stored in the rule |
| 44 // store. | 36 // store. |
| 45 static std::string GetRulesStoredKey(const std::string& event_name, | 37 static std::string GetRulesStoredKey(const std::string& event_name, |
| 46 bool incognito); | 38 bool incognito); |
| 47 | 39 |
| 48 // Initialize the storage functionality. | 40 // Initialize the storage functionality. |
| 49 void Init(); | 41 void Init(RulesRegistry* registry); |
| 50 | 42 |
| 51 void WriteToStorage(const std::string& extension_id, | 43 void WriteToStorage(const std::string& extension_id, |
| 52 scoped_ptr<base::Value> value); | 44 scoped_ptr<base::Value> value); |
| 53 | 45 |
| 54 base::WeakPtr<RulesCacheDelegate> GetWeakPtr() { | 46 base::WeakPtr<RulesCacheDelegate> GetWeakPtr() { |
| 55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 47 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 56 return weak_ptr_factory_.GetWeakPtr(); | 48 return weak_ptr_factory_.GetWeakPtr(); |
| 57 } | 49 } |
| 58 | 50 |
| 59 private: | 51 private: |
| 60 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, | 52 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, |
| 61 DeclarativeRulesStored); | 53 DeclarativeRulesStored); |
| 62 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, | 54 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, |
| 63 RulesStoredFlagMultipleRegistries); | 55 RulesStoredFlagMultipleRegistries); |
| 64 | 56 |
| 65 static const char kRulesStoredKey[]; | 57 static const char kRulesStoredKey[]; |
| 66 | 58 |
| 67 // NotificationObserver | 59 // NotificationObserver |
| 68 virtual void Observe(int type, | 60 virtual void Observe(int type, |
| 69 const content::NotificationSource& source, | 61 const content::NotificationSource& source, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 89 bool GetDeclarativeRulesStored(const std::string& extension_id) const; | 81 bool GetDeclarativeRulesStored(const std::string& extension_id) const; |
| 90 // Modify the preference to |rules_stored|. | 82 // Modify the preference to |rules_stored|. |
| 91 void SetDeclarativeRulesStored(const std::string& extension_id, | 83 void SetDeclarativeRulesStored(const std::string& extension_id, |
| 92 bool rules_stored); | 84 bool rules_stored); |
| 93 | 85 |
| 94 content::NotificationRegistrar registrar_; | 86 content::NotificationRegistrar registrar_; |
| 95 | 87 |
| 96 Profile* profile_; | 88 Profile* profile_; |
| 97 | 89 |
| 98 // The key under which rules are stored. | 90 // The key under which rules are stored. |
| 99 const std::string storage_key_; | 91 std::string storage_key_; |
| 100 | 92 |
| 101 // The key under which we store whether the rules have been stored. | 93 // The key under which we store whether the rules have been stored. |
| 102 const std::string rules_stored_key_; | 94 std::string rules_stored_key_; |
| 103 | 95 |
| 104 // A set of extension IDs that have rules we are reading from storage. | 96 // A set of extension IDs that have rules we are reading from storage. |
| 105 std::set<std::string> waiting_for_extensions_; | 97 std::set<std::string> waiting_for_extensions_; |
| 106 | 98 |
| 107 // We measure the time spent on loading rules on init. The result is logged | 99 // We measure the time spent on loading rules on init. The result is logged |
| 108 // with UMA once per each RulesCacheDelegate instance, unless in Incognito. | 100 // with UMA once per each RulesCacheDelegate instance, unless in Incognito. |
| 109 base::Time storage_init_time_; | 101 base::Time storage_init_time_; |
| 110 bool log_storage_init_delay_; | 102 bool log_storage_init_delay_; |
| 111 | 103 |
| 112 // Weak pointer to post tasks to the owning rules registry. | 104 // Weak pointer to post tasks to the owning rules registry. |
| 113 const base::WeakPtr<RulesRegistry> registry_; | 105 base::WeakPtr<RulesRegistry> registry_; |
| 114 | 106 |
| 115 // The thread |registry_| lives on. | 107 // The thread |registry_| lives on. |
| 116 const content::BrowserThread::ID rules_registry_thread_; | 108 content::BrowserThread::ID rules_registry_thread_; |
| 117 | 109 |
| 118 // We notified the RulesRegistry that the rules are loaded. | 110 // We notified the RulesRegistry that the rules are loaded. |
| 119 bool notified_registry_; | 111 bool notified_registry_; |
| 120 | 112 |
| 121 // Use this factory to generate weak pointers bound to the UI thread. | 113 // Use this factory to generate weak pointers bound to the UI thread. |
| 122 base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_; | 114 base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_; |
| 123 }; | 115 }; |
| 124 | 116 |
| 125 } // namespace extensions | 117 } // namespace extensions |
| 126 | 118 |
| 127 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ | 119 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
| OLD | NEW |