OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_runner_win. h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/base_paths.h" | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/feature_list.h" | |
13 #include "base/location.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/path_service.h" | |
16 #include "base/strings/string_number_conversions.h" | |
17 #include "base/task_scheduler/post_task.h" | |
18 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_client_info_win.h" | |
19 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_field_trial_win.h" | |
20 #include "chrome/installer/util/install_util.h" | |
21 #include "components/chrome_cleaner/public/constants/constants.h" | |
22 #include "components/chrome_cleaner/public/interfaces/chrome_prompt.mojom.h" | |
23 #include "components/version_info/version_info.h" | |
24 #include "content/public/browser/browser_thread.h" | |
25 #include "mojo/edk/embedder/connection_params.h" | |
26 #include "mojo/edk/embedder/embedder.h" | |
27 #include "mojo/edk/embedder/outgoing_broker_client_invitation.h" | |
28 #include "mojo/edk/embedder/platform_channel_pair.h" | |
29 #include "mojo/edk/embedder/transport_protocol.h" | |
30 #include "mojo/public/cpp/system/message_pipe.h" | |
31 | |
32 using chrome_cleaner::mojom::ChromePrompt; | |
33 using chrome_cleaner::mojom::ChromePromptRequest; | |
34 using content::BrowserThread; | |
35 | |
36 namespace safe_browsing { | |
37 | |
38 namespace { | |
39 | |
40 // Global delegate used to override the launching of the Cleaner process during | |
41 // tests. | |
42 ChromeCleanerRunnerTestDelegate* g_test_delegate = nullptr; | |
43 | |
44 void ReleaseChromePromptImpl( | |
45 std::unique_ptr<ChromePromptImpl> chrome_prompt_impl) { | |
46 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
47 DCHECK(chrome_prompt_impl); | |
48 // |chrome_prompt_impl| will be destroyed when the unique_ptr goes out of | |
49 // scope. | |
Joe Mason
2017/05/25 16:40:33
Nit: I like to use an explicit .release() call ins
alito
2017/05/25 18:39:13
Done.
| |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 // static | |
55 void ChromeCleanerRunner::RunChromeCleanerAndReplyWithExitCode( | |
56 const base::FilePath& cleaner_executable_path, | |
57 const SwReporterInvocation& reporter_invocation, | |
58 ChromeMetricsStatus metrics_status, | |
59 CleanerLogsStatus cleaner_logs_status, | |
60 ChromePromptImpl::OnPromptUser on_prompt_user, | |
61 base::OnceClosure on_connection_closed, | |
62 ChromeCleanerRunner::ProcessDoneCallback on_process_done, | |
63 scoped_refptr<base::SequencedTaskRunner> task_runner) { | |
64 auto cleaner_runner = make_scoped_refptr(new ChromeCleanerRunner( | |
65 cleaner_executable_path, reporter_invocation, metrics_status, | |
66 cleaner_logs_status, std::move(on_prompt_user), | |
67 std::move(on_connection_closed), std::move(on_process_done), | |
68 std::move(task_runner))); | |
69 auto launch_and_wait = base::BindOnce( | |
70 &ChromeCleanerRunner::LaunchAndWaitForExitOnBackgroundThread, | |
71 cleaner_runner); | |
72 auto process_done = | |
73 base::BindOnce(&ChromeCleanerRunner::OnProcessDone, cleaner_runner); | |
74 base::PostTaskWithTraitsAndReplyWithResult( | |
75 FROM_HERE, | |
76 // LaunchAndWaitForExitOnBackgroundThread creates (MayBlock()) and joins | |
77 // (WithBaseSyncPrimitives()) a process. | |
78 {base::MayBlock(), base::WithBaseSyncPrimitives(), | |
79 base::TaskPriority::BACKGROUND, | |
80 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, | |
81 std::move(launch_and_wait), std::move(process_done)); | |
82 } | |
83 | |
84 ChromeCleanerRunner::ChromeCleanerRunner( | |
85 const base::FilePath& cleaner_executable_path, | |
86 const SwReporterInvocation& reporter_invocation, | |
87 ChromeMetricsStatus metrics_status, | |
88 CleanerLogsStatus cleaner_logs_status, | |
89 ChromePromptImpl::OnPromptUser on_prompt_user, | |
90 base::OnceClosure on_connection_closed, | |
91 ProcessDoneCallback on_process_done, | |
92 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
93 : task_runner_(std::move(task_runner)), | |
94 cleaner_command_line_(cleaner_executable_path), | |
95 on_prompt_user_(std::move(on_prompt_user)), | |
96 on_connection_closed_(std::move(on_connection_closed)), | |
97 on_process_done_(std::move(on_process_done)) { | |
98 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); | |
99 DCHECK(on_prompt_user_); | |
100 DCHECK(on_connection_closed_); | |
101 DCHECK(on_process_done_); | |
102 DCHECK(!cleaner_executable_path.empty()); | |
103 | |
104 // Add the non-IPC switches that should be passed to the Cleaner process. | |
105 | |
106 // Add switches that pass information about this Chrome installation. | |
107 cleaner_command_line_.AppendSwitchASCII(chrome_cleaner::kChromeVersionSwitch, | |
108 version_info::GetVersionNumber()); | |
109 cleaner_command_line_.AppendSwitchASCII(chrome_cleaner::kChromeChannelSwitch, | |
110 base::IntToString(ChannelAsInt())); | |
111 base::FilePath chrome_exe_path; | |
112 PathService::Get(base::FILE_EXE, &chrome_exe_path); | |
113 cleaner_command_line_.AppendSwitchPath(chrome_cleaner::kChromeExePathSwitch, | |
114 chrome_exe_path); | |
115 if (!InstallUtil::IsPerUserInstall()) | |
116 cleaner_command_line_.AppendSwitch( | |
117 chrome_cleaner::kChromeSystemInstallSwitch); | |
118 | |
119 // Start the cleaner process in scanning mode. | |
120 cleaner_command_line_.AppendSwitchASCII( | |
121 chrome_cleaner::kExecutionModeSwitch, | |
122 base::IntToString( | |
123 static_cast<int>(chrome_cleaner::ExecutionMode::kScanning))); | |
124 | |
125 // If set, forward the engine flag from the reporter. Otherwise, set the | |
126 // engine flag explicitly to 1. | |
127 const std::string& reporter_engine = | |
128 reporter_invocation.command_line.GetSwitchValueASCII( | |
129 chrome_cleaner::kEngineSwitch); | |
130 cleaner_command_line_.AppendSwitchASCII( | |
131 chrome_cleaner::kEngineSwitch, | |
132 reporter_engine.empty() ? "1" : reporter_engine); | |
133 | |
134 // If metrics is enabled, we can enable crash reporting in the Chrome Cleaner | |
135 // process. | |
136 if (metrics_status == ChromeMetricsStatus::kEnabled) { | |
137 cleaner_command_line_.AppendSwitch(chrome_cleaner::kUmaUserSwitch); | |
138 cleaner_command_line_.AppendSwitch( | |
139 chrome_cleaner::kEnableCrashReportingSwitch); | |
140 } | |
141 | |
142 // Enable logs upload for users who have opted into safe browsing extended | |
143 // reporting v2. | |
144 if (cleaner_logs_status == CleanerLogsStatus::kUploadEnabled) | |
145 cleaner_command_line_.AppendSwitch( | |
146 chrome_cleaner::kEnableCleanerLoggingSwitch); | |
147 } | |
148 | |
149 ChromeCleanerRunner::LaunchStatus | |
150 ChromeCleanerRunner::LaunchAndWaitForExitOnBackgroundThread() { | |
151 mojo::edk::OutgoingBrokerClientInvitation invitation; | |
152 std::string mojo_pipe_token = mojo::edk::GenerateRandomToken(); | |
153 mojo::ScopedMessagePipeHandle mojo_pipe = | |
154 invitation.AttachMessagePipe(mojo_pipe_token); | |
155 cleaner_command_line_.AppendSwitchASCII( | |
156 chrome_cleaner::kChromeMojoPipeTokenSwitch, mojo_pipe_token); | |
157 | |
158 mojo::edk::PlatformChannelPair channel; | |
159 base::HandlesToInheritVector handles_to_inherit; | |
160 channel.PrepareToPassClientHandleToChildProcess(&cleaner_command_line_, | |
161 &handles_to_inherit); | |
162 base::LaunchOptions launch_options; | |
163 launch_options.handles_to_inherit = &handles_to_inherit; | |
164 | |
165 base::Process cleaner_process = | |
166 g_test_delegate | |
167 ? g_test_delegate->LaunchTestProcess(cleaner_command_line_, | |
168 launch_options) | |
169 : base::LaunchProcess(cleaner_command_line_, launch_options); | |
170 | |
171 constexpr int kBadProcessExitCode = std::numeric_limits<int>::max(); | |
172 if (!cleaner_process.IsValid()) | |
173 return {false, kBadProcessExitCode}; | |
174 | |
175 // ChromePromptImpl tasks will need to run on the IO thread. There is no | |
176 // need to synchronize its creation, since the client end will wait for this | |
177 // initialization to be done before sending requests. | |
178 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) | |
179 ->PostTask(FROM_HERE, | |
180 base::BindOnce(&ChromeCleanerRunner::CreateChromePromptImpl, | |
181 base::RetainedRef(this), | |
182 chrome_cleaner::mojom::ChromePromptRequest( | |
183 std::move(mojo_pipe)))); | |
184 | |
185 invitation.Send( | |
186 cleaner_process.Handle(), | |
187 mojo::edk::ConnectionParams(mojo::edk::TransportProtocol::kLegacy, | |
188 channel.PassServerHandle())); | |
189 | |
190 int exit_code = kBadProcessExitCode; | |
191 if (cleaner_process.WaitForExit(&exit_code)) | |
192 return {true, exit_code}; | |
193 return {false, kBadProcessExitCode}; | |
194 } | |
195 | |
196 ChromeCleanerRunner::~ChromeCleanerRunner() { | |
197 if (chrome_prompt_impl_) { | |
198 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) | |
199 ->PostTask(FROM_HERE, base::Bind(&ReleaseChromePromptImpl, | |
200 base::Passed(&chrome_prompt_impl_))); | |
201 } | |
202 } | |
203 | |
204 void ChromeCleanerRunner::CreateChromePromptImpl( | |
205 ChromePromptRequest chrome_prompt_request) { | |
206 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
207 DCHECK(!chrome_prompt_impl_); | |
208 | |
209 chrome_prompt_impl_ = base::MakeUnique<ChromePromptImpl>( | |
210 std::move(chrome_prompt_request), | |
211 base::Bind(&ChromeCleanerRunner::OnConnectionClosed, | |
212 base::RetainedRef(this)), | |
213 base::Bind(&ChromeCleanerRunner::OnPromptUser, base::RetainedRef(this))); | |
214 } | |
215 | |
216 void ChromeCleanerRunner::OnPromptUser( | |
217 std::unique_ptr<std::set<base::FilePath>> files_to_delete, | |
218 ChromePrompt::PromptUserCallback prompt_user_callback) { | |
219 if (on_prompt_user_) { | |
220 task_runner_->PostTask(FROM_HERE, | |
221 base::BindOnce(std::move(on_prompt_user_), | |
222 base::Passed(&files_to_delete), | |
223 base::Passed(&prompt_user_callback))); | |
224 } | |
225 } | |
226 | |
227 void ChromeCleanerRunner::OnConnectionClosed() { | |
228 if (on_connection_closed_) | |
229 task_runner_->PostTask(FROM_HERE, std::move(on_connection_closed_)); | |
230 } | |
231 | |
232 void ChromeCleanerRunner::OnProcessDone(LaunchStatus launch_status) { | |
233 if (on_process_done_) { | |
234 task_runner_->PostTask( | |
235 FROM_HERE, base::BindOnce(std::move(on_process_done_), launch_status)); | |
236 } | |
237 } | |
238 | |
239 void SetChromeCleanerRunnerTestDelegateForTesting( | |
240 ChromeCleanerRunnerTestDelegate* test_delegate) { | |
241 g_test_delegate = test_delegate; | |
242 } | |
243 | |
244 } // namespace safe_browsing | |
OLD | NEW |