forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_data_pipe_producer.cc
131 lines (108 loc) · 4.07 KB
/
string_data_pipe_producer.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
// Copyright 2017 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 "mojo/public/cpp/system/string_data_pipe_producer.h"
#include <algorithm>
#include "base/bind.h"
#include "base/callback.h"
#include "base/location.h"
#include "base/task_scheduler/post_task.h"
namespace mojo {
namespace {
// Attempts to write data to a producer handle. Outputs the actual number of
// bytes written in |*size|, and returns a result indicating the status of the
// last attempted write operation.
MojoResult WriteDataToProducerHandle(DataPipeProducerHandle producer,
const char* data,
size_t* size) {
void* dest;
uint32_t bytes_left = static_cast<uint32_t>(*size);
// We loop here since the pipe's available capacity may be larger than its
// *contiguous* capacity, and hence two independent consecutive two-phase
// writes may succeed. The goal here is to write as much of |data| as possible
// until we either run out of data or run out of capacity.
MojoResult result;
do {
uint32_t capacity = bytes_left;
result =
producer.BeginWriteData(&dest, &capacity, MOJO_WRITE_DATA_FLAG_NONE);
if (result == MOJO_RESULT_SHOULD_WAIT) {
result = MOJO_RESULT_OK;
break;
} else if (result != MOJO_RESULT_OK) {
break;
}
capacity = std::min(capacity, bytes_left);
memcpy(dest, data, capacity);
MojoResult end_result = producer.EndWriteData(capacity);
DCHECK_EQ(MOJO_RESULT_OK, end_result);
data += capacity;
bytes_left -= capacity;
} while (bytes_left);
*size -= bytes_left;
return result;
}
} // namespace
StringDataPipeProducer::StringDataPipeProducer(
ScopedDataPipeProducerHandle producer)
: producer_(std::move(producer)),
watcher_(FROM_HERE, SimpleWatcher::ArmingPolicy::AUTOMATIC),
weak_factory_(this) {}
StringDataPipeProducer::~StringDataPipeProducer() = default;
void StringDataPipeProducer::Write(const base::StringPiece& data,
CompletionCallback callback) {
DCHECK(!callback_);
callback_ = std::move(callback);
// Immediately attempt to write data without making an extra copy. If we can
// get it all in one shot, we're done aleady.
size_t size = data.size();
MojoResult result =
WriteDataToProducerHandle(producer_.get(), data.data(), &size);
if (result == MOJO_RESULT_OK && size == data.size()) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&StringDataPipeProducer::InvokeCallback,
weak_factory_.GetWeakPtr(), MOJO_RESULT_OK));
} else {
// Copy whatever data didn't make the cut and try again when the pipe has
// some more capacity.
data_ = std::string(data.data() + size, data.size() - size);
data_view_ = data_;
watcher_.Watch(producer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_WATCH_CONDITION_SATISFIED,
base::Bind(&StringDataPipeProducer::OnProducerHandleReady,
base::Unretained(this)));
}
}
void StringDataPipeProducer::InvokeCallback(MojoResult result) {
// May delete |this|.
std::move(callback_).Run(result);
}
void StringDataPipeProducer::OnProducerHandleReady(
MojoResult ready_result,
const HandleSignalsState& state) {
bool failed = false;
size_t size = data_view_.size();
if (ready_result == MOJO_RESULT_OK) {
MojoResult write_result =
WriteDataToProducerHandle(producer_.get(), data_view_.data(), &size);
if (write_result != MOJO_RESULT_OK)
failed = true;
} else {
failed = true;
}
if (failed) {
watcher_.Cancel();
// May delete |this|.
std::move(callback_).Run(MOJO_RESULT_ABORTED);
return;
}
if (size == data_view_.size()) {
watcher_.Cancel();
// May delete |this|.
std::move(callback_).Run(MOJO_RESULT_OK);
return;
}
data_view_ =
base::StringPiece(data_view_.data() + size, data_view_.size() - size);
}
} // namespace mojo