1 /*
2 * Copyright (C) 2011 Google 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 are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31 #include "config.h"
32 #include "DataTransferItemChromium.h"
33
34 #if ENABLE(DATA_TRANSFER_ITEMS)
35
36 #include "Blob.h"
37 #include "Clipboard.h"
38 #include "ClipboardMimeTypes.h"
39 #include "PlatformBridge.h"
40 #include "SharedBuffer.h"
41 #include "StringCallback.h"
42
43 namespace WebCore {
44
createFromPasteboard(PassRefPtr<Clipboard> owner,ScriptExecutionContext * context,const String & type)45 PassRefPtr<DataTransferItemChromium> DataTransferItemChromium::createFromPasteboard(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context, const String& type)
46 {
47 if (type == mimeTypeTextPlain || type == mimeTypeTextHTML)
48 return adoptRef(new DataTransferItemChromium(owner, context, PasteboardSource, DataTransferItem::kindString, type, ""));
49 return adoptRef(new DataTransferItemChromium(owner, context, PasteboardSource, DataTransferItem::kindFile, type, ""));
50 }
51
create(PassRefPtr<Clipboard> owner,ScriptExecutionContext * context,const String & data,const String & type)52 PassRefPtr<DataTransferItemChromium> DataTransferItemChromium::create(PassRefPtr<Clipboard> owner,
53 ScriptExecutionContext* context,
54 const String& data,
55 const String& type)
56 {
57 return adoptRef(new DataTransferItemChromium(owner, context, InternalSource, DataTransferItem::kindString, type, data));
58 }
59
DataTransferItemChromium(PassRefPtr<Clipboard> owner,ScriptExecutionContext * context,DataSource source,const String & kind,const String & type,const String & data)60 DataTransferItemChromium::DataTransferItemChromium(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context, DataSource source, const String& kind, const String& type, const String& data)
61 : m_owner(owner)
62 , m_context(context)
63 , m_source(source)
64 , m_kind(kind)
65 , m_type(type)
66 , m_data(data)
67 {
68 }
69
kind() const70 String DataTransferItemChromium::kind() const
71 {
72 if (m_owner->policy() == ClipboardNumb)
73 return String();
74 return m_kind;
75 }
76
type() const77 String DataTransferItemChromium::type() const
78 {
79 if (m_owner->policy() == ClipboardNumb)
80 return String();
81 return m_type;
82 }
83
getAsString(PassRefPtr<StringCallback> callback)84 void DataTransferItemChromium::getAsString(PassRefPtr<StringCallback> callback)
85 {
86 if ((m_owner->policy() != ClipboardReadable && m_owner->policy() != ClipboardWritable)
87 || m_kind != kindString)
88 return;
89 if (m_source == InternalSource) {
90 callback->scheduleCallback(m_context, m_data);
91 return;
92 }
93
94 ASSERT(m_source == PasteboardSource);
95 // This is ugly but there's no real alternative.
96 if (m_type == mimeTypeTextPlain) {
97 callback->scheduleCallback(m_context, PlatformBridge::clipboardReadPlainText(PasteboardPrivate::StandardBuffer));
98 return;
99 }
100 if (m_type == mimeTypeTextHTML) {
101 String html;
102 KURL ignoredSourceURL;
103 PlatformBridge::clipboardReadHTML(PasteboardPrivate::StandardBuffer, &html, &ignoredSourceURL);
104 callback->scheduleCallback(m_context, html);
105 return;
106 }
107 ASSERT_NOT_REACHED();
108 }
109
getAsFile()110 PassRefPtr<Blob> DataTransferItemChromium::getAsFile()
111 {
112 if (m_source == InternalSource)
113 return 0;
114
115 ASSERT(m_source == PasteboardSource);
116 if (m_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 = PlatformBridge::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());
133 }
134 return 0;
135 }
136
137 } // namespace WebCore
138
139 #endif // ENABLE(DATA_TRANSFER_ITEMS)
140