Chromium Code Reviews| 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_service.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 #include "chrome/browser/extensions/api/declarative/declarative_api_constants.h" | |
| 12 #include "chrome/browser/extensions/api/declarative/rule_identifier.h" | |
| 13 #include "chrome/common/chrome_notification_types.h" | |
| 14 #include "chrome/common/extensions/extension.h" | |
| 15 #include "content/public/browser/notification_details.h" | |
| 16 #include "content/public/browser/notification_source.h" | |
| 17 | |
| 18 namespace keys = extension_declarative_api_constants; | |
| 19 | |
| 20 namespace { | |
| 21 std::string ToId(int identifier) { | |
| 22 return "_" + base::IntToString(identifier) + "_"; | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 namespace extensions { | |
| 27 | |
| 28 RulesRegistryService::RulesRegistryService() | |
| 29 : last_generated_rule_identifier_id_(0) { | |
| 30 } | |
| 31 | |
| 32 RulesRegistryService::~RulesRegistryService() { | |
| 33 STLDeleteContainerPairSecondPointers( | |
| 34 rule_registries_.begin(), rule_registries_.end()); | |
|
not at google - send to devlin
2012/02/02 11:59:16
if you make RulesRegistryMap contain linked_ptrs,
battre
2012/02/02 19:12:36
Done.
| |
| 35 } | |
| 36 | |
| 37 void RulesRegistryService::Init(Profile* profile) { | |
| 38 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 39 content::Source<Profile>(profile)); | |
| 40 | |
| 41 // TODO(battre): Register various rules registries here. | |
| 42 // RegisterRuleRegistry("chrome.net", new FoobarRulesRegistry()); | |
|
not at google - send to devlin
2012/02/02 11:59:16
Yeah, if you make this owned by a Profile or Exten
battre
2012/02/06 16:21:04
Done.
| |
| 43 } | |
| 44 | |
| 45 void RulesRegistryService::RegisterRuleRegistry( | |
| 46 const std::string& event_name, | |
| 47 scoped_ptr<RulesRegistry> rule_registry) { | |
| 48 DCHECK(rule_registries_.find(event_name) == rule_registries_.end()); | |
| 49 rule_registries_[event_name] = rule_registry.release(); | |
| 50 } | |
| 51 | |
| 52 bool RulesRegistryService::IsUniqueId(const RuleIdentifier& id) const { | |
|
Aaron Boodman
2012/02/02 16:28:51
nit: Prefer "ID" (capitalized).
battre
2012/02/02 19:12:36
I did a check of
grep -r "[ ,.>]ID[ ()]" chrome
vs
| |
| 53 return used_rule_identifiers_.find(id) == used_rule_identifiers_.end(); | |
| 54 } | |
| 55 | |
| 56 std::string RulesRegistryService::GenerateUniqueId( | |
| 57 std::string extension_id, std::string event_name) { | |
| 58 while ( | |
| 59 !IsUniqueId( | |
| 60 RuleIdentifier(extension_id, | |
| 61 event_name, | |
| 62 ToId(last_generated_rule_identifier_id_)))) { | |
| 63 ++last_generated_rule_identifier_id_; | |
| 64 } | |
| 65 RuleIdentifier new_id(extension_id, | |
| 66 event_name, | |
| 67 ToId(last_generated_rule_identifier_id_)); | |
| 68 used_rule_identifiers_.insert(new_id); | |
| 69 return new_id.rule_id(); | |
| 70 } | |
| 71 | |
| 72 bool RulesRegistryService::CheckAndFillInOptionalRules( | |
| 73 const std::string& event_name, | |
| 74 const std::string& extension_id, | |
| 75 const std::vector<DictionaryValue*>& rules, | |
| 76 RulesRegistry::SetErrorCallback set_error_callback) { | |
| 77 std::vector<DictionaryValue*>::const_iterator i; | |
| 78 for (i = rules.begin(); i != rules.end(); ++i) { | |
| 79 DictionaryValue* rule = *i; | |
| 80 if (rule->HasKey(keys::kId)) { | |
| 81 std::string id; | |
| 82 CHECK(rule->GetString(keys::kId, &id)); | |
| 83 if (!IsUniqueId(RuleIdentifier(extension_id, event_name, id))) { | |
| 84 set_error_callback.Run("Id " + id + " was used multiple times."); | |
| 85 return false; | |
| 86 } | |
| 87 } else { | |
| 88 std::string id = GenerateUniqueId(extension_id, event_name); | |
| 89 rule->SetString(keys::kId, id); | |
| 90 } | |
| 91 } | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 void RulesRegistryService::FillInOptionalPriorities( | |
| 96 const std::vector<DictionaryValue*>& rules) { | |
| 97 std::vector<DictionaryValue*>::const_iterator i; | |
| 98 for (i = rules.begin(); i != rules.end(); ++i) { | |
| 99 DictionaryValue* rule = *i; | |
| 100 if (!rule->HasKey(keys::kPriority)) | |
| 101 rule->SetInteger(keys::kPriority, 100); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 bool RulesRegistryService::AddRules( | |
| 106 const std::string& event_name, | |
| 107 const std::string& extension_id, | |
| 108 const std::vector<DictionaryValue*>& rules, | |
| 109 RulesRegistry::SetErrorCallback set_error_callback) { | |
| 110 DCHECK(rule_registries_.find(event_name) != rule_registries_.end()); | |
| 111 if (!CheckAndFillInOptionalRules(event_name, extension_id, rules, | |
| 112 set_error_callback)) { | |
| 113 return false; | |
| 114 } | |
| 115 FillInOptionalPriorities(rules); | |
| 116 | |
| 117 return rule_registries_[event_name]->AddRules( | |
| 118 extension_id, rules, set_error_callback); | |
| 119 } | |
| 120 | |
| 121 bool RulesRegistryService::RemoveRules( | |
| 122 const std::string& event_name, | |
| 123 const std::string& extension_id, | |
| 124 const std::vector<std::string>& rule_identifiers, | |
| 125 RulesRegistry::SetErrorCallback set_error_callback) { | |
| 126 DCHECK(rule_registries_.find(event_name) != rule_registries_.end()); | |
| 127 // TODO(battre): Free rule_identifiers_; | |
| 128 return rule_registries_[event_name]->RemoveRules( | |
| 129 extension_id, rule_identifiers, set_error_callback); | |
| 130 } | |
| 131 | |
| 132 void RulesRegistryService::GetRules( | |
| 133 const std::string& event_name, | |
| 134 const std::string& extension_id, | |
| 135 const std::vector<std::string>& rule_identifiers, | |
| 136 std::vector<DictionaryValue*>* out) { | |
| 137 DCHECK(rule_registries_.find(event_name) != rule_registries_.end()); | |
| 138 rule_registries_[event_name]->GetRules(extension_id, rule_identifiers, out); | |
| 139 } | |
| 140 | |
| 141 void RulesRegistryService::OnExtensionUnloaded( | |
| 142 const std::string& extension_id) { | |
| 143 RulesRegistryMap::iterator i; | |
| 144 for (i = rule_registries_.begin(); i != rule_registries_.end(); ++i) | |
| 145 i->second->OnExtensionUnloaded(extension_id); | |
| 146 } | |
| 147 | |
| 148 void RulesRegistryService::Observe( | |
| 149 int type, | |
| 150 const content::NotificationSource& source, | |
| 151 const content::NotificationDetails& details) { | |
| 152 switch (type) { | |
| 153 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | |
| 154 const Extension* extension = | |
| 155 content::Details<UnloadedExtensionInfo>(details)->extension; | |
| 156 OnExtensionUnloaded(extension->id()); | |
| 157 break; | |
| 158 } | |
| 159 default: | |
| 160 NOTREACHED(); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 } // namespace extensions | |
| OLD | NEW |