Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(701)

Side by Side Diff: chrome/browser/safe_browsing/chrome_cleaner/mock_chrome_cleaner_process_win.cc

Issue 2890023005: Chrome Cleaner UI: reporter no longer uses mojo. (Closed)
Patch Set: Fixes clang compile error Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/mock_chrome_cleaner_proces s_win.h"
6
7 #include <utility>
8
9 #include "base/bind_helpers.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/threading/sequenced_task_runner_handle.h"
16 #include "base/threading/thread.h"
17 #include "mojo/edk/embedder/connection_params.h"
18 #include "mojo/edk/embedder/embedder.h"
19 #include "mojo/edk/embedder/incoming_broker_client_invitation.h"
20 #include "mojo/edk/embedder/outgoing_broker_client_invitation.h"
21 #include "mojo/edk/embedder/platform_channel_pair.h"
22 #include "mojo/edk/embedder/scoped_ipc_support.h"
23 #include "mojo/edk/embedder/transport_protocol.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace safe_browsing {
28
29 namespace {
30
31 using ::chrome_cleaner::mojom::ChromePrompt;
32 using ::chrome_cleaner::mojom::ChromePromptPtr;
33 using ::chrome_cleaner::mojom::ChromePromptPtrInfo;
34 using ::chrome_cleaner::mojom::ElevationStatus;
35 using ::chrome_cleaner::mojom::PromptAcceptance;
36 using ::chrome_cleaner::mojom::UwSPtr;
37
38 constexpr char kCrashPointSwitch[] = "mock-crash-point";
39 constexpr char kUwsFoundSwitch[] = "mock-uws-found";
40 constexpr char kRebootRequiredSwitch[] = "mock-reboot-required";
41
42 std::vector<UwSPtr> clone_uwsptr_vector(const std::vector<UwSPtr>& uws_ptrs) {
43 std::vector<UwSPtr> cloned;
44 for (const UwSPtr& uws_ptr : uws_ptrs)
45 cloned.push_back(uws_ptr->Clone());
46 return cloned;
47 }
48
49 } // namespace
50
51 // static
52 bool MockChromeCleanerProcess::Options::FromCommandLine(
53 const base::CommandLine& command_line,
54 Options* options) {
55 options->SetDoFindUws(command_line.HasSwitch(kUwsFoundSwitch));
56 options->set_reboot_required(command_line.HasSwitch(kRebootRequiredSwitch));
57
58 if (command_line.HasSwitch(kCrashPointSwitch)) {
59 int crash_point_int = 0;
60 if (base::StringToInt(command_line.GetSwitchValueASCII(kCrashPointSwitch),
61 &crash_point_int) &&
62 crash_point_int >= 0 &&
63 crash_point_int < static_cast<int>(CrashPoint::kNumCrashPoints)) {
64 options->set_crash_point(static_cast<CrashPoint>(crash_point_int));
65 } else {
66 return false;
67 }
68 }
69
70 return true;
71 }
72
73 MockChromeCleanerProcess::Options::Options() = default;
74
75 MockChromeCleanerProcess::Options::Options(const Options& other)
76 : files_to_delete_(other.files_to_delete_),
77 reboot_required_(other.reboot_required_),
78 crash_point_(other.crash_point_) {
79 found_uws_ = clone_uwsptr_vector(other.found_uws_);
80 }
81
82 MockChromeCleanerProcess::Options& MockChromeCleanerProcess::Options::operator=(
83 const Options& other) {
84 found_uws_ = clone_uwsptr_vector(other.found_uws_);
85 files_to_delete_ = other.files_to_delete_;
86 reboot_required_ = other.reboot_required_;
87 crash_point_ = other.crash_point_;
88 return *this;
89 }
90
91 MockChromeCleanerProcess::Options::~Options() {}
92
93 void MockChromeCleanerProcess::Options::AddSwitchesToCommandLine(
94 base::CommandLine* command_line) const {
95 if (!found_uws_.empty())
96 command_line->AppendSwitch(kUwsFoundSwitch);
97
98 if (reboot_required())
99 command_line->AppendSwitch(kRebootRequiredSwitch);
100
101 if (crash_point() != CrashPoint::kNone) {
102 command_line->AppendSwitchASCII(
103 kCrashPointSwitch, base::IntToString(static_cast<int>(crash_point())));
104 }
105 }
106
107 void MockChromeCleanerProcess::Options::SetDoFindUws(bool do_find_uws) {
108 found_uws_.clear();
109 files_to_delete_.clear();
110 if (!do_find_uws)
111 return;
112
113 UwSPtr uws1 = chrome_cleaner::mojom::UwS::New();
114 uws1->id = 1;
115 uws1->name = "Some UwS";
116 uws1->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New();
117 uws1->files_to_delete.push_back(
118 base::FilePath((FILE_PATH_LITERAL("/path/to/file1.exe"))));
119 uws1->files_to_delete.push_back(
120 base::FilePath((FILE_PATH_LITERAL("/path/to/other/file2.exe"))));
121 found_uws_.push_back(std::move(uws1));
122
123 UwSPtr uws2 = chrome_cleaner::mojom::UwS::New();
124 uws2->id = 2;
125 uws2->name = "Other UwS";
126 uws2->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New();
127 uws2->files_to_delete.push_back(
128 base::FilePath((FILE_PATH_LITERAL("/path/to/some file.dll"))));
129 found_uws_.push_back(std::move(uws2));
130
131 for (const UwSPtr& uws_ptr : found_uws_) {
132 for (const base::FilePath& filepath : uws_ptr->files_to_delete)
133 files_to_delete_.insert(filepath);
134 }
135 }
136
137 int MockChromeCleanerProcess::Options::ExpectedExitCode(
138 PromptAcceptance received_prompt_acceptance) const {
139 if (crash_point() != CrashPoint::kNone)
140 return kDeliberateCrashExitCode;
141
142 if (found_uws_.empty())
143 return kNothingFoundExitCode;
144
145 if (received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITH_LOGS ||
146 received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITHOUT_LOGS) {
147 return reboot_required() ? kRebootRequiredExitCode
148 : kRebootNotRequiredExitCode;
149 }
150
151 return kCanceledExitCode;
152 }
153
154 MockChromeCleanerProcess::MockChromeCleanerProcess(
155 const Options& options,
156 const std::string& chrome_mojo_pipe_token)
157 : options_(options), chrome_mojo_pipe_token_(chrome_mojo_pipe_token) {}
158
159 int MockChromeCleanerProcess::Run() {
160 // We use EXPECT_*() macros to get good logs in the tests, but since failing a
161 // EXPECT_*() macro does not fail the test (this code is run in a separate
162 // process), we explicitly use ::testing::Test::HasFailure() return an error
ftirelo 2017/05/24 01:45:20 I think this sentence is missing something.
alito 2017/05/24 03:20:23 Yup. Reformulated.
163 // code that indicates that the test has failed.
164 EXPECT_FALSE(chrome_mojo_pipe_token_.empty());
165 if (::testing::Test::HasFailure())
166 return kInternalTestFailureExitCode;
167
168 if (options_.crash_point() == CrashPoint::kOnStartup)
169 exit(kDeliberateCrashExitCode);
170
171 mojo::edk::Init();
172 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
173 base::Thread io_thread("IPCThread");
174 EXPECT_TRUE(io_thread.StartWithOptions(thread_options));
175 if (::testing::Test::HasFailure())
176 return kInternalTestFailureExitCode;
177
178 mojo::edk::ScopedIPCSupport ipc_support(
179 io_thread.task_runner(),
180 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN);
181
182 auto invitation =
183 mojo::edk::IncomingBrokerClientInvitation::AcceptFromCommandLine(
184 mojo::edk::TransportProtocol::kLegacy);
185 ChromePromptPtrInfo prompt_ptr_info(
186 invitation->ExtractMessagePipe(chrome_mojo_pipe_token_), 0);
187
188 if (options_.crash_point() == CrashPoint::kAfterConnection)
189 exit(kDeliberateCrashExitCode);
190
191 base::MessageLoop message_loop;
192 base::RunLoop run_loop;
193 // After the response from the parent process is received, this will post a
194 // task to unblock the child process's main thread.
195 auto quit_closure = base::BindOnce(
196 [](scoped_refptr<base::SequencedTaskRunner> main_runner,
197 base::Closure quit_closure) {
198 main_runner->PostTask(FROM_HERE, std::move(quit_closure));
199 },
200 base::SequencedTaskRunnerHandle::Get(),
201 base::Passed(run_loop.QuitClosure()));
202
203 io_thread.task_runner()->PostTask(
204 FROM_HERE,
205 base::BindOnce(&MockChromeCleanerProcess::SendScanResults,
206 base::Unretained(this), std::move(prompt_ptr_info),
207 base::Passed(&quit_closure)));
208
209 run_loop.Run();
210
211 EXPECT_NE(received_prompt_acceptance_, PromptAcceptance::UNSPECIFIED);
212 if (::testing::Test::HasFailure())
213 return kInternalTestFailureExitCode;
214 return options_.ExpectedExitCode(received_prompt_acceptance_);
215 }
216
217 void MockChromeCleanerProcess::SendScanResults(
218 ChromePromptPtrInfo prompt_ptr_info,
219 base::OnceClosure quit_closure) {
220 // This pointer will be deleted by PromptUserCallback.
221 chrome_prompt_ptr_ = new ChromePromptPtr();
222 chrome_prompt_ptr_->Bind(std::move(prompt_ptr_info));
223
224 if (options_.crash_point() == CrashPoint::kAfterRequestSent) {
225 // This task is posted to the IPC thread so that it will happen after the
226 // request is sent to the parent process and before the response gets
227 // handled on the IPC thread.
228 base::SequencedTaskRunnerHandle::Get()->PostTask(
229 FROM_HERE, base::Bind([]() { exit(kDeliberateCrashExitCode); }));
230 }
231
232 std::vector<UwSPtr> uws_found;
233 for (const UwSPtr& uws_ptr : options_.found_uws())
234 uws_found.push_back(uws_ptr->Clone());
235
236 (*chrome_prompt_ptr_)
237 ->PromptUser(
238 std::move(uws_found), ElevationStatus::REQUIRED,
239 base::BindOnce(&MockChromeCleanerProcess::PromptUserCallback,
240 base::Unretained(this), std::move(quit_closure)));
241 }
242
243 void MockChromeCleanerProcess::PromptUserCallback(
244 base::OnceClosure quit_closure,
245 PromptAcceptance prompt_acceptance) {
246 delete chrome_prompt_ptr_;
247 chrome_prompt_ptr_ = nullptr;
248
249 received_prompt_acceptance_ = prompt_acceptance;
250
251 if (options_.crash_point() == CrashPoint::kAfterResponseReceived)
252 exit(kDeliberateCrashExitCode);
253
254 std::move(quit_closure).Run();
255 }
256
257 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698