| 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> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| 23 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
| 24 #include "extensions/common/extensions_client.h" | 24 #include "extensions/common/extensions_client.h" |
| 25 #include "extensions/common/features/feature.h" | 25 #include "extensions/common/features/feature.h" |
| 26 #include "extensions/common/features/feature_provider.h" | 26 #include "extensions/common/features/feature_provider.h" |
| 27 #include "extensions/common/features/simple_feature.h" | 27 #include "extensions/common/features/simple_feature.h" |
| 28 #include "extensions/common/permissions/permission_set.h" | 28 #include "extensions/common/permissions/permission_set.h" |
| 29 #include "extensions/common/permissions/permissions_data.h" | 29 #include "extensions/common/permissions/permissions_data.h" |
| 30 #include "ui/base/resource/resource_bundle.h" | |
| 31 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 32 | 31 |
| 33 namespace extensions { | 32 namespace extensions { |
| 34 | 33 |
| 35 namespace { | 34 namespace { |
| 36 | 35 |
| 37 const char* kChildKinds[] = { | 36 const char* kChildKinds[] = { |
| 38 "functions", | 37 "functions", |
| 39 "events" | 38 "events" |
| 40 }; | 39 }; |
| 41 | 40 |
| 42 base::StringPiece ReadFromResource(int resource_id) { | |
| 43 return ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 44 resource_id); | |
| 45 } | |
| 46 | |
| 47 std::unique_ptr<base::ListValue> LoadSchemaList( | 41 std::unique_ptr<base::ListValue> LoadSchemaList( |
| 48 const std::string& name, | 42 const std::string& name, |
| 49 const base::StringPiece& schema) { | 43 const base::StringPiece& schema) { |
| 50 std::string error_message; | 44 std::string error_message; |
| 51 std::unique_ptr<base::Value> result(base::JSONReader::ReadAndReturnError( | 45 std::unique_ptr<base::Value> result(base::JSONReader::ReadAndReturnError( |
| 52 schema, | 46 schema, |
| 53 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN, // options | 47 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN, // options |
| 54 NULL, // error code | 48 NULL, // error code |
| 55 &error_message)); | 49 &error_message)); |
| 56 | 50 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 std::unique_ptr<base::DictionaryValue> schema; | 215 std::unique_ptr<base::DictionaryValue> schema; |
| 222 { | 216 { |
| 223 std::unique_ptr<base::Value> val; | 217 std::unique_ptr<base::Value> val; |
| 224 schema_list->Erase(schema_list->begin(), &val); | 218 schema_list->Erase(schema_list->begin(), &val); |
| 225 schema = base::DictionaryValue::From(std::move(val)); | 219 schema = base::DictionaryValue::From(std::move(val)); |
| 226 CHECK(schema); | 220 CHECK(schema); |
| 227 } | 221 } |
| 228 CHECK(schema->GetString("namespace", &schema_namespace)); | 222 CHECK(schema->GetString("namespace", &schema_namespace)); |
| 229 PrefixWithNamespace(schema_namespace, schema.get()); | 223 PrefixWithNamespace(schema_namespace, schema.get()); |
| 230 schemas_[schema_namespace] = std::move(schema); | 224 schemas_[schema_namespace] = std::move(schema); |
| 231 if (!extensions_client->IsAPISchemaGenerated(schema_namespace)) | |
| 232 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace)); | |
| 233 } | 225 } |
| 234 } | 226 } |
| 235 | 227 |
| 236 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { | 228 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { |
| 237 } | 229 } |
| 238 | 230 |
| 239 ExtensionAPI::~ExtensionAPI() { | 231 ExtensionAPI::~ExtensionAPI() { |
| 240 } | 232 } |
| 241 | 233 |
| 242 void ExtensionAPI::InitDefaultConfiguration() { | 234 void ExtensionAPI::InitDefaultConfiguration() { |
| 243 const char* names[] = {"api", "manifest", "permission"}; | 235 const char* names[] = {"api", "manifest", "permission"}; |
| 244 for (size_t i = 0; i < arraysize(names); ++i) | 236 for (size_t i = 0; i < arraysize(names); ++i) |
| 245 RegisterDependencyProvider(names[i], FeatureProvider::GetByName(names[i])); | 237 RegisterDependencyProvider(names[i], FeatureProvider::GetByName(names[i])); |
| 246 | 238 |
| 247 ExtensionsClient::Get()->RegisterAPISchemaResources(this); | |
| 248 default_configuration_initialized_ = true; | 239 default_configuration_initialized_ = true; |
| 249 } | 240 } |
| 250 | 241 |
| 251 void ExtensionAPI::RegisterSchemaResource(const std::string& name, | |
| 252 int resource_id) { | |
| 253 unloaded_schemas_[name] = resource_id; | |
| 254 } | |
| 255 | |
| 256 void ExtensionAPI::RegisterDependencyProvider(const std::string& name, | 242 void ExtensionAPI::RegisterDependencyProvider(const std::string& name, |
| 257 const FeatureProvider* provider) { | 243 const FeatureProvider* provider) { |
| 258 dependency_providers_[name] = provider; | 244 dependency_providers_[name] = provider; |
| 259 } | 245 } |
| 260 | 246 |
| 261 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api, | 247 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api, |
| 262 const Extension* extension, | 248 const Extension* extension, |
| 263 Feature::Context context, | 249 Feature::Context context, |
| 264 const GURL& url) { | 250 const GURL& url) { |
| 265 FeatureProviderMap::iterator provider = dependency_providers_.find("api"); | 251 FeatureProviderMap::iterator provider = dependency_providers_.find("api"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 const std::string& full_name) { | 287 const std::string& full_name) { |
| 302 std::string child_name; | 288 std::string child_name; |
| 303 std::string api_name = GetAPINameFromFullName(full_name, &child_name); | 289 std::string api_name = GetAPINameFromFullName(full_name, &child_name); |
| 304 | 290 |
| 305 const base::DictionaryValue* result = NULL; | 291 const base::DictionaryValue* result = NULL; |
| 306 SchemaMap::iterator maybe_schema = schemas_.find(api_name); | 292 SchemaMap::iterator maybe_schema = schemas_.find(api_name); |
| 307 if (maybe_schema != schemas_.end()) { | 293 if (maybe_schema != schemas_.end()) { |
| 308 result = maybe_schema->second.get(); | 294 result = maybe_schema->second.get(); |
| 309 } else { | 295 } else { |
| 310 // Might not have loaded yet; or might just not exist. | 296 // Might not have loaded yet; or might just not exist. |
| 311 UnloadedSchemaMap::iterator maybe_schema_resource = | |
| 312 unloaded_schemas_.find(api_name); | |
| 313 extensions::ExtensionsClient* extensions_client = | 297 extensions::ExtensionsClient* extensions_client = |
| 314 extensions::ExtensionsClient::Get(); | 298 extensions::ExtensionsClient::Get(); |
| 315 DCHECK(extensions_client); | 299 DCHECK(extensions_client); |
| 316 if (maybe_schema_resource != unloaded_schemas_.end()) { | 300 if (default_configuration_initialized_ && |
| 317 LoadSchema(maybe_schema_resource->first, | 301 extensions_client->IsAPISchemaGenerated(api_name)) { |
| 318 ReadFromResource(maybe_schema_resource->second)); | |
| 319 } else if (default_configuration_initialized_ && | |
| 320 extensions_client->IsAPISchemaGenerated(api_name)) { | |
| 321 LoadSchema(api_name, extensions_client->GetAPISchema(api_name)); | 302 LoadSchema(api_name, extensions_client->GetAPISchema(api_name)); |
| 322 } else { | 303 } else { |
| 323 return NULL; | 304 return nullptr; |
| 324 } | 305 } |
| 325 | 306 |
| 326 maybe_schema = schemas_.find(api_name); | 307 maybe_schema = schemas_.find(api_name); |
| 327 CHECK(schemas_.end() != maybe_schema); | 308 CHECK(schemas_.end() != maybe_schema); |
| 328 result = maybe_schema->second.get(); | 309 result = maybe_schema->second.get(); |
| 329 } | 310 } |
| 330 | 311 |
| 331 if (!child_name.empty()) | 312 if (!child_name.empty()) |
| 332 result = GetSchemaChild(result, child_name); | 313 result = GetSchemaChild(result, child_name); |
| 333 | 314 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 350 std::string child_name; | 331 std::string child_name; |
| 351 feature = provider->second->GetFeature( | 332 feature = provider->second->GetFeature( |
| 352 GetAPINameFromFullName(feature_name, &child_name)); | 333 GetAPINameFromFullName(feature_name, &child_name)); |
| 353 } | 334 } |
| 354 return feature; | 335 return feature; |
| 355 } | 336 } |
| 356 | 337 |
| 357 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name, | 338 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name, |
| 358 std::string* child_name) { | 339 std::string* child_name) { |
| 359 std::string api_name_candidate = full_name; | 340 std::string api_name_candidate = full_name; |
| 360 extensions::ExtensionsClient* extensions_client = | 341 ExtensionsClient* extensions_client = ExtensionsClient::Get(); |
| 361 extensions::ExtensionsClient::Get(); | |
| 362 DCHECK(extensions_client); | 342 DCHECK(extensions_client); |
| 363 while (true) { | 343 while (true) { |
| 364 if (schemas_.find(api_name_candidate) != schemas_.end() || | 344 if (IsKnownAPI(api_name_candidate, extensions_client)) { |
| 365 extensions_client->IsAPISchemaGenerated(api_name_candidate) || | |
| 366 unloaded_schemas_.find(api_name_candidate) != unloaded_schemas_.end()) { | |
| 367 std::string result = api_name_candidate; | |
| 368 | |
| 369 if (child_name) { | 345 if (child_name) { |
| 370 if (result.length() < full_name.length()) | 346 if (api_name_candidate.length() < full_name.length()) |
| 371 *child_name = full_name.substr(result.length() + 1); | 347 *child_name = full_name.substr(api_name_candidate.length() + 1); |
| 372 else | 348 else |
| 373 *child_name = ""; | 349 *child_name = ""; |
| 374 } | 350 } |
| 375 | 351 return api_name_candidate; |
| 376 return result; | |
| 377 } | 352 } |
| 378 | 353 |
| 379 size_t last_dot_index = api_name_candidate.rfind('.'); | 354 size_t last_dot_index = api_name_candidate.rfind('.'); |
| 380 if (last_dot_index == std::string::npos) | 355 if (last_dot_index == std::string::npos) |
| 381 break; | 356 break; |
| 382 | 357 |
| 383 api_name_candidate = api_name_candidate.substr(0, last_dot_index); | 358 api_name_candidate = api_name_candidate.substr(0, last_dot_index); |
| 384 } | 359 } |
| 385 | 360 |
| 386 *child_name = ""; | 361 *child_name = ""; |
| 387 return std::string(); | 362 return std::string(); |
| 388 } | 363 } |
| 389 | 364 |
| 365 bool ExtensionAPI::IsKnownAPI(const std::string& name, |
| 366 ExtensionsClient* client) { |
| 367 return schemas_.find(name) != schemas_.end() || |
| 368 client->IsAPISchemaGenerated(name); |
| 369 } |
| 370 |
| 390 } // namespace extensions | 371 } // namespace extensions |
| OLD | NEW |