Commit c1768757 authored by Darwin Huang's avatar Darwin Huang Committed by Chromium LUCI CQ

Clipboard API: Add early returns and comments.

Add early returns when returning to the main thread, to ensure the
ExecutionContext or LocalFrame is always valid before use.
Also, add some comments for consistency between subclasses.

Change-Id: I6abe00c90405c44214a45c8b5d02e070db767956
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2551304
Commit-Queue: Darwin Huang <huangdarwin@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Auto-Submit: Darwin Huang <huangdarwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836960}
parent 5e2d4287
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
#include "third_party/blink/renderer/core/fileapi/blob.h" #include "third_party/blink/renderer/core/fileapi/blob.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_item.h" #include "third_party/blink/renderer/modules/clipboard/clipboard_item.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_reader.h" #include "third_party/blink/renderer/modules/clipboard/clipboard_reader.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_writer.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
namespace blink { namespace blink {
class ClipboardWriter;
class ScriptPromiseResolver; class ScriptPromiseResolver;
class LocalFrame; class LocalFrame;
class ExecutionContext; class ExecutionContext;
......
...@@ -221,7 +221,7 @@ class ClipboardHtmlReader final : public ClipboardReader { ...@@ -221,7 +221,7 @@ class ClipboardHtmlReader final : public ClipboardReader {
} }
}; };
// Reads SVG from the System Clipboard as a Blob with image/svg content. // Reads SVG from the System Clipboard as a Blob with image/svg+xml content.
class ClipboardSvgReader final : public ClipboardReader { class ClipboardSvgReader final : public ClipboardReader {
public: public:
ClipboardSvgReader(SystemClipboard* system_clipboard, ClipboardSvgReader(SystemClipboard* system_clipboard,
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include "third_party/blink/renderer/core/fileapi/file_reader_loader.h" #include "third_party/blink/renderer/core/fileapi/file_reader_loader.h"
#include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h" #include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_promise.h"
#include "third_party/blink/renderer/platform/image-decoders/image_decoder.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/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h" #include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
...@@ -74,6 +73,8 @@ class ClipboardImageWriter final : public ClipboardWriter { ...@@ -74,6 +73,8 @@ class ClipboardImageWriter final : public ClipboardWriter {
promise_->RejectFromReadOrDecodeFailure(); promise_->RejectFromReadOrDecodeFailure();
return; return;
} }
if (!promise_->GetLocalFrame())
return;
SkBitmap bitmap; SkBitmap bitmap;
image->asLegacyBitmap(&bitmap); image->asLegacyBitmap(&bitmap);
system_clipboard()->WriteImage(std::move(bitmap)); system_clipboard()->WriteImage(std::move(bitmap));
...@@ -118,12 +119,15 @@ class ClipboardTextWriter final : public ClipboardWriter { ...@@ -118,12 +119,15 @@ class ClipboardTextWriter final : public ClipboardWriter {
} }
void Write(const String& text) { void Write(const String& text) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!promise_->GetLocalFrame())
return;
system_clipboard()->WritePlainText(text); system_clipboard()->WritePlainText(text);
promise_->CompleteWriteRepresentation(); promise_->CompleteWriteRepresentation();
} }
}; };
// Writes a blob with text/html content to the System Clipboard.
class ClipboardHtmlWriter final : public ClipboardWriter { class ClipboardHtmlWriter final : public ClipboardWriter {
public: public:
ClipboardHtmlWriter(SystemClipboard* system_clipboard, ClipboardHtmlWriter(SystemClipboard* system_clipboard,
...@@ -137,8 +141,10 @@ class ClipboardHtmlWriter final : public ClipboardWriter { ...@@ -137,8 +141,10 @@ class ClipboardHtmlWriter final : public ClipboardWriter {
scoped_refptr<base::SingleThreadTaskRunner> task_runner) override { scoped_refptr<base::SingleThreadTaskRunner> task_runner) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
promise_->GetExecutionContext()->CountUse( auto* execution_context = promise_->GetExecutionContext();
WebFeature::kHtmlClipboardApiWrite); if (!execution_context)
return;
execution_context->CountUse(WebFeature::kHtmlClipboardApiWrite);
String html_string = String html_string =
String::FromUTF8(reinterpret_cast<const LChar*>(html_data->Data()), String::FromUTF8(reinterpret_cast<const LChar*>(html_data->Data()),
...@@ -150,7 +156,10 @@ class ClipboardHtmlWriter final : public ClipboardWriter { ...@@ -150,7 +156,10 @@ class ClipboardHtmlWriter final : public ClipboardWriter {
unsigned fragment_start = 0; unsigned fragment_start = 0;
unsigned fragment_end = html_string.length(); unsigned fragment_end = html_string.length();
Document* document = promise_->GetLocalFrame()->GetDocument(); LocalFrame* local_frame = promise_->GetLocalFrame();
if (!local_frame)
return;
Document* document = local_frame->GetDocument();
DocumentFragment* fragment = CreateSanitizedFragmentFromMarkupWithContext( DocumentFragment* fragment = CreateSanitizedFragmentFromMarkupWithContext(
*document, html_string, fragment_start, fragment_end, url); *document, html_string, fragment_start, fragment_end, url);
String sanitized_html = String sanitized_html =
...@@ -165,6 +174,7 @@ class ClipboardHtmlWriter final : public ClipboardWriter { ...@@ -165,6 +174,7 @@ class ClipboardHtmlWriter final : public ClipboardWriter {
} }
}; };
// Writes a blob with image/svg+xml content to the System Clipboard.
class ClipboardSvgWriter final : public ClipboardWriter { class ClipboardSvgWriter final : public ClipboardWriter {
public: public:
ClipboardSvgWriter(SystemClipboard* system_clipboard, ClipboardSvgWriter(SystemClipboard* system_clipboard,
...@@ -188,7 +198,10 @@ class ClipboardSvgWriter final : public ClipboardWriter { ...@@ -188,7 +198,10 @@ class ClipboardSvgWriter final : public ClipboardWriter {
unsigned fragment_start = 0; unsigned fragment_start = 0;
unsigned fragment_end = svg_string.length(); unsigned fragment_end = svg_string.length();
Document* document = promise_->GetLocalFrame()->GetDocument(); LocalFrame* local_frame = promise_->GetLocalFrame();
if (!local_frame)
return;
Document* document = local_frame->GetDocument();
DocumentFragment* fragment = CreateSanitizedFragmentFromMarkupWithContext( DocumentFragment* fragment = CreateSanitizedFragmentFromMarkupWithContext(
*document, svg_string, fragment_start, fragment_end, url); *document, svg_string, fragment_start, fragment_end, url);
String sanitized_svg = String sanitized_svg =
...@@ -235,6 +248,8 @@ class ClipboardRawDataWriter final : public ClipboardWriter { ...@@ -235,6 +248,8 @@ class ClipboardRawDataWriter final : public ClipboardWriter {
mojo_base::BigBuffer buffer(std::vector<uint8_t>( mojo_base::BigBuffer buffer(std::vector<uint8_t>(
raw_data_pointer, raw_data_pointer + raw_data->ByteLength())); raw_data_pointer, raw_data_pointer + raw_data->ByteLength()));
if (!promise_->GetLocalFrame())
return;
raw_system_clipboard()->Write(mime_type_, std::move(buffer)); raw_system_clipboard()->Write(mime_type_, std::move(buffer));
promise_->CompleteWriteRepresentation(); promise_->CompleteWriteRepresentation();
......
...@@ -8,13 +8,13 @@ ...@@ -8,13 +8,13 @@
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "third_party/blink/renderer/core/fileapi/blob.h" #include "third_party/blink/renderer/core/fileapi/blob.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_loader_client.h" #include "third_party/blink/renderer/core/fileapi/file_reader_loader_client.h"
#include "third_party/blink/renderer/modules/clipboard/clipboard_promise.h"
#include "third_party/blink/renderer/platform/heap/heap.h" #include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/heap/self_keep_alive.h" #include "third_party/blink/renderer/platform/heap/self_keep_alive.h"
#include "third_party/skia/include/core/SkImage.h" #include "third_party/skia/include/core/SkImage.h"
namespace blink { namespace blink {
class ClipboardPromise;
class FileReaderLoader; class FileReaderLoader;
class SystemClipboard; class SystemClipboard;
class RawSystemClipboard; class RawSystemClipboard;
...@@ -97,8 +97,17 @@ class ClipboardWriter : public GarbageCollected<ClipboardWriter>, ...@@ -97,8 +97,17 @@ class ClipboardWriter : public GarbageCollected<ClipboardWriter>,
DOMArrayBuffer* raw_data, DOMArrayBuffer* raw_data,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) = 0; scoped_refptr<base::SingleThreadTaskRunner> task_runner) = 0;
SystemClipboard* system_clipboard() { return system_clipboard_; } // SystemClipboard and RawSystemClipboard are bound to LocalFrame, so the
RawSystemClipboard* raw_system_clipboard() { return raw_system_clipboard_; } // bound LocalFrame must still be valid by the time they're used.
SystemClipboard* system_clipboard() {
DCHECK(promise_->GetLocalFrame());
return system_clipboard_;
}
RawSystemClipboard* raw_system_clipboard() {
DCHECK(promise_->GetLocalFrame());
return raw_system_clipboard_;
}
// This ClipboardPromise owns this ClipboardWriter. Subclasses use `promise_` // This ClipboardPromise owns this ClipboardWriter. Subclasses use `promise_`
// to report success or failure, or to obtain the execution context. // to report success or failure, or to obtain the execution context.
......
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