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