1 | /* |
2 | * Copyright (C) 2017 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 | #include "config.h" |
27 | #include "DataTransferItemList.h" |
28 | |
29 | #include "DataTransferItem.h" |
30 | #include "FileList.h" |
31 | #include "Pasteboard.h" |
32 | #include "RuntimeEnabledFeatures.h" |
33 | #include "Settings.h" |
34 | #include <wtf/IsoMallocInlines.h> |
35 | |
36 | namespace WebCore { |
37 | |
38 | WTF_MAKE_ISO_ALLOCATED_IMPL(DataTransferItemList); |
39 | |
40 | DataTransferItemList::DataTransferItemList(DataTransfer& dataTransfer) |
41 | : m_dataTransfer(dataTransfer) |
42 | { |
43 | } |
44 | |
45 | DataTransferItemList::~DataTransferItemList() = default; |
46 | |
47 | unsigned DataTransferItemList::length() const |
48 | { |
49 | return ensureItems().size(); |
50 | } |
51 | |
52 | RefPtr<DataTransferItem> DataTransferItemList::item(unsigned index) |
53 | { |
54 | auto& items = ensureItems(); |
55 | if (items.size() <= index) |
56 | return nullptr; |
57 | return items[index].copyRef(); |
58 | } |
59 | |
60 | static bool shouldExposeTypeInItemList(const String& type) |
61 | { |
62 | return RuntimeEnabledFeatures::sharedFeatures().customPasteboardDataEnabled() || Pasteboard::isSafeTypeForDOMToReadAndWrite(type); |
63 | } |
64 | |
65 | ExceptionOr<RefPtr<DataTransferItem>> DataTransferItemList::add(const String& data, const String& type) |
66 | { |
67 | if (!m_dataTransfer.canWriteData()) |
68 | return nullptr; |
69 | |
70 | for (auto& item : ensureItems()) { |
71 | if (!item->isFile() && equalIgnoringASCIICase(item->type(), type)) |
72 | return Exception { NotSupportedError }; |
73 | } |
74 | |
75 | String lowercasedType = type.convertToASCIILowercase(); |
76 | |
77 | if (!shouldExposeTypeInItemList(lowercasedType)) |
78 | return nullptr; |
79 | |
80 | m_dataTransfer.setDataFromItemList(lowercasedType, data); |
81 | ASSERT(m_items); |
82 | m_items->append(DataTransferItem::create(makeWeakPtr(*this), lowercasedType)); |
83 | return m_items->last().ptr(); |
84 | } |
85 | |
86 | RefPtr<DataTransferItem> DataTransferItemList::add(Ref<File>&& file) |
87 | { |
88 | if (!m_dataTransfer.canWriteData()) |
89 | return nullptr; |
90 | |
91 | ensureItems().append(DataTransferItem::create(makeWeakPtr(*this), file->type(), file.copyRef())); |
92 | m_dataTransfer.didAddFileToItemList(); |
93 | return m_items->last().ptr(); |
94 | } |
95 | |
96 | ExceptionOr<void> DataTransferItemList::remove(unsigned index) |
97 | { |
98 | if (!m_dataTransfer.canWriteData()) |
99 | return Exception { InvalidStateError }; |
100 | |
101 | auto& items = ensureItems(); |
102 | if (items.size() <= index) |
103 | return Exception { IndexSizeError }; // Matches Gecko. See https://github.com/whatwg/html/issues/2925 |
104 | |
105 | // FIXME: Remove the file from the pasteboard object once we add support for it. |
106 | Ref<DataTransferItem> removedItem = items[index].copyRef(); |
107 | if (!removedItem->isFile()) |
108 | m_dataTransfer.pasteboard().clear(removedItem->type()); |
109 | removedItem->clearListAndPutIntoDisabledMode(); |
110 | items.remove(index); |
111 | if (removedItem->isFile()) |
112 | m_dataTransfer.updateFileList(); |
113 | |
114 | return { }; |
115 | } |
116 | |
117 | void DataTransferItemList::clear() |
118 | { |
119 | m_dataTransfer.pasteboard().clear(); |
120 | bool removedItemContainingFile = false; |
121 | if (m_items) { |
122 | for (auto& item : *m_items) { |
123 | removedItemContainingFile |= item->isFile(); |
124 | item->clearListAndPutIntoDisabledMode(); |
125 | } |
126 | m_items->clear(); |
127 | } |
128 | |
129 | if (removedItemContainingFile) |
130 | m_dataTransfer.updateFileList(); |
131 | } |
132 | |
133 | Vector<Ref<DataTransferItem>>& DataTransferItemList::ensureItems() const |
134 | { |
135 | if (m_items) |
136 | return *m_items; |
137 | |
138 | Vector<Ref<DataTransferItem>> items; |
139 | for (auto& type : m_dataTransfer.typesForItemList()) { |
140 | auto lowercasedType = type.convertToASCIILowercase(); |
141 | if (shouldExposeTypeInItemList(lowercasedType)) |
142 | items.append(DataTransferItem::create(makeWeakPtr(*const_cast<DataTransferItemList*>(this)), lowercasedType)); |
143 | } |
144 | |
145 | for (auto& file : m_dataTransfer.files().files()) |
146 | items.append(DataTransferItem::create(makeWeakPtr(*const_cast<DataTransferItemList*>(this)), file->type(), file.copyRef())); |
147 | |
148 | m_items = WTFMove(items); |
149 | |
150 | return *m_items; |
151 | } |
152 | |
153 | static void removeStringItemOfLowercasedType(Vector<Ref<DataTransferItem>>& items, const String& lowercasedType) |
154 | { |
155 | auto index = items.findMatching([lowercasedType](auto& item) { |
156 | return !item->isFile() && item->type() == lowercasedType; |
157 | }); |
158 | if (index == notFound) |
159 | return; |
160 | items[index]->clearListAndPutIntoDisabledMode(); |
161 | items.remove(index); |
162 | } |
163 | |
164 | void DataTransferItemList::didClearStringData(const String& type) |
165 | { |
166 | if (!m_items) |
167 | return; |
168 | |
169 | auto& items = *m_items; |
170 | if (!type.isNull()) |
171 | return removeStringItemOfLowercasedType(items, type.convertToASCIILowercase()); |
172 | |
173 | for (auto& item : items) { |
174 | if (!item->isFile()) |
175 | item->clearListAndPutIntoDisabledMode(); |
176 | } |
177 | items.removeAllMatching([](auto& item) { |
178 | return !item->isFile(); |
179 | }); |
180 | } |
181 | |
182 | // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-setdata |
183 | void DataTransferItemList::didSetStringData(const String& type) |
184 | { |
185 | if (!m_items) |
186 | return; |
187 | |
188 | String lowercasedType = type.convertToASCIILowercase(); |
189 | removeStringItemOfLowercasedType(*m_items, type.convertToASCIILowercase()); |
190 | |
191 | m_items->append(DataTransferItem::create(makeWeakPtr(*this), lowercasedType)); |
192 | } |
193 | |
194 | } |
195 | |
196 | |