1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/extension_api.h"
#include <stddef.h>
#include <algorithm>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/strings/span_printf.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "extensions/common/extension.h"
#include "extensions/common/extensions_client.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_provider.h"
#include "extensions/common/features/simple_feature.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/permissions/permission_set.h"
#include "extensions/common/permissions/permissions_data.h"
#include "url/gurl.h"
namespace extensions {
namespace {
const char* const kChildKinds[] = {"functions", "events"};
base::Value::Dict LoadSchemaDictionary(const std::string& name,
std::string_view schema) {
auto result = base::JSONReader::ReadAndReturnValueWithError(schema);
// Tracking down http://crbug.com/121424
char buf[128];
base::SpanPrintf(buf, "%s: (%d) '%s'", name.c_str(),
result.has_value() ? static_cast<int>(result->type()) : -1,
!result.has_value() ? result.error().message.c_str() : "");
CHECK(result.has_value())
<< result.error().message << " for schema " << schema;
CHECK(result->is_dict()) << " for schema " << schema;
return std::move(*result).TakeDict();
}
const base::Value::Dict* FindListItem(const base::Value::List& list,
const std::string& property_name,
const std::string& property_value) {
for (const base::Value& item_value : list) {
const base::Value::Dict* item = item_value.GetIfDict();
CHECK(item) << property_value << "/" << property_name;
const std::string* value = item->FindStringByDottedPath(property_name);
if (value && *value == property_value) {
return item;
}
}
return nullptr;
}
const base::Value::Dict* GetSchemaChild(const base::Value::Dict& schema_node,
const std::string& child_name) {
for (const char* kind : kChildKinds) {
const base::Value::List* list_node = schema_node.FindList(kind);
if (!list_node) {
continue;
}
const base::Value::Dict* child_node =
FindListItem(*list_node, "name", child_name);
if (child_node) {
return child_node;
}
}
return nullptr;
}
struct ExtensionAPIStatic {
ExtensionAPIStatic() : api(ExtensionAPI::CreateWithDefaultConfiguration()) {}
std::unique_ptr<ExtensionAPI> api;
};
base::LazyInstance<ExtensionAPIStatic>::Leaky g_extension_api_static =
LAZY_INSTANCE_INITIALIZER;
// May override |g_extension_api_static| for a test.
ExtensionAPI* g_shared_instance_for_test = nullptr;
} // namespace
// static
ExtensionAPI* ExtensionAPI::GetSharedInstance() {
return g_shared_instance_for_test ? g_shared_instance_for_test
: g_extension_api_static.Get().api.get();
}
// static
ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() {
ExtensionAPI* api = new ExtensionAPI();
api->InitDefaultConfiguration();
return api;
}
// static
void ExtensionAPI::SplitDependencyName(const std::string& full_name,
std::string* feature_type,
std::string* feature_name) {
size_t colon_index = full_name.find(':');
if (colon_index == std::string::npos) {
// TODO(aa): Remove this code when all API descriptions have been updated.
*feature_type = "api";
*feature_name = full_name;
return;
}
*feature_type = full_name.substr(0, colon_index);
*feature_name = full_name.substr(colon_index + 1);
}
ExtensionAPI::OverrideSharedInstanceForTest::OverrideSharedInstanceForTest(
ExtensionAPI* testing_api)
: original_api_(g_shared_instance_for_test) {
g_shared_instance_for_test = testing_api;
}
ExtensionAPI::OverrideSharedInstanceForTest::~OverrideSharedInstanceForTest() {
g_shared_instance_for_test = original_api_;
}
void ExtensionAPI::LoadSchema(const std::string& name,
std::string_view schema) {
lock_.AssertAcquired();
base::Value::Dict schema_dict(LoadSchemaDictionary(name, schema));
const std::string* schema_namespace = schema_dict.FindString("namespace");
CHECK(schema_namespace);
schemas_[*schema_namespace] = std::move(schema_dict);
}
ExtensionAPI::ExtensionAPI() = default;
ExtensionAPI::~ExtensionAPI() = default;
void ExtensionAPI::InitDefaultConfiguration() {
static const constexpr char* const names[] = {"api", "behavior", "manifest",
"permission"};
for (const char* const name : names) {
RegisterDependencyProvider(name, FeatureProvider::GetByName(name));
}
default_configuration_initialized_ = true;
}
void ExtensionAPI::RegisterDependencyProvider(const std::string& name,
const FeatureProvider* provider) {
dependency_providers_[name] = provider;
}
bool ExtensionAPI::IsAnyFeatureAvailableToContext(
const Feature& api,
const Extension* extension,
mojom::ContextType context,
const GURL& url,
CheckAliasStatus check_alias,
int context_id,
const ContextData& context_data) {
auto provider = dependency_providers_.find("api");
CHECK(provider != dependency_providers_.end());
if (api.IsAvailableToContext(extension, context, url, context_id,
context_data)
.is_available()) {
return true;
}
// Check to see if there are any parts of this API that are allowed in this
// context.
const std::vector<const Feature*> features =
provider->second->GetChildren(api);
for (const Feature* feature : features) {
if (feature
->IsAvailableToContext(extension, context, url, context_id,
context_data)
.is_available()) {
return true;
}
}
if (check_alias != CheckAliasStatus::ALLOWED) {
return false;
}
const std::string& alias_name = api.alias();
if (alias_name.empty()) {
return false;
}
const Feature* alias = provider->second->GetFeature(alias_name);
CHECK(alias) << "Cannot find alias feature " << alias_name
<< " for API feature " << api.name();
return IsAnyFeatureAvailableToContext(*alias, extension, context, url,
CheckAliasStatus::NOT_ALLOWED,
context_id, context_data);
}
Feature::Availability ExtensionAPI::IsAvailable(
const std::string& full_name,
const Extension* extension,
mojom::ContextType context,
const GURL& url,
CheckAliasStatus check_alias,
int context_id,
const ContextData& context_data) {
const Feature* feature = GetFeatureDependency(full_name);
if (!feature) {
return Feature::Availability(Feature::NOT_PRESENT,
std::string("Unknown feature: ") + full_name);
}
Feature::Availability availability = feature->IsAvailableToContext(
extension, context, url, context_id, context_data);
if (availability.is_available() || check_alias != CheckAliasStatus::ALLOWED) {
return availability;
}
Feature::Availability alias_availability = IsAliasAvailable(
full_name, *feature, extension, context, url, context_id, context_data);
return alias_availability.is_available() ? alias_availability : availability;
}
std::string_view ExtensionAPI::GetSchemaStringPiece(
const std::string& api_name) {
base::AutoLock lock(lock_);
return GetSchemaStringPieceUnsafe(api_name);
}
const base::Value::Dict* ExtensionAPI::GetSchema(const std::string& full_name) {
base::AutoLock lock(lock_);
std::string child_name;
std::string api_name = GetAPINameFromFullNameUnsafe(full_name, &child_name);
const base::Value::Dict* result = nullptr;
auto maybe_schema = schemas_.find(api_name);
if (maybe_schema != schemas_.end()) {
result = &maybe_schema->second;
} else {
std::string_view schema_string = GetSchemaStringPieceUnsafe(api_name);
if (schema_string.empty()) {
return nullptr;
}
LoadSchema(api_name, schema_string);
maybe_schema = schemas_.find(api_name);
CHECK(schemas_.end() != maybe_schema);
result = &maybe_schema->second;
}
if (!child_name.empty()) {
result = GetSchemaChild(*result, child_name);
}
return result;
}
const Feature* ExtensionAPI::GetFeatureDependency(
const std::string& full_name) {
std::string feature_type;
std::string feature_name;
SplitDependencyName(full_name, &feature_type, &feature_name);
auto provider = dependency_providers_.find(feature_type);
if (provider == dependency_providers_.end()) {
return nullptr;
}
const Feature* feature = provider->second->GetFeature(feature_name);
// Try getting the feature for the parent API, if this was a child.
if (!feature) {
std::string child_name;
feature = provider->second->GetFeature(
GetAPINameFromFullName(feature_name, &child_name));
}
return feature;
}
std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name,
std::string* child_name) {
base::AutoLock lock(lock_);
return GetAPINameFromFullNameUnsafe(full_name, child_name);
}
bool ExtensionAPI::IsKnownAPI(const std::string& name,
ExtensionsClient* client) {
lock_.AssertAcquired();
return base::Contains(schemas_, name) || client->IsAPISchemaGenerated(name);
}
Feature::Availability ExtensionAPI::IsAliasAvailable(
const std::string& full_name,
const Feature& feature,
const Extension* extension,
mojom::ContextType context,
const GURL& url,
int context_id,
const ContextData& context_data) {
const std::string& alias = feature.alias();
if (alias.empty()) {
return Feature::Availability(Feature::NOT_PRESENT, "Alias not defined");
}
auto provider = dependency_providers_.find("api");
CHECK(provider != dependency_providers_.end());
// Check if there is a child feature associated with full name for alias API.
// This is to ensure that the availability of the feature associated with the
// aliased |full_name| is properly determined in case the feature in question
// is a child feature. For example, if API foo has an alias fooAlias, which
// has a child feature fooAlias.method, aliased foo.method availability should
// be determined using fooAlias.method feature, rather than fooAlias feature.
std::string child_name;
GetAPINameFromFullName(full_name, &child_name);
std::string full_alias_name = alias + "." + child_name;
const 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);
}
CHECK(alias_feature) << "Cannot find alias feature " << alias
<< " for API feature " << feature.name();
return alias_feature->IsAvailableToContext(extension, context, url,
context_id, context_data);
}
std::string_view ExtensionAPI::GetSchemaStringPieceUnsafe(
const std::string& api_name) {
lock_.AssertAcquired();
DCHECK_EQ(api_name, GetAPINameFromFullNameUnsafe(api_name, nullptr));
ExtensionsClient* client = ExtensionsClient::Get();
DCHECK(client);
if (!default_configuration_initialized_) {
return std::string_view();
}
return client->GetAPISchema(api_name);
}
std::string ExtensionAPI::GetAPINameFromFullNameUnsafe(
const std::string& full_name,
std::string* child_name) {
lock_.AssertAcquired();
std::string api_name_candidate = full_name;
ExtensionsClient* extensions_client = ExtensionsClient::Get();
DCHECK(extensions_client);
while (true) {
if (IsKnownAPI(api_name_candidate, extensions_client)) {
if (child_name) {
if (api_name_candidate.length() < full_name.length()) {
*child_name = full_name.substr(api_name_candidate.length() + 1);
} else {
*child_name = "";
}
}
return api_name_candidate;
}
size_t last_dot_index = api_name_candidate.rfind('.');
if (last_dot_index == std::string::npos) {
break;
}
api_name_candidate = api_name_candidate.substr(0, last_dot_index);
}
if (child_name) {
*child_name = "";
}
return std::string();
}
} // namespace extensions
|