Commit 69619bec authored by Richard Li's avatar Richard Li Committed by Commit Bot

Change type for DataElement::buf_, bytes_.

This CL change the type of DataElement::buf_ and bytes_ from char to
uint8_t, which makes the conversion with its mojom type more convenience.

Change-Id: I1c60db81e16137ffe2e0d98d6efbecd8427fa206
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1636969Reviewed-by: default avatarLeon Han <leon.han@intel.com>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Richard Li <richard.li@intel.com>
Cr-Commit-Position: refs/heads/master@{#666147}
parent b14c0fa0
......@@ -44,7 +44,10 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
DataElement& operator=(DataElement&& other);
mojom::DataElementType type() const { return type_; }
const char* bytes() const { return bytes_ ? bytes_ : buf_.data(); }
const char* bytes() const {
return bytes_ ? reinterpret_cast<const char*>(bytes_)
: reinterpret_cast<const char*>(buf_.data());
}
const base::FilePath& path() const { return path_; }
const base::File& file() const { return file_; }
const std::string& blob_uuid() const { return blob_uuid_; }
......@@ -56,18 +59,19 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
// For use with SetToAllocatedBytes. Should only be used after calling
// SetToAllocatedBytes.
char* mutable_bytes() { return &buf_[0]; }
char* mutable_bytes() { return reinterpret_cast<char*>(&buf_[0]); }
// Sets TYPE_BYTES data. This copies the given data into the element.
void SetToBytes(const char* bytes, int bytes_len) {
type_ = mojom::DataElementType::kBytes;
bytes_ = nullptr;
buf_.assign(bytes, bytes + bytes_len);
buf_.assign(reinterpret_cast<const uint8_t*>(bytes),
reinterpret_cast<const uint8_t*>(bytes + bytes_len));
length_ = buf_.size();
}
// Sets TYPE_BYTES data. This moves the given data vector into the element.
void SetToBytes(std::vector<char> bytes) {
void SetToBytes(std::vector<uint8_t> bytes) {
type_ = mojom::DataElementType::kBytes;
bytes_ = nullptr;
buf_ = std::move(bytes);
......@@ -89,7 +93,8 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
DCHECK_EQ(type_, mojom::DataElementType::kBytes);
DCHECK_NE(length_, std::numeric_limits<uint64_t>::max());
DCHECK(!bytes_);
buf_.insert(buf_.end(), bytes, bytes + bytes_len);
buf_.insert(buf_.end(), reinterpret_cast<const uint8_t*>(bytes),
reinterpret_cast<const uint8_t*>(bytes + bytes_len));
length_ = buf_.size();
}
......@@ -98,7 +103,7 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
// You cannot use AppendBytes with this method.
void SetToSharedBytes(const char* bytes, int bytes_len) {
type_ = mojom::DataElementType::kBytes;
bytes_ = bytes;
bytes_ = reinterpret_cast<const uint8_t*>(bytes);
length_ = bytes_len;
}
......@@ -175,12 +180,10 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
friend struct mojo::StructTraits<network::mojom::DataElementDataView,
network::DataElement>;
mojom::DataElementType type_;
// TODO(Richard): Use uint8_t instead of char to align with mojom type
// For TYPE_BYTES.
std::vector<char> buf_;
// TODO(Richard): Use uint8_t instead of char to align with mojom type
std::vector<uint8_t> buf_;
// For TYPE_BYTES.
const char* bytes_;
const uint8_t* bytes_;
// For TYPE_FILE and TYPE_RAW_FILE.
base::FilePath path_;
// For TYPE_RAW_FILE.
......
......@@ -21,7 +21,7 @@ scoped_refptr<ResourceRequestBody> ResourceRequestBody::CreateFromBytes(
return result;
}
void ResourceRequestBody::AppendBytes(std::vector<char> bytes) {
void ResourceRequestBody::AppendBytes(std::vector<uint8_t> bytes) {
DCHECK(elements_.empty() ||
elements_.front().type() != mojom::DataElementType::kChunkedDataPipe);
......@@ -32,8 +32,9 @@ void ResourceRequestBody::AppendBytes(std::vector<char> bytes) {
}
void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) {
std::vector<char> vec;
vec.assign(bytes, bytes + bytes_len);
std::vector<uint8_t> vec;
vec.assign(reinterpret_cast<const uint8_t*>(bytes),
reinterpret_cast<const uint8_t*>(bytes + bytes_len));
AppendBytes(std::move(vec));
}
......
......@@ -31,7 +31,7 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) ResourceRequestBody
static scoped_refptr<ResourceRequestBody> CreateFromBytes(const char* bytes,
size_t length);
void AppendBytes(std::vector<char> bytes);
void AppendBytes(std::vector<uint8_t> bytes);
void AppendBytes(const char* bytes, int bytes_len);
void AppendFileRange(const base::FilePath& file_path,
uint64_t offset,
......
......@@ -233,12 +233,8 @@ bool StructTraits<network::mojom::DataElementDataView, network::DataElement>::
!data.ReadExpectedModificationTime(&out->expected_modification_time_)) {
return false;
}
// TODO(Richard): Fix this workaround once |buf_| becomes vector<uint8_t>
if (data.type() == network::mojom::DataElementType::kBytes) {
out->buf_.resize(data.length());
auto buf = base::make_span(reinterpret_cast<uint8_t*>(out->buf_.data()),
out->buf_.size());
if (!data.ReadBuf(&buf))
if (!data.ReadBuf(&out->buf_))
return false;
}
out->type_ = data.type();
......
......@@ -266,14 +266,12 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE)
const network::DataElement& element) {
return element.type_;
}
static base::span<const uint8_t> buf(const network::DataElement& element) {
static std::vector<uint8_t> buf(const network::DataElement& element) {
if (element.bytes_) {
return base::make_span(reinterpret_cast<const uint8_t*>(element.bytes_),
element.length_);
return std::vector<uint8_t>(element.bytes_,
element.bytes_ + element.length_);
}
return base::make_span(
reinterpret_cast<const uint8_t*>(element.buf_.data()),
element.buf_.size());
return std::move(element.buf_);
}
static const base::FilePath& path(const network::DataElement& element) {
return element.path_;
......
......@@ -81,7 +81,7 @@ class BLINK_PLATFORM_EXPORT WebData {
// Same as SharedBuffer::CopyAs, copies the segmented data into a
// contiguous buffer. Use GetSomeData() or ForEachSegment() whenever
// possible, if a copy can be avoided.
WebVector<char> Copy() const;
WebVector<uint8_t> Copy() const;
// Helper for applying a lambda to all data segments, sequentially:
//
......
......@@ -72,10 +72,10 @@ size_t WebData::GetSomeData(const char*& data, size_t position) const {
return it->size();
}
WebVector<char> WebData::Copy() const {
WebVector<uint8_t> WebData::Copy() const {
return private_.IsNull()
? WebVector<char>()
: WebVector<char>(private_->CopyAs<std::vector<char>>());
? WebVector<uint8_t>()
: WebVector<uint8_t>(private_->CopyAs<std::vector<uint8_t>>());
}
WebData::WebData(scoped_refptr<SharedBuffer> buffer)
......
......@@ -265,6 +265,19 @@ inline std::vector<char> SharedBuffer::CopyAs() const {
return buffer;
}
template <>
inline std::vector<uint8_t> SharedBuffer::CopyAs() const {
std::vector<uint8_t> buffer;
buffer.reserve(size_);
for (const auto& span : *this) {
buffer.insert(buffer.end(), reinterpret_cast<const uint8_t*>(span.data()),
reinterpret_cast<const uint8_t*>(span.data() + span.size()));
}
DCHECK_EQ(buffer.size(), size_);
return buffer;
}
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SHARED_BUFFER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment