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/api/extension_api.h" | 5 #include "chrome/common/extensions/api/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/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
16 #include "base/string_split.h" | 16 #include "base/string_split.h" |
17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chrome/common/extensions/api/generated_schemas.h" | 19 #include "chrome/common/extensions/api/generated_schemas.h" |
20 #include "chrome/common/extensions/extension.h" | 20 #include "chrome/common/extensions/extension.h" |
21 #include "chrome/common/extensions/features/simple_feature_provider.h" | 21 #include "chrome/common/extensions/features/base_feature_provider.h" |
| 22 #include "chrome/common/extensions/features/simple_feature.h" |
22 #include "chrome/common/extensions/permissions/permission_set.h" | 23 #include "chrome/common/extensions/permissions/permission_set.h" |
23 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
24 #include "grit/common_resources.h" | 25 #include "grit/common_resources.h" |
25 #include "grit/extensions_api_resources.h" | 26 #include "grit/extensions_api_resources.h" |
26 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
27 | 28 |
28 using base::DictionaryValue; | 29 using base::DictionaryValue; |
29 using base::ListValue; | 30 using base::ListValue; |
30 using base::Value; | 31 using base::Value; |
31 | 32 |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 } | 280 } |
280 | 281 |
281 // Populate feature maps. | 282 // Populate feature maps. |
282 // TODO(aa): Consider not storing features that can never run on the current | 283 // TODO(aa): Consider not storing features that can never run on the current |
283 // machine (e.g., because of platform restrictions). | 284 // machine (e.g., because of platform restrictions). |
284 bool uses_feature_system = false; | 285 bool uses_feature_system = false; |
285 schema->GetBoolean("uses_feature_system", &uses_feature_system); | 286 schema->GetBoolean("uses_feature_system", &uses_feature_system); |
286 if (!uses_feature_system) | 287 if (!uses_feature_system) |
287 continue; | 288 continue; |
288 | 289 |
289 Feature* feature = new Feature(); | 290 SimpleFeature* feature = new SimpleFeature(); |
290 feature->set_name(schema_namespace); | 291 feature->set_name(schema_namespace); |
291 feature->Parse(schema); | 292 feature->Parse(schema); |
292 | 293 |
293 FeatureMap* schema_features = new FeatureMap(); | 294 FeatureMap* schema_features = new FeatureMap(); |
294 CHECK(features_.insert( | 295 CHECK(features_.insert( |
295 std::make_pair(schema_namespace, | 296 std::make_pair(schema_namespace, |
296 make_linked_ptr(schema_features))).second); | 297 make_linked_ptr(schema_features))).second); |
297 CHECK(schema_features->insert( | 298 CHECK(schema_features->insert( |
298 std::make_pair("", make_linked_ptr(feature))).second); | 299 std::make_pair("", make_linked_ptr(feature))).second); |
299 | 300 |
300 for (size_t i = 0; i < arraysize(kChildKinds); ++i) { | 301 for (size_t i = 0; i < arraysize(kChildKinds); ++i) { |
301 ListValue* child_list = NULL; | 302 ListValue* child_list = NULL; |
302 schema->GetList(kChildKinds[i], &child_list); | 303 schema->GetList(kChildKinds[i], &child_list); |
303 if (!child_list) | 304 if (!child_list) |
304 continue; | 305 continue; |
305 | 306 |
306 for (size_t j = 0; j < child_list->GetSize(); ++j) { | 307 for (size_t j = 0; j < child_list->GetSize(); ++j) { |
307 DictionaryValue* child = NULL; | 308 DictionaryValue* child = NULL; |
308 CHECK(child_list->GetDictionary(j, &child)); | 309 CHECK(child_list->GetDictionary(j, &child)); |
309 | 310 |
310 scoped_ptr<Feature> child_feature(new Feature(*feature)); | 311 scoped_ptr<SimpleFeature> child_feature(new SimpleFeature(*feature)); |
311 child_feature->Parse(child); | 312 child_feature->Parse(child); |
312 if (child_feature->Equals(*feature)) | 313 if (child_feature->Equals(*feature)) |
313 continue; // no need to store no-op features | 314 continue; // no need to store no-op features |
314 | 315 |
315 std::string child_name; | 316 std::string child_name; |
316 CHECK(child->GetString("name", &child_name)); | 317 CHECK(child->GetString("name", &child_name)); |
317 child_feature->set_name(schema_namespace + "." + child_name); | 318 child_feature->set_name(schema_namespace + "." + child_name); |
318 CHECK(schema_features->insert( | 319 CHECK(schema_features->insert( |
319 std::make_pair(child_name, | 320 std::make_pair(child_name, |
320 make_linked_ptr(child_feature.release()))).second); | 321 make_linked_ptr(child_feature.release()))).second); |
321 } | 322 } |
322 } | 323 } |
323 } | 324 } |
324 } | 325 } |
325 | 326 |
326 ExtensionAPI::ExtensionAPI() { | 327 ExtensionAPI::ExtensionAPI() { |
327 RegisterDependencyProvider("api", this); | 328 RegisterDependencyProvider("api", this); |
328 | 329 |
329 // TODO(aa): Can remove this when all JSON files are converted. | 330 // TODO(aa): Can remove this when all JSON files are converted. |
330 RegisterDependencyProvider("", this); | 331 RegisterDependencyProvider("", this); |
331 } | 332 } |
332 | 333 |
333 ExtensionAPI::~ExtensionAPI() { | 334 ExtensionAPI::~ExtensionAPI() { |
334 } | 335 } |
335 | 336 |
336 void ExtensionAPI::InitDefaultConfiguration() { | 337 void ExtensionAPI::InitDefaultConfiguration() { |
337 RegisterDependencyProvider( | 338 RegisterDependencyProvider( |
338 "manifest", SimpleFeatureProvider::GetManifestFeatures()); | 339 "manifest", BaseFeatureProvider::GetManifestFeatures()); |
339 RegisterDependencyProvider( | 340 RegisterDependencyProvider( |
340 "permission", SimpleFeatureProvider::GetPermissionFeatures()); | 341 "permission", BaseFeatureProvider::GetPermissionFeatures()); |
341 | 342 |
342 // Schemas to be loaded from resources. | 343 // Schemas to be loaded from resources. |
343 CHECK(unloaded_schemas_.empty()); | 344 CHECK(unloaded_schemas_.empty()); |
344 RegisterSchema("app", ReadFromResource( | 345 RegisterSchema("app", ReadFromResource( |
345 IDR_EXTENSION_API_JSON_APP)); | 346 IDR_EXTENSION_API_JSON_APP)); |
346 RegisterSchema("bookmarks", ReadFromResource( | 347 RegisterSchema("bookmarks", ReadFromResource( |
347 IDR_EXTENSION_API_JSON_BOOKMARKS)); | 348 IDR_EXTENSION_API_JSON_BOOKMARKS)); |
348 RegisterSchema("bookmarkManagerPrivate", ReadFromResource( | 349 RegisterSchema("bookmarkManagerPrivate", ReadFromResource( |
349 IDR_EXTENSION_API_JSON_BOOKMARKMANAGERPRIVATE)); | 350 IDR_EXTENSION_API_JSON_BOOKMARKMANAGERPRIVATE)); |
350 RegisterSchema("browserAction", ReadFromResource( | 351 RegisterSchema("browserAction", ReadFromResource( |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 if (feature) { | 519 if (feature) { |
519 // An API is 'privileged' if it or any of its dependencies can only be run | 520 // An API is 'privileged' if it or any of its dependencies can only be run |
520 // in a blessed context. | 521 // in a blessed context. |
521 std::set<std::string> resolved_dependencies; | 522 std::set<std::string> resolved_dependencies; |
522 resolved_dependencies.insert(full_name); | 523 resolved_dependencies.insert(full_name); |
523 ResolveDependencies(&resolved_dependencies); | 524 ResolveDependencies(&resolved_dependencies); |
524 for (std::set<std::string>::iterator iter = resolved_dependencies.begin(); | 525 for (std::set<std::string>::iterator iter = resolved_dependencies.begin(); |
525 iter != resolved_dependencies.end(); ++iter) { | 526 iter != resolved_dependencies.end(); ++iter) { |
526 Feature* dependency = GetFeatureDependency(*iter); | 527 Feature* dependency = GetFeatureDependency(*iter); |
527 for (std::set<Feature::Context>::iterator context = | 528 for (std::set<Feature::Context>::iterator context = |
528 dependency->contexts()->begin(); | 529 dependency->GetContexts()->begin(); |
529 context != dependency->contexts()->end(); ++context) { | 530 context != dependency->GetContexts()->end(); ++context) { |
530 if (*context != Feature::BLESSED_EXTENSION_CONTEXT) | 531 if (*context != Feature::BLESSED_EXTENSION_CONTEXT) |
531 return false; | 532 return false; |
532 } | 533 } |
533 } | 534 } |
534 return true; | 535 return true; |
535 } | 536 } |
536 | 537 |
537 // If this API hasn't been converted yet, fall back to the old system. | 538 // If this API hasn't been converted yet, fall back to the old system. |
538 if (completely_unprivileged_apis_.count(api_name)) | 539 if (completely_unprivileged_apis_.count(api_name)) |
539 return false; | 540 return false; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 Feature* result = NULL; | 708 Feature* result = NULL; |
708 FeatureMap::iterator child_feature = feature_map->second->find(child_name); | 709 FeatureMap::iterator child_feature = feature_map->second->find(child_name); |
709 if (child_feature != feature_map->second->end()) { | 710 if (child_feature != feature_map->second->end()) { |
710 result = child_feature->second.get(); | 711 result = child_feature->second.get(); |
711 } else { | 712 } else { |
712 FeatureMap::iterator parent_feature = feature_map->second->find(""); | 713 FeatureMap::iterator parent_feature = feature_map->second->find(""); |
713 CHECK(parent_feature != feature_map->second->end()); | 714 CHECK(parent_feature != feature_map->second->end()); |
714 result = parent_feature->second.get(); | 715 result = parent_feature->second.get(); |
715 } | 716 } |
716 | 717 |
717 if (result->contexts()->empty()) { | 718 if (result->GetContexts()->empty()) { |
718 LOG(ERROR) << "API feature '" << full_name | 719 LOG(ERROR) << "API feature '" << full_name |
719 << "' must specify at least one context."; | 720 << "' must specify at least one context."; |
720 return NULL; | 721 return NULL; |
721 } | 722 } |
722 | 723 |
723 return result; | 724 return result; |
724 } | 725 } |
725 | 726 |
726 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) { | 727 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) { |
727 std::string feature_type; | 728 std::string feature_type; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 | 857 |
857 void ExtensionAPI::LoadAllSchemas() { | 858 void ExtensionAPI::LoadAllSchemas() { |
858 while (unloaded_schemas_.size()) { | 859 while (unloaded_schemas_.size()) { |
859 std::map<std::string, base::StringPiece>::iterator it = | 860 std::map<std::string, base::StringPiece>::iterator it = |
860 unloaded_schemas_.begin(); | 861 unloaded_schemas_.begin(); |
861 LoadSchema(it->first, it->second); | 862 LoadSchema(it->first, it->second); |
862 } | 863 } |
863 } | 864 } |
864 | 865 |
865 } // namespace extensions | 866 } // namespace extensions |
OLD | NEW |