• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "extensions/common/extension_api.h"
6 
7 #include <algorithm>
8 #include <string>
9 #include <vector>
10 
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"
26 #include "url/gurl.h"
27 
28 namespace extensions {
29 
30 namespace {
31 
32 const char* kChildKinds[] = {
33   "functions",
34   "events"
35 };
36 
ReadFromResource(int resource_id)37 base::StringPiece ReadFromResource(int resource_id) {
38   return ResourceBundle::GetSharedInstance().GetRawDataResource(
39       resource_id);
40 }
41 
LoadSchemaList(const std::string & name,const base::StringPiece & schema)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(
47           schema,
48           base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN,  // options
49           NULL,  // error code
50           &error_message));
51 
52   // Tracking down http://crbug.com/121424
53   char buf[128];
54   base::snprintf(buf, arraysize(buf), "%s: (%d) '%s'",
55       name.c_str(),
56       result.get() ? result->GetType() : -1,
57       error_message.c_str());
58 
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*>(
62       result.release()));
63 }
64 
FindListItem(const base::ListValue * list,const std::string & property_name,const std::string & property_value)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;
72     std::string value;
73     if (item->GetString(property_name, &value) && value == property_value)
74       return item;
75   }
76 
77   return NULL;
78 }
79 
GetSchemaChild(const base::DictionaryValue * schema_node,const std::string & child_name)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))
87       continue;
88     child_node = FindListItem(list_node, "name", child_name);
89     if (child_node)
90       return child_node;
91   }
92 
93   return NULL;
94 }
95 
96 struct Static {
Staticextensions::__anon8094b2700111::Static97   Static()
98       : api(ExtensionAPI::CreateWithDefaultConfiguration()) {
99   }
100   scoped_ptr<ExtensionAPI> api;
101 };
102 
103 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER;
104 
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| + "." +
107 // |schema[key]|.
MaybePrefixFieldWithNamespace(const std::string & schema_namespace,base::DictionaryValue * schema,const std::string & key)108 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace,
109                                    base::DictionaryValue* schema,
110                                    const std::string& key) {
111   if (!schema->HasKey(key))
112     return;
113 
114   std::string old_id;
115   CHECK(schema->GetString(key, &old_id));
116   if (old_id.find(".") == std::string::npos)
117     schema->SetString(key, schema_namespace + "." + old_id);
118 }
119 
120 // Modify all "$ref" keys anywhere in |schema| to be prefxied by
121 // |schema_namespace| if they do not already specify a namespace.
PrefixRefsWithNamespace(const std::string & schema_namespace,base::Value * value)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);
129     }
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);
136     }
137   }
138 }
139 
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.
PrefixTypesWithNamespace(const std::string & schema_namespace,base::DictionaryValue * schema)142 void PrefixTypesWithNamespace(const std::string& schema_namespace,
143                               base::DictionaryValue* schema) {
144   if (!schema->HasKey("types"))
145     return;
146 
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");
155   }
156 }
157 
158 // Modify the schema so that all types are fully qualified.
PrefixWithNamespace(const std::string & schema_namespace,base::DictionaryValue * schema)159 void PrefixWithNamespace(const std::string& schema_namespace,
160                          base::DictionaryValue* schema) {
161   PrefixTypesWithNamespace(schema_namespace, schema);
162   PrefixRefsWithNamespace(schema_namespace, schema);
163 }
164 
165 }  // namespace
166 
167 // static
GetSharedInstance()168 ExtensionAPI* ExtensionAPI::GetSharedInstance() {
169   return g_lazy_instance.Get().api.get();
170 }
171 
172 // static
CreateWithDefaultConfiguration()173 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() {
174   ExtensionAPI* api = new ExtensionAPI();
175   api->InitDefaultConfiguration();
176   return api;
177 }
178 
179 // static
SplitDependencyName(const std::string & full_name,std::string * feature_type,std::string * feature_name)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;
188     return;
189   }
190 
191   *feature_type = full_name.substr(0, colon_index);
192   *feature_name = full_name.substr(colon_index + 1);
193 }
194 
LoadSchema(const std::string & name,const base::StringPiece & schema)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;
204     {
205       scoped_ptr<base::Value> value;
206       schema_list->Remove(schema_list->GetSize() - 1, &value);
207       CHECK(value.release()->GetAsDictionary(&schema));
208     }
209 
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));
215   }
216 }
217 
ExtensionAPI()218 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) {
219 }
220 
~ExtensionAPI()221 ExtensionAPI::~ExtensionAPI() {
222 }
223 
InitDefaultConfiguration()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]));
228 
229   ExtensionsClient::Get()->RegisterAPISchemaResources(this);
230 
231   default_configuration_initialized_ = true;
232 }
233 
RegisterSchemaResource(const std::string & name,int resource_id)234 void ExtensionAPI::RegisterSchemaResource(const std::string& name,
235                                           int resource_id) {
236   unloaded_schemas_[name] = resource_id;
237 }
238 
RegisterDependencyProvider(const std::string & name,const FeatureProvider * provider)239 void ExtensionAPI::RegisterDependencyProvider(const std::string& name,
240                                               const FeatureProvider* provider) {
241   dependency_providers_[name] = provider;
242 }
243 
IsAnyFeatureAvailableToContext(const Feature & api,const Extension * extension,Feature::Context context,const GURL & url)244 bool ExtensionAPI::IsAnyFeatureAvailableToContext(const Feature& api,
245                                                   const Extension* extension,
246                                                   Feature::Context context,
247                                                   const GURL& url) {
248   FeatureProviderMap::iterator provider = dependency_providers_.find("api");
249   CHECK(provider != dependency_providers_.end());
250   if (IsAvailable(api, extension, context, url).is_available())
251     return true;
252 
253   // Check to see if there are any parts of this API that are allowed in this
254   // context.
255   const std::vector<Feature*> features = provider->second->GetChildren(api);
256   for (std::vector<Feature*>::const_iterator feature = features.begin();
257        feature != features.end();
258        ++feature) {
259     if (IsAvailable(**feature, extension, context, url).is_available())
260       return true;
261   }
262   return false;
263 }
264 
IsAvailable(const std::string & full_name,const Extension * extension,Feature::Context context,const GURL & url)265 Feature::Availability ExtensionAPI::IsAvailable(const std::string& full_name,
266                                                 const Extension* extension,
267                                                 Feature::Context context,
268                                                 const GURL& url) {
269   Feature* feature = GetFeatureDependency(full_name);
270   if (!feature) {
271     return Feature::CreateAvailability(Feature::NOT_PRESENT,
272         std::string("Unknown feature: ") + full_name);
273   }
274   return IsAvailable(*feature, extension, context, url);
275 }
276 
IsAvailable(const Feature & feature,const Extension * extension,Feature::Context context,const GURL & url)277 Feature::Availability ExtensionAPI::IsAvailable(const Feature& feature,
278                                                 const Extension* extension,
279                                                 Feature::Context context,
280                                                 const GURL& url) {
281   Feature::Availability availability =
282       feature.IsAvailableToContext(extension, context, url);
283   if (!availability.is_available())
284     return availability;
285 
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;
292   }
293 
294   return Feature::CreateAvailability(Feature::IS_AVAILABLE, std::string());
295 }
296 
IsPrivileged(const std::string & full_name)297 bool ExtensionAPI::IsPrivileged(const std::string& full_name) {
298   Feature* feature = GetFeatureDependency(full_name);
299   CHECK(feature);
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);
304 }
305 
GetSchema(const std::string & full_name)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);
310 
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();
315   } else {
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));
328     } else {
329       return NULL;
330     }
331 
332     maybe_schema = schemas_.find(api_name);
333     CHECK(schemas_.end() != maybe_schema);
334     result = maybe_schema->second.get();
335   }
336 
337   if (!child_name.empty())
338     result = GetSchemaChild(result, child_name);
339 
340   return result;
341 }
342 
GetFeatureDependency(const std::string & full_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);
347 
348   FeatureProviderMap::iterator provider =
349       dependency_providers_.find(feature_type);
350   if (provider == dependency_providers_.end())
351     return NULL;
352 
353   Feature* feature = provider->second->GetFeature(feature_name);
354   // Try getting the feature for the parent API, if this was a child.
355   if (!feature) {
356     std::string child_name;
357     feature = provider->second->GetFeature(
358         GetAPINameFromFullName(feature_name, &child_name));
359   }
360   return feature;
361 }
362 
GetAPINameFromFullName(const std::string & full_name,std::string * 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);
369   while (true) {
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;
374 
375       if (child_name) {
376         if (result.length() < full_name.length())
377           *child_name = full_name.substr(result.length() + 1);
378         else
379           *child_name = "";
380       }
381 
382       return result;
383     }
384 
385     size_t last_dot_index = api_name_candidate.rfind('.');
386     if (last_dot_index == std::string::npos)
387       break;
388 
389     api_name_candidate = api_name_candidate.substr(0, last_dot_index);
390   }
391 
392   *child_name = "";
393   return std::string();
394 }
395 
396 }  // namespace extensions
397