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"
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"
30 namespace extensions
{
34 const char* kChildKinds
[] = {
39 base::StringPiece
ReadFromResource(int resource_id
) {
40 return ResourceBundle::GetSharedInstance().GetRawDataResource(
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(
50 base::JSON_PARSE_RFC
| base::JSON_DETACHABLE_CHILDREN
, // options
54 // Tracking down http://crbug.com/121424
56 base::snprintf(buf
, arraysize(buf
), "%s: (%d) '%s'",
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 base::ListValue::From(result
.Pass());
66 const base::DictionaryValue
* FindListItem(const base::ListValue
* list
,
67 const std::string
& property_name
,
68 const std::string
& property_value
) {
69 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
70 const base::DictionaryValue
* item
= NULL
;
71 CHECK(list
->GetDictionary(i
, &item
))
72 << property_value
<< "/" << property_name
;
74 if (item
->GetString(property_name
, &value
) && value
== property_value
)
81 const base::DictionaryValue
* GetSchemaChild(
82 const base::DictionaryValue
* schema_node
,
83 const std::string
& child_name
) {
84 const base::DictionaryValue
* child_node
= NULL
;
85 for (size_t i
= 0; i
< arraysize(kChildKinds
); ++i
) {
86 const base::ListValue
* list_node
= NULL
;
87 if (!schema_node
->GetList(kChildKinds
[i
], &list_node
))
89 child_node
= FindListItem(list_node
, "name", child_name
);
99 : api(ExtensionAPI::CreateWithDefaultConfiguration()) {
101 scoped_ptr
<ExtensionAPI
> api
;
104 base::LazyInstance
<Static
> g_lazy_instance
= LAZY_INSTANCE_INITIALIZER
;
106 // May override |g_lazy_instance| for a test.
107 ExtensionAPI
* g_shared_instance_for_test
= NULL
;
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| + "." +
112 void MaybePrefixFieldWithNamespace(const std::string
& schema_namespace
,
113 base::DictionaryValue
* schema
,
114 const std::string
& key
) {
115 if (!schema
->HasKey(key
))
119 CHECK(schema
->GetString(key
, &old_id
));
120 if (old_id
.find(".") == std::string::npos
)
121 schema
->SetString(key
, schema_namespace
+ "." + old_id
);
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
);
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
);
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"))
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");
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
);
172 ExtensionAPI
* ExtensionAPI::GetSharedInstance() {
173 return g_shared_instance_for_test
? g_shared_instance_for_test
174 : g_lazy_instance
.Get().api
.get();
178 ExtensionAPI
* ExtensionAPI::CreateWithDefaultConfiguration() {
179 ExtensionAPI
* api
= new ExtensionAPI();
180 api
->InitDefaultConfiguration();
185 void ExtensionAPI::SplitDependencyName(const std::string
& full_name
,
186 std::string
* feature_type
,
187 std::string
* feature_name
) {
188 size_t colon_index
= full_name
.find(':');
189 if (colon_index
== std::string::npos
) {
190 // TODO(aa): Remove this code when all API descriptions have been updated.
191 *feature_type
= "api";
192 *feature_name
= full_name
;
196 *feature_type
= full_name
.substr(0, colon_index
);
197 *feature_name
= full_name
.substr(colon_index
+ 1);
200 ExtensionAPI::OverrideSharedInstanceForTest::OverrideSharedInstanceForTest(
201 ExtensionAPI
* testing_api
)
202 : original_api_(g_shared_instance_for_test
) {
203 g_shared_instance_for_test
= testing_api
;
206 ExtensionAPI::OverrideSharedInstanceForTest::~OverrideSharedInstanceForTest() {
207 g_shared_instance_for_test
= original_api_
;
210 void ExtensionAPI::LoadSchema(const std::string
& name
,
211 const base::StringPiece
& schema
) {
212 scoped_ptr
<base::ListValue
> schema_list(LoadSchemaList(name
, schema
));
213 std::string schema_namespace
;
214 extensions::ExtensionsClient
* extensions_client
=
215 extensions::ExtensionsClient::Get();
216 DCHECK(extensions_client
);
217 while (!schema_list
->empty()) {
218 base::DictionaryValue
* schema
= NULL
;
220 scoped_ptr
<base::Value
> value
;
221 schema_list
->Remove(schema_list
->GetSize() - 1, &value
);
222 CHECK(value
.release()->GetAsDictionary(&schema
));
225 CHECK(schema
->GetString("namespace", &schema_namespace
));
226 PrefixWithNamespace(schema_namespace
, schema
);
227 schemas_
[schema_namespace
] = make_linked_ptr(schema
);
228 if (!extensions_client
->IsAPISchemaGenerated(schema_namespace
))
229 CHECK_EQ(1u, unloaded_schemas_
.erase(schema_namespace
));
233 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) {
236 ExtensionAPI::~ExtensionAPI() {
239 void ExtensionAPI::InitDefaultConfiguration() {
240 const char* names
[] = {"api", "manifest", "permission"};
241 for (size_t i
= 0; i
< arraysize(names
); ++i
)
242 RegisterDependencyProvider(names
[i
], FeatureProvider::GetByName(names
[i
]));
244 ExtensionsClient::Get()->RegisterAPISchemaResources(this);
246 RegisterSchemaResource("declarativeWebRequest",
247 IDR_EXTENSION_API_JSON_DECLARATIVE_WEBREQUEST
);
248 RegisterSchemaResource("webViewRequest",
249 IDR_EXTENSION_API_JSON_WEB_VIEW_REQUEST
);
251 default_configuration_initialized_
= true;
254 void ExtensionAPI::RegisterSchemaResource(const std::string
& name
,
256 unloaded_schemas_
[name
] = resource_id
;
259 void ExtensionAPI::RegisterDependencyProvider(const std::string
& name
,
260 const FeatureProvider
* provider
) {
261 dependency_providers_
[name
] = provider
;
264 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature
& api
,
265 const Extension
* extension
,
266 Feature::Context context
,
268 FeatureProviderMap::iterator provider
= dependency_providers_
.find("api");
269 CHECK(provider
!= dependency_providers_
.end());
271 if (api
.IsAvailableToContext(extension
, context
, url
).is_available())
274 // Check to see if there are any parts of this API that are allowed in this
276 const std::vector
<Feature
*> features
= provider
->second
->GetChildren(api
);
277 for (std::vector
<Feature
*>::const_iterator it
= features
.begin();
278 it
!= features
.end();
280 if ((*it
)->IsAvailableToContext(extension
, context
, url
).is_available())
286 Feature::Availability
ExtensionAPI::IsAvailable(const std::string
& full_name
,
287 const Extension
* extension
,
288 Feature::Context context
,
290 Feature
* feature
= GetFeatureDependency(full_name
);
292 return Feature::Availability(Feature::NOT_PRESENT
,
293 std::string("Unknown feature: ") + full_name
);
295 return feature
->IsAvailableToContext(extension
, context
, url
);
298 bool ExtensionAPI::IsAvailableToWebUI(const std::string
& name
,
300 return IsAvailable(name
, NULL
, Feature::WEBUI_CONTEXT
, url
).is_available();
303 const base::DictionaryValue
* ExtensionAPI::GetSchema(
304 const std::string
& full_name
) {
305 std::string child_name
;
306 std::string api_name
= GetAPINameFromFullName(full_name
, &child_name
);
308 const base::DictionaryValue
* result
= NULL
;
309 SchemaMap::iterator maybe_schema
= schemas_
.find(api_name
);
310 if (maybe_schema
!= schemas_
.end()) {
311 result
= maybe_schema
->second
.get();
313 // Might not have loaded yet; or might just not exist.
314 UnloadedSchemaMap::iterator maybe_schema_resource
=
315 unloaded_schemas_
.find(api_name
);
316 extensions::ExtensionsClient
* extensions_client
=
317 extensions::ExtensionsClient::Get();
318 DCHECK(extensions_client
);
319 if (maybe_schema_resource
!= unloaded_schemas_
.end()) {
320 LoadSchema(maybe_schema_resource
->first
,
321 ReadFromResource(maybe_schema_resource
->second
));
322 } else if (default_configuration_initialized_
&&
323 extensions_client
->IsAPISchemaGenerated(api_name
)) {
324 LoadSchema(api_name
, extensions_client
->GetAPISchema(api_name
));
329 maybe_schema
= schemas_
.find(api_name
);
330 CHECK(schemas_
.end() != maybe_schema
);
331 result
= maybe_schema
->second
.get();
334 if (!child_name
.empty())
335 result
= GetSchemaChild(result
, child_name
);
340 Feature
* ExtensionAPI::GetFeatureDependency(const std::string
& full_name
) {
341 std::string feature_type
;
342 std::string feature_name
;
343 SplitDependencyName(full_name
, &feature_type
, &feature_name
);
345 FeatureProviderMap::iterator provider
=
346 dependency_providers_
.find(feature_type
);
347 if (provider
== dependency_providers_
.end())
350 Feature
* feature
= provider
->second
->GetFeature(feature_name
);
351 // Try getting the feature for the parent API, if this was a child.
353 std::string child_name
;
354 feature
= provider
->second
->GetFeature(
355 GetAPINameFromFullName(feature_name
, &child_name
));
360 std::string
ExtensionAPI::GetAPINameFromFullName(const std::string
& full_name
,
361 std::string
* child_name
) {
362 std::string api_name_candidate
= full_name
;
363 extensions::ExtensionsClient
* extensions_client
=
364 extensions::ExtensionsClient::Get();
365 DCHECK(extensions_client
);
367 if (schemas_
.find(api_name_candidate
) != schemas_
.end() ||
368 extensions_client
->IsAPISchemaGenerated(api_name_candidate
) ||
369 unloaded_schemas_
.find(api_name_candidate
) != unloaded_schemas_
.end()) {
370 std::string result
= api_name_candidate
;
373 if (result
.length() < full_name
.length())
374 *child_name
= full_name
.substr(result
.length() + 1);
382 size_t last_dot_index
= api_name_candidate
.rfind('.');
383 if (last_dot_index
== std::string::npos
)
386 api_name_candidate
= api_name_candidate
.substr(0, last_dot_index
);
390 return std::string();
393 } // namespace extensions