forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmojo_chrome_prompt_ipc.cc
168 lines (142 loc) · 6.22 KB
/
mojo_chrome_prompt_ipc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/chrome_cleaner/ipc/mojo_chrome_prompt_ipc.h"
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/check_op.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "mojo/public/cpp/system/message_pipe.h"
namespace chrome_cleaner {
MojoChromePromptIPC::MojoChromePromptIPC(
const std::string& chrome_mojo_pipe_token,
scoped_refptr<MojoTaskRunner> task_runner)
: task_runner_(std::move(task_runner)),
chrome_mojo_pipe_token_(chrome_mojo_pipe_token) {
// Accesses to |state_| must happen on |task_runner_|'s sequence, which is
// not the construction sequence.
DETACH_FROM_SEQUENCE(sequence_checker_);
}
void MojoChromePromptIPC::Initialize(ErrorHandler* error_handler) {
DCHECK(!chrome_mojo_pipe_token_.empty());
DCHECK(task_runner_);
error_handler_ = error_handler;
// No need to retain this object, since it will live until the process
// finishes.
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&MojoChromePromptIPC::InitializeChromePromptPtr,
base::Unretained(this)));
}
MojoChromePromptIPC::~MojoChromePromptIPC() {
// This object is only destroyed when the cleaner process ends. At this point
// we don't need to close the IPC, since the broken connection message will
// be received by Chrome anyway, and we can simply leak the pointer.
chrome_prompt_service_.release(); // Leaked.
}
void MojoChromePromptIPC::PostPromptUserTask(
const std::vector<base::FilePath>& files_to_delete,
const std::vector<std::wstring>& registry_keys,
const std::vector<std::wstring>& extension_ids,
PromptUserCallback callback) {
DCHECK(task_runner_);
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&MojoChromePromptIPC::RunPromptUserTask,
base::Unretained(this), files_to_delete, registry_keys,
extension_ids, std::move(callback)));
}
void MojoChromePromptIPC::OnChromeResponseReceived(
PromptUserCallback callback,
mojom::PromptAcceptance prompt_acceptance) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(State::kWaitingForResponseFromChrome, state_);
state_ = State::kDoneInteraction;
std::move(callback).Run(
static_cast<PromptUserResponse::PromptAcceptance>(prompt_acceptance));
}
void MojoChromePromptIPC::OnConnectionError() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_NE(State::kUninitialized, state_);
if (!error_handler_)
return;
if (state_ == State::kDoneInteraction) {
error_handler_->OnConnectionClosedAfterDone();
} else {
state_ = State::kDoneInteraction;
error_handler_->OnConnectionClosed();
}
}
void MojoChromePromptIPC::InitializeChromePromptPtr() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_EQ(State::kUninitialized, state_);
auto channel_endpoint =
mojo::PlatformChannel::RecoverPassedEndpointFromCommandLine(
*base::CommandLine::ForCurrentProcess());
auto incoming_invitation =
mojo::IncomingInvitation::Accept(std::move(channel_endpoint));
mojo::ScopedMessagePipeHandle message_pipe_handle =
incoming_invitation.ExtractMessagePipe(chrome_mojo_pipe_token_);
mojo::PendingRemote<chrome_cleaner::mojom::ChromePrompt> pending_remote(
std::move(message_pipe_handle), /*version=*/0);
chrome_prompt_service_ =
std::make_unique<mojo::Remote<chrome_cleaner::mojom::ChromePrompt>>(
std::move(pending_remote));
// No need to retain this object, since it will live until the process
// finishes.
chrome_prompt_service_->set_disconnect_handler(base::BindOnce(
&MojoChromePromptIPC::OnConnectionError, base::Unretained(this)));
state_ = State::kWaitingForScanResults;
}
void MojoChromePromptIPC::PromptUserCheckVersion(
const std::vector<base::FilePath>& files_to_delete,
const std::vector<std::wstring>& registry_keys,
const std::vector<std::wstring>& extension_ids,
mojom::ChromePrompt::PromptUserCallback callback,
uint32_t version) {
if (version >= 3) {
(*chrome_prompt_service_)
->PromptUser(std::move(files_to_delete), std::move(registry_keys),
std::move(extension_ids), std::move(callback));
} else {
// Before version 3 the delete extensions interface wasn't implemented.
// So we need to not notify the user that there are extensions to be
// deleted.
(*chrome_prompt_service_)
->PromptUser(std::move(files_to_delete), std::move(registry_keys),
absl::nullopt, std::move(callback));
}
}
void MojoChromePromptIPC::RunPromptUserTask(
const std::vector<base::FilePath>& files_to_delete,
const std::vector<std::wstring>& registry_keys,
const std::vector<std::wstring>& extension_ids,
PromptUserCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(chrome_prompt_service_);
DCHECK(state_ != State::kUninitialized);
DCHECK(state_ != State::kWaitingForResponseFromChrome);
// This is a corner case, in which we receive the disconnect message on the
// IPC thread right before this task is posted. In that case, this function
// will be a no-op.
if (state_ == State::kDoneInteraction)
return;
state_ = State::kWaitingForResponseFromChrome;
// Mojo will invoke this callback when a response is received. The
// |prompt_acceptance| parameter is unbound and will be filled in by Mojo.
mojom::ChromePrompt::PromptUserCallback response_callback =
base::BindOnce(&MojoChromePromptIPC::OnChromeResponseReceived,
base::Unretained(this), std::move(callback));
(*chrome_prompt_service_)
.QueryVersion(base::BindOnce(
&MojoChromePromptIPC::PromptUserCheckVersion, base::Unretained(this),
std::move(files_to_delete), std::move(registry_keys),
std::move(extension_ids), std::move(response_callback)));
}
} // namespace chrome_cleaner