| 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/common/extensions/feature.h" | 5 #include "chrome/common/extensions/feature.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 // TODO(aa): Can we replace all this manual parsing with JSON schema stuff? | 45 // TODO(aa): Can we replace all this manual parsing with JSON schema stuff? |
| 46 | 46 |
| 47 void ParseSet(const DictionaryValue* value, | 47 void ParseSet(const DictionaryValue* value, |
| 48 const std::string& property, | 48 const std::string& property, |
| 49 std::set<std::string>* set) { | 49 std::set<std::string>* set) { |
| 50 ListValue* list_value = NULL; | 50 ListValue* list_value = NULL; |
| 51 if (!value->GetList(property, &list_value)) | 51 if (!value->GetList(property, &list_value)) |
| 52 return; | 52 return; |
| 53 | 53 |
| 54 set->clear(); | |
| 55 for (size_t i = 0; i < list_value->GetSize(); ++i) { | 54 for (size_t i = 0; i < list_value->GetSize(); ++i) { |
| 56 std::string str_val; | 55 std::string str_val; |
| 57 CHECK(list_value->GetString(i, &str_val)) << property << " " << i; | 56 CHECK(list_value->GetString(i, &str_val)) << property << " " << i; |
| 58 set->insert(str_val); | 57 set->insert(str_val); |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 template<typename T> | 61 template<typename T> |
| 63 void ParseEnum(const std::string& string_value, | 62 void ParseEnum(const std::string& string_value, |
| 64 T* enum_value, | 63 T* enum_value, |
| 65 const std::map<std::string, T>& mapping) { | 64 const std::map<std::string, T>& mapping) { |
| 66 typename std::map<std::string, T>::const_iterator iter = | 65 typename std::map<std::string, T>::const_iterator iter = |
| 67 mapping.find(string_value); | 66 mapping.find(string_value); |
| 68 CHECK(iter != mapping.end()) << string_value; | 67 CHECK(iter != mapping.end()) << string_value; |
| 69 *enum_value = iter->second; | 68 *enum_value = iter->second; |
| 70 } | 69 } |
| 71 | 70 |
| 72 template<typename T> | 71 template<typename T> |
| 73 void ParseEnum(const DictionaryValue* value, | 72 void ParseEnum(const DictionaryValue* value, |
| 74 const std::string& property, | 73 const std::string& property, |
| 75 T* enum_value, | 74 T* enum_value, |
| 76 const std::map<std::string, T>& mapping) { | 75 const std::map<std::string, T>& mapping) { |
| 77 std::string string_value; | 76 std::string string_value; |
| 78 if (!value->GetString(property, &string_value)) | 77 if (!value->GetString(property, &string_value)) |
| 79 return; | 78 return; |
| 80 | |
| 81 ParseEnum(string_value, enum_value, mapping); | 79 ParseEnum(string_value, enum_value, mapping); |
| 82 } | 80 } |
| 83 | 81 |
| 84 template<typename T> | 82 template<typename T> |
| 85 void ParseEnumSet(const DictionaryValue* value, | 83 void ParseEnumSet(const DictionaryValue* value, |
| 86 const std::string& property, | 84 const std::string& property, |
| 87 std::set<T>* enum_set, | 85 std::set<T>* enum_set, |
| 88 const std::map<std::string, T>& mapping) { | 86 const std::map<std::string, T>& mapping) { |
| 89 if (!value->HasKey(property)) | |
| 90 return; | |
| 91 | |
| 92 enum_set->clear(); | |
| 93 | |
| 94 std::string property_string; | 87 std::string property_string; |
| 95 if (value->GetString(property, &property_string)) { | 88 if (value->GetString(property, &property_string)) { |
| 96 if (property_string == "all") { | 89 if (property_string == "all") { |
| 97 for (typename std::map<std::string, T>::const_iterator j = | 90 for (typename std::map<std::string, T>::const_iterator j = |
| 98 mapping.begin(); j != mapping.end(); ++j) { | 91 mapping.begin(); j != mapping.end(); ++j) { |
| 99 enum_set->insert(j->second); | 92 enum_set->insert(j->second); |
| 100 } | 93 } |
| 101 } | 94 } |
| 102 return; | 95 return; |
| 103 } | 96 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 116 | 109 |
| 117 namespace extensions { | 110 namespace extensions { |
| 118 | 111 |
| 119 Feature::Feature() | 112 Feature::Feature() |
| 120 : location_(UNSPECIFIED_LOCATION), | 113 : location_(UNSPECIFIED_LOCATION), |
| 121 platform_(UNSPECIFIED_PLATFORM), | 114 platform_(UNSPECIFIED_PLATFORM), |
| 122 min_manifest_version_(0), | 115 min_manifest_version_(0), |
| 123 max_manifest_version_(0) { | 116 max_manifest_version_(0) { |
| 124 } | 117 } |
| 125 | 118 |
| 126 Feature::Feature(const Feature& other) | |
| 127 : whitelist_(other.whitelist_), | |
| 128 extension_types_(other.extension_types_), | |
| 129 contexts_(other.contexts_), | |
| 130 location_(other.location_), | |
| 131 platform_(other.platform_), | |
| 132 min_manifest_version_(other.min_manifest_version_), | |
| 133 max_manifest_version_(other.max_manifest_version_) { | |
| 134 } | |
| 135 | |
| 136 Feature::~Feature() { | 119 Feature::~Feature() { |
| 137 } | 120 } |
| 138 | 121 |
| 139 bool Feature::Equals(const Feature& other) const { | 122 // static |
| 140 return whitelist_ == other.whitelist_ && | 123 scoped_ptr<Feature> Feature::Parse(const DictionaryValue* value) { |
| 141 extension_types_ == other.extension_types_ && | 124 scoped_ptr<Feature> feature(new Feature()); |
| 142 contexts_ == other.contexts_ && | 125 |
| 143 location_ == other.location_ && | 126 ParseSet(value, "whitelist", feature->whitelist()); |
| 144 platform_ == other.platform_ && | 127 ParseEnumSet<Extension::Type>(value, "extension_types", |
| 145 min_manifest_version_ == other.min_manifest_version_ && | 128 feature->extension_types(), |
| 146 max_manifest_version_ == other.max_manifest_version_; | 129 g_mappings.Get().extension_types); |
| 130 ParseEnumSet<Context>(value, "contexts", feature->contexts(), |
| 131 g_mappings.Get().contexts); |
| 132 ParseEnum<Location>(value, "location", &feature->location_, |
| 133 g_mappings.Get().locations); |
| 134 ParseEnum<Platform>(value, "platform", &feature->platform_, |
| 135 g_mappings.Get().platforms); |
| 136 |
| 137 value->GetInteger("min_manifest_version", &feature->min_manifest_version_); |
| 138 value->GetInteger("max_manifest_version", &feature->max_manifest_version_); |
| 139 |
| 140 return feature.Pass(); |
| 147 } | 141 } |
| 148 | 142 |
| 149 // static | 143 // static |
| 150 Feature::Platform Feature::GetCurrentPlatform() { | 144 Feature::Platform Feature::GetCurrentPlatform() { |
| 151 #if defined(OS_CHROMEOS) | 145 #if defined(OS_CHROMEOS) |
| 152 return CHROMEOS_PLATFORM; | 146 return CHROMEOS_PLATFORM; |
| 153 #else | 147 #else |
| 154 return UNSPECIFIED_PLATFORM; | 148 return UNSPECIFIED_PLATFORM; |
| 155 #endif | 149 #endif |
| 156 } | 150 } |
| 157 | 151 |
| 158 // static | 152 // static |
| 159 Feature::Location Feature::ConvertLocation(Extension::Location location) { | 153 Feature::Location Feature::ConvertLocation(Extension::Location location) { |
| 160 if (location == Extension::COMPONENT) | 154 if (location == Extension::COMPONENT) |
| 161 return COMPONENT_LOCATION; | 155 return COMPONENT_LOCATION; |
| 162 else | 156 else |
| 163 return UNSPECIFIED_LOCATION; | 157 return UNSPECIFIED_LOCATION; |
| 164 } | 158 } |
| 165 | 159 |
| 166 void Feature::Parse(const DictionaryValue* value) { | |
| 167 ParseSet(value, "whitelist", &whitelist_); | |
| 168 ParseEnumSet<Extension::Type>(value, "extension_types", &extension_types_, | |
| 169 g_mappings.Get().extension_types); | |
| 170 ParseEnumSet<Context>(value, "contexts", &contexts_, | |
| 171 g_mappings.Get().contexts); | |
| 172 ParseEnum<Location>(value, "location", &location_, | |
| 173 g_mappings.Get().locations); | |
| 174 ParseEnum<Platform>(value, "platform", &platform_, | |
| 175 g_mappings.Get().platforms); | |
| 176 value->GetInteger("min_manifest_version", &min_manifest_version_); | |
| 177 value->GetInteger("max_manifest_version", &max_manifest_version_); | |
| 178 } | |
| 179 | |
| 180 std::string Feature::GetErrorMessage(Feature::Availability result) { | 160 std::string Feature::GetErrorMessage(Feature::Availability result) { |
| 181 switch (result) { | 161 switch (result) { |
| 182 case IS_AVAILABLE: | 162 case IS_AVAILABLE: |
| 183 return ""; | 163 return ""; |
| 184 case NOT_FOUND_IN_WHITELIST: | 164 case NOT_FOUND_IN_WHITELIST: |
| 185 return "Not allowed for specified extension ID."; | 165 return "Not allowed for specified extension ID."; |
| 186 case INVALID_TYPE: | 166 case INVALID_TYPE: |
| 187 return "Not allowed for specified package type (theme, app, etc.)."; | 167 return "Not allowed for specified package type (theme, app, etc.)."; |
| 188 case INVALID_CONTEXT: | 168 case INVALID_CONTEXT: |
| 189 return "Not allowed for specified context type content script, extension " | 169 return "Not allowed for specified context type content script, extension " |
| 190 "page, web page, etc.)."; | 170 "page, web page, etc.)."; |
| 191 case INVALID_LOCATION: | 171 case INVALID_LOCATION: |
| 192 return "Not allowed for specified install location."; | 172 return "Not allowed for specified install location."; |
| 193 case INVALID_PLATFORM: | 173 case INVALID_PLATFORM: |
| 194 return "Not allowed for specified platform."; | 174 return "Not allowed for specified platform."; |
| 195 case INVALID_MIN_MANIFEST_VERSION: | 175 case INVALID_MIN_MANIFEST_VERSION: |
| 196 return base::StringPrintf("Requires manifest version of at least %d.", | 176 return base::StringPrintf("Requires manifest version of at least %d.", |
| 197 min_manifest_version_); | 177 min_manifest_version_); |
| 198 case INVALID_MAX_MANIFEST_VERSION: | 178 case INVALID_MAX_MANIFEST_VERSION: |
| 199 return base::StringPrintf("Requires manifest version of %d or lower.", | 179 return base::StringPrintf("Requires manifest version of %d or lower.", |
| 200 max_manifest_version_); | 180 max_manifest_version_); |
| 201 default: | 181 default: |
| 202 CHECK(false); | 182 CHECK(false); |
| 203 return ""; | 183 return ""; |
| 204 } | 184 } |
| 205 } | 185 } |
| 206 | 186 |
| 207 Feature::Availability Feature::IsAvailableToManifest( | 187 Feature::Availability Feature::IsAvailable(const std::string& extension_id, |
| 208 const std::string& extension_id, | 188 Extension::Type type, |
| 209 Extension::Type type, | 189 Location location, |
| 210 Location location, | 190 Context context, |
| 211 int manifest_version, | 191 Platform platform, |
| 212 Platform platform) const { | 192 int manifest_version) { |
| 213 // Component extensions can access any feature. | 193 // Component extensions can access any feature. |
| 214 if (location == COMPONENT_LOCATION) | 194 if (location == COMPONENT_LOCATION) |
| 215 return IS_AVAILABLE; | 195 return IS_AVAILABLE; |
| 216 | 196 |
| 217 if (!whitelist_.empty()) { | 197 if (!whitelist_.empty()) { |
| 218 if (whitelist_.find(extension_id) == whitelist_.end()) { | 198 if (whitelist_.find(extension_id) == whitelist_.end()) { |
| 219 // TODO(aa): This is gross. There should be a better way to test the | 199 // TODO(aa): This is gross. There should be a better way to test the |
| 220 // whitelist. | 200 // whitelist. |
| 221 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 201 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 222 if (!command_line->HasSwitch(switches::kWhitelistedExtensionID)) | 202 if (!command_line->HasSwitch(switches::kWhitelistedExtensionID)) |
| 223 return NOT_FOUND_IN_WHITELIST; | 203 return NOT_FOUND_IN_WHITELIST; |
| 224 | 204 |
| 225 std::string whitelist_switch_value = | 205 std::string whitelist_switch_value = |
| 226 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 206 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 227 switches::kWhitelistedExtensionID); | 207 switches::kWhitelistedExtensionID); |
| 228 if (extension_id != whitelist_switch_value) | 208 if (extension_id != whitelist_switch_value) |
| 229 return NOT_FOUND_IN_WHITELIST; | 209 return NOT_FOUND_IN_WHITELIST; |
| 230 } | 210 } |
| 231 } | 211 } |
| 232 | 212 |
| 233 if (!extension_types_.empty() && | 213 if (!extension_types_.empty() && |
| 234 extension_types_.find(type) == extension_types_.end()) { | 214 extension_types_.find(type) == extension_types_.end()) { |
| 235 return INVALID_TYPE; | 215 return INVALID_TYPE; |
| 236 } | 216 } |
| 237 | 217 |
| 218 if (!contexts_.empty() && |
| 219 contexts_.find(context) == contexts_.end()) { |
| 220 return INVALID_CONTEXT; |
| 221 } |
| 222 |
| 238 if (location_ != UNSPECIFIED_LOCATION && location_ != location) | 223 if (location_ != UNSPECIFIED_LOCATION && location_ != location) |
| 239 return INVALID_LOCATION; | 224 return INVALID_LOCATION; |
| 240 | 225 |
| 241 if (platform_ != UNSPECIFIED_PLATFORM && platform_ != platform) | 226 if (platform_ != UNSPECIFIED_PLATFORM && platform_ != platform) |
| 242 return INVALID_PLATFORM; | 227 return INVALID_PLATFORM; |
| 243 | 228 |
| 244 if (min_manifest_version_ != 0 && manifest_version < min_manifest_version_) | 229 if (min_manifest_version_ != 0 && manifest_version < min_manifest_version_) |
| 245 return INVALID_MIN_MANIFEST_VERSION; | 230 return INVALID_MIN_MANIFEST_VERSION; |
| 246 | 231 |
| 247 if (max_manifest_version_ != 0 && manifest_version > max_manifest_version_) | 232 if (max_manifest_version_ != 0 && manifest_version > max_manifest_version_) |
| 248 return INVALID_MAX_MANIFEST_VERSION; | 233 return INVALID_MAX_MANIFEST_VERSION; |
| 249 | 234 |
| 250 return IS_AVAILABLE; | 235 return IS_AVAILABLE; |
| 251 } | 236 } |
| 252 | 237 |
| 253 Feature::Availability Feature::IsAvailableToContext( | |
| 254 const Extension* extension, | |
| 255 Feature::Context context, | |
| 256 Feature::Platform platform) const { | |
| 257 Availability result = IsAvailableToManifest( | |
| 258 extension->id(), | |
| 259 extension->GetType(), | |
| 260 ConvertLocation(extension->location()), | |
| 261 extension->manifest_version(), | |
| 262 platform); | |
| 263 if (result != IS_AVAILABLE) | |
| 264 return result; | |
| 265 | |
| 266 if (!contexts_.empty() && | |
| 267 contexts_.find(context) == contexts_.end()) { | |
| 268 return INVALID_CONTEXT; | |
| 269 } | |
| 270 | |
| 271 return IS_AVAILABLE; | |
| 272 } | |
| 273 | |
| 274 } // namespace | 238 } // namespace |
| OLD | NEW |