Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 5 | #include "content/browser/renderer_host/data_transfer_util.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 11 | #include "base/check.h" |
Ken Rockot | c73e50a7 | 2020-10-27 00:32:23 | [diff] [blame] | 12 | #include "base/containers/span.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
Lei Zhang | f40ac9a61 | 2025-06-02 22:35:10 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
Claudio DeSouza | 45a3369 | 2023-04-19 12:22:19 | [diff] [blame] | 16 | #include "base/uuid.h" |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 17 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
Austin Sullivan | 559bb38 | 2021-01-26 00:52:43 | [diff] [blame] | 18 | #include "content/browser/file_system_access/file_system_access_manager_impl.h" |
Michael Thiessen | 2d864d9 | 2023-04-06 15:20:56 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 20 | #include "mojo/public/cpp/bindings/pending_remote.h" |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 21 | #include "net/base/mime_util.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 22 | #include "services/network/public/mojom/referrer_policy.mojom-shared.h" |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 23 | #include "storage/browser/blob/blob_storage_context.h" |
Marijn Kruisselbrink | c100047 | 2020-10-01 22:19:54 | [diff] [blame] | 24 | #include "storage/browser/file_system/external_mount_points.h" |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 25 | #include "storage/browser/file_system/file_system_context.h" |
| 26 | #include "third_party/blink/public/mojom/blob/serialized_blob.mojom.h" |
Sharon Yang | 2abe198 | 2021-10-01 15:56:34 | [diff] [blame] | 27 | #include "third_party/blink/public/mojom/drag/drag.mojom.h" |
Joel Hockey | da00f2b7 | 2021-02-17 09:39:52 | [diff] [blame] | 28 | #include "third_party/blink/public/mojom/file_system_access/file_system_access_data_transfer_token.mojom.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 29 | #include "ui/base/clipboard/clipboard_constants.h" |
Joel Hockey | 5192d0a8 | 2021-01-30 18:01:58 | [diff] [blame] | 30 | #include "ui/base/clipboard/file_info.h" |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 31 | #include "url/gurl.h" |
| 32 | |
| 33 | namespace content { |
| 34 | |
Marijn Kruisselbrink | c100047 | 2020-10-01 22:19:54 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | // On Chrome OS paths that exist on an external mount point need to be treated |
Austin Sullivan | 559bb38 | 2021-01-26 00:52:43 | [diff] [blame] | 38 | // differently to make sure the File System Access code accesses these paths via |
Marijn Kruisselbrink | c100047 | 2020-10-01 22:19:54 | [diff] [blame] | 39 | // the correct file system backend. This method checks if this is the case, and |
Austin Sullivan | 559bb38 | 2021-01-26 00:52:43 | [diff] [blame] | 40 | // updates `entry_path` to the path that should be used by the File System |
| 41 | // Access implementation. |
Joel Hockey | 46029090 | 2024-10-08 22:49:20 | [diff] [blame] | 42 | content::PathType MaybeRemapPath(base::FilePath* entry_path) { |
Georg Neis | 35ff854b | 2024-12-17 02:02:08 | [diff] [blame] | 43 | #if BUILDFLAG(IS_CHROMEOS) |
Marijn Kruisselbrink | c100047 | 2020-10-01 22:19:54 | [diff] [blame] | 44 | base::FilePath virtual_path; |
| 45 | auto* external_mount_points = |
| 46 | storage::ExternalMountPoints::GetSystemInstance(); |
| 47 | if (external_mount_points->GetVirtualPath(*entry_path, &virtual_path)) { |
| 48 | *entry_path = std::move(virtual_path); |
Joel Hockey | 46029090 | 2024-10-08 22:49:20 | [diff] [blame] | 49 | return content::PathType::kExternal; |
Marijn Kruisselbrink | c100047 | 2020-10-01 22:19:54 | [diff] [blame] | 50 | } |
| 51 | #endif |
Joel Hockey | 46029090 | 2024-10-08 22:49:20 | [diff] [blame] | 52 | return content::PathType::kLocal; |
Marijn Kruisselbrink | c100047 | 2020-10-01 22:19:54 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 57 | std::vector<blink::mojom::DataTransferFilePtr> FileInfosToDataTransferFiles( |
| 58 | const std::vector<ui::FileInfo>& filenames, |
| 59 | FileSystemAccessManagerImpl* file_system_access_manager, |
| 60 | int child_id) { |
| 61 | std::vector<blink::mojom::DataTransferFilePtr> result; |
| 62 | for (const ui::FileInfo& file_info : filenames) { |
| 63 | blink::mojom::DataTransferFilePtr file = |
| 64 | blink::mojom::DataTransferFile::New(); |
| 65 | file->path = file_info.path; |
| 66 | file->display_name = file_info.display_name; |
| 67 | mojo::PendingRemote<blink::mojom::FileSystemAccessDataTransferToken> |
| 68 | pending_token; |
| 69 | base::FilePath entry_path = file_info.path; |
Joel Hockey | 46029090 | 2024-10-08 22:49:20 | [diff] [blame] | 70 | content::PathType path_type = MaybeRemapPath(&entry_path); |
| 71 | base::FilePath display_name = !file_info.display_name.empty() |
| 72 | ? file_info.display_name |
| 73 | : entry_path.BaseName(); |
Joel Hockey | ed3d0b9 | 2025-03-11 01:48:43 | [diff] [blame] | 74 | if (entry_path.empty() || display_name.empty()) { |
| 75 | continue; |
| 76 | } |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 77 | file_system_access_manager->CreateFileSystemAccessDataTransferToken( |
Joel Hockey | 46029090 | 2024-10-08 22:49:20 | [diff] [blame] | 78 | content::PathInfo(path_type, entry_path, display_name.AsUTF8Unsafe()), |
| 79 | child_id, pending_token.InitWithNewPipeAndPassReceiver()); |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 80 | file->file_system_access_token = std::move(pending_token); |
| 81 | result.push_back(std::move(file)); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 86 | std::vector<blink::mojom::DragItemFileSystemFilePtr> |
| 87 | FileSystemFileInfosToDragItemFileSystemFilePtr( |
| 88 | std::vector<DropData::FileSystemFileInfo> file_system_file_infos, |
| 89 | FileSystemAccessManagerImpl* file_system_access_manager, |
| 90 | scoped_refptr<content::ChromeBlobStorageContext> context) { |
| 91 | std::vector<blink::mojom::DragItemFileSystemFilePtr> result; |
| 92 | for (const content::DropData::FileSystemFileInfo& file_system_file : |
| 93 | file_system_file_infos) { |
| 94 | blink::mojom::DragItemFileSystemFilePtr item = |
| 95 | blink::mojom::DragItemFileSystemFile::New(); |
| 96 | item->url = file_system_file.url; |
| 97 | item->size = file_system_file.size; |
| 98 | item->file_system_id = file_system_file.filesystem_id; |
| 99 | |
| 100 | storage::FileSystemURL file_system_url = |
| 101 | file_system_access_manager->context()->CrackURLInFirstPartyContext( |
| 102 | file_system_file.url); |
| 103 | DCHECK(file_system_url.type() != storage::kFileSystemTypePersistent); |
| 104 | DCHECK(file_system_url.type() != storage::kFileSystemTypeTemporary); |
| 105 | |
Claudio DeSouza | 45a3369 | 2023-04-19 12:22:19 | [diff] [blame] | 106 | std::string uuid = base::Uuid::GenerateRandomV4().AsLowercaseString(); |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 107 | |
| 108 | std::string content_type; |
| 109 | |
Michael Maltsev | d9382b669 | 2025-03-18 02:30:47 | [diff] [blame] | 110 | std::string mime_type; |
| 111 | // TODO(crbug.com/40291155): Historically for blobs created from |
| 112 | // file system URLs we've only considered well known content types to |
| 113 | // avoid leaking the presence of locally installed applications when |
| 114 | // creating blobs from files in the sandboxed file system. However, since |
| 115 | // this code path should only deal with real/"trusted" paths, we could |
| 116 | // consider taking platform defined mime type mappings into account here |
| 117 | // as well. Note that the approach used here must not block or else it |
| 118 | // can't be called from the UI thread (for example, calls to |
| 119 | // GetMimeTypeFromExtension can block). |
| 120 | if (net::GetWellKnownMimeTypeFromFile(file_system_url.path(), &mime_type)) { |
| 121 | content_type = std::move(mime_type); |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 122 | } |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 123 | // TODO(crbug.com/41458368): Consider some kind of fallback type when |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 124 | // the above mime type detection fails. |
| 125 | |
| 126 | mojo::PendingRemote<blink::mojom::Blob> blob_remote; |
| 127 | mojo::PendingReceiver<blink::mojom::Blob> blob_receiver = |
| 128 | blob_remote.InitWithNewPipeAndPassReceiver(); |
| 129 | |
| 130 | item->serialized_blob = blink::mojom::SerializedBlob::New( |
| 131 | uuid, content_type, item->size, std::move(blob_remote)); |
| 132 | |
| 133 | GetIOThreadTaskRunner({})->PostTask( |
| 134 | FROM_HERE, |
| 135 | base::BindOnce( |
| 136 | &ChromeBlobStorageContext::CreateFileSystemBlob, context, |
| 137 | base::WrapRefCounted(file_system_access_manager->context()), |
| 138 | std::move(blob_receiver), std::move(file_system_url), |
| 139 | std::move(uuid), std::move(content_type), item->size, |
| 140 | base::Time())); |
| 141 | |
| 142 | result.push_back(std::move(item)); |
| 143 | } |
| 144 | return result; |
| 145 | } |
| 146 | |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 147 | blink::mojom::DragDataPtr DropDataToDragData( |
| 148 | const DropData& drop_data, |
Austin Sullivan | 559bb38 | 2021-01-26 00:52:43 | [diff] [blame] | 149 | FileSystemAccessManagerImpl* file_system_access_manager, |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 150 | int child_id, |
| 151 | scoped_refptr<ChromeBlobStorageContext> chrome_blob_storage_context) { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 152 | // These fields are currently unused when dragging into Blink. |
| 153 | DCHECK(drop_data.download_metadata.empty()); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 154 | DCHECK(drop_data.file_contents_content_disposition.empty()); |
| 155 | |
| 156 | std::vector<blink::mojom::DragItemPtr> items; |
| 157 | if (drop_data.text) { |
| 158 | blink::mojom::DragItemStringPtr item = blink::mojom::DragItemString::New(); |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 159 | item->string_type = ui::kMimeTypePlainText; |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 160 | item->string_data = *drop_data.text; |
| 161 | items.push_back(blink::mojom::DragItem::NewString(std::move(item))); |
| 162 | } |
| 163 | if (!drop_data.url.is_empty()) { |
| 164 | blink::mojom::DragItemStringPtr item = blink::mojom::DragItemString::New(); |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 165 | item->string_type = ui::kMimeTypeUriList; |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 166 | item->string_data = base::UTF8ToUTF16(drop_data.url.spec()); |
| 167 | item->title = drop_data.url_title; |
| 168 | items.push_back(blink::mojom::DragItem::NewString(std::move(item))); |
| 169 | } |
| 170 | if (drop_data.html) { |
| 171 | blink::mojom::DragItemStringPtr item = blink::mojom::DragItemString::New(); |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 172 | item->string_type = ui::kMimeTypeHtml; |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 173 | item->string_data = *drop_data.html; |
| 174 | item->base_url = drop_data.html_base_url; |
| 175 | items.push_back(blink::mojom::DragItem::NewString(std::move(item))); |
| 176 | } |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 177 | std::vector<blink::mojom::DataTransferFilePtr> files = |
| 178 | FileInfosToDataTransferFiles(drop_data.filenames, |
| 179 | file_system_access_manager, child_id); |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 180 | for (auto& file : files) { |
| 181 | items.push_back(blink::mojom::DragItem::NewFile(std::move(file))); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 182 | } |
Andrew Williams | 1255f392 | 2022-02-07 07:02:13 | [diff] [blame] | 183 | |
| 184 | std::vector<blink::mojom::DragItemFileSystemFilePtr> file_system_files = |
| 185 | FileSystemFileInfosToDragItemFileSystemFilePtr( |
| 186 | drop_data.file_system_files, file_system_access_manager, |
| 187 | std::move(chrome_blob_storage_context)); |
| 188 | for (auto& file_system_file : file_system_files) { |
| 189 | items.push_back( |
| 190 | blink::mojom::DragItem::NewFileSystemFile(std::move(file_system_file))); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 191 | } |
Joel Hockey | a81a94d | 2021-09-15 04:12:27 | [diff] [blame] | 192 | if (drop_data.file_contents_source_url.is_valid()) { |
| 193 | blink::mojom::DragItemBinaryPtr item = blink::mojom::DragItemBinary::New(); |
Peter Kasting | 5f6928c | 2024-11-29 21:25:11 | [diff] [blame] | 194 | item->data = |
| 195 | mojo_base::BigBuffer(base::as_byte_span(drop_data.file_contents)); |
Joel Hockey | 3acd3b0 | 2021-11-19 21:10:20 | [diff] [blame] | 196 | item->is_image_accessible = drop_data.file_contents_image_accessible; |
Joel Hockey | a81a94d | 2021-09-15 04:12:27 | [diff] [blame] | 197 | item->source_url = drop_data.file_contents_source_url; |
| 198 | item->filename_extension = |
| 199 | base::FilePath(drop_data.file_contents_filename_extension); |
| 200 | items.push_back(blink::mojom::DragItem::NewBinary(std::move(item))); |
| 201 | } |
| 202 | for (const std::pair<const std::u16string, std::u16string>& data : |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 203 | drop_data.custom_data) { |
| 204 | blink::mojom::DragItemStringPtr item = blink::mojom::DragItemString::New(); |
| 205 | item->string_type = base::UTF16ToUTF8(data.first); |
| 206 | item->string_data = data.second; |
| 207 | items.push_back(blink::mojom::DragItem::NewString(std::move(item))); |
| 208 | } |
| 209 | |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 210 | return blink::mojom::DragData::New( |
| 211 | std::move(items), |
| 212 | // While this shouldn't be a problem in production code, as the |
| 213 | // real file_system_id should never be empty if used in browser to |
| 214 | // renderer messages, some tests use this function to test renderer to |
| 215 | // browser messages, in which case the field is unused and this will hit |
| 216 | // a DCHECK. |
| 217 | drop_data.filesystem_id.empty() |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 218 | ? std::nullopt |
| 219 | : std::optional<std::string>( |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 220 | base::UTF16ToUTF8(drop_data.filesystem_id)), |
Roger Tawa | 2f912ee0 | 2023-09-28 13:24:56 | [diff] [blame] | 221 | /*force_default_action=*/!drop_data.document_is_handling_drag, |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 222 | drop_data.referrer_policy); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 223 | } |
| 224 | |
Dave Tapuska | d2a5520 | 2020-10-27 17:08:11 | [diff] [blame] | 225 | blink::mojom::DragDataPtr DropMetaDataToDragData( |
| 226 | const std::vector<DropData::Metadata>& drop_meta_data) { |
| 227 | std::vector<blink::mojom::DragItemPtr> items; |
| 228 | |
| 229 | for (const auto& meta_data_item : drop_meta_data) { |
| 230 | if (meta_data_item.kind == DropData::Kind::STRING) { |
| 231 | blink::mojom::DragItemStringPtr item = |
| 232 | blink::mojom::DragItemString::New(); |
| 233 | item->string_type = base::UTF16ToUTF8(meta_data_item.mime_type); |
| 234 | // Have to pass a dummy URL here instead of an empty URL because the |
| 235 | // DropData received by browser_plugins goes through a round trip: |
| 236 | // DropData::MetaData --> WebDragData-->DropData. In the end, DropData |
| 237 | // will contain an empty URL (which means no URL is dragged) if the URL in |
| 238 | // WebDragData is empty. |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 239 | if (base::EqualsASCII(meta_data_item.mime_type, ui::kMimeTypeUriList)) { |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 240 | item->string_data = u"about:dragdrop-placeholder"; |
Dave Tapuska | d2a5520 | 2020-10-27 17:08:11 | [diff] [blame] | 241 | } |
| 242 | items.push_back(blink::mojom::DragItem::NewString(std::move(item))); |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | // TODO(hush): crbug.com/584789. Blink needs to support creating a file with |
| 247 | // just the mimetype. This is needed to drag files to WebView on Android |
| 248 | // platform. |
| 249 | if ((meta_data_item.kind == DropData::Kind::FILENAME) && |
| 250 | !meta_data_item.filename.empty()) { |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 251 | blink::mojom::DataTransferFilePtr item = |
| 252 | blink::mojom::DataTransferFile::New(); |
Dave Tapuska | d2a5520 | 2020-10-27 17:08:11 | [diff] [blame] | 253 | item->path = meta_data_item.filename; |
| 254 | items.push_back(blink::mojom::DragItem::NewFile(std::move(item))); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | if (meta_data_item.kind == DropData::Kind::FILESYSTEMFILE) { |
| 259 | blink::mojom::DragItemFileSystemFilePtr item = |
| 260 | blink::mojom::DragItemFileSystemFile::New(); |
| 261 | item->url = meta_data_item.file_system_url; |
| 262 | items.push_back( |
| 263 | blink::mojom::DragItem::NewFileSystemFile(std::move(item))); |
| 264 | continue; |
| 265 | } |
Joel Hockey | a81a94d | 2021-09-15 04:12:27 | [diff] [blame] | 266 | |
| 267 | if (meta_data_item.kind == DropData::Kind::BINARY) { |
| 268 | blink::mojom::DragItemBinaryPtr item = |
| 269 | blink::mojom::DragItemBinary::New(); |
| 270 | item->source_url = meta_data_item.file_contents_url; |
| 271 | items.push_back(blink::mojom::DragItem::NewBinary(std::move(item))); |
| 272 | continue; |
| 273 | } |
Dave Tapuska | d2a5520 | 2020-10-27 17:08:11 | [diff] [blame] | 274 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 275 | return blink::mojom::DragData::New(std::move(items), std::nullopt, |
Roger Tawa | 2f912ee0 | 2023-09-28 13:24:56 | [diff] [blame] | 276 | /*force_default_action=*/false, |
Dave Tapuska | d2a5520 | 2020-10-27 17:08:11 | [diff] [blame] | 277 | network::mojom::ReferrerPolicy::kDefault); |
| 278 | } |
| 279 | |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 280 | DropData DragDataToDropData(const blink::mojom::DragData& drag_data) { |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 281 | // This field should be empty when dragging from the renderer. |
| 282 | DCHECK(!drag_data.file_system_id); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 283 | |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 284 | DropData result; |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 285 | for (const blink::mojom::DragItemPtr& item : drag_data.items) { |
| 286 | switch (item->which()) { |
Daniel Cheng | 07383d49 | 2022-04-21 15:02:33 | [diff] [blame] | 287 | case blink::mojom::DragItemDataView::Tag::kString: { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 288 | const blink::mojom::DragItemStringPtr& string_item = item->get_string(); |
| 289 | std::string str_type = string_item->string_type; |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 290 | if (str_type == ui::kMimeTypePlainText) { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 291 | result.text = string_item->string_data; |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 292 | } else if (str_type == ui::kMimeTypeUriList) { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 293 | result.url = GURL(string_item->string_data); |
| 294 | if (string_item->title) |
| 295 | result.url_title = *string_item->title; |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 296 | } else if (str_type == ui::kMimeTypeDownloadUrl) { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 297 | result.download_metadata = string_item->string_data; |
| 298 | result.referrer_policy = drag_data.referrer_policy; |
Avi Drissman | 4bbdb200 | 2025-04-03 01:43:44 | [diff] [blame] | 299 | } else if (str_type == ui::kMimeTypeHtml) { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 300 | result.html = string_item->string_data; |
| 301 | if (string_item->base_url) |
| 302 | result.html_base_url = *string_item->base_url; |
| 303 | } else { |
| 304 | result.custom_data.emplace( |
| 305 | base::UTF8ToUTF16(string_item->string_type), |
| 306 | string_item->string_data); |
| 307 | } |
| 308 | break; |
| 309 | } |
Daniel Cheng | 07383d49 | 2022-04-21 15:02:33 | [diff] [blame] | 310 | case blink::mojom::DragItemDataView::Tag::kBinary: { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 311 | DCHECK(result.file_contents.empty()); |
| 312 | |
| 313 | const blink::mojom::DragItemBinaryPtr& binary_item = item->get_binary(); |
Peter Kasting | 5f6928c | 2024-11-29 21:25:11 | [diff] [blame] | 314 | base::span<const uint8_t> contents(binary_item->data); |
Henrique Ferreiro | 35b04f2 | 2020-10-05 09:18:39 | [diff] [blame] | 315 | result.file_contents.assign(contents.begin(), contents.end()); |
Joel Hockey | 3acd3b0 | 2021-11-19 21:10:20 | [diff] [blame] | 316 | result.file_contents_image_accessible = |
| 317 | binary_item->is_image_accessible; |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 318 | result.file_contents_source_url = binary_item->source_url; |
| 319 | result.file_contents_filename_extension = |
| 320 | binary_item->filename_extension.value(); |
| 321 | if (binary_item->content_disposition) { |
| 322 | result.file_contents_content_disposition = |
| 323 | *binary_item->content_disposition; |
| 324 | } |
| 325 | break; |
| 326 | } |
Daniel Cheng | 07383d49 | 2022-04-21 15:02:33 | [diff] [blame] | 327 | case blink::mojom::DragItemDataView::Tag::kFile: { |
Joel Hockey | d75d506 | 2021-02-23 19:53:25 | [diff] [blame] | 328 | const blink::mojom::DataTransferFilePtr& file_item = item->get_file(); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 329 | // TODO(varunjain): This only works on chromeos. Support win/mac/gtk. |
| 330 | result.filenames.emplace_back(file_item->path, file_item->display_name); |
| 331 | break; |
| 332 | } |
Daniel Cheng | 07383d49 | 2022-04-21 15:02:33 | [diff] [blame] | 333 | case blink::mojom::DragItemDataView::Tag::kFileSystemFile: { |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 334 | const blink::mojom::DragItemFileSystemFilePtr& file_system_file_item = |
| 335 | item->get_file_system_file(); |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 336 | // This field should be empty when dragging from the renderer. |
| 337 | DCHECK(!file_system_file_item->file_system_id); |
| 338 | |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 339 | DropData::FileSystemFileInfo info; |
| 340 | info.url = file_system_file_item->url; |
| 341 | info.size = file_system_file_item->size; |
Henrique Ferreiro | a6ea10e5 | 2020-10-27 12:21:25 | [diff] [blame] | 342 | info.filesystem_id = std::string(); |
Henrique Ferreiro | 0bb413bf | 2020-09-17 13:37:54 | [diff] [blame] | 343 | result.file_system_files.push_back(std::move(info)); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | } // namespace content |