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 "chrome/common/extensions/extension.h"
     15 #include "chrome/common/extensions/features/feature.h"
     16 #include "chrome/common/extensions/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   Platform platform() const { return platform_; }
     44   void set_platform(Platform platform) { platform_ = platform; }
     45 
     46   int min_manifest_version() const { return min_manifest_version_; }
     47   void set_min_manifest_version(int min_manifest_version) {
     48     min_manifest_version_ = min_manifest_version;
     49   }
     50 
     51   int max_manifest_version() const { return max_manifest_version_; }
     52   void set_max_manifest_version(int max_manifest_version) {
     53     max_manifest_version_ = max_manifest_version;
     54   }
     55 
     56   Availability IsAvailableToContext(const Extension* extension,
     57                                     Context context) const {
     58     return IsAvailableToContext(extension, context, GURL());
     59   }
     60   Availability IsAvailableToContext(const Extension* extension,
     61                                     Context context,
     62                                     Platform platform) const {
     63     return IsAvailableToContext(extension, context, GURL(), platform);
     64   }
     65   Availability IsAvailableToContext(const Extension* extension,
     66                                     Context context,
     67                                     const GURL& url) const {
     68     return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
     69   }
     70 
     71   // extension::Feature:
     72   virtual Availability IsAvailableToManifest(const std::string& extension_id,
     73                                              Manifest::Type type,
     74                                              Location location,
     75                                              int manifest_version,
     76                                              Platform platform) const OVERRIDE;
     77 
     78   virtual Availability IsAvailableToContext(const Extension* extension,
     79                                             Context context,
     80                                             const GURL& url,
     81                                             Platform platform) const OVERRIDE;
     82 
     83   virtual std::string GetAvailabilityMessage(AvailabilityResult result,
     84                                              Manifest::Type type,
     85                                              const GURL& url) 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 
     93  protected:
     94   Availability CreateAvailability(AvailabilityResult result) const;
     95   Availability CreateAvailability(AvailabilityResult result,
     96                                   Manifest::Type type) const;
     97   Availability CreateAvailability(AvailabilityResult result,
     98                                   const GURL& url) const;
     99 
    100  private:
    101   // For clarity and consistency, we handle the default value of each of these
    102   // members the same way: it matches everything. It is up to the higher level
    103   // code that reads Features out of static data to validate that data and set
    104   // sensible defaults.
    105   std::set<std::string> whitelist_;
    106   std::set<Manifest::Type> extension_types_;
    107   std::set<Context> contexts_;
    108   URLPatternSet matches_;
    109   Location location_;  // we only care about component/not-component now
    110   Platform platform_;  // we only care about chromeos/not-chromeos now
    111   int min_manifest_version_;
    112   int max_manifest_version_;
    113   chrome::VersionInfo::Channel channel_;
    114   bool has_parent_;
    115   bool channel_has_been_set_;
    116 
    117   FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Context);
    118 };
    119 
    120 }  // namespace extensions
    121 
    122 #endif  // CHROME_COMMON_EXTENSIONS_FEATURES_SIMPLE_FEATURE_H_
    123