1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
|
/*
* Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
* Copyright (C) 2008 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/core/page/drag_controller.h"
#include <memory>
#include "base/memory/scoped_refptr.h"
#include "build/build_config.h"
#include "third_party/blink/public/common/page/drag_operation.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom-blink.h"
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/blink/public/platform/web_drag_data.h"
#include "third_party/blink/renderer/core/clipboard/data_object.h"
#include "third_party/blink/renderer/core/clipboard/data_transfer.h"
#include "third_party/blink/renderer/core/clipboard/data_transfer_access_policy.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/document_fragment.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/node.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/text.h"
#include "third_party/blink/renderer/core/editing/commands/drag_and_drop_command.h"
#include "third_party/blink/renderer/core/editing/drag_caret.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/editor.h"
#include "third_party/blink/renderer/core/editing/ephemeral_range.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/selection_template.h"
#include "third_party/blink/renderer/core/editing/serializers/serialization.h"
#include "third_party/blink/renderer/core/events/text_event.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/html/forms/html_form_element.h"
#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/core/html/html_anchor_element.h"
#include "third_party/blink/renderer/core/html/html_plugin_element.h"
#include "third_party/blink/renderer/core/html/plugin_document.h"
#include "third_party/blink/renderer/core/input/event_handler.h"
#include "third_party/blink/renderer/core/input_type_names.h"
#include "third_party/blink/renderer/core/layout/hit_test_request.h"
#include "third_party/blink/renderer/core/layout/hit_test_result.h"
#include "third_party/blink/renderer/core/layout/layout_image.h"
#include "third_party/blink/renderer/core/layout/layout_theme.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/loader/frame_load_request.h"
#include "third_party/blink/renderer/core/loader/frame_loader.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_content.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/page/drag_data.h"
#include "third_party/blink/renderer/core/page/drag_image.h"
#include "third_party/blink/renderer/core/page/drag_state.h"
#include "third_party/blink/renderer/core/page/focus_controller.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/graphics/image.h"
#include "third_party/blink/renderer/platform/graphics/image_orientation.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_record_builder.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_request.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/shared_buffer.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-blink.h"
#include "ui/display/screen_info.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
namespace blink {
using mojom::blink::FormControlType;
using ui::mojom::blink::DragOperation;
static const int kMaxOriginalImageArea = 1500 * 1500;
static const int kLinkDragBorderInset = 2;
#if BUILDFLAG(IS_ANDROID)
// Android handles drag image transparency at the browser level
static const float kDragImageAlpha = 1.00f;
#else
static const float kDragImageAlpha = 0.75f;
#endif
#if DCHECK_IS_ON()
static bool DragTypeIsValid(DragSourceAction action) {
switch (action) {
case kDragSourceActionDHTML:
case kDragSourceActionImage:
case kDragSourceActionLink:
case kDragSourceActionSelection:
return true;
case kDragSourceActionNone:
return false;
}
NOTREACHED();
}
#endif // DCHECK_IS_ON()
static WebMouseEvent CreateMouseEvent(DragData* drag_data) {
WebMouseEvent result(
WebInputEvent::Type::kMouseMove, drag_data->ClientPosition(),
drag_data->GlobalPosition(), WebPointerProperties::Button::kLeft, 0,
static_cast<WebInputEvent::Modifiers>(drag_data->GetModifiers()),
base::TimeTicks::Now());
result.SetFrameScale(1);
return result;
}
static DataTransfer* CreateDraggingDataTransfer(DataTransferAccessPolicy policy,
DragData* drag_data) {
return DataTransfer::Create(DataTransfer::kDragAndDrop, policy,
drag_data->PlatformData());
}
DragController::DragController(Page* page)
: ExecutionContextLifecycleObserver(
static_cast<ExecutionContext*>(nullptr)),
page_(page),
document_under_mouse_(nullptr),
drag_initiator_(nullptr),
file_input_element_under_mouse_(nullptr),
document_is_handling_drag_(false),
drag_destination_action_(kDragDestinationActionNone),
did_initiate_drag_(false) {}
static DocumentFragment* DocumentFragmentFromDragData(
DragData* drag_data,
LocalFrame* frame,
Range* context,
bool allow_plain_text,
DragSourceType& drag_source_type,
bool is_richly_editable_position) {
DCHECK(drag_data);
drag_source_type = DragSourceType::kHTMLSource;
Document& document = context->OwnerDocument();
if (drag_data->ContainsCompatibleContent()) {
if (DocumentFragment* fragment = drag_data->AsFragment(frame))
return fragment;
if (is_richly_editable_position &&
drag_data->ContainsURL(DragData::kDoNotConvertFilenames)) {
String title;
String url = drag_data->AsURL(DragData::kDoNotConvertFilenames, &title);
if (!url.empty()) {
auto* anchor = MakeGarbageCollected<HTMLAnchorElement>(document);
anchor->SetHref(AtomicString(url));
if (title.empty()) {
// Try the plain text first because the url might be normalized or
// escaped.
if (drag_data->ContainsPlainText())
title = drag_data->AsPlainText();
if (title.empty())
title = url;
}
Node* anchor_text = document.createTextNode(title);
anchor->AppendChild(anchor_text);
DocumentFragment* fragment = document.createDocumentFragment();
fragment->AppendChild(anchor);
return fragment;
}
}
}
if (allow_plain_text && drag_data->ContainsPlainText()) {
drag_source_type = DragSourceType::kPlainTextSource;
return CreateFragmentFromText(EphemeralRange(context),
drag_data->AsPlainText());
}
return nullptr;
}
bool DragController::DragIsMove(FrameSelection& selection,
DragData* drag_data) {
return document_under_mouse_ ==
(drag_initiator_ ? drag_initiator_->document() : nullptr) &&
selection.SelectionHasFocus() &&
selection.ComputeVisibleSelectionInDOMTreeDeprecated()
.IsContentEditable() &&
selection.ComputeVisibleSelectionInDOMTreeDeprecated().IsRange() &&
!IsCopyKeyDown(drag_data);
}
void DragController::ClearDragCaret() {
page_->GetDragCaret().Clear();
}
void DragController::DragEnded() {
drag_initiator_ = nullptr;
did_initiate_drag_ = false;
drag_pointer_id_.reset();
page_->GetDragCaret().Clear();
// When dragging occurs, the mousedown event is triggered, causing the caret's
// blinking state to be suspended. Therefore, it is necessary to reset the
// blinking state after dragging.
if (auto* focused_frame = page_->GetFocusController().FocusedFrame()) {
focused_frame->Selection().SetCaretBlinkingSuspended(false);
}
}
void DragController::DragExited(DragData* drag_data, LocalFrame& local_root) {
DCHECK(drag_data);
LocalFrameView* frame_view(local_root.View());
if (frame_view) {
DataTransferAccessPolicy policy = DataTransferAccessPolicy::kTypesReadable;
DataTransfer* data_transfer = CreateDraggingDataTransfer(policy, drag_data);
data_transfer->SetSourceOperation(drag_data->DraggingSourceOperationMask());
local_root.GetEventHandler().CancelDragAndDrop(CreateMouseEvent(drag_data),
data_transfer);
data_transfer->SetAccessPolicy(
DataTransferAccessPolicy::kNumb); // invalidate clipboard here for
// security
}
MouseMovedIntoDocument(nullptr);
if (file_input_element_under_mouse_)
file_input_element_under_mouse_->SetCanReceiveDroppedFiles(false);
file_input_element_under_mouse_ = nullptr;
}
void DragController::PerformDrag(DragData* drag_data, LocalFrame& local_root) {
DCHECK(drag_data);
document_under_mouse_ = local_root.DocumentAtPoint(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition()));
LocalFrame::NotifyUserActivation(
document_under_mouse_ ? document_under_mouse_->GetFrame() : nullptr,
mojom::blink::UserActivationNotificationType::kInteraction);
if ((drag_destination_action_ & kDragDestinationActionDHTML) &&
document_is_handling_drag_) {
bool prevented_default = false;
if (drag_data->ForceDefaultAction()) {
// Tell the document that the drag has left the building.
DragExited(drag_data, local_root);
} else if (local_root.View()) {
// Sending an event can result in the destruction of the view and part.
DataTransfer* data_transfer = CreateDraggingDataTransfer(
DataTransferAccessPolicy::kReadable, drag_data);
data_transfer->SetSourceOperation(
drag_data->DraggingSourceOperationMask());
EventHandler& event_handler = local_root.GetEventHandler();
prevented_default = event_handler.PerformDragAndDrop(
CreateMouseEvent(drag_data), data_transfer) !=
WebInputEventResult::kNotHandled;
if (!prevented_default && document_under_mouse_) {
// When drop target is plugin element and it can process drag, we
// should prevent default behavior.
const HitTestLocation location(local_root.View()->ConvertFromRootFrame(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition())));
const HitTestResult result =
event_handler.HitTestResultAtLocation(location);
auto* html_plugin_element =
DynamicTo<HTMLPlugInElement>(result.InnerNode());
prevented_default |=
html_plugin_element && html_plugin_element->CanProcessDrag();
}
// Invalidate clipboard here for security.
data_transfer->SetAccessPolicy(DataTransferAccessPolicy::kNumb);
}
if (prevented_default) {
document_under_mouse_ = nullptr;
ClearDragCaret();
return;
}
}
if ((drag_destination_action_ & kDragDestinationActionEdit) &&
ConcludeEditDrag(drag_data)) {
document_under_mouse_ = nullptr;
return;
}
if (OperationForLoad(drag_data, local_root) != DragOperation::kNone) {
Vector<String> urls;
if (base::FeatureList::IsEnabled(
blink::features::kOpenAllUrlsOrFilesOnDrop)) {
urls = drag_data->AsURLs();
} else {
urls.push_back(drag_data->AsURL());
}
bool has_transient_user_activation = LocalFrame::HasTransientUserActivation(
document_under_mouse_ ? document_under_mouse_->GetFrame() : nullptr);
bool should_focus_tab = true;
for (const String& url : urls) {
ResourceRequest resource_request(url);
resource_request.SetHasUserGesture(has_transient_user_activation);
// Use a unique origin to match other navigations that are initiated
// outside of a renderer process (e.g. omnibox navigations). Here, the
// initiator of the navigation is a user dragging files from *outside* of
// the current page. See also https://crbug.com/930049.
//
// TODO(crbug.com/331733543): Once supported, use the source of the drag
// as the initiator of the navigation below.
resource_request.SetRequestorOrigin(SecurityOrigin::CreateUniqueOpaque());
FrameLoadRequest request(nullptr, resource_request);
// Open the dropped URL in a new tab to avoid potential data-loss in the
// current tab. See https://crbug.com/451659.
// First tab should be focused, the rest should be background tabs.
request.SetNavigationPolicy(
should_focus_tab
? NavigationPolicy::kNavigationPolicyNewForegroundTab
: NavigationPolicy::kNavigationPolicyNewBackgroundTab);
local_root.Navigate(request, WebFrameLoadType::kStandard);
should_focus_tab = false;
}
}
document_under_mouse_ = nullptr;
}
void DragController::MouseMovedIntoDocument(Document* new_document) {
if (document_under_mouse_ == new_document)
return;
// If we were over another document clear the selection
if (document_under_mouse_)
ClearDragCaret();
document_under_mouse_ = new_document;
}
DragController::Operation DragController::DragEnteredOrUpdated(
DragData* drag_data,
LocalFrame& local_root) {
DCHECK(drag_data);
MouseMovedIntoDocument(local_root.DocumentAtPoint(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition())));
// TODO(crbug.com/331682039): Replace `AcceptsLoadDrops` with a Setting used
// in core.
drag_destination_action_ =
page_->GetChromeClient().AcceptsLoadDrops()
? kDragDestinationActionAny
: static_cast<DragDestinationAction>(kDragDestinationActionDHTML |
kDragDestinationActionEdit);
Operation drag_operation;
document_is_handling_drag_ =
TryDocumentDrag(drag_data, drag_destination_action_,
drag_operation.operation, local_root);
if (!document_is_handling_drag_ &&
(drag_destination_action_ & kDragDestinationActionLoad)) {
drag_operation.operation = OperationForLoad(drag_data, local_root);
}
drag_operation.document_is_handling_drag = document_is_handling_drag_;
return drag_operation;
}
static HTMLInputElement* AsFileInput(Node* node) {
DCHECK(node);
for (; node; node = node->OwnerShadowHost()) {
auto* html_input_element = DynamicTo<HTMLInputElement>(node);
if (html_input_element &&
html_input_element->FormControlType() == FormControlType::kInputFile) {
return html_input_element;
}
}
return nullptr;
}
// This can return null if an empty document is loaded.
static Element* ElementUnderMouse(Document* document_under_mouse,
const PhysicalOffset& point) {
HitTestRequest request(HitTestRequest::kReadOnly | HitTestRequest::kActive);
HitTestLocation location(point);
HitTestResult result(request, location);
document_under_mouse->GetLayoutView()->HitTest(location, result);
Node* n = result.InnerNode();
while (n && !n->IsElementNode())
n = n->ParentOrShadowHostNode();
if (n && n->IsInShadowTree())
n = n->OwnerShadowHost();
return To<Element>(n);
}
bool DragController::TryDocumentDrag(DragData* drag_data,
DragDestinationAction action_mask,
DragOperation& drag_operation,
LocalFrame& local_root) {
DCHECK(drag_data);
if (!document_under_mouse_)
return false;
// This is the renderer-side check for https://crbug.com/59081 to prevent
// drags between cross-origin frames within the same page. This logic relies
// on the browser process to have already filtered out any drags that might
// span distinct `blink::Page` objects but still be part of the same logical
// page. Otherwise, `drag_initiator_` will be null here and the drag will
// incorrectly be allowed to proceed.
//
// Note: One example where the drag start frame and the drop target frame can
// be part of the same logical page, but belong to different `blink::Page`
// instances is if the two frames are hosted in different renderer processes.
auto* under_mouse_origin =
document_under_mouse_->GetExecutionContext()->GetSecurityOrigin();
if (drag_initiator_ &&
!under_mouse_origin->CanAccess(drag_initiator_->GetSecurityOrigin())) {
return false;
}
bool is_handling_drag = false;
if (action_mask & kDragDestinationActionDHTML) {
is_handling_drag = TryDHTMLDrag(drag_data, drag_operation, local_root);
// Do not continue if m_documentUnderMouse has been reset by tryDHTMLDrag.
// tryDHTMLDrag fires dragenter event. The event listener that listens
// to this event may create a nested run loop (open a modal dialog),
// which could process dragleave event and reset m_documentUnderMouse in
// dragExited.
if (!document_under_mouse_)
return false;
}
// It's unclear why this check is after tryDHTMLDrag.
// We send drag events in tryDHTMLDrag and that may be the reason.
LocalFrameView* frame_view = document_under_mouse_->View();
if (!frame_view)
return false;
if (is_handling_drag) {
page_->GetDragCaret().Clear();
return true;
}
if ((action_mask & kDragDestinationActionEdit) &&
CanProcessDrag(drag_data, local_root)) {
PhysicalOffset point = frame_view->ConvertFromRootFrame(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition()));
Element* element = ElementUnderMouse(document_under_mouse_.Get(), point);
if (!element)
return false;
HTMLInputElement* element_as_file_input = AsFileInput(element);
if (file_input_element_under_mouse_ != element_as_file_input) {
if (file_input_element_under_mouse_)
file_input_element_under_mouse_->SetCanReceiveDroppedFiles(false);
file_input_element_under_mouse_ = element_as_file_input;
}
if (!file_input_element_under_mouse_) {
page_->GetDragCaret().SetCaretPosition(
document_under_mouse_->GetFrame()->PositionForPoint(point));
}
LocalFrame* inner_frame = element->GetDocument().GetFrame();
drag_operation = DragIsMove(inner_frame->Selection(), drag_data)
? DragOperation::kMove
: DragOperation::kCopy;
if (file_input_element_under_mouse_) {
bool can_receive_dropped_files = false;
if (!file_input_element_under_mouse_->IsDisabledFormControl()) {
can_receive_dropped_files = file_input_element_under_mouse_->Multiple()
? drag_data->NumberOfFiles() > 0
: drag_data->NumberOfFiles() == 1;
}
if (!can_receive_dropped_files)
drag_operation = DragOperation::kNone;
file_input_element_under_mouse_->SetCanReceiveDroppedFiles(
can_receive_dropped_files);
}
return true;
}
// We are not over an editable region. Make sure we're clearing any prior drag
// cursor.
page_->GetDragCaret().Clear();
if (file_input_element_under_mouse_)
file_input_element_under_mouse_->SetCanReceiveDroppedFiles(false);
file_input_element_under_mouse_ = nullptr;
return false;
}
DragOperation DragController::OperationForLoad(DragData* drag_data,
LocalFrame& local_root) {
DCHECK(drag_data);
Document* doc = local_root.DocumentAtPoint(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition()));
if (doc &&
(did_initiate_drag_ || IsA<PluginDocument>(doc) || IsEditable(*doc)))
return DragOperation::kNone;
return GetDragOperation(drag_data);
}
// Returns true if node at |point| is editable with populating |dragCaret| and
// |range|, otherwise returns false.
static bool SetSelectionToDragCaret(LocalFrame* frame,
const SelectionInDOMTree& drag_caret,
Range*& range,
const PhysicalOffset& point) {
frame->Selection().SetSelection(drag_caret, SetSelectionOptions());
// TODO(crbug.com/40458806): Audit the usage of `UpdateStyleAndLayout`.
frame->GetDocument()->UpdateStyleAndLayout(DocumentUpdateReason::kEditing);
if (!frame->Selection().ComputeVisibleSelectionInDOMTree().IsNone()) {
return frame->Selection()
.ComputeVisibleSelectionInDOMTree()
.IsContentEditable();
}
const PositionWithAffinity& position = frame->PositionForPoint(point);
if (!position.IsConnected())
return false;
frame->Selection().SetSelection(
SelectionInDOMTree::Builder().Collapse(position).Build(),
SetSelectionOptions());
// TODO(crbug.com/40458806): Audit the usage of `UpdateStyleAndLayout`.
frame->GetDocument()->UpdateStyleAndLayout(DocumentUpdateReason::kEditing);
const VisibleSelection& visible_selection =
frame->Selection().ComputeVisibleSelectionInDOMTree();
range = CreateRange(visible_selection.ToNormalizedEphemeralRange());
return !visible_selection.IsNone() && visible_selection.IsContentEditable();
}
DispatchEventResult DragController::DispatchTextInputEventFor(
LocalFrame* inner_frame,
DragData* drag_data) {
// Layout should be clean due to a hit test performed in |elementUnderMouse|.
DCHECK(!inner_frame->GetDocument()->NeedsLayoutTreeUpdate());
DCHECK(page_->GetDragCaret().HasCaret());
String text = page_->GetDragCaret().IsContentRichlyEditable()
? ""
: drag_data->AsPlainText();
const PositionWithAffinity& caret_position =
page_->GetDragCaret().CaretPosition();
DCHECK(caret_position.IsConnected()) << caret_position;
Element* target = FindEventTargetFrom(
*inner_frame,
CreateVisibleSelection(
SelectionInDOMTree::Builder().Collapse(caret_position).Build()));
if (!target)
return DispatchEventResult::kNotCanceled;
return target->DispatchEvent(
*TextEvent::CreateForDrop(inner_frame->DomWindow(), text));
}
bool DragController::ConcludeEditDrag(DragData* drag_data) {
DCHECK(drag_data);
HTMLInputElement* file_input = file_input_element_under_mouse_;
if (file_input_element_under_mouse_) {
file_input_element_under_mouse_->SetCanReceiveDroppedFiles(false);
file_input_element_under_mouse_ = nullptr;
}
if (!document_under_mouse_)
return false;
PhysicalOffset point = document_under_mouse_->View()->ConvertFromRootFrame(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition()));
Element* element = ElementUnderMouse(document_under_mouse_.Get(), point);
if (!element)
return false;
LocalFrame* inner_frame = element->ownerDocument()->GetFrame();
DCHECK(inner_frame);
if (page_->GetDragCaret().HasCaret() &&
DispatchTextInputEventFor(inner_frame, drag_data) !=
DispatchEventResult::kNotCanceled)
return true;
if (drag_data->ContainsFiles() && file_input) {
// fileInput should be the element we hit tested for, unless it was made
// display:none in a drop event handler.
if (file_input->GetLayoutObject())
DCHECK_EQ(file_input, element);
if (file_input->IsDisabledFormControl())
return false;
return file_input->ReceiveDroppedFiles(drag_data);
}
if (!page_->GetDragController().CanProcessDrag(
drag_data, inner_frame->LocalFrameRoot())) {
page_->GetDragCaret().Clear();
return false;
}
if (page_->GetDragCaret().HasCaret()) {
// TODO(crbug.com/40458806): Audit the usage of` UpdateStyleAndLayout`.
page_->GetDragCaret()
.CaretPosition()
.GetPosition()
.GetDocument()
->UpdateStyleAndLayout(DocumentUpdateReason::kEditing);
}
const PositionWithAffinity& caret_position =
page_->GetDragCaret().CaretPosition();
if (!caret_position.IsConnected()) {
// "editing/pasteboard/drop-text-events-sideeffect-crash.html" and
// "editing/pasteboard/drop-text-events-sideeffect.html" reach here.
page_->GetDragCaret().Clear();
return false;
}
VisibleSelection drag_caret = CreateVisibleSelection(
SelectionInDOMTree::Builder().Collapse(caret_position).Build());
page_->GetDragCaret().Clear();
// |innerFrame| can be removed by event handler called by
// |dispatchTextInputEventFor()|.
if (!inner_frame->Selection().IsAvailable()) {
// "editing/pasteboard/drop-text-events-sideeffect-crash.html" reaches
// here.
return false;
}
Range* range = CreateRange(drag_caret.ToNormalizedEphemeralRange());
Element* root_editable_element =
inner_frame->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()
.RootEditableElement();
// For range to be null a WebKit client must have done something bad while
// manually controlling drag behaviour
if (!range)
return false;
ResourceFetcher* fetcher = range->OwnerDocument().Fetcher();
ResourceCacheValidationSuppressor validation_suppressor(fetcher);
// Start new Drag&Drop command group, invalidate previous command group.
// Assume no other places is firing |DeleteByDrag| and |InsertFromDrop|.
inner_frame->GetEditor().RegisterCommandGroup(
MakeGarbageCollected<DragAndDropCommand>(*inner_frame->GetDocument()));
bool drag_is_move = DragIsMove(inner_frame->Selection(), drag_data);
bool is_richly_editable_position =
IsRichlyEditablePosition(drag_caret.Anchor());
if (drag_is_move || is_richly_editable_position) {
DragSourceType drag_source_type = DragSourceType::kHTMLSource;
DocumentFragment* fragment = DocumentFragmentFromDragData(
drag_data, inner_frame, range, true, drag_source_type,
is_richly_editable_position);
if (!fragment)
return false;
if (drag_is_move) {
// NSTextView behavior is to always smart delete on moving a selection,
// but only to smart insert if the selection granularity is word
// granularity.
const DeleteMode delete_mode =
inner_frame->GetEditor().SmartInsertDeleteEnabled()
? DeleteMode::kSmart
: DeleteMode::kSimple;
const InsertMode insert_mode =
(delete_mode == DeleteMode::kSmart &&
inner_frame->Selection().Granularity() == TextGranularity::kWord &&
drag_data->CanSmartReplace())
? InsertMode::kSmart
: InsertMode::kSimple;
if (!inner_frame->GetEditor().DeleteSelectionAfterDraggingWithEvents(
FindEventTargetFrom(
*inner_frame,
inner_frame->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()),
delete_mode, drag_caret.Anchor())) {
return false;
}
inner_frame->Selection().SetSelection(
SelectionInDOMTree::Builder()
.SetBaseAndExtent(EphemeralRange(range))
.Build(),
SetSelectionOptions());
if (inner_frame->Selection().IsAvailable()) {
DCHECK(document_under_mouse_);
if (!inner_frame->GetEditor().ReplaceSelectionAfterDraggingWithEvents(
element, drag_data, fragment, range, insert_mode,
drag_source_type))
return false;
}
} else {
if (SetSelectionToDragCaret(inner_frame, drag_caret.AsSelection(), range,
point)) {
DCHECK(document_under_mouse_);
if (!inner_frame->GetEditor().ReplaceSelectionAfterDraggingWithEvents(
element, drag_data, fragment, range,
drag_data->CanSmartReplace() ? InsertMode::kSmart
: InsertMode::kSimple,
drag_source_type))
return false;
}
}
} else {
String text = drag_data->AsPlainText();
if (text.empty())
return false;
if (SetSelectionToDragCaret(inner_frame, drag_caret.AsSelection(), range,
point)) {
DCHECK(document_under_mouse_);
if (!inner_frame->GetEditor().ReplaceSelectionAfterDraggingWithEvents(
element, drag_data,
CreateFragmentFromText(EphemeralRange(range), text), range,
InsertMode::kSimple, DragSourceType::kPlainTextSource))
return false;
}
}
if (root_editable_element) {
if (LocalFrame* frame = root_editable_element->GetDocument().GetFrame()) {
frame->GetEventHandler().UpdateDragStateAfterEditDragIfNeeded(
root_editable_element);
}
}
return true;
}
bool DragController::CanProcessDrag(DragData* drag_data,
LocalFrame& local_root) {
DCHECK(drag_data);
if (!drag_data->ContainsCompatibleContent())
return false;
if (!local_root.ContentLayoutObject())
return false;
const PhysicalOffset point_in_local_root =
local_root.View()->ConvertFromRootFrame(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition()));
const HitTestResult result =
local_root.GetEventHandler().HitTestResultAtLocation(
HitTestLocation(point_in_local_root));
if (!result.InnerNode())
return false;
if (drag_data->ContainsFiles() && AsFileInput(result.InnerNode()))
return true;
if (auto* plugin = DynamicTo<HTMLPlugInElement>(result.InnerNode())) {
if (!plugin->CanProcessDrag() && !IsEditable(*result.InnerNode()))
return false;
} else if (!IsEditable(*result.InnerNode())) {
return false;
}
if (did_initiate_drag_ &&
document_under_mouse_ ==
(drag_initiator_ ? drag_initiator_->document() : nullptr)) {
const PhysicalOffset point_in_frame =
result.InnerNode()
->GetDocument()
.GetFrame()
->View()
->ConvertFromRootFrame(
PhysicalOffset::FromPointFRound(drag_data->ClientPosition()));
return !result.IsSelected(HitTestLocation(point_in_frame));
}
return true;
}
static DragOperation DefaultOperationForDrag(DragOperationsMask src_op_mask) {
// This is designed to match IE's operation fallback for the case where
// the page calls preventDefault() in a drag event but doesn't set dropEffect.
if (src_op_mask == kDragOperationEvery)
return DragOperation::kCopy;
if (src_op_mask == kDragOperationNone)
return DragOperation::kNone;
if (src_op_mask & kDragOperationMove)
return DragOperation::kMove;
if (src_op_mask & kDragOperationCopy)
return DragOperation::kCopy;
if (src_op_mask & kDragOperationLink)
return DragOperation::kLink;
return DragOperation::kNone;
}
bool DragController::TryDHTMLDrag(DragData* drag_data,
DragOperation& operation,
LocalFrame& local_root) {
DCHECK(drag_data);
DCHECK(document_under_mouse_);
if (!local_root.View())
return false;
DataTransferAccessPolicy policy = DataTransferAccessPolicy::kTypesReadable;
DataTransfer* data_transfer = CreateDraggingDataTransfer(policy, drag_data);
DragOperationsMask src_op_mask = drag_data->DraggingSourceOperationMask();
data_transfer->SetSourceOperation(src_op_mask);
WebMouseEvent event = CreateMouseEvent(drag_data);
if (local_root.GetEventHandler().UpdateDragAndDrop(event, data_transfer) ==
WebInputEventResult::kNotHandled) {
data_transfer->SetAccessPolicy(
DataTransferAccessPolicy::kNumb); // invalidate clipboard here for
// security
return false;
}
if (!data_transfer->DropEffectIsInitialized()) {
operation = DefaultOperationForDrag(src_op_mask);
} else {
operation = data_transfer->DestinationOperation();
if (!(src_op_mask & static_cast<int>(operation))) {
// The element picked an operation which is not supported by the source.
operation = DragOperation::kNone;
}
}
data_transfer->SetAccessPolicy(
DataTransferAccessPolicy::kNumb); // invalidate clipboard here for
// security
return true;
}
bool SelectTextInsteadOfDrag(const Node& node) {
if (!node.IsTextNode())
return false;
// Editable elements loose their draggability,
// see https://github.com/whatwg/html/issues/3114.
if (IsEditable(node))
return true;
for (Node& ancestor_node : NodeTraversal::InclusiveAncestorsOf(node)) {
auto* html_element = DynamicTo<HTMLElement>(ancestor_node);
if (html_element && html_element->draggable())
return false;
}
return node.CanStartSelection();
}
Node* DragController::DraggableNode(const LocalFrame* src,
Node* start_node,
const gfx::Point& drag_origin,
SelectionDragPolicy selection_drag_policy,
DragSourceAction& drag_type) const {
if (src->Selection().Contains(PhysicalOffset(drag_origin))) {
drag_type = kDragSourceActionSelection;
if (selection_drag_policy == kImmediateSelectionDragResolution)
return start_node;
} else {
drag_type = kDragSourceActionNone;
}
Node* node = nullptr;
DragSourceAction candidate_drag_type = kDragSourceActionNone;
for (const LayoutObject* layout_object = start_node->GetLayoutObject();
layout_object; layout_object = layout_object->Parent()) {
node = layout_object->NonPseudoNode();
if (!node) {
// Anonymous layout blocks don't correspond to actual DOM nodes, so we
// skip over them for the purposes of finding a draggable node.
continue;
}
if (drag_type != kDragSourceActionSelection &&
SelectTextInsteadOfDrag(*node)) {
// We have a click in an unselected, selectable text that is not
// draggable... so we want to start the selection process instead
// of looking for a parent to try to drag.
return nullptr;
}
if (node->IsElementNode()) {
EUserDrag drag_mode = layout_object->Style()->UserDrag();
if (drag_mode == EUserDrag::kNone)
continue;
// Even if the image is part of a selection, we always only drag the image
// in this case.
if (layout_object->IsImage() && src->GetSettings() &&
src->GetSettings()->GetLoadsImagesAutomatically()) {
drag_type = kDragSourceActionImage;
return node;
}
// Other draggable elements are considered unselectable.
if (drag_mode == EUserDrag::kElement) {
candidate_drag_type = kDragSourceActionDHTML;
break;
}
// TODO(crbug.com/369219144): Should this be
// DynamicTo<HTMLAnchorElementBase>?
auto* html_anchor_element = DynamicTo<HTMLAnchorElement>(node);
if (html_anchor_element && html_anchor_element->IsLiveLink()) {
candidate_drag_type = kDragSourceActionLink;
break;
}
}
}
if (candidate_drag_type == kDragSourceActionNone) {
// Either:
// 1) Nothing under the cursor is considered draggable, so we bail out.
// 2) There was a selection under the cursor but selectionDragPolicy is set
// to DelayedSelectionDragResolution and no other draggable element could
// be found, so bail out and allow text selection to start at the cursor
// instead.
return nullptr;
}
DCHECK(node);
if (drag_type == kDragSourceActionSelection) {
// Dragging unselectable elements in a selection has special behavior if
// selectionDragPolicy is DelayedSelectionDragResolution and this drag was
// flagged as a potential selection drag. In that case, don't allow
// selection and just drag the entire selection instead.
DCHECK_EQ(selection_drag_policy, kDelayedSelectionDragResolution);
node = start_node;
} else {
// If the cursor isn't over a selection, then just drag the node we found
// earlier.
DCHECK_EQ(drag_type, kDragSourceActionNone);
drag_type = candidate_drag_type;
}
return node;
}
static void PrepareDataTransferForImageDrag(LocalFrame* source,
DataTransfer* data_transfer,
Element* node,
const KURL& link_url,
const KURL& image_url,
const String& label) {
node->GetDocument().UpdateStyleAndLayoutTree();
if (IsRichlyEditable(*node)) {
// TODO(crbug.com/331666850): Replace `EphemeralRange` usage with `Range`.
Range* range = source->GetDocument()->createRange();
range->selectNode(node, ASSERT_NO_EXCEPTION);
source->Selection().SetSelection(
SelectionInDOMTree::Builder()
.SetBaseAndExtent(EphemeralRange(range))
.Build(),
SetSelectionOptions());
}
data_transfer->DeclareAndWriteDragImage(node, link_url, image_url, label);
}
bool DragController::PopulateDragDataTransfer(LocalFrame* src,
const DragState& state,
const gfx::Point& drag_origin) {
#if DCHECK_IS_ON()
DCHECK(DragTypeIsValid(state.drag_type_));
#endif
DCHECK(src);
if (!src->View() || !src->ContentLayoutObject())
return false;
HitTestLocation location(drag_origin);
HitTestResult hit_test_result =
src->GetEventHandler().HitTestResultAtLocation(location);
// FIXME: Can this even happen? I guess it's possible, but should verify
// with a web test.
Node* hit_inner_node = hit_test_result.InnerNode();
if (!hit_inner_node ||
!state.drag_src_->IsShadowIncludingInclusiveAncestorOf(*hit_inner_node)) {
// The original node being dragged isn't under the drag origin anymore...
// maybe it was hidden or moved out from under the cursor. Regardless, we
// don't want to start a drag on something that's not actually under the
// drag origin.
return false;
}
const KURL& link_url = hit_test_result.AbsoluteLinkURL();
const KURL& image_url = hit_test_result.AbsoluteImageURL();
DataTransfer* data_transfer = state.drag_data_transfer_.Get();
Node* node = state.drag_src_.Get();
// TODO(crbug.com/369219144): Should this be DynamicTo<HTMLAnchorElementBase>?
auto* html_anchor_element = DynamicTo<HTMLAnchorElement>(node);
if (html_anchor_element && html_anchor_element->IsLiveLink() &&
!link_url.IsEmpty()) {
// Simplify whitespace so the title put on the clipboard resembles what
// the user sees on the web page. This includes replacing newlines with
// spaces.
data_transfer->WriteURL(node, link_url,
hit_test_result.TextContent().SimplifyWhiteSpace());
}
if (state.drag_type_ == kDragSourceActionSelection) {
data_transfer->WriteSelection(src->Selection());
} else if (state.drag_type_ == kDragSourceActionImage) {
auto* element = DynamicTo<Element>(node);
if (image_url.IsEmpty() || !element)
return false;
PrepareDataTransferForImageDrag(src, data_transfer, element, link_url,
image_url,
hit_test_result.AltDisplayString());
} else if (state.drag_type_ == kDragSourceActionLink) {
if (link_url.IsEmpty())
return false;
} else if (state.drag_type_ == kDragSourceActionDHTML) {
LayoutObject* layout_object = node->GetLayoutObject();
if (!layout_object) {
// The layoutObject has disappeared, this can happen if the onStartDrag
// handler has hidden the element in some way. In this case we just kill
// the drag.
return false;
}
gfx::Rect bounding_including_descendants =
layout_object->AbsoluteBoundingBoxRectIncludingDescendants();
gfx::Point drag_element_location =
drag_origin - bounding_including_descendants.OffsetFromOrigin();
data_transfer->SetDragImageElement(node, drag_element_location);
// FIXME: For DHTML/draggable element drags, write element markup to
// clipboard.
}
// Observe context related to source to allow dropping drag_state_ when the
// Document goes away.
SetExecutionContext(src->DomWindow());
return true;
}
namespace {
gfx::Point DragLocationForDHTMLDrag(const gfx::Point& mouse_dragged_point,
const gfx::Point& drag_initiation_location,
const gfx::Point& drag_image_offset,
bool is_link_image) {
if (is_link_image) {
return gfx::Point(mouse_dragged_point.x() - drag_image_offset.x(),
mouse_dragged_point.y() - drag_image_offset.y());
}
return gfx::Point(drag_initiation_location.x() - drag_image_offset.x(),
drag_initiation_location.y() - drag_image_offset.y());
}
gfx::Rect DragRectForSelectionDrag(const LocalFrame& frame) {
frame.View()->UpdateLifecycleToLayoutClean(DocumentUpdateReason::kSelection);
gfx::Rect dragging_rect =
gfx::ToEnclosingRect(DragController::ClippedSelection(frame));
int x1 = dragging_rect.x();
int y1 = dragging_rect.y();
int x2 = dragging_rect.right();
int y2 = dragging_rect.bottom();
gfx::Point origin(std::min(x1, x2), std::min(y1, y2));
gfx::Size size(std::abs(x2 - x1), std::abs(y2 - y1));
return gfx::Rect(origin, size);
}
const gfx::Size MaxDragImageSize(float device_scale_factor) {
#if BUILDFLAG(IS_MAC)
// Match Safari's drag image size.
static const gfx::Size kMaxDragImageSize(400, 400);
#else
static const gfx::Size kMaxDragImageSize(200, 200);
#endif
return gfx::ScaleToFlooredSize(kMaxDragImageSize, device_scale_factor);
}
bool CanDragImage(const Element& element) {
auto* layout_image = DynamicTo<LayoutImage>(element.GetLayoutObject());
if (!layout_image)
return false;
const ImageResourceContent* image_content = layout_image->CachedImage();
if (!image_content || image_content->ErrorOccurred() ||
!image_content->HasImage()) {
return false;
}
scoped_refptr<const SharedBuffer> buffer = image_content->ResourceBuffer();
if (!buffer || !buffer->size())
return false;
// We shouldn't be starting a drag for an image that can't provide an
// extension.
// This is an early detection for problems encountered later upon drop.
DCHECK(!image_content->GetImage()->FilenameExtension().empty());
return true;
}
std::unique_ptr<DragImage> DragImageForImage(
const Element& element,
float device_scale_factor,
const gfx::Size& image_element_size_in_pixels) {
auto* layout_image = To<LayoutImage>(element.GetLayoutObject());
const LayoutImageResource& image_resource = *layout_image->ImageResource();
scoped_refptr<Image> image =
image_resource.GetImage(image_element_size_in_pixels);
RespectImageOrientationEnum respect_orientation =
image_resource.ImageOrientation();
gfx::Size image_size = image->Size(respect_orientation);
if (image_size.Area64() > kMaxOriginalImageArea)
return nullptr;
InterpolationQuality interpolation_quality = GetDefaultInterpolationQuality();
if (layout_image->StyleRef().ImageRendering() == EImageRendering::kPixelated)
interpolation_quality = kInterpolationNone;
gfx::Vector2dF image_scale =
DragImage::ClampedImageScale(image_size, image_element_size_in_pixels,
MaxDragImageSize(device_scale_factor));
return DragImage::Create(image.get(), respect_orientation,
interpolation_quality, kDragImageAlpha, image_scale);
}
gfx::Rect DragRectForImage(const DragImage* drag_image,
const gfx::Point& drag_initiation_location,
const gfx::Point& image_element_location,
const gfx::Size& image_element_size_in_pixels) {
if (!drag_image)
return gfx::Rect(drag_initiation_location, gfx::Size());
gfx::Size original_size = image_element_size_in_pixels;
gfx::Size new_size = drag_image->Size();
// Properly orient the drag image and orient it differently if it's smaller
// than the original
float scale = new_size.width() / static_cast<float>(original_size.width());
gfx::Vector2dF offset = image_element_location - drag_initiation_location;
gfx::Point origin = drag_initiation_location +
gfx::ToRoundedVector2d(gfx::ScaleVector2d(offset, scale));
return gfx::Rect(origin, new_size);
}
std::unique_ptr<DragImage> DragImageForLink(const KURL& link_url,
const String& link_text,
float device_scale_factor) {
return DragImage::Create(link_url, link_text, device_scale_factor);
}
gfx::Rect DragRectForLink(const DragImage* link_image,
const gfx::Point& origin,
float device_scale_factor,
float page_scale_factor) {
if (!link_image)
return gfx::Rect(origin, gfx::Size());
gfx::Size image_size = link_image->Size();
// Offset the image so that the cursor is horizontally centered.
gfx::PointF image_offset(-image_size.width() / 2.f, -kLinkDragBorderInset);
// |origin| is in the coordinate space of the frame's contents whereas the
// size of |link_image| is in physical pixels. Adjust the image offset to be
// scaled in the frame's contents.
// TODO(crbug.com/331670940): Unify this calculation with the
// `DragImageForImage` scaling code.
float scale = 1.f / (device_scale_factor * page_scale_factor);
image_offset.Scale(scale);
image_offset += origin.OffsetFromOrigin();
return gfx::Rect(gfx::ToRoundedPoint(image_offset), image_size);
}
} // namespace
// static
gfx::RectF DragController::ClippedSelection(const LocalFrame& frame) {
DCHECK(frame.View());
return DataTransfer::ClipByVisualViewport(
gfx::RectF(frame.Selection().AbsoluteUnclippedBounds()), frame);
}
// static
std::unique_ptr<DragImage> DragController::DragImageForSelection(
LocalFrame& frame,
float opacity) {
if (!frame.Selection().ComputeVisibleSelectionInDOMTreeDeprecated().IsRange())
return nullptr;
frame.View()->UpdateAllLifecyclePhasesExceptPaint(
DocumentUpdateReason::kDragImage);
DCHECK(frame.GetDocument()->IsActive());
gfx::RectF painting_rect = DragController::ClippedSelection(frame);
PaintFlags paint_flags =
PaintFlag::kSelectionDragImageOnly | PaintFlag::kOmitCompositingInfo;
PaintRecordBuilder builder;
frame.View()->PaintOutsideOfLifecycle(
builder.Context(), paint_flags,
CullRect(gfx::ToEnclosingRect(painting_rect)));
auto property_tree_state = frame.View()
->GetLayoutView()
->FirstFragment()
.LocalBorderBoxProperties()
.Unalias();
return DataTransfer::CreateDragImageForFrame(
frame, opacity, painting_rect.size(), painting_rect.OffsetFromOrigin(),
builder, property_tree_state);
}
namespace {
void SelectEnclosingAnchorIfContentEditable(LocalFrame* frame) {
if (frame->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()
.IsCaret() &&
frame->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()
.IsContentEditable()) {
// A user can initiate a drag on a link without having any text
// selected. In this case, we should expand the selection to
// the enclosing anchor element.
if (Node* anchor = EnclosingAnchorElement(
frame->Selection()
.ComputeVisibleSelectionInDOMTreeDeprecated()
.Anchor())) {
frame->Selection().SetSelection(
SelectionInDOMTree::Builder().SelectAllChildren(*anchor).Build(),
SetSelectionOptions());
}
}
}
std::unique_ptr<DragImage> DetermineDragImageAndRect(
gfx::Rect& drag_obj_rect,
gfx::Point& effective_drag_initiation_location,
LocalFrame* frame,
const DragState& state,
const HitTestResult& hit_test_result,
const gfx::Point& drag_initiation_location,
const gfx::Point& mouse_dragged_point) {
DataTransfer* data_transfer = state.drag_data_transfer_.Get();
const KURL& link_url = hit_test_result.AbsoluteLinkURL();
float device_scale_factor =
frame->GetChromeClient().GetScreenInfo(*frame).device_scale_factor;
gfx::Point drag_offset;
// HTML DnD spec allows setting the drag image, even if it is a link, image or
// text we are dragging.
std::unique_ptr<DragImage> drag_image =
data_transfer->CreateDragImage(drag_offset, device_scale_factor, frame);
if (drag_image) {
drag_obj_rect.set_origin(
DragLocationForDHTMLDrag(mouse_dragged_point, drag_initiation_location,
drag_offset, !link_url.IsEmpty()));
drag_obj_rect.set_size(drag_image.get()->Size());
} else {
drag_obj_rect = gfx::Rect();
}
effective_drag_initiation_location = drag_initiation_location;
// If |drag_image| is not provided, try to determine a drag-source-specific
// image and location.
if (state.drag_type_ == kDragSourceActionSelection) {
if (!drag_image) {
drag_image =
DragController::DragImageForSelection(*frame, kDragImageAlpha);
drag_obj_rect = DragRectForSelectionDrag(*frame);
}
} else if (state.drag_type_ == kDragSourceActionImage) {
if (!drag_image) {
auto* element = DynamicTo<Element>(state.drag_src_.Get());
const gfx::Rect& image_rect = hit_test_result.ImageRect();
// TODO(crbug.com/331670941): Remove this scaling and simply pass
// `imageRect`to `dragImageForImage` once all platforms are migrated
// to use zoom for dsf.
gfx::Size image_size_in_pixels = gfx::ScaleToFlooredSize(
image_rect.size(), frame->GetPage()->GetVisualViewport().Scale());
// Pass the selected image size in DIP becasue dragImageForImage clips the
// image in DIP. The coordinates of the locations are in Viewport
// coordinates, and they're converted in the Blink client.
// TODO(crbug.com/331753419): Consider clipping screen coordinates to
// use a high resolution image on high DPI screens.
drag_image = DragImageForImage(*element, device_scale_factor,
image_size_in_pixels);
drag_obj_rect =
DragRectForImage(drag_image.get(), effective_drag_initiation_location,
image_rect.origin(), image_size_in_pixels);
}
} else if (state.drag_type_ == kDragSourceActionLink) {
if (!drag_image) {
DCHECK(frame->GetPage());
drag_image = DragImageForLink(link_url, hit_test_result.TextContent(),
device_scale_factor);
drag_obj_rect = DragRectForLink(drag_image.get(), mouse_dragged_point,
device_scale_factor,
frame->GetPage()->PageScaleFactor());
}
// Why is the initiation location different only for link-drags?
effective_drag_initiation_location = mouse_dragged_point;
}
return drag_image;
}
} // namespace
bool DragController::StartDrag(LocalFrame* frame,
const DragState& state,
const WebMouseEvent& drag_event,
const gfx::Point& drag_initiation_location) {
DCHECK(frame);
if (!frame->View() || !frame->ContentLayoutObject())
return false;
HitTestLocation location(drag_initiation_location);
HitTestResult hit_test_result =
frame->GetEventHandler().HitTestResultAtLocation(location);
Node* hit_inner_node = hit_test_result.InnerNode();
if (!hit_inner_node ||
!state.drag_src_->IsShadowIncludingInclusiveAncestorOf(*hit_inner_node)) {
// The original node being dragged isn't under the drag origin anymore...
// maybe it was hidden or moved out from under the cursor. Regardless, we
// don't want to start a drag on something that's not actually under the
// drag origin.
return false;
}
// Note that drag_origin is different from event position.
gfx::Point mouse_dragged_point = frame->View()->ConvertFromRootFrame(
gfx::ToFlooredPoint(drag_event.PositionInRootFrame()));
// Check early return conditions.
if (state.drag_type_ == kDragSourceActionImage) {
const KURL& image_url = hit_test_result.AbsoluteImageURL();
auto* element = DynamicTo<Element>(state.drag_src_.Get());
if (image_url.IsEmpty() || !element || !CanDragImage(*element))
return false;
} else if (state.drag_type_ == kDragSourceActionLink) {
const KURL& link_url = hit_test_result.AbsoluteLinkURL();
if (link_url.IsEmpty())
return false;
} else if (state.drag_type_ != kDragSourceActionSelection &&
state.drag_type_ != kDragSourceActionDHTML) {
NOTREACHED();
}
if (state.drag_type_ == kDragSourceActionLink)
SelectEnclosingAnchorIfContentEditable(frame);
gfx::Rect drag_obj_rect;
gfx::Point effective_drag_initiation_location;
std::unique_ptr<DragImage> drag_image = DetermineDragImageAndRect(
drag_obj_rect, effective_drag_initiation_location, frame, state,
hit_test_result, drag_initiation_location, mouse_dragged_point);
drag_pointer_id_ = drag_event.id;
DoSystemDrag(drag_image.get(), drag_obj_rect,
effective_drag_initiation_location,
state.drag_data_transfer_.Get(), frame);
return true;
}
void DragController::DoSystemDrag(DragImage* image,
const gfx::Rect& drag_obj_rect,
const gfx::Point& drag_initiation_location,
DataTransfer* data_transfer,
LocalFrame* frame) {
did_initiate_drag_ = true;
drag_initiator_ = frame->DomWindow();
SetExecutionContext(frame->DomWindow());
// TODO(crbug.com/331753420): `drag_obj_rect` and `drag_initiation_location`
// should be passed in as `gfx::RectF` and `gfx::PointF` respectively to
// avoid unnecessary rounding.
gfx::Point adjusted_drag_obj_location =
frame->View()->FrameToViewport(drag_obj_rect.origin());
gfx::Point adjusted_event_pos =
frame->View()->FrameToViewport(drag_initiation_location);
gfx::Vector2d cursor_offset = adjusted_event_pos - adjusted_drag_obj_location;
WebDragData drag_data = data_transfer->GetDataObject()->ToWebDragData();
drag_data.SetReferrerPolicy(drag_initiator_->GetReferrerPolicy());
DragOperationsMask drag_operation_mask = data_transfer->SourceOperation();
SkBitmap drag_image = image ? image->Bitmap() : SkBitmap();
page_->GetChromeClient().StartDragging(frame, drag_data, drag_operation_mask,
std::move(drag_image), cursor_offset,
drag_obj_rect);
}
DragOperation DragController::GetDragOperation(DragData* drag_data) {
// FIXME: To match the MacOS behaviour we should return DragOperation::kNone
// if we are a modal window, we are the drag source, or the window is an
// attached sheet If this can be determined from within WebCore
// operationForDrag can be pulled into WebCore itself
DCHECK(drag_data);
return drag_data->ContainsURL() && !did_initiate_drag_ ? DragOperation::kCopy
: DragOperation::kNone;
}
bool DragController::IsCopyKeyDown(DragData* drag_data) {
int modifiers = drag_data->GetModifiers();
#if BUILDFLAG(IS_MAC)
return modifiers & WebInputEvent::kAltKey;
#else
return modifiers & WebInputEvent::kControlKey;
#endif
}
DragState& DragController::GetDragState() {
if (!drag_state_)
drag_state_ = MakeGarbageCollected<DragState>();
return *drag_state_;
}
void DragController::ContextDestroyed() {
drag_state_ = nullptr;
}
void DragController::Trace(Visitor* visitor) const {
visitor->Trace(page_);
visitor->Trace(document_under_mouse_);
visitor->Trace(drag_initiator_);
visitor->Trace(drag_state_);
visitor->Trace(file_input_element_under_mouse_);
ExecutionContextLifecycleObserver::Trace(visitor);
}
} // namespace blink
|