initial import
[vuplus_webkit] / Source / WebCore / platform / chromium / DataTransferItemChromium.cpp
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 "ClipboardChromium.h"
39 #include "ClipboardMimeTypes.h"
40 #include "PlatformSupport.h"
41 #include "SharedBuffer.h"
42 #include "StringCallback.h"
43
44 namespace WebCore {
45
46 PassRefPtr<DataTransferItemChromium> DataTransferItemChromium::createFromPasteboard(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context, const String& type)
47 {
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, ""));
51 }
52
53 PassRefPtr<DataTransferItemChromium> DataTransferItemChromium::create(PassRefPtr<Clipboard> owner,
54                                                                       ScriptExecutionContext* context,
55                                                                       const String& data,
56                                                                       const String& type)
57 {
58     return adoptRef(new DataTransferItemChromium(owner, context, InternalSource, DataTransferItem::kindString, type, data));
59 }
60
61 PassRefPtr<DataTransferItem> DataTransferItem::create(PassRefPtr<Clipboard> owner,
62                                                       ScriptExecutionContext* context,
63                                                       const String& data,
64                                                       const String& type)
65 {
66     return DataTransferItemChromium::create(owner, context, data, type);
67 }
68
69 DataTransferItemChromium::DataTransferItemChromium(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context, DataSource source, const String& kind, const String& type, const String& data)
70     : DataTransferItem(owner, kind, type)
71     , m_context(context)
72     , m_source(source)
73     , m_data(data)
74 {
75 }
76
77 void DataTransferItemChromium::getAsString(PassRefPtr<StringCallback> callback)
78 {
79     if ((owner()->policy() != ClipboardReadable && owner()->policy() != ClipboardWritable)
80         || kind() != kindString)
81         return;
82
83     if (static_cast<ClipboardChromium*>(owner())->platformClipboardChanged())
84         return;
85
86     if (m_source == InternalSource) {
87         callback->scheduleCallback(m_context, m_data);
88         return;
89     }
90
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));
95         return;
96     }
97     if (type() == mimeTypeTextHTML) {
98         String html;
99         KURL ignoredSourceURL;
100         PlatformSupport::clipboardReadHTML(PasteboardPrivate::StandardBuffer, &html, &ignoredSourceURL);
101         callback->scheduleCallback(m_context, html);
102         return;
103     }
104     ASSERT_NOT_REACHED();
105 }
106
107 PassRefPtr<Blob> DataTransferItemChromium::getAsFile()
108 {
109     if (m_source == InternalSource)
110         return 0;
111
112     if (static_cast<ClipboardChromium*>(owner())->platformClipboardChanged())
113         return 0;
114
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());
133     }
134     return 0;
135 }
136
137 } // namespace WebCore
138
139 #endif // ENABLE(DATA_TRANSFER_ITEMS)