morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/command_line.h" |
Min Qin | eb96190 | 2019-03-16 08:08:22 | [diff] [blame] | 6 | #include "build/build_config.h" |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 7 | #include "content/browser/child_process_launcher.h" |
| 8 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 9 | #include "content/public/browser/navigation_entry.h" |
Lukasz Anforowicz | 9e0ce4e | 2017-09-28 19:09:15 | [diff] [blame] | 10 | #include "content/public/browser/render_frame_host.h" |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 11 | #include "content/public/browser/web_contents.h" |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 12 | #include "content/public/common/content_switches.h" |
| 13 | #include "content/public/test/content_browser_test.h" |
| 14 | #include "content/public/test/content_browser_test_utils.h" |
Lukasz Anforowicz | b36b4e2 | 2019-06-11 18:37:03 | [diff] [blame] | 15 | #include "content/public/test/no_renderer_crashes_assertion.h" |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 16 | #include "content/public/test/test_navigation_observer.h" |
| 17 | #include "content/shell/browser/shell.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class MockChildProcessLauncherClient |
| 22 | : public content::ChildProcessLauncher::Client { |
| 23 | public: |
| 24 | MockChildProcessLauncherClient() |
| 25 | : client_(nullptr), simulate_failure_(false) {} |
| 26 | |
| 27 | void OnProcessLaunched() override { |
| 28 | if (simulate_failure_) |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 29 | client_->OnProcessLaunchFailed(content::LAUNCH_RESULT_FAILURE); |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 30 | else |
| 31 | client_->OnProcessLaunched(); |
Nico Weber | fefaa41 | 2019-02-11 16:10:11 | [diff] [blame] | 32 | } |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 33 | |
| 34 | void OnProcessLaunchFailed(int error_code) override { |
| 35 | client_->OnProcessLaunchFailed(error_code); |
Nico Weber | fefaa41 | 2019-02-11 16:10:11 | [diff] [blame] | 36 | } |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 37 | |
Min Qin | eb96190 | 2019-03-16 08:08:22 | [diff] [blame] | 38 | #if defined(OS_ANDROID) |
| 39 | bool CanUseWarmUpConnection() override { return true; } |
| 40 | #endif |
| 41 | |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 42 | content::ChildProcessLauncher::Client* client_; |
| 43 | bool simulate_failure_; |
| 44 | }; |
| 45 | |
Lukasz Anforowicz | 9e0ce4e | 2017-09-28 19:09:15 | [diff] [blame] | 46 | } // namespace |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 47 | |
| 48 | namespace content { |
| 49 | |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 50 | class ChildProcessLauncherBrowserTest : public ContentBrowserTest {}; |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 51 | |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 52 | IN_PROC_BROWSER_TEST_F(ChildProcessLauncherBrowserTest, ChildSpawnFail) { |
| 53 | GURL url("about:blank"); |
| 54 | Shell* window = shell(); |
| 55 | MockChildProcessLauncherClient* client(nullptr); |
| 56 | |
| 57 | // Navigate once and simulate a process failing to spawn. |
| 58 | TestNavigationObserver nav_observer1(window->web_contents(), 1); |
| 59 | client = new MockChildProcessLauncherClient; |
| 60 | window->LoadURL(url); |
| 61 | client->client_ = static_cast<RenderProcessHostImpl*>( |
Lukasz Anforowicz | 9e0ce4e | 2017-09-28 19:09:15 | [diff] [blame] | 62 | window->web_contents()->GetMainFrame()->GetProcess()) |
| 63 | ->child_process_launcher_->ReplaceClientForTest(client); |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 64 | client->simulate_failure_ = true; |
Lukasz Anforowicz | efbb6493 | 2019-06-19 15:43:51 | [diff] [blame] | 65 | { |
| 66 | ScopedAllowRendererCrashes allow_renderer_crashes(shell()); |
| 67 | nav_observer1.Wait(); |
| 68 | } |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 69 | delete client; |
| 70 | NavigationEntry* last_entry = |
| 71 | shell()->web_contents()->GetController().GetLastCommittedEntry(); |
| 72 | // Make sure we didn't navigate. |
| 73 | CHECK(!last_entry); |
| 74 | |
| 75 | // Navigate again and let the process spawn correctly. |
| 76 | TestNavigationObserver nav_observer2(window->web_contents(), 1); |
| 77 | window->LoadURL(url); |
| 78 | nav_observer2.Wait(); |
| 79 | last_entry = shell()->web_contents()->GetController().GetLastCommittedEntry(); |
| 80 | // Make sure that we navigated to the proper URL. |
| 81 | CHECK(last_entry && last_entry->GetPageType() == PAGE_TYPE_NORMAL); |
| 82 | CHECK(shell()->web_contents()->GetLastCommittedURL() == url); |
| 83 | |
| 84 | // Navigate again, using the same renderer. |
| 85 | url = GURL("data:text/html,dataurl"); |
| 86 | TestNavigationObserver nav_observer3(window->web_contents(), 1); |
| 87 | window->LoadURL(url); |
| 88 | nav_observer3.Wait(); |
| 89 | last_entry = shell()->web_contents()->GetController().GetLastCommittedEntry(); |
| 90 | // Make sure that we navigated to the proper URL. |
| 91 | CHECK(last_entry && last_entry->GetPageType() == PAGE_TYPE_NORMAL); |
| 92 | CHECK(shell()->web_contents()->GetLastCommittedURL() == url); |
| 93 | } |
| 94 | |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 95 | } // namespace content |