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 #include "chrome/common/extensions/feature.h" | 5 #include "chrome/common/extensions/feature.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 | 13 |
| 14 namespace extensions { |
| 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 struct Mappings { | 18 struct Mappings { |
17 Mappings() { | 19 Mappings() { |
18 extension_types["extension"] = Extension::TYPE_EXTENSION; | 20 extension_types["extension"] = Extension::TYPE_EXTENSION; |
19 extension_types["theme"] = Extension::TYPE_THEME; | 21 extension_types["theme"] = Extension::TYPE_THEME; |
20 extension_types["packaged_app"] = Extension::TYPE_PACKAGED_APP; | 22 extension_types["packaged_app"] = Extension::TYPE_PACKAGED_APP; |
21 extension_types["hosted_app"] = Extension::TYPE_HOSTED_APP; | 23 extension_types["hosted_app"] = Extension::TYPE_HOSTED_APP; |
22 extension_types["platform_app"] = Extension::TYPE_PLATFORM_APP; | 24 extension_types["platform_app"] = Extension::TYPE_PLATFORM_APP; |
23 | 25 |
(...skipping 13 matching lines...) Expand all Loading... |
37 std::map<std::string, extensions::Feature::Context> contexts; | 39 std::map<std::string, extensions::Feature::Context> contexts; |
38 std::map<std::string, extensions::Feature::Location> locations; | 40 std::map<std::string, extensions::Feature::Location> locations; |
39 std::map<std::string, extensions::Feature::Platform> platforms; | 41 std::map<std::string, extensions::Feature::Platform> platforms; |
40 }; | 42 }; |
41 | 43 |
42 static base::LazyInstance<Mappings> g_mappings = | 44 static base::LazyInstance<Mappings> g_mappings = |
43 LAZY_INSTANCE_INITIALIZER; | 45 LAZY_INSTANCE_INITIALIZER; |
44 | 46 |
45 // TODO(aa): Can we replace all this manual parsing with JSON schema stuff? | 47 // TODO(aa): Can we replace all this manual parsing with JSON schema stuff? |
46 | 48 |
47 void ParseSet(const DictionaryValue* value, | 49 void ParseSetImpl(const DictionaryValue* value, |
48 const std::string& property, | 50 const std::string& property, |
49 std::set<std::string>* set) { | 51 std::set<std::string>* set) { |
50 ListValue* list_value = NULL; | 52 ListValue* list_value = NULL; |
51 if (!value->GetList(property, &list_value)) | 53 if (!value->GetList(property, &list_value)) |
52 return; | 54 return; |
53 | 55 |
54 set->clear(); | 56 set->clear(); |
55 for (size_t i = 0; i < list_value->GetSize(); ++i) { | 57 for (size_t i = 0; i < list_value->GetSize(); ++i) { |
56 std::string str_val; | 58 std::string str_val; |
57 CHECK(list_value->GetString(i, &str_val)) << property << " " << i; | 59 CHECK(list_value->GetString(i, &str_val)) << property << " " << i; |
58 set->insert(str_val); | 60 set->insert(str_val); |
59 } | 61 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 if (property_string == "all") { | 98 if (property_string == "all") { |
97 for (typename std::map<std::string, T>::const_iterator j = | 99 for (typename std::map<std::string, T>::const_iterator j = |
98 mapping.begin(); j != mapping.end(); ++j) { | 100 mapping.begin(); j != mapping.end(); ++j) { |
99 enum_set->insert(j->second); | 101 enum_set->insert(j->second); |
100 } | 102 } |
101 } | 103 } |
102 return; | 104 return; |
103 } | 105 } |
104 | 106 |
105 std::set<std::string> string_set; | 107 std::set<std::string> string_set; |
106 ParseSet(value, property, &string_set); | 108 ParseSetImpl(value, property, &string_set); |
107 for (std::set<std::string>::iterator iter = string_set.begin(); | 109 for (std::set<std::string>::iterator iter = string_set.begin(); |
108 iter != string_set.end(); ++iter) { | 110 iter != string_set.end(); ++iter) { |
109 T enum_value = static_cast<T>(0); | 111 T enum_value = static_cast<T>(0); |
110 ParseEnum(*iter, &enum_value, mapping); | 112 ParseEnum(*iter, &enum_value, mapping); |
111 enum_set->insert(enum_value); | 113 enum_set->insert(enum_value); |
112 } | 114 } |
113 } | 115 } |
114 | 116 |
115 } // namespace | 117 } // namespace |
116 | 118 |
117 namespace extensions { | |
118 | |
119 Feature::Feature() | 119 Feature::Feature() |
120 : location_(UNSPECIFIED_LOCATION), | 120 : location_(UNSPECIFIED_LOCATION), |
121 platform_(UNSPECIFIED_PLATFORM), | 121 platform_(UNSPECIFIED_PLATFORM), |
122 min_manifest_version_(0), | 122 min_manifest_version_(0), |
123 max_manifest_version_(0) { | 123 max_manifest_version_(0) { |
124 } | 124 } |
125 | 125 |
126 Feature::Feature(const Feature& other) | 126 Feature::Feature(const Feature& other) |
127 : whitelist_(other.whitelist_), | 127 : name_(other.name_), |
| 128 whitelist_(other.whitelist_), |
128 extension_types_(other.extension_types_), | 129 extension_types_(other.extension_types_), |
129 contexts_(other.contexts_), | 130 contexts_(other.contexts_), |
130 location_(other.location_), | 131 location_(other.location_), |
131 platform_(other.platform_), | 132 platform_(other.platform_), |
132 min_manifest_version_(other.min_manifest_version_), | 133 min_manifest_version_(other.min_manifest_version_), |
133 max_manifest_version_(other.max_manifest_version_) { | 134 max_manifest_version_(other.max_manifest_version_) { |
134 } | 135 } |
135 | 136 |
136 Feature::~Feature() { | 137 Feature::~Feature() { |
137 } | 138 } |
138 | 139 |
139 bool Feature::Equals(const Feature& other) const { | 140 bool Feature::Equals(const Feature& other) const { |
140 return whitelist_ == other.whitelist_ && | 141 return name_ == other.name_ && |
| 142 whitelist_ == other.whitelist_ && |
141 extension_types_ == other.extension_types_ && | 143 extension_types_ == other.extension_types_ && |
142 contexts_ == other.contexts_ && | 144 contexts_ == other.contexts_ && |
143 location_ == other.location_ && | 145 location_ == other.location_ && |
144 platform_ == other.platform_ && | 146 platform_ == other.platform_ && |
145 min_manifest_version_ == other.min_manifest_version_ && | 147 min_manifest_version_ == other.min_manifest_version_ && |
146 max_manifest_version_ == other.max_manifest_version_; | 148 max_manifest_version_ == other.max_manifest_version_; |
147 } | 149 } |
148 | 150 |
| 151 bool Feature::IsEmpty() const { |
| 152 Feature test; |
| 153 test.set_name(name()); |
| 154 return Equals(test); |
| 155 } |
| 156 |
149 // static | 157 // static |
150 Feature::Platform Feature::GetCurrentPlatform() { | 158 Feature::Platform Feature::GetCurrentPlatform() { |
151 #if defined(OS_CHROMEOS) | 159 #if defined(OS_CHROMEOS) |
152 return CHROMEOS_PLATFORM; | 160 return CHROMEOS_PLATFORM; |
153 #else | 161 #else |
154 return UNSPECIFIED_PLATFORM; | 162 return UNSPECIFIED_PLATFORM; |
155 #endif | 163 #endif |
156 } | 164 } |
157 | 165 |
158 // static | 166 // static |
159 Feature::Location Feature::ConvertLocation(Extension::Location location) { | 167 Feature::Location Feature::ConvertLocation(Extension::Location location) { |
160 if (location == Extension::COMPONENT) | 168 if (location == Extension::COMPONENT) |
161 return COMPONENT_LOCATION; | 169 return COMPONENT_LOCATION; |
162 else | 170 else |
163 return UNSPECIFIED_LOCATION; | 171 return UNSPECIFIED_LOCATION; |
164 } | 172 } |
165 | 173 |
| 174 // static |
| 175 void Feature::ParseSet(const DictionaryValue* value, |
| 176 const std::string& property, |
| 177 std::set<std::string>* set) { |
| 178 ParseSetImpl(value, property, set); |
| 179 } |
| 180 |
166 void Feature::Parse(const DictionaryValue* value) { | 181 void Feature::Parse(const DictionaryValue* value) { |
167 ParseSet(value, "whitelist", &whitelist_); | 182 ParseSetImpl(value, "whitelist", &whitelist_); |
168 ParseEnumSet<Extension::Type>(value, "extension_types", &extension_types_, | 183 ParseEnumSet<Extension::Type>(value, "extension_types", &extension_types_, |
169 g_mappings.Get().extension_types); | 184 g_mappings.Get().extension_types); |
170 ParseEnumSet<Context>(value, "contexts", &contexts_, | 185 ParseEnumSet<Context>(value, "contexts", &contexts_, |
171 g_mappings.Get().contexts); | 186 g_mappings.Get().contexts); |
172 ParseEnum<Location>(value, "location", &location_, | 187 ParseEnum<Location>(value, "location", &location_, |
173 g_mappings.Get().locations); | 188 g_mappings.Get().locations); |
174 ParseEnum<Platform>(value, "platform", &platform_, | 189 ParseEnum<Platform>(value, "platform", &platform_, |
175 g_mappings.Get().platforms); | 190 g_mappings.Get().platforms); |
176 value->GetInteger("min_manifest_version", &min_manifest_version_); | 191 value->GetInteger("min_manifest_version", &min_manifest_version_); |
177 value->GetInteger("max_manifest_version", &max_manifest_version_); | 192 value->GetInteger("max_manifest_version", &max_manifest_version_); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 280 |
266 if (!contexts_.empty() && | 281 if (!contexts_.empty() && |
267 contexts_.find(context) == contexts_.end()) { | 282 contexts_.find(context) == contexts_.end()) { |
268 return INVALID_CONTEXT; | 283 return INVALID_CONTEXT; |
269 } | 284 } |
270 | 285 |
271 return IS_AVAILABLE; | 286 return IS_AVAILABLE; |
272 } | 287 } |
273 | 288 |
274 } // namespace | 289 } // namespace |
OLD | NEW |