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 // A page within webui. 49 WEBUI_CONTEXT, 50 }; 51 52 // The platforms the feature is supported in. 53 enum Platform { 54 UNSPECIFIED_PLATFORM, 55 CHROMEOS_PLATFORM, 56 LINUX_PLATFORM, 57 MACOSX_PLATFORM, 58 WIN_PLATFORM 59 }; 60 61 // Whether a feature is available in a given situation or not, and if not, 62 // why not. 63 enum AvailabilityResult { 64 IS_AVAILABLE, 65 NOT_FOUND_IN_WHITELIST, 66 INVALID_URL, 67 INVALID_TYPE, 68 INVALID_CONTEXT, 69 INVALID_LOCATION, 70 INVALID_PLATFORM, 71 INVALID_MIN_MANIFEST_VERSION, 72 INVALID_MAX_MANIFEST_VERSION, 73 NOT_PRESENT, 74 UNSUPPORTED_CHANNEL, 75 FOUND_IN_BLACKLIST, 76 }; 77 78 // Container for AvailabiltyResult that also exposes a user-visible error 79 // message in cases where the feature is not available. 80 class Availability { 81 public: 82 AvailabilityResult result() const { return result_; } 83 bool is_available() const { return result_ == IS_AVAILABLE; } 84 const std::string& message() const { return message_; } 85 86 private: 87 friend class SimpleFeature; 88 friend class Feature; 89 90 // Instances should be created via Feature::CreateAvailability. 91 Availability(AvailabilityResult result, const std::string& message) 92 : result_(result), message_(message) { } 93 94 const AvailabilityResult result_; 95 const std::string message_; 96 }; 97 98 Feature(); 99 virtual ~Feature(); 100 101 // Used by ChromeV8Context until the feature system is fully functional. 102 // TODO(kalman): This is no longer used by ChromeV8Context, so what is the 103 // comment trying to say? 104 static Availability CreateAvailability(AvailabilityResult result, 105 const std::string& message); 106 107 const std::string& name() const { return name_; } 108 void set_name(const std::string& name) { name_ = name; } 109 bool no_parent() const { return no_parent_; } 110 111 // Gets the platform the code is currently running on. 112 static Platform GetCurrentPlatform(); 113 114 // Tests whether this is an internal API or not. 115 virtual bool IsInternal() const = 0; 116 117 // Returns true if the feature is available to be parsed into a new extension 118 // manifest. 119 Availability IsAvailableToManifest(const std::string& extension_id, 120 Manifest::Type type, 121 Manifest::Location location, 122 int manifest_version) const { 123 return IsAvailableToManifest(extension_id, type, location, manifest_version, 124 GetCurrentPlatform()); 125 } 126 virtual Availability IsAvailableToManifest(const std::string& extension_id, 127 Manifest::Type type, 128 Manifest::Location location, 129 int manifest_version, 130 Platform platform) const = 0; 131 132 // Returns true if the feature is available to |extension|. 133 Availability IsAvailableToExtension(const Extension* extension); 134 135 // Returns true if the feature is available to be used in the specified 136 // extension and context. 137 Availability IsAvailableToContext(const Extension* extension, 138 Context context, 139 const GURL& url) const { 140 return IsAvailableToContext(extension, context, url, GetCurrentPlatform()); 141 } 142 virtual Availability IsAvailableToContext(const Extension* extension, 143 Context context, 144 const GURL& url, 145 Platform platform) const = 0; 146 147 virtual std::string GetAvailabilityMessage(AvailabilityResult result, 148 Manifest::Type type, 149 const GURL& url, 150 Context context) const = 0; 151 152 virtual bool IsIdInBlacklist(const std::string& extension_id) const = 0; 153 virtual bool IsIdInWhitelist(const std::string& extension_id) const = 0; 154 155 protected: 156 std::string name_; 157 bool no_parent_; 158 }; 159 160 } // namespace extensions 161 162 #endif // EXTENSIONS_COMMON_FEATURES_FEATURE_H_ 163