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 scoped_ptr
<base::ListValue
>(static_cast<base::ListValue
*>(
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
;
75 if (item
->GetString(property_name
, &value
) && value
== property_value
)
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
))
90 child_node
= FindListItem(list_node
, "name", child_name
);
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| + "." +
113 void MaybePrefixFieldWithNamespace(const std::string
& schema_namespace
,
114 base::DictionaryValue
* schema
,
115 const std::string
& key
) {
116 if (!schema
->HasKey(key
))
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"))
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
);
173 ExtensionAPI
* ExtensionAPI::GetSharedInstance() {
174 return g_shared_instance_for_test
? g_shared_instance_for_test
175 : g_lazy_instance
.Get().api
.get();
179 ExtensionAPI
* ExtensionAPI::CreateWithDefaultConfiguration() {
180 ExtensionAPI
* api
= new ExtensionAPI();
181 api
->InitDefaultConfiguration();
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
;
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
,
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
,
269 FeatureProviderMap::iterator provider
= dependency_providers_
.find("api");
270 CHECK(provider
!= dependency_providers_
.end());
272 if (api
.IsAvailableToContext(extension
, context
, url
).is_available())
275 // Check to see if there are any parts of this API that are allowed in this
277 const std::vector
<Feature
*> features
= provider
->second
->GetChildren(api
);
278 for (std::vector
<Feature
*>::const_iterator it
= features
.begin();
279 it
!= features
.end();
281 if ((*it
)->IsAvailableToContext(extension
, context
, url
).is_available())
287 Feature::Availability
ExtensionAPI::IsAvailable(const std::string
& full_name
,
288 const Extension
* extension
,
289 Feature::Context context
,
291 Feature
* feature
= GetFeatureDependency(full_name
);
293 return Feature::CreateAvailability(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
,
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();
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
));
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
);
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())
351 Feature
* feature
= provider
->second
->GetFeature(feature_name
);
352 // Try getting the feature for the parent API, if this was a child.
354 std::string child_name
;
355 feature
= provider
->second
->GetFeature(
356 GetAPINameFromFullName(feature_name
, &child_name
));
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
);
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
;
374 if (result
.length() < full_name
.length())
375 *child_name
= full_name
.substr(result
.length() + 1);
383 size_t last_dot_index
= api_name_candidate
.rfind('.');
384 if (last_dot_index
== std::string::npos
)
387 api_name_candidate
= api_name_candidate
.substr(0, last_dot_index
);
391 return std::string();
394 } // namespace extensions