| 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 "chrome/browser/extensions/webstore_inline_installer.h" | 5 #include "chrome/browser/extensions/webstore_inline_installer.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 11 #include "chrome/browser/extensions/extension_install_prompt.h" | 13 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/tab_helper.h" | 15 #include "chrome/browser/extensions/tab_helper.h" |
| 14 #include "chrome/browser/extensions/webstore_inline_installer_factory.h" | 16 #include "chrome/browser/extensions/webstore_inline_installer_factory.h" |
| 15 #include "chrome/browser/extensions/webstore_installer_test.h" | 17 #include "chrome/browser/extensions/webstore_installer_test.h" |
| 16 #include "chrome/browser/extensions/webstore_standalone_installer.h" | 18 #include "chrome/browser/extensions/webstore_standalone_installer.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_finder.h" | 21 #include "chrome/browser/ui/browser_finder.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/test/base/ui_test_utils.h" | 23 #include "chrome/test/base/ui_test_utils.h" |
| 22 #include "components/content_settings/core/browser/host_content_settings_map.h" | 24 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 23 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/test/browser_test_utils.h" | 26 #include "content/public/test/browser_test_utils.h" |
| 25 #include "extensions/browser/extension_registry.h" | 27 #include "extensions/browser/extension_registry.h" |
| 26 #include "extensions/browser/extension_system.h" | 28 #include "extensions/browser/extension_system.h" |
| 27 #include "extensions/common/permissions/permission_set.h" | 29 #include "extensions/common/permissions/permission_set.h" |
| 30 #include "net/dns/mock_host_resolver.h" |
| 31 #include "net/test/embedded_test_server/http_request.h" |
| 28 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 29 | 33 |
| 30 using content::WebContents; | 34 using content::WebContents; |
| 31 | 35 |
| 32 namespace extensions { | 36 namespace extensions { |
| 33 | 37 |
| 34 namespace { | 38 namespace { |
| 35 | 39 |
| 36 const char kWebstoreDomain[] = "cws.com"; | 40 const char kWebstoreDomain[] = "cws.com"; |
| 37 const char kAppDomain[] = "app.com"; | 41 const char kAppDomain[] = "app.com"; |
| 38 const char kNonAppDomain[] = "nonapp.com"; | 42 const char kNonAppDomain[] = "nonapp.com"; |
| 39 const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje"; | 43 const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje"; |
| 40 const char kTestDataPath[] = "extensions/api_test/webstore_inline_install"; | 44 const char kTestDataPath[] = "extensions/api_test/webstore_inline_install"; |
| 41 const char kCrxFilename[] = "extension.crx"; | 45 const char kCrxFilename[] = "extension.crx"; |
| 42 | 46 |
| 47 const char kRedirect1Domain[] = "redirect1.com"; |
| 48 const char kRedirect2Domain[] = "redirect2.com"; |
| 49 |
| 43 // A struct for letting us store the actual parameters that were passed to | 50 // A struct for letting us store the actual parameters that were passed to |
| 44 // the install callback. | 51 // the install callback. |
| 45 struct InstallResult { | 52 struct InstallResult { |
| 46 bool success = false; | 53 bool success = false; |
| 47 std::string error; | 54 std::string error; |
| 48 webstore_install::Result result = webstore_install::RESULT_LAST; | 55 webstore_install::Result result = webstore_install::RESULT_LAST; |
| 49 }; | 56 }; |
| 50 | 57 |
| 51 } // namespace | 58 } // namespace |
| 52 | 59 |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 } | 363 } |
| 357 | 364 |
| 358 // Test calling chrome.webstore.install() twice without waiting for the first to | 365 // Test calling chrome.webstore.install() twice without waiting for the first to |
| 359 // finish. | 366 // finish. |
| 360 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerTest, DoubleInlineInstallTest) { | 367 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerTest, DoubleInlineInstallTest) { |
| 361 ui_test_utils::NavigateToURL( | 368 ui_test_utils::NavigateToURL( |
| 362 browser(), GenerateTestServerUrl(kAppDomain, "double_install.html")); | 369 browser(), GenerateTestServerUrl(kAppDomain, "double_install.html")); |
| 363 RunTest("runTest"); | 370 RunTest("runTest"); |
| 364 } | 371 } |
| 365 | 372 |
| 373 class WebstoreInlineInstallerRedirectTest : public WebstoreInlineInstallerTest { |
| 374 public: |
| 375 WebstoreInlineInstallerRedirectTest() : cws_request_received_(false) {} |
| 376 ~WebstoreInlineInstallerRedirectTest() override {} |
| 377 |
| 378 void SetUpInProcessBrowserTestFixture() override { |
| 379 WebstoreInstallerTest::SetUpInProcessBrowserTestFixture(); |
| 380 host_resolver()->AddRule(kRedirect1Domain, "127.0.0.1"); |
| 381 host_resolver()->AddRule(kRedirect2Domain, "127.0.0.1"); |
| 382 } |
| 383 |
| 384 void ProcessServerRequest( |
| 385 const net::test_server::HttpRequest& request) override { |
| 386 if (request.content.find("redirect_chain") != std::string::npos) { |
| 387 cws_request_received_ = true; |
| 388 |
| 389 std::unique_ptr<base::Value> contents = |
| 390 base::JSONReader::Read(request.content); |
| 391 ASSERT_EQ(base::Value::Type::DICTIONARY, contents->GetType()); |
| 392 cws_request_json_data_ = base::DictionaryValue::From(std::move(contents)); |
| 393 } |
| 394 } |
| 395 |
| 396 bool cws_request_received_; |
| 397 std::unique_ptr<base::DictionaryValue> cws_request_json_data_; |
| 398 }; |
| 399 |
| 400 // Test that an install from a page arrived at via redirects includes the |
| 401 // redirect information in the webstore request. |
| 402 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerRedirectTest, |
| 403 IncludesRedirectData) { |
| 404 // Hand craft a url that will cause the test server to issue redirects. |
| 405 const std::vector<std::string> redirects = {kRedirect1Domain, |
| 406 kRedirect2Domain}; |
| 407 net::HostPortPair host_port = embedded_test_server()->host_port_pair(); |
| 408 std::string redirect_chain; |
| 409 for (const auto& redirect : redirects) { |
| 410 std::string redirect_url = base::StringPrintf( |
| 411 "http://%s:%d/server-redirect?", redirect.c_str(), host_port.port()); |
| 412 redirect_chain += redirect_url; |
| 413 } |
| 414 const GURL install_url = |
| 415 GURL(redirect_chain + |
| 416 GenerateTestServerUrl(kAppDomain, "install.html").spec()); |
| 417 |
| 418 AutoAcceptInstall(); |
| 419 ui_test_utils::NavigateToURL(browser(), install_url); |
| 420 RunTest("runTest"); |
| 421 |
| 422 EXPECT_TRUE(cws_request_received_); |
| 423 ASSERT_NE(nullptr, cws_request_json_data_); |
| 424 |
| 425 base::ListValue* redirect_list = nullptr; |
| 426 cws_request_json_data_->GetList("redirect_chain", &redirect_list); |
| 427 ASSERT_NE(nullptr, redirect_list); |
| 428 |
| 429 // Check that the expected domains are in the redirect list. |
| 430 const std::vector<std::string> expected_redirect_domains = { |
| 431 kRedirect1Domain, kRedirect2Domain, kAppDomain}; |
| 432 ASSERT_EQ(expected_redirect_domains.size(), redirect_list->GetSize()); |
| 433 int i = 0; |
| 434 for (const auto& value : *redirect_list) { |
| 435 std::string value_string; |
| 436 ASSERT_TRUE(value->GetAsString(&value_string)); |
| 437 GURL redirect_url(value_string); |
| 438 EXPECT_EQ(expected_redirect_domains[i++], redirect_url.host()); |
| 439 } |
| 440 } |
| 441 |
| 366 class WebstoreInlineInstallerListenerTest : public WebstoreInlineInstallerTest { | 442 class WebstoreInlineInstallerListenerTest : public WebstoreInlineInstallerTest { |
| 367 public: | 443 public: |
| 368 WebstoreInlineInstallerListenerTest() {} | 444 WebstoreInlineInstallerListenerTest() {} |
| 369 ~WebstoreInlineInstallerListenerTest() override {} | 445 ~WebstoreInlineInstallerListenerTest() override {} |
| 370 | 446 |
| 371 protected: | 447 protected: |
| 372 void RunTest(const std::string& file_name) { | 448 void RunTest(const std::string& file_name) { |
| 373 AutoAcceptInstall(); | 449 AutoAcceptInstall(); |
| 374 ui_test_utils::NavigateToURL(browser(), | 450 ui_test_utils::NavigateToURL(browser(), |
| 375 GenerateTestServerUrl(kAppDomain, file_name)); | 451 GenerateTestServerUrl(kAppDomain, file_name)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 406 WindowOpenDisposition::NEW_FOREGROUND_TAB, | 482 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 407 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 483 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 408 DCHECK_NE(old_tab_index, browser()->tab_strip_model()->active_index()); | 484 DCHECK_NE(old_tab_index, browser()->tab_strip_model()->active_index()); |
| 409 browser()->tab_strip_model()->CloseWebContentsAt(old_tab_index, | 485 browser()->tab_strip_model()->CloseWebContentsAt(old_tab_index, |
| 410 TabStripModel::CLOSE_NONE); | 486 TabStripModel::CLOSE_NONE); |
| 411 WebstoreInstallerTest::RunTest("runTest"); | 487 WebstoreInstallerTest::RunTest("runTest"); |
| 412 EXPECT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId)); | 488 EXPECT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId)); |
| 413 } | 489 } |
| 414 | 490 |
| 415 } // namespace extensions | 491 } // namespace extensions |
| OLD | NEW |