Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: chrome/common/extensions/features/simple_feature.cc

Issue 12093036: Move Extension Location and Type enums to Manifest, and move InstallWarning to its own file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/features/simple_feature.h" 5 #include "chrome/common/extensions/features/simple_feature.h"
6 6
7 #include <map> 7 #include <map>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/string_util.h" 12 #include "base/string_util.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
15 15
16 using chrome::VersionInfo; 16 using chrome::VersionInfo;
17 using extensions::Extension; 17
18 namespace extensions {
18 19
19 namespace { 20 namespace {
20 21
21 struct Mappings { 22 struct Mappings {
22 Mappings() { 23 Mappings() {
23 extension_types["extension"] = Extension::TYPE_EXTENSION; 24 extension_types["extension"] = Manifest::TYPE_EXTENSION;
24 extension_types["theme"] = Extension::TYPE_THEME; 25 extension_types["theme"] = Manifest::TYPE_THEME;
25 extension_types["packaged_app"] 26 extension_types["packaged_app"] = Manifest::TYPE_LEGACY_PACKAGED_APP;
26 = Extension::TYPE_LEGACY_PACKAGED_APP; 27 extension_types["hosted_app"] = Manifest::TYPE_HOSTED_APP;
27 extension_types["hosted_app"] = Extension::TYPE_HOSTED_APP; 28 extension_types["platform_app"] = Manifest::TYPE_PLATFORM_APP;
28 extension_types["platform_app"] = Extension::TYPE_PLATFORM_APP;
29 29
30 contexts["blessed_extension"] = 30 contexts["blessed_extension"] = Feature::BLESSED_EXTENSION_CONTEXT;
31 extensions::Feature::BLESSED_EXTENSION_CONTEXT; 31 contexts["unblessed_extension"] = Feature::UNBLESSED_EXTENSION_CONTEXT;
32 contexts["unblessed_extension"] = 32 contexts["content_script"] = Feature::CONTENT_SCRIPT_CONTEXT;
33 extensions::Feature::UNBLESSED_EXTENSION_CONTEXT; 33 contexts["web_page"] = Feature::WEB_PAGE_CONTEXT;
34 contexts["content_script"] = extensions::Feature::CONTENT_SCRIPT_CONTEXT;
35 contexts["web_page"] = extensions::Feature::WEB_PAGE_CONTEXT;
36 34
37 locations["component"] = extensions::Feature::COMPONENT_LOCATION; 35 locations["component"] = Feature::COMPONENT_LOCATION;
38 36
39 platforms["chromeos"] = extensions::Feature::CHROMEOS_PLATFORM; 37 platforms["chromeos"] = Feature::CHROMEOS_PLATFORM;
40 38
41 channels["trunk"] = VersionInfo::CHANNEL_UNKNOWN; 39 channels["trunk"] = VersionInfo::CHANNEL_UNKNOWN;
42 channels["canary"] = VersionInfo::CHANNEL_CANARY; 40 channels["canary"] = VersionInfo::CHANNEL_CANARY;
43 channels["dev"] = VersionInfo::CHANNEL_DEV; 41 channels["dev"] = VersionInfo::CHANNEL_DEV;
44 channels["beta"] = VersionInfo::CHANNEL_BETA; 42 channels["beta"] = VersionInfo::CHANNEL_BETA;
45 channels["stable"] = VersionInfo::CHANNEL_STABLE; 43 channels["stable"] = VersionInfo::CHANNEL_STABLE;
46 } 44 }
47 45
48 std::map<std::string, Extension::Type> extension_types; 46 std::map<std::string, Manifest::Type> extension_types;
49 std::map<std::string, extensions::Feature::Context> contexts; 47 std::map<std::string, Feature::Context> contexts;
50 std::map<std::string, extensions::Feature::Location> locations; 48 std::map<std::string, Feature::Location> locations;
51 std::map<std::string, extensions::Feature::Platform> platforms; 49 std::map<std::string, Feature::Platform> platforms;
52 std::map<std::string, VersionInfo::Channel> channels; 50 std::map<std::string, VersionInfo::Channel> channels;
53 }; 51 };
54 52
55 base::LazyInstance<Mappings> g_mappings = LAZY_INSTANCE_INITIALIZER; 53 base::LazyInstance<Mappings> g_mappings = LAZY_INSTANCE_INITIALIZER;
56 54
57 std::string GetChannelName(VersionInfo::Channel channel) { 55 std::string GetChannelName(VersionInfo::Channel channel) {
58 typedef std::map<std::string, VersionInfo::Channel> ChannelsMap; 56 typedef std::map<std::string, VersionInfo::Channel> ChannelsMap;
59 ChannelsMap channels = g_mappings.Get().channels; 57 ChannelsMap channels = g_mappings.Get().channels;
60 for (ChannelsMap::iterator i = channels.begin(); i != channels.end(); ++i) { 58 for (ChannelsMap::iterator i = channels.begin(); i != channels.end(); ++i) {
61 if (i->second == channel) 59 if (i->second == channel)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 ParseSet(value, property, &string_set); 127 ParseSet(value, property, &string_set);
130 for (std::set<std::string>::iterator iter = string_set.begin(); 128 for (std::set<std::string>::iterator iter = string_set.begin();
131 iter != string_set.end(); ++iter) { 129 iter != string_set.end(); ++iter) {
132 T enum_value = static_cast<T>(0); 130 T enum_value = static_cast<T>(0);
133 ParseEnum(*iter, &enum_value, mapping); 131 ParseEnum(*iter, &enum_value, mapping);
134 enum_set->insert(enum_value); 132 enum_set->insert(enum_value);
135 } 133 }
136 } 134 }
137 135
138 // Gets a human-readable name for the given extension type. 136 // Gets a human-readable name for the given extension type.
139 std::string GetDisplayTypeName(Extension::Type type) { 137 std::string GetDisplayTypeName(Manifest::Type type) {
140 switch (type) { 138 switch (type) {
141 case Extension::TYPE_UNKNOWN: 139 case Manifest::TYPE_UNKNOWN:
142 return "unknown"; 140 return "unknown";
143 case Extension::TYPE_EXTENSION: 141 case Manifest::TYPE_EXTENSION:
144 return "extension"; 142 return "extension";
145 case Extension::TYPE_HOSTED_APP: 143 case Manifest::TYPE_HOSTED_APP:
146 return "hosted app"; 144 return "hosted app";
147 case Extension::TYPE_LEGACY_PACKAGED_APP: 145 case Manifest::TYPE_LEGACY_PACKAGED_APP:
148 return "legacy packaged app"; 146 return "legacy packaged app";
149 case Extension::TYPE_PLATFORM_APP: 147 case Manifest::TYPE_PLATFORM_APP:
150 return "packaged app"; 148 return "packaged app";
151 case Extension::TYPE_THEME: 149 case Manifest::TYPE_THEME:
152 return "theme"; 150 return "theme";
153 case Extension::TYPE_USER_SCRIPT: 151 case Manifest::TYPE_USER_SCRIPT:
154 return "user script"; 152 return "user script";
155 } 153 }
156 154
157 NOTREACHED(); 155 NOTREACHED();
158 return ""; 156 return "";
159 } 157 }
160 158
161 } // namespace 159 } // namespace
162 160
163 namespace extensions {
164
165 SimpleFeature::SimpleFeature() 161 SimpleFeature::SimpleFeature()
166 : location_(UNSPECIFIED_LOCATION), 162 : location_(UNSPECIFIED_LOCATION),
167 platform_(UNSPECIFIED_PLATFORM), 163 platform_(UNSPECIFIED_PLATFORM),
168 min_manifest_version_(0), 164 min_manifest_version_(0),
169 max_manifest_version_(0), 165 max_manifest_version_(0),
170 channel_(VersionInfo::CHANNEL_UNKNOWN) { 166 channel_(VersionInfo::CHANNEL_UNKNOWN) {
171 } 167 }
172 168
173 SimpleFeature::SimpleFeature(const SimpleFeature& other) 169 SimpleFeature::SimpleFeature(const SimpleFeature& other)
174 : whitelist_(other.whitelist_), 170 : whitelist_(other.whitelist_),
(...skipping 15 matching lines...) Expand all
190 contexts_ == other.contexts_ && 186 contexts_ == other.contexts_ &&
191 location_ == other.location_ && 187 location_ == other.location_ &&
192 platform_ == other.platform_ && 188 platform_ == other.platform_ &&
193 min_manifest_version_ == other.min_manifest_version_ && 189 min_manifest_version_ == other.min_manifest_version_ &&
194 max_manifest_version_ == other.max_manifest_version_ && 190 max_manifest_version_ == other.max_manifest_version_ &&
195 channel_ == other.channel_; 191 channel_ == other.channel_;
196 } 192 }
197 193
198 void SimpleFeature::Parse(const DictionaryValue* value) { 194 void SimpleFeature::Parse(const DictionaryValue* value) {
199 ParseSet(value, "whitelist", &whitelist_); 195 ParseSet(value, "whitelist", &whitelist_);
200 ParseEnumSet<Extension::Type>(value, "extension_types", &extension_types_, 196 ParseEnumSet<Manifest::Type>(value, "extension_types", &extension_types_,
201 g_mappings.Get().extension_types); 197 g_mappings.Get().extension_types);
202 ParseEnumSet<Context>(value, "contexts", &contexts_, 198 ParseEnumSet<Context>(value, "contexts", &contexts_,
203 g_mappings.Get().contexts); 199 g_mappings.Get().contexts);
204 ParseEnum<Location>(value, "location", &location_, 200 ParseEnum<Location>(value, "location", &location_,
205 g_mappings.Get().locations); 201 g_mappings.Get().locations);
206 ParseEnum<Platform>(value, "platform", &platform_, 202 ParseEnum<Platform>(value, "platform", &platform_,
207 g_mappings.Get().platforms); 203 g_mappings.Get().platforms);
208 value->GetInteger("min_manifest_version", &min_manifest_version_); 204 value->GetInteger("min_manifest_version", &min_manifest_version_);
209 value->GetInteger("max_manifest_version", &max_manifest_version_); 205 value->GetInteger("max_manifest_version", &max_manifest_version_);
210 ParseEnum<VersionInfo::Channel>( 206 ParseEnum<VersionInfo::Channel>(
211 value, "channel", &channel_, 207 value, "channel", &channel_,
212 g_mappings.Get().channels); 208 g_mappings.Get().channels);
213 } 209 }
214 210
215 Feature::Availability SimpleFeature::IsAvailableToManifest( 211 Feature::Availability SimpleFeature::IsAvailableToManifest(
216 const std::string& extension_id, 212 const std::string& extension_id,
217 Extension::Type type, 213 Manifest::Type type,
218 Location location, 214 Location location,
219 int manifest_version, 215 int manifest_version,
220 Platform platform) const { 216 Platform platform) const {
221 // Component extensions can access any feature. 217 // Component extensions can access any feature.
222 if (location == COMPONENT_LOCATION) 218 if (location == COMPONENT_LOCATION)
223 return CreateAvailability(IS_AVAILABLE, type); 219 return CreateAvailability(IS_AVAILABLE, type);
224 220
225 if (!whitelist_.empty()) { 221 if (!whitelist_.empty()) {
226 if (whitelist_.find(extension_id) == whitelist_.end()) { 222 if (whitelist_.find(extension_id) == whitelist_.end()) {
227 // TODO(aa): This is gross. There should be a better way to test the 223 // TODO(aa): This is gross. There should be a better way to test the
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 272
277 if (!contexts_.empty() && 273 if (!contexts_.empty() &&
278 contexts_.find(context) == contexts_.end()) { 274 contexts_.find(context) == contexts_.end()) {
279 return CreateAvailability(INVALID_CONTEXT, extension->GetType()); 275 return CreateAvailability(INVALID_CONTEXT, extension->GetType());
280 } 276 }
281 277
282 return CreateAvailability(IS_AVAILABLE); 278 return CreateAvailability(IS_AVAILABLE);
283 } 279 }
284 280
285 std::string SimpleFeature::GetAvailabilityMessage( 281 std::string SimpleFeature::GetAvailabilityMessage(
286 AvailabilityResult result, Extension::Type type) const { 282 AvailabilityResult result, Manifest::Type type) const {
287 switch (result) { 283 switch (result) {
288 case IS_AVAILABLE: 284 case IS_AVAILABLE:
289 return ""; 285 return "";
290 case NOT_FOUND_IN_WHITELIST: 286 case NOT_FOUND_IN_WHITELIST:
291 return base::StringPrintf( 287 return base::StringPrintf(
292 "'%s' is not allowed for specified extension ID.", 288 "'%s' is not allowed for specified extension ID.",
293 name().c_str()); 289 name().c_str());
294 case INVALID_TYPE: { 290 case INVALID_TYPE: {
295 std::string allowed_type_names; 291 std::string allowed_type_names;
296 // Turn the set of allowed types into a vector so that it's easier to 292 // Turn the set of allowed types into a vector so that it's easier to
297 // inject the appropriate separator into the display string. 293 // inject the appropriate separator into the display string.
298 std::vector<Extension::Type> extension_types( 294 std::vector<Manifest::Type> extension_types(
299 extension_types_.begin(), extension_types_.end()); 295 extension_types_.begin(), extension_types_.end());
300 for (size_t i = 0; i < extension_types.size(); i++) { 296 for (size_t i = 0; i < extension_types.size(); i++) {
301 // Pluralize type name. 297 // Pluralize type name.
302 allowed_type_names += GetDisplayTypeName(extension_types[i]) + "s"; 298 allowed_type_names += GetDisplayTypeName(extension_types[i]) + "s";
303 if (i == extension_types_.size() - 2) { 299 if (i == extension_types_.size() - 2) {
304 allowed_type_names += " and "; 300 allowed_type_names += " and ";
305 } else if (i != extension_types_.size() - 1) { 301 } else if (i != extension_types_.size() - 1) {
306 allowed_type_names += ", "; 302 allowed_type_names += ", ";
307 } 303 }
308 } 304 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 GetChannelName(GetCurrentChannel()).c_str()); 345 GetChannelName(GetCurrentChannel()).c_str());
350 } 346 }
351 347
352 NOTREACHED(); 348 NOTREACHED();
353 return ""; 349 return "";
354 } 350 }
355 351
356 Feature::Availability SimpleFeature::CreateAvailability( 352 Feature::Availability SimpleFeature::CreateAvailability(
357 AvailabilityResult result) const { 353 AvailabilityResult result) const {
358 return Availability( 354 return Availability(
359 result, GetAvailabilityMessage(result, Extension::TYPE_UNKNOWN)); 355 result, GetAvailabilityMessage(result, Manifest::TYPE_UNKNOWN));
360 } 356 }
361 357
362 Feature::Availability SimpleFeature::CreateAvailability( 358 Feature::Availability SimpleFeature::CreateAvailability(
363 AvailabilityResult result, Extension::Type type) const { 359 AvailabilityResult result, Manifest::Type type) const {
364 return Availability(result, GetAvailabilityMessage(result, type)); 360 return Availability(result, GetAvailabilityMessage(result, type));
365 } 361 }
366 362
367 std::set<Feature::Context>* SimpleFeature::GetContexts() { 363 std::set<Feature::Context>* SimpleFeature::GetContexts() {
368 return &contexts_; 364 return &contexts_;
369 } 365 }
370 366
371 } // namespace extensions 367 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/features/simple_feature.h ('k') | chrome/common/extensions/features/simple_feature_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698