2 * Copyright (C) 2006-2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "DragImage.h"
29 #include "PasteboardItemInfo.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/ListHashSet.h>
33 #include <wtf/Noncopyable.h>
34 #include <wtf/Vector.h>
35 #include <wtf/text/WTFString.h>
47 #include "WCDataObject.h"
50 typedef struct HWND__* HWND;
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.
58 class DocumentFragment;
62 class PasteboardStrategy;
67 enum class WebContentReadingPolicy { AnyType, OnlyRichTextTypes };
68 enum ShouldSerializeSelectedTextForDataTransfer { DefaultSelectedTextType, IncludeImageAltTextForDataTransfer };
70 // For writing to the pasteboard. Generally sorted with the richest formats on top.
72 struct PasteboardWebContent {
74 WEBCORE_EXPORT PasteboardWebContent();
75 WEBCORE_EXPORT ~PasteboardWebContent();
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;
88 bool canSmartCopyOrDelete;
98 struct PasteboardURL {
102 String userVisibleForm;
109 struct PasteboardImage {
110 WEBCORE_EXPORT PasteboardImage();
111 WEBCORE_EXPORT ~PasteboardImage();
114 RefPtr<SharedBuffer> dataInWebArchiveFormat;
119 #if !(PLATFORM(GTK) || PLATFORM(WIN))
120 RefPtr<SharedBuffer> resourceData;
121 String resourceMIMEType;
122 Vector<String> clientTypes;
123 Vector<RefPtr<SharedBuffer>> clientData;
125 String suggestedName;
129 // For reading from the pasteboard.
131 class PasteboardWebContentReader {
133 String contentOrigin;
135 virtual ~PasteboardWebContentReader() = default;
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;
146 virtual bool readPlainText(const String&) = 0;
149 struct PasteboardPlainText {
156 struct PasteboardFileReader {
157 virtual ~PasteboardFileReader() = default;
158 virtual void readFilename(const String&) = 0;
159 virtual void readBuffer(const String& filename, const String& type, Ref<SharedBuffer>&&) = 0;
162 // FIXME: We need to ensure that the contents of sameOriginCustomData are not accessible across different origins.
163 struct PasteboardCustomData {
165 Vector<String> orderedTypes;
166 HashMap<String, String> platformData;
167 HashMap<String, String> sameOriginCustomData;
169 WEBCORE_EXPORT Ref<SharedBuffer> createSharedBuffer() const;
170 WEBCORE_EXPORT static PasteboardCustomData fromSharedBuffer(const SharedBuffer&);
173 static const char* cocoaType();
178 WTF_MAKE_NONCOPYABLE(Pasteboard); WTF_MAKE_FAST_ALLOCATED;
181 virtual ~Pasteboard();
184 explicit Pasteboard(const String& name);
185 explicit Pasteboard(SelectionData&);
189 explicit Pasteboard(IDataObject*);
190 explicit Pasteboard(WCDataObject*);
191 explicit Pasteboard(const DragDataMap&);
194 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForCopyAndPaste();
196 static bool isSafeTypeForDOMToReadAndWrite(const String&);
197 static bool canExposeURLToDOMWhenPasteboardContainsFiles(const String&);
199 virtual bool isStatic() const { return false; }
201 virtual WEBCORE_EXPORT bool hasData();
202 virtual WEBCORE_EXPORT Vector<String> typesSafeForBindings(const String& origin);
203 virtual WEBCORE_EXPORT Vector<String> typesForLegacyUnsafeBindings();
204 virtual WEBCORE_EXPORT String readOrigin();
205 virtual WEBCORE_EXPORT String readString(const String& type);
206 virtual WEBCORE_EXPORT String readStringInCustomData(const String& type);
208 virtual WEBCORE_EXPORT void writeString(const String& type, const String& data);
209 virtual WEBCORE_EXPORT void clear();
210 virtual WEBCORE_EXPORT void clear(const String& type);
212 virtual WEBCORE_EXPORT void read(PasteboardPlainText&);
213 virtual WEBCORE_EXPORT void read(PasteboardWebContentReader&, WebContentReadingPolicy = WebContentReadingPolicy::AnyType);
214 virtual WEBCORE_EXPORT void read(PasteboardFileReader&);
216 virtual WEBCORE_EXPORT void write(const PasteboardURL&);
217 virtual WEBCORE_EXPORT void writeTrustworthyWebURLsPboardType(const PasteboardURL&);
218 virtual WEBCORE_EXPORT void write(const PasteboardImage&);
219 virtual WEBCORE_EXPORT void write(const PasteboardWebContent&);
221 virtual WEBCORE_EXPORT void writeCustomData(const PasteboardCustomData&);
223 virtual WEBCORE_EXPORT bool containsFiles();
224 virtual WEBCORE_EXPORT bool canSmartReplace();
226 virtual WEBCORE_EXPORT void writeMarkup(const String& markup);
227 enum SmartReplaceOption { CanSmartReplace, CannotSmartReplace };
228 virtual WEBCORE_EXPORT void writePlainText(const String&, SmartReplaceOption); // FIXME: Two separate functions would be clearer than one function with an argument.
230 #if ENABLE(DRAG_SUPPORT)
231 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForDragAndDrop();
232 WEBCORE_EXPORT static std::unique_ptr<Pasteboard> createForDragAndDrop(const DragData&);
234 virtual void setDragImage(DragImage, const IntPoint& hotSpot);
238 RefPtr<DocumentFragment> documentFragment(Frame&, Range&, bool allowPlainText, bool& chosePlainText); // FIXME: Layering violation.
239 void writeImage(Element&, const URL&, const String& title); // FIXME: Layering violation.
240 void writeSelection(Range&, bool canSmartCopyOrDelete, Frame&, ShouldSerializeSelectedTextForDataTransfer = DefaultSelectedTextType); // FIXME: Layering violation.
244 const SelectionData& selectionData() const;
245 static std::unique_ptr<Pasteboard> createForGlobalSelection();
249 explicit Pasteboard(long changeCount);
250 explicit Pasteboard(const String& pasteboardName);
252 static NSArray *supportedWebContentPasteboardTypes();
253 static String resourceMIMEType(NSString *mimeType);
257 explicit Pasteboard(const String& pasteboardName, const Vector<String>& promisedFilePaths = { });
261 static bool shouldTreatCocoaTypeAsFile(const String&);
262 WEBCORE_EXPORT static NSArray *supportedFileUploadPasteboardTypes();
263 const String& name() const { return m_pasteboardName; }
264 long changeCount() const;
265 const PasteboardCustomData& readCustomData();
269 COMPtr<IDataObject> dataObject() const { return m_dataObject; }
270 void setExternalDataObject(IDataObject*);
271 const DragDataMap& dragDataMap() const { return m_dragDataMap; }
272 void writeURLToWritableDataObject(const URL&, const String&);
273 COMPtr<WCDataObject> writableDataObject() const { return m_writableDataObject; }
274 void writeImageToDataObject(Element&, const URL&); // FIXME: Layering violation.
279 bool respectsUTIFidelities() const;
280 void readRespectingUTIFidelities(PasteboardWebContentReader&, WebContentReadingPolicy);
282 enum class ReaderResult {
285 PasteboardWasChangedExternally
287 ReaderResult readPasteboardWebContentDataForType(PasteboardWebContentReader&, PasteboardStrategy&, NSString *type, int itemIndex);
291 void finishCreatingPasteboard();
292 void writeRangeToDataObject(Range&, Frame&); // FIXME: Layering violation.
293 void writeURLToDataObject(const URL&, const String&);
294 void writePlainTextToDataObject(const String&, SmartReplaceOption);
298 Vector<String> readFilePaths();
299 String readPlatformValueAsString(const String& domType, long changeCount, const String& pasteboardName);
300 static void addHTMLClipboardTypesForCocoaType(ListHashSet<String>& resultTypes, const String& cocoaType);
301 String readStringForPlatformType(const String&);
302 Vector<String> readTypesWithSecurityCheck();
303 RefPtr<SharedBuffer> readBufferForTypeWithSecurityCheck(const String&);
307 void writeToClipboard();
308 void readFromClipboard();
309 Ref<SelectionData> m_selectionData;
314 String m_pasteboardName;
316 std::optional<PasteboardCustomData> m_customDataCache;
320 Vector<String> m_promisedFilePaths;
325 COMPtr<IDataObject> m_dataObject;
326 COMPtr<WCDataObject> m_writableDataObject;
327 DragDataMap m_dragDataMap;
332 extern NSString *WebArchivePboardType;
336 extern const char* const WebArchivePboardType;
337 extern const char* const WebURLNamePboardType;
342 inline Pasteboard::~Pasteboard()
348 } // namespace WebCore