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