Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / extensions / common / extension_api.cc
blob8267695b62b9da73b2a6865c6c0a3c3b401b4564
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
3 // found in the LICENSE file.
5 #include "extensions/common/extension_api.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
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 "extensions/common/extension.h"
20 #include "extensions/common/extensions_client.h"
21 #include "extensions/common/features/feature.h"
22 #include "extensions/common/features/feature_provider.h"
23 #include "extensions/common/features/simple_feature.h"
24 #include "extensions/common/permissions/permission_set.h"
25 #include "extensions/common/permissions/permissions_data.h"
26 #include "extensions/grit/extensions_resources.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "url/gurl.h"
30 namespace extensions {
32 namespace {
34 const char* kChildKinds[] = {
35 "functions",
36 "events"
39 base::StringPiece ReadFromResource(int resource_id) {
40 return ResourceBundle::GetSharedInstance().GetRawDataResource(
41 resource_id);
44 scoped_ptr<base::ListValue> LoadSchemaList(const std::string& name,
45 const base::StringPiece& schema) {
46 std::string error_message;
47 scoped_ptr<base::Value> result(
48 base::JSONReader::ReadAndReturnError(
49 schema,
50 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN, // options
51 NULL, // error code
52 &error_message));
54 // Tracking down http://crbug.com/121424
55 char buf[128];
56 base::snprintf(buf, arraysize(buf), "%s: (%d) '%s'",
57 name.c_str(),
58 result.get() ? result->GetType() : -1,
59 error_message.c_str());
61 CHECK(result.get()) << error_message << " for schema " << schema;
62 CHECK(result->IsType(base::Value::TYPE_LIST)) << " for schema " << schema;
63 return scoped_ptr<base::ListValue>(static_cast<base::ListValue*>(
64 result.release()));
67 const base::DictionaryValue* FindListItem(const base::ListValue* list,
68 const std::string& property_name,
69 const std::string& property_value) {
70 for (size_t i = 0; i < list->GetSize(); ++i) {
71 const base::DictionaryValue* item = NULL;
72 CHECK(list->GetDictionary(i, &item))
73 << property_value << "/" << property_name;
74 std::string value;
75 if (item->GetString(property_name, &value) && value == property_value)
76 return item;
79 return NULL;
82 const base::DictionaryValue* GetSchemaChild(
83 const base::DictionaryValue* schema_node,
84 const std::string& child_name) {
85 const base::DictionaryValue* child_node = NULL;
86 for (size_t i = 0; i < arraysize(kChildKinds); ++i) {
87 const base::ListValue* list_node = NULL;
88 if (!schema_node->GetList(kChildKinds[i], &list_node))
89 continue;
90 child_node = FindListItem(list_node, "name", child_name);
91 if (child_node)
92 return child_node;
95 return NULL;
98 struct Static {
99 Static()
100 : api(ExtensionAPI::CreateWithDefaultConfiguration()) {
102 scoped_ptr<ExtensionAPI> api;
105 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER;
107 // May override |g_lazy_instance| for a test.
108 ExtensionAPI* g_shared_instance_for_test = NULL;
110 // If it exists and does not already specify a namespace, then the value stored
111 // with key |key| in |schema| will be updated to |schema_namespace| + "." +
112 // |schema[key]|.
113 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace,
114 base::DictionaryValue* schema,
115 const std::string& key) {
116 if (!schema->HasKey(key))
117 return;
119 std::string old_id;
120 CHECK(schema->GetString(key, &old_id));
121 if (old_id.find(".") == std::string::npos)
122 schema->SetString(key, schema_namespace + "." + old_id);
125 // Modify all "$ref" keys anywhere in |schema| to be prefxied by
126 // |schema_namespace| if they do not already specify a namespace.
127 void PrefixRefsWithNamespace(const std::string& schema_namespace,
128 base::Value* value) {
129 base::ListValue* list = NULL;
130 base::DictionaryValue* dict = NULL;
131 if (value->GetAsList(&list)) {
132 for (base::ListValue::iterator i = list->begin(); i != list->end(); ++i) {
133 PrefixRefsWithNamespace(schema_namespace, *i);
135 } else if (value->GetAsDictionary(&dict)) {
136 MaybePrefixFieldWithNamespace(schema_namespace, dict, "$ref");
137 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) {
138 base::Value* value = NULL;
139 CHECK(dict->GetWithoutPathExpansion(i.key(), &value));
140 PrefixRefsWithNamespace(schema_namespace, value);
145 // Modify all objects in the "types" section of the schema to be prefixed by
146 // |schema_namespace| if they do not already specify a namespace.
147 void PrefixTypesWithNamespace(const std::string& schema_namespace,
148 base::DictionaryValue* schema) {
149 if (!schema->HasKey("types"))
150 return;
152 // Add the namespace to all of the types defined in this schema
153 base::ListValue *types = NULL;
154 CHECK(schema->GetList("types", &types));
155 for (size_t i = 0; i < types->GetSize(); ++i) {
156 base::DictionaryValue *type = NULL;
157 CHECK(types->GetDictionary(i, &type));
158 MaybePrefixFieldWithNamespace(schema_namespace, type, "id");
159 MaybePrefixFieldWithNamespace(schema_namespace, type, "customBindings");
163 // Modify the schema so that all types are fully qualified.
164 void PrefixWithNamespace(const std::string& schema_namespace,
165 base::DictionaryValue* schema) {
166 PrefixTypesWithNamespace(schema_namespace, schema);
167 PrefixRefsWithNamespace(schema_namespace, schema);
170 } // namespace
172 // static
173 ExtensionAPI* ExtensionAPI::GetSharedInstance() {
174 return g_shared_instance_for_test ? g_shared_instance_for_test
175 : g_lazy_instance.Get().api.get();
178 // static
179 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() {
180 ExtensionAPI* api = new ExtensionAPI();
181 api->InitDefaultConfiguration();
182 return api;
185 // static
186 void ExtensionAPI::SplitDependencyName(const std::string& full_name,
187 std::string* feature_type,
188 std::string* feature_name) {
189 size_t colon_index = full_name.find(':');
190 if (colon_index == std::string::npos) {
191 // TODO(aa): Remove this code when all API descriptions have been updated.
192 *feature_type = "api";
193 *feature_name = full_name;
194 return;
197 *feature_type = full_name.substr(0, colon_index);
198 *feature_name = full_name.substr(colon_index + 1);
201 ExtensionAPI::OverrideSharedInstanceForTest::OverrideSharedInstanceForTest(
202 ExtensionAPI* testing_api)
203 : original_api_(g_shared_instance_for_test) {
204 g_shared_instance_for_test = testing_api;
207 ExtensionAPI::OverrideSharedInstanceForTest::~OverrideSharedInstanceForTest() {
208 g_shared_instance_for_test = original_api_;
211 void ExtensionAPI::LoadSchema(const std::string& name,
212 const base::StringPiece& schema) {
213 scoped_ptr<base::ListValue> schema_list(LoadSchemaList(name, schema));
214 std::string schema_namespace;
215 extensions::ExtensionsClient* extensions_client =
216 extensions::ExtensionsClient::Get();
217 DCHECK(extensions_client);
218 while (!schema_list->empty()) {
219 base::DictionaryValue* schema = NULL;
221 scoped_ptr<base::Value> value;
222 schema_list->Remove(schema_list->GetSize() - 1, &value);
223 CHECK(value.release()->GetAsDictionary(&schema));
226 CHECK(schema->GetString("namespace", &schema_namespace));
227 PrefixWithNamespace(schema_namespace, schema);
228 schemas_[schema_namespace] = make_linked_ptr(schema);
229 if (!extensions_client->IsAPISchemaGenerated(schema_namespace))
230 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace));
234 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) {
237 ExtensionAPI::~ExtensionAPI() {
240 void ExtensionAPI::InitDefaultConfiguration() {
241 const char* names[] = {"api", "manifest", "permission"};
242 for (size_t i = 0; i < arraysize(names); ++i)
243 RegisterDependencyProvider(names[i], FeatureProvider::GetByName(names[i]));
245 ExtensionsClient::Get()->RegisterAPISchemaResources(this);
247 RegisterSchemaResource("declarativeWebRequest",
248 IDR_EXTENSION_API_JSON_DECLARATIVE_WEBREQUEST);
249 RegisterSchemaResource("webViewRequest",
250 IDR_EXTENSION_API_JSON_WEB_VIEW_REQUEST);
252 default_configuration_initialized_ = true;
255 void ExtensionAPI::RegisterSchemaResource(const std::string& name,
256 int resource_id) {
257 unloaded_schemas_[name] = resource_id;
260 void ExtensionAPI::RegisterDependencyProvider(const std::string& name,
261 const FeatureProvider* provider) {
262 dependency_providers_[name] = provider;
265 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api,
266 const Extension* extension,
267 Feature::Context context,
268 const GURL& url) {
269 FeatureProviderMap::iterator provider = dependency_providers_.find("api");
270 CHECK(provider != dependency_providers_.end());
272 if (api.IsAvailableToContext(extension, context, url).is_available())
273 return true;
275 // Check to see if there are any parts of this API that are allowed in this
276 // context.
277 const std::vector<Feature*> features = provider->second->GetChildren(api);
278 for (std::vector<Feature*>::const_iterator it = features.begin();
279 it != features.end();
280 ++it) {
281 if ((*it)->IsAvailableToContext(extension, context, url).is_available())
282 return true;
284 return false;
287 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name,
288 const Extension* extension,
289 Feature::Context context,
290 const GURL& url) {
291 Feature* feature = GetFeatureDependency(full_name);
292 if (!feature) {
293 return Feature::CreateAvailability(Feature::NOT_PRESENT,
294 std::string("Unknown feature: ") + full_name);
296 return feature->IsAvailableToContext(extension, context, url);
299 bool ExtensionAPI::IsAvailableInUntrustedContext(const std::string& name,
300 const Extension* extension) {
301 return IsAvailable(name, extension, Feature::CONTENT_SCRIPT_CONTEXT, GURL())
302 .is_available() ||
303 IsAvailable(
304 name, extension, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL())
305 .is_available() ||
306 IsAvailable(name, extension, Feature::BLESSED_WEB_PAGE_CONTEXT, GURL())
307 .is_available() ||
308 IsAvailable(name, extension, Feature::WEB_PAGE_CONTEXT, GURL())
309 .is_available();
312 bool ExtensionAPI::IsAvailableToWebUI(const std::string& name,
313 const GURL& url) {
314 return IsAvailable(name, NULL, Feature::WEBUI_CONTEXT, url).is_available();
317 const base::DictionaryValue* ExtensionAPI::GetSchema(
318 const std::string& full_name) {
319 std::string child_name;
320 std::string api_name = GetAPINameFromFullName(full_name, &child_name);
322 const base::DictionaryValue* result = NULL;
323 SchemaMap::iterator maybe_schema = schemas_.find(api_name);
324 if (maybe_schema != schemas_.end()) {
325 result = maybe_schema->second.get();
326 } else {
327 // Might not have loaded yet; or might just not exist.
328 UnloadedSchemaMap::iterator maybe_schema_resource =
329 unloaded_schemas_.find(api_name);
330 extensions::ExtensionsClient* extensions_client =
331 extensions::ExtensionsClient::Get();
332 DCHECK(extensions_client);
333 if (maybe_schema_resource != unloaded_schemas_.end()) {
334 LoadSchema(maybe_schema_resource->first,
335 ReadFromResource(maybe_schema_resource->second));
336 } else if (default_configuration_initialized_ &&
337 extensions_client->IsAPISchemaGenerated(api_name)) {
338 LoadSchema(api_name, extensions_client->GetAPISchema(api_name));
339 } else {
340 return NULL;
343 maybe_schema = schemas_.find(api_name);
344 CHECK(schemas_.end() != maybe_schema);
345 result = maybe_schema->second.get();
348 if (!child_name.empty())
349 result = GetSchemaChild(result, child_name);
351 return result;
354 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) {
355 std::string feature_type;
356 std::string feature_name;
357 SplitDependencyName(full_name, &feature_type, &feature_name);
359 FeatureProviderMap::iterator provider =
360 dependency_providers_.find(feature_type);
361 if (provider == dependency_providers_.end())
362 return NULL;
364 Feature* feature = provider->second->GetFeature(feature_name);
365 // Try getting the feature for the parent API, if this was a child.
366 if (!feature) {
367 std::string child_name;
368 feature = provider->second->GetFeature(
369 GetAPINameFromFullName(feature_name, &child_name));
371 return feature;
374 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name,
375 std::string* child_name) {
376 std::string api_name_candidate = full_name;
377 extensions::ExtensionsClient* extensions_client =
378 extensions::ExtensionsClient::Get();
379 DCHECK(extensions_client);
380 while (true) {
381 if (schemas_.find(api_name_candidate) != schemas_.end() ||
382 extensions_client->IsAPISchemaGenerated(api_name_candidate) ||
383 unloaded_schemas_.find(api_name_candidate) != unloaded_schemas_.end()) {
384 std::string result = api_name_candidate;
386 if (child_name) {
387 if (result.length() < full_name.length())
388 *child_name = full_name.substr(result.length() + 1);
389 else
390 *child_name = "";
393 return result;
396 size_t last_dot_index = api_name_candidate.rfind('.');
397 if (last_dot_index == std::string::npos)
398 break;
400 api_name_candidate = api_name_candidate.substr(0, last_dot_index);
403 *child_name = "";
404 return std::string();
407 } // namespace extensions