| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 | 69 |
| 70 namespace extensions { | 70 namespace extensions { |
| 71 | 71 |
| 72 // RulesRegistry | 72 // RulesRegistry |
| 73 | 73 |
| 74 RulesRegistry::RulesRegistry( | 74 RulesRegistry::RulesRegistry( |
| 75 Profile* profile, | 75 Profile* profile, |
| 76 const std::string& event_name, | 76 const std::string& event_name, |
| 77 content::BrowserThread::ID owner_thread, | 77 content::BrowserThread::ID owner_thread, |
| 78 bool log_storage_init_delay, | 78 RulesCacheDelegate* cache_delegate) |
| 79 scoped_ptr<RulesCacheDelegate>* ui_part) | 79 : profile_(profile), |
| 80 : owner_thread_(owner_thread), | 80 owner_thread_(owner_thread), |
| 81 event_name_(event_name), | 81 event_name_(event_name), |
| 82 weak_ptr_factory_(profile ? this : NULL), | 82 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 | 83 process_changed_rules_requested_(profile ? NOT_SCHEDULED_FOR_PROCESSING |
| 91 : NEVER_PROCESS), | 84 : NEVER_PROCESS), |
| 92 last_generated_rule_identifier_id_(0) { | 85 last_generated_rule_identifier_id_(0) { |
| 93 if (!profile) { | 86 if (cache_delegate) { |
| 94 CHECK(!ui_part); | 87 cache_delegate_ = cache_delegate->GetWeakPtr(); |
| 95 return; | 88 cache_delegate->Init(this); |
| 89 } else { |
| 90 ready_.Signal(); |
| 96 } | 91 } |
| 97 | |
| 98 ui_part->reset(cache_delegate_.get()); | |
| 99 | |
| 100 cache_delegate_->Init(); | |
| 101 } | 92 } |
| 102 | 93 |
| 103 std::string RulesRegistry::AddRules( | 94 std::string RulesRegistry::AddRules( |
| 104 const std::string& extension_id, | 95 const std::string& extension_id, |
| 105 const std::vector<linked_ptr<Rule> >& rules) { | 96 const std::vector<linked_ptr<Rule> >& rules) { |
| 106 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 97 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 107 | 98 |
| 108 std::string error = CheckAndFillInOptionalRules(extension_id, rules); | 99 std::string error = CheckAndFillInOptionalRules(extension_id, rules); |
| 109 if (!error.empty()) | 100 if (!error.empty()) |
| 110 return error; | 101 return error; |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 used_rule_identifiers_.begin(); | 215 used_rule_identifiers_.begin(); |
| 225 extension != used_rule_identifiers_.end(); | 216 extension != used_rule_identifiers_.end(); |
| 226 ++extension) { | 217 ++extension) { |
| 227 // Each extension is counted as 1 just for being there. Otherwise we miss | 218 // Each extension is counted as 1 just for being there. Otherwise we miss |
| 228 // keys with empty values. | 219 // keys with empty values. |
| 229 entry_count += 1u + extension->second.size(); | 220 entry_count += 1u + extension->second.size(); |
| 230 } | 221 } |
| 231 return entry_count; | 222 return entry_count; |
| 232 } | 223 } |
| 233 | 224 |
| 225 void RulesRegistry::DeserializeAndAddRules( |
| 226 const std::string& extension_id, |
| 227 scoped_ptr<base::Value> rules) { |
| 228 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 229 |
| 230 AddRules(extension_id, RulesFromValue(rules.get())); |
| 231 } |
| 232 |
| 234 RulesRegistry::~RulesRegistry() { | 233 RulesRegistry::~RulesRegistry() { |
| 235 } | 234 } |
| 236 | 235 |
| 237 void RulesRegistry::MarkReady(base::Time storage_init_time) { | 236 void RulesRegistry::MarkReady(base::Time storage_init_time) { |
| 238 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 237 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 239 | 238 |
| 240 if (!storage_init_time.is_null()) { | 239 if (!storage_init_time.is_null()) { |
| 241 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", | 240 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", |
| 242 base::Time::Now() - storage_init_time); | 241 base::Time::Now() - storage_init_time); |
| 243 } | 242 } |
| 244 | 243 |
| 245 ready_.Signal(); | 244 ready_.Signal(); |
| 246 } | 245 } |
| 247 | 246 |
| 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) { | 247 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { |
| 257 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 248 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 258 | 249 |
| 259 process_changed_rules_requested_ = NOT_SCHEDULED_FOR_PROCESSING; | 250 process_changed_rules_requested_ = NOT_SCHEDULED_FOR_PROCESSING; |
| 260 | 251 |
| 261 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; | 252 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; |
| 262 std::string error = GetAllRules(extension_id, &new_rules); | 253 std::string error = GetAllRules(extension_id, &new_rules); |
| 263 DCHECK_EQ(std::string(), error); | 254 DCHECK_EQ(std::string(), error); |
| 264 content::BrowserThread::PostTask( | 255 content::BrowserThread::PostTask( |
| 265 content::BrowserThread::UI, | 256 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) | 336 for (i = identifiers.begin(); i != identifiers.end(); ++i) |
| 346 used_rule_identifiers_[extension_id].erase(*i); | 337 used_rule_identifiers_[extension_id].erase(*i); |
| 347 } | 338 } |
| 348 | 339 |
| 349 void RulesRegistry::RemoveAllUsedRuleIdentifiers( | 340 void RulesRegistry::RemoveAllUsedRuleIdentifiers( |
| 350 const std::string& extension_id) { | 341 const std::string& extension_id) { |
| 351 used_rule_identifiers_.erase(extension_id); | 342 used_rule_identifiers_.erase(extension_id); |
| 352 } | 343 } |
| 353 | 344 |
| 354 } // namespace extensions | 345 } // namespace extensions |
| OLD | NEW |