2 * Copyright (C) 2011 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/DataObjectItem.h"
34 #include "core/clipboard/Pasteboard.h"
35 #include "core/fileapi/Blob.h"
36 #include "platform/clipboard/ClipboardMimeTypes.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebClipboard.h"
42 PassRefPtrWillBeRawPtr<DataObjectItem> DataObjectItem::createFromString(const String& type, const String& data)
44 RefPtrWillBeRawPtr<DataObjectItem> item = adoptRefWillBeNoop(new DataObjectItem(StringKind, type));
46 return item.release();
49 PassRefPtrWillBeRawPtr<DataObjectItem> DataObjectItem::createFromFile(File* file)
51 RefPtrWillBeRawPtr<DataObjectItem> item = adoptRefWillBeNoop(new DataObjectItem(FileKind, file->type()));
53 return item.release();
56 PassRefPtrWillBeRawPtr<DataObjectItem> DataObjectItem::createFromURL(const String& url, const String& title)
58 RefPtrWillBeRawPtr<DataObjectItem> item = adoptRefWillBeNoop(new DataObjectItem(StringKind, mimeTypeTextURIList));
60 item->m_title = title;
61 return item.release();
64 PassRefPtrWillBeRawPtr<DataObjectItem> DataObjectItem::createFromHTML(const String& html, const KURL& baseURL)
66 RefPtrWillBeRawPtr<DataObjectItem> item = adoptRefWillBeNoop(new DataObjectItem(StringKind, mimeTypeTextHTML));
68 item->m_baseURL = baseURL;
69 return item.release();
72 PassRefPtrWillBeRawPtr<DataObjectItem> DataObjectItem::createFromSharedBuffer(const String& name, PassRefPtr<SharedBuffer> buffer)
74 RefPtrWillBeRawPtr<DataObjectItem> item = adoptRefWillBeNoop(new DataObjectItem(FileKind, String()));
75 item->m_sharedBuffer = buffer;
77 return item.release();
80 PassRefPtrWillBeRawPtr<DataObjectItem> DataObjectItem::createFromPasteboard(const String& type, uint64_t sequenceNumber)
82 if (type == mimeTypeImagePng)
83 return adoptRefWillBeNoop(new DataObjectItem(FileKind, type, sequenceNumber));
84 return adoptRefWillBeNoop(new DataObjectItem(StringKind, type, sequenceNumber));
87 DataObjectItem::DataObjectItem(Kind kind, const String& type)
88 : m_source(InternalSource)
95 DataObjectItem::DataObjectItem(Kind kind, const String& type, uint64_t sequenceNumber)
96 : m_source(PasteboardSource)
99 , m_sequenceNumber(sequenceNumber)
103 Blob* DataObjectItem::getAsFile() const
105 if (kind() != FileKind)
108 if (m_source == InternalSource) {
111 ASSERT(m_sharedBuffer);
112 // FIXME: This code is currently impossible--we never populate m_sharedBuffer when dragging
113 // in. At some point though, we may need to support correctly converting a shared buffer
118 ASSERT(m_source == PasteboardSource);
119 if (type() == mimeTypeImagePng) {
120 // FIXME: This is pretty inefficient. We copy the data from the browser
121 // to the renderer. We then place it in a blob in WebKit, which
122 // registers it and copies it *back* to the browser. When a consumer
123 // wants to read the data, we then copy the data back into the renderer.
124 // https://bugs.webkit.org/show_bug.cgi?id=58107 has been filed to track
125 // improvements to this code (in particular, add a registerClipboardBlob
126 // method to the blob registry; that way the data is only copied over
127 // into the renderer when it's actually read, not when the blob is
128 // initially constructed).
129 RefPtr<SharedBuffer> data = static_cast<PassRefPtr<SharedBuffer> >(blink::Platform::current()->clipboard()->readImage(blink::WebClipboard::BufferStandard));
130 OwnPtr<BlobData> blobData = BlobData::create();
131 blobData->appendBytes(data->data(), data->size());
132 blobData->setContentType(mimeTypeImagePng);
133 return Blob::create(BlobDataHandle::create(blobData.release(), data->size()));
139 String DataObjectItem::getAsString() const
141 ASSERT(m_kind == StringKind);
143 if (m_source == InternalSource)
146 ASSERT(m_source == PasteboardSource);
148 blink::WebClipboard::Buffer buffer = Pasteboard::generalPasteboard()->buffer();
150 // This is ugly but there's no real alternative.
151 if (m_type == mimeTypeTextPlain) {
152 data = blink::Platform::current()->clipboard()->readPlainText(buffer);
153 } else if (m_type == mimeTypeTextHTML) {
154 blink::WebURL ignoredSourceURL;
156 data = blink::Platform::current()->clipboard()->readHTML(buffer, &ignoredSourceURL, &ignored, &ignored);
158 data = blink::Platform::current()->clipboard()->readCustomData(buffer, m_type);
161 return blink::Platform::current()->clipboard()->sequenceNumber(buffer) == m_sequenceNumber ? data : String();
164 bool DataObjectItem::isFilename() const
166 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=81261: When we properly support File dragout,
167 // we'll need to make sure this works as expected for DragDataChromium.
168 return m_kind == FileKind && m_file;
171 void DataObjectItem::trace(Visitor* visitor)
173 visitor->trace(m_file);