| 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()); | |
| 48 for (size_t i = 0; i < list->GetSize(); ++i) { | 47 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 49 const base::DictionaryValue* dict = NULL; | 48 const base::DictionaryValue* dict = NULL; |
| 50 if (!list->GetDictionary(i, &dict)) | 49 if (!list->GetDictionary(i, &dict)) |
| 51 continue; | 50 continue; |
| 52 linked_ptr<extensions::RulesRegistry::Rule> rule( | 51 linked_ptr<extensions::RulesRegistry::Rule> rule( |
| 53 new extensions::RulesRegistry::Rule()); | 52 new extensions::RulesRegistry::Rule()); |
| 54 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) | 53 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) |
| 55 rules.push_back(rule); | 54 rules.push_back(rule); |
| 56 } | 55 } |
| 57 | 56 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 74 | 73 |
| 75 // RulesRegistryWithCache | 74 // RulesRegistryWithCache |
| 76 | 75 |
| 77 RulesRegistryWithCache::RulesRegistryWithCache( | 76 RulesRegistryWithCache::RulesRegistryWithCache( |
| 78 Profile* profile, | 77 Profile* profile, |
| 79 const char* event_name, | 78 const char* event_name, |
| 80 content::BrowserThread::ID owner_thread, | 79 content::BrowserThread::ID owner_thread, |
| 81 bool log_storage_init_delay, | 80 bool log_storage_init_delay, |
| 82 scoped_ptr<RuleStorageOnUI>* ui_part) | 81 scoped_ptr<RuleStorageOnUI>* ui_part) |
| 83 : RulesRegistry(owner_thread, event_name), | 82 : RulesRegistry(owner_thread, event_name), |
| 84 weak_ptr_factory_(this), | 83 weak_ptr_factory_((profile) ? this : NULL), |
| 85 storage_on_ui_((profile | 84 storage_on_ui_((profile |
| 86 ? (new RuleStorageOnUI(profile, | 85 ? (new RuleStorageOnUI(profile, |
| 87 GetDeclarativeRuleStorageKey( | 86 GetDeclarativeRuleStorageKey( |
| 88 event_name, | 87 event_name, |
| 89 profile->IsOffTheRecord()), | 88 profile->IsOffTheRecord()), |
| 90 owner_thread, | 89 owner_thread, |
| 91 weak_ptr_factory_.GetWeakPtr(), | 90 weak_ptr_factory_.GetWeakPtr(), |
| 92 log_storage_init_delay)) | 91 log_storage_init_delay)) |
| 93 ->GetWeakPtr() | 92 ->GetWeakPtr() |
| 94 : base::WeakPtr<RuleStorageOnUI>())), | 93 : base::WeakPtr<RuleStorageOnUI>())) { |
| 95 process_changed_rules_requested_(false) { | |
| 96 if (!profile) { | 94 if (!profile) { |
| 97 CHECK(!ui_part); | 95 CHECK(!ui_part); |
| 98 return; | 96 return; |
| 99 } | 97 } |
| 100 | 98 |
| 101 ui_part->reset(storage_on_ui_.get()); | 99 ui_part->reset(storage_on_ui_.get()); |
| 102 | 100 |
| 103 // After construction, |this| continues to live only on |owner_thread|. | 101 // After construction, |this| continues to live only on |owner_thread|. |
| 104 weak_ptr_factory_.DetachFromThread(); | 102 weak_ptr_factory_.DetachFromThread(); |
| 105 | 103 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 126 return error; | 124 return error; |
| 127 | 125 |
| 128 // Commit all rules into |rules_| on success. | 126 // Commit all rules into |rules_| on success. |
| 129 for (std::vector<linked_ptr<Rule> >::const_iterator i = | 127 for (std::vector<linked_ptr<Rule> >::const_iterator i = |
| 130 rules.begin(); i != rules.end(); ++i) { | 128 rules.begin(); i != rules.end(); ++i) { |
| 131 const RuleId& rule_id = *((*i)->id); | 129 const RuleId& rule_id = *((*i)->id); |
| 132 RulesDictionaryKey key(extension_id, rule_id); | 130 RulesDictionaryKey key(extension_id, rule_id); |
| 133 rules_[key] = *i; | 131 rules_[key] = *i; |
| 134 } | 132 } |
| 135 | 133 |
| 136 if (!process_changed_rules_requested_) { | 134 ProcessChangedRules(extension_id); |
| 137 process_changed_rules_requested_ = true; | |
| 138 ready_.Post(FROM_HERE, | |
| 139 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, | |
| 140 weak_ptr_factory_.GetWeakPtr(), | |
| 141 extension_id)); | |
| 142 } | |
| 143 return kSuccess; | 135 return kSuccess; |
| 144 } | 136 } |
| 145 | 137 |
| 146 std::string RulesRegistryWithCache::RemoveRules( | 138 std::string RulesRegistryWithCache::RemoveRules( |
| 147 const std::string& extension_id, | 139 const std::string& extension_id, |
| 148 const std::vector<std::string>& rule_identifiers) { | 140 const std::vector<std::string>& rule_identifiers) { |
| 149 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 141 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 150 | 142 |
| 151 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | 143 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); |
| 152 | 144 |
| 153 if (!error.empty()) | 145 if (!error.empty()) |
| 154 return error; | 146 return error; |
| 155 | 147 |
| 156 // Commit removal of rules from |rules_| on success. | 148 // Commit removal of rules from |rules_| on success. |
| 157 for (std::vector<std::string>::const_iterator i = | 149 for (std::vector<std::string>::const_iterator i = |
| 158 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { | 150 rule_identifiers.begin(); i != rule_identifiers.end(); ++i) { |
| 159 RulesDictionaryKey lookup_key(extension_id, *i); | 151 RulesDictionaryKey lookup_key(extension_id, *i); |
| 160 rules_.erase(lookup_key); | 152 rules_.erase(lookup_key); |
| 161 } | 153 } |
| 162 | 154 |
| 163 if (!process_changed_rules_requested_) { | 155 ProcessChangedRules(extension_id); |
| 164 process_changed_rules_requested_ = true; | |
| 165 ready_.Post(FROM_HERE, | |
| 166 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, | |
| 167 weak_ptr_factory_.GetWeakPtr(), | |
| 168 extension_id)); | |
| 169 } | |
| 170 return kSuccess; | 156 return kSuccess; |
| 171 } | 157 } |
| 172 | 158 |
| 173 std::string RulesRegistryWithCache::RemoveAllRules( | 159 std::string RulesRegistryWithCache::RemoveAllRules( |
| 174 const std::string& extension_id) { | 160 const std::string& extension_id) { |
| 175 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 161 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 176 | 162 |
| 177 std::string error = RemoveAllRulesImpl(extension_id); | 163 std::string error = RemoveAllRulesImpl(extension_id); |
| 178 | 164 |
| 179 if (!error.empty()) | 165 if (!error.empty()) |
| 180 return error; | 166 return error; |
| 181 | 167 |
| 182 // Commit removal of rules from |rules_| on success. | 168 // Commit removal of rules from |rules_| on success. |
| 183 for (RulesDictionary::const_iterator i = rules_.begin(); | 169 for (RulesDictionary::const_iterator i = rules_.begin(); |
| 184 i != rules_.end();) { | 170 i != rules_.end();) { |
| 185 const RulesDictionaryKey& key = i->first; | 171 const RulesDictionaryKey& key = i->first; |
| 186 ++i; | 172 ++i; |
| 187 if (key.first == extension_id) | 173 if (key.first == extension_id) |
| 188 rules_.erase(key); | 174 rules_.erase(key); |
| 189 } | 175 } |
| 190 | 176 |
| 191 if (!process_changed_rules_requested_) { | 177 ProcessChangedRules(extension_id); |
| 192 process_changed_rules_requested_ = true; | |
| 193 ready_.Post(FROM_HERE, | |
| 194 base::Bind(&RulesRegistryWithCache::ProcessChangedRules, | |
| 195 weak_ptr_factory_.GetWeakPtr(), | |
| 196 extension_id)); | |
| 197 } | |
| 198 return kSuccess; | 178 return kSuccess; |
| 199 } | 179 } |
| 200 | 180 |
| 201 std::string RulesRegistryWithCache::GetRules( | 181 std::string RulesRegistryWithCache::GetRules( |
| 202 const std::string& extension_id, | 182 const std::string& extension_id, |
| 203 const std::vector<std::string>& rule_identifiers, | 183 const std::vector<std::string>& rule_identifiers, |
| 204 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { | 184 std::vector<linked_ptr<RulesRegistry::Rule> >* out) { |
| 205 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 185 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 206 | 186 |
| 207 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... |
| 255 scoped_ptr<base::Value> rules) { | 235 scoped_ptr<base::Value> rules) { |
| 256 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 236 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 257 | 237 |
| 258 AddRules(extension_id, RulesFromValue(rules.get())); | 238 AddRules(extension_id, RulesFromValue(rules.get())); |
| 259 } | 239 } |
| 260 | 240 |
| 261 void RulesRegistryWithCache::ProcessChangedRules( | 241 void RulesRegistryWithCache::ProcessChangedRules( |
| 262 const std::string& extension_id) { | 242 const std::string& extension_id) { |
| 263 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); | 243 DCHECK(content::BrowserThread::CurrentlyOn(owner_thread())); |
| 264 | 244 |
| 265 process_changed_rules_requested_ = false; | |
| 266 | |
| 267 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; | 245 std::vector<linked_ptr<RulesRegistry::Rule> > new_rules; |
| 268 std::string error = GetAllRules(extension_id, &new_rules); | 246 std::string error = GetAllRules(extension_id, &new_rules); |
| 269 DCHECK_EQ(std::string(), error); | 247 DCHECK_EQ(std::string(), error); |
| 270 content::BrowserThread::PostTask( | 248 content::BrowserThread::PostTask( |
| 271 content::BrowserThread::UI, | 249 content::BrowserThread::UI, |
| 272 FROM_HERE, | 250 FROM_HERE, |
| 273 base::Bind(&RuleStorageOnUI::WriteToStorage, | 251 base::Bind(&RuleStorageOnUI::WriteToStorage, |
| 274 storage_on_ui_, | 252 storage_on_ui_, |
| 275 extension_id, | 253 extension_id, |
| 276 base::Passed(RulesToValue(new_rules)))); | 254 base::Passed(RulesToValue(new_rules)))); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 void RulesRegistryWithCache::RuleStorageOnUI::SetDeclarativeRulesStored( | 431 void RulesRegistryWithCache::RuleStorageOnUI::SetDeclarativeRulesStored( |
| 454 const std::string& extension_id, | 432 const std::string& extension_id, |
| 455 bool rules_stored) { | 433 bool rules_stored) { |
| 456 CHECK(profile_); | 434 CHECK(profile_); |
| 457 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); | 435 ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 458 extension_prefs->UpdateExtensionPref( | 436 extension_prefs->UpdateExtensionPref( |
| 459 extension_id, kRulesStoredKey, new base::FundamentalValue(rules_stored)); | 437 extension_id, kRulesStoredKey, new base::FundamentalValue(rules_stored)); |
| 460 } | 438 } |
| 461 | 439 |
| 462 } // namespace extensions | 440 } // namespace extensions |
| OLD | NEW |