1 // Copyright 2013 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 #ifndef EXTENSIONS_COMMON_FEATURES_FEATURE_H_ 6 #define EXTENSIONS_COMMON_FEATURES_FEATURE_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/values.h" 12 #include "extensions/common/manifest.h" 13 14 class GURL; 15 16 namespace extensions { 17 18 class Extension; 19 20 // Represents a single feature accessible to an extension developer, such as a 21 // top-level manifest key, a permission, or a programmatic API. A feature can 22 // express requirements for where it can be accessed, and supports testing 23 // support for those requirements. If platforms are not specified, then feature 24 // is available on all platforms. 25 class Feature { 26 public: 27 // The JavaScript contexts the feature is supported in. 28 enum Context { 29 UNSPECIFIED_CONTEXT, 30 31 // A context in a privileged extension process. 32 BLESSED_EXTENSION_CONTEXT, 33 34 // A context in an unprivileged extension process. 35 UNBLESSED_EXTENSION_CONTEXT, 36 37 // A context from a content script. 38 CONTENT_SCRIPT_CONTEXT, 39 40 // A normal web page. This should have an associated URL matching pattern. 41 WEB_PAGE_CONTEXT, 42 43 // A web page context which has been blessed by the user. Typically this 44 // will be via the installation of a hosted app, so this may host an 45 // extension. This is not affected by the URL matching pattern. 46 BLESSED_WEB_PAGE_CONTEXT, 47 }; 48 49 // The location required of extensions the feature is supported in. 50 enum Location { 51 UNSPECIFIED_LOCATION, 52 COMPONENT_LOCATION 53 }; 54 55 // The platforms the feature is supported in. 56 enum Platform { 57 UNSPECIFIED_PLATFORM, 58 CHROMEOS_PLATFORM, 59 LINUX_PLATFORM, 60 MACOSX_PLATFORM, 61 WIN_PLATFORM 62 }; 63 64 // Whether a feature is available in a given situation or not, and if not, 65 // why not. 66 enum AvailabilityResult { 67 IS_AVAILABLE, 68 NOT_FOUND_IN_WHITELIST, 69 INVALID_URL, 70 INVALID_TYPE, 71 INVALID_CONTEXT, 72 INVALID_LOCATION, 73 INVALID_PLATFORM, 74 INVALID_MIN_MANIFEST_VERSION, 75 INVALID_MAX_MANIFEST_VERSION, 76 NOT_PRESENT, 77 UNSUPPORTED_CHANNEL, 78 }; 79 80 // Container for AvailabiltyResult that also exposes a user-visible error 81 // message in cases where the feature is not available. 82 class Availability { 83 public: result()84 AvailabilityResult result() const { return result_; } is_available()85 bool is_available() const { return result_ == IS_AVAILABLE; } message()86 const std::string& message() const { return message_; } 87 88 private: 89 friend class SimpleFeature; 90 friend class Feature; 91 92 // Instances should be created via Feature::CreateAvailability. Availability(AvailabilityResult result,const std::string & message)93 Availability(AvailabilityResult result, const std::string& message) 94 : result_(result), message_(message) { } 95 96 const AvailabilityResult result_; 97 const std::string message_; 98 }; 99 100 Feature(); 101 virtual ~Feature(); 102 103 // Used by ChromeV8Context until the feature system is fully functional. 104 static Availability CreateAvailability(AvailabilityResult result, 105 const std::string& message); 106 name()107 const std::string& name() const { return name_; } set_name(const std::string & name)108 void set_name(const std::string& name) { name_ = name; } dependencies()109 const std::set<std::string>& dependencies() { return dependencies_; } no_parent()110 bool no_parent() const { return no_parent_; } 111 112 // Gets the platform the code is currently running on. 113 static Platform GetCurrentPlatform(); 114 115 // Gets the Feature::Location value for the specified Manifest::Location. 116 static Location ConvertLocation(Manifest::Location extension_location); 117 118 virtual std::set<Context>* GetContexts() = 0; 119 120 // Tests whether this is an internal API or not. 121 virtual bool IsInternal() const = 0; 122 123 // Returns true if the feature is available to be parsed into a new extension 124 // manifest. IsAvailableToManifest(const std::string & extension_id,Manifest::Type type,Location location,int manifest_version)125 Availability IsAvailableToManifest(const std::string& extension_id, 126 Manifest::Type type, 127 Location location, 128 int manifest_version) const { 129 return IsAvailableToManifest(extension_id, type, location, manifest_version, 130 GetCurrentPlatform()); 131 } 132 virtual Availability IsAvailableToManifest(const std::string& extension_id, 133 Manifest::Type type, 134 Location location, 135 int manifest_version, 136 Platform platform) const = 0; 137 138 // Returns true if the feature is available to be used in the specified 139 // extension and context. IsAvailableToContext(const Extension * extension,Context context,const GURL & url)140 Availability IsAvailableToContext(const Extension* extension, 141 Context context, 142 const GURL& url) const { 143 return IsAvailableToContext(extension, context, url, GetCurrentPlatform()); 144 } 145 virtual Availability IsAvailableToContext(const Extension* extension, 146 Context context, 147 const GURL& url, 148 Platform platform) const = 0; 149 150 virtual std::string GetAvailabilityMessage(AvailabilityResult result, 151 Manifest::Type type, 152 const GURL& url, 153 Context context) const = 0; 154 155 virtual bool IsIdInWhitelist(const std::string& extension_id) const = 0; 156 157 protected: 158 std::string name_; 159 std::set<std::string> dependencies_; 160 bool no_parent_; 161 }; 162 163 } // namespace extensions 164 165 #endif // EXTENSIONS_COMMON_FEATURES_FEATURE_H_ 166