OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_COMMON_EXTENSIONS_FEATURE_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_FEATURE_H_ |
6 #define CHROME_COMMON_EXTENSIONS_FEATURE_H_ | 6 #define CHROME_COMMON_EXTENSIONS_FEATURE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 // Whether a feature is available in a given situation or not, and if not, | 53 // Whether a feature is available in a given situation or not, and if not, |
54 // why not. | 54 // why not. |
55 enum Availability { | 55 enum Availability { |
56 IS_AVAILABLE, | 56 IS_AVAILABLE, |
57 NOT_FOUND_IN_WHITELIST, | 57 NOT_FOUND_IN_WHITELIST, |
58 INVALID_TYPE, | 58 INVALID_TYPE, |
59 INVALID_CONTEXT, | 59 INVALID_CONTEXT, |
60 INVALID_LOCATION, | 60 INVALID_LOCATION, |
61 INVALID_PLATFORM, | 61 INVALID_PLATFORM, |
62 INVALID_MIN_MANIFEST_VERSION, | 62 INVALID_MIN_MANIFEST_VERSION, |
63 INVALID_MAX_MANIFEST_VERSION | 63 INVALID_MAX_MANIFEST_VERSION, |
| 64 NOT_PRESENT, |
| 65 DEPENDENCY_NOT_PRESENT |
64 }; | 66 }; |
65 | 67 |
66 Feature(); | 68 Feature(); |
67 ~Feature(); | 69 Feature(const Feature& other); |
| 70 virtual ~Feature(); |
68 | 71 |
69 // Parses a feature from its JSON representation. | 72 const std::string& name() const { return name_; } |
70 static scoped_ptr<Feature> Parse(const DictionaryValue* value); | 73 void set_name(const std::string& name) { name_ = name; } |
71 | 74 |
72 // Gets the platform the code is currently running on. | 75 // Gets the platform the code is currently running on. |
73 static Platform GetCurrentPlatform(); | 76 static Platform GetCurrentPlatform(); |
74 | 77 |
75 // Gets the Feature::Location value for the specified Extension::Location. | 78 // Gets the Feature::Location value for the specified Extension::Location. |
76 static Location ConvertLocation(Extension::Location extension_location); | 79 static Location ConvertLocation(Extension::Location extension_location); |
77 | 80 |
78 std::set<std::string>* whitelist() { return &whitelist_; } | 81 std::set<std::string>* whitelist() { return &whitelist_; } |
79 std::set<Extension::Type>* extension_types() { return &extension_types_; } | 82 std::set<Extension::Type>* extension_types() { return &extension_types_; } |
80 std::set<Context>* contexts() { return &contexts_; } | 83 std::set<Context>* contexts() { return &contexts_; } |
81 | 84 |
82 Location location() const { return location_; } | 85 Location location() const { return location_; } |
83 void set_location(Location location) { location_ = location; } | 86 void set_location(Location location) { location_ = location; } |
84 | 87 |
85 Platform platform() const { return platform_; } | 88 Platform platform() const { return platform_; } |
86 void set_platform(Platform platform) { platform_ = platform; } | 89 void set_platform(Platform platform) { platform_ = platform; } |
87 | 90 |
88 int min_manifest_version() const { return min_manifest_version_; } | 91 int min_manifest_version() const { return min_manifest_version_; } |
89 void set_min_manifest_version(int min_manifest_version) { | 92 void set_min_manifest_version(int min_manifest_version) { |
90 min_manifest_version_ = min_manifest_version; | 93 min_manifest_version_ = min_manifest_version; |
91 } | 94 } |
92 | 95 |
93 int max_manifest_version() const { return max_manifest_version_; } | 96 int max_manifest_version() const { return max_manifest_version_; } |
94 void set_max_manifest_version(int max_manifest_version) { | 97 void set_max_manifest_version(int max_manifest_version) { |
95 max_manifest_version_ = max_manifest_version; | 98 max_manifest_version_ = max_manifest_version; |
96 } | 99 } |
97 | 100 |
98 // Returns true if the feature is available to the specified extension. Use | 101 // Parses the JSON representation of a feature into the fields of this object. |
99 // this overload for features that are not associated with a specific context. | 102 // Unspecified values in the JSON are not modified in the object. This allows |
100 Availability IsAvailable(const Extension* extension) { | 103 // us to implement inheritance by parsing one value after another. |
101 return IsAvailable(extension, UNSPECIFIED_CONTEXT); | 104 void Parse(const DictionaryValue* value); |
| 105 |
| 106 // Returns true if the feature contains the same values as another. |
| 107 bool Equals(const Feature& other) const; |
| 108 |
| 109 // Returns true if the feature is available to be parsed into a new extension |
| 110 // manifest. |
| 111 Availability IsAvailableToManifest(const std::string& extension_id, |
| 112 Extension::Type type, |
| 113 Location location, |
| 114 int manifest_version) const { |
| 115 return IsAvailableToManifest(extension_id, type, location, manifest_version, |
| 116 GetCurrentPlatform()); |
102 } | 117 } |
| 118 Availability IsAvailableToManifest(const std::string& extension_id, |
| 119 Extension::Type type, |
| 120 Location location, |
| 121 int manifest_version, |
| 122 Platform platform) const; |
103 | 123 |
104 // Returns true if the feature is available to the specified extension, in the | 124 // Returns true if the feature is available to be used in the specified |
105 // specified context type. | 125 // extension and context. |
106 Availability IsAvailable(const Extension* extension, Context context) { | 126 Availability IsAvailableToContext(const Extension* extension, |
107 return IsAvailable(extension->id(), extension->GetType(), | 127 Context context) const { |
108 ConvertLocation(extension->location()), context, | 128 return IsAvailableToContext(extension, context, GetCurrentPlatform()); |
109 GetCurrentPlatform(), | |
110 extension->manifest_version()); | |
111 } | 129 } |
112 | 130 virtual Availability IsAvailableToContext(const Extension* extension, |
113 // Returns true if the feature is available to extensions with the specified | 131 Context context, |
114 // properties. Use this overload for features that are not associated with a | 132 Platform platform) const; |
115 // specific context, and when a full Extension object is not available. | |
116 Availability IsAvailable(const std::string& extension_id, | |
117 Extension::Type type, Location location, | |
118 int manifest_version) { | |
119 return IsAvailable(extension_id, type, location, UNSPECIFIED_CONTEXT, | |
120 GetCurrentPlatform(), manifest_version); | |
121 } | |
122 | |
123 // Returns true if the feature is available to extensions with the specified | |
124 // properties, in the specified context type, and on the specified platform. | |
125 // This overload is mainly used for testing. | |
126 Availability IsAvailable(const std::string& extension_id, | |
127 Extension::Type type, Location location, | |
128 Context context, Platform platform, | |
129 int manifest_version); | |
130 | 133 |
131 // Returns an error message for an Availability code. | 134 // Returns an error message for an Availability code. |
132 std::string GetErrorMessage(Availability result); | 135 std::string GetErrorMessage(Availability result); |
133 | 136 |
134 private: | 137 private: |
| 138 std::string name_; |
| 139 |
135 // For clarify and consistency, we handle the default value of each of these | 140 // For clarify and consistency, we handle the default value of each of these |
136 // members the same way: it matches everything. It is up to the higher level | 141 // members the same way: it matches everything. It is up to the higher level |
137 // code that reads Features out of static data to validate that data and set | 142 // code that reads Features out of static data to validate that data and set |
138 // sensible defaults. | 143 // sensible defaults. |
139 std::set<std::string> whitelist_; | 144 std::set<std::string> whitelist_; |
140 std::set<Extension::Type> extension_types_; | 145 std::set<Extension::Type> extension_types_; |
141 std::set<Context> contexts_; | 146 std::set<Context> contexts_; |
142 Location location_; // we only care about component/not-component now | 147 Location location_; // we only care about component/not-component now |
143 Platform platform_; // we only care about chromeos/not-chromeos now | 148 Platform platform_; // we only care about chromeos/not-chromeos now |
144 int min_manifest_version_; | 149 int min_manifest_version_; |
145 int max_manifest_version_; | 150 int max_manifest_version_; |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(Feature); | |
148 }; | 151 }; |
149 | 152 |
150 } // namespace extensions | 153 } // namespace extensions |
151 | 154 |
152 #endif // CHROME_COMMON_EXTENSIONS_FEATURE_H_ | 155 #endif // CHROME_COMMON_EXTENSIONS_FEATURE_H_ |
OLD | NEW |