Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / extensions / common / features / simple_feature.h
blob9d8c539414bb42f775911dbd76fa7c1cbaed192f
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.
5 #ifndef EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_
6 #define EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/callback_forward.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/lazy_instance.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/values.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/features/feature.h"
20 #include "extensions/common/features/simple_feature_filter.h"
21 #include "extensions/common/manifest.h"
23 namespace extensions {
25 class BaseFeatureProviderTest;
26 class ExtensionAPITest;
27 class ManifestUnitTest;
28 class SimpleFeatureTest;
30 class SimpleFeature : public Feature {
31 public:
32 SimpleFeature();
33 ~SimpleFeature() override;
35 // Dependency resolution is a property of Features that is preferrably
36 // handled internally to avoid temptation, but FeatureFilters may need
37 // to know if there are any at all.
38 bool HasDependencies() const;
40 // Adds a filter to this feature. The feature takes ownership of the filter.
41 void AddFilter(scoped_ptr<SimpleFeatureFilter> filter);
43 // Parses the JSON representation of a feature into the fields of this object.
44 // Unspecified values in the JSON are not modified in the object. This allows
45 // us to implement inheritance by parsing one value after another. Returns
46 // the error found, or an empty string on success.
47 virtual std::string Parse(const base::DictionaryValue* dictionary);
49 Availability IsAvailableToContext(const Extension* extension,
50 Context context) const {
51 return IsAvailableToContext(extension, context, GURL());
53 Availability IsAvailableToContext(const Extension* extension,
54 Context context,
55 Platform platform) const {
56 return IsAvailableToContext(extension, context, GURL(), platform);
58 Availability IsAvailableToContext(const Extension* extension,
59 Context context,
60 const GURL& url) const {
61 return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
64 // extension::Feature:
65 Availability IsAvailableToManifest(const std::string& extension_id,
66 Manifest::Type type,
67 Manifest::Location location,
68 int manifest_version,
69 Platform platform) const override;
71 Availability IsAvailableToContext(const Extension* extension,
72 Context context,
73 const GURL& url,
74 Platform platform) const override;
76 std::string GetAvailabilityMessage(AvailabilityResult result,
77 Manifest::Type type,
78 const GURL& url,
79 Context context) const override;
81 bool IsInternal() const override;
83 bool IsIdInBlacklist(const std::string& extension_id) const override;
84 bool IsIdInWhitelist(const std::string& extension_id) const override;
86 static bool IsIdInArray(const std::string& extension_id,
87 const char* const array[],
88 size_t array_length);
90 protected:
91 // Similar to Manifest::Location, these are the classes of locations
92 // supported in feature files. Production code should never directly access
93 // these.
94 enum Location {
95 UNSPECIFIED_LOCATION,
96 COMPONENT_LOCATION,
97 EXTERNAL_COMPONENT_LOCATION,
98 POLICY_LOCATION,
101 // Accessors defined for testing.
102 std::vector<std::string>* blacklist() { return &blacklist_; }
103 const std::vector<std::string>* blacklist() const { return &blacklist_; }
104 std::vector<std::string>* whitelist() { return &whitelist_; }
105 const std::vector<std::string>* whitelist() const { return &whitelist_; }
106 std::vector<Manifest::Type>* extension_types() { return &extension_types_; }
107 const std::vector<Manifest::Type>* extension_types() const {
108 return &extension_types_;
110 std::vector<Context>* contexts() { return &contexts_; }
111 const std::vector<Context>* contexts() const { return &contexts_; }
112 std::vector<Platform>* platforms() { return &platforms_; }
113 Location location() const { return location_; }
114 void set_location(Location location) { location_ = location; }
115 int min_manifest_version() const { return min_manifest_version_; }
116 void set_min_manifest_version(int min_manifest_version) {
117 min_manifest_version_ = min_manifest_version;
119 int max_manifest_version() const { return max_manifest_version_; }
120 void set_max_manifest_version(int max_manifest_version) {
121 max_manifest_version_ = max_manifest_version;
123 const std::string& command_line_switch() const {
124 return command_line_switch_;
126 void set_command_line_switch(const std::string& command_line_switch) {
127 command_line_switch_ = command_line_switch;
130 // Handy utilities which construct the correct availability message.
131 Availability CreateAvailability(AvailabilityResult result) const;
132 Availability CreateAvailability(AvailabilityResult result,
133 Manifest::Type type) const;
134 Availability CreateAvailability(AvailabilityResult result,
135 const GURL& url) const;
136 Availability CreateAvailability(AvailabilityResult result,
137 Context context) const;
139 private:
140 friend class SimpleFeatureTest;
141 FRIEND_TEST_ALL_PREFIXES(BaseFeatureProviderTest, ManifestFeatureTypes);
142 FRIEND_TEST_ALL_PREFIXES(BaseFeatureProviderTest, PermissionFeatureTypes);
143 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, DefaultConfigurationFeatures);
144 FRIEND_TEST_ALL_PREFIXES(ManifestUnitTest, Extension);
145 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Blacklist);
146 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, CommandLineSwitch);
147 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Context);
148 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, HashedIdBlacklist);
149 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, HashedIdWhitelist);
150 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Inheritance);
151 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Location);
152 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ManifestVersion);
153 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, PackageType);
154 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseContexts);
155 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseLocation);
156 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseManifestVersion);
157 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseNull);
158 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParsePackageTypes);
159 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParsePlatforms);
160 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseWhitelist);
161 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Platform);
162 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Whitelist);
164 // Holds String to Enum value mappings.
165 struct Mappings;
167 static bool IsIdInList(const std::string& extension_id,
168 const std::vector<std::string>& list);
170 bool MatchesManifestLocation(Manifest::Location manifest_location) const;
172 Availability CheckDependencies(
173 const base::Callback<Availability(const Feature*)>& checker) const;
175 static bool IsValidExtensionId(const std::string& extension_id);
177 // For clarity and consistency, we handle the default value of each of these
178 // members the same way: it matches everything. It is up to the higher level
179 // code that reads Features out of static data to validate that data and set
180 // sensible defaults.
181 std::vector<std::string> blacklist_;
182 std::vector<std::string> whitelist_;
183 std::vector<std::string> dependencies_;
184 std::vector<Manifest::Type> extension_types_;
185 std::vector<Context> contexts_;
186 std::vector<Platform> platforms_;
187 URLPatternSet matches_;
188 Location location_;
189 int min_manifest_version_;
190 int max_manifest_version_;
191 bool component_extensions_auto_granted_;
192 std::string command_line_switch_;
194 ScopedVector<SimpleFeatureFilter> filters_;;
196 DISALLOW_COPY_AND_ASSIGN(SimpleFeature);
199 } // namespace extensions
201 #endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_