blob: 84f50a3b748ec6b9a65ef4871b7575a66ac69659 [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
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
Torne (Richard Coles)09380292014-02-21 12:17:33 +000033#include "core/clipboard/DataObject.h"
34#include "core/clipboard/DataTransferItem.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000035#include "heap/Handle.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010036#include "modules/filesystem/DraggedIsolatedFileSystem.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010037#include "platform/clipboard/ClipboardMimeTypes.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010038#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"
Ben Murdoch591b9582013-07-10 11:41:44 +010043#include "wtf/HashMap.h"
44#include "wtf/PassRefPtr.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000045
46using namespace WebCore;
47
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000048namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000049
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000050void WebDragData::initialize()
51{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000052 m_private = DataObject::create();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000053}
54
55void WebDragData::reset()
56{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000057 m_private.reset();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000058}
59
60void WebDragData::assign(const WebDragData& other)
61{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000062 m_private = other.m_private;
63}
64
65WebDragData::WebDragData(const PassRefPtrWillBeRawPtr<WebCore::DataObject>& object)
66{
67 m_private = object;
68}
69
70WebDragData& WebDragData::operator=(const PassRefPtrWillBeRawPtr<WebCore::DataObject>& object)
71{
72 m_private = object;
73 return *this;
74}
75
76DataObject* WebDragData::getValue() const
77{
78 return m_private.get();
79}
80
81void WebDragData::ensureMutable()
82{
83 ASSERT(!isNull());
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000084}
85
86WebVector<WebDragData::Item> WebDragData::items() const
87{
88 Vector<Item> itemList;
89 for (size_t i = 0; i < m_private->length(); ++i) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +000090 DataObjectItem* originalItem = m_private->item(i).get();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000091 WebDragData::Item item;
Torne (Richard Coles)09380292014-02-21 12:17:33 +000092 if (originalItem->kind() == DataObjectItem::StringKind) {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000093 item.storageType = Item::StorageTypeString;
94 item.stringType = originalItem->type();
Torne (Richard Coles)09380292014-02-21 12:17:33 +000095 item.stringData = originalItem->getAsString();
96 } else if (originalItem->kind() == DataObjectItem::FileKind) {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000097 if (originalItem->sharedBuffer()) {
98 item.storageType = Item::StorageTypeBinaryData;
99 item.binaryData = originalItem->sharedBuffer();
100 } else if (originalItem->isFilename()) {
101 item.storageType = Item::StorageTypeFilename;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000102 RefPtrWillBeRawPtr<WebCore::Blob> blob = originalItem->getAsFile();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000103 if (blob->isFile()) {
104 File* file = toFile(blob.get());
105 item.filenameData = file->path();
106 item.displayNameData = file->name();
107 } else
108 ASSERT_NOT_REACHED();
109 } else
110 ASSERT_NOT_REACHED();
111 } else
112 ASSERT_NOT_REACHED();
113 item.title = originalItem->title();
114 item.baseURL = originalItem->baseURL();
115 itemList.append(item);
116 }
117 return itemList;
118}
119
120void WebDragData::setItems(const WebVector<Item>& itemList)
121{
122 m_private->clearAll();
123 for (size_t i = 0; i < itemList.size(); ++i)
124 addItem(itemList[i]);
125}
126
127void WebDragData::addItem(const Item& item)
128{
129 ensureMutable();
130 switch (item.storageType) {
131 case Item::StorageTypeString:
132 if (String(item.stringType) == mimeTypeTextURIList)
133 m_private->setURLAndTitle(item.stringData, item.title);
134 else if (String(item.stringType) == mimeTypeTextHTML)
135 m_private->setHTMLAndBaseURL(item.stringData, item.baseURL);
136 else
137 m_private->setData(item.stringType, item.stringData);
138 return;
139 case Item::StorageTypeFilename:
140 m_private->addFilename(item.filenameData, item.displayNameData);
141 return;
142 case Item::StorageTypeBinaryData:
143 // This should never happen when dragging in.
144 ASSERT_NOT_REACHED();
145 }
146}
147
148WebString WebDragData::filesystemId() const
149{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000150 ASSERT(!isNull());
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000151 DraggedIsolatedFileSystem* filesystem = DraggedIsolatedFileSystem::from(m_private.get());
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000152 if (filesystem)
153 return filesystem->filesystemId();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000154 return WebString();
155}
156
157void WebDragData::setFilesystemId(const WebString& filesystemId)
158{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000159 // The ID is an opaque string, given by and validated by chromium port.
160 ensureMutable();
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000161 DraggedIsolatedFileSystem::provideTo(*m_private.get(), DraggedIsolatedFileSystem::supplementName(), DraggedIsolatedFileSystem::create(filesystemId));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000162}
163
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000164} // namespace blink