OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/common/extension_api.h" | 5 #include "extensions/common/extension_api.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 std::string("Unknown feature: ") + full_name); | 205 std::string("Unknown feature: ") + full_name); |
206 } | 206 } |
207 return feature->IsAvailableToContext(extension, context, url); | 207 return feature->IsAvailableToContext(extension, context, url); |
208 } | 208 } |
209 | 209 |
210 bool ExtensionAPI::IsAvailableToWebUI(const std::string& name, | 210 bool ExtensionAPI::IsAvailableToWebUI(const std::string& name, |
211 const GURL& url) { | 211 const GURL& url) { |
212 return IsAvailable(name, NULL, Feature::WEBUI_CONTEXT, url).is_available(); | 212 return IsAvailable(name, NULL, Feature::WEBUI_CONTEXT, url).is_available(); |
213 } | 213 } |
214 | 214 |
| 215 base::StringPiece ExtensionAPI::GetSchemaStringPiece( |
| 216 const std::string& api_name) { |
| 217 DCHECK_EQ(api_name, GetAPINameFromFullName(api_name, nullptr)); |
| 218 StringPieceMap::iterator cached = schema_strings_.find(api_name); |
| 219 if (cached != schema_strings_.end()) |
| 220 return cached->second; |
| 221 |
| 222 ExtensionsClient* client = ExtensionsClient::Get(); |
| 223 DCHECK(client); |
| 224 if (default_configuration_initialized_ && |
| 225 client->IsAPISchemaGenerated(api_name)) { |
| 226 base::StringPiece schema = client->GetAPISchema(api_name); |
| 227 CHECK(!schema.empty()); |
| 228 schema_strings_[api_name] = schema; |
| 229 return schema; |
| 230 } |
| 231 |
| 232 return base::StringPiece(); |
| 233 } |
| 234 |
215 const base::DictionaryValue* ExtensionAPI::GetSchema( | 235 const base::DictionaryValue* ExtensionAPI::GetSchema( |
216 const std::string& full_name) { | 236 const std::string& full_name) { |
217 std::string child_name; | 237 std::string child_name; |
218 std::string api_name = GetAPINameFromFullName(full_name, &child_name); | 238 std::string api_name = GetAPINameFromFullName(full_name, &child_name); |
219 | 239 |
220 const base::DictionaryValue* result = NULL; | 240 const base::DictionaryValue* result = NULL; |
221 SchemaMap::iterator maybe_schema = schemas_.find(api_name); | 241 SchemaMap::iterator maybe_schema = schemas_.find(api_name); |
222 if (maybe_schema != schemas_.end()) { | 242 if (maybe_schema != schemas_.end()) { |
223 result = maybe_schema->second.get(); | 243 result = maybe_schema->second.get(); |
224 } else { | 244 } else { |
225 // Might not have loaded yet; or might just not exist. | 245 base::StringPiece schema_string = GetSchemaStringPiece(api_name); |
226 extensions::ExtensionsClient* extensions_client = | 246 if (schema_string.empty()) |
227 extensions::ExtensionsClient::Get(); | |
228 DCHECK(extensions_client); | |
229 if (default_configuration_initialized_ && | |
230 extensions_client->IsAPISchemaGenerated(api_name)) { | |
231 LoadSchema(api_name, extensions_client->GetAPISchema(api_name)); | |
232 } else { | |
233 return nullptr; | 247 return nullptr; |
234 } | 248 LoadSchema(api_name, schema_string); |
235 | 249 |
236 maybe_schema = schemas_.find(api_name); | 250 maybe_schema = schemas_.find(api_name); |
237 CHECK(schemas_.end() != maybe_schema); | 251 CHECK(schemas_.end() != maybe_schema); |
238 result = maybe_schema->second.get(); | 252 result = maybe_schema->second.get(); |
239 } | 253 } |
240 | 254 |
241 if (!child_name.empty()) | 255 if (!child_name.empty()) |
242 result = GetSchemaChild(result, child_name); | 256 result = GetSchemaChild(result, child_name); |
243 | 257 |
244 return result; | 258 return result; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 return api_name_candidate; | 294 return api_name_candidate; |
281 } | 295 } |
282 | 296 |
283 size_t last_dot_index = api_name_candidate.rfind('.'); | 297 size_t last_dot_index = api_name_candidate.rfind('.'); |
284 if (last_dot_index == std::string::npos) | 298 if (last_dot_index == std::string::npos) |
285 break; | 299 break; |
286 | 300 |
287 api_name_candidate = api_name_candidate.substr(0, last_dot_index); | 301 api_name_candidate = api_name_candidate.substr(0, last_dot_index); |
288 } | 302 } |
289 | 303 |
290 *child_name = ""; | 304 if (child_name) |
| 305 *child_name = ""; |
291 return std::string(); | 306 return std::string(); |
292 } | 307 } |
293 | 308 |
294 bool ExtensionAPI::IsKnownAPI(const std::string& name, | 309 bool ExtensionAPI::IsKnownAPI(const std::string& name, |
295 ExtensionsClient* client) { | 310 ExtensionsClient* client) { |
296 return schemas_.find(name) != schemas_.end() || | 311 return schemas_.find(name) != schemas_.end() || |
297 client->IsAPISchemaGenerated(name); | 312 client->IsAPISchemaGenerated(name); |
298 } | 313 } |
299 | 314 |
300 } // namespace extensions | 315 } // namespace extensions |
OLD | NEW |