OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/extension_api.h" | 5 #include "extensions/common/extension_api.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 }; | 36 }; |
37 | 37 |
38 base::StringPiece ReadFromResource(int resource_id) { | 38 base::StringPiece ReadFromResource(int resource_id) { |
39 return ResourceBundle::GetSharedInstance().GetRawDataResource( | 39 return ResourceBundle::GetSharedInstance().GetRawDataResource( |
40 resource_id); | 40 resource_id); |
41 } | 41 } |
42 | 42 |
43 scoped_ptr<base::ListValue> LoadSchemaList(const std::string& name, | 43 scoped_ptr<base::ListValue> LoadSchemaList(const std::string& name, |
44 const base::StringPiece& schema) { | 44 const base::StringPiece& schema) { |
45 std::string error_message; | 45 std::string error_message; |
46 scoped_ptr<base::Value> result( | 46 scoped_ptr<base::Value> result(base::JSONReader::ReadAndReturnError( |
47 base::JSONReader::ReadAndReturnError( | 47 schema, |
48 schema, | 48 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN, // options |
49 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN, // options | 49 nullptr, // error code |
50 NULL, // error code | 50 &error_message)); |
51 &error_message)); | |
52 | 51 |
53 // Tracking down http://crbug.com/121424 | 52 // Tracking down http://crbug.com/121424 |
54 char buf[128]; | 53 char buf[128]; |
55 base::snprintf(buf, arraysize(buf), "%s: (%d) '%s'", | 54 base::snprintf(buf, arraysize(buf), "%s: (%d) '%s'", |
56 name.c_str(), | 55 name.c_str(), |
57 result.get() ? result->GetType() : -1, | 56 result.get() ? result->GetType() : -1, |
58 error_message.c_str()); | 57 error_message.c_str()); |
59 | 58 |
60 CHECK(result.get()) << error_message << " for schema " << schema; | 59 CHECK(result.get()) << error_message << " for schema " << schema; |
61 CHECK(result->IsType(base::Value::TYPE_LIST)) << " for schema " << schema; | 60 CHECK(result->IsType(base::Value::TYPE_LIST)) << " for schema " << schema; |
62 return scoped_ptr<base::ListValue>(static_cast<base::ListValue*>( | 61 return scoped_ptr<base::ListValue>(static_cast<base::ListValue*>( |
63 result.release())); | 62 result.release())); |
64 } | 63 } |
65 | 64 |
66 const base::DictionaryValue* FindListItem(const base::ListValue* list, | 65 const base::DictionaryValue* FindListItem(const base::ListValue* list, |
67 const std::string& property_name, | 66 const std::string& property_name, |
68 const std::string& property_value) { | 67 const std::string& property_value) { |
69 for (size_t i = 0; i < list->GetSize(); ++i) { | 68 for (size_t i = 0; i < list->GetSize(); ++i) { |
70 const base::DictionaryValue* item = NULL; | 69 const base::DictionaryValue* item = nullptr; |
71 CHECK(list->GetDictionary(i, &item)) | 70 CHECK(list->GetDictionary(i, &item)) |
72 << property_value << "/" << property_name; | 71 << property_value << "/" << property_name; |
73 std::string value; | 72 std::string value; |
74 if (item->GetString(property_name, &value) && value == property_value) | 73 if (item->GetString(property_name, &value) && value == property_value) |
75 return item; | 74 return item; |
76 } | 75 } |
77 | 76 |
78 return NULL; | 77 return nullptr; |
79 } | 78 } |
80 | 79 |
81 const base::DictionaryValue* GetSchemaChild( | 80 const base::DictionaryValue* GetSchemaChild( |
82 const base::DictionaryValue* schema_node, | 81 const base::DictionaryValue* schema_node, |
83 const std::string& child_name) { | 82 const std::string& child_name) { |
84 const base::DictionaryValue* child_node = NULL; | 83 const base::DictionaryValue* child_node = nullptr; |
85 for (size_t i = 0; i < arraysize(kChildKinds); ++i) { | 84 for (size_t i = 0; i < arraysize(kChildKinds); ++i) { |
86 const base::ListValue* list_node = NULL; | 85 const base::ListValue* list_node = nullptr; |
87 if (!schema_node->GetList(kChildKinds[i], &list_node)) | 86 if (!schema_node->GetList(kChildKinds[i], &list_node)) |
88 continue; | 87 continue; |
89 child_node = FindListItem(list_node, "name", child_name); | 88 child_node = FindListItem(list_node, "name", child_name); |
90 if (child_node) | 89 if (child_node) |
91 return child_node; | 90 return child_node; |
92 } | 91 } |
93 | 92 |
94 return NULL; | 93 return nullptr; |
95 } | 94 } |
96 | 95 |
97 struct Static { | 96 struct Static { |
98 Static() | 97 Static() |
99 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { | 98 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { |
100 } | 99 } |
101 scoped_ptr<ExtensionAPI> api; | 100 scoped_ptr<ExtensionAPI> api; |
102 }; | 101 }; |
103 | 102 |
104 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; | 103 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; |
105 | 104 |
106 // May override |g_lazy_instance| for a test. | 105 // May override |g_lazy_instance| for a test. |
107 ExtensionAPI* g_shared_instance_for_test = NULL; | 106 ExtensionAPI* g_shared_instance_for_test = nullptr; |
108 | 107 |
109 // If it exists and does not already specify a namespace, then the value stored | 108 // 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| + "." + | 109 // with key |key| in |schema| will be updated to |schema_namespace| + "." + |
111 // |schema[key]|. | 110 // |schema[key]|. |
112 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace, | 111 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace, |
113 base::DictionaryValue* schema, | 112 base::DictionaryValue* schema, |
114 const std::string& key) { | 113 const std::string& key) { |
115 if (!schema->HasKey(key)) | 114 if (!schema->HasKey(key)) |
116 return; | 115 return; |
117 | 116 |
118 std::string old_id; | 117 std::string old_id; |
119 CHECK(schema->GetString(key, &old_id)); | 118 CHECK(schema->GetString(key, &old_id)); |
120 if (old_id.find(".") == std::string::npos) | 119 if (old_id.find(".") == std::string::npos) |
121 schema->SetString(key, schema_namespace + "." + old_id); | 120 schema->SetString(key, schema_namespace + "." + old_id); |
122 } | 121 } |
123 | 122 |
124 // Modify all "$ref" keys anywhere in |schema| to be prefxied by | 123 // Modify all "$ref" keys anywhere in |schema| to be prefxied by |
125 // |schema_namespace| if they do not already specify a namespace. | 124 // |schema_namespace| if they do not already specify a namespace. |
126 void PrefixRefsWithNamespace(const std::string& schema_namespace, | 125 void PrefixRefsWithNamespace(const std::string& schema_namespace, |
127 base::Value* value) { | 126 base::Value* value) { |
128 base::ListValue* list = NULL; | 127 base::ListValue* list = nullptr; |
129 base::DictionaryValue* dict = NULL; | 128 base::DictionaryValue* dict = nullptr; |
130 if (value->GetAsList(&list)) { | 129 if (value->GetAsList(&list)) { |
131 for (base::ListValue::iterator i = list->begin(); i != list->end(); ++i) { | 130 for (base::ListValue::iterator i = list->begin(); i != list->end(); ++i) { |
132 PrefixRefsWithNamespace(schema_namespace, *i); | 131 PrefixRefsWithNamespace(schema_namespace, *i); |
133 } | 132 } |
134 } else if (value->GetAsDictionary(&dict)) { | 133 } else if (value->GetAsDictionary(&dict)) { |
135 MaybePrefixFieldWithNamespace(schema_namespace, dict, "$ref"); | 134 MaybePrefixFieldWithNamespace(schema_namespace, dict, "$ref"); |
136 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { | 135 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { |
137 base::Value* value = NULL; | 136 base::Value* value = nullptr; |
138 CHECK(dict->GetWithoutPathExpansion(i.key(), &value)); | 137 CHECK(dict->GetWithoutPathExpansion(i.key(), &value)); |
139 PrefixRefsWithNamespace(schema_namespace, value); | 138 PrefixRefsWithNamespace(schema_namespace, value); |
140 } | 139 } |
141 } | 140 } |
142 } | 141 } |
143 | 142 |
144 // Modify all objects in the "types" section of the schema to be prefixed by | 143 // 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. | 144 // |schema_namespace| if they do not already specify a namespace. |
146 void PrefixTypesWithNamespace(const std::string& schema_namespace, | 145 void PrefixTypesWithNamespace(const std::string& schema_namespace, |
147 base::DictionaryValue* schema) { | 146 base::DictionaryValue* schema) { |
148 if (!schema->HasKey("types")) | 147 if (!schema->HasKey("types")) |
149 return; | 148 return; |
150 | 149 |
151 // Add the namespace to all of the types defined in this schema | 150 // Add the namespace to all of the types defined in this schema |
152 base::ListValue *types = NULL; | 151 base::ListValue* types = nullptr; |
153 CHECK(schema->GetList("types", &types)); | 152 CHECK(schema->GetList("types", &types)); |
154 for (size_t i = 0; i < types->GetSize(); ++i) { | 153 for (size_t i = 0; i < types->GetSize(); ++i) { |
155 base::DictionaryValue *type = NULL; | 154 base::DictionaryValue* type = nullptr; |
156 CHECK(types->GetDictionary(i, &type)); | 155 CHECK(types->GetDictionary(i, &type)); |
157 MaybePrefixFieldWithNamespace(schema_namespace, type, "id"); | 156 MaybePrefixFieldWithNamespace(schema_namespace, type, "id"); |
158 MaybePrefixFieldWithNamespace(schema_namespace, type, "customBindings"); | 157 MaybePrefixFieldWithNamespace(schema_namespace, type, "customBindings"); |
159 } | 158 } |
160 } | 159 } |
161 | 160 |
162 // Modify the schema so that all types are fully qualified. | 161 // Modify the schema so that all types are fully qualified. |
163 void PrefixWithNamespace(const std::string& schema_namespace, | 162 void PrefixWithNamespace(const std::string& schema_namespace, |
164 base::DictionaryValue* schema) { | 163 base::DictionaryValue* schema) { |
165 PrefixTypesWithNamespace(schema_namespace, schema); | 164 PrefixTypesWithNamespace(schema_namespace, schema); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 207 } |
209 | 208 |
210 void ExtensionAPI::LoadSchema(const std::string& name, | 209 void ExtensionAPI::LoadSchema(const std::string& name, |
211 const base::StringPiece& schema) { | 210 const base::StringPiece& schema) { |
212 scoped_ptr<base::ListValue> schema_list(LoadSchemaList(name, schema)); | 211 scoped_ptr<base::ListValue> schema_list(LoadSchemaList(name, schema)); |
213 std::string schema_namespace; | 212 std::string schema_namespace; |
214 extensions::ExtensionsClient* extensions_client = | 213 extensions::ExtensionsClient* extensions_client = |
215 extensions::ExtensionsClient::Get(); | 214 extensions::ExtensionsClient::Get(); |
216 DCHECK(extensions_client); | 215 DCHECK(extensions_client); |
217 while (!schema_list->empty()) { | 216 while (!schema_list->empty()) { |
218 base::DictionaryValue* schema = NULL; | 217 base::DictionaryValue* schema = nullptr; |
219 { | 218 { |
220 scoped_ptr<base::Value> value; | 219 scoped_ptr<base::Value> value; |
221 schema_list->Remove(schema_list->GetSize() - 1, &value); | 220 schema_list->Remove(schema_list->GetSize() - 1, &value); |
222 CHECK(value.release()->GetAsDictionary(&schema)); | 221 CHECK(value.release()->GetAsDictionary(&schema)); |
223 } | 222 } |
224 | 223 |
225 CHECK(schema->GetString("namespace", &schema_namespace)); | 224 CHECK(schema->GetString("namespace", &schema_namespace)); |
226 PrefixWithNamespace(schema_namespace, schema); | 225 PrefixWithNamespace(schema_namespace, schema); |
227 schemas_[schema_namespace] = make_linked_ptr(schema); | 226 schemas_[schema_namespace] = make_linked_ptr(schema); |
228 if (!extensions_client->IsAPISchemaGenerated(schema_namespace)) | 227 if (!extensions_client->IsAPISchemaGenerated(schema_namespace)) |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 name, extension, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL()) | 297 name, extension, Feature::UNBLESSED_EXTENSION_CONTEXT, GURL()) |
299 .is_available() || | 298 .is_available() || |
300 IsAvailable(name, extension, Feature::BLESSED_WEB_PAGE_CONTEXT, GURL()) | 299 IsAvailable(name, extension, Feature::BLESSED_WEB_PAGE_CONTEXT, GURL()) |
301 .is_available() || | 300 .is_available() || |
302 IsAvailable(name, extension, Feature::WEB_PAGE_CONTEXT, GURL()) | 301 IsAvailable(name, extension, Feature::WEB_PAGE_CONTEXT, GURL()) |
303 .is_available(); | 302 .is_available(); |
304 } | 303 } |
305 | 304 |
306 bool ExtensionAPI::IsAvailableToWebUI(const std::string& name, | 305 bool ExtensionAPI::IsAvailableToWebUI(const std::string& name, |
307 const GURL& url) { | 306 const GURL& url) { |
308 return IsAvailable(name, NULL, Feature::WEBUI_CONTEXT, url).is_available(); | 307 return IsAvailable(name, nullptr, Feature::WEBUI_CONTEXT, url).is_available(); |
309 } | 308 } |
310 | 309 |
311 const base::DictionaryValue* ExtensionAPI::GetSchema( | 310 const base::DictionaryValue* ExtensionAPI::GetSchema( |
312 const std::string& full_name) { | 311 const std::string& full_name) { |
313 std::string child_name; | 312 std::string child_name; |
314 std::string api_name = GetAPINameFromFullName(full_name, &child_name); | 313 std::string api_name = GetAPINameFromFullName(full_name, &child_name); |
315 | 314 |
316 const base::DictionaryValue* result = NULL; | 315 const base::DictionaryValue* result = nullptr; |
317 SchemaMap::iterator maybe_schema = schemas_.find(api_name); | 316 SchemaMap::iterator maybe_schema = schemas_.find(api_name); |
318 if (maybe_schema != schemas_.end()) { | 317 if (maybe_schema != schemas_.end()) { |
319 result = maybe_schema->second.get(); | 318 result = maybe_schema->second.get(); |
320 } else { | 319 } else { |
321 // Might not have loaded yet; or might just not exist. | 320 // Might not have loaded yet; or might just not exist. |
322 UnloadedSchemaMap::iterator maybe_schema_resource = | 321 UnloadedSchemaMap::iterator maybe_schema_resource = |
323 unloaded_schemas_.find(api_name); | 322 unloaded_schemas_.find(api_name); |
324 extensions::ExtensionsClient* extensions_client = | 323 extensions::ExtensionsClient* extensions_client = |
325 extensions::ExtensionsClient::Get(); | 324 extensions::ExtensionsClient::Get(); |
326 DCHECK(extensions_client); | 325 DCHECK(extensions_client); |
327 if (maybe_schema_resource != unloaded_schemas_.end()) { | 326 if (maybe_schema_resource != unloaded_schemas_.end()) { |
328 LoadSchema(maybe_schema_resource->first, | 327 LoadSchema(maybe_schema_resource->first, |
329 ReadFromResource(maybe_schema_resource->second)); | 328 ReadFromResource(maybe_schema_resource->second)); |
330 } else if (default_configuration_initialized_ && | 329 } else if (default_configuration_initialized_ && |
331 extensions_client->IsAPISchemaGenerated(api_name)) { | 330 extensions_client->IsAPISchemaGenerated(api_name)) { |
332 LoadSchema(api_name, extensions_client->GetAPISchema(api_name)); | 331 LoadSchema(api_name, extensions_client->GetAPISchema(api_name)); |
333 } else { | 332 } else { |
334 return NULL; | 333 return nullptr; |
335 } | 334 } |
336 | 335 |
337 maybe_schema = schemas_.find(api_name); | 336 maybe_schema = schemas_.find(api_name); |
338 CHECK(schemas_.end() != maybe_schema); | 337 CHECK(schemas_.end() != maybe_schema); |
339 result = maybe_schema->second.get(); | 338 result = maybe_schema->second.get(); |
340 } | 339 } |
341 | 340 |
342 if (!child_name.empty()) | 341 if (!child_name.empty()) |
343 result = GetSchemaChild(result, child_name); | 342 result = GetSchemaChild(result, child_name); |
344 | 343 |
345 return result; | 344 return result; |
346 } | 345 } |
347 | 346 |
348 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) { | 347 Feature* ExtensionAPI::GetFeatureDependency(const std::string& full_name) { |
349 std::string feature_type; | 348 std::string feature_type; |
350 std::string feature_name; | 349 std::string feature_name; |
351 SplitDependencyName(full_name, &feature_type, &feature_name); | 350 SplitDependencyName(full_name, &feature_type, &feature_name); |
352 | 351 |
353 FeatureProviderMap::iterator provider = | 352 FeatureProviderMap::iterator provider = |
354 dependency_providers_.find(feature_type); | 353 dependency_providers_.find(feature_type); |
355 if (provider == dependency_providers_.end()) | 354 if (provider == dependency_providers_.end()) |
356 return NULL; | 355 return nullptr; |
357 | 356 |
358 Feature* feature = provider->second->GetFeature(feature_name); | 357 Feature* feature = provider->second->GetFeature(feature_name); |
359 // Try getting the feature for the parent API, if this was a child. | 358 // Try getting the feature for the parent API, if this was a child. |
360 if (!feature) { | 359 if (!feature) { |
361 std::string child_name; | 360 std::string child_name; |
362 feature = provider->second->GetFeature( | 361 feature = provider->second->GetFeature( |
363 GetAPINameFromFullName(feature_name, &child_name)); | 362 GetAPINameFromFullName(feature_name, &child_name)); |
364 } | 363 } |
365 return feature; | 364 return feature; |
366 } | 365 } |
(...skipping 25 matching lines...) Expand all Loading... |
392 break; | 391 break; |
393 | 392 |
394 api_name_candidate = api_name_candidate.substr(0, last_dot_index); | 393 api_name_candidate = api_name_candidate.substr(0, last_dot_index); |
395 } | 394 } |
396 | 395 |
397 *child_name = ""; | 396 *child_name = ""; |
398 return std::string(); | 397 return std::string(); |
399 } | 398 } |
400 | 399 |
401 } // namespace extensions | 400 } // namespace extensions |
OLD | NEW |