forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_pipe_utils.cc
211 lines (180 loc) · 6.79 KB
/
data_pipe_utils.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2016 The Fuchsia 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 "flutter/glue/data_pipe_utils.h"
#include <stdio.h>
#include <unistd.h>
#include <algorithm>
#include <limits>
#include <utility>
#include "lib/ftl/files/file_descriptor.h"
#include "mojo/public/cpp/environment/async_waiter.h"
#include "mojo/public/cpp/environment/environment.h"
namespace glue {
namespace {
// CopyToFileHandler -----------------------------------------------------------
class CopyToFileHandler {
public:
CopyToFileHandler(mojo::ScopedDataPipeConsumerHandle source,
ftl::UniqueFD destination,
ftl::TaskRunner* task_runner,
const std::function<void(bool)>& callback);
private:
~CopyToFileHandler();
void SendCallback(bool value);
void OnHandleReady(MojoResult result);
static void WaitComplete(void* context, MojoResult result);
mojo::ScopedDataPipeConsumerHandle source_;
ftl::UniqueFD destination_;
ftl::RefPtr<ftl::TaskRunner> task_runner_;
std::function<void(bool)> callback_;
const MojoAsyncWaiter* waiter_;
MojoAsyncWaitID wait_id_;
FTL_DISALLOW_COPY_AND_ASSIGN(CopyToFileHandler);
};
CopyToFileHandler::CopyToFileHandler(mojo::ScopedDataPipeConsumerHandle source,
ftl::UniqueFD destination,
ftl::TaskRunner* task_runner,
const std::function<void(bool)>& callback)
: source_(std::move(source)),
destination_(std::move(destination)),
task_runner_(task_runner),
callback_(callback),
waiter_(mojo::Environment::GetDefaultAsyncWaiter()),
wait_id_(0) {
task_runner_->PostTask([this]() { OnHandleReady(MOJO_RESULT_OK); });
}
CopyToFileHandler::~CopyToFileHandler() {}
void CopyToFileHandler::SendCallback(bool value) {
FTL_DCHECK(!wait_id_);
auto callback = callback_;
delete this;
callback(value);
}
void CopyToFileHandler::OnHandleReady(MojoResult result) {
if (result == MOJO_RESULT_OK) {
const void* buffer = nullptr;
uint32_t size = 0;
result = BeginReadDataRaw(source_.get(), &buffer, &size,
MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_OK) {
bool write_success = ftl::WriteFileDescriptor(
destination_.get(), static_cast<const char*>(buffer), size);
result = EndReadDataRaw(source_.get(), size);
if (!write_success || result != MOJO_RESULT_OK) {
SendCallback(false);
} else {
task_runner_->PostTask([this]() { OnHandleReady(MOJO_RESULT_OK); });
}
return;
}
}
if (result == MOJO_RESULT_FAILED_PRECONDITION) {
SendCallback(true);
return;
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
wait_id_ =
waiter_->AsyncWait(source_.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE, &WaitComplete, this);
return;
}
SendCallback(false);
}
void CopyToFileHandler::WaitComplete(void* context, MojoResult result) {
CopyToFileHandler* handler = static_cast<CopyToFileHandler*>(context);
handler->wait_id_ = 0;
handler->OnHandleReady(result);
}
// CopyFromFileHandler ---------------------------------------------------------
class CopyFromFileHandler {
public:
CopyFromFileHandler(ftl::UniqueFD source,
mojo::ScopedDataPipeProducerHandle destination,
ftl::TaskRunner* task_runner,
const std::function<void(bool)>& callback);
private:
~CopyFromFileHandler();
void SendCallback(bool value);
void OnHandleReady(MojoResult result);
static void WaitComplete(void* context, MojoResult result);
ftl::UniqueFD source_;
mojo::ScopedDataPipeProducerHandle destination_;
ftl::RefPtr<ftl::TaskRunner> task_runner_;
std::function<void(bool)> callback_;
const MojoAsyncWaiter* waiter_;
MojoAsyncWaitID wait_id_;
FTL_DISALLOW_COPY_AND_ASSIGN(CopyFromFileHandler);
};
CopyFromFileHandler::CopyFromFileHandler(
ftl::UniqueFD source,
mojo::ScopedDataPipeProducerHandle destination,
ftl::TaskRunner* task_runner,
const std::function<void(bool)>& callback)
: source_(std::move(source)),
destination_(std::move(destination)),
task_runner_(task_runner),
callback_(callback),
waiter_(mojo::Environment::GetDefaultAsyncWaiter()),
wait_id_(0) {
task_runner_->PostTask([this]() { OnHandleReady(MOJO_RESULT_OK); });
}
CopyFromFileHandler::~CopyFromFileHandler() {}
void CopyFromFileHandler::SendCallback(bool value) {
FTL_DCHECK(!wait_id_);
auto callback = callback_;
delete this;
callback(value);
}
void CopyFromFileHandler::OnHandleReady(MojoResult result) {
if (result == MOJO_RESULT_OK) {
void* buffer = nullptr;
uint32_t size = 0;
result = BeginWriteDataRaw(destination_.get(), &buffer, &size,
MOJO_WRITE_DATA_FLAG_NONE);
if (result == MOJO_RESULT_OK) {
FTL_DCHECK(size < static_cast<uint32_t>(std::numeric_limits<int>::max()));
ssize_t bytes_read = ftl::ReadFileDescriptor(
source_.get(), static_cast<char*>(buffer), size);
result = EndWriteDataRaw(destination_.get(),
std::max<ssize_t>(0l, bytes_read));
if (bytes_read == -1 || result != MOJO_RESULT_OK) {
SendCallback(false);
} else if (bytes_read < size) {
// Reached EOF. Stop the process.
SendCallback(true);
} else {
task_runner_->PostTask([this]() { OnHandleReady(MOJO_RESULT_OK); });
}
return;
}
}
if (result == MOJO_RESULT_SHOULD_WAIT) {
wait_id_ = waiter_->AsyncWait(
destination_.get().value(), MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_DEADLINE_INDEFINITE, &WaitComplete, this);
return;
}
SendCallback(false);
}
void CopyFromFileHandler::WaitComplete(void* context, MojoResult result) {
CopyFromFileHandler* handler = static_cast<CopyFromFileHandler*>(context);
handler->wait_id_ = 0;
handler->OnHandleReady(result);
}
} // namespace
void CopyToFileDescriptor(mojo::ScopedDataPipeConsumerHandle source,
ftl::UniqueFD destination,
ftl::TaskRunner* task_runner,
const std::function<void(bool)>& callback) {
new CopyToFileHandler(std::move(source), std::move(destination), task_runner,
callback);
}
void CopyFromFileDescriptor(ftl::UniqueFD source,
mojo::ScopedDataPipeProducerHandle destination,
ftl::TaskRunner* task_runner,
const std::function<void(bool)>& callback) {
new CopyFromFileHandler(std::move(source), std::move(destination),
task_runner, callback);
}
} // namespace glue