| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/stl_util.h" | |
| 14 #include "base/strings/stringprintf.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/chrome_notification_types.h" | |
| 18 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" | |
| 19 #include "chrome/browser/extensions/extension_service.h" | |
| 20 #include "chrome/browser/extensions/extension_util.h" | |
| 21 #include "content/public/browser/browser_thread.h" | |
| 22 #include "content/public/browser/notification_details.h" | |
| 23 #include "content/public/browser/notification_source.h" | |
| 24 #include "extensions/browser/extension_prefs.h" | |
| 25 #include "extensions/browser/extension_system.h" | |
| 26 #include "extensions/browser/state_store.h" | |
| 27 #include "extensions/common/extension.h" | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 const char kSuccess[] = ""; | |
| 32 const char kDuplicateRuleId[] = "Duplicate rule ID: %s"; | |
| 33 | |
| 34 scoped_ptr<base::Value> RulesToValue( | |
| 35 const std::vector<linked_ptr<extensions::RulesRegistry::Rule> >& rules) { | |
| 36 scoped_ptr<base::ListValue> list(new base::ListValue()); | |
| 37 for (size_t i = 0; i < rules.size(); ++i) | |
| 38 list->Append(rules[i]->ToValue().release()); | |
| 39 return list.PassAs<base::Value>(); | |
| 40 } | |
| 41 | |
| 42 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue( | |
| 43 const base::Value* value) { | |
| 44 std::vector<linked_ptr<extensions::RulesRegistry::Rule> > rules; | |
| 45 | |
| 46 const base::ListValue* list = NULL; | |
| 47 if (!value || !value->GetAsList(&list)) | |
| 48 return rules; | |
| 49 | |
| 50 rules.reserve(list->GetSize()); | |
| 51 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 52 const base::DictionaryValue* dict = NULL; | |
| 53 if (!list->GetDictionary(i, &dict)) | |
| 54 continue; | |
| 55 linked_ptr<extensions::RulesRegistry::Rule> rule( | |
| 56 new extensions::RulesRegistry::Rule()); | |
| 57 if (extensions::RulesRegistry::Rule::Populate(*dict, rule.get())) | |
| 58 rules.push_back(rule); | |
| 59 } | |
| 60 | |
| 61 return rules; | |
| 62 } | |
| 63 | |
| 64 std::string ToId(int identifier) { | |
| 65 return base::StringPrintf("_%d_", identifier); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 | |
| 71 namespace extensions { | |
| 72 | |
| 73 // RulesRegistry | |
| 74 | |
| 75 RulesRegistry::RulesRegistry(content::BrowserContext* browser_context, | |
| 76 const std::string& event_name, | |
| 77 content::BrowserThread::ID owner_thread, | |
| 78 RulesCacheDelegate* cache_delegate, | |
| 79 const WebViewKey& webview_key) | |
| 80 : browser_context_(browser_context), | |
| 81 owner_thread_(owner_thread), | |
| 82 event_name_(event_name), | |
| 83 webview_key_(webview_key), | |
| 84 ready_(/*signaled=*/!cache_delegate), // Immediately ready if no cache | |
| 85 // delegate to wait for. | |
| 86 weak_ptr_factory_(browser_context_ ? this : NULL), | |
| 87 last_generated_rule_identifier_id_(0) { | |
| 88 if (cache_delegate) { | |
| 89 cache_delegate_ = cache_delegate->GetWeakPtr(); | |
| 90 cache_delegate->Init(this); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 std::string RulesRegistry::AddRulesNoFill( | |
| 95 const std::string& extension_id, | |
| 96 const std::vector<linked_ptr<Rule> >& rules) { | |
| 97 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 98 | |
| 99 // Verify that all rule IDs are new. | |
| 100 for (std::vector<linked_ptr<Rule> >::const_iterator i = | |
| 101 rules.begin(); i != rules.end(); ++i) { | |
| 102 const RuleId& rule_id = *((*i)->id); | |
| 103 // Every rule should have a priority assigned. | |
| 104 DCHECK((*i)->priority); | |
| 105 RulesDictionaryKey key(extension_id, rule_id); | |
| 106 if (rules_.find(key) != rules_.end()) | |
| 107 return base::StringPrintf(kDuplicateRuleId, rule_id.c_str()); | |
| 108 } | |
| 109 | |
| 110 std::string error = AddRulesImpl(extension_id, rules); | |
| 111 | |
| 112 if (!error.empty()) | |
| 113 return error; | |
| 114 | |
| 115 // Commit all rules into |rules_| on success. | |
| 116 for (std::vector<linked_ptr<Rule> >::const_iterator i = | |
| 117 rules.begin(); i != rules.end(); ++i) { | |
| 118 const RuleId& rule_id = *((*i)->id); | |
| 119 RulesDictionaryKey key(extension_id, rule_id); | |
| 120 rules_[key] = *i; | |
| 121 } | |
| 122 | |
| 123 MaybeProcessChangedRules(extension_id); | |
| 124 return kSuccess; | |
| 125 } | |
| 126 | |
| 127 std::string RulesRegistry::AddRules( | |
| 128 const std::string& extension_id, | |
| 129 const std::vector<linked_ptr<Rule> >& rules) { | |
| 130 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 131 | |
| 132 std::string error = CheckAndFillInOptionalRules(extension_id, rules); | |
| 133 if (!error.empty()) | |
| 134 return error; | |
| 135 FillInOptionalPriorities(rules); | |
| 136 | |
| 137 return AddRulesNoFill(extension_id, rules); | |
| 138 } | |
| 139 | |
| 140 std::string RulesRegistry::RemoveRules( | |
| 141 const std::string& extension_id, | |
| 142 const std::vector<std::string>& rule_identifiers) { | |
| 143 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 144 | |
| 145 std::string error = RemoveRulesImpl(extension_id, rule_identifiers); | |
| 146 | |
| 147 if (!error.empty()) | |
| 148 return error; | |
| 149 | |
| 150 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | |
| 151 i != rule_identifiers.end(); | |
| 152 ++i) { | |
| 153 RulesDictionaryKey lookup_key(extension_id, *i); | |
| 154 rules_.erase(lookup_key); | |
| 155 } | |
| 156 | |
| 157 MaybeProcessChangedRules(extension_id); | |
| 158 RemoveUsedRuleIdentifiers(extension_id, rule_identifiers); | |
| 159 return kSuccess; | |
| 160 } | |
| 161 | |
| 162 std::string RulesRegistry::RemoveAllRules(const std::string& extension_id) { | |
| 163 std::string result = RulesRegistry::RemoveAllRulesNoStoreUpdate(extension_id); | |
| 164 MaybeProcessChangedRules(extension_id); // Now update the prefs and store. | |
| 165 return result; | |
| 166 } | |
| 167 | |
| 168 std::string RulesRegistry::RemoveAllRulesNoStoreUpdate( | |
| 169 const std::string& extension_id) { | |
| 170 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 171 | |
| 172 std::string error = RemoveAllRulesImpl(extension_id); | |
| 173 | |
| 174 if (!error.empty()) | |
| 175 return error; | |
| 176 | |
| 177 for (RulesDictionary::const_iterator i = rules_.begin(); | |
| 178 i != rules_.end();) { | |
| 179 const RulesDictionaryKey& key = i->first; | |
| 180 ++i; | |
| 181 if (key.first == extension_id) | |
| 182 rules_.erase(key); | |
| 183 } | |
| 184 | |
| 185 RemoveAllUsedRuleIdentifiers(extension_id); | |
| 186 return kSuccess; | |
| 187 } | |
| 188 | |
| 189 void RulesRegistry::GetRules(const std::string& extension_id, | |
| 190 const std::vector<std::string>& rule_identifiers, | |
| 191 std::vector<linked_ptr<Rule> >* out) { | |
| 192 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 193 | |
| 194 for (std::vector<std::string>::const_iterator i = rule_identifiers.begin(); | |
| 195 i != rule_identifiers.end(); ++i) { | |
| 196 RulesDictionaryKey lookup_key(extension_id, *i); | |
| 197 RulesDictionary::iterator entry = rules_.find(lookup_key); | |
| 198 if (entry != rules_.end()) | |
| 199 out->push_back(entry->second); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 void RulesRegistry::GetAllRules(const std::string& extension_id, | |
| 204 std::vector<linked_ptr<Rule> >* out) { | |
| 205 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 206 | |
| 207 for (RulesDictionary::const_iterator i = rules_.begin(); | |
| 208 i != rules_.end(); ++i) { | |
| 209 const RulesDictionaryKey& key = i->first; | |
| 210 if (key.first == extension_id) | |
| 211 out->push_back(i->second); | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void RulesRegistry::OnExtensionUnloaded(const std::string& extension_id) { | |
| 216 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 217 std::string error = RemoveAllRulesImpl(extension_id); | |
| 218 if (!error.empty()) | |
| 219 LOG(ERROR) << error; | |
| 220 } | |
| 221 | |
| 222 void RulesRegistry::OnExtensionUninstalled(const std::string& extension_id) { | |
| 223 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 224 std::string error = RemoveAllRulesNoStoreUpdate(extension_id); | |
| 225 if (!error.empty()) | |
| 226 LOG(ERROR) << error; | |
| 227 } | |
| 228 | |
| 229 void RulesRegistry::OnExtensionLoaded(const std::string& extension_id) { | |
| 230 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 231 std::vector<linked_ptr<Rule> > rules; | |
| 232 GetAllRules(extension_id, &rules); | |
| 233 std::string error = AddRulesImpl(extension_id, rules); | |
| 234 if (!error.empty()) | |
| 235 LOG(ERROR) << error; | |
| 236 } | |
| 237 | |
| 238 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const { | |
| 239 size_t entry_count = 0u; | |
| 240 for (RuleIdentifiersMap::const_iterator extension = | |
| 241 used_rule_identifiers_.begin(); | |
| 242 extension != used_rule_identifiers_.end(); | |
| 243 ++extension) { | |
| 244 // Each extension is counted as 1 just for being there. Otherwise we miss | |
| 245 // keys with empty values. | |
| 246 entry_count += 1u + extension->second.size(); | |
| 247 } | |
| 248 return entry_count; | |
| 249 } | |
| 250 | |
| 251 void RulesRegistry::DeserializeAndAddRules( | |
| 252 const std::string& extension_id, | |
| 253 scoped_ptr<base::Value> rules) { | |
| 254 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 255 | |
| 256 AddRulesNoFill(extension_id, RulesFromValue(rules.get())); | |
| 257 } | |
| 258 | |
| 259 RulesRegistry::~RulesRegistry() { | |
| 260 } | |
| 261 | |
| 262 void RulesRegistry::MarkReady(base::Time storage_init_time) { | |
| 263 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 264 | |
| 265 if (!storage_init_time.is_null()) { | |
| 266 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization", | |
| 267 base::Time::Now() - storage_init_time); | |
| 268 } | |
| 269 | |
| 270 ready_.Signal(); | |
| 271 } | |
| 272 | |
| 273 void RulesRegistry::ProcessChangedRules(const std::string& extension_id) { | |
| 274 DCHECK_CURRENTLY_ON(owner_thread()); | |
| 275 | |
| 276 DCHECK(ContainsKey(process_changed_rules_requested_, extension_id)); | |
| 277 process_changed_rules_requested_[extension_id] = NOT_SCHEDULED_FOR_PROCESSING; | |
| 278 | |
| 279 std::vector<linked_ptr<Rule> > new_rules; | |
| 280 GetAllRules(extension_id, &new_rules); | |
| 281 content::BrowserThread::PostTask( | |
| 282 content::BrowserThread::UI, | |
| 283 FROM_HERE, | |
| 284 base::Bind(&RulesCacheDelegate::WriteToStorage, | |
| 285 cache_delegate_, | |
| 286 extension_id, | |
| 287 base::Passed(RulesToValue(new_rules)))); | |
| 288 } | |
| 289 | |
| 290 void RulesRegistry::MaybeProcessChangedRules(const std::string& extension_id) { | |
| 291 // Read and initialize |process_changed_rules_requested_[extension_id]| if | |
| 292 // necessary. (Note that the insertion below will not overwrite | |
| 293 // |process_changed_rules_requested_[extension_id]| if that already exists. | |
| 294 std::pair<ProcessStateMap::iterator, bool> insertion = | |
| 295 process_changed_rules_requested_.insert(std::make_pair( | |
| 296 extension_id, | |
| 297 browser_context_ ? NOT_SCHEDULED_FOR_PROCESSING : NEVER_PROCESS)); | |
| 298 if (insertion.first->second != NOT_SCHEDULED_FOR_PROCESSING) | |
| 299 return; | |
| 300 | |
| 301 process_changed_rules_requested_[extension_id] = SCHEDULED_FOR_PROCESSING; | |
| 302 ready_.Post(FROM_HERE, | |
| 303 base::Bind(&RulesRegistry::ProcessChangedRules, | |
| 304 weak_ptr_factory_.GetWeakPtr(), | |
| 305 extension_id)); | |
| 306 } | |
| 307 | |
| 308 bool RulesRegistry::IsUniqueId(const std::string& extension_id, | |
| 309 const std::string& rule_id) const { | |
| 310 RuleIdentifiersMap::const_iterator identifiers = | |
| 311 used_rule_identifiers_.find(extension_id); | |
| 312 if (identifiers == used_rule_identifiers_.end()) | |
| 313 return true; | |
| 314 return identifiers->second.find(rule_id) == identifiers->second.end(); | |
| 315 } | |
| 316 | |
| 317 std::string RulesRegistry::GenerateUniqueId(const std::string& extension_id) { | |
| 318 while (!IsUniqueId(extension_id, ToId(last_generated_rule_identifier_id_))) | |
| 319 ++last_generated_rule_identifier_id_; | |
| 320 return ToId(last_generated_rule_identifier_id_); | |
| 321 } | |
| 322 | |
| 323 std::string RulesRegistry::CheckAndFillInOptionalRules( | |
| 324 const std::string& extension_id, | |
| 325 const std::vector<linked_ptr<Rule> >& rules) { | |
| 326 // IDs we have inserted, in case we need to rollback this operation. | |
| 327 std::vector<std::string> rollback_log; | |
| 328 | |
| 329 // First we insert all rules with existing identifier, so that generated | |
| 330 // identifiers cannot collide with identifiers passed by the caller. | |
| 331 for (std::vector<linked_ptr<Rule> >::const_iterator i = rules.begin(); | |
| 332 i != rules.end(); | |
| 333 ++i) { | |
| 334 Rule* rule = i->get(); | |
| 335 if (rule->id.get()) { | |
| 336 std::string id = *(rule->id); | |
| 337 if (!IsUniqueId(extension_id, id)) { | |
| 338 RemoveUsedRuleIdentifiers(extension_id, rollback_log); | |
| 339 return "Id " + id + " was used multiple times."; | |
| 340 } | |
| 341 used_rule_identifiers_[extension_id].insert(id); | |
| 342 } | |
| 343 } | |
| 344 // Now we generate IDs in case they were not specified in the rules. This | |
| 345 // cannot fail so we do not need to keep track of a rollback log. | |
| 346 for (std::vector<linked_ptr<Rule> >::const_iterator i = rules.begin(); | |
| 347 i != rules.end(); | |
| 348 ++i) { | |
| 349 Rule* rule = i->get(); | |
| 350 if (!rule->id.get()) { | |
| 351 rule->id.reset(new std::string(GenerateUniqueId(extension_id))); | |
| 352 used_rule_identifiers_[extension_id].insert(*(rule->id)); | |
| 353 } | |
| 354 } | |
| 355 return std::string(); | |
| 356 } | |
| 357 | |
| 358 void RulesRegistry::FillInOptionalPriorities( | |
| 359 const std::vector<linked_ptr<Rule> >& rules) { | |
| 360 std::vector<linked_ptr<Rule> >::const_iterator i; | |
| 361 for (i = rules.begin(); i != rules.end(); ++i) { | |
| 362 if (!(*i)->priority.get()) | |
| 363 (*i)->priority.reset(new int(DEFAULT_PRIORITY)); | |
| 364 } | |
| 365 } | |
| 366 | |
| 367 void RulesRegistry::RemoveUsedRuleIdentifiers( | |
| 368 const std::string& extension_id, | |
| 369 const std::vector<std::string>& identifiers) { | |
| 370 std::vector<std::string>::const_iterator i; | |
| 371 for (i = identifiers.begin(); i != identifiers.end(); ++i) | |
| 372 used_rule_identifiers_[extension_id].erase(*i); | |
| 373 } | |
| 374 | |
| 375 void RulesRegistry::RemoveAllUsedRuleIdentifiers( | |
| 376 const std::string& extension_id) { | |
| 377 used_rule_identifiers_.erase(extension_id); | |
| 378 } | |
| 379 | |
| 380 } // namespace extensions | |
| OLD | NEW |