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 "DataTransferItemChromium.h"
34 #if ENABLE(DATA_TRANSFER_ITEMS)
37 #include "Clipboard.h"
38 #include "ClipboardChromium.h"
39 #include "ClipboardMimeTypes.h"
40 #include "PlatformSupport.h"
41 #include "SharedBuffer.h"
42 #include "StringCallback.h"
46 PassRefPtr<DataTransferItemChromium> DataTransferItemChromium::createFromPasteboard(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context, const String& type)
48 if (type == mimeTypeTextPlain || type == mimeTypeTextHTML)
49 return adoptRef(new DataTransferItemChromium(owner, context, PasteboardSource, DataTransferItem::kindString, type, ""));
50 return adoptRef(new DataTransferItemChromium(owner, context, PasteboardSource, DataTransferItem::kindFile, type, ""));
53 PassRefPtr<DataTransferItemChromium> DataTransferItemChromium::create(PassRefPtr<Clipboard> owner,
54 ScriptExecutionContext* context,
58 return adoptRef(new DataTransferItemChromium(owner, context, InternalSource, DataTransferItem::kindString, type, data));
61 PassRefPtr<DataTransferItem> DataTransferItem::create(PassRefPtr<Clipboard> owner,
62 ScriptExecutionContext* context,
66 return DataTransferItemChromium::create(owner, context, data, type);
69 DataTransferItemChromium::DataTransferItemChromium(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context, DataSource source, const String& kind, const String& type, const String& data)
70 : DataTransferItem(owner, kind, type)
77 void DataTransferItemChromium::getAsString(PassRefPtr<StringCallback> callback)
79 if ((owner()->policy() != ClipboardReadable && owner()->policy() != ClipboardWritable)
80 || kind() != kindString)
83 if (static_cast<ClipboardChromium*>(owner())->platformClipboardChanged())
86 if (m_source == InternalSource) {
87 callback->scheduleCallback(m_context, m_data);
91 ASSERT(m_source == PasteboardSource);
92 // This is ugly but there's no real alternative.
93 if (type() == mimeTypeTextPlain) {
94 callback->scheduleCallback(m_context, PlatformSupport::clipboardReadPlainText(PasteboardPrivate::StandardBuffer));
97 if (type() == mimeTypeTextHTML) {
99 KURL ignoredSourceURL;
100 PlatformSupport::clipboardReadHTML(PasteboardPrivate::StandardBuffer, &html, &ignoredSourceURL);
101 callback->scheduleCallback(m_context, html);
104 ASSERT_NOT_REACHED();
107 PassRefPtr<Blob> DataTransferItemChromium::getAsFile()
109 if (m_source == InternalSource)
112 if (static_cast<ClipboardChromium*>(owner())->platformClipboardChanged())
115 ASSERT(m_source == PasteboardSource);
116 if (type() == mimeTypeImagePng) {
117 // FIXME: This is pretty inefficient. We copy the data from the browser
118 // to the renderer. We then place it in a blob in WebKit, which
119 // registers it and copies it *back* to the browser. When a consumer
120 // wants to read the data, we then copy the data back into the renderer.
121 // https://bugs.webkit.org/show_bug.cgi?id=58107 has been filed to track
122 // improvements to this code (in particular, add a registerClipboardBlob
123 // method to the blob registry; that way the data is only copied over
124 // into the renderer when it's actually read, not when the blob is
125 // initially constructed).
126 RefPtr<SharedBuffer> data = PlatformSupport::clipboardReadImage(PasteboardPrivate::StandardBuffer);
127 RefPtr<RawData> rawData = RawData::create();
128 rawData->mutableData()->append(data->data(), data->size());
129 OwnPtr<BlobData> blobData = BlobData::create();
130 blobData->appendData(rawData, 0, -1);
131 blobData->setContentType(mimeTypeImagePng);
132 return Blob::create(blobData.release(), data->size());
137 } // namespace WebCore
139 #endif // ENABLE(DATA_TRANSFER_ITEMS)