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 "ChromiumDataObject.h"
34 #include "ClipboardMimeTypes.h"
35 #include "ClipboardUtilitiesChromium.h"
36 #include "DataTransferItem.h"
37 #include "ExceptionCode.h"
38 #include "ExceptionCodePlaceholder.h"
39 #include "PlatformSupport.h"
43 PassRefPtr<ChromiumDataObject> ChromiumDataObject::createFromPasteboard()
45 RefPtr<ChromiumDataObject> dataObject = create();
46 uint64_t sequenceNumber = PlatformSupport::clipboardSequenceNumber(currentPasteboardBuffer());
48 HashSet<String> types = PlatformSupport::clipboardReadAvailableTypes(currentPasteboardBuffer(), &ignored);
49 for (HashSet<String>::const_iterator it = types.begin(); it != types.end(); ++it)
50 dataObject->m_itemList.append(ChromiumDataObjectItem::createFromPasteboard(*it, sequenceNumber));
51 return dataObject.release();
54 PassRefPtr<ChromiumDataObject> ChromiumDataObject::create()
56 return adoptRef(new ChromiumDataObject());
59 PassRefPtr<ChromiumDataObject> ChromiumDataObject::copy() const
61 return adoptRef(new ChromiumDataObject(*this));
64 size_t ChromiumDataObject::length() const
66 return m_itemList.size();
69 PassRefPtr<ChromiumDataObjectItem> ChromiumDataObject::item(unsigned long index)
71 if (index >= length())
73 return m_itemList[index];
76 void ChromiumDataObject::deleteItem(unsigned long index)
78 if (index >= length())
80 m_itemList.remove(index);
83 void ChromiumDataObject::clearAll()
88 void ChromiumDataObject::add(const String& data, const String& type, ExceptionCode& ec)
90 if (!internalAddStringItem(ChromiumDataObjectItem::createFromString(type, data)))
91 ec = NOT_SUPPORTED_ERR;
94 void ChromiumDataObject::add(PassRefPtr<File> file, ScriptExecutionContext* context)
99 m_itemList.append(ChromiumDataObjectItem::createFromFile(file));
102 void ChromiumDataObject::clearData(const String& type)
104 for (size_t i = 0; i < m_itemList.size(); ++i) {
105 if (m_itemList[i]->kind() == DataTransferItem::kindString && m_itemList[i]->type() == type) {
106 // Per the spec, type must be unique among all items of kind 'string'.
107 m_itemList.remove(i);
113 void ChromiumDataObject::clearAllExceptFiles()
115 for (size_t i = 0; i < m_itemList.size(); ) {
116 if (m_itemList[i]->kind() != DataTransferItem::kindFile) {
117 m_itemList.remove(i);
124 HashSet<String> ChromiumDataObject::types() const
126 HashSet<String> results;
127 bool containsFiles = false;
128 for (size_t i = 0; i < m_itemList.size(); ++i) {
129 if (m_itemList[i]->kind() == DataTransferItem::kindString)
130 results.add(m_itemList[i]->type());
131 else if (m_itemList[i]->kind() == DataTransferItem::kindFile)
132 containsFiles = true;
134 ASSERT_NOT_REACHED();
137 results.add(mimeTypeFiles);
141 String ChromiumDataObject::getData(const String& type) const
143 for (size_t i = 0; i < m_itemList.size(); ++i) {
144 if (m_itemList[i]->kind() == DataTransferItem::kindString && m_itemList[i]->type() == type)
145 return m_itemList[i]->internalGetAsString();
150 bool ChromiumDataObject::setData(const String& type, const String& data)
153 add(data, type, ASSERT_NO_EXCEPTION);
157 void ChromiumDataObject::urlAndTitle(String& url, String* title) const
159 RefPtr<ChromiumDataObjectItem> item = findStringItem(mimeTypeTextURIList);
162 url = convertURIListToURL(item->internalGetAsString());
164 *title = item->title();
167 void ChromiumDataObject::setURLAndTitle(const String& url, const String& title)
169 clearData(mimeTypeTextURIList);
170 internalAddStringItem(ChromiumDataObjectItem::createFromURL(url, title));
173 void ChromiumDataObject::htmlAndBaseURL(String& html, KURL& baseURL) const
175 RefPtr<ChromiumDataObjectItem> item = findStringItem(mimeTypeTextHTML);
178 html = item->internalGetAsString();
179 baseURL = item->baseURL();
182 void ChromiumDataObject::setHTMLAndBaseURL(const String& html, const KURL& baseURL)
184 clearData(mimeTypeTextHTML);
185 internalAddStringItem(ChromiumDataObjectItem::createFromHTML(html, baseURL));
188 bool ChromiumDataObject::containsFilenames() const
190 for (size_t i = 0; i < m_itemList.size(); ++i)
191 if (m_itemList[i]->isFilename())
196 Vector<String> ChromiumDataObject::filenames() const
198 Vector<String> results;
199 for (size_t i = 0; i < m_itemList.size(); ++i)
200 if (m_itemList[i]->isFilename())
201 results.append(static_cast<File*>(m_itemList[i]->getAsFile().get())->path());
205 void ChromiumDataObject::addFilename(const String& filename, const String& displayName)
207 internalAddFileItem(ChromiumDataObjectItem::createFromFile(File::createWithName(filename, displayName)));
210 void ChromiumDataObject::addSharedBuffer(const String& name, PassRefPtr<SharedBuffer> buffer)
212 internalAddFileItem(ChromiumDataObjectItem::createFromSharedBuffer(name, buffer));
215 ChromiumDataObject::ChromiumDataObject()
216 : m_modifierKeyState(0)
220 ChromiumDataObject::ChromiumDataObject(const ChromiumDataObject& other)
221 : RefCounted<ChromiumDataObject>()
222 , m_itemList(other.m_itemList)
223 , m_modifierKeyState(0)
227 PassRefPtr<ChromiumDataObjectItem> ChromiumDataObject::findStringItem(const String& type) const
229 for (size_t i = 0; i < m_itemList.size(); ++i) {
230 if (m_itemList[i]->kind() == DataTransferItem::kindString && m_itemList[i]->type() == type)
231 return m_itemList[i];
236 bool ChromiumDataObject::internalAddStringItem(PassRefPtr<ChromiumDataObjectItem> item)
238 ASSERT(item->kind() == DataTransferItem::kindString);
239 for (size_t i = 0; i < m_itemList.size(); ++i)
240 if (m_itemList[i]->kind() == DataTransferItem::kindString && m_itemList[i]->type() == item->type())
243 m_itemList.append(item);
247 void ChromiumDataObject::internalAddFileItem(PassRefPtr<ChromiumDataObjectItem> item)
249 ASSERT(item->kind() == DataTransferItem::kindFile);
250 m_itemList.append(item);
253 } // namespace WebCore