1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_SYSTEM_CLIPBOARD_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_SYSTEM_CLIPBOARD_H_
#include <memory>
#include <optional>
#include "third_party/blink/public/mojom/clipboard/clipboard.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/frame/platform_event_dispatcher.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class DataObject;
class Image;
class KURL;
class LocalFrame;
class ScopedSystemClipboardSnapshot;
// SystemClipboard:
// - is a LocalFrame bounded object.
// - provides sanitized, platform-neutral read/write access to the clipboard.
// - mediates between core classes and mojom::ClipboardHost.
//
// All calls to write functions must be followed by a call to CommitWrite().
class CORE_EXPORT SystemClipboard final
: public GarbageCollected<SystemClipboard>,
public PlatformEventDispatcher,
public mojom::blink::ClipboardListener {
public:
enum SmartReplaceOption { kCanSmartReplace, kCannotSmartReplace };
explicit SystemClipboard(LocalFrame* frame);
SystemClipboard(const SystemClipboard&) = delete;
SystemClipboard& operator=(const SystemClipboard&) = delete;
// Inherited from PlatformEventDispatcher.
void StartListening(LocalDOMWindow*) override;
void StopListening() override;
// Inherited from ClipboardListener.
void OnClipboardDataChanged() override;
ClipboardSequenceNumberToken SequenceNumber();
bool IsSelectionMode() const;
void SetSelectionMode(bool);
Vector<String> ReadAvailableTypes();
bool IsFormatAvailable(mojom::ClipboardFormat format);
String ReadPlainText();
String ReadPlainText(mojom::blink::ClipboardBuffer buffer);
void ReadPlainText(mojom::blink::ClipboardBuffer buffer,
mojom::blink::ClipboardHost::ReadTextCallback callback);
void WritePlainText(const String&, SmartReplaceOption = kCannotSmartReplace);
// If no data is read, an empty string will be returned and all out parameters
// will be cleared. If applicable, the page URL will be assigned to the KURL
// parameter. fragmentStart and fragmentEnd are indexes into the returned
// markup that indicate the start and end of the returned markup. If there is
// no additional context, fragmentStart will be zero and fragmentEnd will be
// the same as the length of the markup.
String ReadHTML(KURL& url, unsigned& fragment_start, unsigned& fragment_end);
void ReadHTML(mojom::blink::ClipboardHost::ReadHtmlCallback callback);
void WriteHTML(const String& markup,
const KURL& document_url,
SmartReplaceOption = kCannotSmartReplace);
void ReadSvg(mojom::blink::ClipboardHost::ReadSvgCallback callback);
void WriteSvg(const String& markup);
String ReadRTF();
mojo_base::BigBuffer ReadPng(mojom::blink::ClipboardBuffer);
String ReadImageAsImageMarkup(mojom::blink::ClipboardBuffer);
// Write the image and its associated tag (bookmark/HTML types).
void WriteImageWithTag(Image*, const KURL&, const String& title);
// Write the image only.
void WriteImage(const SkBitmap&);
// Read files.
mojom::blink::ClipboardFilesPtr ReadFiles();
String ReadDataTransferCustomData(const String& type);
void WriteDataObject(DataObject*);
// Clipboard write functions must use CommitWrite for changes to reach
// the OS clipboard.
void CommitWrite();
void CopyToFindPboard(const String& text);
void ReadAvailableCustomAndStandardFormats(
mojom::blink::ClipboardHost::ReadAvailableCustomAndStandardFormatsCallback
callback);
void ReadUnsanitizedCustomFormat(
const String& type,
mojom::blink::ClipboardHost::ReadUnsanitizedCustomFormatCallback
callback);
void WriteUnsanitizedCustomFormat(const String& type,
mojo_base::BigBuffer data);
void Trace(Visitor*) const override;
private:
friend class ScopedSystemClipboardSnapshot;
// When in scope, forces the specified system clipboard to take a snapshot
// of its current state and return those value for the ReadXXX methods.
// Calling WriteXXX methods on the specified clipboard are not allowed and
// will DCHECK.
class CORE_EXPORT Snapshot {
public:
explicit Snapshot();
~Snapshot();
// The methods below are an API intended for only SystemClipboard. Each
// data type XXX has three basic methods:
//
// HasXXX(): whether this snapshot object has cached a value for XXX.
// SetXXX(): sets the value in the snapshot for data type XXX.
// XXX(): gets the value in the snapshot for data type XXX.
bool HasPlainText(mojom::blink::ClipboardBuffer buffer) const;
const String& PlainText(mojom::blink::ClipboardBuffer buffer) const;
void SetPlainText(mojom::blink::ClipboardBuffer buffer, const String& text);
bool HasHtml(mojom::blink::ClipboardBuffer buffer) const;
const KURL& Url(mojom::blink::ClipboardBuffer buffer) const;
unsigned FragmentStart(mojom::blink::ClipboardBuffer buffer) const;
unsigned FragmentEnd(mojom::blink::ClipboardBuffer buffer) const;
const String& Html(mojom::blink::ClipboardBuffer buffer) const;
void SetHtml(mojom::blink::ClipboardBuffer buffer,
const String& html,
const KURL& url,
unsigned fragment_start,
unsigned fragment_end);
bool HasRtf(mojom::blink::ClipboardBuffer buffer) const;
const String& Rtf(mojom::blink::ClipboardBuffer buffer) const;
void SetRtf(mojom::blink::ClipboardBuffer buffer, const String& rtf);
bool HasPng(mojom::blink::ClipboardBuffer buffer) const;
mojo_base::BigBuffer Png(mojom::blink::ClipboardBuffer buffer) const;
void SetPng(mojom::blink::ClipboardBuffer buffer,
const mojo_base::BigBuffer& png);
bool HasFiles(mojom::blink::ClipboardBuffer buffer) const;
mojom::blink::ClipboardFilesPtr Files(
mojom::blink::ClipboardBuffer buffer) const;
// Because of the special handling needed to clone ClipboardFilesPtr,
// SetFiles() needs to change the function argument `files`.
void SetFiles(mojom::blink::ClipboardBuffer buffer,
mojom::blink::ClipboardFilesPtr& files);
bool HasCustomData(mojom::blink::ClipboardBuffer buffer,
const String& type) const;
String CustomData(mojom::blink::ClipboardBuffer buffer,
const String& type) const;
void SetCustomData(mojom::blink::ClipboardBuffer buffer,
const String& type,
const String& data);
static mojom::blink::ClipboardFilesPtr CloneFiles(
mojom::blink::ClipboardFilesPtr& files);
private:
// Called in the set methods to bind this snapshot to the specified buffer.
// All calls to set data for all types need to specify the same buffer.
void BindToBuffer(mojom::blink::ClipboardBuffer buffer);
std::optional<mojom::blink::ClipboardBuffer> buffer_;
std::optional<String> plain_text_;
std::optional<String> html_;
KURL url_;
unsigned fragment_start_ = 0;
unsigned fragment_end_ = 0;
std::optional<String> rtf_;
std::optional<mojo_base::BigBuffer> png_;
mutable std::optional<mojom::blink::ClipboardFilesPtr> files_;
WTF::HashMap<String, String> custom_data_;
};
bool IsValidBufferType(mojom::blink::ClipboardBuffer);
// Methods to enter and leave snapshot mode. Only the
// ScopedSystemClipboardSnapshot calls these methods.
void TakeSnapshot();
void DropSnapshot();
// Resets the clipboard snapshot if a write operation occurs
// while the snapshot is active.
void ResetSnapshot();
HeapMojoRemote<mojom::blink::ClipboardHost> clipboard_;
HeapMojoReceiver<mojom::blink::ClipboardListener, SystemClipboard>
clipboard_listener_receiver_;
// In some Linux environments, |buffer_| may equal ClipboardBuffer::kStandard
// or kSelection. In other platforms |buffer_| always equals
// ClipboardBuffer::kStandard.
mojom::blink::ClipboardBuffer buffer_ =
mojom::blink::ClipboardBuffer::kStandard;
// Whether the selection buffer is available on the underlying platform.
bool is_selection_buffer_available_ = false;
// When non-null, the system clipboard uses the cached value in the snapshot
// rather than making calls to the browser process via the clipboard host.
// Snapshots can be nested and `snapshot_count_` counts the number of
// nestings. The snapshot is created when the first call to TakeSnapshot()
// is made and released once an equal number of DropSnapshot()s have been
// made.
std::unique_ptr<Snapshot> snapshot_;
size_t snapshot_count_ = 0;
// Declared SystemClipboardTest class as friend to access the private members
// of this class as we need to use clipboard_ and buffer_ for unbound remote
// tests.
friend class SystemClipboardTest;
};
// When in scope, forces the specified system clipboard to take a snapshot
// of its current state and return those value for the ReadXXX methods. Calling
// WriteXXX methods on the specified clipboard are not allowed and will DCHECK.
class CORE_EXPORT ScopedSystemClipboardSnapshot {
STACK_ALLOCATED();
public:
explicit ScopedSystemClipboardSnapshot(SystemClipboard& clipboard);
~ScopedSystemClipboardSnapshot();
private:
SystemClipboard& clipboard_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_SYSTEM_CLIPBOARD_H_
|