Skip to content

Commit

Permalink
CrossThreadHandle: Use in ClipBoard{Reader,Write}
Browse files Browse the repository at this point in the history
Rewrite safe usage of CrossThreadPersistent with CrossThreadHandle.

Drive-by: Rewrite usages of CTP for DOMArrayBuffer with using
ArrayBufferContents which is a thread-safe smart pointer around the
backing store.

Bug: 1370013
Change-Id: I8fb144901949d5d18c88ae76bc01249650ca8d8b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3921741
Commit-Queue: Michael Lippautz <[email protected]>
Reviewed-by: Kentaro Hara <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1054641}
NOKEYCHECK=True
GitOrigin-RevId: c37933f86e2920473f233141d16515abeceaf5c9
  • Loading branch information
mlippautz authored and copybara-github committed Oct 4, 2022
1 parent b883cf7 commit b0108a7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 49 deletions.
62 changes: 33 additions & 29 deletions blink/renderer/modules/clipboard/clipboard_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "third_party/blink/renderer/modules/clipboard/clipboard.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_promise.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_writer.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_handle.h"
#include "third_party/blink/renderer/platform/image-encoders/image_encoder.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
Expand Down Expand Up @@ -83,15 +84,15 @@ class ClipboardTextReader final : public ClipboardReader {
}

worker_pool::PostTask(
FROM_HERE, CrossThreadBindOnce(
&ClipboardTextReader::EncodeOnBackgroundThread,
std::move(plain_text), WrapCrossThreadPersistent(this),
std::move(clipboard_task_runner_)));
FROM_HERE,
CrossThreadBindOnce(&ClipboardTextReader::EncodeOnBackgroundThread,
std::move(plain_text), MakeCrossThreadHandle(this),
std::move(clipboard_task_runner_)));
}

static void EncodeOnBackgroundThread(
String plain_text,
ClipboardTextReader* reader,
CrossThreadHandle<ClipboardTextReader> reader,
scoped_refptr<base::SingleThreadTaskRunner> clipboard_task_runner) {
DCHECK(!IsMainThread());

Expand All @@ -101,10 +102,13 @@ class ClipboardTextReader final : public ClipboardReader {
utf8_bytes.ReserveInitialCapacity(utf8_text.size());
utf8_bytes.Append(utf8_text.data(), utf8_text.size());

PostCrossThreadTask(*clipboard_task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardTextReader::NextRead,
WrapCrossThreadPersistent(reader),
std::move(utf8_bytes)));
PostCrossThreadTask(
*clipboard_task_runner, FROM_HERE,
CrossThreadBindOnce(
&ClipboardTextReader::NextRead,
MakeUnwrappingCrossThreadHandle<ClipboardTextReader>(
std::move(reader)),
std::move(utf8_bytes)));
}

void NextRead(Vector<uint8_t> utf8_bytes) override {
Expand Down Expand Up @@ -160,16 +164,15 @@ class ClipboardHtmlReader final : public ClipboardReader {
return;
}
worker_pool::PostTask(
FROM_HERE,
CrossThreadBindOnce(&ClipboardHtmlReader::EncodeOnBackgroundThread,
std::move(sanitized_html),
WrapCrossThreadPersistent(this),
std::move(clipboard_task_runner_)));
FROM_HERE, CrossThreadBindOnce(
&ClipboardHtmlReader::EncodeOnBackgroundThread,
std::move(sanitized_html), MakeCrossThreadHandle(this),
std::move(clipboard_task_runner_)));
}

static void EncodeOnBackgroundThread(
String plain_text,
ClipboardHtmlReader* reader,
CrossThreadHandle<ClipboardHtmlReader> reader,
scoped_refptr<base::SingleThreadTaskRunner> clipboard_task_runner) {
DCHECK(!IsMainThread());

Expand All @@ -179,10 +182,11 @@ class ClipboardHtmlReader final : public ClipboardReader {
utf8_bytes.ReserveInitialCapacity(utf8_text.size());
utf8_bytes.Append(utf8_text.data(), utf8_text.size());

PostCrossThreadTask(*clipboard_task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardHtmlReader::NextRead,
WrapCrossThreadPersistent(reader),
std::move(utf8_bytes)));
PostCrossThreadTask(
*clipboard_task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardHtmlReader::NextRead,
MakeUnwrappingCrossThreadHandle(std::move(reader)),
std::move(utf8_bytes)));
}

void NextRead(Vector<uint8_t> utf8_bytes) override {
Expand Down Expand Up @@ -234,16 +238,15 @@ class ClipboardSvgReader final : public ClipboardReader {
return;
}
worker_pool::PostTask(
FROM_HERE,
CrossThreadBindOnce(&ClipboardSvgReader::EncodeOnBackgroundThread,
std::move(sanitized_svg),
WrapCrossThreadPersistent(this),
std::move(clipboard_task_runner_)));
FROM_HERE, CrossThreadBindOnce(
&ClipboardSvgReader::EncodeOnBackgroundThread,
std::move(sanitized_svg), MakeCrossThreadHandle(this),
std::move(clipboard_task_runner_)));
}

