| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/common/extensions/api/generated_schemas.h" | |
| 20 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/extensions_client.h" |
| 21 #include "extensions/common/features/feature.h" | 21 #include "extensions/common/features/feature.h" |
| 22 #include "extensions/common/features/feature_provider.h" | 22 #include "extensions/common/features/feature_provider.h" |
| 23 #include "extensions/common/permissions/permission_set.h" | 23 #include "extensions/common/permissions/permission_set.h" |
| 24 #include "extensions/common/permissions/permissions_data.h" | 24 #include "extensions/common/permissions/permissions_data.h" |
| 25 #include "grit/common_resources.h" | 25 #include "grit/common_resources.h" |
| 26 #include "grit/extensions_api_resources.h" | 26 #include "grit/extensions_api_resources.h" |
| 27 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 29 | 29 |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 | 31 |
| 32 using api::GeneratedSchemas; | |
| 33 | |
| 34 namespace { | 32 namespace { |
| 35 | 33 |
| 36 const char* kChildKinds[] = { | 34 const char* kChildKinds[] = { |
| 37 "functions", | 35 "functions", |
| 38 "events" | 36 "events" |
| 39 }; | 37 }; |
| 40 | 38 |
| 41 base::StringPiece ReadFromResource(int resource_id) { | 39 base::StringPiece ReadFromResource(int resource_id) { |
| 42 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 40 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 43 resource_id); | 41 resource_id); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 191 } |
| 194 | 192 |
| 195 *feature_type = full_name.substr(0, colon_index); | 193 *feature_type = full_name.substr(0, colon_index); |
| 196 *feature_name = full_name.substr(colon_index + 1); | 194 *feature_name = full_name.substr(colon_index + 1); |
| 197 } | 195 } |
| 198 | 196 |
| 199 void ExtensionAPI::LoadSchema(const std::string& name, | 197 void ExtensionAPI::LoadSchema(const std::string& name, |
| 200 const base::StringPiece& schema) { | 198 const base::StringPiece& schema) { |
| 201 scoped_ptr<base::ListValue> schema_list(LoadSchemaList(name, schema)); | 199 scoped_ptr<base::ListValue> schema_list(LoadSchemaList(name, schema)); |
| 202 std::string schema_namespace; | 200 std::string schema_namespace; |
| 203 | 201 extensions::ExtensionsClient* extensions_client = |
| 202 extensions::ExtensionsClient::Get(); |
| 203 DCHECK(extensions_client); |
| 204 while (!schema_list->empty()) { | 204 while (!schema_list->empty()) { |
| 205 base::DictionaryValue* schema = NULL; | 205 base::DictionaryValue* schema = NULL; |
| 206 { | 206 { |
| 207 scoped_ptr<base::Value> value; | 207 scoped_ptr<base::Value> value; |
| 208 schema_list->Remove(schema_list->GetSize() - 1, &value); | 208 schema_list->Remove(schema_list->GetSize() - 1, &value); |
| 209 CHECK(value.release()->GetAsDictionary(&schema)); | 209 CHECK(value.release()->GetAsDictionary(&schema)); |
| 210 } | 210 } |
| 211 | 211 |
| 212 CHECK(schema->GetString("namespace", &schema_namespace)); | 212 CHECK(schema->GetString("namespace", &schema_namespace)); |
| 213 PrefixWithNamespace(schema_namespace, schema); | 213 PrefixWithNamespace(schema_namespace, schema); |
| 214 schemas_[schema_namespace] = make_linked_ptr(schema); | 214 schemas_[schema_namespace] = make_linked_ptr(schema); |
| 215 if (!GeneratedSchemas::IsGenerated(schema_namespace)) | 215 if (!extensions_client->IsAPISchemaGenerated(schema_namespace)) |
| 216 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace)); | 216 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace)); |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { | 220 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { |
| 221 } | 221 } |
| 222 | 222 |
| 223 ExtensionAPI::~ExtensionAPI() { | 223 ExtensionAPI::~ExtensionAPI() { |
| 224 } | 224 } |
| 225 | 225 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 const std::string& full_name) { | 337 const std::string& full_name) { |
| 338 std::string child_name; | 338 std::string child_name; |
| 339 std::string api_name = GetAPINameFromFullName(full_name, &child_name); | 339 std::string api_name = GetAPINameFromFullName(full_name, &child_name); |
| 340 | 340 |
| 341 const base::DictionaryValue* result = NULL; | 341 const base::DictionaryValue* result = NULL; |
| 342 SchemaMap::iterator maybe_schema = schemas_.find(api_name); | 342 SchemaMap::iterator maybe_schema = schemas_.find(api_name); |
| 343 if (maybe_schema != schemas_.end()) { | 343 if (maybe_schema != schemas_.end()) { |
| 344 result = maybe_schema->second.get(); | 344 result = maybe_schema->second.get(); |
| 345 } else { | 345 } else { |
| 346 // Might not have loaded yet; or might just not exist. | 346 // Might not have loaded yet; or might just not exist. |
| 347 UnloadedSchemaMap::iterator maybe_schema_resource = | 347 UnloadedSchemaMap::iterator maybe_schema_resource = |
| 348 unloaded_schemas_.find(api_name); | 348 unloaded_schemas_.find(api_name); |
| 349 extensions::ExtensionsClient* extensions_client = |
| 350 extensions::ExtensionsClient::Get(); |
| 351 DCHECK(extensions_client); |
| 349 if (maybe_schema_resource != unloaded_schemas_.end()) { | 352 if (maybe_schema_resource != unloaded_schemas_.end()) { |
| 350 LoadSchema(maybe_schema_resource->first, | 353 LoadSchema(maybe_schema_resource->first, |
| 351 ReadFromResource(maybe_schema_resource->second)); | 354 ReadFromResource(maybe_schema_resource->second)); |
| 352 } else if (default_configuration_initialized_ && | 355 } else if (default_configuration_initialized_ && |
| 353 GeneratedSchemas::IsGenerated(api_name)) { | 356 extensions_client->IsAPISchemaGenerated(api_name)) { |
| 354 LoadSchema(api_name, GeneratedSchemas::Get(api_name)); | 357 LoadSchema(api_name, extensions_client->GetAPISchema(api_name)); |
| 355 } else { | 358 } else { |
| 356 return NULL; | 359 return NULL; |
| 357 } | 360 } |
| 358 | 361 |
| 359 maybe_schema = schemas_.find(api_name); | 362 maybe_schema = schemas_.find(api_name); |
| 360 CHECK(schemas_.end() != maybe_schema); | 363 CHECK(schemas_.end() != maybe_schema); |
| 361 result = maybe_schema->second.get(); | 364 result = maybe_schema->second.get(); |
| 362 } | 365 } |
| 363 | 366 |
| 364 if (!child_name.empty()) | 367 if (!child_name.empty()) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 383 std::string child_name; | 386 std::string child_name; |
| 384 feature = provider->second->GetFeature( | 387 feature = provider->second->GetFeature( |
| 385 GetAPINameFromFullName(feature_name, &child_name)); | 388 GetAPINameFromFullName(feature_name, &child_name)); |
| 386 } | 389 } |
| 387 return feature; | 390 return feature; |
| 388 } | 391 } |
| 389 | 392 |
| 390 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name, | 393 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name, |
| 391 std::string* child_name) { | 394 std::string* child_name) { |
| 392 std::string api_name_candidate = full_name; | 395 std::string api_name_candidate = full_name; |
| 396 extensions::ExtensionsClient* extensions_client = |
| 397 extensions::ExtensionsClient::Get(); |
| 398 DCHECK(extensions_client); |
| 393 while (true) { | 399 while (true) { |
| 394 if (schemas_.find(api_name_candidate) != schemas_.end() || | 400 if (schemas_.find(api_name_candidate) != schemas_.end() || |
| 395 GeneratedSchemas::IsGenerated(api_name_candidate) || | 401 extensions_client->IsAPISchemaGenerated(api_name_candidate) || |
| 396 unloaded_schemas_.find(api_name_candidate) != unloaded_schemas_.end()) { | 402 unloaded_schemas_.find(api_name_candidate) != unloaded_schemas_.end()) { |
| 397 std::string result = api_name_candidate; | 403 std::string result = api_name_candidate; |
| 398 | 404 |
| 399 if (child_name) { | 405 if (child_name) { |
| 400 if (result.length() < full_name.length()) | 406 if (result.length() < full_name.length()) |
| 401 *child_name = full_name.substr(result.length() + 1); | 407 *child_name = full_name.substr(result.length() + 1); |
| 402 else | 408 else |
| 403 *child_name = ""; | 409 *child_name = ""; |
| 404 } | 410 } |
| 405 | 411 |
| 406 return result; | 412 return result; |
| 407 } | 413 } |
| 408 | 414 |
| 409 size_t last_dot_index = api_name_candidate.rfind('.'); | 415 size_t last_dot_index = api_name_candidate.rfind('.'); |
| 410 if (last_dot_index == std::string::npos) | 416 if (last_dot_index == std::string::npos) |
| 411 break; | 417 break; |
| 412 | 418 |
| 413 api_name_candidate = api_name_candidate.substr(0, last_dot_index); | 419 api_name_candidate = api_name_candidate.substr(0, last_dot_index); |
| 414 } | 420 } |
| 415 | 421 |
| 416 *child_name = ""; | 422 *child_name = ""; |
| 417 return std::string(); | 423 return std::string(); |
| 418 } | 424 } |
| 419 | 425 |
| 420 } // namespace extensions | 426 } // namespace extensions |
| OLD | NEW |