1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "webkit/glue/webdropdata.h"
9 #include "base/string_util.h"
10 #include "base/logging.h"
11 #include "base/utf_string_conversions.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebData.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebDragData.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
17 #include "ui/base/clipboard/clipboard.h"
19 using WebKit::WebData
;
20 using WebKit::WebDragData
;
21 using WebKit::WebString
;
22 using WebKit::WebVector
;
24 WebDropData::FileInfo::FileInfo() {
27 WebDropData::FileInfo::FileInfo(const base::string16
& path
,
28 const base::string16
& display_name
)
30 display_name(display_name
) {
33 WebDropData::WebDropData(const WebDragData
& drag_data
)
34 : referrer_policy(WebKit::WebReferrerPolicyDefault
),
35 text(NullableString16(true)),
36 html(NullableString16(true)) {
37 const WebVector
<WebDragData::Item
>& item_list
= drag_data
.items();
38 for (size_t i
= 0; i
< item_list
.size(); ++i
) {
39 const WebDragData::Item
& item
= item_list
[i
];
40 switch (item
.storageType
) {
41 case WebDragData::Item::StorageTypeString
: {
42 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeText
)) {
43 text
= NullableString16(item
.stringData
, false);
46 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeURIList
)) {
47 url
= GURL(item
.stringData
);
48 url_title
= item
.title
;
51 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeDownloadURL
)) {
52 download_metadata
= item
.stringData
;
55 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeHTML
)) {
56 html
= NullableString16(item
.stringData
, false);
57 html_base_url
= item
.baseURL
;
60 custom_data
.insert(std::make_pair(item
.stringType
, item
.stringData
));
63 case WebDragData::Item::StorageTypeBinaryData
:
64 file_contents
.assign(item
.binaryData
.data(),
65 item
.binaryData
.size());
66 file_description_filename
= item
.title
;
68 case WebDragData::Item::StorageTypeFilename
:
69 // TODO(varunjain): This only works on chromeos. Support win/mac/gtk.
70 filenames
.push_back(FileInfo(item
.filenameData
,
71 item
.displayNameData
));
77 WebDropData::WebDropData()
78 : referrer_policy(WebKit::WebReferrerPolicyDefault
),
79 text(NullableString16(true)),
80 html(NullableString16(true)) {
83 WebDropData::~WebDropData() {
86 WebDragData
WebDropData::ToDragData() const {
87 std::vector
<WebDragData::Item
> item_list
;
89 // These fields are currently unused when dragging into WebKit.
90 DCHECK(download_metadata
.empty());
91 DCHECK(file_contents
.empty());
92 DCHECK(file_description_filename
.empty());
94 if (!text
.is_null()) {
95 WebDragData::Item item
;
96 item
.storageType
= WebDragData::Item::StorageTypeString
;
97 item
.stringType
= WebString::fromUTF8(ui::Clipboard::kMimeTypeText
);
98 item
.stringData
= text
.string();
99 item_list
.push_back(item
);
102 // TODO(dcheng): Do we need to distinguish between null and empty URLs? Is it
103 // meaningful to write an empty URL to the clipboard?
104 if (!url
.is_empty()) {
105 WebDragData::Item item
;
106 item
.storageType
= WebDragData::Item::StorageTypeString
;
107 item
.stringType
= WebString::fromUTF8(ui::Clipboard::kMimeTypeURIList
);
108 item
.stringData
= WebString::fromUTF8(url
.spec());
109 item
.title
= url_title
;
110 item_list
.push_back(item
);
113 if (!html
.is_null()) {
114 WebDragData::Item item
;
115 item
.storageType
= WebDragData::Item::StorageTypeString
;
116 item
.stringType
= WebString::fromUTF8(ui::Clipboard::kMimeTypeHTML
);
117 item
.stringData
= html
.string();
118 item
.baseURL
= html_base_url
;
119 item_list
.push_back(item
);
122 for (std::vector
<FileInfo
>::const_iterator it
=
123 filenames
.begin(); it
!= filenames
.end(); ++it
) {
124 WebDragData::Item item
;
125 item
.storageType
= WebDragData::Item::StorageTypeFilename
;
126 item
.filenameData
= it
->path
;
127 item
.displayNameData
= it
->display_name
;
128 item_list
.push_back(item
);
131 for (std::map
<base::string16
, base::string16
>::const_iterator it
=
133 it
!= custom_data
.end();
135 WebDragData::Item item
;
136 item
.storageType
= WebDragData::Item::StorageTypeString
;
137 item
.stringType
= it
->first
;
138 item
.stringData
= it
->second
;
139 item_list
.push_back(item
);
144 result
.setItems(item_list
);
145 result
.setFilesystemId(filesystem_id
);