static void EncodeOnBackgroundThread(
String plain_text,
ClipboardSvgReader* reader,
CrossThreadHandle<ClipboardSvgReader> reader,
scoped_refptr<base::SingleThreadTaskRunner> clipboard_task_runner) {
DCHECK(!IsMainThread());

Expand All @@ -253,10 +256,11 @@ class ClipboardSvgReader final : public ClipboardReader {
utf8_bytes.ReserveInitialCapacity(utf8_text.size());
utf8_bytes.Append(utf8_text.data(), utf8_text.size());

PostCrossThreadTask(*clipboard_task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardSvgReader::NextRead,
WrapCrossThreadPersistent(reader),
std::move(utf8_bytes)));
PostCrossThreadTask(
*clipboard_task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardSvgReader::NextRead,
MakeUnwrappingCrossThreadHandle(std::move(reader)),
std::move(utf8_bytes)));
}

void NextRead(Vector<uint8_t> utf8_bytes) override {
Expand Down
49 changes: 29 additions & 20 deletions blink/renderer/modules/clipboard/clipboard_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/typed_arrays/array_buffer/array_buffer_contents.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_handle.h"
#include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
Expand Down Expand Up @@ -44,31 +46,35 @@ class ClipboardImageWriter final : public ClipboardWriter {
scoped_refptr<base::SingleThreadTaskRunner> task_runner) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

// ArrayBufferContents is a thread-safe smart pointer around the backing
// store.
ArrayBufferContents contents = *raw_data->Content();
worker_pool::PostTask(
FROM_HERE,
CrossThreadBindOnce(&ClipboardImageWriter::DecodeOnBackgroundThread,
WrapCrossThreadPersistent(raw_data),
WrapCrossThreadPersistent(this), task_runner));
std::move(contents), MakeCrossThreadHandle(this),
task_runner));
}
static void DecodeOnBackgroundThread(
DOMArrayBuffer* png_data,
ClipboardImageWriter* writer,
ArrayBufferContents png_data,
CrossThreadHandle<ClipboardImageWriter> writer,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(!IsMainThread());
std::unique_ptr<ImageDecoder> decoder = ImageDecoder::Create(
SegmentReader::CreateFromSkData(
SkData::MakeWithoutCopy(png_data->Data(), png_data->ByteLength())),
SkData::MakeWithoutCopy(png_data.Data(), png_data.DataLength())),
true, ImageDecoder::kAlphaPremultiplied, ImageDecoder::kDefaultBitDepth,
ColorBehavior::Tag());
sk_sp<SkImage> image = nullptr;
// `decoder` is nullptr if `png_data` doesn't begin with the PNG signature.
if (decoder)
image = ImageBitmap::GetSkImageFromDecoder(std::move(decoder));

PostCrossThreadTask(*task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardImageWriter::Write,
WrapCrossThreadPersistent(writer),
std::move(image)));
PostCrossThreadTask(
*task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardImageWriter::Write,
MakeUnwrappingCrossThreadHandle(std::move(writer)),
std::move(image)));
}
void Write(sk_sp<SkImage> image) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Expand Down Expand Up @@ -99,25 +105,28 @@ class ClipboardTextWriter final : public ClipboardWriter {
scoped_refptr<base::SingleThreadTaskRunner> task_runner) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

// ArrayBufferContents is a thread-safe smart pointer around the backing
// store.
ArrayBufferContents contents = *raw_data->Content();
worker_pool::PostTask(
FROM_HERE,
CrossThreadBindOnce(&ClipboardTextWriter::DecodeOnBackgroundThread,
WrapCrossThreadPersistent(raw_data),
WrapCrossThreadPersistent(this), task_runner));
std::move(contents), MakeCrossThreadHandle(this),
task_runner));
}
static void DecodeOnBackgroundThread(
DOMArrayBuffer* raw_data,
ClipboardTextWriter* writer,
ArrayBufferContents raw_data,
CrossThreadHandle<ClipboardTextWriter> writer,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(!IsMainThread());

String wtf_string =
String::FromUTF8(reinterpret_cast<const LChar*>(raw_data->Data()),
raw_data->ByteLength());
PostCrossThreadTask(*task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardTextWriter::Write,
WrapCrossThreadPersistent(writer),
std::move(wtf_string)));
String wtf_string = String::FromUTF8(
reinterpret_cast<const LChar*>(raw_data.Data()), raw_data.DataLength());
PostCrossThreadTask(
*task_runner, FROM_HERE,
CrossThreadBindOnce(&ClipboardTextWriter::Write,
MakeUnwrappingCrossThreadHandle(std::move(writer)),
std::move(wtf_string)));
}
void Write(const String& text) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Expand Down

0 comments on commit b0108a7

Please sign in to comment.