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 <stddef.h>
9
10#include <initializer_list>
11#include <memory>
12#include <set>
13#include <string>
14#include <vector>
15
16#include "base/callback_forward.h"
17#include "base/gtest_prod_util.h"
18#include "base/lazy_instance.h"
19#include "base/macros.h"
20#include "base/optional.h"
21#include "base/values.h"
22#include "components/version_info/version_info.h"
23#include "extensions/common/extension.h"
24#include "extensions/common/features/feature.h"
25#include "extensions/common/features/feature_session_type.h"
26#include "extensions/common/manifest.h"
27
28namespace extensions {
29
30class FeatureProviderTest;
31class ExtensionAPITest;
32
33class SimpleFeature : public Feature {
34 public:
35 // Used by tests to override the cached --whitelisted-extension-id.
36 // NOTE: Not thread-safe! This is because it sets extension id on global
37 // singleton during its construction and destruction.
38 class ScopedThreadUnsafeAllowlistForTest {
39 public:
40 explicit ScopedThreadUnsafeAllowlistForTest(const std::string& id);
41 ~ScopedThreadUnsafeAllowlistForTest();
42
43 private:
44 std::string previous_id_;
45
46 DISALLOW_COPY_AND_ASSIGN(ScopedThreadUnsafeAllowlistForTest);
47 };
48
49 SimpleFeature();
50 ~SimpleFeature() override;
51
52 Availability IsAvailableToContext(const Extension* extension,
53 Context context) const {
54 return IsAvailableToContext(extension, context, GURL());
55 }
56 Availability IsAvailableToContext(const Extension* extension,
57 Context context,
58 Platform platform) const {
59 return IsAvailableToContext(extension, context, GURL(), platform);
60 }
61 Availability IsAvailableToContext(const Extension* extension,
62 Context context,
63 const GURL& url) const {
64 return IsAvailableToContext(extension, context, url, GetCurrentPlatform());
65 }
66
67 // extension::Feature:
68 Availability IsAvailableToManifest(const HashedExtensionId& hashed_id,
69 Manifest::Type type,
70 Manifest::Location location,
71 int manifest_version,
72 Platform platform) const override;
73 Availability IsAvailableToContext(const Extension* extension,
74 Context context,
75 const GURL& url,
76 Platform platform) const override;
77 Availability IsAvailableToEnvironment() const override;
78 bool IsInternal() const override;
79 bool IsIdInBlocklist(const HashedExtensionId& hashed_id) const override;
80 bool IsIdInAllowlist(const HashedExtensionId& hashed_id) const override;
81
82 static bool IsIdInArray(const std::string& extension_id,
83 const char* const array[],
84 size_t array_length);
85
86 // Similar to Manifest::Location, these are the classes of locations
87 // supported in feature files. These should only be used in this class and in
88 // generated files.
89 enum Location {
90 COMPONENT_LOCATION,
91 EXTERNAL_COMPONENT_LOCATION,
92 POLICY_LOCATION,
93 };
94
95 // Setters used by generated code to create the feature.
96 // NOTE: These setters use base::StringPiece and std::initalizer_list rather
97 // than std::string and std::vector for binary size reasons. Using STL types
98 // directly in the header means that code that doesn't already have that exact
99 // type ends up triggering many implicit conversions which are all inlined.
100 void set_blocklist(std::initializer_list<const char* const> blocklist);
101 void set_channel(version_info::Channel channel) { channel_ = channel; }
102 void set_command_line_switch(base::StringPiece command_line_switch);
103 void set_component_extensions_auto_granted(bool granted) {
104 component_extensions_auto_granted_ = granted;
105 }
106 void set_contexts(std::initializer_list<Context> contexts);
107 void set_dependencies(std::initializer_list<const char* const> dependencies);
108 void set_extension_types(std::initializer_list<Manifest::Type> types);
109 void set_session_types(std::initializer_list<FeatureSessionType> types);
110 void set_internal(bool is_internal) { is_internal_ = is_internal; }
111 void set_location(Location location) { location_ = location; }
112 // set_matches() is an exception to pass-by-value since we construct an
113 // URLPatternSet from the vector of strings.
114 // TODO(devlin): Pass in an URLPatternSet directly.
115 void set_matches(std::initializer_list<const char* const> matches);
116 void set_max_manifest_version(int max_manifest_version) {
117 max_manifest_version_ = max_manifest_version;
118 }
119 void set_min_manifest_version(int min_manifest_version) {
120 min_manifest_version_ = min_manifest_version;
121 }
122 void set_noparent(bool no_parent) { no_parent_ = no_parent; }
123 void set_platforms(std::initializer_list<Platform> platforms);
124 void set_allowlist(std::initializer_list<const char* const> allowlist);
125
126 protected:
127 // Accessors used by subclasses in feature verification.
128 const std::vector<std::string>& blocklist() const { return blocklist_; }
129 const std::vector<std::string>& allowlist() const { return allowlist_; }
130 const std::vector<Manifest::Type>& extension_types() const {
131 return extension_types_;
132 }
133 const std::vector<Platform>& platforms() const { return platforms_; }
134 const std::vector<Context>& contexts() const { return contexts_; }
135 const std::vector<std::string>& dependencies() const { return dependencies_; }
136 const base::Optional<version_info::Channel> channel() const {
137 return channel_;
138 }
139 const base::Optional<Location> location() const { return location_; }
140 const base::Optional<int> min_manifest_version() const {
141 return min_manifest_version_;
142 }
143 const base::Optional<int> max_manifest_version() const {
144 return max_manifest_version_;
145 }
146 const base::Optional<std::string>& command_line_switch() const {
147 return command_line_switch_;
148 }
149 bool component_extensions_auto_granted() const {
150 return component_extensions_auto_granted_;
151 }
152 const URLPatternSet& matches() const { return matches_; }
153
154 std::string GetAvailabilityMessage(AvailabilityResult result,
155 Manifest::Type type,
156 const GURL& url,
157 Context context,
158 version_info::Channel channel,
159 FeatureSessionType session_type) const;
160
161 // Handy utilities which construct the correct availability message.
162 Availability CreateAvailability(AvailabilityResult result) const;
163 Availability CreateAvailability(AvailabilityResult result,
164 Manifest::Type type) const;
165 Availability CreateAvailability(AvailabilityResult result,
166 const GURL& url) const;
167 Availability CreateAvailability(AvailabilityResult result,
168 Context context) const;
169 Availability CreateAvailability(AvailabilityResult result,
170 version_info::Channel channel) const;
171 Availability CreateAvailability(AvailabilityResult result,
172 FeatureSessionType session_type) const;
173
174 private:
175 friend struct FeatureComparator;
176 FRIEND_TEST_ALL_PREFIXES(FeatureProviderTest, ManifestFeatureTypes);
177 FRIEND_TEST_ALL_PREFIXES(FeatureProviderTest, PermissionFeatureTypes);
178 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, DefaultConfigurationFeatures);
179 FRIEND_TEST_ALL_PREFIXES(FeaturesGenerationTest, FeaturesTest);
180
181 // Holds String to Enum value mappings.
182 struct Mappings;
183
184 static bool IsIdInList(const HashedExtensionId& hashed_id,
185 const std::vector<std::string>& list);
186
187 bool MatchesManifestLocation(Manifest::Location manifest_location) const;
188
189 // Checks if the feature is allowed in a session of type |session_type|
190 // (based on session type feature restrictions).
191 bool MatchesSessionTypes(FeatureSessionType session_type) const;
192
193 Availability CheckDependencies(
194 const base::Callback<Availability(const Feature*)>& checker) const;
195
196 static bool IsValidExtensionId(const std::string& extension_id);
197 static bool IsValidHashedExtensionId(const HashedExtensionId& hashed_id);
198
199 // Returns the availability of the feature with respect to the basic
200 // environment Chrome is running in.
201 Availability GetEnvironmentAvailability(
202 Platform platform,
203 version_info::Channel channel,
204 FeatureSessionType session_type) const;
205
206 // Returns the availability of the feature with respect to a given extension's
207 // properties.
208 Availability GetManifestAvailability(const HashedExtensionId& hashed_id,
209 Manifest::Type type,
210 Manifest::Location location,
211 int manifest_version) const;
212
213 // Returns the availability of the feature with respect to a given context.
214 Availability GetContextAvailability(Context context, const GURL& url) const;
215
216 // For clarity and consistency, we handle the default value of each of these
217 // members the same way: it matches everything. It is up to the higher level
218 // code that reads Features out of static data to validate that data and set
219 // sensible defaults.
220 std::vector<std::string> blocklist_;
221 std::vector<std::string> allowlist_;
222 std::vector<std::string> dependencies_;
223 std::vector<Manifest::Type> extension_types_;
224 std::vector<FeatureSessionType> session_types_;
225 std::vector<Context> contexts_;
226 std::vector<Platform> platforms_;
227 URLPatternSet matches_;
228
229 base::Optional<Location> location_;
230 base::Optional<int> min_manifest_version_;
231 base::Optional<int> max_manifest_version_;
232 base::Optional<std::string> command_line_switch_;
233 base::Optional<version_info::Channel> channel_;
234 // Whether to ignore channel-based restrictions (such as because the user has
235 // enabled experimental extension APIs). Note: this is lazily calculated, and
236 // then cached.
237 mutable base::Optional<bool> ignore_channel_;
238
239 bool component_extensions_auto_granted_;
240 bool is_internal_;
241
242 DISALLOW_COPY_AND_ASSIGN(SimpleFeature);
243};
244
245} // namespace extensions
246
247#endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_
248