OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/test/chromedriver/chrome/chrome_desktop_impl.h" | 5 #include "chrome/test/chromedriver/chrome/chrome_desktop_impl.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/posix/eintr_wrapper.h" | 9 #include "base/posix/eintr_wrapper.h" |
10 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
11 #include "base/sys_info.h" | 11 #include "base/sys_info.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "chrome/test/chromedriver/chrome/automation_extension.h" | 14 #include "chrome/test/chromedriver/chrome/automation_extension.h" |
15 #include "chrome/test/chromedriver/chrome/devtools_client.h" | 15 #include "chrome/test/chromedriver/chrome/devtools_client.h" |
16 #include "chrome/test/chromedriver/chrome/devtools_http_client.h" | 16 #include "chrome/test/chromedriver/chrome/devtools_http_client.h" |
17 #include "chrome/test/chromedriver/chrome/status.h" | 17 #include "chrome/test/chromedriver/chrome/status.h" |
18 #include "chrome/test/chromedriver/chrome/web_view_impl.h" | 18 #include "chrome/test/chromedriver/chrome/web_view_impl.h" |
19 #include "chrome/test/chromedriver/net/port_server.h" | 19 #include "chrome/test/chromedriver/net/port_server.h" |
20 | 20 |
21 #if defined(OS_POSIX) | 21 #if defined(OS_POSIX) |
22 #include <errno.h> | 22 #include <errno.h> |
23 #include <signal.h> | 23 #include <signal.h> |
24 #include <sys/wait.h> | 24 #include <sys/wait.h> |
25 #include <unistd.h> | 25 #include <unistd.h> |
26 #endif | 26 #endif |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 bool KillProcess(base::ProcessHandle process_id, bool kill_gracefully) { | 30 bool KillProcess(const base::Process& process, bool kill_gracefully) { |
31 #if defined(OS_POSIX) | 31 #if defined(OS_POSIX) |
32 if (!kill_gracefully) { | 32 if (!kill_gracefully) { |
33 kill(process_id, SIGKILL); | 33 kill(process.Pid(), SIGKILL); |
34 base::TimeTicks deadline = | 34 base::TimeTicks deadline = |
35 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(30); | 35 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(30); |
36 while (base::TimeTicks::Now() < deadline) { | 36 while (base::TimeTicks::Now() < deadline) { |
37 pid_t pid = HANDLE_EINTR(waitpid(process_id, NULL, WNOHANG)); | 37 pid_t pid = HANDLE_EINTR(waitpid(process.Pid(), NULL, WNOHANG)); |
38 if (pid == process_id) | 38 if (pid == process.Pid()) |
39 return true; | 39 return true; |
40 if (pid == -1) { | 40 if (pid == -1) { |
41 if (errno == ECHILD) { | 41 if (errno == ECHILD) { |
42 // The wait may fail with ECHILD if another process also waited for | 42 // The wait may fail with ECHILD if another process also waited for |
43 // the same pid, causing the process state to get cleaned up. | 43 // the same pid, causing the process state to get cleaned up. |
44 return true; | 44 return true; |
45 } | 45 } |
46 LOG(WARNING) << "Error waiting for process " << process_id; | 46 LOG(WARNING) << "Error waiting for process " << process.Pid(); |
47 } | 47 } |
48 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); | 48 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50)); |
49 } | 49 } |
50 return false; | 50 return false; |
51 } | 51 } |
52 #endif | 52 #endif |
53 | 53 |
54 if (!base::KillProcess(process_id, 0, true)) { | 54 if (!process.Terminate(0, true)) { |
55 int exit_code; | 55 int exit_code; |
56 return base::GetTerminationStatus(process_id, &exit_code) != | 56 return base::GetTerminationStatus(process.Handle(), &exit_code) != |
57 base::TERMINATION_STATUS_STILL_RUNNING; | 57 base::TERMINATION_STATUS_STILL_RUNNING; |
58 } | 58 } |
59 return true; | 59 return true; |
60 } | 60 } |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 ChromeDesktopImpl::ChromeDesktopImpl( | 64 ChromeDesktopImpl::ChromeDesktopImpl( |
65 scoped_ptr<DevToolsHttpClient> http_client, | 65 scoped_ptr<DevToolsHttpClient> http_client, |
66 scoped_ptr<DevToolsClient> websocket_client, | 66 scoped_ptr<DevToolsClient> websocket_client, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 bool ChromeDesktopImpl::IsMobileEmulationEnabled() const { | 166 bool ChromeDesktopImpl::IsMobileEmulationEnabled() const { |
167 return devtools_http_client_->device_metrics() != NULL; | 167 return devtools_http_client_->device_metrics() != NULL; |
168 } | 168 } |
169 | 169 |
170 Status ChromeDesktopImpl::QuitImpl() { | 170 Status ChromeDesktopImpl::QuitImpl() { |
171 // If the Chrome session uses a custom user data directory, try sending a | 171 // If the Chrome session uses a custom user data directory, try sending a |
172 // SIGTERM signal before SIGKILL, so that Chrome has a chance to write | 172 // SIGTERM signal before SIGKILL, so that Chrome has a chance to write |
173 // everything back out to the user data directory and exit cleanly.If | 173 // everything back out to the user data directory and exit cleanly.If |
174 // we're using a temporary user data directory, we're going to delete | 174 // we're using a temporary user data directory, we're going to delete |
175 // the temporary directory anyway, so just send SIGKILL immediately. | 175 // the temporary directory anyway, so just send SIGKILL immediately. |
176 if (!KillProcess(process_.Handle(), !user_data_dir_.IsValid())) | 176 if (!KillProcess(process_, !user_data_dir_.IsValid())) |
177 return Status(kUnknownError, "cannot kill Chrome"); | 177 return Status(kUnknownError, "cannot kill Chrome"); |
178 return Status(kOk); | 178 return Status(kOk); |
179 } | 179 } |
180 | 180 |
181 const base::CommandLine& ChromeDesktopImpl::command() const { | 181 const base::CommandLine& ChromeDesktopImpl::command() const { |
182 return command_; | 182 return command_; |
183 } | 183 } |
OLD | NEW |