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/permissions/permission_set.h"
24 #include "extensions/common/permissions/permissions_data.h"
25 #include "ui/base/resource/resource_bundle.h"
28 namespace extensions
{
32 const char* kChildKinds
[] = {
37 base::StringPiece
ReadFromResource(int resource_id
) {
38 return ResourceBundle::GetSharedInstance().GetRawDataResource(
42 scoped_ptr
<base::ListValue
> LoadSchemaList(const std::string
& name
,
43 const base::StringPiece
& schema
) {
44 std::string error_message
;
45 scoped_ptr
<base::Value
> result(
46 base::JSONReader::ReadAndReturnError(
48 base::JSON_PARSE_RFC
| base::JSON_DETACHABLE_CHILDREN
, // options
52 // Tracking down http://crbug.com/121424
54 base::snprintf(buf
, arraysize(buf
), "%s: (%d) '%s'",
56 result
.get() ? result
->GetType() : -1,
57 error_message
.c_str());
59 CHECK(result
.get()) << error_message
<< " for schema " << schema
;
60 CHECK(result
->IsType(base::Value::TYPE_LIST
)) << " for schema " << schema
;
61 return scoped_ptr
<base::ListValue
>(static_cast<base::ListValue
*>(
65 const base::DictionaryValue
* FindListItem(const base::ListValue
* list
,
66 const std::string
& property_name
,
67 const std::string
& property_value
) {
68 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
69 const base::DictionaryValue
* item
= NULL
;
70 CHECK(list
->GetDictionary(i
, &item
))
71 << property_value
<< "/" << property_name
;
73 if (item
->GetString(property_name
, &value
) && value
== property_value
)
80 const base::DictionaryValue
* GetSchemaChild(
81 const base::DictionaryValue
* schema_node
,
82 const std::string
& child_name
) {
83 const base::DictionaryValue
* child_node
= NULL
;
84 for (size_t i
= 0; i
< arraysize(kChildKinds
); ++i
) {
85 const base::ListValue
* list_node
= NULL
;
86 if (!schema_node
->GetList(kChildKinds
[i
], &list_node
))
88 child_node
= FindListItem(list_node
, "name", child_name
);
98 : api(ExtensionAPI::CreateWithDefaultConfiguration()) {
100 scoped_ptr
<ExtensionAPI
> api
;
103 base::LazyInstance
<Static
> g_lazy_instance
= LAZY_INSTANCE_INITIALIZER
;
105 // If it exists and does not already specify a namespace, then the value stored
106 // with key |key| in |schema| will be updated to |schema_namespace| + "." +
108 void MaybePrefixFieldWithNamespace(const std::string
& schema_namespace
,
109 base::DictionaryValue
* schema
,
110 const std::string
& key
) {
111 if (!schema
->HasKey(key
))
115 CHECK(schema
->GetString(key
, &old_id
));
116 if (old_id
.find(".") == std::string::npos
)
117 schema
->SetString(key
, schema_namespace
+ "." + old_id
);
120 // Modify all "$ref" keys anywhere in |schema| to be prefxied by
121 // |schema_namespace| if they do not already specify a namespace.
122 void PrefixRefsWithNamespace(const std::string
& schema_namespace
,
123 base::Value
* value
) {
124 base::ListValue
* list
= NULL
;
125 base::DictionaryValue
* dict
= NULL
;
126 if (value
->GetAsList(&list
)) {
127 for (base::ListValue::iterator i
= list
->begin(); i
!= list
->end(); ++i
) {
128 PrefixRefsWithNamespace(schema_namespace
, *i
);
130 } else if (value
->GetAsDictionary(&dict
)) {
131 MaybePrefixFieldWithNamespace(schema_namespace
, dict
, "$ref");
132 for (base::DictionaryValue::Iterator
i(*dict
); !i
.IsAtEnd(); i
.Advance()) {
133 base::Value
* value
= NULL
;
134 CHECK(dict
->GetWithoutPathExpansion(i
.key(), &value
));
135 PrefixRefsWithNamespace(schema_namespace
, value
);
140 // Modify all objects in the "types" section of the schema to be prefixed by
141 // |schema_namespace| if they do not already specify a namespace.
142 void PrefixTypesWithNamespace(const std::string
& schema_namespace
,
143 base::DictionaryValue
* schema
) {
144 if (!schema
->HasKey("types"))
147 // Add the namespace to all of the types defined in this schema
148 base::ListValue
*types
= NULL
;
149 CHECK(schema
->GetList("types", &types
));
150 for (size_t i
= 0; i
< types
->GetSize(); ++i
) {
151 base::DictionaryValue
*type
= NULL
;
152 CHECK(types
->GetDictionary(i
, &type
));
153 MaybePrefixFieldWithNamespace(schema_namespace
, type
, "id");
154 MaybePrefixFieldWithNamespace(schema_namespace
, type
, "customBindings");
158 // Modify the schema so that all types are fully qualified.
159 void PrefixWithNamespace(const std::string
& schema_namespace
,
160 base::DictionaryValue
* schema
) {
161 PrefixTypesWithNamespace(schema_namespace
, schema
);
162 PrefixRefsWithNamespace(schema_namespace
, schema
);
168 ExtensionAPI
* ExtensionAPI::GetSharedInstance() {
169 return g_lazy_instance
.Get().api
.get();
173 ExtensionAPI
* ExtensionAPI::CreateWithDefaultConfiguration() {
174 ExtensionAPI
* api
= new ExtensionAPI();
175 api
->InitDefaultConfiguration();
180 void ExtensionAPI::SplitDependencyName(const std::string
& full_name
,
181 std::string
* feature_type
,
182 std::string
* feature_name
) {
183 size_t colon_index
= full_name
.find(':');
184 if (colon_index
== std::string::npos
) {
185 // TODO(aa): Remove this code when all API descriptions have been updated.
186 *feature_type
= "api";
187 *feature_name
= full_name
;
191 *feature_type
= full_name
.substr(0, colon_index
);
192 *feature_name
= full_name
.substr(colon_index
+ 1);
195 void ExtensionAPI::LoadSchema(const std::string
& name
,
196 const base::StringPiece
& schema
) {
197 scoped_ptr
<base::ListValue
> schema_list(LoadSchemaList(name
, schema
));
198 std::string schema_namespace
;
199 extensions::ExtensionsClient
* extensions_client
=
200 extensions::ExtensionsClient::Get();
201 DCHECK(extensions_client
);
202 while (!schema_list
->empty()) {
203 base::DictionaryValue
* schema
= NULL
;
205 scoped_ptr
<base::Value
> value
;
206 schema_list
->Remove(schema_list
->GetSize() - 1, &value
);
207 CHECK(value
.release()->GetAsDictionary(&schema
));
210 CHECK(schema
->GetString("namespace", &schema_namespace
));
211 PrefixWithNamespace(schema_namespace
, schema
);
212 schemas_
[schema_namespace
] = make_linked_ptr(schema
);
213 if (!extensions_client
->IsAPISchemaGenerated(schema_namespace
))
214 CHECK_EQ(1u, unloaded_schemas_
.erase(schema_namespace
));
218 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) {
221 ExtensionAPI::~ExtensionAPI() {
224 void ExtensionAPI::InitDefaultConfiguration() {
225 const char* names
[] = {"api", "manifest", "permission"};
226 for (size_t i
= 0; i
< arraysize(names
); ++i
)
227 RegisterDependencyProvider(names
[i
], FeatureProvider::GetByName(names
[i
]));
229 ExtensionsClient::Get()->RegisterAPISchemaResources(this);
231 default_configuration_initialized_
= true;
234 void ExtensionAPI::RegisterSchemaResource(const std::string
& name
,
236 unloaded_schemas_
[name
] = resource_id
;
239 void ExtensionAPI::RegisterDependencyProvider(const std::string
& name
,
240 const FeatureProvider
* provider
) {
241 dependency_providers_
[name
] = provider
;
244 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature
& api
,
245 const Extension
* extension
,
246 Feature::Context context
,
248 FeatureProviderMap::iterator provider
= dependency_providers_
.find("api");
249 CHECK(provider
!= dependency_providers_
.end());
250 if (IsAvailable(api
, extension
, context
, url
).is_available())
253 // Check to see if there are any parts of this API that are allowed in this
255 const std::vector
<Feature
*> features
= provider
->second
->GetChildren(api
);
256 for (std::vector
<Feature
*>::const_iterator feature
= features
.begin();
257 feature
!= features
.end();
259 if (IsAvailable(**feature
, extension
, context
, url
).is_available())
265 Feature::Availability
ExtensionAPI::IsAvailable(const std::string
& full_name
,
266 const Extension
* extension
,
267 Feature::Context context
,
269 Feature
* feature
= GetFeatureDependency(full_name
);
271 return Feature::CreateAvailability(Feature::NOT_PRESENT
,
272 std::string("Unknown feature: ") + full_name
);
274 return IsAvailable(*feature
, extension
, context
, url
);
277 Feature::Availability
ExtensionAPI::IsAvailable(const Feature
& feature
,
278 const Extension
* extension
,
279 Feature::Context context
,
281 Feature::Availability availability
=
282 feature
.IsAvailableToContext(extension
, context
, url
);
283 if (!availability
.is_available())
286 for (std::set
<std::string
>::iterator iter
= feature
.dependencies().begin();
287 iter
!= feature
.dependencies().end(); ++iter
) {
288 Feature::Availability dependency_availability
=
289 IsAvailable(*iter
, extension
, context
, url
);
290 if (!dependency_availability
.is_available())
291 return dependency_availability
;
294 return Feature::CreateAvailability(Feature::IS_AVAILABLE
, std::string());
297 bool ExtensionAPI::IsPrivileged(const std::string
& full_name
) {
298 Feature
* feature
= GetFeatureDependency(full_name
);
300 DCHECK(!feature
->GetContexts()->empty());
301 // An API is 'privileged' if it can only be run in a blessed context.
302 return feature
->GetContexts()->size() ==
303 feature
->GetContexts()->count(Feature::BLESSED_EXTENSION_CONTEXT
);
306 const base::DictionaryValue
* ExtensionAPI::GetSchema(
307 const std::string
& full_name
) {
308 std::string child_name
;
309 std::string api_name
= GetAPINameFromFullName(full_name
, &child_name
);
311 const base::DictionaryValue
* result
= NULL
;
312 SchemaMap::iterator maybe_schema
= schemas_
.find(api_name
);
313 if (maybe_schema
!= schemas_
.end()) {
314 result
= maybe_schema
->second
.get();
316 // Might not have loaded yet; or might just not exist.
317 UnloadedSchemaMap::iterator maybe_schema_resource
=
318 unloaded_schemas_
.find(api_name
);
319 extensions::ExtensionsClient
* extensions_client
=
320 extensions::ExtensionsClient::Get();
321 DCHECK(extensions_client
);
322 if (maybe_schema_resource
!= unloaded_schemas_
.end()) {
323 LoadSchema(maybe_schema_resource
->first
,
324 ReadFromResource(maybe_schema_resource
->second
));
325 } else if (default_configuration_initialized_
&&
326 extensions_client
->IsAPISchemaGenerated(api_name
)) {
327 LoadSchema(api_name
, extensions_client
->GetAPISchema(api_name
));
332 maybe_schema
= schemas_
.find(api_name
);
333 CHECK(schemas_
.end() != maybe_schema
);
334 result
= maybe_schema
->second
.get();
337 if (!child_name
.empty())
338 result
= GetSchemaChild(result
, child_name
);
343 Feature
* ExtensionAPI::GetFeatureDependency(const std::string
& full_name
) {
344 std::string feature_type
;
345 std::string feature_name
;
346 SplitDependencyName(full_name
, &feature_type
, &feature_name
);
348 FeatureProviderMap::iterator provider
=
349 dependency_providers_
.find(feature_type
);
350 if (provider
== dependency_providers_
.end())
353 Feature
* feature
= provider
->second
->GetFeature(feature_name
);
354 // Try getting the feature for the parent API, if this was a child.
356 std::string child_name
;
357 feature
= provider
->second
->GetFeature(
358 GetAPINameFromFullName(feature_name
, &child_name
));
363 std::string
ExtensionAPI::GetAPINameFromFullName(const std::string
& full_name
,
364 std::string
* child_name
) {
365 std::string api_name_candidate
= full_name
;
366 extensions::ExtensionsClient
* extensions_client
=
367 extensions::ExtensionsClient::Get();
368 DCHECK(extensions_client
);
370 if (schemas_
.find(api_name_candidate
) != schemas_
.end() ||
371 extensions_client
->IsAPISchemaGenerated(api_name_candidate
) ||
372 unloaded_schemas_
.find(api_name_candidate
) != unloaded_schemas_
.end()) {
373 std::string result
= api_name_candidate
;
376 if (result
.length() < full_name
.length())
377 *child_name
= full_name
.substr(result
.length() + 1);
385 size_t last_dot_index
= api_name_candidate
.rfind('.');
386 if (last_dot_index
== std::string::npos
)
389 api_name_candidate
= api_name_candidate
.substr(0, last_dot_index
);
393 return std::string();
396 } // namespace extensions