1 /*
2 * Copyright (C) 2009 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
33 #include "core/clipboard/DataObject.h"
34 #include "core/clipboard/DataTransferItem.h"
35 #include "modules/filesystem/DraggedIsolatedFileSystem.h"
36 #include "platform/clipboard/ClipboardMimeTypes.h"
37 #include "platform/heap/Handle.h"
38 #include "public/platform/WebData.h"
39 #include "public/platform/WebDragData.h"
40 #include "public/platform/WebString.h"
41 #include "public/platform/WebURL.h"
42 #include "public/platform/WebVector.h"
43 #include "wtf/HashMap.h"
44 #include "wtf/PassRefPtr.h"
45
46 using namespace WebCore;
47
48 namespace blink {
49
initialize()50 void WebDragData::initialize()
51 {
52 m_private = DataObject::create();
53 }
54
reset()55 void WebDragData::reset()
56 {
57 m_private.reset();
58 }
59
assign(const WebDragData & other)60 void WebDragData::assign(const WebDragData& other)
61 {
62 m_private = other.m_private;
63 }
64
WebDragData(const PassRefPtrWillBeRawPtr<WebCore::DataObject> & object)65 WebDragData::WebDragData(const PassRefPtrWillBeRawPtr<WebCore::DataObject>& object)
66 {
67 m_private = object;
68 }
69
operator =(const PassRefPtrWillBeRawPtr<WebCore::DataObject> & object)70 WebDragData& WebDragData::operator=(const PassRefPtrWillBeRawPtr<WebCore::DataObject>& object)
71 {
72 m_private = object;
73 return *this;
74 }
75
getValue() const76 DataObject* WebDragData::getValue() const
77 {
78 return m_private.get();
79 }
80
ensureMutable()81 void WebDragData::ensureMutable()
82 {
83 ASSERT(!isNull());
84 }
85
items() const86 WebVector<WebDragData::Item> WebDragData::items() const
87 {
88 Vector<Item> itemList;
89 for (size_t i = 0; i < m_private->length(); ++i) {
90 DataObjectItem* originalItem = m_private->item(i).get();
91 WebDragData::Item item;
92 if (originalItem->kind() == DataObjectItem::StringKind) {
93 item.storageType = Item::StorageTypeString;
94 item.stringType = originalItem->type();
95 item.stringData = originalItem->getAsString();
96 } else if (originalItem->kind() == DataObjectItem::FileKind) {
97 if (originalItem->sharedBuffer()) {
98 item.storageType = Item::StorageTypeBinaryData;
99 item.binaryData = originalItem->sharedBuffer();
100 } else if (originalItem->isFilename()) {
101 RefPtrWillBeRawPtr<WebCore::Blob> blob = originalItem->getAsFile();
102 if (blob->isFile()) {
103 File* file = toFile(blob.get());
104 if (!file->path().isEmpty()) {
105 item.storageType = Item::StorageTypeFilename;
106 item.filenameData = file->path();
107 item.displayNameData = file->name();
108 } else if (!file->fileSystemURL().isEmpty()) {
109 item.storageType = Item::StorageTypeFileSystemFile;
110 item.fileSystemURL = file->fileSystemURL();
111 item.fileSystemFileSize = file->size();
112 } else {
113 ASSERT_NOT_REACHED();
114 }
115 } else
116 ASSERT_NOT_REACHED();
117 } else
118 ASSERT_NOT_REACHED();
119 } else
120 ASSERT_NOT_REACHED();
121 item.title = originalItem->title();
122 item.baseURL = originalItem->baseURL();
123 itemList.append(item);
124 }
125 return itemList;
126 }
127
setItems(const WebVector<Item> & itemList)128 void WebDragData::setItems(const WebVector<Item>& itemList)
129 {
130 m_private->clearAll();
131 for (size_t i = 0; i < itemList.size(); ++i)
132 addItem(itemList[i]);
133 }
134
addItem(const Item & item)135 void WebDragData::addItem(const Item& item)
136 {
137 ensureMutable();
138 switch (item.storageType) {
139 case Item::StorageTypeString:
140 if (String(item.stringType) == mimeTypeTextURIList)
141 m_private->setURLAndTitle(item.stringData, item.title);
142 else if (String(item.stringType) == mimeTypeTextHTML)
143 m_private->setHTMLAndBaseURL(item.stringData, item.baseURL);
144 else
145 m_private->setData(item.stringType, item.stringData);
146 return;
147 case Item::StorageTypeFilename:
148 m_private->addFilename(item.filenameData, item.displayNameData);
149 return;
150 case Item::StorageTypeBinaryData:
151 // This should never happen when dragging in.
152 ASSERT_NOT_REACHED();
153 return;
154 case Item::StorageTypeFileSystemFile:
155 {
156 FileMetadata fileMetadata;
157 fileMetadata.length = item.fileSystemFileSize;
158 m_private->add(File::createForFileSystemFile(item.fileSystemURL, fileMetadata));
159 }
160 return;
161 }
162 }
163
filesystemId() const164 WebString WebDragData::filesystemId() const
165 {
166 ASSERT(!isNull());
167 return m_private.get()->filesystemId();
168 }
169
setFilesystemId(const WebString & filesystemId)170 void WebDragData::setFilesystemId(const WebString& filesystemId)
171 {
172 // The ID is an opaque string, given by and validated by chromium port.
173 ensureMutable();
174 DraggedIsolatedFileSystem::provideTo(*m_private.get(), DraggedIsolatedFileSystem::supplementName(), DraggedIsolatedFileSystem::create(*m_private.get(), filesystemId));
175 }
176
177 } // namespace blink
178