| 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_with_cache.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.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.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( | 39 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( |
| 40 const base::Value* value) { | 40 const base::Value* value) { |
| 41 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules; | 41 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules; |
| 42 | 42 |
| 43 const base::ListValue* list = NULL; | 43 const base::ListValue* list = NULL; |
| 44 if (!value || !value->GetAsList(&list)) | 44 if (!value || !value->GetAsList(&list)) |
| 45 return rules; | 45 return rules; |
| 46 | 46 |
| 47 rules.reserve(list->GetSize()); |
| 47 for (size_t i = 0; i < list->GetSize(); ++i) { | 48 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 48 const base::DictionaryValue* dict = NULL; | 49 const base::DictionaryValue* dict = NULL; |
| 49 if (!list->GetDictionary(i, &dict)) | 50 if (!list->GetDictionary(i, &dict)) |
| 50 continue; | 51 continue; |
| 51 linked_ptr<extensions::RulesRegistry::Rule> rule( | 52 linked_ptr<extensions::RulesRegistry::Rule> rule( |
| 52 new extensions::RulesRegistry::Rule()); | 53 new extensions::RulesRegistry::Rule()); |
| 53 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) | 54 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) |
| 54 rules.push_back(rule); | 55 rules.push_back(rule); |
| 55 } | 56 } |
| 56 | 57 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 73 | 74 |
| 74 // RulesRegistryWithCache | 75 // RulesRegistryWithCache |
| 75 | 76 |
| 76 RulesRegistryWithCache::RulesRegistryWithCache( | 77 RulesRegistryWithCache::RulesRegistryWithCache( |
| 77 Profile* profile, | 78 Profile* profile, |
| 78 const char* event_name, | 79 const char* event_name, |
| 79 content::BrowserThread::ID owner_thread, | 80 content::BrowserThread::ID owner_thread, |
| 80 bool log_storage_init_delay, | 81 bool log_storage_init_delay, |
| 81 scoped_ptr<RuleStorageOnUI>* ui_part) | 82 scoped_ptr<RuleStorageOnUI>* ui_part) |
| 82 : RulesRegistry(owner_thread, event_name), | 83 : RulesRegistry(owner_thread, event_name), |
| 83 weak_ptr_factory_((profile) ? this : NULL), | 84 weak_ptr_factory_(profile ? this : NULL), |
| 84 storage_on_ui_((profile | 85 storage_on_ui_((profile |
| 85 ? (new RuleStorageOnUI(profile, | 86 ? (new RuleStorageOnUI(profile, |
| 86 GetDeclarativeRuleStorageKey( | 87 GetDeclarativeRuleStorageKey( |
| 87 event_name, | 88 event_name, |
| 88 profile->IsOffTheRecord()), | 89 profile->IsOffTheRecord()), |
| 89 owner_thread, | 90 owner_thread, |
| 90 weak_ptr_factory_.GetWeakPtr(), | 91 weak_ptr_factory_.GetWeakPtr(), |
| 91 log_storage_init_delay)) | 92 log_storage_init_delay)) |
| 92 ->GetWeakPtr() | 93 ->GetWeakPtr() |
| 93 : base::WeakPtr<RuleStorageOnUI>())) { | 94 : base::WeakPtr<RuleStorageOnUI>())), |
| 95 process_changed_rules_requested_(profile ? NOT_SCHEDULED_FOR_PROCESSING |
| 96 : NEVER_PROCESS) { |
| 94 if (!profile) { | 97 if (!profile) { |
| 95 CHECK(!ui_part); | 98 CHECK(!ui_part); |
| 96 return; | 99 return; |
| 97 } | 100 } |
| 98 | 101 |
| 99 ui_part->reset(storage_on_ui_.get()); | 102 ui_part->reset(storage_on_ui_.get()); |
| 100 | 103 |
| 101 storage_on_ui_->Init(); | 104 storage_on_ui_->Init(); |
| 102 } | 105 } |
| 103 | 106 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 121 return error; | 124 return error; |
| 122 | 125 |
| 123 // Commit all rules into |rules_| on success. | 126 // Commit all rules into |rules_| on success. |
| 124 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 127 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
| 125 rules.begin(); i != rules.end(); ++i) { | 128 rules.begin(); i != rules.end(); ++i) { |
| 126 const RuleId& rule_id = *((*i)->id); | 129 const RuleId& rule_id = *((*i)->id); |
| 127 RulesDictionaryKey key(extension_id, rule_id); | 130 RulesDictionaryKey key(extension_id, rule_id); |
| 128 rules_[key] = *i; | 131 rules_[key] = *i; |
| 129 } | 132 } |
| 130 | 133 |
| 131 ProcessChangedRules(extension_id); | 134 MaybeProcessChangedRules(extension_id); |
| 132 return kSuccess; | 135 return kSuccess; |
| 133 } | 136 } |
| 134 | 137 |
| 135 std::string RulesRegistryWithCache::RemoveRules( | 138 std::string RulesRegistryWithCache::RemoveRules( |
| 136 const std::string& extension_id, | 139 const std::string& extension_id, |
| 137 const std::vector<std::string>& rule_identifiers) { | 140 const std::vector<std::string>& rule_identifiers) { |
| 138 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 141 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 139 | 142 |
| 140 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 143 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
| 141 | 144 |
| 142 if (!error.empty()) | 145 if (!error.empty()) |
| 143 return error; | 146 return error; |
| 144 | 147 |
| 145 // Commit removal of rules from |rules_| on success. | 148 // Commit removal of rules from |rules_| on success. |
| 146 for (std::vector<std::string>::const_iterator i = | 149 for (std::vector<std::string>::const_iterator i = |
| 147 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { | 150 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
| 148 RulesDictionaryKey lookup_key(extension_id, *i); | 151 RulesDictionaryKey lookup_key(extension_id, *i); |
| 149 rules_.erase(lookup_key); | 152 rules_.erase(lookup_key); |
| 150 } | 153 } |
| 151 | 154 |
| 152 ProcessChangedRules(extension_id); | 155 MaybeProcessChangedRules(extension_id); |
| 153 return kSuccess; | 156 return kSuccess; |
| 154 } | 157 } |
| 155 | 158 |
| 156 std::string RulesRegistryWithCache::RemoveAllRules( | 159 std::string RulesRegistryWithCache::RemoveAllRules( |
| 157 const std::string& extension_id) { | 160 const std::string& extension_id) { |
| 158 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 161 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 159 | 162 |
| 160 std::string error = RemoveAllRulesImpl(extension_id); | 163 std::string error = RemoveAllRulesImpl(extension_id); |
| 161 | 164 |
| 162 if (!error.empty()) | 165 if (!error.empty()) |
| 163 return error; | 166 return error; |
| 164 | 167 |
| 165 // Commit removal of rules from |rules_| on success. | 168 // Commit removal of rules from |rules_| on success. |
| 166 for (RulesDictionary::const_iterator i = rules_.begin(); | 169 for (RulesDictionary::const_iterator i = rules_.begin(); |
| 167 i != rules_.end();) { | 170 i != rules_.end();) { |
| 168 const RulesDictionaryKey& key = i->first; | 171 const RulesDictionaryKey& key = i->first; |
| 169 ++i; | 172 ++i; |
| 170 if (key.first == extension_id) | 173 if (key.first == extension_id) |
| 171 rules_.erase(key); | 174 rules_.erase(key); |
| 172 } | 175 } |
| 173 | 176 |
| 174 ProcessChangedRules(extension_id); | 177 MaybeProcessChangedRules(extension_id); |
| 175 return kSuccess; | 178 return kSuccess; |
| 176 } | 179 } |
| 177 | 180 |
| 178 std::string RulesRegistryWithCache::GetRules( | 181 std::string RulesRegistryWithCache::GetRules( |
| 179 const std::string& extension_id, | 182 const std::string& extension_id, |
| 180 const std::vector<std::string>& rule_identifiers, | 183 const std::vector<std::string>& rule_identifiers, |
| 181 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { | 184 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
| 182 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 185 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 183 | 186 |
| 184 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | 187 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 scoped_ptr<base::Value> rules) { | 235 scoped_ptr<base::Value> rules) { |
| 233 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 236 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 234 | 237 |
| 235 AddRules(extension_id, RulesFromValue(rules.get())); | 238 AddRules(extension_id, RulesFromValue(rules.get())); |
| 236 } | 239 } |
| 237 | 240 |
| 238 void RulesRegistryWithCache::ProcessChangedRules( | 241 void RulesRegistryWithCache::ProcessChangedRules( |
| 239 const std::string& extension_id) { | 242 const std::string& extension_id) { |
| 240 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 243 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 241 | 244 |
| 245 process_changed_rules_requested_ = NOT_SCHEDULED_FOR_PROCESSING; |
| 246 |
| 242 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; | 247 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; |
| 243 std::string error = GetAllRules(extension_id, &new_rules); | 248 std::string error = GetAllRules(extension_id, &new_rules); |
| 244 DCHECK_EQ(std::string(), error); | 249 DCHECK_EQ(std::string(), error); |
| 245 content::BrowserThread::PostTask( | 250 content::BrowserThread::PostTask( |
| 246 content::BrowserThread::UI, | 251 content::BrowserThread::UI, |
| 247 FROM_HERE, | 252 FROM_HERE, |
| 248 base::Bind(&RuleStorageOnUI::WriteToStorage, | 253 base::Bind(&RuleStorageOnUI::WriteToStorage, |
| 249 storage_on_ui_, | 254 storage_on_ui_, |
| 250 extension_id, | 255 extension_id, |
| 251 base::Passed(RulesToValue(new_rules)))); | 256 base::Passed(RulesToValue(new_rules)))); |
| 252 } | 257 } |
| 253 | 258 |
| 259 void RulesRegistryWithCache::MaybeProcessChangedRules( |
| 260 const std::string& extension_id) { |
| 261 if (process_changed_rules_requested_ != NOT_SCHEDULED_FOR_PROCESSING) |
| 262 return; |
| 263 |
| 264 process_changed_rules_requested_ = SCHEDULED_FOR_PROCESSING; |
| 265 ready_.Post(FROM_HERE, |
| 266 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, |
| 267 weak_ptr_factory_.GetWeakPtr(), |
| 268 extension_id)); |
| 269 } |
| 270 |
| 254 // RulesRegistryWithCache::RuleStorageOnUI | 271 // RulesRegistryWithCache::RuleStorageOnUI |
| 255 | 272 |
| 256 const char RulesRegistryWithCache::RuleStorageOnUI::kRulesStoredKey[] = | 273 const char RulesRegistryWithCache::RuleStorageOnUI::kRulesStoredKey[] = |
| 257 "has_declarative_rules"; | 274 "has_declarative_rules"; |
| 258 | 275 |
| 259 RulesRegistryWithCache::RuleStorageOnUI::RuleStorageOnUI( | 276 RulesRegistryWithCache::RuleStorageOnUI::RuleStorageOnUI( |
| 260 Profile* profile, | 277 Profile* profile, |
| 261 const std::string& storage_key, | 278 const std::string& storage_key, |
| 262 content::BrowserThread::ID rules_registry_thread, | 279 content::BrowserThread::ID rules_registry_thread, |
| 263 base::WeakPtr<RulesRegistryWithCache> registry, | 280 base::WeakPtr<RulesRegistryWithCache> registry, |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 void RulesRegistryWithCache::RuleStorageOnUI::SetDeclarativeRulesStored( | 445 void RulesRegistryWithCache::RuleStorageOnUI::SetDeclarativeRulesStored( |
| 429 const std::string& extension_id, | 446 const std::string& extension_id, |
| 430 bool rules_stored) { | 447 bool rules_stored) { |
| 431 CHECK(profile_); | 448 CHECK(profile_); |
| 432 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 449 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 433 extension_prefs->UpdateExtensionPref( | 450 extension_prefs->UpdateExtensionPref( |
| 434 extension_id, kRulesStoredKey, new base::FundamentalValue(rules_stored)); | 451 extension_id, kRulesStoredKey, new base::FundamentalValue(rules_stored)); |
| 435 } | 452 } |
| 436 | 453 |
| 437 } // namespace extensions | 454 } // namespace extensions |
| OLD | NEW |