OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ | 5 #ifndef EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ |
6 #define EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ | 6 #define EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/lazy_instance.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
18 #include "extensions/common/features/feature.h" | 19 #include "extensions/common/features/feature.h" |
19 #include "extensions/common/features/simple_feature_filter.h" | 20 #include "extensions/common/features/simple_feature_filter.h" |
20 #include "extensions/common/manifest.h" | 21 #include "extensions/common/manifest.h" |
21 | 22 |
22 namespace extensions { | 23 namespace extensions { |
23 | 24 |
| 25 class BaseFeatureProviderTest; |
| 26 class ExtensionAPITest; |
| 27 class ManifestUnitTest; |
| 28 class SimpleFeatureTest; |
| 29 |
24 class SimpleFeature : public Feature { | 30 class SimpleFeature : public Feature { |
25 public: | 31 public: |
26 SimpleFeature(); | 32 SimpleFeature(); |
27 ~SimpleFeature() override; | 33 ~SimpleFeature() override; |
28 | 34 |
29 // Similar to Manifest::Location, these are the classes of locations | |
30 // supported in feature files. | |
31 // | |
32 // This is only public for testing. Production code should never access it, | |
33 // nor should it really have any reason to access the SimpleFeature class | |
34 // directly, it should be dealing with the Feature interface. | |
35 enum Location { | |
36 UNSPECIFIED_LOCATION, | |
37 COMPONENT_LOCATION, | |
38 EXTERNAL_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 std::set<std::string>* blacklist() { return &blacklist_; } | |
45 const std::set<std::string>* blacklist() const { return &blacklist_; } | |
46 std::set<std::string>* whitelist() { return &whitelist_; } | |
47 const std::set<std::string>* whitelist() const { return &whitelist_; } | |
48 std::set<Manifest::Type>* extension_types() { return &extension_types_; } | |
49 const std::set<Manifest::Type>* extension_types() const { | |
50 return &extension_types_; | |
51 } | |
52 std::set<Context>* contexts() { return &contexts_; } | |
53 const std::set<Context>* contexts() const { return &contexts_; } | |
54 Location location() const { return location_; } | |
55 void set_location(Location location) { location_ = location; } | |
56 int min_manifest_version() const { return min_manifest_version_; } | |
57 void set_min_manifest_version(int min_manifest_version) { | |
58 min_manifest_version_ = min_manifest_version; | |
59 } | |
60 int max_manifest_version() const { return max_manifest_version_; } | |
61 void set_max_manifest_version(int max_manifest_version) { | |
62 max_manifest_version_ = max_manifest_version; | |
63 } | |
64 const std::string& command_line_switch() const { | |
65 return command_line_switch_; | |
66 } | |
67 void set_command_line_switch(const std::string& command_line_switch) { | |
68 command_line_switch_ = command_line_switch; | |
69 } | |
70 | |
71 // Dependency resolution is a property of Features that is preferrably | 35 // Dependency resolution is a property of Features that is preferrably |
72 // handled internally to avoid temptation, but FeatureFilters may need | 36 // handled internally to avoid temptation, but FeatureFilters may need |
73 // to know if there are any at all. | 37 // to know if there are any at all. |
74 bool HasDependencies() const; | 38 bool HasDependencies() const; |
75 | 39 |
76 // Adds a filter to this feature. The feature takes ownership of the filter. | 40 // Adds a filter to this feature. The feature takes ownership of the filter. |
77 void AddFilter(scoped_ptr<SimpleFeatureFilter> filter); | 41 void AddFilter(scoped_ptr<SimpleFeatureFilter> filter); |
78 | 42 |
79 // Parses the JSON representation of a feature into the fields of this object. | 43 // Parses the JSON representation of a feature into the fields of this object. |
80 // Unspecified values in the JSON are not modified in the object. This allows | 44 // Unspecified values in the JSON are not modified in the object. This allows |
81 // us to implement inheritance by parsing one value after another. Returns | 45 // us to implement inheritance by parsing one value after another. Returns |
82 // the error found, or an empty string on success. | 46 // the error found, or an empty string on success. |
83 virtual std::string Parse(const base::DictionaryValue* value); | 47 virtual std::string Parse(const base::DictionaryValue* dictionary); |
84 | |
85 std::set<Platform>* platforms() { return &platforms_; } | |
86 | 48 |
87 Availability IsAvailableToContext(const Extension* extension, | 49 Availability IsAvailableToContext(const Extension* extension, |
88 Context context) const { | 50 Context context) const { |
89 return IsAvailableToContext(extension, context, GURL()); | 51 return IsAvailableToContext(extension, context, GURL()); |
90 } | 52 } |
91 Availability IsAvailableToContext(const Extension* extension, | 53 Availability IsAvailableToContext(const Extension* extension, |
92 Context context, | 54 Context context, |
93 Platform platform) const { | 55 Platform platform) const { |
94 return IsAvailableToContext(extension, context, GURL(), platform); | 56 return IsAvailableToContext(extension, context, GURL(), platform); |
95 } | 57 } |
(...skipping 17 matching lines...) Expand all Loading... |
113 | 75 |
114 std::string GetAvailabilityMessage(AvailabilityResult result, | 76 std::string GetAvailabilityMessage(AvailabilityResult result, |
115 Manifest::Type type, | 77 Manifest::Type type, |
116 const GURL& url, | 78 const GURL& url, |
117 Context context) const override; | 79 Context context) const override; |
118 | 80 |
119 bool IsInternal() const override; | 81 bool IsInternal() const override; |
120 | 82 |
121 bool IsIdInBlacklist(const std::string& extension_id) const override; | 83 bool IsIdInBlacklist(const std::string& extension_id) const override; |
122 bool IsIdInWhitelist(const std::string& extension_id) const override; | 84 bool IsIdInWhitelist(const std::string& extension_id) const override; |
123 static bool IsIdInList(const std::string& extension_id, | 85 |
124 const std::set<std::string>& list); | 86 static bool IsIdInArray(const std::string& extension_id, |
| 87 const char* const array[], |
| 88 size_t array_length); |
125 | 89 |
126 protected: | 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, |
| 99 }; |
| 100 |
| 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_; |
| 109 } |
| 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; |
| 118 } |
| 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; |
| 122 } |
| 123 const std::string& command_line_switch() const { |
| 124 return command_line_switch_; |
| 125 } |
| 126 void set_command_line_switch(const std::string& command_line_switch) { |
| 127 command_line_switch_ = command_line_switch; |
| 128 } |
| 129 |
127 // Handy utilities which construct the correct availability message. | 130 // Handy utilities which construct the correct availability message. |
128 Availability CreateAvailability(AvailabilityResult result) const; | 131 Availability CreateAvailability(AvailabilityResult result) const; |
129 Availability CreateAvailability(AvailabilityResult result, | 132 Availability CreateAvailability(AvailabilityResult result, |
130 Manifest::Type type) const; | 133 Manifest::Type type) const; |
131 Availability CreateAvailability(AvailabilityResult result, | 134 Availability CreateAvailability(AvailabilityResult result, |
132 const GURL& url) const; | 135 const GURL& url) const; |
133 Availability CreateAvailability(AvailabilityResult result, | 136 Availability CreateAvailability(AvailabilityResult result, |
134 Context context) const; | 137 Context context) const; |
135 | 138 |
136 private: | 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); |
| 163 |
| 164 // Holds String to Enum value mappings. |
| 165 struct Mappings; |
| 166 |
| 167 static bool IsIdInList(const std::string& extension_id, |
| 168 const std::vector<std::string>& list); |
| 169 |
137 bool MatchesManifestLocation(Manifest::Location manifest_location) const; | 170 bool MatchesManifestLocation(Manifest::Location manifest_location) const; |
138 | 171 |
139 Availability CheckDependencies( | 172 Availability CheckDependencies( |
140 const base::Callback<Availability(const Feature*)>& checker) const; | 173 const base::Callback<Availability(const Feature*)>& checker) const; |
141 | 174 |
| 175 static bool IsValidExtensionId(const std::string& extension_id); |
| 176 |
142 // For clarity and consistency, we handle the default value of each of these | 177 // For clarity and consistency, we handle the default value of each of these |
143 // members the same way: it matches everything. It is up to the higher level | 178 // members the same way: it matches everything. It is up to the higher level |
144 // code that reads Features out of static data to validate that data and set | 179 // code that reads Features out of static data to validate that data and set |
145 // sensible defaults. | 180 // sensible defaults. |
146 std::set<std::string> blacklist_; | 181 std::vector<std::string> blacklist_; |
147 std::set<std::string> whitelist_; | 182 std::vector<std::string> whitelist_; |
148 std::set<std::string> dependencies_; | 183 std::vector<std::string> dependencies_; |
149 std::set<Manifest::Type> extension_types_; | 184 std::vector<Manifest::Type> extension_types_; |
150 std::set<Context> contexts_; | 185 std::vector<Context> contexts_; |
| 186 std::vector<Platform> platforms_; |
151 URLPatternSet matches_; | 187 URLPatternSet matches_; |
152 Location location_; | 188 Location location_; |
153 std::set<Platform> platforms_; | |
154 int min_manifest_version_; | 189 int min_manifest_version_; |
155 int max_manifest_version_; | 190 int max_manifest_version_; |
156 bool component_extensions_auto_granted_; | 191 bool component_extensions_auto_granted_; |
157 std::string command_line_switch_; | 192 std::string command_line_switch_; |
158 | 193 |
159 ScopedVector<SimpleFeatureFilter> filters_;; | 194 ScopedVector<SimpleFeatureFilter> filters_;; |
160 | 195 |
161 DISALLOW_COPY_AND_ASSIGN(SimpleFeature); | 196 DISALLOW_COPY_AND_ASSIGN(SimpleFeature); |
162 }; | 197 }; |
163 | 198 |
164 } // namespace extensions | 199 } // namespace extensions |
165 | 200 |
166 #endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ | 201 #endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ |
OLD | NEW |