1/*
2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 * Copyright (C) 2003-2016 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#pragma once
25
26#include "CachedResourceHandle.h"
27#include "DragActions.h"
28#include "DragImage.h"
29#include <wtf/text/WTFString.h>
30
31namespace WebCore {
32
33class CachedImage;
34class DataTransferItemList;
35class Document;
36class DragData;
37class DragImageLoader;
38class Element;
39class FileList;
40class File;
41class Pasteboard;
42enum class WebContentReadingPolicy;
43
44class DataTransfer : public RefCounted<DataTransfer> {
45public:
46 // https://html.spec.whatwg.org/multipage/dnd.html#drag-data-store-mode
47 enum class StoreMode { Invalid, ReadWrite, Readonly, Protected };
48
49 static Ref<DataTransfer> createForCopyAndPaste(Document&, StoreMode, std::unique_ptr<Pasteboard>&&);
50 static Ref<DataTransfer> createForInputEvent(const String& plainText, const String& htmlText);
51
52 WEBCORE_EXPORT ~DataTransfer();
53
54 String dropEffect() const;
55 void setDropEffect(const String&);
56
57 String effectAllowed() const;
58 void setEffectAllowed(const String&);
59
60 DataTransferItemList& items();
61 Vector<String> types() const;
62 Vector<String> typesForItemList() const;
63
64 FileList& files() const;
65
66 void clearData(const String& type = String());
67
68 String getData(Document&, const String& type) const;
69 String getDataForItem(Document&, const String& type) const;
70
71 void setData(const String& type, const String& data);
72 void setDataFromItemList(const String& type, const String& data);
73
74 void setDragImage(Element*, int x, int y);
75
76 void makeInvalidForSecurity() { m_storeMode = StoreMode::Invalid; }
77
78 bool canReadTypes() const;
79 bool canReadData() const;
80 bool canWriteData() const;
81
82 bool hasFileOfType(const String&);
83 bool hasStringOfType(const String&);
84
85 Pasteboard& pasteboard() { return *m_pasteboard; }
86 void commitToPasteboard(Pasteboard&);
87
88#if ENABLE(DRAG_SUPPORT)
89 static Ref<DataTransfer> createForDrag();
90 static Ref<DataTransfer> createForDragStartEvent(Document&);
91 static Ref<DataTransfer> createForDrop(Document&, std::unique_ptr<Pasteboard>&&, DragOperation, bool draggingFiles);
92 static Ref<DataTransfer> createForUpdatingDropTarget(Document&, std::unique_ptr<Pasteboard>&&, DragOperation, bool draggingFiles);
93
94 bool dropEffectIsUninitialized() const { return m_dropEffect == "uninitialized"; }
95
96 DragOperation sourceOperation() const;
97 DragOperation destinationOperation() const;
98 void setSourceOperation(DragOperation);
99 void setDestinationOperation(DragOperation);
100
101 void setDragHasStarted() { m_shouldUpdateDragImage = true; }
102 DragImageRef createDragImage(IntPoint& dragLocation) const;
103 void updateDragImage();
104 RefPtr<Element> dragImageElement() const;
105
106 void moveDragState(Ref<DataTransfer>&&);
107 bool hasDragImage() const;
108#endif
109
110 void didAddFileToItemList();
111 void updateFileList();
112
113private:
114 enum class Type { CopyAndPaste, DragAndDropData, DragAndDropFiles, InputEvent };
115 DataTransfer(StoreMode, std::unique_ptr<Pasteboard>, Type = Type::CopyAndPaste);
116
117#if ENABLE(DRAG_SUPPORT)
118 bool forDrag() const { return m_type == Type::DragAndDropData || m_type == Type::DragAndDropFiles; }
119 bool forFileDrag() const { return m_type == Type::DragAndDropFiles; }
120#else
121 bool forDrag() const { return false; }
122 bool forFileDrag() const { return false; }
123#endif
124
125 String readStringFromPasteboard(Document&, const String& lowercaseType, WebContentReadingPolicy) const;
126 bool shouldSuppressGetAndSetDataToAvoidExposingFilePaths() const;
127
128 enum class AddFilesType { No, Yes };
129 Vector<String> types(AddFilesType) const;
130 Vector<Ref<File>> filesFromPasteboardAndItemList() const;
131
132 String m_originIdentifier;
133 StoreMode m_storeMode;
134 std::unique_ptr<Pasteboard> m_pasteboard;
135 std::unique_ptr<DataTransferItemList> m_itemList;
136
137 mutable RefPtr<FileList> m_fileList;
138
139#if ENABLE(DRAG_SUPPORT)
140 Type m_type;
141 String m_dropEffect;
142 String m_effectAllowed;
143 bool m_shouldUpdateDragImage;
144 IntPoint m_dragLocation;
145 CachedResourceHandle<CachedImage> m_dragImage;
146 RefPtr<Element> m_dragImageElement;
147 std::unique_ptr<DragImageLoader> m_dragImageLoader;
148#endif
149};
150
151} // namespace WebCore
152