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

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

Issue 2494653005: Support API aliases (Closed)
Patch Set: update docs 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 RegisterDependencyProvider(names[i], FeatureProvider::GetByName(names[i])); 163 RegisterDependencyProvider(names[i], FeatureProvider::GetByName(names[i]));
164 164
165 default_configuration_initialized_ = true; 165 default_configuration_initialized_ = true;
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(
174 const Extension* extension, 174 const Feature& api,
175 Feature::Context context, 175 const Extension* extension,
176 const GURL& url) { 176 Feature::Context context,
177 const GURL& url,
178 CheckAliasStatus alias_status) {
177 FeatureProviderMap::iterator provider = dependency_providers_.find("api"); 179 FeatureProviderMap::iterator provider = dependency_providers_.find("api");
178 CHECK(provider != dependency_providers_.end()); 180 CHECK(provider != dependency_providers_.end());
179 181
180 if (api.IsAvailableToContext(extension, context, url).is_available()) 182 if (api.IsAvailableToContext(extension, context, url).is_available())
181 return true; 183 return true;
182 184
183 // Check to see if there are any parts of this API that are allowed in this 185 // Check to see if there are any parts of this API that are allowed in this
184 // context. 186 // context.
185 const std::vector<Feature*> features = provider->second->GetChildren(api); 187 const std::vector<Feature*> features = provider->second->GetChildren(api);
186 for (std::vector<Feature*>::const_iterator it = features.begin(); 188 for (std::vector<Feature*>::const_iterator it = features.begin();
187 it != features.end(); 189 it != features.end();
188 ++it) { 190 ++it) {
189 if ((*it)->IsAvailableToContext(extension, context, url).is_available()) 191 if ((*it)->IsAvailableToContext(extension, context, url).is_available())
190 return true; 192 return true;
191 } 193 }
192 return false; 194
195 if (alias_status != CheckAliasStatus::ALLOWED)
196 return false;
197
198 const std::string& alias_name = api.alias();
199 if (alias_name.empty())
200 return false;
201
202 const Feature* alias = provider->second->GetFeature(alias_name);
203 CHECK(alias) << "Cannot find alias feature " << alias_name
204 << " for API feature " << api.name();
205 return IsAnyFeatureAvailableToContext(*alias, extension, context, url,
206 CheckAliasStatus::NOT_ALLOWED);
193 } 207 }
194 208
195 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name, 209 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name,
196 const Extension* extension, 210 const Extension* extension,
197 Feature::Context context, 211 Feature::Context context,
198 const GURL& url) { 212 const GURL& url,
213 CheckAliasStatus alias_status) {
199 Feature* feature = GetFeatureDependency(full_name); 214 Feature* feature = GetFeatureDependency(full_name);
200 if (!feature) { 215 if (!feature) {
201 return Feature::Availability(Feature::NOT_PRESENT, 216 return Feature::Availability(Feature::NOT_PRESENT,
202 std::string("Unknown feature: ") + full_name); 217 std::string("Unknown feature: ") + full_name);
203 } 218 }
204 return feature->IsAvailableToContext(extension, context, url); 219
220 Feature::Availability availability =
221 feature->IsAvailableToContext(extension, context, url);
222 if (availability.is_available() || alias_status != CheckAliasStatus::ALLOWED)
223 return availability;
224
225 Feature::Availability alias_availability =
226 IsAliasAvailable(full_name, feature, extension, context, url);
227 return alias_availability.is_available() ? alias_availability : availability;
205 } 228 }
206 229
207 base::StringPiece ExtensionAPI::GetSchemaStringPiece( 230 base::StringPiece ExtensionAPI::GetSchemaStringPiece(
208 const std::string& api_name) { 231 const std::string& api_name) {
209 DCHECK_EQ(api_name, GetAPINameFromFullName(api_name, nullptr)); 232 DCHECK_EQ(api_name, GetAPINameFromFullName(api_name, nullptr));
210 StringPieceMap::iterator cached = schema_strings_.find(api_name); 233 StringPieceMap::iterator cached = schema_strings_.find(api_name);
211 if (cached != schema_strings_.end()) 234 if (cached != schema_strings_.end())
212 return cached->second; 235 return cached->second;
213 236
214 ExtensionsClient* client = ExtensionsClient::Get(); 237 ExtensionsClient* client = ExtensionsClient::Get();
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 *child_name = ""; 320 *child_name = "";
298 return std::string(); 321 return std::string();
299 } 322 }
300 323
301 bool ExtensionAPI::IsKnownAPI(const std::string& name, 324 bool ExtensionAPI::IsKnownAPI(const std::string& name,
302 ExtensionsClient* client) { 325 ExtensionsClient* client) {
303 return schemas_.find(name) != schemas_.end() || 326 return schemas_.find(name) != schemas_.end() ||
304 client->IsAPISchemaGenerated(name); 327 client->IsAPISchemaGenerated(name);
305 } 328 }
306 329
330 Feature::Availability ExtensionAPI::IsAliasAvailable(
331 const std::string& full_name,
332 Feature* api,
Devlin 2016/11/17 01:11:15 s/api/feature
tbarzic 2016/11/17 18:12:13 Done.
333 const Extension* extension,
334 Feature::Context context,
335 const GURL& url) {
336 const std::string& alias = api->alias();
337 if (alias.empty())
338 return Feature::Availability(Feature::NOT_PRESENT, "Alias not defined");
339
340 FeatureProviderMap::iterator provider = dependency_providers_.find("api");
341 CHECK(provider != dependency_providers_.end());
342
343 // Check if there is a child feature associated with full name for alias API.
Devlin 2016/11/17 01:11:15 Can you give an example of when this would be the
tbarzic 2016/11/17 18:12:13 Done.
344 std::string child_name;
345 GetAPINameFromFullName(full_name, &child_name);
346 std::string full_alias_name = alias + "." + child_name;
347 Feature* alias_feature = provider->second->GetFeature(full_alias_name);
348
349 // If there is no child feature, use the alias API feature to check
350 // availability.
351 if (!alias_feature)
352 alias_feature = provider->second->GetFeature(alias);
353
354 CHECK(alias_feature) << "Cannot find alias feature " << alias
355 << " for API feature " << api->name();
356
357 return alias_feature->IsAvailableToContext(extension, context, url);
358 }
359
307 } // namespace extensions 360 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698