Roll src/third_party/WebKit b3f094a:f697bbd (svn 194310:194313)
[chromium-blink-merge.git] / extensions / common / extension_api.cc
blob81ca72ae5697f3da5fa45a0d7bde0febee023d94
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::Availability(Feature::NOT_PRESENT,
294 std::string("Unknown feature: ") + full_name);
296 return feature->IsAvailableToContext(extension, context, url);
299 bool ExtensionAPI::IsAvailableToWebUI(const std::string& name,
300 const GURL& url) {
301 return IsAvailable(name, NULL, Feature::WEBUI_CONTEXT, url).is_available();
304 const base::DictionaryValue* ExtensionAPI::GetSchema(
305 const std::string& full_name) {
306 std::string child_name;
307 std::string api_name = GetAPINameFromFullName(full_name, &child_name);
309 const base::DictionaryValue* result = NULL;
310 SchemaMap::iterator maybe_schema = schemas_.find(api_name);
311 if (maybe_schema != schemas_.end()) {
312 result = maybe_schema->second.get();
313 } else {
314 // Might not have loaded yet; or might just not exist.
315 UnloadedSchemaMap::iterator maybe_schema_resource =
316 unloaded_schemas_.find(api_name);
317 extensions::ExtensionsClient* extensions_client =
318 extensions::ExtensionsClient::Get();
319 DCHECK(extensions_client);
320 if (maybe_schema_resource != unloaded_schemas_.end()) {
321 LoadSchema(maybe_schema_resource->first,
322 ReadFromResource(maybe_schema_resource->second));
323 } else if (default_configuration_initialized_ &&
324 extensions_client->IsAPISchemaGenerated(api_name)) {
325 LoadSchema(api_name, extensions_client->GetAPISchema(api_name));
326 } else {
327 return NULL;
330 maybe_schema = schemas_.find(api_name);
331 CHECK(schemas_.end() != maybe_schema);
332 result = maybe_schema->second.get();
335 if (!child_name.empty())
336 result = GetSchemaChild(result, child_name);
338 return result;
341 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) {
342 std::string feature_type;
343 std::string feature_name;
344 SplitDependencyName(full_name, &feature_type, &feature_name);
346 FeatureProviderMap::iterator provider =
347 dependency_providers_.find(feature_type);
348 if (provider == dependency_providers_.end())
349 return NULL;
351 Feature* feature = provider->second->GetFeature(feature_name);
352 // Try getting the feature for the parent API, if this was a child.
353 if (!feature) {
354 std::string child_name;
355 feature = provider->second->GetFeature(
356 GetAPINameFromFullName(feature_name, &child_name));
358 return feature;
361 std::string ExtensionAPI::GetAPINameFromFullName(const std::string& full_name,
362 std::string* child_name) {
363 std::string api_name_candidate = full_name;
364 extensions::ExtensionsClient* extensions_client =
365 extensions::ExtensionsClient::Get();
366 DCHECK(extensions_client);
367 while (true) {
368 if (schemas_.find(api_name_candidate) != schemas_.end() ||
369 extensions_client->IsAPISchemaGenerated(api_name_candidate) ||
370 unloaded_schemas_.find(api_name_candidate) != unloaded_schemas_.end()) {
371 std::string result = api_name_candidate;
373 if (child_name) {
374 if (result.length() < full_name.length())
375 *child_name = full_name.substr(result.length() + 1);
376 else
377 *child_name = "";
380 return result;
383 size_t last_dot_index = api_name_candidate.rfind('.');
384 if (last_dot_index == std::string::npos)
385 break;
387 api_name_candidate = api_name_candidate.substr(0, last_dot_index);
390 *child_name = "";
391 return std::string();
394 } // namespace extensions