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" | |
15 #include "content/public/browser/notification_registrar.h" | |
16 | 14 |
17 class Profile; | 15 class Profile; |
18 | 16 |
19 namespace extensions { | 17 namespace extensions { |
20 | 18 |
21 class RulesRegistry; | 19 class RulesRegistry; |
22 | 20 |
23 // RulesCacheDelegate implements the part of the RulesRegistry which works on | 21 // RulesCacheDelegate implements the part of the RulesRegistry which works on |
24 // the UI thread. It should only be used on the UI thread. | 22 // the UI thread. It should only be used on the UI thread. |
25 // If |log_storage_init_delay| is set, the delay caused by loading and | 23 // If |log_storage_init_delay| is set, the delay caused by loading and |
26 // registering rules on initialization will be logged with UMA. | 24 // registering rules on initialization will be logged with UMA. |
27 class RulesCacheDelegate : public content::NotificationObserver { | 25 class RulesCacheDelegate { |
28 public: | 26 public: |
29 | 27 |
30 explicit RulesCacheDelegate(bool log_storage_init_delay); | 28 explicit RulesCacheDelegate(bool log_storage_init_delay); |
31 | 29 |
32 virtual ~RulesCacheDelegate(); | 30 virtual ~RulesCacheDelegate(); |
33 | 31 |
34 // Returns a key for the state store. The associated preference is a boolean | 32 // Returns a key for the state store. The associated preference is a boolean |
35 // indicating whether there are some declarative rules stored in the rule | 33 // indicating whether there are some declarative rules stored in the rule |
36 // store. | 34 // store. |
37 static std::string GetRulesStoredKey(const std::string& event_name, | 35 static std::string GetRulesStoredKey(const std::string& event_name, |
(...skipping 11 matching lines...) Expand all Loading... |
49 } | 47 } |
50 | 48 |
51 private: | 49 private: |
52 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, | 50 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, |
53 DeclarativeRulesStored); | 51 DeclarativeRulesStored); |
54 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, | 52 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, |
55 RulesStoredFlagMultipleRegistries); | 53 RulesStoredFlagMultipleRegistries); |
56 | 54 |
57 static const char kRulesStoredKey[]; | 55 static const char kRulesStoredKey[]; |
58 | 56 |
59 // NotificationObserver | |
60 virtual void Observe(int type, | |
61 const content::NotificationSource& source, | |
62 const content::NotificationDetails& details) OVERRIDE; | |
63 | |
64 // Check if we are done reading all data from storage on startup, and notify | 57 // Check if we are done reading all data from storage on startup, and notify |
65 // the RulesRegistry on its thread if so. The notification is delivered | 58 // the RulesRegistry on its thread if so. The notification is delivered |
66 // exactly once. | 59 // exactly once. |
67 void CheckIfReady(); | 60 void CheckIfReady(); |
68 | 61 |
69 // Schedules retrieving rules for already loaded extensions where | 62 // Schedules retrieving rules for already loaded extensions where |
70 // appropriate. | 63 // appropriate. |
71 void ReadRulesForInstalledExtensions(); | 64 void ReadRulesForInstalledExtensions(); |
72 | 65 |
73 // Read/write a list of rules serialized to Values. | 66 // Read/write a list of rules serialized to Values. |
74 void ReadFromStorage(const std::string& extension_id); | 67 void ReadFromStorage(const std::string& extension_id); |
75 void ReadFromStorageCallback(const std::string& extension_id, | 68 void ReadFromStorageCallback(const std::string& extension_id, |
76 scoped_ptr<base::Value> value); | 69 scoped_ptr<base::Value> value); |
77 | 70 |
78 // Check the preferences whether the extension with |extension_id| has some | 71 // Check the preferences whether the extension with |extension_id| has some |
79 // rules stored on disk. If this information is not in the preferences, true | 72 // rules stored on disk. If this information is not in the preferences, true |
80 // is returned as a safe default value. | 73 // is returned as a safe default value. |
81 bool GetDeclarativeRulesStored(const std::string& extension_id) const; | 74 bool GetDeclarativeRulesStored(const std::string& extension_id) const; |
82 // Modify the preference to |rules_stored|. | 75 // Modify the preference to |rules_stored|. |
83 void SetDeclarativeRulesStored(const std::string& extension_id, | 76 void SetDeclarativeRulesStored(const std::string& extension_id, |
84 bool rules_stored); | 77 bool rules_stored); |
85 | 78 |
86 content::NotificationRegistrar registrar_; | |
87 | |
88 Profile* profile_; | 79 Profile* profile_; |
89 | 80 |
90 // The key under which rules are stored. | 81 // The key under which rules are stored. |
91 std::string storage_key_; | 82 std::string storage_key_; |
92 | 83 |
93 // The key under which we store whether the rules have been stored. | 84 // The key under which we store whether the rules have been stored. |
94 std::string rules_stored_key_; | 85 std::string rules_stored_key_; |
95 | 86 |
96 // A set of extension IDs that have rules we are reading from storage. | 87 // A set of extension IDs that have rules we are reading from storage. |
97 std::set<std::string> waiting_for_extensions_; | 88 std::set<std::string> waiting_for_extensions_; |
(...skipping 12 matching lines...) Expand all Loading... |
110 // We notified the RulesRegistry that the rules are loaded. | 101 // We notified the RulesRegistry that the rules are loaded. |
111 bool notified_registry_; | 102 bool notified_registry_; |
112 | 103 |
113 // Use this factory to generate weak pointers bound to the UI thread. | 104 // Use this factory to generate weak pointers bound to the UI thread. |
114 base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_; | 105 base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_; |
115 }; | 106 }; |
116 | 107 |
117 } // namespace extensions | 108 } // namespace extensions |
118 | 109 |
119 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ | 110 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ |
OLD | NEW |