1/*
2 * Copyright (C) 2006-2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "DragImage.h"
29#include "PasteboardItemInfo.h"
30#include <wtf/HashMap.h>
31#include <wtf/ListHashSet.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/URL.h>
34#include <wtf/Vector.h>
35#include <wtf/text/WTFString.h>
36
37#if PLATFORM(IOS_FAMILY)
38OBJC_CLASS NSString;
39#endif
40
41#if PLATFORM(COCOA)
42OBJC_CLASS NSArray;
43#endif
44
45#if PLATFORM(WIN)
46#include "COMPtr.h"
47#include "WCDataObject.h"
48#include <objidl.h>
49#include <windows.h>
50typedef struct HWND__* HWND;
51#endif
52
53// FIXME: This class uses the DOM and makes calls to Editor.
54// It should be divested of its knowledge of the frame and editor.
55
56namespace WebCore {
57
58class DocumentFragment;
59class DragData;
60class Element;
61class Frame;
62class PasteboardStrategy;
63class Range;
64class SelectionData;
65class SharedBuffer;
66
67enum class WebContentReadingPolicy { AnyType, OnlyRichTextTypes };
68enum ShouldSerializeSelectedTextForDataTransfer { DefaultSelectedTextType, IncludeImageAltTextForDataTransfer };
69
70// For writing to the pasteboard. Generally sorted with the richest formats on top.
71
72struct PasteboardWebContent {
73#if PLATFORM(COCOA)
74 WEBCORE_EXPORT PasteboardWebContent();
75 WEBCORE_EXPORT ~PasteboardWebContent();
76 String contentOrigin;
77 bool canSmartCopyOrDelete;
78 RefPtr<SharedBuffer> dataInWebArchiveFormat;
79 RefPtr<SharedBuffer> dataInRTFDFormat;
80 RefPtr<SharedBuffer> dataInRTFFormat;
81 RefPtr<SharedBuffer> dataInAttributedStringFormat;
82 String dataInHTMLFormat;
83 String dataInStringFormat;
84 Vector<String> clientTypes;
85 Vector<RefPtr<SharedBuffer>> clientData;
86#endif
87#if PLATFORM(GTK)
88 bool canSmartCopyOrDelete;
89 String text;
90 String markup;
91#endif
92#if USE(LIBWPE)
93 String text;
94 String markup;
95#endif
96};
97
98struct PasteboardURL {
99 URL url;
100 String title;
101#if PLATFORM(MAC)
102 String userVisibleForm;
103#endif
104#if PLATFORM(GTK)
105 String markup;
106#endif
107};
108
109struct PasteboardImage {
110 WEBCORE_EXPORT PasteboardImage();
111 WEBCORE_EXPORT ~PasteboardImage();
112 RefPtr<Image> image;
113#if PLATFORM(MAC)
114 RefPtr<SharedBuffer> dataInWebArchiveFormat;
115#endif
116#if !PLATFORM(WIN)
117 PasteboardURL url;
118#endif
119#if !(PLATFORM(GTK) || PLATFORM(WIN))
120 RefPtr<SharedBuffer> resourceData;
121 String resourceMIMEType;
122 Vector<String> clientTypes;
123 Vector<RefPtr<SharedBuffer>> clientData;
124#endif
125 String suggestedName;
126 FloatSize imageSize;
127};
128
129// For reading from the pasteboard.
130
131class PasteboardWebContentReader {
132public:
133 String contentOrigin;
134
135 virtual ~PasteboardWebContentReader() = default;
136
137#if PLATFORM(COCOA)
138 virtual bool readWebArchive(SharedBuffer&) = 0;
139 virtual bool readFilePath(const String&, PresentationSize preferredPresentationSize = { }, const String& contentType = { }) = 0;
140 virtual bool readFilePaths(const Vector<String>&) = 0;
141 virtual bool readHTML(const String&) = 0;
142 virtual bool readRTFD(SharedBuffer&) = 0;
143 virtual bool readRTF(SharedBuffer&) = 0;
144 virtual bool readImage(Ref<SharedBuffer>&&, const String& type, PresentationSize preferredPresentationSize = { }) = 0;
145 virtual bool readURL(const URL&, const String& title) = 0;
146 virtual bool readDataBuffer(SharedBuffer&, const String& type, const String& name, PresentationSize preferredPresentationSize = { }) = 0;
147#endif
148 virtual bool readPlainText(const String&) = 0;
149};
150
151struct PasteboardPlainText {
152 String text;
153#if PLATFORM(COCOA)
154 bool isURL;
155#endif
156};
157
158struct PasteboardFileReader {
159 virtual ~PasteboardFileReader() = default;
160 virtual void readFilename(const String&) = 0;
161 virtual void readBuffer(const String& filename, const String& type, Ref<SharedBuffer>&&) = 0;
162};
163
164// FIXME: We need to ensure that the contents of sameOriginCustomData are not accessible across different origins.
165struct PasteboardCustomData {
166 String origin;
167 Vector<String> orderedTypes;
168 HashMap<String, String> platformData;
169 HashMap<String, String> sameOriginCustomData;
170
171 WEBCORE_EXPORT Ref<SharedBuffer> createSharedBuffer() const;
172 WEBCORE_EXPORT static PasteboardCustomData fromSharedBuffer(const SharedBuffer&);
173
174#if PLATFORM(COCOA)
175 WEBCORE_EXPORT static const char* cocoaType();
176#endif
177};
178
179class Pasteboard {
180 WTF_MAKE_NONCOPYABLE(Pasteboard); WTF_MAKE_FAST_ALLOCATED;
181public:
182 Pasteboard();
183 virtual ~Pasteboard();
184
185#if PLATFORM(GTK)
186 explicit Pasteboard(const String& name);
187 explicit Pasteboard(SelectionData&);
188#endif
189
190#if PLATFORM(WIN)
191 explicit Pasteboard(IDataObject*);
192 explicit Pasteboard(WCDataObject*);
193 explicit Pasteboard(const DragDataMap&);
194#endif
195
196 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForCopyAndPaste();
197
198 static bool isSafeTypeForDOMToReadAndWrite(const String&);
199 static bool canExposeURLToDOMWhenPasteboardContainsFiles(const String&);
200
201 virtual bool isStatic() const { return false; }
202
203 virtual WEBCORE_EXPORT bool hasData();
204 virtual WEBCORE_EXPORT Vector<String> typesSafeForBindings(const String& origin);
205 virtual WEBCORE_EXPORT Vector<String> typesForLegacyUnsafeBindings();
206 virtual WEBCORE_EXPORT String readOrigin();
207 virtual WEBCORE_EXPORT String readString(const String& type);
208 virtual WEBCORE_EXPORT String readStringInCustomData(const String& type);
209 virtual WEBCORE_EXPORT Vector<String> readAllStrings(const String& type);
210
211 virtual WEBCORE_EXPORT void writeString(const String& type, const String& data);
212 virtual WEBCORE_EXPORT void clear();
213 virtual WEBCORE_EXPORT void clear(const String& type);
214
215 virtual WEBCORE_EXPORT void read(PasteboardPlainText&);
216 virtual WEBCORE_EXPORT void read(PasteboardWebContentReader&, WebContentReadingPolicy = WebContentReadingPolicy::AnyType);
217 virtual WEBCORE_EXPORT void read(PasteboardFileReader&);
218
219 virtual WEBCORE_EXPORT void write(const Color&);
220 virtual WEBCORE_EXPORT void write(const PasteboardURL&);
221 virtual WEBCORE_EXPORT void writeTrustworthyWebURLsPboardType(const PasteboardURL&);
222 virtual WEBCORE_EXPORT void write(const PasteboardImage&);
223 virtual WEBCORE_EXPORT void write(const PasteboardWebContent&);
224
225 virtual WEBCORE_EXPORT void writeCustomData(const PasteboardCustomData&);
226
227 enum class FileContentState { NoFileOrImageData, InMemoryImage, MayContainFilePaths };
228 virtual WEBCORE_EXPORT FileContentState fileContentState();
229 virtual WEBCORE_EXPORT bool canSmartReplace();
230
231 virtual WEBCORE_EXPORT void writeMarkup(const String& markup);
232 enum SmartReplaceOption { CanSmartReplace, CannotSmartReplace };
233 virtual WEBCORE_EXPORT void writePlainText(const String&, SmartReplaceOption); // FIXME: Two separate functions would be clearer than one function with an argument.
234
235#if ENABLE(DRAG_SUPPORT)
236 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForDragAndDrop();
237 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForDragAndDrop(const DragData&);
238
239 virtual void setDragImage(DragImage, const IntPoint& hotSpot);
240#endif
241
242#if PLATFORM(WIN)
243 RefPtr<DocumentFragment> documentFragment(Frame&, Range&, bool allowPlainText, bool& chosePlainText); // FIXME: Layering violation.
244 void writeImage(Element&, const URL&, const String& title); // FIXME: Layering violation.
245 void writeSelection(Range&, bool canSmartCopyOrDelete, Frame&, ShouldSerializeSelectedTextForDataTransfer = DefaultSelectedTextType); // FIXME: Layering violation.
246#endif
247
248#if PLATFORM(GTK)
249 const SelectionData& selectionData() const;
250 static std::unique_ptr<Pasteboard> createForGlobalSelection();
251#endif
252
253#if PLATFORM(IOS_FAMILY)
254 explicit Pasteboard(long changeCount);
255 explicit Pasteboard(const String& pasteboardName);
256
257 static NSArray *supportedWebContentPasteboardTypes();
258 static String resourceMIMEType(NSString *mimeType);
259#endif
260
261#if PLATFORM(MAC)
262 explicit Pasteboard(const String& pasteboardName, const Vector<String>& promisedFilePaths = { });
263#endif
264
265#if PLATFORM(COCOA)
266 static bool shouldTreatCocoaTypeAsFile(const String&);
267 WEBCORE_EXPORT static NSArray *supportedFileUploadPasteboardTypes();
268 const String& name() const { return m_pasteboardName; }
269 long changeCount() const;
270 const PasteboardCustomData& readCustomData();
271#endif
272
273#if PLATFORM(WIN)
274 COMPtr<IDataObject> dataObject() const { return m_dataObject; }
275 void setExternalDataObject(IDataObject*);
276 const DragDataMap& dragDataMap() const { return m_dragDataMap; }
277 void writeURLToWritableDataObject(const URL&, const String&);
278 COMPtr<WCDataObject> writableDataObject() const { return m_writableDataObject; }
279 void writeImageToDataObject(Element&, const URL&); // FIXME: Layering violation.
280#endif
281
282private:
283#if PLATFORM(IOS_FAMILY)
284 bool respectsUTIFidelities() const;
285 void readRespectingUTIFidelities(PasteboardWebContentReader&, WebContentReadingPolicy);
286
287 enum class ReaderResult {
288 ReadType,
289 DidNotReadType,
290 PasteboardWasChangedExternally
291 };
292 ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, const PasteboardItemInfo&, int itemIndex);
293#endif
294
295#if PLATFORM(WIN)
296 void finishCreatingPasteboard();
297 void writeRangeToDataObject(Range&, Frame&); // FIXME: Layering violation.
298 void writeURLToDataObject(const URL&, const String&);
299 void writePlainTextToDataObject(const String&, SmartReplaceOption);
300#endif
301
302#if PLATFORM(COCOA)
303 Vector<String> readFilePaths();
304 Vector<String> readPlatformValuesAsStrings(const String& domType, long changeCount, const String& pasteboardName);
305 static void addHTMLClipboardTypesForCocoaType(ListHashSet<String>& resultTypes, const String& cocoaType);
306 String readStringForPlatformType(const String&);
307 Vector<String> readTypesWithSecurityCheck();
308 RefPtr<SharedBuffer> readBufferForTypeWithSecurityCheck(const String&);
309#endif
310
311#if PLATFORM(GTK)
312 void writeToClipboard();
313 void readFromClipboard();
314 Ref<SelectionData> m_selectionData;
315 String m_name;
316#endif
317
318#if PLATFORM(COCOA)
319 String m_pasteboardName;
320 long m_changeCount;
321 Optional<PasteboardCustomData> m_customDataCache;
322#endif
323
324#if PLATFORM(MAC)
325 Vector<String> m_promisedFilePaths;
326#endif
327
328#if PLATFORM(WIN)
329 HWND m_owner;
330 COMPtr<IDataObject> m_dataObject;
331 COMPtr<WCDataObject> m_writableDataObject;
332 DragDataMap m_dragDataMap;
333#endif
334};
335
336#if PLATFORM(IOS_FAMILY)
337extern NSString *WebArchivePboardType;
338extern NSString *UIColorPboardType;
339#endif
340
341#if PLATFORM(MAC)
342extern const char* const WebArchivePboardType;
343extern const char* const WebURLNamePboardType;
344extern const char* const WebURLsWithTitlesPboardType;
345#endif
346
347#if !PLATFORM(GTK)
348
349inline Pasteboard::~Pasteboard()
350{
351}
352
353#endif
354
355} // namespace WebCore
356