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 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 const char kDuplicateRuleId[] = "Duplicate rule ID: %s"; | 31 const char kDuplicateRuleId[] = "Duplicate rule ID: %s"; |
| 32 | 32 |
| 33 scoped_ptr<base::Value> RulesToValue( | 33 scoped_ptr<base::Value> RulesToValue( |
| 34 const std::vector<linked_ptr<extensions::RulesRegistry::Rule> >& rules) { | 34 const std::vector<linked_ptr<extensions::RulesRegistry::Rule> >& rules) { |
| 35 scoped_ptr<base::ListValue> list(new base::ListValue()); | 35 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 36 for (size_t i = 0; i < rules.size(); ++i) | 36 for (size_t i = 0; i < rules.size(); ++i) |
| 37 list->Append(rules[i]->ToValue().release()); | 37 list->Append(rules[i]->ToValue().release()); |
| 38 return list.PassAs<base::Value>(); | 38 return list.PassAs<base::Value>(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( | |
| 42 const base::Value* value) { | |
| 43 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules; | |
| 44 | |
| 45 const base::ListValue* list = NULL; | |
| 46 if (!value || !value->GetAsList(&list)) | |
| 47 return rules; | |
| 48 | |
| 49 rules.reserve(list->GetSize()); | |
| 50 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 51 const base::DictionaryValue* dict = NULL; | |
| 52 if (!list->GetDictionary(i, &dict)) | |
| 53 continue; | |
| 54 linked_ptr<extensions::RulesRegistry::Rule> rule( | |
| 55 new extensions::RulesRegistry::Rule()); | |
| 56 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) | |
| 57 rules.push_back(rule); | |
| 58 } | |
| 59 | |
| 60 return rules; | |
| 61 } | |
| 62 | |
| 63 std::string ToId(int identifier) { | 41 std::string ToId(int identifier) { |
| 64 return base::StringPrintf("_%d_", identifier); | 42 return base::StringPrintf("_%d_", identifier); |
| 65 } | 43 } |
| 66 | 44 |
| 67 } // namespace | 45 } // namespace |
| 68 | 46 |
| 69 | 47 |
| 70 namespace extensions { | 48 namespace extensions { |
| 71 | 49 |
| 72 // RulesRegistry | 50 // RulesRegistry |
| 73 | 51 |
| 74 RulesRegistry::RulesRegistry( | 52 RulesRegistry::RulesRegistry( |
| 75 Profile* profile, | 53 Profile* profile, |
| 76 const std::string& event_name, | 54 const std::string& event_name, |
| 77 content::BrowserThread::ID owner_thread, | 55 content::BrowserThread::ID owner_thread) |
| 78 bool log_storage_init_delay, | 56 : profile_(profile), |
| 79 scoped_ptr<RulesCacheDelegate>* ui_part) | 57 owner_thread_(owner_thread), |
| 80 : owner_thread_(owner_thread), | |
| 81 event_name_(event_name), | 58 event_name_(event_name), |
|
Jeffrey Yasskin
2013/11/01 00:15:09
event_name_ is never used in RulesRegistry, just t
Fady Samuel
2013/11/01 15:13:39
More cleanup coming in subsequent CLs. I'd like to
| |
| 82 weak_ptr_factory_(profile ? this : NULL), | 59 weak_ptr_factory_(profile ? this : NULL), |
| 83 cache_delegate_( | |
| 84 (profile ? (new RulesCacheDelegate(profile, | |
| 85 event_name, | |
| 86 owner_thread, | |
| 87 weak_ptr_factory_.GetWeakPtr(), | |
| 88 log_storage_init_delay))->GetWeakPtr() | |
| 89 : base::WeakPtr<RulesCacheDelegate>())), | |
| 90 process_changed_rules_requested_(profile ? NOT_SCHEDULED_FOR_PROCESSING | 60 process_changed_rules_requested_(profile ? NOT_SCHEDULED_FOR_PROCESSING |
| 91 : NEVER_PROCESS), | 61 : NEVER_PROCESS), |
| 92 last_generated_rule_identifier_id_(0) { | 62 last_generated_rule_identifier_id_(0) { |
| 93 if (!profile) { | |
| 94 CHECK(!ui_part); | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 ui_part->reset(cache_delegate_.get()); | |
| 99 | |
| 100 cache_delegate_->Init(); | |
| 101 } | 63 } |
| 102 | 64 |
| 103 std::string RulesRegistry::AddRules( | 65 std::string RulesRegistry::AddRules( |
| 104 const std::string& extension_id, | 66 const std::string& extension_id, |
| 105 const std::vector<linked_ptr<Rule> >& rules) { | 67 const std::vector<linked_ptr<Rule> >& rules) { |
| 106 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 68 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 107 | 69 |
| 108 std::string error = CheckAndFillInOptionalRules(extension_id, rules); | 70 std::string error = CheckAndFillInOptionalRules(extension_id, rules); |
| 109 if (!error.empty()) | 71 if (!error.empty()) |
| 110 return error; | 72 return error; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 } | 173 } |
| 212 | 174 |
| 213 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { | 175 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { |
| 214 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 176 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 215 std::string error = RemoveAllRules(extension_id); | 177 std::string error = RemoveAllRules(extension_id); |
| 216 if (!error.empty()) | 178 if (!error.empty()) |
| 217 LOG(ERROR) << error; | 179 LOG(ERROR) << error; |
| 218 used_rule_identifiers_.erase(extension_id); | 180 used_rule_identifiers_.erase(extension_id); |
| 219 } | 181 } |
| 220 | 182 |
| 183 bool RulesRegistry::SetCacheDelegate( | |
| 184 base::WeakPtr<RulesCacheDelegate> cache_delegate) { | |
| 185 if (cache_delegate_) | |
| 186 return false; | |
| 187 cache_delegate_ = cache_delegate; | |
| 188 cache_delegate_->Init(); | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 221 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { | 192 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { |
| 222 size_t entry_count = 0u; | 193 size_t entry_count = 0u; |
| 223 for (RuleIdentifiersMap::const_iterator extension = | 194 for (RuleIdentifiersMap::const_iterator extension = |
| 224 used_rule_identifiers_.begin(); | 195 used_rule_identifiers_.begin(); |
| 225 extension != used_rule_identifiers_.end(); | 196 extension != used_rule_identifiers_.end(); |
| 226 ++extension) { | 197 ++extension) { |
| 227 // Each extension is counted as 1 just for being there. Otherwise we miss | 198 // Each extension is counted as 1 just for being there. Otherwise we miss |
| 228 // keys with empty values. | 199 // keys with empty values. |
| 229 entry_count += 1u + extension->second.size(); | 200 entry_count += 1u + extension->second.size(); |
| 230 } | 201 } |
| 231 return entry_count; | 202 return entry_count; |
| 232 } | 203 } |
| 233 | 204 |
| 234 RulesRegistry::~RulesRegistry() { | 205 RulesRegistry::~RulesRegistry() { |
| 235 } | 206 } |
| 236 | 207 |
| 237 void RulesRegistry::MarkReady(base::Time storage_init_time) { | 208 void RulesRegistry::MarkReady(base::Time storage_init_time) { |
| 238 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 209 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 239 | 210 |
| 240 if (!storage_init_time.is_null()) { | 211 if (!storage_init_time.is_null()) { |
| 241 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", | 212 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", |
| 242 base::Time::Now() - storage_init_time); | 213 base::Time::Now() - storage_init_time); |
| 243 } | 214 } |
| 244 | 215 |
| 245 ready_.Signal(); | 216 ready_.Signal(); |
| 246 } | 217 } |
| 247 | 218 |
| 248 void RulesRegistry::DeserializeAndAddRules( | |
| 249 const std::string& extension_id, | |
| 250 scoped_ptr<base::Value> rules) { | |
| 251 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | |
| 252 | |
| 253 AddRules(extension_id, RulesFromValue(rules.get())); | |
| 254 } | |
| 255 | |
| 256 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { | 219 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { |
| 257 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 220 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 258 | 221 |
| 259 process_changed_rules_requested_ = NOT_SCHEDULED_FOR_PROCESSING; | 222 process_changed_rules_requested_ = NOT_SCHEDULED_FOR_PROCESSING; |
| 260 | 223 |
| 261 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; | 224 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; |
| 262 std::string error = GetAllRules(extension_id, &new_rules); | 225 std::string error = GetAllRules(extension_id, &new_rules); |
| 263 DCHECK_EQ(std::string(), error); | 226 DCHECK_EQ(std::string(), error); |
| 264 content::BrowserThread::PostTask( | 227 content::BrowserThread::PostTask( |
| 265 content::BrowserThread::UI, | 228 content::BrowserThread::UI, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 for (i = identifiers.begin(); i != identifiers.end(); ++i) | 308 for (i = identifiers.begin(); i != identifiers.end(); ++i) |
| 346 used_rule_identifiers_[extension_id].erase(*i); | 309 used_rule_identifiers_[extension_id].erase(*i); |
| 347 } | 310 } |
| 348 | 311 |
| 349 void RulesRegistry::RemoveAllUsedRuleIdentifiers( | 312 void RulesRegistry::RemoveAllUsedRuleIdentifiers( |
| 350 const std::string& extension_id) { | 313 const std::string& extension_id) { |
| 351 used_rule_identifiers_.erase(extension_id); | 314 used_rule_identifiers_.erase(extension_id); |
| 352 } | 315 } |
| 353 | 316 |
| 354 } // namespace extensions | 317 } // namespace extensions |
| OLD | NEW |