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