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/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/lazy_instance.h" |
13 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
17 #include "extensions/common/features/feature.h" | 18 #include "extensions/common/features/feature.h" |
18 #include "extensions/common/features/simple_feature_filter.h" | 19 #include "extensions/common/features/simple_feature_filter.h" |
19 #include "extensions/common/manifest.h" | 20 #include "extensions/common/manifest.h" |
20 | 21 |
21 namespace extensions { | 22 namespace extensions { |
22 | 23 |
23 class ComplexFeature; | 24 class ComplexFeature; |
24 | 25 |
25 class SimpleFeature : public Feature { | 26 class SimpleFeature : public Feature { |
26 public: | 27 public: |
27 SimpleFeature(); | 28 SimpleFeature(); |
28 virtual ~SimpleFeature(); | 29 virtual ~SimpleFeature(); |
29 | 30 |
30 std::set<std::string>* whitelist() { return &whitelist_; } | 31 std::set<std::string>* whitelist() { return &whitelist_; } |
31 std::set<Manifest::Type>* extension_types() { return &extension_types_; } | 32 std::set<Manifest::Type>* extension_types() { return &extension_types_; } |
32 | 33 |
33 // Adds a filter to this feature. The feature takes ownership of the filter. | 34 // Adds a filter to this feature. The feature takes ownership of the filter. |
34 void AddFilter(scoped_ptr<SimpleFeatureFilter> filter); | 35 void AddFilter(scoped_ptr<SimpleFeatureFilter> filter); |
35 | 36 |
36 // Parses the JSON representation of a feature into the fields of this object. | 37 // Parses the JSON representation of a feature into the fields of this object. |
37 // Unspecified values in the JSON are not modified in the object. This allows | 38 // Unspecified values in the JSON are not modified in the object. This allows |
38 // us to implement inheritance by parsing one value after another. Returns | 39 // us to implement inheritance by parsing one value after another. Returns |
39 // the error found, or an empty string on success. | 40 // the error found, or an empty string on success. |
40 virtual std::string Parse(const base::DictionaryValue* value); | 41 virtual std::string Parse(const base::DictionaryValue* value); |
41 | 42 |
42 Location location() const { return location_; } | |
43 void set_location(Location location) { location_ = location; } | |
44 | |
45 std::set<Platform>* platforms() { return &platforms_; } | 43 std::set<Platform>* platforms() { return &platforms_; } |
46 | 44 |
47 int min_manifest_version() const { return min_manifest_version_; } | |
48 void set_min_manifest_version(int min_manifest_version) { | |
49 min_manifest_version_ = min_manifest_version; | |
50 } | |
51 | |
52 int max_manifest_version() const { return max_manifest_version_; } | |
53 void set_max_manifest_version(int max_manifest_version) { | |
54 max_manifest_version_ = max_manifest_version; | |
55 } | |
56 | |
57 Availability IsAvailableToContext(const Extension* extension, | 45 Availability IsAvailableToContext(const Extension* extension, |
58 Context context) const { | 46 Context context) const { |
59 return IsAvailableToContext(extension, context, GURL()); | 47 return IsAvailableToContext(extension, context, GURL()); |
60 } | 48 } |
61 Availability IsAvailableToContext(const Extension* extension, | 49 Availability IsAvailableToContext(const Extension* extension, |
62 Context context, | 50 Context context, |
63 Platform platform) const { | 51 Platform platform) const { |
64 return IsAvailableToContext(extension, context, GURL(), platform); | 52 return IsAvailableToContext(extension, context, GURL(), platform); |
65 } | 53 } |
66 Availability IsAvailableToContext(const Extension* extension, | 54 Availability IsAvailableToContext(const Extension* extension, |
67 Context context, | 55 Context context, |
68 const GURL& url) const { | 56 const GURL& url) const { |
69 return IsAvailableToContext(extension, context, url, GetCurrentPlatform()); | 57 return IsAvailableToContext(extension, context, url, GetCurrentPlatform()); |
70 } | 58 } |
71 | 59 |
72 // extension::Feature: | 60 // extension::Feature: |
73 virtual Availability IsAvailableToManifest(const std::string& extension_id, | 61 virtual Availability IsAvailableToManifest(const std::string& extension_id, |
74 Manifest::Type type, | 62 Manifest::Type type, |
75 Location location, | 63 Manifest::Location location, |
76 int manifest_version, | 64 int manifest_version, |
77 Platform platform) const OVERRIDE; | 65 Platform platform) const OVERRIDE; |
78 | 66 |
79 virtual Availability IsAvailableToContext(const Extension* extension, | 67 virtual Availability IsAvailableToContext(const Extension* extension, |
80 Context context, | 68 Context context, |
81 const GURL& url, | 69 const GURL& url, |
82 Platform platform) const OVERRIDE; | 70 Platform platform) const OVERRIDE; |
83 | 71 |
84 virtual std::string GetAvailabilityMessage(AvailabilityResult result, | 72 virtual std::string GetAvailabilityMessage(AvailabilityResult result, |
85 Manifest::Type type, | 73 Manifest::Type type, |
(...skipping 12 matching lines...) Expand all Loading... |
98 protected: | 86 protected: |
99 Availability CreateAvailability(AvailabilityResult result) const; | 87 Availability CreateAvailability(AvailabilityResult result) const; |
100 Availability CreateAvailability(AvailabilityResult result, | 88 Availability CreateAvailability(AvailabilityResult result, |
101 Manifest::Type type) const; | 89 Manifest::Type type) const; |
102 Availability CreateAvailability(AvailabilityResult result, | 90 Availability CreateAvailability(AvailabilityResult result, |
103 const GURL& url) const; | 91 const GURL& url) const; |
104 Availability CreateAvailability(AvailabilityResult result, | 92 Availability CreateAvailability(AvailabilityResult result, |
105 Context context) const; | 93 Context context) const; |
106 | 94 |
107 private: | 95 private: |
| 96 // Similar to Manifest::Location, these are the classes of locations |
| 97 // supported in feature files; "component" implies |
| 98 // COMPONENT/EXTERNAL_COMPONENT manifest location types, etc. |
| 99 enum Location { |
| 100 UNSPECIFIED_LOCATION, |
| 101 COMPONENT_LOCATION, |
| 102 POLICY_LOCATION, |
| 103 }; |
| 104 |
| 105 bool MatchesManifestLocation(Manifest::Location manifest_location) const; |
| 106 |
| 107 // Accessors defined for testing. |
| 108 Location location() const { return location_; } |
| 109 void set_location(Location location) { location_ = location; } |
| 110 int min_manifest_version() const { return min_manifest_version_; } |
| 111 void set_min_manifest_version(int min_manifest_version) { |
| 112 min_manifest_version_ = min_manifest_version; |
| 113 } |
| 114 int max_manifest_version() const { return max_manifest_version_; } |
| 115 void set_max_manifest_version(int max_manifest_version) { |
| 116 max_manifest_version_ = max_manifest_version; |
| 117 } |
| 118 |
108 // For clarity and consistency, we handle the default value of each of these | 119 // For clarity and consistency, we handle the default value of each of these |
109 // members the same way: it matches everything. It is up to the higher level | 120 // members the same way: it matches everything. It is up to the higher level |
110 // code that reads Features out of static data to validate that data and set | 121 // code that reads Features out of static data to validate that data and set |
111 // sensible defaults. | 122 // sensible defaults. |
112 std::set<std::string> whitelist_; | 123 std::set<std::string> whitelist_; |
113 std::set<Manifest::Type> extension_types_; | 124 std::set<Manifest::Type> extension_types_; |
114 std::set<Context> contexts_; | 125 std::set<Context> contexts_; |
115 URLPatternSet matches_; | 126 URLPatternSet matches_; |
116 Location location_; // we only care about component/not-component now | 127 Location location_; |
117 std::set<Platform> platforms_; | 128 std::set<Platform> platforms_; |
118 int min_manifest_version_; | 129 int min_manifest_version_; |
119 int max_manifest_version_; | 130 int max_manifest_version_; |
120 bool has_parent_; | 131 bool has_parent_; |
121 | 132 |
122 typedef std::vector<linked_ptr<SimpleFeatureFilter> > FilterList; | 133 typedef std::vector<linked_ptr<SimpleFeatureFilter> > FilterList; |
123 FilterList filters_; | 134 FilterList filters_; |
124 | 135 |
| 136 struct Mappings; |
| 137 static base::LazyInstance<Mappings> g_mappings; |
| 138 |
| 139 FRIEND_TEST_ALL_PREFIXES(ExtensionAPITest, DefaultConfigurationFeatures); |
125 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Context); | 140 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Context); |
| 141 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Inheritance); |
| 142 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Location); |
| 143 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, ManifestVersion); |
| 144 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, ParseLocation); |
| 145 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, ParseNull); |
| 146 FRIEND_TEST_ALL_PREFIXES(ExtensionSimpleFeatureTest, Version); |
| 147 FRIEND_TEST_ALL_PREFIXES(ManifestTest, Extension); |
| 148 |
126 DISALLOW_COPY_AND_ASSIGN(SimpleFeature); | 149 DISALLOW_COPY_AND_ASSIGN(SimpleFeature); |
127 }; | 150 }; |
128 | 151 |
129 } // namespace extensions | 152 } // namespace extensions |
130 | 153 |
131 #endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ | 154 #endif // EXTENSIONS_COMMON_FEATURES_SIMPLE_FEATURE_H_ |
OLD | NEW |