Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chrome/common/extensions/api/extension_api_unittest.cc

Issue 12522004: Lazily load extension API schemas (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/common/extensions/api/extension_api.h" 5 #include "chrome/common/extensions/api/extension_api.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 << test_data[i].filename; 156 << test_data[i].filename;
157 157
158 ExtensionAPI api; 158 ExtensionAPI api;
159 api.RegisterSchema("test", manifest_str); 159 api.RegisterSchema("test", manifest_str);
160 160
161 TestFeatureProvider test2_provider(test_data[i].test2_contexts); 161 TestFeatureProvider test2_provider(test_data[i].test2_contexts);
162 if (test_data[i].test2_contexts != Feature::UNSPECIFIED_CONTEXT) { 162 if (test_data[i].test2_contexts != Feature::UNSPECIFIED_CONTEXT) {
163 api.RegisterDependencyProvider("test2", &test2_provider); 163 api.RegisterDependencyProvider("test2", &test2_provider);
164 } 164 }
165 165
166 api.LoadAllSchemas();
167 EXPECT_EQ(test_data[i].expect_is_privilged, 166 EXPECT_EQ(test_data[i].expect_is_privilged,
168 api.IsPrivileged(test_data[i].api_full_name)) << i; 167 api.IsPrivileged(test_data[i].api_full_name)) << i;
169 } 168 }
170 } 169 }
171 170
172 TEST(ExtensionAPI, LazyGetSchema) { 171 TEST(ExtensionAPI, LazyGetSchema) {
173 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration()); 172 scoped_ptr<ExtensionAPI> apis(ExtensionAPI::CreateWithDefaultConfiguration());
174 173
175 EXPECT_EQ(NULL, apis->GetSchema("")); 174 EXPECT_EQ(NULL, apis->GetSchema(""));
176 EXPECT_EQ(NULL, apis->GetSchema("")); 175 EXPECT_EQ(NULL, apis->GetSchema(""));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 { 227 {
229 std::set<std::string> permissions; 228 std::set<std::string> permissions;
230 permissions.insert("storage"); 229 permissions.insert("storage");
231 permissions.insert("history"); 230 permissions.insert("history");
232 extension = CreateExtensionWithPermissions(permissions); 231 extension = CreateExtensionWithPermissions(permissions);
233 } 232 }
234 233
235 scoped_ptr<ExtensionAPI> extension_api( 234 scoped_ptr<ExtensionAPI> extension_api(
236 ExtensionAPI::CreateWithDefaultConfiguration()); 235 ExtensionAPI::CreateWithDefaultConfiguration());
237 236
238 std::set<std::string> privileged_apis = extension_api->GetAPIsForContext(
239 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
240
241 std::set<std::string> unprivileged_apis = extension_api->GetAPIsForContext(
242 Feature::UNBLESSED_EXTENSION_CONTEXT, extension.get(), GURL());
243
244 std::set<std::string> content_script_apis = extension_api->GetAPIsForContext(
245 Feature::CONTENT_SCRIPT_CONTEXT, extension.get(), GURL());
246
247 // "storage" is completely unprivileged. 237 // "storage" is completely unprivileged.
248 EXPECT_EQ(1u, privileged_apis.count("storage")); 238 EXPECT_TRUE(extension_api->IsAvailable("storage",
249 EXPECT_EQ(1u, unprivileged_apis.count("storage")); 239 extension.get(),
250 EXPECT_EQ(1u, content_script_apis.count("storage")); 240 Feature::BLESSED_EXTENSION_CONTEXT,
241 GURL()).is_available());
242 EXPECT_TRUE(extension_api->IsAvailable("storage",
243 extension.get(),
244 Feature::UNBLESSED_EXTENSION_CONTEXT,
245 GURL()).is_available());
246 EXPECT_TRUE(extension_api->IsAvailable("storage",
247 extension.get(),
248 Feature::CONTENT_SCRIPT_CONTEXT,
249 GURL()).is_available());
251 250
252 // "extension" is partially unprivileged. 251 // "extension" is partially unprivileged.
253 EXPECT_EQ(1u, privileged_apis.count("extension")); 252 EXPECT_TRUE(extension_api->IsAvailable("extension",
254 EXPECT_EQ(1u, unprivileged_apis.count("extension")); 253 extension.get(),
255 EXPECT_EQ(1u, content_script_apis.count("extension")); 254 Feature::BLESSED_EXTENSION_CONTEXT,
255 GURL()).is_available());
256 EXPECT_TRUE(extension_api->IsAvailable("extension",
257 extension.get(),
258 Feature::UNBLESSED_EXTENSION_CONTEXT,
259 GURL()).is_available());
260 EXPECT_TRUE(extension_api->IsAvailable("extension",
261 extension.get(),
262 Feature::CONTENT_SCRIPT_CONTEXT,
263 GURL()).is_available());
256 264
257 // "history" is entirely privileged. 265 // "history" is entirely privileged.
258 EXPECT_EQ(1u, privileged_apis.count("history")); 266 EXPECT_TRUE(extension_api->IsAvailable("history",
259 EXPECT_EQ(0u, unprivileged_apis.count("history")); 267 extension.get(),
260 EXPECT_EQ(0u, content_script_apis.count("history")); 268 Feature::BLESSED_EXTENSION_CONTEXT,
269 GURL()).is_available());
270 EXPECT_FALSE(extension_api->IsAvailable("history",
271 extension.get(),
272 Feature::UNBLESSED_EXTENSION_CONTEXT,
273 GURL()).is_available());
274 EXPECT_FALSE(extension_api->IsAvailable("history",
275 extension.get(),
276 Feature::CONTENT_SCRIPT_CONTEXT,
277 GURL()).is_available());
261 } 278 }
262 279
263 TEST(ExtensionAPI, ExtensionWithDependencies) { 280 TEST(ExtensionAPI, ExtensionWithDependencies) {
264 // Extension with the "ttsEngine" permission but not the "tts" permission; it 281 // Extension with the "ttsEngine" permission but not the "tts" permission; it
265 // must load TTS. 282 // should not automatically get "tts" permission.
266 { 283 {
267 scoped_refptr<Extension> extension = 284 scoped_refptr<Extension> extension =
268 CreateExtensionWithPermission("ttsEngine"); 285 CreateExtensionWithPermission("ttsEngine");
269 scoped_ptr<ExtensionAPI> api( 286 scoped_ptr<ExtensionAPI> api(
270 ExtensionAPI::CreateWithDefaultConfiguration()); 287 ExtensionAPI::CreateWithDefaultConfiguration());
271 std::set<std::string> apis = api->GetAPIsForContext( 288 EXPECT_TRUE(api->IsAvailable("ttsEngine",
272 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); 289 extension.get(),
273 EXPECT_EQ(1u, apis.count("ttsEngine")); 290 Feature::BLESSED_EXTENSION_CONTEXT,
274 EXPECT_EQ(1u, apis.count("tts")); 291 GURL()).is_available());
292 EXPECT_FALSE(api->IsAvailable("tts",
293 extension.get(),
294 Feature::BLESSED_EXTENSION_CONTEXT,
295 GURL()).is_available());
275 } 296 }
276 297
277 // Conversely, extension with the "tts" permission but not the "ttsEngine" 298 // Conversely, extension with the "tts" permission but not the "ttsEngine"
278 // permission shouldn't get the "ttsEngine" permission. 299 // permission shouldn't get the "ttsEngine" permission.
279 { 300 {
280 scoped_refptr<Extension> extension = 301 scoped_refptr<Extension> extension =
281 CreateExtensionWithPermission("tts"); 302 CreateExtensionWithPermission("tts");
282 scoped_ptr<ExtensionAPI> api( 303 scoped_ptr<ExtensionAPI> api(
283 ExtensionAPI::CreateWithDefaultConfiguration()); 304 ExtensionAPI::CreateWithDefaultConfiguration());
284 std::set<std::string> apis = api->GetAPIsForContext( 305 EXPECT_FALSE(api->IsAvailable("ttsEngine",
285 Feature::BLESSED_EXTENSION_CONTEXT, extension.get(), GURL()); 306 extension.get(),
286 EXPECT_EQ(0u, apis.count("ttsEngine")); 307 Feature::BLESSED_EXTENSION_CONTEXT,
287 EXPECT_EQ(1u, apis.count("tts")); 308 GURL()).is_available());
309 EXPECT_TRUE(api->IsAvailable("tts",
310 extension.get(),
311 Feature::BLESSED_EXTENSION_CONTEXT,
312 GURL()).is_available());
288 } 313 }
289 } 314 }
290 315
291 bool MatchesURL( 316 bool MatchesURL(
292 ExtensionAPI* api, const std::string& api_name, const std::string& url) { 317 ExtensionAPI* api, const std::string& api_name, const std::string& url) {
293 std::set<std::string> apis = 318 return api->IsAvailable(
294 api->GetAPIsForContext(Feature::WEB_PAGE_CONTEXT, NULL, GURL(url)); 319 api_name, NULL, Feature::WEB_PAGE_CONTEXT, GURL(url)).is_available();
295 return apis.count(api_name);
296 } 320 }
297 321
298 TEST(ExtensionAPI, URLMatching) { 322 TEST(ExtensionAPI, URLMatching) {
299 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration()); 323 scoped_ptr<ExtensionAPI> api(ExtensionAPI::CreateWithDefaultConfiguration());
300 324
301 // "app" API is available to all URLs that content scripts can be injected. 325 // "app" API is available to all URLs that content scripts can be injected.
302 EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html")); 326 EXPECT_TRUE(MatchesURL(api.get(), "app", "http://example.com/example.html"));
303 EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net")); 327 EXPECT_TRUE(MatchesURL(api.get(), "app", "https://blah.net"));
304 EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html")); 328 EXPECT_TRUE(MatchesURL(api.get(), "app", "file://somefile.html"));
305 329
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 { schema1.get(), true }, 427 { schema1.get(), true },
404 { schema2.get(), false } 428 { schema2.get(), false }
405 }; 429 };
406 430
407 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { 431 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
408 std::string schema_source; 432 std::string schema_source;
409 base::JSONWriter::Write(test_data[i].schema, &schema_source); 433 base::JSONWriter::Write(test_data[i].schema, &schema_source);
410 434
411 ExtensionAPI api; 435 ExtensionAPI api;
412 api.RegisterSchema("test", base::StringPiece(schema_source)); 436 api.RegisterSchema("test", base::StringPiece(schema_source));
413 api.LoadAllSchemas();
414 437
415 Feature* feature = api.GetFeature("test"); 438 Feature* feature = api.GetFeature("test");
416 EXPECT_EQ(test_data[i].expect_success, feature != NULL) << i; 439 EXPECT_EQ(test_data[i].expect_success, feature != NULL) << i;
417 } 440 }
418 } 441 }
419 442
420 static void GetDictionaryFromList(const base::DictionaryValue* schema, 443 static void GetDictionaryFromList(const base::DictionaryValue* schema,
421 const std::string& list_name, 444 const std::string& list_name,
422 const int list_index, 445 const int list_index,
423 const base::DictionaryValue** out) { 446 const base::DictionaryValue** out) {
424 const base::ListValue* list; 447 const base::ListValue* list;
425 EXPECT_TRUE(schema->GetList(list_name, &list)); 448 EXPECT_TRUE(schema->GetList(list_name, &list));
426 EXPECT_TRUE(list->GetDictionary(list_index, out)); 449 EXPECT_TRUE(list->GetDictionary(list_index, out));
427 } 450 }
428 451
429 TEST(ExtensionAPI, TypesHaveNamespace) { 452 TEST(ExtensionAPI, TypesHaveNamespace) {
430 base::FilePath manifest_path; 453 base::FilePath manifest_path;
431 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path); 454 PathService::Get(chrome::DIR_TEST_DATA, &manifest_path);
432 manifest_path = manifest_path.AppendASCII("extensions") 455 manifest_path = manifest_path.AppendASCII("extensions")
433 .AppendASCII("extension_api_unittest") 456 .AppendASCII("extension_api_unittest")
434 .AppendASCII("types_have_namespace.json"); 457 .AppendASCII("types_have_namespace.json");
435 458
436 std::string manifest_str; 459 std::string manifest_str;
437 ASSERT_TRUE(file_util::ReadFileToString(manifest_path, &manifest_str)) 460 ASSERT_TRUE(file_util::ReadFileToString(manifest_path, &manifest_str))
438 << "Failed to load: " << manifest_path.value(); 461 << "Failed to load: " << manifest_path.value();
439 462
440 ExtensionAPI api; 463 ExtensionAPI api;
441 api.RegisterSchema("test.foo", manifest_str); 464 api.RegisterSchema("test.foo", manifest_str);
442 api.LoadAllSchemas();
443 465
444 const base::DictionaryValue* schema = api.GetSchema("test.foo"); 466 const base::DictionaryValue* schema = api.GetSchema("test.foo");
445 467
446 const base::DictionaryValue* dict; 468 const base::DictionaryValue* dict;
447 const base::DictionaryValue* sub_dict; 469 const base::DictionaryValue* sub_dict;
448 std::string type; 470 std::string type;
449 471
450 GetDictionaryFromList(schema, "types", 0, &dict); 472 GetDictionaryFromList(schema, "types", 0, &dict);
451 EXPECT_TRUE(dict->GetString("id", &type)); 473 EXPECT_TRUE(dict->GetString("id", &type));
452 EXPECT_EQ("test.foo.TestType", type); 474 EXPECT_EQ("test.foo.TestType", type);
(...skipping 28 matching lines...) Expand all
481 GetDictionaryFromList(dict, "parameters", 0, &sub_dict); 503 GetDictionaryFromList(dict, "parameters", 0, &sub_dict);
482 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); 504 EXPECT_TRUE(sub_dict->GetString("$ref", &type));
483 EXPECT_EQ("test.foo.TestType", type); 505 EXPECT_EQ("test.foo.TestType", type);
484 GetDictionaryFromList(dict, "parameters", 1, &sub_dict); 506 GetDictionaryFromList(dict, "parameters", 1, &sub_dict);
485 EXPECT_TRUE(sub_dict->GetString("$ref", &type)); 507 EXPECT_TRUE(sub_dict->GetString("$ref", &type));
486 EXPECT_EQ("fully.qualified.Type", type); 508 EXPECT_EQ("fully.qualified.Type", type);
487 } 509 }
488 510
489 } // namespace 511 } // namespace
490 } // namespace extensions 512 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/extension_api.cc ('k') | chrome/common/extensions/api/page_action.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698