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