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 { ...@@ -44,7 +44,10 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
DataElement& operator=(DataElement&& other); DataElement& operator=(DataElement&& other);
mojom::DataElementType type() const { return type_; } 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::FilePath& path() const { return path_; }
const base::File& file() const { return file_; } const base::File& file() const { return file_; }
const std::string& blob_uuid() const { return blob_uuid_; } const std::string& blob_uuid() const { return blob_uuid_; }
...@@ -56,18 +59,19 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement { ...@@ -56,18 +59,19 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
// For use with SetToAllocatedBytes. Should only be used after calling // For use with SetToAllocatedBytes. Should only be used after calling
// SetToAllocatedBytes. // 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. // Sets TYPE_BYTES data. This copies the given data into the element.
void SetToBytes(const char* bytes, int bytes_len) { void SetToBytes(const char* bytes, int bytes_len) {
type_ = mojom::DataElementType::kBytes; type_ = mojom::DataElementType::kBytes;
bytes_ = nullptr; 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(); length_ = buf_.size();
} }
// Sets TYPE_BYTES data. This moves the given data vector into the element. // 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; type_ = mojom::DataElementType::kBytes;
bytes_ = nullptr; bytes_ = nullptr;
buf_ = std::move(bytes); buf_ = std::move(bytes);
...@@ -89,7 +93,8 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement { ...@@ -89,7 +93,8 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
DCHECK_EQ(type_, mojom::DataElementType::kBytes); DCHECK_EQ(type_, mojom::DataElementType::kBytes);
DCHECK_NE(length_, std::numeric_limits<uint64_t>::max()); DCHECK_NE(length_, std::numeric_limits<uint64_t>::max());
DCHECK(!bytes_); 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(); length_ = buf_.size();
} }
...@@ -98,7 +103,7 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement { ...@@ -98,7 +103,7 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
// You cannot use AppendBytes with this method. // You cannot use AppendBytes with this method.
void SetToSharedBytes(const char* bytes, int bytes_len) { void SetToSharedBytes(const char* bytes, int bytes_len) {
type_ = mojom::DataElementType::kBytes; type_ = mojom::DataElementType::kBytes;
bytes_ = bytes; bytes_ = reinterpret_cast<const uint8_t*>(bytes);
length_ = bytes_len; length_ = bytes_len;
} }
...@@ -175,12 +180,10 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement { ...@@ -175,12 +180,10 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) DataElement {
friend struct mojo::StructTraits<network::mojom::DataElementDataView, friend struct mojo::StructTraits<network::mojom::DataElementDataView,
network::DataElement>; network::DataElement>;
mojom::DataElementType type_; mojom::DataElementType type_;
// TODO(Richard): Use uint8_t instead of char to align with mojom type
// For TYPE_BYTES. // For TYPE_BYTES.
std::vector<char> buf_; std::vector<uint8_t> buf_;
// TODO(Richard): Use uint8_t instead of char to align with mojom type
// For TYPE_BYTES. // For TYPE_BYTES.
const char* bytes_; const uint8_t* bytes_;
// For TYPE_FILE and TYPE_RAW_FILE. // For TYPE_FILE and TYPE_RAW_FILE.
base::FilePath path_; base::FilePath path_;
// For TYPE_RAW_FILE. // For TYPE_RAW_FILE.
......
...@@ -21,7 +21,7 @@ scoped_refptr<ResourceRequestBody> ResourceRequestBody::CreateFromBytes( ...@@ -21,7 +21,7 @@ scoped_refptr<ResourceRequestBody> ResourceRequestBody::CreateFromBytes(
return result; return result;
} }
void ResourceRequestBody::AppendBytes(std::vector<char> bytes) { void ResourceRequestBody::AppendBytes(std::vector<uint8_t> bytes) {
DCHECK(elements_.empty() || DCHECK(elements_.empty() ||
elements_.front().type() != mojom::DataElementType::kChunkedDataPipe); elements_.front().type() != mojom::DataElementType::kChunkedDataPipe);
...@@ -32,8 +32,9 @@ void ResourceRequestBody::AppendBytes(std::vector<char> bytes) { ...@@ -32,8 +32,9 @@ void ResourceRequestBody::AppendBytes(std::vector<char> bytes) {
} }
void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) { void ResourceRequestBody::AppendBytes(const char* bytes, int bytes_len) {
std::vector<char> vec; std::vector<uint8_t> vec;
vec.assign(bytes, bytes + bytes_len); vec.assign(reinterpret_cast<const uint8_t*>(bytes),
reinterpret_cast<const uint8_t*>(bytes + bytes_len));
AppendBytes(std::move(vec)); AppendBytes(std::move(vec));
} }
......
...@@ -31,7 +31,7 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) ResourceRequestBody ...@@ -31,7 +31,7 @@ class COMPONENT_EXPORT(NETWORK_CPP_BASE) ResourceRequestBody
static scoped_refptr<ResourceRequestBody> CreateFromBytes(const char* bytes, static scoped_refptr<ResourceRequestBody> CreateFromBytes(const char* bytes,
size_t length); 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 AppendBytes(const char* bytes, int bytes_len);
void AppendFileRange(const base::FilePath& file_path, void AppendFileRange(const base::FilePath& file_path,
uint64_t offset, uint64_t offset,
......
...@@ -233,12 +233,8 @@ bool StructTraits<network::mojom::DataElementDataView, network::DataElement>:: ...@@ -233,12 +233,8 @@ bool StructTraits<network::mojom::DataElementDataView, network::DataElement>::
!data.ReadExpectedModificationTime(&out->expected_modification_time_)) { !data.ReadExpectedModificationTime(&out->expected_modification_time_)) {
return false; return false;
} }
// TODO(Richard): Fix this workaround once |buf_| becomes vector<uint8_t>
if (data.type() == network::mojom::DataElementType::kBytes) { if (data.type() == network::mojom::DataElementType::kBytes) {
out->buf_.resize(data.length()); if (!data.ReadBuf(&out->buf_))
auto buf = base::make_span(reinterpret_cast<uint8_t*>(out->buf_.data()),
out->buf_.size());
if (!data.ReadBuf(&buf))
return false; return false;
} }
out->type_ = data.type(); out->type_ = data.type();
......
...@@ -266,14 +266,12 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE) ...@@ -266,14 +266,12 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE)
const network::DataElement& element) { const network::DataElement& element) {
return element.type_; 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_) { if (element.bytes_) {
return base::make_span(reinterpret_cast<const uint8_t*>(element.bytes_), return std::vector<uint8_t>(element.bytes_,
element.length_); element.bytes_ + element.length_);
} }
return base::make_span( return std::move(element.buf_);
reinterpret_cast<const uint8_t*>(element.buf_.data()),
element.buf_.size());
} }
static const base::FilePath& path(const network::DataElement& element) { static const base::FilePath& path(const network::DataElement& element) {
return element.path_; return element.path_;
......
...@@ -81,7 +81,7 @@ class BLINK_PLATFORM_EXPORT WebData { ...@@ -81,7 +81,7 @@ class BLINK_PLATFORM_EXPORT WebData {
// Same as SharedBuffer::CopyAs, copies the segmented data into a // Same as SharedBuffer::CopyAs, copies the segmented data into a
// contiguous buffer. Use GetSomeData() or ForEachSegment() whenever // contiguous buffer. Use GetSomeData() or ForEachSegment() whenever
// possible, if a copy can be avoided. // 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: // 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 { ...@@ -72,10 +72,10 @@ size_t WebData::GetSomeData(const char*& data, size_t position) const {
return it->size(); return it->size();
} }
WebVector<char> WebData::Copy() const { WebVector<uint8_t> WebData::Copy() const {
return private_.IsNull() return private_.IsNull()
? WebVector<char>() ? WebVector<uint8_t>()
: WebVector<char>(private_->CopyAs<std::vector<char>>()); : WebVector<uint8_t>(private_->CopyAs<std::vector<uint8_t>>());
} }
WebData::WebData(scoped_refptr<SharedBuffer> buffer) WebData::WebData(scoped_refptr<SharedBuffer> buffer)
......
...@@ -265,6 +265,19 @@ inline std::vector<char> SharedBuffer::CopyAs() const { ...@@ -265,6 +265,19 @@ inline std::vector<char> SharedBuffer::CopyAs() const {
return buffer; 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 } // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SHARED_BUFFER_H_ #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