Commit b203b4e1 authored by Joel Hockey's avatar Joel Hockey Committed by Commit Bot

Add base::RefCountedString16

Move RefCountedString16 from exo DataOffer to base where it can be
reused.

Change-Id: I0a3e13b006d690be1605c90da6d46274c4811685
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2507235
Commit-Queue: Mitsuru Oshima <oshima@chromium.org>
Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Auto-Submit: Joel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822791}
parent 00838e08
......@@ -83,6 +83,26 @@ size_t RefCountedString::size() const {
return data_.size();
}
RefCountedString16::RefCountedString16() = default;
RefCountedString16::~RefCountedString16() = default;
// static
scoped_refptr<RefCountedString16> RefCountedString16::TakeString(
string16* to_destroy) {
auto self = MakeRefCounted<RefCountedString16>();
to_destroy->swap(self->data_);
return self;
}
const unsigned char* RefCountedString16::front() const {
return reinterpret_cast<const unsigned char*>(data_.data());
}
size_t RefCountedString16::size() const {
return data_.size() * sizeof(char16);
}
RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
ReadOnlySharedMemoryMapping mapping)
: mapping_(std::move(mapping)), size_(mapping_.size()) {
......
......@@ -15,6 +15,7 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/strings/string16.h"
namespace base {
......@@ -144,6 +145,28 @@ class BASE_EXPORT RefCountedString : public RefCountedMemory {
DISALLOW_COPY_AND_ASSIGN(RefCountedString);
};
// An implementation of RefCountedMemory, where the bytes are stored in a
// string16.
class BASE_EXPORT RefCountedString16 : public base::RefCountedMemory {
public:
RefCountedString16();
// Constructs a RefCountedString16 object by performing a swap.
static scoped_refptr<RefCountedString16> TakeString(string16* to_destroy);
// RefCountedMemory:
const unsigned char* front() const override;
size_t size() const override;
protected:
~RefCountedString16() override;
private:
string16 data_;
DISALLOW_COPY_AND_ASSIGN(RefCountedString16);
};
// An implementation of RefCountedMemory, where the bytes are stored in
// ReadOnlySharedMemoryMapping.
class BASE_EXPORT RefCountedSharedMemoryMapping : public RefCountedMemory {
......
......@@ -44,28 +44,6 @@ constexpr char kUriListSeparator[] = "\r\n";
constexpr char kUTF8[] = "utf8";
constexpr char kUTF16[] = "utf16";
class RefCountedString16 : public base::RefCountedMemory {
public:
static scoped_refptr<RefCountedString16> TakeString(
base::string16&& to_destroy) {
scoped_refptr<RefCountedString16> self(new RefCountedString16);
to_destroy.swap(self->data_);
return self;
}
// Overridden from base::RefCountedMemory:
const unsigned char* front() const override {
return reinterpret_cast<const unsigned char*>(data_.data());
}
size_t size() const override { return data_.size() * sizeof(base::char16); }
protected:
~RefCountedString16() override {}
private:
base::string16 data_;
};
void WriteFileDescriptorOnWorkerThread(
base::ScopedFD fd,
scoped_refptr<base::RefCountedMemory> memory) {
......@@ -259,7 +237,7 @@ void DataOffer::SetDropData(FileHelper* file_helper,
base::string16 url_list_string;
if (GetUrlListFromDataFile(file_helper, data, &url_list_string)) {
data_.emplace(uri_list_mime_type,
RefCountedString16::TakeString(std::move(url_list_string)));
base::RefCountedString16::TakeString(&url_list_string));
delegate_->OnOffer(uri_list_mime_type);
return;
}
......@@ -374,7 +352,7 @@ void DataOffer::OnPickledUrlsResolved(const std::string& mime_type,
url_list_string += base::UTF8ToUTF16(url.spec());
}
const auto ref_counted_memory =
RefCountedString16::TakeString(std::move(url_list_string));
base::RefCountedString16::TakeString(&url_list_string);
data_.emplace(mime_type, ref_counted_memory);
// Process pending receive requests for this mime type, if there are any.
......
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