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.
5 #ifndef EXTENSIONS_COMMON_FEATURES_FEATURE_H_
6 #define EXTENSIONS_COMMON_FEATURES_FEATURE_H_
11 #include "base/values.h"
12 #include "extensions/common/manifest.h"
16 namespace extensions {
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.
27 // The JavaScript contexts the feature is supported in.
31 // A context in a privileged extension process.
32 BLESSED_EXTENSION_CONTEXT,
34 // A context in an unprivileged extension process.
35 UNBLESSED_EXTENSION_CONTEXT,
37 // A context from a content script.
38 CONTENT_SCRIPT_CONTEXT,
40 // A normal web page. This should have an associated URL matching pattern.
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,
49 // The platforms the feature is supported in.
58 // Whether a feature is available in a given situation or not, and if not,
60 enum AvailabilityResult {
62 NOT_FOUND_IN_WHITELIST,
68 INVALID_MIN_MANIFEST_VERSION,
69 INVALID_MAX_MANIFEST_VERSION,
75 // Container for AvailabiltyResult that also exposes a user-visible error
76 // message in cases where the feature is not available.
79 AvailabilityResult result() const { return result_; }
80 bool is_available() const { return result_ == IS_AVAILABLE; }
81 const std::string& message() const { return message_; }
84 friend class SimpleFeature;
87 // Instances should be created via Feature::CreateAvailability.
88 Availability(AvailabilityResult result, const std::string& message)
89 : result_(result), message_(message) { }
91 const AvailabilityResult result_;
92 const std::string message_;
98 // Used by ChromeV8Context until the feature system is fully functional.
99 // TODO(kalman): This is no longer used by ChromeV8Context, so what is the
100 // comment trying to say?
101 static Availability CreateAvailability(AvailabilityResult result,
102 const std::string& message);
104 const std::string& name() const { return name_; }
105 void set_name(const std::string& name) { name_ = name; }
106 const std::set<std::string>& dependencies() const { return dependencies_; }
107 bool no_parent() const { return no_parent_; }
109 // Gets the platform the code is currently running on.
110 static Platform GetCurrentPlatform();
112 virtual std::set<Context>* GetContexts() = 0;
114 // Tests whether this is an internal API or not.
115 virtual bool IsInternal() const = 0;
117 // Returns True for features excluded from service worker backed contexts.
118 virtual bool IsBlockedInServiceWorker() const = 0;
120 // Returns true if the feature is available to be parsed into a new extension
122 Availability IsAvailableToManifest(const std::string& extension_id,
124 Manifest::Location location,
125 int manifest_version) const {
126 return IsAvailableToManifest(extension_id, type, location, manifest_version,
127 GetCurrentPlatform());
129 virtual Availability IsAvailableToManifest(const std::string& extension_id,
131 Manifest::Location location,
132 int manifest_version,
133 Platform platform) const = 0;
135 // Returns true if the feature is available to |extension|.
136 Availability IsAvailableToExtension(const Extension* extension);
138 // Returns true if the feature is available to be used in the specified
139 // extension and context.
140 Availability IsAvailableToContext(const Extension* extension,
142 const GURL& url) const {
143 return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
145 virtual Availability IsAvailableToContext(const Extension* extension,
148 Platform platform) const = 0;
150 virtual std::string GetAvailabilityMessage(AvailabilityResult result,
153 Context context) const = 0;
155 virtual bool IsIdInBlacklist(const std::string& extension_id) const = 0;
156 virtual bool IsIdInWhitelist(const std::string& extension_id) const = 0;
160 std::set<std::string> dependencies_;
164 } // namespace extensions
166 #endif // EXTENSIONS_COMMON_FEATURES_FEATURE_H_