Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / common / features / simple_feature.h
blob5ec95cf91b4a55e2714bcb176e960f4a29cdbe11
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 // Used by tests to override the cached --whitelisted-extension-id.
33 class ScopedWhitelistForTest {
34 public:
35 explicit ScopedWhitelistForTest(const std::string& id);
36 ~ScopedWhitelistForTest();
38 private:
39 std::string* previous_id_;
41 DISALLOW_COPY_AND_ASSIGN(ScopedWhitelistForTest);
44 SimpleFeature();
45 ~SimpleFeature() override;
47 // Dependency resolution is a property of Features that is preferrably
48 // handled internally to avoid temptation, but FeatureFilters may need
49 // to know if there are any at all.
50 bool HasDependencies() const;
52 // Adds a filter to this feature. The feature takes ownership of the filter.
53 void AddFilter(scoped_ptr<SimpleFeatureFilter> filter);
55 // Parses the JSON representation of a feature into the fields of this object.
56 // Unspecified values in the JSON are not modified in the object. This allows
57 // us to implement inheritance by parsing one value after another. Returns
58 // the error found, or an empty string on success.
59 virtual std::string Parse(const base::DictionaryValue* dictionary);
61 Availability IsAvailableToContext(const Extension* extension,
62 Context context) const {
63 return IsAvailableToContext(extension, context, GURL());
65 Availability IsAvailableToContext(const Extension* extension,
66 Context context,
67 Platform platform) const {
68 return IsAvailableToContext(extension, context, GURL(), platform);
70 Availability IsAvailableToContext(const Extension* extension,
71 Context context,
72 const GURL& url) const {
73 return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
76 // extension::Feature:
77 Availability IsAvailableToManifest(const std::string& extension_id,
78 Manifest::Type type,
79 Manifest::Location location,
80 int manifest_version,
81 Platform platform) const override;
83 Availability IsAvailableToContext(const Extension* extension,
84 Context context,
85 const GURL& url,
86 Platform platform) const override;
88 std::string GetAvailabilityMessage(AvailabilityResult result,
89 Manifest::Type type,
90 const GURL& url,
91 Context context) const override;
93 bool IsInternal() const override;
95 bool IsIdInBlacklist(const std::string& extension_id) const override;
96 bool IsIdInWhitelist(const std::string& extension_id) const override;
98 static bool IsIdInArray(const std::string& extension_id,
99 const char* const array[],
100 size_t array_length);
102 protected:
103 // Similar to Manifest::Location, these are the classes of locations
104 // supported in feature files. Production code should never directly access
105 // these.
106 enum Location {
107 UNSPECIFIED_LOCATION,
108 COMPONENT_LOCATION,
109 EXTERNAL_COMPONENT_LOCATION,
110 POLICY_LOCATION,
113 // Accessors defined for testing.
114 std::vector<std::string>* blacklist() { return &blacklist_; }
115 const std::vector<std::string>* blacklist() const { return &blacklist_; }
116 std::vector<std::string>* whitelist() { return &whitelist_; }
117 const std::vector<std::string>* whitelist() const { return &whitelist_; }
118 std::vector<Manifest::Type>* extension_types() { return &extension_types_; }
119 const std::vector<Manifest::Type>* extension_types() const {
120 return &extension_types_;
122 std::vector<Context>* contexts() { return &contexts_; }
123 const std::vector<Context>* contexts() const { return &contexts_; }
124 std::vector<Platform>* platforms() { return &platforms_; }
125 Location location() const { return location_; }
126 void set_location(Location location) { location_ = location; }
127 int min_manifest_version() const { return min_manifest_version_; }
128 void set_min_manifest_version(int min_manifest_version) {
129 min_manifest_version_ = min_manifest_version;
131 int max_manifest_version() const { return max_manifest_version_; }
132 void set_max_manifest_version(int max_manifest_version) {
133 max_manifest_version_ = max_manifest_version;
135 const std::string& command_line_switch() const {
136 return command_line_switch_;
138 void set_command_line_switch(const std::string& command_line_switch) {
139 command_line_switch_ = command_line_switch;
142 // Handy utilities which construct the correct availability message.
143 Availability CreateAvailability(AvailabilityResult result) const;
144 Availability CreateAvailability(AvailabilityResult result,
145 Manifest::Type type) const;
146 Availability CreateAvailability(AvailabilityResult result,
147 const GURL& url) const;
148 Availability CreateAvailability(AvailabilityResult result,
149 Context context) const;
151 private:
152 friend class SimpleFeatureTest;
153 FRIEND_TEST_ALL_PREFIXES(BaseFeatureProviderTest, ManifestFeatureTypes);
154 FRIEND_TEST_ALL_PREFIXES(BaseFeatureProviderTest, PermissionFeatureTypes);
155 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, DefaultConfigurationFeatures);
156 FRIEND_TEST_ALL_PREFIXES(ManifestUnitTest, Extension);
157 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Blacklist);
158 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, CommandLineSwitch);
159 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Context);
160 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, HashedIdBlacklist);
161 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, HashedIdWhitelist);
162 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Inheritance);
163 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Location);
164 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ManifestVersion);
165 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, PackageType);
166 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseContexts);
167 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseLocation);
168 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseManifestVersion);
169 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseNull);
170 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParsePackageTypes);
171 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParsePlatforms);
172 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, ParseWhitelist);
173 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Platform);
174 FRIEND_TEST_ALL_PREFIXES(SimpleFeatureTest, Whitelist);
176 // Holds String to Enum value mappings.
177 struct Mappings;
179 static bool IsIdInList(const std::string& extension_id,
180 const std::vector<std::string>& list);
182 bool MatchesManifestLocation(Manifest::Location manifest_location) const;
184 Availability CheckDependencies(
185 const base::Callback<Availability(const Feature*)>& checker) const;
187 static bool IsValidExtensionId(const std::string& extension_id);
189 // For clarity and consistency, we handle the default value of each of these
190 // members the same way: it matches everything. It is up to the higher level
191 // code that reads Features out of static data to validate that data and set
192 // sensible defaults.
193 std::vector<std::string> blacklist_;
194 std::vector<std::string> whitelist_;
195 std::vector<std::string> dependencies_;
196 std::vector<Manifest::Type> extension_types_;
197 std::vector<Context> contexts_;
198 std::vector<Platform> platforms_;
199 URLPatternSet matches_;
200 Location location_;
201 int min_manifest_version_;
202 int max_manifest_version_;
203 bool component_extensions_auto_granted_;
204 std::string command_line_switch_;
206 ScopedVector<SimpleFeatureFilter> filters_;;
208 DISALLOW_COPY_AND_ASSIGN(SimpleFeature);
211 } // namespace extensions
213 #endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_