• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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_SIMPLE_FEATURE_H_
6 #define EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_
7 
8 #include <set>
9 #include <string>
10 #include <vector>
11 
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/values.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/features/feature.h"
18 #include "extensions/common/features/simple_feature_filter.h"
19 #include "extensions/common/manifest.h"
20 
21 namespace extensions {
22 
23 class ComplexFeature;
24 
25 class SimpleFeature : public Feature {
26  public:
27   SimpleFeature();
28   virtual ~SimpleFeature();
29 
30   // Similar to Manifest::Location, these are the classes of locations
31   // supported in feature files; "component" implies
32   // COMPONENT/EXTERNAL_COMPONENT manifest location types, etc.
33   //
34   // This is only public for testing. Production code should never access it,
35   // nor should it really have any reason to access the SimpleFeature class
36   // directly, it should be dealing with the Feature interface.
37   enum Location {
38     UNSPECIFIED_LOCATION,
39     COMPONENT_LOCATION,
40     POLICY_LOCATION,
41   };
42 
43   // Accessors defined for testing. See comment above about not directly using
44   // SimpleFeature in production code.
location()45   Location location() const { return location_; }
set_location(Location location)46   void set_location(Location location) { location_ = location; }
min_manifest_version()47   int min_manifest_version() const { return min_manifest_version_; }
set_min_manifest_version(int min_manifest_version)48   void set_min_manifest_version(int min_manifest_version) {
49     min_manifest_version_ = min_manifest_version;
50   }
max_manifest_version()51   int max_manifest_version() const { return max_manifest_version_; }
set_max_manifest_version(int max_manifest_version)52   void set_max_manifest_version(int max_manifest_version) {
53     max_manifest_version_ = max_manifest_version;
54   }
55 
blacklist()56   std::set<std::string>* blacklist() { return &blacklist_; }
whitelist()57   std::set<std::string>* whitelist() { return &whitelist_; }
extension_types()58   std::set<Manifest::Type>* extension_types() { return &extension_types_; }
59 
60   // Adds a filter to this feature. The feature takes ownership of the filter.
61   void AddFilter(scoped_ptr<SimpleFeatureFilter> filter);
62 
63   // Parses the JSON representation of a feature into the fields of this object.
64   // Unspecified values in the JSON are not modified in the object. This allows
65   // us to implement inheritance by parsing one value after another. Returns
66   // the error found, or an empty string on success.
67   virtual std::string Parse(const base::DictionaryValue* value);
68 
platforms()69   std::set<Platform>* platforms() { return &platforms_; }
70 
IsAvailableToContext(const Extension * extension,Context context)71   Availability IsAvailableToContext(const Extension* extension,
72                                     Context context) const {
73     return IsAvailableToContext(extension, context, GURL());
74   }
IsAvailableToContext(const Extension * extension,Context context,Platform platform)75   Availability IsAvailableToContext(const Extension* extension,
76                                     Context context,
77                                     Platform platform) const {
78     return IsAvailableToContext(extension, context, GURL(), platform);
79   }
IsAvailableToContext(const Extension * extension,Context context,const GURL & url)80   Availability IsAvailableToContext(const Extension* extension,
81                                     Context context,
82                                     const GURL& url) const {
83     return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
84   }
85 
86   // extension::Feature:
87   virtual Availability IsAvailableToManifest(const std::string& extension_id,
88                                              Manifest::Type type,
89                                              Manifest::Location location,
90                                              int manifest_version,
91                                              Platform platform) const OVERRIDE;
92 
93   virtual Availability IsAvailableToContext(const Extension* extension,
94                                             Context context,
95                                             const GURL& url,
96                                             Platform platform) const OVERRIDE;
97 
98   virtual std::string GetAvailabilityMessage(AvailabilityResult result,
99                                              Manifest::Type type,
100                                              const GURL& url,
101                                              Context context) const OVERRIDE;
102 
103   virtual std::set<Context>* GetContexts() OVERRIDE;
104 
105   virtual bool IsInternal() const OVERRIDE;
106   virtual bool IsBlockedInServiceWorker() const OVERRIDE;
107 
108   virtual bool IsIdInBlacklist(const std::string& extension_id) const OVERRIDE;
109   virtual bool IsIdInWhitelist(const std::string& extension_id) const OVERRIDE;
110   static bool IsIdInList(const std::string& extension_id,
111                          const std::set<std::string>& list);
112 
113  protected:
114   Availability CreateAvailability(AvailabilityResult result) const;
115   Availability CreateAvailability(AvailabilityResult result,
116                                   Manifest::Type type) const;
117   Availability CreateAvailability(AvailabilityResult result,
118                                   const GURL& url) const;
119   Availability CreateAvailability(AvailabilityResult result,
120                                   Context context) const;
121 
122  private:
123   bool MatchesManifestLocation(Manifest::Location manifest_location) const;
124 
125   // For clarity and consistency, we handle the default value of each of these
126   // members the same way: it matches everything. It is up to the higher level
127   // code that reads Features out of static data to validate that data and set
128   // sensible defaults.
129   std::set<std::string> blacklist_;
130   std::set<std::string> whitelist_;
131   std::set<Manifest::Type> extension_types_;
132   std::set<Context> contexts_;
133   URLPatternSet matches_;
134   Location location_;
135   std::set<Platform> platforms_;
136   int min_manifest_version_;
137   int max_manifest_version_;
138   bool has_parent_;
139   bool component_extensions_auto_granted_;
140 
141   typedef std::vector<linked_ptr<SimpleFeatureFilter> > FilterList;
142   FilterList filters_;
143 
144   DISALLOW_COPY_AND_ASSIGN(SimpleFeature);
145 };
146 
147 }  // namespace extensions
148 
149 #endif  // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_
150