Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: extensions/common/extension_api.cc

Issue 2494653005: Support API aliases (Closed)
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 166 }
167 167
168 void ExtensionAPI::RegisterDependencyProvider(const std::string& name, 168 void ExtensionAPI::RegisterDependencyProvider(const std::string& name,
169 const FeatureProvider* provider) { 169 const FeatureProvider* provider) {
170 dependency_providers_[name] = provider; 170 dependency_providers_[name] = provider;
171 } 171 }
172 172
173 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api, 173 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api,
174 const Extension* extension, 174 const Extension* extension,
175 Feature::Context context, 175 Feature::Context context,
176 const GURL& url) { 176 const GURL& url,
177 bool allow_alias) {
177 FeatureProviderMap::iterator provider = dependency_providers_.find("api"); 178 FeatureProviderMap::iterator provider = dependency_providers_.find("api");
178 CHECK(provider != dependency_providers_.end()); 179 CHECK(provider != dependency_providers_.end());
179 180
180 if (api.IsAvailableToContext(extension, context, url).is_available()) 181 if (api.IsAvailableToContext(extension, context, url).is_available())
181 return true; 182 return true;
182 183
183 // Check to see if there are any parts of this API that are allowed in this 184 // Check to see if there are any parts of this API that are allowed in this
184 // context. 185 // context.
185 const std::vector<Feature*> features = provider->second->GetChildren(api); 186 const std::vector<Feature*> features = provider->second->GetChildren(api);
186 for (std::vector<Feature*>::const_iterator it = features.begin(); 187 for (std::vector<Feature*>::const_iterator it = features.begin();
187 it != features.end(); 188 it != features.end();
188 ++it) { 189 ++it) {
189 if ((*it)->IsAvailableToContext(extension, context, url).is_available()) 190 if ((*it)->IsAvailableToContext(extension, context, url).is_available())
190 return true; 191 return true;
191 } 192 }
192 return false; 193
194 if (!allow_alias)
195 return false;
196
197 const std::string alias_name = api.GetAlias();
198 if (alias_name.empty())
199 return false;
200
201 const Feature* alias = provider->second->GetFeature(alias_name);
202 if (!alias)
Devlin 2016/11/14 18:03:56 Can this happen?
tbarzic 2016/11/15 04:45:07 It should not anymore
203 return false;
204 return IsAnyFeatureAvailableToContext(*alias, extension, context, url, false);
193 } 205 }
194 206
195 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name, 207 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name,
196 const Extension* extension, 208 const Extension* extension,
197 Feature::Context context, 209 Feature::Context context,
198 const GURL& url) { 210 const GURL& url,
211 bool allow_alias) {
199 Feature* feature = GetFeatureDependency(full_name); 212 Feature* feature = GetFeatureDependency(full_name);
200 if (!feature) { 213 if (!feature) {
201 return Feature::Availability(Feature::NOT_PRESENT, 214 return Feature::Availability(Feature::NOT_PRESENT,
202 std::string("Unknown feature: ") + full_name); 215 std::string("Unknown feature: ") + full_name);
203 } 216 }
204 return feature->IsAvailableToContext(extension, context, url); 217
218 Feature::Availability availability =
219 feature->IsAvailableToContext(extension, context, url);
220 if (availability.is_available() || !allow_alias)
221 return availability;
222
223 Feature::Availability alias_availability =
224 IsAliasAvailable(full_name, feature, extension, context, url);
225 return alias_availability.is_available() ? alias_availability : availability;
205 } 226 }
206 227
207 base::StringPiece ExtensionAPI::GetSchemaStringPiece( 228 base::StringPiece ExtensionAPI::GetSchemaStringPiece(
208 const std::string& api_name) { 229 const std::string& api_name) {
209 DCHECK_EQ(api_name, GetAPINameFromFullName(api_name, nullptr)); 230 DCHECK_EQ(api_name, GetAPINameFromFullName(api_name, nullptr));
210 StringPieceMap::iterator cached = schema_strings_.find(api_name); 231 StringPieceMap::iterator cached = schema_strings_.find(api_name);
211 if (cached != schema_strings_.end()) 232 if (cached != schema_strings_.end())
212 return cached->second; 233 return cached->second;
213 234
214 ExtensionsClient* client = ExtensionsClient::Get(); 235 ExtensionsClient* client = ExtensionsClient::Get();
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 *child_name = ""; 318 *child_name = "";
298 return std::string(); 319 return std::string();
299 } 320 }
300 321
301 bool ExtensionAPI::IsKnownAPI(const std::string& name, 322 bool ExtensionAPI::IsKnownAPI(const std::string& name,
302 ExtensionsClient* client) { 323 ExtensionsClient* client) {
303 return schemas_.find(name) != schemas_.end() || 324 return schemas_.find(name) != schemas_.end() ||
304 client->IsAPISchemaGenerated(name); 325 client->IsAPISchemaGenerated(name);
305 } 326 }
306 327
328 Feature::Availability ExtensionAPI::IsAliasAvailable(
329 const std::string& full_name,
330 Feature* api,
331 const Extension* extension,
332 Feature::Context context,
333 const GURL& url) {
334 std::string alias = api->GetAlias();
335 if (alias.empty())
336 return Feature::Availability(Feature::NOT_PRESENT, "Alias not defined");
337
338 FeatureProviderMap::iterator provider = dependency_providers_.find("api");
339 CHECK(provider != dependency_providers_.end());
340
341 // Check if there is a child feature associated with full name for alias API.
342 std::string child_name;
343 GetAPINameFromFullName(full_name, &child_name);
344 std::string full_alias_name = alias + "." + child_name;
345 Feature* alias_feature = provider->second->GetFeature(full_alias_name);
346
347 // If there is no child feature, use the alias API feature to check
348 // availability.
349 if (!alias_feature)
350 alias_feature = provider->second->GetFeature(alias);
351
352 if (!alias_feature)
353 return Feature::Availability(Feature::NOT_PRESENT, "Alias not found");
354
355 return alias_feature->IsAvailableToContext(extension, context, url);
356 }
357
307 } // namespace extensions 358 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698