Chromium Code Reviews| 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 if (contents->IsType(base::Value::Type::DICTIONARY)) { | |
|
Devlin
2017/01/30 17:24:48
Would this ever not be the case, especially in a t
robertshield
2017/01/31 02:48:14
Done.
| |
| 392 cws_request_json_data_ = | |
| 393 base::DictionaryValue::From(std::move(contents)); | |
| 394 } | |
| 395 } | |
| 396 } | |
| 397 | |
| 398 bool cws_request_received_; | |
| 399 std::unique_ptr<base::DictionaryValue> cws_request_json_data_; | |
| 400 }; | |
| 401 | |
| 402 // Test that an install from a page arrived at via redirects includes the | |
| 403 // redirect information in the webstore request. | |
| 404 IN_PROC_BROWSER_TEST_F(WebstoreInlineInstallerRedirectTest, | |
| 405 IncludesRedirectData) { | |
| 406 // Hand craft a url that will cause the test server to issue redirects. | |
| 407 const std::vector<std::string> redirects{kRedirect1Domain, kRedirect2Domain}; | |
| 408 net::HostPortPair host_port = embedded_test_server()->host_port_pair(); | |
| 409 std::string redirect_chain; | |
| 410 for (const auto& redirect : redirects) { | |
| 411 std::string redirect_url = base::StringPrintf( | |
| 412 "http://%s:%d/server-redirect?", redirect.c_str(), host_port.port()); | |
| 413 redirect_chain += redirect_url; | |
| 414 } | |
| 415 const GURL install_url = | |
| 416 GURL(redirect_chain + | |
| 417 GenerateTestServerUrl(kAppDomain, "install.html").spec()); | |
| 418 | |
| 419 AutoAcceptInstall(); | |
| 420 ui_test_utils::NavigateToURL(browser(), install_url); | |
| 421 RunTest("runTest"); | |
| 422 | |
| 423 EXPECT_TRUE(cws_request_received_); | |
| 424 ASSERT_NE(nullptr, cws_request_json_data_); | |
| 425 | |
| 426 base::ListValue* redirect_list = nullptr; | |
| 427 cws_request_json_data_->GetList("redirect_chain", &redirect_list); | |
| 428 ASSERT_NE(nullptr, redirect_list); | |
| 429 | |
| 430 // Check that the expected domains are in the redirect list. | |
| 431 const std::vector<std::string> expected_redirect_domains{ | |
|
Devlin
2017/01/30 17:24:48
nitty nit: more common in chromium to use = {}, so
robertshield
2017/01/31 02:48:14
Done.
| |
| 432 kRedirect1Domain, kRedirect2Domain, kAppDomain}; | |
| 433 ASSERT_EQ(expected_redirect_domains.size(), redirect_list->GetSize()); | |
| 434 int i = 0; | |
| 435 for (const auto& value : *redirect_list) { | |
| 436 std::string value_string; | |
| 437 ASSERT_TRUE(value->GetAsString(&value_string)); | |
| 438 GURL redirect_url(value_string); | |
| 439 EXPECT_EQ(expected_redirect_domains[i++], redirect_url.host()); | |
| 440 } | |
| 441 } | |
| 442 | |
| 366 class WebstoreInlineInstallerListenerTest : public WebstoreInlineInstallerTest { | 443 class WebstoreInlineInstallerListenerTest : public WebstoreInlineInstallerTest { |
| 367 public: | 444 public: |
| 368 WebstoreInlineInstallerListenerTest() {} | 445 WebstoreInlineInstallerListenerTest() {} |
| 369 ~WebstoreInlineInstallerListenerTest() override {} | 446 ~WebstoreInlineInstallerListenerTest() override {} |
| 370 | 447 |
| 371 protected: | 448 protected: |
| 372 void RunTest(const std::string& file_name) { | 449 void RunTest(const std::string& file_name) { |
| 373 AutoAcceptInstall(); | 450 AutoAcceptInstall(); |
| 374 ui_test_utils::NavigateToURL(browser(), | 451 ui_test_utils::NavigateToURL(browser(), |
| 375 GenerateTestServerUrl(kAppDomain, file_name)); | 452 GenerateTestServerUrl(kAppDomain, file_name)); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 406 WindowOpenDisposition::NEW_FOREGROUND_TAB, | 483 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 407 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 484 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 408 DCHECK_NE(old_tab_index, browser()->tab_strip_model()->active_index()); | 485 DCHECK_NE(old_tab_index, browser()->tab_strip_model()->active_index()); |
| 409 browser()->tab_strip_model()->CloseWebContentsAt(old_tab_index, | 486 browser()->tab_strip_model()->CloseWebContentsAt(old_tab_index, |
| 410 TabStripModel::CLOSE_NONE); | 487 TabStripModel::CLOSE_NONE); |
| 411 WebstoreInstallerTest::RunTest("runTest"); | 488 WebstoreInstallerTest::RunTest("runTest"); |
| 412 EXPECT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId)); | 489 EXPECT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId)); |
| 413 } | 490 } |
| 414 | 491 |
| 415 } // namespace extensions | 492 } // namespace extensions |
| OLD | NEW |