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