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 2014 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/common/data_pipe_utils.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <utility>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/message_loop/message_loop.h"
#include "base/task_runner_util.h"
namespace mojo {
namespace common {
namespace {
bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source,
const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
for (;;) {
const void* buffer;
uint32_t num_bytes;
MojoResult result = BeginReadDataRaw(
source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_OK) {
size_t bytes_written = write_bytes.Run(buffer, num_bytes);
result = EndReadDataRaw(source.get(), num_bytes);
if (bytes_written < num_bytes || result != MOJO_RESULT_OK)
return false;
} else if (result == MOJO_RESULT_SHOULD_WAIT) {
result = Wait(source.get(),
MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE,
nullptr);
if (result != MOJO_RESULT_OK) {
// If the producer handle was closed, then treat as EOF.
return result == MOJO_RESULT_FAILED_PRECONDITION;
}
} else if (result == MOJO_RESULT_FAILED_PRECONDITION) {
// If the producer handle was closed, then treat as EOF.
return true;
} else {
// Some other error occurred.
break;
}
}
return false;
}
size_t CopyToStringHelper(
std::string* result, const void* buffer, uint32_t num_bytes) {
result->append(static_cast<const char*>(buffer), num_bytes);
return num_bytes;
}
size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) {
return fwrite(buffer, 1, num_bytes, fp);
}
} // namespace
// TODO(hansmuller): Add a max_size parameter.
bool BlockingCopyToString(ScopedDataPipeConsumerHandle source,
std::string* result) {
CHECK(result);
result->clear();
return BlockingCopyHelper(std::move(source),
base::Bind(&CopyToStringHelper, result));
}
bool MOJO_COMMON_EXPORT BlockingCopyFromString(
const std::string& source,
const ScopedDataPipeProducerHandle& destination) {
auto it = source.begin();
for (;;) {
void* buffer = nullptr;
uint32_t buffer_num_bytes = 0;
MojoResult result =
BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes,
MOJO_WRITE_DATA_FLAG_NONE);
if (result == MOJO_RESULT_OK) {
char* char_buffer = static_cast<char*>(buffer);
uint32_t byte_index = 0;
while (it != source.end() && byte_index < buffer_num_bytes) {
char_buffer[byte_index++] = *it++;
}
EndWriteDataRaw(destination.get(), byte_index);
if (it == source.end())
return true;
} else if (result == MOJO_RESULT_SHOULD_WAIT) {
result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
MOJO_DEADLINE_INDEFINITE, nullptr);
if (result != MOJO_RESULT_OK) {
// If the consumer handle was closed, then treat as EOF.
return result == MOJO_RESULT_FAILED_PRECONDITION;
}
} else {
// If the consumer handle was closed, then treat as EOF.
return result == MOJO_RESULT_FAILED_PRECONDITION;
}
}
}
bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source,
const base::FilePath& destination) {
base::ScopedFILE fp(base::OpenFile(destination, "wb"));
if (!fp)
return false;
return BlockingCopyHelper(std::move(source),
base::Bind(&CopyToFileHelper, fp.get()));
}
void CopyToFile(ScopedDataPipeConsumerHandle source,
const base::FilePath& destination,
base::TaskRunner* task_runner,
const base::Callback<void(bool)>& callback) {
base::PostTaskAndReplyWithResult(
task_runner,
FROM_HERE,
base::Bind(&BlockingCopyToFile, base::Passed(&source), destination),
callback);
}
} // namespace common
} // namespace mojo
|