| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/extensions/api/developer_private/developer_private_api.
h" |
| 6 |
| 7 #include <memory> |
| 5 #include <utility> | 8 #include <utility> |
| 6 | 9 |
| 7 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 8 #include "base/macros.h" | 11 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/api/developer_private/developer_private_api.
h" | |
| 12 #include "chrome/browser/extensions/error_console/error_console.h" | 14 #include "chrome/browser/extensions/error_console/error_console.h" |
| 13 #include "chrome/browser/extensions/extension_function_test_utils.h" | 15 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/extension_service_test_base.h" | 17 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 16 #include "chrome/browser/extensions/extension_util.h" | 18 #include "chrome/browser/extensions/extension_util.h" |
| 17 #include "chrome/browser/extensions/test_extension_dir.h" | 19 #include "chrome/browser/extensions/test_extension_dir.h" |
| 18 #include "chrome/browser/extensions/test_extension_system.h" | 20 #include "chrome/browser/extensions/test_extension_system.h" |
| 19 #include "chrome/browser/extensions/unpacked_installer.h" | 21 #include "chrome/browser/extensions/unpacked_installer.h" |
| 20 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/common/extensions/api/developer_private.h" | 23 #include "chrome/common/extensions/api/developer_private.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 34 #include "extensions/common/extension_set.h" | 36 #include "extensions/common/extension_set.h" |
| 35 #include "extensions/common/feature_switch.h" | 37 #include "extensions/common/feature_switch.h" |
| 36 #include "extensions/common/manifest_constants.h" | 38 #include "extensions/common/manifest_constants.h" |
| 37 #include "extensions/common/test_util.h" | 39 #include "extensions/common/test_util.h" |
| 38 #include "extensions/common/value_builder.h" | 40 #include "extensions/common/value_builder.h" |
| 39 | 41 |
| 40 namespace extensions { | 42 namespace extensions { |
| 41 | 43 |
| 42 namespace { | 44 namespace { |
| 43 | 45 |
| 44 scoped_ptr<KeyedService> BuildAPI(content::BrowserContext* context) { | 46 std::unique_ptr<KeyedService> BuildAPI(content::BrowserContext* context) { |
| 45 return make_scoped_ptr(new DeveloperPrivateAPI(context)); | 47 return base::WrapUnique(new DeveloperPrivateAPI(context)); |
| 46 } | 48 } |
| 47 | 49 |
| 48 scoped_ptr<KeyedService> BuildEventRouter(content::BrowserContext* profile) { | 50 std::unique_ptr<KeyedService> BuildEventRouter( |
| 49 return make_scoped_ptr( | 51 content::BrowserContext* profile) { |
| 52 return base::WrapUnique( |
| 50 new EventRouter(profile, ExtensionPrefs::Get(profile))); | 53 new EventRouter(profile, ExtensionPrefs::Get(profile))); |
| 51 } | 54 } |
| 52 | 55 |
| 53 } // namespace | 56 } // namespace |
| 54 | 57 |
| 55 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { | 58 class DeveloperPrivateApiUnitTest : public ExtensionServiceTestBase { |
| 56 protected: | 59 protected: |
| 57 DeveloperPrivateApiUnitTest() {} | 60 DeveloperPrivateApiUnitTest() {} |
| 58 ~DeveloperPrivateApiUnitTest() override {} | 61 ~DeveloperPrivateApiUnitTest() override {} |
| 59 | 62 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 82 int expected_flags); | 85 int expected_flags); |
| 83 | 86 |
| 84 Browser* browser() { return browser_.get(); } | 87 Browser* browser() { return browser_.get(); } |
| 85 | 88 |
| 86 private: | 89 private: |
| 87 // ExtensionServiceTestBase: | 90 // ExtensionServiceTestBase: |
| 88 void SetUp() override; | 91 void SetUp() override; |
| 89 void TearDown() override; | 92 void TearDown() override; |
| 90 | 93 |
| 91 // The browser (and accompanying window). | 94 // The browser (and accompanying window). |
| 92 scoped_ptr<TestBrowserWindow> browser_window_; | 95 std::unique_ptr<TestBrowserWindow> browser_window_; |
| 93 scoped_ptr<Browser> browser_; | 96 std::unique_ptr<Browser> browser_; |
| 94 | 97 |
| 95 ScopedVector<TestExtensionDir> test_extension_dirs_; | 98 ScopedVector<TestExtensionDir> test_extension_dirs_; |
| 96 | 99 |
| 97 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); | 100 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); |
| 98 }; | 101 }; |
| 99 | 102 |
| 100 bool DeveloperPrivateApiUnitTest::RunFunction( | 103 bool DeveloperPrivateApiUnitTest::RunFunction( |
| 101 const scoped_refptr<UIThreadExtensionFunction>& function, | 104 const scoped_refptr<UIThreadExtensionFunction>& function, |
| 102 const base::ListValue& args) { | 105 const base::ListValue& args) { |
| 103 return extension_function_test_utils::RunFunction( | 106 return extension_function_test_utils::RunFunction( |
| 104 function.get(), | 107 function.get(), base::WrapUnique(args.DeepCopy()), browser(), |
| 105 make_scoped_ptr(args.DeepCopy()), | |
| 106 browser(), | |
| 107 extension_function_test_utils::NONE); | 108 extension_function_test_utils::NONE); |
| 108 } | 109 } |
| 109 | 110 |
| 110 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() { | 111 const Extension* DeveloperPrivateApiUnitTest::LoadUnpackedExtension() { |
| 111 const char kManifest[] = | 112 const char kManifest[] = |
| 112 "{" | 113 "{" |
| 113 " \"name\": \"foo\"," | 114 " \"name\": \"foo\"," |
| 114 " \"version\": \"1.0\"," | 115 " \"version\": \"1.0\"," |
| 115 " \"manifest_version\": 2," | 116 " \"manifest_version\": 2," |
| 116 " \"permissions\": [\"*://*/*\"]" | 117 " \"permissions\": [\"*://*/*\"]" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 int expected_flags) { | 196 int expected_flags) { |
| 196 scoped_refptr<UIThreadExtensionFunction> function( | 197 scoped_refptr<UIThreadExtensionFunction> function( |
| 197 new api::DeveloperPrivatePackDirectoryFunction()); | 198 new api::DeveloperPrivatePackDirectoryFunction()); |
| 198 if (!RunFunction(function, args)) | 199 if (!RunFunction(function, args)) |
| 199 return testing::AssertionFailure() << "Could not run function."; | 200 return testing::AssertionFailure() << "Could not run function."; |
| 200 | 201 |
| 201 // Extract the result. We don't have to test this here, since it's verified as | 202 // Extract the result. We don't have to test this here, since it's verified as |
| 202 // part of the general extension api system. | 203 // part of the general extension api system. |
| 203 const base::Value* response_value = nullptr; | 204 const base::Value* response_value = nullptr; |
| 204 CHECK(function->GetResultList()->Get(0u, &response_value)); | 205 CHECK(function->GetResultList()->Get(0u, &response_value)); |
| 205 scoped_ptr<api::developer_private::PackDirectoryResponse> response = | 206 std::unique_ptr<api::developer_private::PackDirectoryResponse> response = |
| 206 api::developer_private::PackDirectoryResponse::FromValue(*response_value); | 207 api::developer_private::PackDirectoryResponse::FromValue(*response_value); |
| 207 CHECK(response); | 208 CHECK(response); |
| 208 | 209 |
| 209 if (response->status != expected_status) { | 210 if (response->status != expected_status) { |
| 210 return testing::AssertionFailure() << "Expected status: " << | 211 return testing::AssertionFailure() << "Expected status: " << |
| 211 expected_status << ", found status: " << response->status << | 212 expected_status << ", found status: " << response->status << |
| 212 ", message: " << response->message; | 213 ", message: " << response->message; |
| 213 } | 214 } |
| 214 | 215 |
| 215 if (response->override_flags != expected_flags) { | 216 if (response->override_flags != expected_flags) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 path, | 398 path, |
| 398 registry()->enabled_extensions().GetByID(*id_difference.begin())->path()); | 399 registry()->enabled_extensions().GetByID(*id_difference.begin())->path()); |
| 399 | 400 |
| 400 path = data_dir().AppendASCII("empty_manifest"); | 401 path = data_dir().AppendASCII("empty_manifest"); |
| 401 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path); | 402 api::EntryPicker::SkipPickerAndAlwaysSelectPathForTest(&path); |
| 402 | 403 |
| 403 // Try loading a bad extension (it should fail, and we should get an error). | 404 // Try loading a bad extension (it should fail, and we should get an error). |
| 404 function = new api::DeveloperPrivateLoadUnpackedFunction(); | 405 function = new api::DeveloperPrivateLoadUnpackedFunction(); |
| 405 function->SetRenderFrameHost(web_contents->GetMainFrame()); | 406 function->SetRenderFrameHost(web_contents->GetMainFrame()); |
| 406 base::ListValue unpacked_args; | 407 base::ListValue unpacked_args; |
| 407 scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue()); | 408 std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue()); |
| 408 options->SetBoolean("failQuietly", true); | 409 options->SetBoolean("failQuietly", true); |
| 409 unpacked_args.Append(options.release()); | 410 unpacked_args.Append(options.release()); |
| 410 current_ids = registry()->enabled_extensions().GetIDs(); | 411 current_ids = registry()->enabled_extensions().GetIDs(); |
| 411 EXPECT_FALSE(RunFunction(function, unpacked_args)); | 412 EXPECT_FALSE(RunFunction(function, unpacked_args)); |
| 412 EXPECT_EQ(manifest_errors::kManifestUnreadable, function->GetError()); | 413 EXPECT_EQ(manifest_errors::kManifestUnreadable, function->GetError()); |
| 413 // We should have no new extensions installed. | 414 // We should have no new extensions installed. |
| 414 EXPECT_EQ(0u, base::STLSetDifference<ExtensionIdSet>( | 415 EXPECT_EQ(0u, base::STLSetDifference<ExtensionIdSet>( |
| 415 registry()->enabled_extensions().GetIDs(), | 416 registry()->enabled_extensions().GetIDs(), |
| 416 current_ids).size()); | 417 current_ids).size()); |
| 417 } | 418 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 433 properties.manifest_key.reset(new std::string("name")); | 434 properties.manifest_key.reset(new std::string("name")); |
| 434 | 435 |
| 435 scoped_refptr<UIThreadExtensionFunction> function( | 436 scoped_refptr<UIThreadExtensionFunction> function( |
| 436 new api::DeveloperPrivateRequestFileSourceFunction()); | 437 new api::DeveloperPrivateRequestFileSourceFunction()); |
| 437 base::ListValue file_source_args; | 438 base::ListValue file_source_args; |
| 438 file_source_args.Append(properties.ToValue().release()); | 439 file_source_args.Append(properties.ToValue().release()); |
| 439 EXPECT_TRUE(RunFunction(function, file_source_args)) << function->GetError(); | 440 EXPECT_TRUE(RunFunction(function, file_source_args)) << function->GetError(); |
| 440 | 441 |
| 441 const base::Value* response_value = nullptr; | 442 const base::Value* response_value = nullptr; |
| 442 ASSERT_TRUE(function->GetResultList()->Get(0u, &response_value)); | 443 ASSERT_TRUE(function->GetResultList()->Get(0u, &response_value)); |
| 443 scoped_ptr<api::developer_private::RequestFileSourceResponse> response = | 444 std::unique_ptr<api::developer_private::RequestFileSourceResponse> response = |
| 444 api::developer_private::RequestFileSourceResponse::FromValue( | 445 api::developer_private::RequestFileSourceResponse::FromValue( |
| 445 *response_value); | 446 *response_value); |
| 446 EXPECT_FALSE(response->before_highlight.empty()); | 447 EXPECT_FALSE(response->before_highlight.empty()); |
| 447 EXPECT_EQ("\"name\": \"foo\"", response->highlight); | 448 EXPECT_EQ("\"name\": \"foo\"", response->highlight); |
| 448 EXPECT_FALSE(response->after_highlight.empty()); | 449 EXPECT_FALSE(response->after_highlight.empty()); |
| 449 EXPECT_EQ("foo: manifest.json", response->title); | 450 EXPECT_EQ("foo: manifest.json", response->title); |
| 450 EXPECT_EQ(kErrorMessage, response->message); | 451 EXPECT_EQ(kErrorMessage, response->message); |
| 451 } | 452 } |
| 452 | 453 |
| 453 // Test developerPrivate.getExtensionsInfo. | 454 // Test developerPrivate.getExtensionsInfo. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 464 scoped_refptr<UIThreadExtensionFunction> function( | 465 scoped_refptr<UIThreadExtensionFunction> function( |
| 465 new api::DeveloperPrivateGetExtensionsInfoFunction()); | 466 new api::DeveloperPrivateGetExtensionsInfoFunction()); |
| 466 EXPECT_TRUE(RunFunction(function, base::ListValue())) << function->GetError(); | 467 EXPECT_TRUE(RunFunction(function, base::ListValue())) << function->GetError(); |
| 467 const base::ListValue* results = function->GetResultList(); | 468 const base::ListValue* results = function->GetResultList(); |
| 468 ASSERT_EQ(1u, results->GetSize()); | 469 ASSERT_EQ(1u, results->GetSize()); |
| 469 const base::ListValue* list = nullptr; | 470 const base::ListValue* list = nullptr; |
| 470 ASSERT_TRUE(results->GetList(0u, &list)); | 471 ASSERT_TRUE(results->GetList(0u, &list)); |
| 471 ASSERT_EQ(1u, list->GetSize()); | 472 ASSERT_EQ(1u, list->GetSize()); |
| 472 const base::Value* value = nullptr; | 473 const base::Value* value = nullptr; |
| 473 ASSERT_TRUE(list->Get(0u, &value)); | 474 ASSERT_TRUE(list->Get(0u, &value)); |
| 474 scoped_ptr<api::developer_private::ExtensionInfo> info = | 475 std::unique_ptr<api::developer_private::ExtensionInfo> info = |
| 475 api::developer_private::ExtensionInfo::FromValue(*value); | 476 api::developer_private::ExtensionInfo::FromValue(*value); |
| 476 ASSERT_TRUE(info); | 477 ASSERT_TRUE(info); |
| 477 | 478 |
| 478 // As a sanity check, also run the GetItemsInfo and make sure it returns a | 479 // As a sanity check, also run the GetItemsInfo and make sure it returns a |
| 479 // sane value. | 480 // sane value. |
| 480 function = new api::DeveloperPrivateGetItemsInfoFunction(); | 481 function = new api::DeveloperPrivateGetItemsInfoFunction(); |
| 481 base::ListValue args; | 482 base::ListValue args; |
| 482 args.AppendBoolean(false); | 483 args.AppendBoolean(false); |
| 483 args.AppendBoolean(false); | 484 args.AppendBoolean(false); |
| 484 EXPECT_TRUE(RunFunction(function, args)) << function->GetError(); | 485 EXPECT_TRUE(RunFunction(function, args)) << function->GetError(); |
| 485 results = function->GetResultList(); | 486 results = function->GetResultList(); |
| 486 ASSERT_EQ(1u, results->GetSize()); | 487 ASSERT_EQ(1u, results->GetSize()); |
| 487 ASSERT_TRUE(results->GetList(0u, &list)); | 488 ASSERT_TRUE(results->GetList(0u, &list)); |
| 488 ASSERT_EQ(1u, list->GetSize()); | 489 ASSERT_EQ(1u, list->GetSize()); |
| 489 ASSERT_TRUE(list->Get(0u, &value)); | 490 ASSERT_TRUE(list->Get(0u, &value)); |
| 490 scoped_ptr<api::developer_private::ItemInfo> item_info = | 491 std::unique_ptr<api::developer_private::ItemInfo> item_info = |
| 491 api::developer_private::ItemInfo::FromValue(*value); | 492 api::developer_private::ItemInfo::FromValue(*value); |
| 492 ASSERT_TRUE(item_info); | 493 ASSERT_TRUE(item_info); |
| 493 } | 494 } |
| 494 | 495 |
| 495 // Test developerPrivate.deleteExtensionErrors. | 496 // Test developerPrivate.deleteExtensionErrors. |
| 496 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDeleteExtensionErrors) { | 497 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDeleteExtensionErrors) { |
| 497 FeatureSwitch::ScopedOverride error_console_override( | 498 FeatureSwitch::ScopedOverride error_console_override( |
| 498 FeatureSwitch::error_console(), true); | 499 FeatureSwitch::error_console(), true); |
| 499 profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); | 500 profile()->GetPrefs()->SetBoolean(prefs::kExtensionsUIDeveloperMode, true); |
| 500 const Extension* extension = LoadSimpleExtension(); | 501 const Extension* extension = LoadSimpleExtension(); |
| 501 | 502 |
| 502 // Report some errors. | 503 // Report some errors. |
| 503 ErrorConsole* error_console = ErrorConsole::Get(profile()); | 504 ErrorConsole* error_console = ErrorConsole::Get(profile()); |
| 504 error_console->SetReportingAllForExtension(extension->id(), true); | 505 error_console->SetReportingAllForExtension(extension->id(), true); |
| 505 error_console->ReportError( | 506 error_console->ReportError( |
| 506 error_test_util::CreateNewRuntimeError(extension->id(), "foo")); | 507 error_test_util::CreateNewRuntimeError(extension->id(), "foo")); |
| 507 error_console->ReportError( | 508 error_console->ReportError( |
| 508 error_test_util::CreateNewRuntimeError(extension->id(), "bar")); | 509 error_test_util::CreateNewRuntimeError(extension->id(), "bar")); |
| 509 error_console->ReportError( | 510 error_console->ReportError( |
| 510 error_test_util::CreateNewManifestError(extension->id(), "baz")); | 511 error_test_util::CreateNewManifestError(extension->id(), "baz")); |
| 511 EXPECT_EQ(3u, error_console->GetErrorsForExtension(extension->id()).size()); | 512 EXPECT_EQ(3u, error_console->GetErrorsForExtension(extension->id()).size()); |
| 512 | 513 |
| 513 // Start by removing all errors for the extension of a given type (manifest). | 514 // Start by removing all errors for the extension of a given type (manifest). |
| 514 std::string type_string = api::developer_private::ToString( | 515 std::string type_string = api::developer_private::ToString( |
| 515 api::developer_private::ERROR_TYPE_MANIFEST); | 516 api::developer_private::ERROR_TYPE_MANIFEST); |
| 516 scoped_ptr<base::ListValue> args = | 517 std::unique_ptr<base::ListValue> args = |
| 517 ListBuilder() | 518 ListBuilder() |
| 518 .Append(DictionaryBuilder() | 519 .Append(DictionaryBuilder() |
| 519 .Set("extensionId", extension->id()) | 520 .Set("extensionId", extension->id()) |
| 520 .Set("type", type_string) | 521 .Set("type", type_string) |
| 521 .Build()) | 522 .Build()) |
| 522 .Build(); | 523 .Build(); |
| 523 scoped_refptr<UIThreadExtensionFunction> function = | 524 scoped_refptr<UIThreadExtensionFunction> function = |
| 524 new api::DeveloperPrivateDeleteExtensionErrorsFunction(); | 525 new api::DeveloperPrivateDeleteExtensionErrorsFunction(); |
| 525 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); | 526 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); |
| 526 // Two errors should remain. | 527 // Two errors should remain. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 548 .Append( | 549 .Append( |
| 549 DictionaryBuilder().Set("extensionId", extension->id()).Build()) | 550 DictionaryBuilder().Set("extensionId", extension->id()).Build()) |
| 550 .Build(); | 551 .Build(); |
| 551 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction(); | 552 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction(); |
| 552 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); | 553 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); |
| 553 // No more errors! | 554 // No more errors! |
| 554 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty()); | 555 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty()); |
| 555 } | 556 } |
| 556 | 557 |
| 557 } // namespace extensions | 558 } // namespace extensions |
| OLD | NEW |