2 * Copyright (c) 2008, 2009, 2012 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/clipboard/DataObject.h"
34 #include "core/clipboard/Pasteboard.h"
35 #include "platform/clipboard/ClipboardMimeTypes.h"
36 #include "platform/clipboard/ClipboardUtilities.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebClipboard.h"
42 PassRefPtr<DataObject> DataObject::createFromPasteboard(PasteMode pasteMode)
44 RefPtr<DataObject> dataObject = create();
45 blink::WebClipboard::Buffer buffer = Pasteboard::generalPasteboard()->buffer();
46 uint64_t sequenceNumber = blink::Platform::current()->clipboard()->sequenceNumber(buffer);
48 blink::WebVector<blink::WebString> webTypes = blink::Platform::current()->clipboard()->readAvailableTypes(buffer, &ignored);
49 ListHashSet<String> types;
50 for (size_t i = 0; i < webTypes.size(); ++i)
51 types.add(webTypes[i]);
52 for (ListHashSet<String>::const_iterator it = types.begin(); it != types.end(); ++it) {
53 if (pasteMode == PlainTextOnly && *it != mimeTypeTextPlain)
55 dataObject->m_itemList.append(DataObjectItem::createFromPasteboard(*it, sequenceNumber));
57 return dataObject.release();
60 PassRefPtr<DataObject> DataObject::create()
62 return adoptRef(new DataObject());
65 PassRefPtr<DataObject> DataObject::copy() const
67 return adoptRef(new DataObject(*this));
70 size_t DataObject::length() const
72 return m_itemList.size();
75 PassRefPtr<DataObjectItem> DataObject::item(unsigned long index)
77 if (index >= length())
79 return m_itemList[index];
82 void DataObject::deleteItem(unsigned long index)
84 if (index >= length())
86 m_itemList.remove(index);
89 void DataObject::clearAll()
94 PassRefPtr<DataObjectItem> DataObject::add(const String& data, const String& type)
96 RefPtr<DataObjectItem> item = DataObjectItem::createFromString(type, data);
97 if (!internalAddStringItem(item))
102 PassRefPtr<DataObjectItem> DataObject::add(PassRefPtr<File> file)
107 RefPtr<DataObjectItem> item = DataObjectItem::createFromFile(file);
108 m_itemList.append(item);
112 void DataObject::clearData(const String& type)
114 for (size_t i = 0; i < m_itemList.size(); ++i) {
115 if (m_itemList[i]->kind() == DataObjectItem::StringKind && m_itemList[i]->type() == type) {
116 // Per the spec, type must be unique among all items of kind 'string'.
117 m_itemList.remove(i);
123 void DataObject::clearAllExceptFiles()
125 for (size_t i = 0; i < m_itemList.size(); ) {
126 if (m_itemList[i]->kind() != DataObjectItem::FileKind) {
127 m_itemList.remove(i);
134 ListHashSet<String> DataObject::types() const
136 ListHashSet<String> results;
137 bool containsFiles = false;
138 for (size_t i = 0; i < m_itemList.size(); ++i) {
139 switch (m_itemList[i]->kind()) {
140 case DataObjectItem::StringKind:
141 results.add(m_itemList[i]->type());
143 case DataObjectItem::FileKind:
144 containsFiles = true;
149 results.add(mimeTypeFiles);
153 String DataObject::getData(const String& type) const
155 for (size_t i = 0; i < m_itemList.size(); ++i) {
156 if (m_itemList[i]->kind() == DataObjectItem::StringKind && m_itemList[i]->type() == type)
157 return m_itemList[i]->getAsString();
162 bool DataObject::setData(const String& type, const String& data)
165 if (!add(data, type))
166 ASSERT_NOT_REACHED();
170 void DataObject::urlAndTitle(String& url, String* title) const
172 RefPtr<DataObjectItem> item = findStringItem(mimeTypeTextURIList);
175 url = convertURIListToURL(item->getAsString());
177 *title = item->title();
180 void DataObject::setURLAndTitle(const String& url, const String& title)
182 clearData(mimeTypeTextURIList);
183 internalAddStringItem(DataObjectItem::createFromURL(url, title));
186 void DataObject::htmlAndBaseURL(String& html, KURL& baseURL) const
188 RefPtr<DataObjectItem> item = findStringItem(mimeTypeTextHTML);
191 html = item->getAsString();
192 baseURL = item->baseURL();
195 void DataObject::setHTMLAndBaseURL(const String& html, const KURL& baseURL)
197 clearData(mimeTypeTextHTML);
198 internalAddStringItem(DataObjectItem::createFromHTML(html, baseURL));
201 bool DataObject::containsFilenames() const
203 for (size_t i = 0; i < m_itemList.size(); ++i) {
204 if (m_itemList[i]->isFilename())
210 Vector<String> DataObject::filenames() const
212 Vector<String> results;
213 for (size_t i = 0; i < m_itemList.size(); ++i) {
214 if (m_itemList[i]->isFilename())
215 results.append(static_cast<File*>(m_itemList[i]->getAsFile().get())->path());
220 void DataObject::addFilename(const String& filename, const String& displayName)
222 internalAddFileItem(DataObjectItem::createFromFile(File::createWithName(filename, displayName, File::AllContentTypes)));
225 void DataObject::addSharedBuffer(const String& name, PassRefPtr<SharedBuffer> buffer)
227 internalAddFileItem(DataObjectItem::createFromSharedBuffer(name, buffer));
230 DataObject::DataObject()
231 : m_modifierKeyState(0)
235 DataObject::DataObject(const DataObject& other)
236 : RefCounted<DataObject>()
237 , m_itemList(other.m_itemList)
238 , m_modifierKeyState(0)
242 PassRefPtr<DataObjectItem> DataObject::findStringItem(const String& type) const
244 for (size_t i = 0; i < m_itemList.size(); ++i) {
245 if (m_itemList[i]->kind() == DataObjectItem::StringKind && m_itemList[i]->type() == type)
246 return m_itemList[i];
251 bool DataObject::internalAddStringItem(PassRefPtr<DataObjectItem> item)
253 ASSERT(item->kind() == DataObjectItem::StringKind);
254 for (size_t i = 0; i < m_itemList.size(); ++i) {
255 if (m_itemList[i]->kind() == DataObjectItem::StringKind && m_itemList[i]->type() == item->type())
259 m_itemList.append(item);
263 void DataObject::internalAddFileItem(PassRefPtr<DataObjectItem> item)
265 ASSERT(item->kind() == DataObjectItem::FileKind);
266 m_itemList.append(item);
269 } // namespace WebCore