| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/extensions/api/extension_api.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/json/json_writer.h" | |
| 13 #include "base/lazy_instance.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/strings/string_number_conversions.h" | |
| 16 #include "base/strings/string_split.h" | |
| 17 #include "base/strings/string_util.h" | |
| 18 #include "base/values.h" | |
| 19 #include "chrome/common/extensions/api/generated_schemas.h" | |
| 20 #include "chrome/common/extensions/extension.h" | |
| 21 #include "chrome/common/extensions/permissions/permissions_data.h" | |
| 22 #include "extensions/common/features/feature.h" | |
| 23 #include "extensions/common/features/feature_provider.h" | |
| 24 #include "extensions/common/permissions/permission_set.h" | |
| 25 #include "grit/common_resources.h" | |
| 26 #include "grit/extensions_api_resources.h" | |
| 27 #include "ui/base/resource/resource_bundle.h" | |
| 28 #include "url/gurl.h" | |
| 29 | |
| 30 namespace extensions { | |
| 31 | |
| 32 using api::GeneratedSchemas; | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 const char* kChildKinds[] = { | |
| 37 "functions", | |
| 38 "events" | |
| 39 }; | |
| 40 | |
| 41 base::StringPiece ReadFromResource(int resource_id) { | |
| 42 return ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 43 resource_id); | |
| 44 } | |
| 45 | |
| 46 scoped_ptr<base::ListValue> LoadSchemaList(const std::string& name, | |
| 47 const base::StringPiece& schema) { | |
| 48 std::string error_message; | |
| 49 scoped_ptr<base::Value> result( | |
| 50 base::JSONReader::ReadAndReturnError( | |
| 51 schema, | |
| 52 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN, // options | |
| 53 NULL, // error code | |
| 54 &error_message)); | |
| 55 | |
| 56 // Tracking down http://crbug.com/121424 | |
| 57 char buf[128]; | |
| 58 base::snprintf(buf, arraysize(buf), "%s: (%d) '%s'", | |
| 59 name.c_str(), | |
| 60 result.get() ? result->GetType() : -1, | |
| 61 error_message.c_str()); | |
| 62 | |
| 63 CHECK(result.get()) << error_message << " for schema " << schema; | |
| 64 CHECK(result->IsType(base::Value::TYPE_LIST)) << " for schema " << schema; | |
| 65 return scoped_ptr<base::ListValue>(static_cast<base::ListValue*>( | |
| 66 result.release())); | |
| 67 } | |
| 68 | |
| 69 const base::DictionaryValue* FindListItem(const base::ListValue* list, | |
| 70 const std::string& property_name, | |
| 71 const std::string& property_value) { | |
| 72 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 73 const base::DictionaryValue* item = NULL; | |
| 74 CHECK(list->GetDictionary(i, &item)) | |
| 75 << property_value << "/" << property_name; | |
| 76 std::string value; | |
| 77 if (item->GetString(property_name, &value) && value == property_value) | |
| 78 return item; | |
| 79 } | |
| 80 | |
| 81 return NULL; | |
| 82 } | |
| 83 | |
| 84 const base::DictionaryValue* GetSchemaChild( | |
| 85 const base::DictionaryValue* schema_node, | |
| 86 const std::string& child_name) { | |
| 87 const base::DictionaryValue* child_node = NULL; | |
| 88 for (size_t i = 0; i < arraysize(kChildKinds); ++i) { | |
| 89 const base::ListValue* list_node = NULL; | |
| 90 if (!schema_node->GetList(kChildKinds[i], &list_node)) | |
| 91 continue; | |
| 92 child_node = FindListItem(list_node, "name", child_name); | |
| 93 if (child_node) | |
| 94 return child_node; | |
| 95 } | |
| 96 | |
| 97 return NULL; | |
| 98 } | |
| 99 | |
| 100 struct Static { | |
| 101 Static() | |
| 102 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { | |
| 103 } | |
| 104 scoped_ptr<ExtensionAPI> api; | |
| 105 }; | |
| 106 | |
| 107 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; | |
| 108 | |
| 109 // If it exists and does not already specify a namespace, then the value stored | |
| 110 // with key |key| in |schema| will be updated to |schema_namespace| + "." + | |
| 111 // |schema[key]|. | |
| 112 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace, | |
| 113 base::DictionaryValue* schema, | |
| 114 const std::string& key) { | |
| 115 if (!schema->HasKey(key)) | |
| 116 return; | |
| 117 | |
| 118 std::string old_id; | |
| 119 CHECK(schema->GetString(key, &old_id)); | |
| 120 if (old_id.find(".") == std::string::npos) | |
| 121 schema->SetString(key, schema_namespace + "." + old_id); | |
| 122 } | |
| 123 | |
| 124 // Modify all "$ref" keys anywhere in |schema| to be prefxied by | |
| 125 // |schema_namespace| if they do not already specify a namespace. | |
| 126 void PrefixRefsWithNamespace(const std::string& schema_namespace, | |
| 127 base::Value* value) { | |
| 128 base::ListValue* list = NULL; | |
| 129 base::DictionaryValue* dict = NULL; | |
| 130 if (value->GetAsList(&list)) { | |
| 131 for (base::ListValue::iterator i = list->begin(); i != list->end(); ++i) { | |
| 132 PrefixRefsWithNamespace(schema_namespace, *i); | |
| 133 } | |
| 134 } else if (value->GetAsDictionary(&dict)) { | |
| 135 MaybePrefixFieldWithNamespace(schema_namespace, dict, "$ref"); | |
| 136 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { | |
| 137 base::Value* value = NULL; | |
| 138 CHECK(dict->GetWithoutPathExpansion(i.key(), &value)); | |
| 139 PrefixRefsWithNamespace(schema_namespace, value); | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 // Modify all objects in the "types" section of the schema to be prefixed by | |
| 145 // |schema_namespace| if they do not already specify a namespace. | |
| 146 void PrefixTypesWithNamespace(const std::string& schema_namespace, | |
| 147 base::DictionaryValue* schema) { | |
| 148 if (!schema->HasKey("types")) | |
| 149 return; | |
| 150 | |
| 151 // Add the namespace to all of the types defined in this schema | |
| 152 base::ListValue *types = NULL; | |
| 153 CHECK(schema->GetList("types", &types)); | |
| 154 for (size_t i = 0; i < types->GetSize(); ++i) { | |
| 155 base::DictionaryValue *type = NULL; | |
| 156 CHECK(types->GetDictionary(i, &type)); | |
| 157 MaybePrefixFieldWithNamespace(schema_namespace, type, "id"); | |
| 158 MaybePrefixFieldWithNamespace(schema_namespace, type, "customBindings"); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 // Modify the schema so that all types are fully qualified. | |
| 163 void PrefixWithNamespace(const std::string& schema_namespace, | |
| 164 base::DictionaryValue* schema) { | |
| 165 PrefixTypesWithNamespace(schema_namespace, schema); | |
| 166 PrefixRefsWithNamespace(schema_namespace, schema); | |
| 167 } | |
| 168 | |
| 169 } // namespace | |
| 170 | |
| 171 // static | |
| 172 ExtensionAPI* ExtensionAPI::GetSharedInstance() { | |
| 173 return g_lazy_instance.Get().api.get(); | |
| 174 } | |
| 175 | |
| 176 // static | |
| 177 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() { | |
| 178 ExtensionAPI* api = new ExtensionAPI(); | |
| 179 api->InitDefaultConfiguration(); | |
| 180 return api; | |
| 181 } | |
| 182 | |
| 183 // static | |
| 184 void ExtensionAPI::SplitDependencyName(const std::string& full_name, | |
| 185 std::string* feature_type, | |
| 186 std::string* feature_name) { | |
| 187 size_t colon_index = full_name.find(':'); | |
| 188 if (colon_index == std::string::npos) { | |
| 189 // TODO(aa): Remove this code when all API descriptions have been updated. | |
| 190 *feature_type = "api"; | |
| 191 *feature_name = full_name; | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 *feature_type = full_name.substr(0, colon_index); | |
| 196 *feature_name = full_name.substr(colon_index + 1); | |
| 197 } | |
| 198 | |
| 199 void ExtensionAPI::LoadSchema(const std::string& name, | |
| 200 const base::StringPiece& schema) { | |
| 201 scoped_ptr<base::ListValue> schema_list(LoadSchemaList(name, schema)); | |
| 202 std::string schema_namespace; | |
| 203 | |
| 204 while (!schema_list->empty()) { | |
| 205 base::DictionaryValue* schema = NULL; | |
| 206 { | |
| 207 scoped_ptr<base::Value> value; | |
| 208 schema_list->Remove(schema_list->GetSize() - 1, &value); | |
| 209 CHECK(value.release()->GetAsDictionary(&schema)); | |
| 210 } | |
| 211 | |
| 212 CHECK(schema->GetString("namespace", &schema_namespace)); | |
| 213 PrefixWithNamespace(schema_namespace, schema); | |
| 214 schemas_[schema_namespace] = make_linked_ptr(schema); | |
| 215 if (!GeneratedSchemas::IsGenerated(schema_namespace)) | |
| 216 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace)); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { | |
| 221 } | |
| 222 | |
| 223 ExtensionAPI::~ExtensionAPI() { | |
| 224 } | |
| 225 | |
| 226 void ExtensionAPI::InitDefaultConfiguration() { | |
| 227 const char* names[] = {"api", "manifest", "permission"}; | |
| 228 for (size_t i = 0; i < arraysize(names); ++i) | |
| 229 RegisterDependencyProvider(names[i], FeatureProvider::GetByName(names[i])); | |
| 230 | |
| 231 // Schemas to be loaded from resources. | |
| 232 CHECK(unloaded_schemas_.empty()); | |
| 233 RegisterSchemaResource("app", IDR_EXTENSION_API_JSON_APP); | |
| 234 RegisterSchemaResource("browserAction", IDR_EXTENSION_API_JSON_BROWSERACTION); | |
| 235 RegisterSchemaResource("browsingData", IDR_EXTENSION_API_JSON_BROWSINGDATA); | |
| 236 RegisterSchemaResource("commands", IDR_EXTENSION_API_JSON_COMMANDS); | |
| 237 RegisterSchemaResource("declarativeContent", | |
| 238 IDR_EXTENSION_API_JSON_DECLARATIVE_CONTENT); | |
| 239 RegisterSchemaResource("declarativeWebRequest", | |
| 240 IDR_EXTENSION_API_JSON_DECLARATIVE_WEBREQUEST); | |
| 241 RegisterSchemaResource("runtime", IDR_EXTENSION_API_JSON_RUNTIME); | |
| 242 RegisterSchemaResource("fileBrowserHandler", | |
| 243 IDR_EXTENSION_API_JSON_FILEBROWSERHANDLER); | |
| 244 RegisterSchemaResource("fileBrowserPrivate", | |
| 245 IDR_EXTENSION_API_JSON_FILEBROWSERPRIVATE); | |
| 246 RegisterSchemaResource("inputMethodPrivate", | |
| 247 IDR_EXTENSION_API_JSON_INPUTMETHODPRIVATE); | |
| 248 RegisterSchemaResource("pageAction", IDR_EXTENSION_API_JSON_PAGEACTION); | |
| 249 RegisterSchemaResource("pageActions", IDR_EXTENSION_API_JSON_PAGEACTIONS); | |
| 250 RegisterSchemaResource("privacy", IDR_EXTENSION_API_JSON_PRIVACY); | |
| 251 RegisterSchemaResource("processes", IDR_EXTENSION_API_JSON_PROCESSES); | |
| 252 RegisterSchemaResource("proxy", IDR_EXTENSION_API_JSON_PROXY); | |
| 253 RegisterSchemaResource("scriptBadge", IDR_EXTENSION_API_JSON_SCRIPTBADGE); | |
| 254 RegisterSchemaResource("streamsPrivate", | |
| 255 IDR_EXTENSION_API_JSON_STREAMSPRIVATE); | |
| 256 RegisterSchemaResource("ttsEngine", IDR_EXTENSION_API_JSON_TTSENGINE); | |
| 257 RegisterSchemaResource("tts", IDR_EXTENSION_API_JSON_TTS); | |
| 258 RegisterSchemaResource("types", IDR_EXTENSION_API_JSON_TYPES); | |
| 259 RegisterSchemaResource("types.private", IDR_EXTENSION_API_JSON_TYPES_PRIVATE); | |
| 260 RegisterSchemaResource("webRequestInternal", | |
| 261 IDR_EXTENSION_API_JSON_WEBREQUESTINTERNAL); | |
| 262 RegisterSchemaResource("webstore", IDR_EXTENSION_API_JSON_WEBSTORE); | |
| 263 RegisterSchemaResource("webstorePrivate", | |
| 264 IDR_EXTENSION_API_JSON_WEBSTOREPRIVATE); | |
| 265 default_configuration_initialized_ = true; | |
| 266 } | |
| 267 | |
| 268 void ExtensionAPI::RegisterSchemaResource(const std::string& name, | |
| 269 int resource_id) { | |
| 270 unloaded_schemas_[name] = resource_id; | |
| 271 } | |
| 272 | |
| 273 void ExtensionAPI::RegisterDependencyProvider(const std::string& name, | |
| 274 FeatureProvider* provider) { | |
| 275 dependency_providers_[name] = provider; | |
| 276 } | |
| 277 | |
| 278 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const std::string& api_name, | |
| 279 const Extension* extension, | |
| 280 Feature::Context context, | |
| 281 const GURL& url) { | |
| 282 FeatureProviderMap::iterator provider = dependency_providers_.find("api"); | |
| 283 CHECK(provider != dependency_providers_.end()); | |
| 284 const std::vector<std::string>& features = | |
| 285 provider->second->GetAllFeatureNames(); | |
| 286 | |
| 287 // Check to see if there are any parts of this API that are allowed in this | |
| 288 // context. | |
| 289 for (std::vector<std::string>::const_iterator i = features.begin(); | |
| 290 i != features.end(); ++i) { | |
| 291 const std::string& feature_name = *i; | |
| 292 if (feature_name != api_name && feature_name.find(api_name + ".") == 0) { | |
| 293 if (IsAvailable(feature_name, extension, context, url).is_available()) | |
| 294 return true; | |
| 295 } | |
| 296 } | |
| 297 return IsAvailable(api_name, extension, context, url).is_available(); | |
| 298 } | |
| 299 | |
| 300 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name, | |
| 301 const Extension* extension, | |
| 302 Feature::Context context, | |
| 303 const GURL& url) { | |
| 304 Feature* feature = GetFeatureDependency(full_name); | |
| 305 CHECK(feature) << full_name; | |
| 306 | |
| 307 Feature::Availability availability = | |
| 308 feature->IsAvailableToContext(extension, context, url); | |
| 309 if (!availability.is_available()) | |
| 310 return availability; | |
| 311 | |
| 312 for (std::set<std::string>::iterator iter = feature->dependencies().begin(); | |
| 313 iter != feature->dependencies().end(); ++iter) { | |
| 314 Feature::Availability dependency_availability = | |
| 315 IsAvailable(*iter, extension, context, url); | |
| 316 if (!dependency_availability.is_available()) | |
| 317 return dependency_availability; | |
| 318 } | |
| 319 | |
| 320 return Feature::CreateAvailability(Feature::IS_AVAILABLE, std::string()); | |
| 321 } | |
| 322 | |
| 323 bool ExtensionAPI::IsPrivileged(const std::string& full_name) { | |
| 324 Feature* feature = GetFeatureDependency(full_name); | |
| 325 CHECK(feature); | |
| 326 DCHECK(!feature->GetContexts()->empty()); | |
| 327 // An API is 'privileged' if it can only be run in a blessed context. | |
| 328 return feature->GetContexts()->size() == | |
| 329 feature->GetContexts()->count(Feature::BLESSED_EXTENSION_CONTEXT); | |
| 330 } | |
| 331 | |
| 332 const base::DictionaryValue* ExtensionAPI::GetSchema( | |
| 333 const std::string& full_name) { | |
| 334 std::string child_name; | |
| 335 std::string api_name = GetAPINameFromFullName(full_name, &child_name); | |
| 336 | |
| 337 const base::DictionaryValue* result = NULL; | |
| 338 SchemaMap::iterator maybe_schema = schemas_.find(api_name); | |
| 339 if (maybe_schema != schemas_.end()) { | |
| 340 result = maybe_schema->second.get(); | |
| 341 } else { | |
| 342 // Might not have loaded yet; or might just not exist. | |
| 343 UnloadedSchemaMap::iterator maybe_schema_resource = | |
| 344 unloaded_schemas_.find(api_name); | |
| 345 if (maybe_schema_resource != unloaded_schemas_.end()) { | |
| 346 LoadSchema(maybe_schema_resource->first, | |
| 347 ReadFromResource(maybe_schema_resource->second)); | |
| 348 } else if (default_configuration_initialized_ && | |
| 349 GeneratedSchemas::IsGenerated(api_name)) { | |
| 350 LoadSchema(api_name, GeneratedSchemas::Get(api_name)); | |
| 351 } else { | |
| 352 return NULL; | |
| 353 } | |
| 354 | |
| 355 maybe_schema = schemas_.find(api_name); | |
| 356 CHECK(schemas_.end() != maybe_schema); | |
| 357 result = maybe_schema->second.get(); | |
| 358 } | |
| 359 | |
| 360 if (!child_name.empty()) | |
| 361 result = GetSchemaChild(result, child_name); | |
| 362 | |
| 363 return result; | |
| 364 } | |
| 365 | |
| 366 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) { | |
| 367 std::string feature_type; | |
| 368 std::string feature_name; | |
| 369 SplitDependencyName(full_name, &feature_type, &feature_name); | |
| 370 | |
| 371 FeatureProviderMap::iterator provider = | |
| 372 dependency_providers_.find(feature_type); | |
| 373 if (provider == dependency_providers_.end()) | |
| 374 return NULL; | |
| 375 | |
| 376 Feature* feature = provider->second->GetFeature(feature_name); | |
| 377 // Try getting the feature for the parent API, if this was a child. | |
| 378 if (!feature) { | |
| 379 std::string child_name; | |
| 380 feature = provider->second->GetFeature( | |
| 381 GetAPINameFromFullName(feature_name, &child_name)); | |
| 382 } | |
| 383 return feature; | |
| 384 } | |
| 385 | |
| 386 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name, | |
| 387 std::string* child_name) { | |
| 388 std::string api_name_candidate = full_name; | |
| 389 while (true) { | |
| 390 if (schemas_.find(api_name_candidate) != schemas_.end() || | |
| 391 GeneratedSchemas::IsGenerated(api_name_candidate) || | |
| 392 unloaded_schemas_.find(api_name_candidate) != unloaded_schemas_.end()) { | |
| 393 std::string result = api_name_candidate; | |
| 394 | |
| 395 if (child_name) { | |
| 396 if (result.length() < full_name.length()) | |
| 397 *child_name = full_name.substr(result.length() + 1); | |
| 398 else | |
| 399 *child_name = ""; | |
| 400 } | |
| 401 | |
| 402 return result; | |
| 403 } | |
| 404 | |
| 405 size_t last_dot_index = api_name_candidate.rfind('.'); | |
| 406 if (last_dot_index == std::string::npos) | |
| 407 break; | |
| 408 | |
| 409 api_name_candidate = api_name_candidate.substr(0, last_dot_index); | |
| 410 } | |
| 411 | |
| 412 *child_name = ""; | |
| 413 return std::string(); | |
| 414 } | |
| 415 | |
| 416 } // namespace extensions | |
| OLD | NEW |