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_SIMPLE_FEATURE_H_
      6 #define CHROME_COMMON_EXTENSIONS_FEATURES_SIMPLE_FEATURE_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/gtest_prod_util.h"
     12 #include "base/values.h"
     13 #include "chrome/common/chrome_version_info.h"
     14 #include "extensions/common/extension.h"
     15 #include "extensions/common/features/feature.h"
     16 #include "extensions/common/manifest.h"
     17 
     18 namespace extensions {
     19 
     20 class ComplexFeature;
     21 
     22 class SimpleFeature : public Feature {
     23  public:
     24   SimpleFeature();
     25   SimpleFeature(const SimpleFeature& other);
     26   virtual ~SimpleFeature();
     27 
     28   std::set<std::string>* whitelist() { return &whitelist_; }
     29   std::set<Manifest::Type>* extension_types() { return &extension_types_; }
     30 
     31   // Parses the JSON representation of a feature into the fields of this object.
     32   // Unspecified values in the JSON are not modified in the object. This allows
     33   // us to implement inheritance by parsing one value after another. Returns
     34   // the error found, or an empty string on success.
     35   virtual std::string Parse(const base::DictionaryValue* value);
     36 
     37   // Returns true if the feature contains the same values as another.
     38   bool Equals(const SimpleFeature& other) const;
     39 
     40   Location location() const { return location_; }
     41   void set_location(Location location) { location_ = location; }
     42 
     43   std::set<Platform>* platforms() { return &platforms_; }
     44 
     45   int min_manifest_version() const { return min_manifest_version_; }
     46   void set_min_manifest_version(int min_manifest_version) {
     47     min_manifest_version_ = min_manifest_version;
     48   }
     49 
     50   int max_manifest_version() const { return max_manifest_version_; }
     51   void set_max_manifest_version(int max_manifest_version) {
     52     max_manifest_version_ = max_manifest_version;
     53   }
     54 
     55   Availability IsAvailableToContext(const Extension* extension,
     56                                     Context context) const {
     57     return IsAvailableToContext(extension, context, GURL());
     58   }
     59   Availability IsAvailableToContext(const Extension* extension,
     60                                     Context context,
     61                                     Platform platform) const {
     62     return IsAvailableToContext(extension, context, GURL(), platform);
     63   }
     64   Availability IsAvailableToContext(const Extension* extension,
     65                                     Context context,
     66                                     const GURL& url) const {
     67     return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
     68   }
     69 
     70   // extension::Feature:
     71   virtual Availability IsAvailableToManifest(const std::string& extension_id,
     72                                              Manifest::Type type,
     73                                              Location location,
     74                                              int manifest_version,
     75                                              Platform platform) const OVERRIDE;
     76 
     77   virtual Availability IsAvailableToContext(const Extension* extension,
     78                                             Context context,
     79                                             const GURL& url,
     80                                             Platform platform) const OVERRIDE;
     81 
     82   virtual std::string GetAvailabilityMessage(AvailabilityResult result,
     83                                              Manifest::Type type,
     84                                              const GURL& url,
     85                                              Context context) const OVERRIDE;
     86 
     87   virtual std::set<Context>* GetContexts() OVERRIDE;
     88 
     89   virtual bool IsInternal() const OVERRIDE;
     90 
     91   virtual bool IsIdInWhitelist(const std::string& extension_id) const OVERRIDE;
     92   static bool IsIdInWhitelist(const std::string& extension_id,
     93                               const std::set<std::string>& whitelist);
     94 
     95  protected:
     96   Availability CreateAvailability(AvailabilityResult result) const;
     97   Availability CreateAvailability(AvailabilityResult result,
     98                                   Manifest::Type type) const;
     99   Availability CreateAvailability(AvailabilityResult result,
    100                                   const GURL& url) const;
    101   Availability CreateAvailability(AvailabilityResult result,
    102                                   Context context) const;
    103 
    104  private:
    105   // For clarity and consistency, we handle the default value of each of these
    106   // members the same way: it matches everything. It is up to the higher level
    107   // code that reads Features out of static data to validate that data and set
    108   // sensible defaults.
    109   std::set<std::string> whitelist_;
    110   std::set<Manifest::Type> extension_types_;
    111   std::set<Context> contexts_;
    112   URLPatternSet matches_;
    113   Location location_;  // we only care about component/not-component now
    114   std::set<Platform> platforms_;
    115   int min_manifest_version_;
    116   int max_manifest_version_;
    117   chrome::VersionInfo::Channel channel_;
    118   bool has_parent_;
    119   bool channel_has_been_set_;
    120 
    121   FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Context);
    122 };
    123 
    124 }  // namespace extensions
    125 
    126 #endif  // CHROME_COMMON_EXTENSIONS_FEATURES_SIMPLE_FEATURE_H_
    127