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 readFilePaths(const Vector<String>&) = 0;
140 virtual bool readHTML(const String&) = 0;
141 virtual bool readRTFD(SharedBuffer&) = 0;
142 virtual bool readRTF(SharedBuffer&) = 0;
143 virtual bool readImage(Ref<SharedBuffer>&&, const String& type) = 0;
144 virtual bool readURL(const URL&, const String& title) = 0;
145 virtual bool readDataBuffer(SharedBuffer&, const String& type, const String& name) = 0;
146#endif
147 virtual bool readPlainText(const String&) = 0;
148};
149
150struct PasteboardPlainText {
151 String text;
152#if PLATFORM(COCOA)
153 bool isURL;
154#endif
155};
156
157struct PasteboardFileReader {
158 virtual ~PasteboardFileReader() = default;
159 virtual void readFilename(const String&) = 0;
160 virtual void readBuffer(const String& filename, const String& type, Ref<SharedBuffer>&&) = 0;
161};
162
163// FIXME: We need to ensure that the contents of sameOriginCustomData are not accessible across different origins.
164struct PasteboardCustomData {
165 String origin;
166 Vector<String> orderedTypes;
167 HashMap<String, String> platformData;
168 HashMap<String, String> sameOriginCustomData;
169
170 WEBCORE_EXPORT Ref<SharedBuffer> createSharedBuffer() const;
171 WEBCORE_EXPORT static PasteboardCustomData fromSharedBuffer(const SharedBuffer&);
172
173#if PLATFORM(COCOA)
174 WEBCORE_EXPORT static const char* cocoaType();
175#endif
176};
177
178class Pasteboard {
179 WTF_MAKE_NONCOPYABLE(Pasteboard); WTF_MAKE_FAST_ALLOCATED;
180public:
181 Pasteboard();
182 virtual ~Pasteboard();
183
184#if PLATFORM(GTK)
185 explicit Pasteboard(const String& name);
186 explicit Pasteboard(SelectionData&);
187#endif
188
189#if PLATFORM(WIN)
190 explicit Pasteboard(IDataObject*);
191 explicit Pasteboard(WCDataObject*);
192 explicit Pasteboard(const DragDataMap&);
193#endif
194
195 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForCopyAndPaste();
196
197 static bool isSafeTypeForDOMToReadAndWrite(const String&);
198 static bool canExposeURLToDOMWhenPasteboardContainsFiles(const String&);
199
200 virtual bool isStatic() const { return false; }
201
202 virtual WEBCORE_EXPORT bool hasData();
203 virtual WEBCORE_EXPORT Vector<String> typesSafeForBindings(const String& origin);
204 virtual WEBCORE_EXPORT Vector<String> typesForLegacyUnsafeBindings();
205 virtual WEBCORE_EXPORT String readOrigin();
206 virtual WEBCORE_EXPORT String readString(const String& type);
207 virtual WEBCORE_EXPORT String readStringInCustomData(const String& type);
208 virtual WEBCORE_EXPORT Vector<String> readAllStrings(const String& type);
209
210 virtual WEBCORE_EXPORT void writeString(const String& type, const String& data);
211 virtual WEBCORE_EXPORT void clear();
212 virtual WEBCORE_EXPORT void clear(const String& type);
213
214 virtual WEBCORE_EXPORT void read(PasteboardPlainText&);
215 virtual WEBCORE_EXPORT void read(PasteboardWebContentReader&, WebContentReadingPolicy = WebContentReadingPolicy::AnyType);
216 virtual WEBCORE_EXPORT void read(PasteboardFileReader&);
217
218 virtual WEBCORE_EXPORT void write(const Color&);
219 virtual WEBCORE_EXPORT void write(const PasteboardURL&);
220 virtual WEBCORE_EXPORT void writeTrustworthyWebURLsPboardType(const PasteboardURL&);
221 virtual WEBCORE_EXPORT void write(const PasteboardImage&);
222 virtual WEBCORE_EXPORT void write(const PasteboardWebContent&);
223
224 virtual WEBCORE_EXPORT void writeCustomData(const PasteboardCustomData&);
225
226 enum class FileContentState { NoFileOrImageData, InMemoryImage, MayContainFilePaths };
227 virtual WEBCORE_EXPORT FileContentState fileContentState();
228 virtual WEBCORE_EXPORT bool canSmartReplace();
229
230 virtual WEBCORE_EXPORT void writeMarkup(const String& markup);
231 enum SmartReplaceOption { CanSmartReplace, CannotSmartReplace };
232 virtual WEBCORE_EXPORT void writePlainText(const String&, SmartReplaceOption); // FIXME: Two separate functions would be clearer than one function with an argument.
233
234#if ENABLE(DRAG_SUPPORT)
235 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForDragAndDrop();
236 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForDragAndDrop(const DragData&);
237
238 virtual void setDragImage(DragImage, const IntPoint& hotSpot);
239#endif
240
241#if PLATFORM(WIN)
242 RefPtr<DocumentFragment> documentFragment(Frame&, Range&, bool allowPlainText, bool& chosePlainText); // FIXME: Layering violation.
243 void writeImage(Element&, const URL&, const String& title); // FIXME: Layering violation.
244 void writeSelection(Range&, bool canSmartCopyOrDelete, Frame&, ShouldSerializeSelectedTextForDataTransfer = DefaultSelectedTextType); // FIXME: Layering violation.
245#endif
246
247#if PLATFORM(GTK)
248 const SelectionData& selectionData() const;
249 static std::unique_ptr<Pasteboard> createForGlobalSelection();
250#endif
251
252#if PLATFORM(IOS_FAMILY)
253 explicit Pasteboard(long changeCount);
254 explicit Pasteboard(const String& pasteboardName);
255
256 static NSArray *supportedWebContentPasteboardTypes();
257 static String resourceMIMEType(NSString *mimeType);
258#endif
259
260#if PLATFORM(MAC)
261 explicit Pasteboard(const String& pasteboardName, const Vector<String>& promisedFilePaths = { });
262#endif
263
264#if PLATFORM(COCOA)
265 static bool shouldTreatCocoaTypeAsFile(const String&);
266 WEBCORE_EXPORT static NSArray *supportedFileUploadPasteboardTypes();
267 const String& name() const { return m_pasteboardName; }
268 long changeCount() const;
269 const PasteboardCustomData& readCustomData();
270#endif
271
272#if PLATFORM(WIN)
273 COMPtr<IDataObject> dataObject() const { return m_dataObject; }
274 void setExternalDataObject(IDataObject*);
275 const DragDataMap& dragDataMap() const { return m_dragDataMap; }
276 void writeURLToWritableDataObject(const URL&, const String&);
277 COMPtr<WCDataObject> writableDataObject() const { return m_writableDataObject; }
278 void writeImageToDataObject(Element&, const URL&); // FIXME: Layering violation.
279#endif
280
281private:
282#if PLATFORM(IOS_FAMILY)
283 bool respectsUTIFidelities() const;
284 void readRespectingUTIFidelities(PasteboardWebContentReader&, WebContentReadingPolicy);
285
286 enum class ReaderResult {
287 ReadType,
288 DidNotReadType,
289 PasteboardWasChangedExternally
290 };
291 ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, int itemIndex);
292#endif
293
294#if PLATFORM(WIN)
295 void finishCreatingPasteboard();
296 void writeRangeToDataObject(Range&, Frame&); // FIXME: Layering violation.
297 void writeURLToDataObject(const URL&, const String&);
298 void writePlainTextToDataObject(const String&, SmartReplaceOption);
299#endif
300
301#if PLATFORM(COCOA)
302 Vector<String> readFilePaths();
303 Vector<String> readPlatformValuesAsStrings(const String& domType, long changeCount, const String& pasteboardName);
304 static void addHTMLClipboardTypesForCocoaType(ListHashSet<String>& resultTypes, const String& cocoaType);
305 String readStringForPlatformType(const String&);
306 Vector<String> readTypesWithSecurityCheck();
307 RefPtr<SharedBuffer> readBufferForTypeWithSecurityCheck(const String&);
308#endif
309
310#if PLATFORM(GTK)
311 void writeToClipboard();
312 void readFromClipboard();
313 Ref<SelectionData> m_selectionData;
314 String m_name;
315#endif
316
317#if PLATFORM(COCOA)
318 String m_pasteboardName;
319 long m_changeCount;
320 Optional<PasteboardCustomData> m_customDataCache;
321#endif
322
323#if PLATFORM(MAC)
324 Vector<String> m_promisedFilePaths;
325#endif
326
327#if PLATFORM(WIN)
328 HWND m_owner;
329 COMPtr<IDataObject> m_dataObject;
330 COMPtr<WCDataObject> m_writableDataObject;
331 DragDataMap m_dragDataMap;
332#endif
333};
334
335#if PLATFORM(IOS_FAMILY)
336extern NSString *WebArchivePboardType;
337extern NSString *UIColorPboardType;
338#endif
339
340#if PLATFORM(MAC)
341extern const char* const WebArchivePboardType;
342extern const char* const WebURLNamePboardType;
343extern const char* const WebURLsWithTitlesPboardType;
344#endif
345
346#if !PLATFORM(GTK)
347
348inline Pasteboard::~Pasteboard()
349{
350}
351
352#endif
353
354} // namespace WebCore
355