Chromium Code Reviews| Index: extensions/common/extension_api.cc |
| diff --git a/extensions/common/extension_api.cc b/extensions/common/extension_api.cc |
| index 44aa787ebcd85495e5d1b954bbf6bfe75c2272d0..bd22755b6ca1949f347d7f9760c14e0c1f6ee414 100644 |
| --- a/extensions/common/extension_api.cc |
| +++ b/extensions/common/extension_api.cc |
| @@ -173,7 +173,8 @@ void ExtensionAPI::RegisterDependencyProvider(const std::string& name, |
| bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api, |
| const Extension* extension, |
| Feature::Context context, |
| - const GURL& url) { |
| + const GURL& url, |
| + bool allow_alias) { |
| FeatureProviderMap::iterator provider = dependency_providers_.find("api"); |
| CHECK(provider != dependency_providers_.end()); |
| @@ -189,19 +190,39 @@ bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api, |
| if ((*it)->IsAvailableToContext(extension, context, url).is_available()) |
| return true; |
| } |
| - return false; |
| + |
| + if (!allow_alias) |
| + return false; |
| + |
| + const std::string alias_name = api.GetAlias(); |
| + if (alias_name.empty()) |
| + return false; |
| + |
| + const Feature* alias = provider->second->GetFeature(alias_name); |
| + if (!alias) |
|
Devlin
2016/11/14 18:03:56
Can this happen?
tbarzic
2016/11/15 04:45:07
It should not anymore
|
| + return false; |
| + return IsAnyFeatureAvailableToContext(*alias, extension, context, url, false); |
| } |
| Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name, |
| const Extension* extension, |
| Feature::Context context, |
| - const GURL& url) { |
| + const GURL& url, |
| + bool allow_alias) { |
| Feature* feature = GetFeatureDependency(full_name); |
| if (!feature) { |
| return Feature::Availability(Feature::NOT_PRESENT, |
| std::string("Unknown feature: ") + full_name); |
| } |
| - return feature->IsAvailableToContext(extension, context, url); |
| + |
| + Feature::Availability availability = |
| + feature->IsAvailableToContext(extension, context, url); |
| + if (availability.is_available() || !allow_alias) |
| + return availability; |
| + |
| + Feature::Availability alias_availability = |
| + IsAliasAvailable(full_name, feature, extension, context, url); |
| + return alias_availability.is_available() ? alias_availability : availability; |
| } |
| base::StringPiece ExtensionAPI::GetSchemaStringPiece( |
| @@ -304,4 +325,34 @@ bool ExtensionAPI::IsKnownAPI(const std::string& name, |
| client->IsAPISchemaGenerated(name); |
| } |
| +Feature::Availability ExtensionAPI::IsAliasAvailable( |
| + const std::string& full_name, |
| + Feature* api, |
| + const Extension* extension, |
| + Feature::Context context, |
| + const GURL& url) { |
| + std::string alias = api->GetAlias(); |
| + if (alias.empty()) |
| + return Feature::Availability(Feature::NOT_PRESENT, "Alias not defined"); |
| + |
| + FeatureProviderMap::iterator provider = dependency_providers_.find("api"); |
| + CHECK(provider != dependency_providers_.end()); |
| + |
| + // Check if there is a child feature associated with full name for alias API. |
| + std::string child_name; |
| + GetAPINameFromFullName(full_name, &child_name); |
| + std::string full_alias_name = alias + "." + child_name; |
| + Feature* alias_feature = provider->second->GetFeature(full_alias_name); |
| + |
| + // If there is no child feature, use the alias API feature to check |
| + // availability. |
| + if (!alias_feature) |
| + alias_feature = provider->second->GetFeature(alias); |
| + |
| + if (!alias_feature) |
| + return Feature::Availability(Feature::NOT_PRESENT, "Alias not found"); |
| + |
| + return alias_feature->IsAvailableToContext(extension, context, url); |
| +} |
| + |
| } // namespace extensions |