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 } // namespace | |
45 | |
46 // static | |
47 void ChromeCleanerRunner::RunChromeCleanerAndReplyWithExitCode( | |
48 const base::FilePath& executable_path, | |
49 const SwReporterInvocation& reporter_invocation, | |
50 ChromeMetricsStatus metrics_status, | |
51 CleanerLogsStatus cleaner_logs_status, | |
52 ChromePromptImpl::OnPromptUser on_prompt_user, | |
53 base::OnceClosure on_connection_closed, | |
54 ChromeCleanerRunner::ProcessDoneCallback on_process_done, | |
55 scoped_refptr<base::SequencedTaskRunner> task_runner) { | |
56 auto cleaner_runner = make_scoped_refptr(new ChromeCleanerRunner( | |
57 executable_path, reporter_invocation, metrics_status, cleaner_logs_status, | |
58 std::move(on_prompt_user), std::move(on_connection_closed), | |
59 std::move(on_process_done), task_runner)); | |
ftirelo
2017/05/24 01:45:20
Maybe std::move(task_runner) as well?
alito
2017/05/24 03:20:23
Done.
| |
60 auto launch_and_wait = base::BindOnce( | |
61 &ChromeCleanerRunner::LaunchAndWaitForExitOnBackgroundThread, | |
62 cleaner_runner); | |
ftirelo
2017/05/24 01:45:20
Should we use base::RetainedRef(cleaner_runner) he
alito
2017/05/24 03:20:23
Actually, I think the way it is written here is th
| |
63 auto process_done = | |
64 base::BindOnce(&ChromeCleanerRunner::OnProcessDone, cleaner_runner); | |
ftirelo
2017/05/24 01:45:20
Should we use base::RetainedRef(cleaner_runner) he
alito
2017/05/24 03:20:23
See previous comment.
| |
65 base::PostTaskWithTraitsAndReplyWithResult( | |
66 FROM_HERE, | |
67 // LaunchAndWaitForExitOnBackgroundThread creates (MayBlock()) and joins | |
68 // (WithBaseSyncPrimitives()) a process. | |
69 {base::MayBlock(), base::WithBaseSyncPrimitives(), | |
70 base::TaskPriority::BACKGROUND, | |
71 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, | |
72 std::move(launch_and_wait), std::move(process_done)); | |
73 } | |
74 | |
75 ChromeCleanerRunner::ChromeCleanerRunner( | |
76 const base::FilePath& executable_path, | |
77 const SwReporterInvocation& reporter_invocation, | |
78 ChromeMetricsStatus metrics_status, | |
79 CleanerLogsStatus cleaner_logs_status, | |
80 ChromePromptImpl::OnPromptUser on_prompt_user, | |
81 base::OnceClosure on_connection_closed, | |
82 ProcessDoneCallback on_process_done, | |
83 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
84 : task_runner_(task_runner), | |
ftirelo
2017/05/24 01:45:20
Ditto.
alito
2017/05/24 03:20:23
Done.
| |
85 command_line_(executable_path), | |
86 on_prompt_user_(std::move(on_prompt_user)), | |
87 on_connection_closed_(std::move(on_connection_closed)), | |
88 on_process_done_(std::move(on_process_done)) { | |
89 DCHECK(base::FeatureList::IsEnabled(kInBrowserCleanerUIFeature)); | |
90 DCHECK(on_prompt_user_); | |
91 DCHECK(on_connection_closed_); | |
92 DCHECK(on_process_done_); | |
93 DCHECK(!executable_path.empty()); | |
94 | |
95 if (g_test_delegate) | |
96 command_line_ = g_test_delegate->GetTestBaseCommandLine(); | |
97 | |
98 // Add the non-IPC switches that should be passed to the Cleaner process. | |
99 | |
100 // Add switches that pass information about this Chrome installation. | |
101 command_line_.AppendSwitchASCII(chrome_cleaner::kChromeVersionSwitch, | |
102 version_info::GetVersionNumber()); | |
103 command_line_.AppendSwitchASCII(chrome_cleaner::kChromeChannelSwitch, | |
104 base::IntToString(ChannelAsInt())); | |
105 base::FilePath chrome_exe_path; | |
106 PathService::Get(base::FILE_EXE, &chrome_exe_path); | |
107 command_line_.AppendSwitchPath(chrome_cleaner::kChromeExePathSwitch, | |
108 chrome_exe_path); | |
109 if (!InstallUtil::IsPerUserInstall()) | |
110 command_line_.AppendSwitch(chrome_cleaner::kChromeSystemInstallSwitch); | |
111 | |
112 // Start the cleaner process in scanning mode. | |
113 command_line_.AppendSwitchASCII( | |
114 chrome_cleaner::kExecutionModeSwitch, | |
115 base::IntToString( | |
116 static_cast<int>(chrome_cleaner::ExecutionMode::kScanning))); | |
117 | |
118 // If set, forward the engine flag from the reporter. Otherwise, set the | |
119 // engine flag explicitly to 1. | |
120 command_line_.AppendSwitchASCII( | |
121 chrome_cleaner::kEngineSwitch, | |
122 reporter_invocation.command_line.HasSwitch(chrome_cleaner::kEngineSwitch) | |
123 ? reporter_invocation.command_line.GetSwitchValueASCII( | |
124 chrome_cleaner::kEngineSwitch) | |
125 : base::IntToString(1)); | |
ftirelo
2017/05/24 01:45:20
I think it'd be easier to read this if you saved t
alito
2017/05/24 03:20:23
Done.
| |
126 | |
127 // If metrics is enabled, we can enable crash reporting in the Chrome Cleaner | |
128 // process. | |
129 if (metrics_status == ChromeMetricsStatus::kEnabled) { | |
130 command_line_.AppendSwitch(chrome_cleaner::kUmaUserSwitch); | |
131 command_line_.AppendSwitch(chrome_cleaner::kEnableCrashReportingSwitch); | |
132 } | |
133 | |
134 // Enable logs upload for users who have opted into safe browsing extended | |
135 // reporting v2. | |
136 if (cleaner_logs_status == CleanerLogsStatus::kUploadEnabled) | |
137 command_line_.AppendSwitch(chrome_cleaner::kEnableCleanerLoggingSwitch); | |
138 } | |
139 | |
140 ChromeCleanerRunner::LaunchStatus | |
141 ChromeCleanerRunner::LaunchAndWaitForExitOnBackgroundThread() { | |
142 constexpr int kBadProcessExitCode = std::numeric_limits<int>::max(); | |
143 | |
144 mojo::edk::OutgoingBrokerClientInvitation invitation; | |
145 std::string mojo_pipe_token = mojo::edk::GenerateRandomToken(); | |
146 mojo::ScopedMessagePipeHandle mojo_pipe = | |
147 invitation.AttachMessagePipe(mojo_pipe_token); | |
148 command_line_.AppendSwitchASCII(chrome_cleaner::kChromeMojoPipeTokenSwitch, | |
149 mojo_pipe_token); | |
150 | |
151 mojo::edk::PlatformChannelPair channel; | |
152 base::HandlesToInheritVector handles_to_inherit; | |
153 channel.PrepareToPassClientHandleToChildProcess(&command_line_, | |
154 &handles_to_inherit); | |
155 base::LaunchOptions launch_options; | |
156 launch_options.handles_to_inherit = &handles_to_inherit; | |
157 | |
158 base::Process cleaner_process = | |
159 g_test_delegate | |
160 ? g_test_delegate->LaunchTestProcess(command_line_, launch_options) | |
161 : base::LaunchProcess(command_line_, launch_options); | |
162 | |
163 if (!cleaner_process.IsValid()) | |
164 return {false, kBadProcessExitCode}; | |
165 | |
166 // ChromePromptImpl tasks will need to run on the IO thread. There is no | |
167 // need to synchronize its creation, since the client end will wait for this | |
168 // initialization to be done before sending requests. | |
169 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) | |
170 ->PostTask(FROM_HERE, | |
171 base::BindOnce(&ChromeCleanerRunner::CreateChromePromptImpl, | |
172 base::RetainedRef(this), | |
173 chrome_cleaner::mojom::ChromePromptRequest( | |
174 std::move(mojo_pipe)))); | |
175 | |
176 invitation.Send( | |
177 cleaner_process.Handle(), | |
178 mojo::edk::ConnectionParams(mojo::edk::TransportProtocol::kLegacy, | |
179 channel.PassServerHandle())); | |
180 | |
181 int exit_code = kBadProcessExitCode; | |
182 if (cleaner_process.WaitForExit(&exit_code)) | |
183 return {true, exit_code}; | |
184 return {false, kBadProcessExitCode}; | |
185 } | |
186 | |
187 ChromeCleanerRunner::~ChromeCleanerRunner() { | |
188 DCHECK(!chrome_prompt_impl_); | |
189 } | |
190 | |
191 void ChromeCleanerRunner::CreateChromePromptImpl( | |
192 ChromePromptRequest chrome_prompt_request) { | |
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
194 DCHECK(!chrome_prompt_impl_); | |
195 | |
196 chrome_prompt_impl_ = base::MakeUnique<ChromePromptImpl>( | |
197 std::move(chrome_prompt_request), | |
198 base::Bind(&ChromeCleanerRunner::OnConnectionClosed, | |
199 base::RetainedRef(this)), | |
200 base::Bind(&ChromeCleanerRunner::OnPromptUser, base::RetainedRef(this))); | |
201 // Ensure that this object is not destroyed before chrome_prompt_impl_ has | |
202 // been released in ReleaseChromePromptImpl(). This is a bit paranoid, but | |
203 // better safe than sorry. | |
204 AddRef(); | |
205 } | |
206 | |
207 void ChromeCleanerRunner::ReleaseChromePromptImpl() { | |
208 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
209 DCHECK(chrome_prompt_impl_); | |
210 chrome_prompt_impl_.release(); | |
211 // Balanced by the call to AddRef() in CreateChromePromptImpl(). | |
212 Release(); | |
213 } | |
214 | |
215 void ChromeCleanerRunner::OnPromptUser( | |
216 std::unique_ptr<std::set<base::FilePath>> files_to_delete, | |
217 ChromePrompt::PromptUserCallback prompt_user_callback) { | |
218 if (on_prompt_user_) { | |
219 task_runner_->PostTask(FROM_HERE, | |
220 base::BindOnce(std::move(on_prompt_user_), | |
221 base::Passed(&files_to_delete), | |
222 base::Passed(&prompt_user_callback))); | |
223 } | |
224 } | |
225 | |
226 void ChromeCleanerRunner::OnConnectionClosed() { | |
227 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO) | |
228 ->PostTask(FROM_HERE, | |
229 base::Bind(&ChromeCleanerRunner::ReleaseChromePromptImpl, | |
230 base::RetainedRef(this))); | |
231 | |
232 if (on_connection_closed_) | |
233 task_runner_->PostTask(FROM_HERE, std::move(on_connection_closed_)); | |
234 } | |
235 | |
236 void ChromeCleanerRunner::OnProcessDone(LaunchStatus launch_status) { | |
237 if (on_process_done_) { | |
238 task_runner_->PostTask( | |
239 FROM_HERE, base::BindOnce(std::move(on_process_done_), launch_status)); | |
240 } | |
241 } | |
242 | |
243 void SetChromeCleanerRunnerTestDelegateForTesting( | |
244 ChromeCleanerRunnerTestDelegate* test_delegate) { | |
245 g_test_delegate = test_delegate; | |
246 } | |
247 | |
248 } // namespace safe_browsing | |
OLD | NEW |