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

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: Rebase 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 kExitCodeSwitch[] = "mock-exit-code";
40 constexpr char kUwsFoundSwitch[] = "mock-uws-found";
41 constexpr char kRebootRequiredSwitch[] = "mock-reboot-required";
42
43 } // namespace
44
45 // static
46 bool MockChromeCleanerProcess::Options::FromCommandLine(
47 const base::CommandLine& command_line,
48 Options* options) {
49 options->SetDoFindUws(command_line.HasSwitch(kUwsFoundSwitch));
50 options->set_reboot_required(command_line.HasSwitch(kRebootRequiredSwitch));
51
52 if (command_line.HasSwitch(kCrashPointSwitch)) {
53 int crash_point_int = 0;
54 if (base::StringToInt(command_line.GetSwitchValueASCII(kCrashPointSwitch),
55 &crash_point_int) &&
56 crash_point_int >= 0 &&
57 crash_point_int < static_cast<int>(CrashPoint::kNumCrashPoints)) {
58 options->set_crash_point(static_cast<CrashPoint>(crash_point_int));
59 } else {
60 return false;
61 }
62 }
63
64 return true;
65 }
66
67 MockChromeCleanerProcess::Options::Options(bool do_find_uws,
68 bool reboot_required,
69 CrashPoint crash_point)
Joe Mason 2017/05/23 15:39:36 Nit: This constructor's never actually used. Might
alito 2017/05/23 18:58:57 Done. Now only a default constructor without argum
70 : reboot_required_(reboot_required), crash_point_(crash_point) {
71 SetDoFindUws(do_find_uws);
72 }
73
74 MockChromeCleanerProcess::Options::Options(const Options& other)
75 : files_to_delete_(other.files_to_delete_),
76 reboot_required_(other.reboot_required_),
77 crash_point_(other.crash_point_) {
78 for (const UwSPtr& uws_ptr : other.found_uws())
79 found_uws_.push_back(uws_ptr->Clone());
80 }
81
82 void MockChromeCleanerProcess::Options::AddSwitchesToCommandLine(
83 base::CommandLine* command_line) const {
84 if (!found_uws_.empty())
85 command_line->AppendSwitch(kUwsFoundSwitch);
86
87 if (reboot_required())
88 command_line->AppendSwitch(kRebootRequiredSwitch);
89
90 if (crash_point() != CrashPoint::kNone) {
91 command_line->AppendSwitchASCII(
92 kCrashPointSwitch, base::IntToString(static_cast<int>(crash_point())));
93 }
94 }
95
96 void MockChromeCleanerProcess::Options::SetDoFindUws(bool do_find_uws) {
97 found_uws_.clear();
98 files_to_delete_.clear();
99 if (!do_find_uws)
100 return;
101
102 UwSPtr uws1 = chrome_cleaner::mojom::UwS::New();
103 uws1->id = 1;
104 uws1->name = "Some UwS";
105 uws1->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New();
106 uws1->files_to_delete.push_back(
107 base::FilePath((FILE_PATH_LITERAL("/path/to/file1.exe"))));
108 uws1->files_to_delete.push_back(
109 base::FilePath((FILE_PATH_LITERAL("/path/to/other/file2.exe"))));
110 found_uws_.push_back(std::move(uws1));
111
112 UwSPtr uws2 = chrome_cleaner::mojom::UwS::New();
113 uws2->id = 2;
114 uws2->name = "Other UwS";
115 uws2->observed_behaviours = chrome_cleaner::mojom::ObservedBehaviours::New();
116 uws2->files_to_delete.push_back(
117 base::FilePath((FILE_PATH_LITERAL("/path/to/some file.dll"))));
118 found_uws_.push_back(std::move(uws2));
119
120 for (const UwSPtr& uws_ptr : found_uws_) {
121 for (const base::FilePath& filepath : uws_ptr->files_to_delete)
122 files_to_delete_.insert(filepath);
123 }
124 }
125
126 int MockChromeCleanerProcess::Options::ExpectedExitCode(
127 PromptAcceptance received_prompt_acceptance) const {
128 if (crash_point() != CrashPoint::kNone)
129 return kDeliberateCrashExitCode;
130
131 if (found_uws_.empty())
132 return kNothingFoundExitCode;
133
134 if (received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITH_LOGS ||
135 received_prompt_acceptance == PromptAcceptance::ACCEPTED_WITHOUT_LOGS) {
136 return reboot_required() ? kRebootRequiredExitCode
137 : kRebootNotRequiredExitCode;
138 }
139
140 return kCanceledExitCode;
141 }
142
143 MockChromeCleanerProcess::MockChromeCleanerProcess(
144 const Options& options,
145 const std::string& chrome_mojo_pipe_token)
146 : options_(options), chrome_mojo_pipe_token_(chrome_mojo_pipe_token) {}
147
148 int MockChromeCleanerProcess::Run() {
149 EXPECT_FALSE(chrome_mojo_pipe_token_.empty());
150 if (::testing::Test::HasFailure())
Joe Mason 2017/05/23 15:39:36 Can you comment on what this pattern means?
alito 2017/05/23 18:58:57 Done.
151 return kInternalTestFailureExitCode;
152
153 if (options_.crash_point() == CrashPoint::kOnStartup)
154 exit(kDeliberateCrashExitCode);
155
156 mojo::edk::Init();
157 base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
158 base::Thread io_thread("IPCThread");
159 EXPECT_TRUE(io_thread.StartWithOptions(thread_options));
160 if (::testing::Test::HasFailure())
161 return kInternalTestFailureExitCode;
162
163 mojo::edk::ScopedIPCSupport ipc_support(
164 io_thread.task_runner(),
165 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN);
166
167 auto invitation =
168 mojo::edk::IncomingBrokerClientInvitation::AcceptFromCommandLine(
169 mojo::edk::TransportProtocol::kLegacy);
170 ChromePromptPtrInfo prompt_ptr_info(
171 invitation->ExtractMessagePipe(chrome_mojo_pipe_token_), 0);
172
173 if (options_.crash_point() == CrashPoint::kAfterConnection)
174 exit(kDeliberateCrashExitCode);
175
176 base::MessageLoop message_loop;
177 base::RunLoop run_loop;
178 // After the response from the parent process is received, this will post a
179 // task to unblock the child process's main thread.
180 auto quit_closure = base::BindOnce(
181 [](scoped_refptr<base::SequencedTaskRunner> main_runner,
182 base::Closure quit_closure) {
183 main_runner->PostTask(FROM_HERE, std::move(quit_closure));
184 },
185 base::SequencedTaskRunnerHandle::Get(),
186 base::Passed(run_loop.QuitClosure()));
187
188 io_thread.task_runner()->PostTask(
189 FROM_HERE,
190 base::BindOnce(&MockChromeCleanerProcess::SendScanResults,
191 base::Unretained(this), std::move(prompt_ptr_info),
192 base::Passed(&quit_closure)));
193
194 run_loop.Run();
195
196 EXPECT_NE(received_prompt_acceptance_, PromptAcceptance::UNSPECIFIED);
197 if (::testing::Test::HasFailure())
198 return kInternalTestFailureExitCode;
199 return options_.ExpectedExitCode(received_prompt_acceptance_);
200 }
201
202 void MockChromeCleanerProcess::SendScanResults(
203 ChromePromptPtrInfo prompt_ptr_info,
204 base::OnceClosure quit_closure) {
205 // This pointer will be deleted by PromptUserCallback.
206 chrome_prompt_ptr_ = new ChromePromptPtr();
207 chrome_prompt_ptr_->Bind(std::move(prompt_ptr_info));
208
209 if (options_.crash_point() == CrashPoint::kAfterRequestSent) {
210 // This task is posted to the IPC thread so that it will happen after the
211 // request is sent to the parent process and before the response gets
212 // handled on the IPC thread.
213 base::SequencedTaskRunnerHandle::Get()->PostTask(
214 FROM_HERE, base::Bind([]() { exit(kDeliberateCrashExitCode); }));
Joe Mason 2017/05/23 15:39:36 Nit: could avoid the lambda with "base::Bind(&exit
alito 2017/05/23 18:58:57 Done.
alito 2017/05/24 00:42:47 Turns out, the clang compiler does not like bindin
215 }
216
217 std::vector<UwSPtr> uws_found;
218 for (const UwSPtr& uws_ptr : options_.found_uws())
219 uws_found.push_back(uws_ptr->Clone());
220
221 (*chrome_prompt_ptr_)
222 ->PromptUser(
223 std::move(uws_found), ElevationStatus::REQUIRED,
224 base::BindOnce(&MockChromeCleanerProcess::PromptUserCallback,
225 base::Unretained(this), std::move(quit_closure)));
226 }
227
228 void MockChromeCleanerProcess::PromptUserCallback(
229 base::OnceClosure quit_closure,
230 PromptAcceptance prompt_acceptance) {
231 delete chrome_prompt_ptr_;
232 chrome_prompt_ptr_ = nullptr;
233
234 received_prompt_acceptance_ = prompt_acceptance;
235
236 if (options_.crash_point() == CrashPoint::kAfterResponseReceived)
237 exit(kDeliberateCrashExitCode);
238
239 std::move(quit_closure).Run();
240 }
241
242 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698