[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 1 | // Copyright 2013 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. | ||||
4 | |||||
5 | #include "content/renderer/drop_data_builder.h" | ||||
6 | |||||
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | #include <stddef.h> |
8 | |||||
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
10 | #include "content/public/common/drop_data.h" | ||||
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 11 | #include "third_party/blink/public/platform/file_path_conversion.h" |
12 | #include "third_party/blink/public/platform/url_conversion.h" | ||||
13 | #include "third_party/blink/public/platform/web_drag_data.h" | ||||
14 | #include "third_party/blink/public/platform/web_string.h" | ||||
15 | #include "third_party/blink/public/platform/web_vector.h" | ||||
Maksim Sisov | a33072d2 | 2019-01-24 15:44:25 | [diff] [blame] | 16 | #include "ui/base/clipboard/clipboard_constants.h" |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 17 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 18 | using blink::WebDragData; |
kinuko | 365b58f | 2017-01-26 03:15:36 | [diff] [blame] | 19 | using blink::WebString; |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 20 | using blink::WebVector; |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 21 | |
22 | namespace content { | ||||
23 | |||||
ajose | 1e515a6 | 2015-07-28 23:42:27 | [diff] [blame] | 24 | // static |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 25 | DropData DropDataBuilder::Build(const WebDragData& drag_data) { |
26 | DropData result; | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 27 | result.key_modifiers = drag_data.ModifierKeyState(); |
Richard Li | 49fe04d | 2018-10-21 09:07:19 | [diff] [blame] | 28 | result.referrer_policy = network::mojom::ReferrerPolicy::kDefault; |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 29 | |
Darwin Huang | 87ffab86 | 2018-12-08 01:52:47 | [diff] [blame] | 30 | for (const WebDragData::Item& item : drag_data.Items()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 31 | switch (item.storage_type) { |
32 | case WebDragData::Item::kStorageTypeString: { | ||||
33 | base::string16 str_type(item.string_type.Utf16()); | ||||
Maksim Sisov | a33072d2 | 2019-01-24 15:44:25 | [diff] [blame] | 34 | if (base::EqualsASCII(str_type, ui::kMimeTypeText)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 35 | result.text = WebString::ToNullableString16(item.string_data); |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 36 | break; |
37 | } | ||||
Maksim Sisov | a33072d2 | 2019-01-24 15:44:25 | [diff] [blame] | 38 | if (base::EqualsASCII(str_type, ui::kMimeTypeURIList)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 39 | result.url = blink::WebStringToGURL(item.string_data); |
40 | result.url_title = item.title.Utf16(); | ||||
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 41 | break; |
42 | } | ||||
Maksim Sisov | a33072d2 | 2019-01-24 15:44:25 | [diff] [blame] | 43 | if (base::EqualsASCII(str_type, ui::kMimeTypeDownloadURL)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 44 | result.download_metadata = item.string_data.Utf16(); |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 45 | break; |
46 | } | ||||
Maksim Sisov | a33072d2 | 2019-01-24 15:44:25 | [diff] [blame] | 47 | if (base::EqualsASCII(str_type, ui::kMimeTypeHTML)) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 48 | result.html = WebString::ToNullableString16(item.string_data); |
49 | result.html_base_url = item.base_url; | ||||
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 50 | break; |
51 | } | ||||
52 | result.custom_data.insert( | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 53 | std::make_pair(item.string_type.Utf16(), item.string_data.Utf16())); |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 54 | break; |
55 | } | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 56 | case WebDragData::Item::kStorageTypeBinaryData: |
Florin Malita | a7ecc49b | 2017-07-24 17:21:05 | [diff] [blame] | 57 | DCHECK(result.file_contents.empty()); |
58 | result.file_contents.reserve(item.binary_data.size()); | ||||
59 | item.binary_data.ForEachSegment([&result](const char* segment, | ||||
60 | size_t segment_size, | ||||
61 | size_t segment_offset) { | ||||
62 | result.file_contents.append(segment, segment_size); | ||||
63 | return true; | ||||
64 | }); | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 65 | result.file_contents_source_url = item.binary_data_source_url; |
dcheng | 3dd8561 | 2017-02-08 10:46:23 | [diff] [blame] | 66 | #if defined(OS_WIN) |
67 | result.file_contents_filename_extension = | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 68 | item.binary_data_filename_extension.Utf16(); |
dcheng | 3dd8561 | 2017-02-08 10:46:23 | [diff] [blame] | 69 | #else |
70 | result.file_contents_filename_extension = | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 71 | item.binary_data_filename_extension.Utf8(); |
dcheng | 3dd8561 | 2017-02-08 10:46:23 | [diff] [blame] | 72 | #endif |
73 | result.file_contents_content_disposition = | ||||
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 74 | item.binary_data_content_disposition.Utf8(); |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 75 | break; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 76 | case WebDragData::Item::kStorageTypeFilename: |
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 77 | // TODO(varunjain): This only works on chromeos. Support win/mac/gtk. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 78 | result.filenames.push_back( |
79 | ui::FileInfo(blink::WebStringToFilePath(item.filename_data), | ||||
80 | blink::WebStringToFilePath(item.display_name_data))); | ||||
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 81 | break; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 82 | case WebDragData::Item::kStorageTypeFileSystemFile: { |
[email protected] | 60ed95f | 2014-04-23 12:19:48 | [diff] [blame] | 83 | DropData::FileSystemFileInfo info; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 84 | info.url = item.file_system_url; |
85 | info.size = item.file_system_file_size; | ||||
86 | info.filesystem_id = item.file_system_id.Ascii(); | ||||
[email protected] | 60ed95f | 2014-04-23 12:19:48 | [diff] [blame] | 87 | result.file_system_files.push_back(info); |
88 | break; | ||||
89 | } | ||||
[email protected] | dc293a7 | 2013-07-01 11:11:22 | [diff] [blame] | 90 | } |
91 | } | ||||
92 | |||||
93 | return result; | ||||
94 | } | ||||
95 | |||||
96 | } // namespace content |