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

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: Merge with ToT 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 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue(
30 const base::Value* value) {
31 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules;
32
33 const base::ListValue* list = NULL;
34 if (!value || !value->GetAsList(&list))
35 return rules;
36
37 rules.reserve(list->GetSize());
38 for (size_t i = 0; i < list->GetSize(); ++i) {
39 const base::DictionaryValue* dict = NULL;
40 if (!list->GetDictionary(i, &dict))
41 continue;
42 linked_ptr<extensions::RulesRegistry::Rule> rule(
43 new extensions::RulesRegistry::Rule());
44 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get()))
45 rules.push_back(rule);
46 }
47
48 return rules;
49 }
50
29 } // namespace 51 } // namespace
30 52
31 namespace extensions { 53 namespace extensions {
32 54
33 // RulesCacheDelegate 55 // RulesCacheDelegate
34 56
35 const char RulesCacheDelegate::kRulesStoredKey[] = 57 const char RulesCacheDelegate::kRulesStoredKey[] =
36 "has_declarative_rules"; 58 "has_declarative_rules";
37 59
38 RulesCacheDelegate::RulesCacheDelegate( 60 RulesCacheDelegate::RulesCacheDelegate(
39 Profile* profile,
40 const std::string& event_name,
41 content::BrowserThread::ID rules_registry_thread,
42 base::WeakPtr<RulesRegistry> registry, 61 base::WeakPtr<RulesRegistry> registry,
43 bool log_storage_init_delay) 62 bool log_storage_init_delay)
44 : profile_(profile), 63 : profile_(registry->profile()),
45 storage_key_(GetDeclarativeRuleStorageKey(event_name, 64 storage_key_(GetDeclarativeRuleStorageKey(registry->event_name(),
46 profile->IsOffTheRecord())), 65 profile_->IsOffTheRecord())),
47 rules_stored_key_(GetRulesStoredKey(event_name, 66 rules_stored_key_(GetRulesStoredKey(registry->event_name(),
48 profile->IsOffTheRecord())), 67 profile_->IsOffTheRecord())),
49 log_storage_init_delay_(log_storage_init_delay), 68 log_storage_init_delay_(log_storage_init_delay),
50 registry_(registry), 69 registry_(registry),
51 rules_registry_thread_(rules_registry_thread), 70 rules_registry_thread_(registry->owner_thread()),
52 notified_registry_(false), 71 notified_registry_(false),
53 weak_ptr_factory_(this) {} 72 weak_ptr_factory_(this) {}
54 73
55 RulesCacheDelegate::~RulesCacheDelegate() {} 74 RulesCacheDelegate::~RulesCacheDelegate() {}
56 75
57 // Returns the key to use for storing whether the rules have been stored. 76 // Returns the key to use for storing whether the rules have been stored.
58 // static 77 // static
59 std::string RulesCacheDelegate::GetRulesStoredKey(const std::string& event_name, 78 std::string RulesCacheDelegate::GetRulesStoredKey(const std::string& event_name,
60 bool incognito) { 79 bool incognito) {
61 std::string result(kRulesStoredKey); 80 std::string result(kRulesStoredKey);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 extension_id)); 217 extension_id));
199 } 218 }
200 219
201 void RulesCacheDelegate::ReadFromStorageCallback( 220 void RulesCacheDelegate::ReadFromStorageCallback(
202 const std::string& extension_id, 221 const std::string& extension_id,
203 scoped_ptr<base::Value> value) { 222 scoped_ptr<base::Value> value) {
204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 223 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
205 content::BrowserThread::PostTask( 224 content::BrowserThread::PostTask(
206 rules_registry_thread_, 225 rules_registry_thread_,
207 FROM_HERE, 226 FROM_HERE,
208 base::Bind(&RulesRegistry::DeserializeAndAddRules, 227 base::Bind(&RulesCacheDelegate::DeserializeAndAddRules,
209 registry_, 228 registry_,
210 extension_id, 229 extension_id,
211 base::Passed(&value))); 230 base::Passed(&value)));
212 231
213 waiting_for_extensions_.erase(extension_id); 232 waiting_for_extensions_.erase(extension_id);
214 233
215 if (waiting_for_extensions_.empty()) 234 if (waiting_for_extensions_.empty())
216 ExtensionSystem::Get(profile_)->ready().Post( 235 ExtensionSystem::Get(profile_)->ready().Post(
217 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady, GetWeakPtr())); 236 FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady, GetWeakPtr()));
218 } 237 }
(...skipping 17 matching lines...) Expand all
236 const std::string& extension_id, 255 const std::string& extension_id,
237 bool rules_stored) { 256 bool rules_stored) {
238 CHECK(profile_); 257 CHECK(profile_);
239 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); 258 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
240 extension_prefs->UpdateExtensionPref( 259 extension_prefs->UpdateExtensionPref(
241 extension_id, 260 extension_id,
242 rules_stored_key_, 261 rules_stored_key_,
243 new base::FundamentalValue(rules_stored)); 262 new base::FundamentalValue(rules_stored));
244 } 263 }
245 264
265 // static
266 void RulesCacheDelegate::DeserializeAndAddRules(
Jeffrey Yasskin 2013/11/01 00:15:09 I'm curious why this moved. It doesn't seem to nee
Fady Samuel 2013/11/01 15:13:39 I thought it would be cleaner but it didn't really
267 base::WeakPtr<RulesRegistry> registry,
268 const std::string& extension_id,
269 scoped_ptr<base::Value> rules) {
270 DCHECK(content::BrowserThread::CurrentlyOn(registry->owner_thread()));
271
272 if (!registry)
Jeffrey Yasskin 2013/11/01 00:15:09 This needs to be before the CurrentlyOn call, whic
Fady Samuel 2013/11/01 15:13:39 This is no longer relevant. DeserialAndAddRules ha
273 return;
274
275 registry->AddRules(extension_id, RulesFromValue(rules.get()));
276 }
277
246 } // namespace extensions 278 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698