Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(731)

Side by Side Diff: chrome/browser/extensions/api/declarative/rules_cache_delegate.cc

Issue 53273002: Decouple RulesCacheDelegate from RulesRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor_rules_registry_with_cache
Patch Set: Updated Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" 5 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
6 6
7 #include "chrome/browser/chrome_notification_types.h" 7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
9 #include "chrome/browser/extensions/extension_info_map.h" 9 #include "chrome/browser/extensions/extension_info_map.h"
10 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_system.h" 11 #include "chrome/browser/extensions/extension_system.h"
12 #include "chrome/browser/extensions/extension_util.h" 12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/extensions/state_store.h" 13 #include "chrome/browser/extensions/state_store.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "content/public/browser/notification_details.h" 15 #include "content/public/browser/notification_details.h"
16 #include "content/public/browser/notification_source.h" 16 #include "content/public/browser/notification_source.h"
17 17
18 namespace { 18 namespace {
19 19
20 // Returns the key to use for storing declarative rules in the state store. 20 // Returns the key to use for storing declarative rules in the state store.
21 std::string GetDeclarativeRuleStorageKey(const std::string& event_name, 21 std::string GetDeclarativeRuleStorageKey(const std::string& event_name,
22 bool incognito) { 22 bool incognito) {
23 if (incognito) 23 if (incognito)
24 return "declarative_rules.incognito." + event_name; 24 return "declarative_rules.incognito." + event_name;
25 else 25 else
26 return "declarative_rules." + event_name; 26 return "declarative_rules." + event_name;
27 } 27 }
28 28
29
29 } // namespace 30 } // namespace
30 31
31 namespace extensions { 32 namespace extensions {
32 33
33 // RulesCacheDelegate 34 // RulesCacheDelegate
34 35
35 const char RulesCacheDelegate::kRulesStoredKey[] = 36 const char RulesCacheDelegate::kRulesStoredKey[] =
36 "has_declarative_rules"; 37 "has_declarative_rules";
37 38
38 RulesCacheDelegate::RulesCacheDelegate( 39 RulesCacheDelegate::RulesCacheDelegate(bool log_storage_init_delay)
39 Profile* profile, 40 : profile_(NULL),
40 const std::string& event_name,
41 content::BrowserThread::ID rules_registry_thread,
42 base::WeakPtr<RulesRegistry> registry,
43 bool log_storage_init_delay)
44 : profile_(profile),
45 storage_key_(GetDeclarativeRuleStorageKey(event_name,
46 profile->IsOffTheRecord())),
47 rules_stored_key_(GetRulesStoredKey(event_name,
48 profile->IsOffTheRecord())),
49 log_storage_init_delay_(log_storage_init_delay), 41 log_storage_init_delay_(log_storage_init_delay),
50 registry_(registry),
51 rules_registry_thread_(rules_registry_thread),
52 notified_registry_(false), 42 notified_registry_(false),
53 weak_ptr_factory_(this) {} 43 weak_ptr_factory_(this) {
44 }
54 45
55 RulesCacheDelegate::~RulesCacheDelegate() {} 46 RulesCacheDelegate::~RulesCacheDelegate() {}
56 47
57 // Returns the key to use for storing whether the rules have been stored. 48 // Returns the key to use for storing whether the rules have been stored.
58 // static 49 // static
59 std::string RulesCacheDelegate::GetRulesStoredKey(const std::string& event_name, 50 std::string RulesCacheDelegate::GetRulesStoredKey(const std::string& event_name,
60 bool incognito) { 51 bool incognito) {
61 std::string result(kRulesStoredKey); 52 std::string result(kRulesStoredKey);
62 result += incognito ? ".incognito." : "."; 53 result += incognito ? ".incognito." : ".";
63 return result + event_name; 54 return result + event_name;
64 } 55 }
65 56
66 // This is called from the constructor of RulesRegistry, so it is 57 // This is called from the constructor of RulesRegistry, so it is
67 // important that it both 58 // important that it both
68 // 1. calls no (in particular virtual) methods of the rules registry, and 59 // 1. calls no (in particular virtual) methods of the rules registry, and
69 // 2. does not create scoped_refptr holding the registry. (A short-lived 60 // 2. does not create scoped_refptr holding the registry. (A short-lived
70 // scoped_refptr might delete the rules registry before it is constructed.) 61 // scoped_refptr might delete the rules registry before it is constructed.)
71 void RulesCacheDelegate::Init() { 62 void RulesCacheDelegate::Init(RulesRegistry* registry) {
72 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
73 64
65 // WARNING: The first use of |registry_| will bind it to the calling thread
66 // so don't use this here.
67 registry_ = registry->GetWeakPtr();
68
69 profile_ = registry->profile();
70 storage_key_ =
71 GetDeclarativeRuleStorageKey(registry->event_name(),
72 profile_->IsOffTheRecord());
73 rules_stored_key_ = GetRulesStoredKey(registry->event_name(),
74 profile_->IsOffTheRecord());
75 rules_registry_thread_ = registry->owner_thread();
76
74 ExtensionSystem& system = *ExtensionSystem::Get(profile_); 77 ExtensionSystem& system = *ExtensionSystem::Get(profile_);
75 extensions::StateStore* store = system.rules_store(); 78 extensions::StateStore* store = system.rules_store();
76 if (store) 79 if (store)
77 store->RegisterKey(storage_key_); 80 store->RegisterKey(storage_key_);
78 81
79 registrar_.Add(this, 82 registrar_.Add(this,
80 chrome::NOTIFICATION_EXTENSION_LOADED, 83 chrome::NOTIFICATION_EXTENSION_LOADED,
81 content::Source<Profile>(profile_->GetOriginalProfile())); 84 content::Source<Profile>(profile_->GetOriginalProfile()));
82 85
83 if (profile_->IsOffTheRecord()) 86 if (profile_->IsOffTheRecord())
84 log_storage_init_delay_ = false; 87 log_storage_init_delay_ = false;
85 88
86 system.ready().Post( 89 system.ready().Post(
87 FROM_HERE, 90 FROM_HERE,
88 base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions, 91 base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions,
89 GetWeakPtr())); 92 weak_ptr_factory_.GetWeakPtr()));
90 system.ready().Post(FROM_HERE, 93 system.ready().Post(FROM_HERE,
91 base::Bind(&RulesCacheDelegate::CheckIfReady, 94 base::Bind(&RulesCacheDelegate::CheckIfReady,
92 GetWeakPtr())); 95 weak_ptr_factory_.GetWeakPtr()));
93 } 96 }
94 97
95 void RulesCacheDelegate::WriteToStorage(const std::string& extension_id, 98 void RulesCacheDelegate::WriteToStorage(const std::string& extension_id,
96 scoped_ptr<base::Value> value) { 99 scoped_ptr<base::Value> value) {
97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
98 if (!profile_) 101 if (!profile_)
99 return; 102 return;
100 103
101 const base::ListValue* rules = NULL; 104 const base::ListValue* rules = NULL;
102 CHECK(value->GetAsList(&rules)); 105 CHECK(value->GetAsList(&rules));
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 void RulesCacheDelegate::ReadFromStorage(const std::string& extension_id) { 178 void RulesCacheDelegate::ReadFromStorage(const std::string& extension_id) {
176 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 179 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
177 if (!profile_) 180 if (!profile_)
178 return; 181 return;
179 182
180 if (log_storage_init_delay_ && storage_init_time_.is_null()) 183 if (log_storage_init_delay_ && storage_init_time_.is_null())
181 storage_init_time_ = base::Time::Now(); 184 storage_init_time_ = base::Time::Now();
182 185
183 if (!GetDeclarativeRulesStored(extension_id)) { 186 if (!GetDeclarativeRulesStored(extension_id)) {
184 ExtensionSystem::Get(profile_)->ready().Post( 187 ExtensionSystem::Get(profile_)->ready().Post(
185 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady, GetWeakPtr())); 188 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady,
189 weak_ptr_factory_.GetWeakPtr()));
186 return; 190 return;
187 } 191 }
188 192
189 extensions::StateStore* store = ExtensionSystem::Get(profile_)->rules_store(); 193 extensions::StateStore* store = ExtensionSystem::Get(profile_)->rules_store();
190 if (!store) 194 if (!store)
191 return; 195 return;
192 waiting_for_extensions_.insert(extension_id); 196 waiting_for_extensions_.insert(extension_id);
193 store->GetExtensionValue( 197 store->GetExtensionValue(
194 extension_id, 198 extension_id,
195 storage_key_, 199 storage_key_,
(...skipping 11 matching lines...) Expand all
207 FROM_HERE, 211 FROM_HERE,
208 base::Bind(&RulesRegistry::DeserializeAndAddRules, 212 base::Bind(&RulesRegistry::DeserializeAndAddRules,
209 registry_, 213 registry_,
210 extension_id, 214 extension_id,
211 base::Passed(&value))); 215 base::Passed(&value)));
212 216
213 waiting_for_extensions_.erase(extension_id); 217 waiting_for_extensions_.erase(extension_id);
214 218
215 if (waiting_for_extensions_.empty()) 219 if (waiting_for_extensions_.empty())
216 ExtensionSystem::Get(profile_)->ready().Post( 220 ExtensionSystem::Get(profile_)->ready().Post(
217 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady, GetWeakPtr())); 221 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady,
222 weak_ptr_factory_.GetWeakPtr()));
218 } 223 }
219 224
220 bool RulesCacheDelegate::GetDeclarativeRulesStored( 225 bool RulesCacheDelegate::GetDeclarativeRulesStored(
221 const std::string& extension_id) const { 226 const std::string& extension_id) const {
222 CHECK(profile_); 227 CHECK(profile_);
223 const ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); 228 const ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
224 229
225 bool rules_stored = true; 230 bool rules_stored = true;
226 if (extension_prefs->ReadPrefAsBoolean( 231 if (extension_prefs->ReadPrefAsBoolean(
227 extension_id, rules_stored_key_, &rules_stored)) 232 extension_id, rules_stored_key_, &rules_stored))
228 return rules_stored; 233 return rules_stored;
229 234
230 // Safe default -- if we don't know that the rules are not stored, we force 235 // Safe default -- if we don't know that the rules are not stored, we force
231 // a read by returning true. 236 // a read by returning true.
232 return true; 237 return true;
233 } 238 }
234 239
235 void RulesCacheDelegate::SetDeclarativeRulesStored( 240 void RulesCacheDelegate::SetDeclarativeRulesStored(
236 const std::string& extension_id, 241 const std::string& extension_id,
237 bool rules_stored) { 242 bool rules_stored) {
238 CHECK(profile_); 243 CHECK(profile_);
239 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); 244 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
240 extension_prefs->UpdateExtensionPref( 245 extension_prefs->UpdateExtensionPref(
241 extension_id, 246 extension_id,
242 rules_stored_key_, 247 rules_stored_key_,
243 new base::FundamentalValue(rules_stored)); 248 new base::FundamentalValue(rules_stored));
244 } 249 }
245 250
246 } // namespace extensions 251 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698