1/*
2 * Copyright (C) 2007-2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "DragController.h"
28
29#include "HTMLAnchorElement.h"
30#include "SVGAElement.h"
31
32#if ENABLE(DRAG_SUPPORT)
33#include "CachedImage.h"
34#include "CachedResourceLoader.h"
35#include "DataTransfer.h"
36#include "Document.h"
37#include "DocumentFragment.h"
38#include "DragActions.h"
39#include "DragClient.h"
40#include "DragData.h"
41#include "DragImage.h"
42#include "DragState.h"
43#include "Editing.h"
44#include "Editor.h"
45#include "EditorClient.h"
46#include "ElementAncestorIterator.h"
47#include "EventHandler.h"
48#include "File.h"
49#include "FloatRect.h"
50#include "FocusController.h"
51#include "Frame.h"
52#include "FrameLoadRequest.h"
53#include "FrameLoader.h"
54#include "FrameSelection.h"
55#include "FrameView.h"
56#include "HTMLAttachmentElement.h"
57#include "HTMLImageElement.h"
58#include "HTMLInputElement.h"
59#include "HTMLParserIdioms.h"
60#include "HTMLPlugInElement.h"
61#include "HitTestRequest.h"
62#include "HitTestResult.h"
63#include "Image.h"
64#include "ImageOrientation.h"
65#include "MoveSelectionCommand.h"
66#include "Page.h"
67#include "Pasteboard.h"
68#include "PlatformKeyboardEvent.h"
69#include "PluginDocument.h"
70#include "PluginViewBase.h"
71#include "Position.h"
72#include "PromisedAttachmentInfo.h"
73#include "RenderAttachment.h"
74#include "RenderFileUploadControl.h"
75#include "RenderImage.h"
76#include "RenderView.h"
77#include "ReplaceSelectionCommand.h"
78#include "ResourceRequest.h"
79#include "SecurityOrigin.h"
80#include "Settings.h"
81#include "ShadowRoot.h"
82#include "StyleProperties.h"
83#include "Text.h"
84#include "TextEvent.h"
85#include "VisiblePosition.h"
86#include "markup.h"
87
88#if ENABLE(DATA_INTERACTION)
89#include "SelectionRect.h"
90#endif
91
92#include <wtf/RefPtr.h>
93#include <wtf/SetForScope.h>
94#endif
95
96#if ENABLE(DATA_DETECTION)
97#include "DataDetection.h"
98#endif
99
100namespace WebCore {
101
102bool isDraggableLink(const Element& element)
103{
104 if (is<HTMLAnchorElement>(element)) {
105 auto& anchorElement = downcast<HTMLAnchorElement>(element);
106 if (!anchorElement.isLiveLink())
107 return false;
108#if ENABLE(DATA_DETECTION)
109 return !DataDetection::isDataDetectorURL(anchorElement.href());
110#else
111 return true;
112#endif
113 }
114 if (is<SVGAElement>(element))
115 return element.isLink();
116 return false;
117}
118
119#if ENABLE(DRAG_SUPPORT)
120
121static PlatformMouseEvent createMouseEvent(const DragData& dragData)
122{
123 bool shiftKey = false;
124 bool ctrlKey = false;
125 bool altKey = false;
126 bool metaKey = false;
127
128 PlatformKeyboardEvent::getCurrentModifierState(shiftKey, ctrlKey, altKey, metaKey);
129
130 return PlatformMouseEvent(dragData.clientPosition(), dragData.globalPosition(),
131 LeftButton, PlatformEvent::MouseMoved, 0, shiftKey, ctrlKey, altKey,
132 metaKey, WallTime::now(), ForceAtClick, NoTap);
133}
134
135DragController::DragController(Page& page, DragClient& client)
136 : m_page(page)
137 , m_client(client)
138 , m_numberOfItemsToBeAccepted(0)
139 , m_dragHandlingMethod(DragHandlingMethod::None)
140 , m_dragDestinationAction(DragDestinationActionNone)
141 , m_dragSourceAction(DragSourceActionNone)
142 , m_didInitiateDrag(false)
143 , m_sourceDragOperation(DragOperationNone)
144{
145}
146
147DragController::~DragController()
148{
149 m_client.dragControllerDestroyed();
150}
151
152static RefPtr<DocumentFragment> documentFragmentFromDragData(const DragData& dragData, Frame& frame, Range& context, bool allowPlainText, bool& chosePlainText)
153{
154 chosePlainText = false;
155
156 Document& document = context.ownerDocument();
157 if (dragData.containsCompatibleContent()) {
158 if (auto fragment = frame.editor().webContentFromPasteboard(*Pasteboard::createForDragAndDrop(dragData), context, allowPlainText, chosePlainText))
159 return fragment;
160
161 if (dragData.containsURL(DragData::DoNotConvertFilenames)) {
162 String title;
163 String url = dragData.asURL(DragData::DoNotConvertFilenames, &title);
164 if (!url.isEmpty()) {
165 auto anchor = HTMLAnchorElement::create(document);
166 anchor->setHref(url);
167 if (title.isEmpty()) {
168 // Try the plain text first because the url might be normalized or escaped.
169 if (dragData.containsPlainText())
170 title = dragData.asPlainText();
171 if (title.isEmpty())
172 title = url;
173 }
174 anchor->appendChild(document.createTextNode(title));
175 auto fragment = document.createDocumentFragment();
176 fragment->appendChild(anchor);
177 return fragment;
178 }
179 }
180 }
181 if (allowPlainText && dragData.containsPlainText()) {
182 chosePlainText = true;
183 return createFragmentFromText(context, dragData.asPlainText()).ptr();
184 }
185
186 return nullptr;
187}
188
189#if !PLATFORM(IOS_FAMILY)
190
191DragOperation DragController::platformGenericDragOperation()
192{
193 return DragOperationMove;
194}
195
196#endif
197
198bool DragController::dragIsMove(FrameSelection& selection, const DragData& dragData)
199{
200 const VisibleSelection& visibleSelection = selection.selection();
201 return m_documentUnderMouse == m_dragInitiator && visibleSelection.isContentEditable() && visibleSelection.isRange() && !isCopyKeyDown(dragData);
202}
203
204void DragController::clearDragCaret()
205{
206 m_page.dragCaretController().clear();
207}
208
209void DragController::dragEnded()
210{
211 m_dragInitiator = nullptr;
212 m_didInitiateDrag = false;
213 m_documentUnderMouse = nullptr;
214 clearDragCaret();
215
216 m_client.dragEnded();
217}
218
219DragOperation DragController::dragEntered(const DragData& dragData)
220{
221 return dragEnteredOrUpdated(dragData);
222}
223
224void DragController::dragExited(const DragData& dragData)
225{
226 auto& mainFrame = m_page.mainFrame();
227 if (mainFrame.view())
228 mainFrame.eventHandler().cancelDragAndDrop(createMouseEvent(dragData), Pasteboard::createForDragAndDrop(dragData), dragData.draggingSourceOperationMask(), dragData.containsFiles());
229 mouseMovedIntoDocument(nullptr);
230 if (m_fileInputElementUnderMouse)
231 m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
232 m_fileInputElementUnderMouse = nullptr;
233}
234
235DragOperation DragController::dragUpdated(const DragData& dragData)
236{
237 return dragEnteredOrUpdated(dragData);
238}
239
240inline static bool dragIsHandledByDocument(DragHandlingMethod dragHandlingMethod)
241{
242 return dragHandlingMethod != DragHandlingMethod::None && dragHandlingMethod != DragHandlingMethod::PageLoad;
243}
244
245bool DragController::performDragOperation(const DragData& dragData)
246{
247 SetForScope<bool> isPerformingDrop(m_isPerformingDrop, true);
248 TemporarySelectionChange ignoreSelectionChanges(m_page.focusController().focusedOrMainFrame(), WTF::nullopt, TemporarySelectionOption::IgnoreSelectionChanges);
249
250 m_documentUnderMouse = m_page.mainFrame().documentAtPoint(dragData.clientPosition());
251
252 ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy = ShouldOpenExternalURLsPolicy::ShouldNotAllow;
253 if (m_documentUnderMouse)
254 shouldOpenExternalURLsPolicy = m_documentUnderMouse->shouldOpenExternalURLsPolicyToPropagate();
255
256 if ((m_dragDestinationAction & DragDestinationActionDHTML) && dragIsHandledByDocument(m_dragHandlingMethod)) {
257 m_client.willPerformDragDestinationAction(DragDestinationActionDHTML, dragData);
258 Ref<Frame> mainFrame(m_page.mainFrame());
259 bool preventedDefault = false;
260 if (mainFrame->view())
261 preventedDefault = mainFrame->eventHandler().performDragAndDrop(createMouseEvent(dragData), Pasteboard::createForDragAndDrop(dragData), dragData.draggingSourceOperationMask(), dragData.containsFiles());
262 if (preventedDefault) {
263 clearDragCaret();
264 m_documentUnderMouse = nullptr;
265 return true;
266 }
267 }
268
269 if ((m_dragDestinationAction & DragDestinationActionEdit) && concludeEditDrag(dragData)) {
270 m_client.didConcludeEditDrag();
271 m_documentUnderMouse = nullptr;
272 clearDragCaret();
273 return true;
274 }
275
276 m_documentUnderMouse = nullptr;
277 clearDragCaret();
278
279 if (operationForLoad(dragData) == DragOperationNone)
280 return false;
281
282 auto urlString = dragData.asURL();
283 if (urlString.isEmpty())
284 return false;
285
286 m_client.willPerformDragDestinationAction(DragDestinationActionLoad, dragData);
287 FrameLoadRequest frameLoadRequest { m_page.mainFrame(), { urlString }, shouldOpenExternalURLsPolicy };
288 frameLoadRequest.setIsRequestFromClientOrUserInput();
289 m_page.mainFrame().loader().load(WTFMove(frameLoadRequest));
290 return true;
291}
292
293void DragController::mouseMovedIntoDocument(Document* newDocument)
294{
295 if (m_documentUnderMouse == newDocument)
296 return;
297
298 // If we were over another document clear the selection
299 if (m_documentUnderMouse)
300 clearDragCaret();
301 m_documentUnderMouse = newDocument;
302}
303
304DragOperation DragController::dragEnteredOrUpdated(const DragData& dragData)
305{
306 mouseMovedIntoDocument(m_page.mainFrame().documentAtPoint(dragData.clientPosition()));
307
308 m_dragDestinationAction = dragData.dragDestinationAction();
309 if (m_dragDestinationAction == DragDestinationActionNone) {
310 clearDragCaret(); // FIXME: Why not call mouseMovedIntoDocument(nullptr)?
311 return DragOperationNone;
312 }
313
314 DragOperation dragOperation = DragOperationNone;
315 m_dragHandlingMethod = tryDocumentDrag(dragData, m_dragDestinationAction, dragOperation);
316 if (m_dragHandlingMethod == DragHandlingMethod::None && (m_dragDestinationAction & DragDestinationActionLoad)) {
317 dragOperation = operationForLoad(dragData);
318 if (dragOperation != DragOperationNone)
319 m_dragHandlingMethod = DragHandlingMethod::PageLoad;
320 } else if (m_dragHandlingMethod == DragHandlingMethod::SetColor)
321 dragOperation = DragOperationCopy;
322
323 updateSupportedTypeIdentifiersForDragHandlingMethod(m_dragHandlingMethod, dragData);
324 return dragOperation;
325}
326
327static HTMLInputElement* asFileInput(Node& node)
328{
329 if (!is<HTMLInputElement>(node))
330 return nullptr;
331
332 auto* inputElement = &downcast<HTMLInputElement>(node);
333
334 // If this is a button inside of the a file input, move up to the file input.
335 if (inputElement->isTextButton() && is<ShadowRoot>(inputElement->treeScope().rootNode())) {
336 auto& host = *downcast<ShadowRoot>(inputElement->treeScope().rootNode()).host();
337 inputElement = is<HTMLInputElement>(host) ? &downcast<HTMLInputElement>(host) : nullptr;
338 }
339
340 return inputElement && inputElement->isFileUpload() ? inputElement : nullptr;
341}
342
343#if ENABLE(INPUT_TYPE_COLOR)
344
345static bool isEnabledColorInput(Node& node)
346{
347 if (!is<HTMLInputElement>(node))
348 return false;
349 auto& input = downcast<HTMLInputElement>(node);
350 return input.isColorControl() && !input.isDisabledFormControl();
351}
352
353static bool isInShadowTreeOfEnabledColorInput(Node& node)
354{
355 auto* host = node.shadowHost();
356 return host && isEnabledColorInput(*host);
357}
358
359#endif
360
361// This can return null if an empty document is loaded.
362static Element* elementUnderMouse(Document* documentUnderMouse, const IntPoint& p)
363{
364 Frame* frame = documentUnderMouse->frame();
365 float zoomFactor = frame ? frame->pageZoomFactor() : 1;
366 LayoutPoint point(p.x() * zoomFactor, p.y() * zoomFactor);
367
368 HitTestResult result(point);
369 documentUnderMouse->hitTest(HitTestRequest(), result);
370
371 auto* node = result.innerNode();
372 if (!node)
373 return nullptr;
374 // FIXME: Use parentElementInComposedTree here.
375 auto* element = is<Element>(*node) ? &downcast<Element>(*node) : node->parentElement();
376 auto* host = element->shadowHost();
377 return host ? host : element;
378}
379
380#if !ENABLE(DATA_INTERACTION)
381
382void DragController::updateSupportedTypeIdentifiersForDragHandlingMethod(DragHandlingMethod, const DragData&) const
383{
384}
385
386#endif
387
388DragHandlingMethod DragController::tryDocumentDrag(const DragData& dragData, DragDestinationAction actionMask, DragOperation& dragOperation)
389{
390 if (!m_documentUnderMouse)
391 return DragHandlingMethod::None;
392
393 if (m_dragInitiator && !m_documentUnderMouse->securityOrigin().canReceiveDragData(m_dragInitiator->securityOrigin()))
394 return DragHandlingMethod::None;
395
396 bool isHandlingDrag = false;
397 if (actionMask & DragDestinationActionDHTML) {
398 isHandlingDrag = tryDHTMLDrag(dragData, dragOperation);
399 // Do not continue if m_documentUnderMouse has been reset by tryDHTMLDrag.
400 // tryDHTMLDrag fires dragenter event. The event listener that listens
401 // to this event may create a nested message loop (open a modal dialog),
402 // which could process dragleave event and reset m_documentUnderMouse in
403 // dragExited.
404 if (!m_documentUnderMouse)
405 return DragHandlingMethod::None;
406 }
407
408 // It's unclear why this check is after tryDHTMLDrag.
409 // We send drag events in tryDHTMLDrag and that may be the reason.
410 RefPtr<FrameView> frameView = m_documentUnderMouse->view();
411 if (!frameView)
412 return DragHandlingMethod::None;
413
414 if (isHandlingDrag) {
415 clearDragCaret();
416 m_numberOfItemsToBeAccepted = dragData.numberOfFiles();
417 return DragHandlingMethod::NonDefault;
418 }
419
420 if ((actionMask & DragDestinationActionEdit) && canProcessDrag(dragData)) {
421 if (dragData.containsColor()) {
422 dragOperation = DragOperationGeneric;
423 return DragHandlingMethod::SetColor;
424 }
425
426 IntPoint point = frameView->windowToContents(dragData.clientPosition());
427 Element* element = elementUnderMouse(m_documentUnderMouse.get(), point);
428 if (!element)
429 return DragHandlingMethod::None;
430
431 HTMLInputElement* elementAsFileInput = asFileInput(*element);
432 if (m_fileInputElementUnderMouse != elementAsFileInput) {
433 if (m_fileInputElementUnderMouse)
434 m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
435 m_fileInputElementUnderMouse = elementAsFileInput;
436 }
437
438 if (!m_fileInputElementUnderMouse)
439 m_page.dragCaretController().setCaretPosition(m_documentUnderMouse->frame()->visiblePositionForPoint(point));
440 else
441 clearDragCaret();
442
443 Frame* innerFrame = element->document().frame();
444 dragOperation = dragIsMove(innerFrame->selection(), dragData) ? DragOperationMove : DragOperationCopy;
445
446 unsigned numberOfFiles = dragData.numberOfFiles();
447 if (m_fileInputElementUnderMouse) {
448 if (m_fileInputElementUnderMouse->isDisabledFormControl())
449 m_numberOfItemsToBeAccepted = 0;
450 else if (m_fileInputElementUnderMouse->multiple())
451 m_numberOfItemsToBeAccepted = numberOfFiles;
452 else if (numberOfFiles > 1)
453 m_numberOfItemsToBeAccepted = 0;
454 else
455 m_numberOfItemsToBeAccepted = 1;
456
457 if (!m_numberOfItemsToBeAccepted)
458 dragOperation = DragOperationNone;
459 m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(m_numberOfItemsToBeAccepted);
460 } else {
461 // We are not over a file input element. The dragged item(s) will loaded into the view,
462 // dropped as text paths on other input elements, or handled by script on the page.
463 m_numberOfItemsToBeAccepted = numberOfFiles;
464 }
465
466 if (m_fileInputElementUnderMouse)
467 return DragHandlingMethod::UploadFile;
468
469 if (m_page.dragCaretController().isContentRichlyEditable())
470 return DragHandlingMethod::EditRichText;
471
472 return DragHandlingMethod::EditPlainText;
473 }
474
475 // We are not over an editable region. Make sure we're clearing any prior drag cursor.
476 clearDragCaret();
477 if (m_fileInputElementUnderMouse)
478 m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
479 m_fileInputElementUnderMouse = nullptr;
480 return DragHandlingMethod::None;
481}
482
483DragSourceAction DragController::delegateDragSourceAction(const IntPoint& rootViewPoint)
484{
485 m_dragSourceAction = m_client.dragSourceActionMaskForPoint(rootViewPoint);
486 return m_dragSourceAction;
487}
488
489DragOperation DragController::operationForLoad(const DragData& dragData)
490{
491 Document* document = m_page.mainFrame().documentAtPoint(dragData.clientPosition());
492
493 bool pluginDocumentAcceptsDrags = false;
494
495 if (is<PluginDocument>(document)) {
496 const Widget* widget = downcast<PluginDocument>(*document).pluginWidget();
497 const PluginViewBase* pluginView = is<PluginViewBase>(widget) ? downcast<PluginViewBase>(widget) : nullptr;
498
499 if (pluginView)
500 pluginDocumentAcceptsDrags = pluginView->shouldAllowNavigationFromDrags();
501 }
502
503 if (document && (m_didInitiateDrag || (is<PluginDocument>(*document) && !pluginDocumentAcceptsDrags) || document->hasEditableStyle()))
504 return DragOperationNone;
505 return dragOperation(dragData);
506}
507
508static bool setSelectionToDragCaret(Frame* frame, VisibleSelection& dragCaret, RefPtr<Range>& range, const IntPoint& point)
509{
510 Ref<Frame> protector(*frame);
511 frame->selection().setSelection(dragCaret);
512 if (frame->selection().selection().isNone()) {
513 dragCaret = frame->visiblePositionForPoint(point);
514 frame->selection().setSelection(dragCaret);
515 range = dragCaret.toNormalizedRange();
516 }
517 return !frame->selection().isNone() && frame->selection().selection().isContentEditable();
518}
519
520bool DragController::dispatchTextInputEventFor(Frame* innerFrame, const DragData& dragData)
521{
522 ASSERT(m_page.dragCaretController().hasCaret());
523 String text = m_page.dragCaretController().isContentRichlyEditable() ? emptyString() : dragData.asPlainText();
524 Element* target = innerFrame->editor().findEventTargetFrom(m_page.dragCaretController().caretPosition());
525 // FIXME: What guarantees target is not null?
526 auto event = TextEvent::createForDrop(&innerFrame->windowProxy(), text);
527 target->dispatchEvent(event);
528 return !event->defaultPrevented();
529}
530
531bool DragController::concludeEditDrag(const DragData& dragData)
532{
533 RefPtr<HTMLInputElement> fileInput = m_fileInputElementUnderMouse;
534 if (m_fileInputElementUnderMouse) {
535 m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
536 m_fileInputElementUnderMouse = nullptr;
537 }
538
539 if (!m_documentUnderMouse)
540 return false;
541
542 IntPoint point = m_documentUnderMouse->view()->windowToContents(dragData.clientPosition());
543 Element* element = elementUnderMouse(m_documentUnderMouse.get(), point);
544 if (!element)
545 return false;
546 RefPtr<Frame> innerFrame = element->document().frame();
547 ASSERT(innerFrame);
548
549 if (m_page.dragCaretController().hasCaret() && !dispatchTextInputEventFor(innerFrame.get(), dragData))
550 return true;
551
552 if (dragData.containsColor()) {
553 Color color = dragData.asColor();
554 if (!color.isValid())
555 return false;
556#if ENABLE(INPUT_TYPE_COLOR)
557 if (isEnabledColorInput(*element)) {
558 auto& input = downcast<HTMLInputElement>(*element);
559 input.setValue(color.serialized(), DispatchInputAndChangeEvent);
560 return true;
561 }
562#endif
563 auto innerRange = innerFrame->selection().toNormalizedRange();
564 if (!innerRange)
565 return false;
566 auto style = MutableStyleProperties::create();
567 style->setProperty(CSSPropertyColor, color.serialized(), false);
568 if (!innerFrame->editor().shouldApplyStyle(style.ptr(), innerRange.get()))
569 return false;
570 m_client.willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
571 innerFrame->editor().applyStyle(style.ptr(), EditAction::SetColor);
572 return true;
573 }
574
575 if (dragData.containsFiles() && fileInput) {
576 // fileInput should be the element we hit tested for, unless it was made
577 // display:none in a drop event handler.
578 ASSERT(fileInput == element || !fileInput->renderer());
579 if (fileInput->isDisabledFormControl())
580 return false;
581
582 return fileInput->receiveDroppedFiles(dragData);
583 }
584
585 if (!m_page.dragController().canProcessDrag(dragData))
586 return false;
587
588 VisibleSelection dragCaret = m_page.dragCaretController().caretPosition();
589 RefPtr<Range> range = dragCaret.toNormalizedRange();
590 RefPtr<Element> rootEditableElement = innerFrame->selection().selection().rootEditableElement();
591
592 // For range to be null a WebKit client must have done something bad while
593 // manually controlling drag behaviour
594 if (!range)
595 return false;
596
597 ResourceCacheValidationSuppressor validationSuppressor(range->ownerDocument().cachedResourceLoader());
598 auto& editor = innerFrame->editor();
599 bool isMove = dragIsMove(innerFrame->selection(), dragData);
600 if (isMove || dragCaret.isContentRichlyEditable()) {
601 bool chosePlainText = false;
602 RefPtr<DocumentFragment> fragment = documentFragmentFromDragData(dragData, *innerFrame, *range, true, chosePlainText);
603 if (!fragment || !editor.shouldInsertFragment(*fragment, range.get(), EditorInsertAction::Dropped))
604 return false;
605
606 m_client.willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
607
608 if (editor.client() && editor.client()->performTwoStepDrop(*fragment, *range, isMove))
609 return true;
610
611 if (isMove) {
612 // NSTextView behavior is to always smart delete on moving a selection,
613 // but only to smart insert if the selection granularity is word granularity.
614 bool smartDelete = editor.smartInsertDeleteEnabled();
615 bool smartInsert = smartDelete && innerFrame->selection().granularity() == WordGranularity && dragData.canSmartReplace();
616 MoveSelectionCommand::create(fragment.releaseNonNull(), dragCaret.base(), smartInsert, smartDelete)->apply();
617 } else {
618 if (setSelectionToDragCaret(innerFrame.get(), dragCaret, range, point)) {
619 OptionSet<ReplaceSelectionCommand::CommandOption> options { ReplaceSelectionCommand::SelectReplacement, ReplaceSelectionCommand::PreventNesting };
620 if (dragData.canSmartReplace())
621 options.add(ReplaceSelectionCommand::SmartReplace);
622 if (chosePlainText)
623 options.add(ReplaceSelectionCommand::MatchStyle);
624 ReplaceSelectionCommand::create(*m_documentUnderMouse, fragment.releaseNonNull(), options, EditAction::InsertFromDrop)->apply();
625 }
626 }
627 } else {
628 String text = dragData.asPlainText();
629 if (text.isEmpty() || !editor.shouldInsertText(text, range.get(), EditorInsertAction::Dropped))
630 return false;
631
632 m_client.willPerformDragDestinationAction(DragDestinationActionEdit, dragData);
633 RefPtr<DocumentFragment> fragment = createFragmentFromText(*range, text);
634 if (!fragment)
635 return false;
636
637 if (editor.client() && editor.client()->performTwoStepDrop(*fragment, *range, isMove))
638 return true;
639
640 if (setSelectionToDragCaret(innerFrame.get(), dragCaret, range, point))
641 ReplaceSelectionCommand::create(*m_documentUnderMouse, fragment.get(), { ReplaceSelectionCommand::SelectReplacement, ReplaceSelectionCommand::MatchStyle, ReplaceSelectionCommand::PreventNesting }, EditAction::InsertFromDrop)->apply();
642 }
643
644 if (rootEditableElement) {
645 if (Frame* frame = rootEditableElement->document().frame())
646 frame->eventHandler().updateDragStateAfterEditDragIfNeeded(*rootEditableElement);
647 }
648
649 return true;
650}
651
652bool DragController::canProcessDrag(const DragData& dragData)
653{
654 IntPoint point = m_page.mainFrame().view()->windowToContents(dragData.clientPosition());
655 HitTestResult result = HitTestResult(point);
656 if (!m_page.mainFrame().contentRenderer())
657 return false;
658
659 result = m_page.mainFrame().eventHandler().hitTestResultAtPoint(point, HitTestRequest::ReadOnly | HitTestRequest::Active);
660
661 auto* dragNode = result.innerNonSharedNode();
662 if (!dragNode)
663 return false;
664
665 DragData::DraggingPurpose dragPurpose = DragData::DraggingPurpose::ForEditing;
666 if (asFileInput(*dragNode))
667 dragPurpose = DragData::DraggingPurpose::ForFileUpload;
668#if ENABLE(INPUT_TYPE_COLOR)
669 else if (isEnabledColorInput(*dragNode) || isInShadowTreeOfEnabledColorInput(*dragNode))
670 dragPurpose = DragData::DraggingPurpose::ForColorControl;
671#endif
672
673 if (!dragData.containsCompatibleContent(dragPurpose))
674 return false;
675
676 if (dragPurpose == DragData::DraggingPurpose::ForFileUpload)
677 return true;
678
679#if ENABLE(INPUT_TYPE_COLOR)
680 if (dragPurpose == DragData::DraggingPurpose::ForColorControl)
681 return true;
682#endif
683
684 if (is<HTMLPlugInElement>(*dragNode)) {
685 if (!downcast<HTMLPlugInElement>(*dragNode).canProcessDrag() && !dragNode->hasEditableStyle())
686 return false;
687 } else if (!dragNode->hasEditableStyle())
688 return false;
689
690 if (m_didInitiateDrag && m_documentUnderMouse == m_dragInitiator && result.isSelected())
691 return false;
692
693 return true;
694}
695
696static DragOperation defaultOperationForDrag(DragOperation srcOpMask)
697{
698 // This is designed to match IE's operation fallback for the case where
699 // the page calls preventDefault() in a drag event but doesn't set dropEffect.
700 if (srcOpMask == DragOperationEvery)
701 return DragOperationCopy;
702 if (srcOpMask == DragOperationNone)
703 return DragOperationNone;
704 if (srcOpMask & DragOperationMove)
705 return DragOperationMove;
706 if (srcOpMask & DragOperationGeneric)
707 return DragController::platformGenericDragOperation();
708 if (srcOpMask & DragOperationCopy)
709 return DragOperationCopy;
710 if (srcOpMask & DragOperationLink)
711 return DragOperationLink;
712
713 // FIXME: Does IE really return "generic" even if no operations were allowed by the source?
714 return DragOperationGeneric;
715}
716
717bool DragController::tryDHTMLDrag(const DragData& dragData, DragOperation& operation)
718{
719 ASSERT(m_documentUnderMouse);
720 Ref<Frame> mainFrame(m_page.mainFrame());
721 RefPtr<FrameView> viewProtector = mainFrame->view();
722 if (!viewProtector)
723 return false;
724
725 DragOperation sourceOperation = dragData.draggingSourceOperationMask();
726 auto targetResponse = mainFrame->eventHandler().updateDragAndDrop(createMouseEvent(dragData), [&dragData]() { return Pasteboard::createForDragAndDrop(dragData); }, sourceOperation, dragData.containsFiles());
727 if (!targetResponse.accept)
728 return false;
729
730 if (!targetResponse.operation)
731 operation = defaultOperationForDrag(sourceOperation);
732 else if (!(sourceOperation & targetResponse.operation.value())) // The element picked an operation which is not supported by the source
733 operation = DragOperationNone;
734 else
735 operation = targetResponse.operation.value();
736
737 return true;
738}
739
740static bool imageElementIsDraggable(const HTMLImageElement& image, const Frame& sourceFrame)
741{
742 if (sourceFrame.settings().loadsImagesAutomatically())
743 return true;
744
745 auto* renderer = image.renderer();
746 if (!is<RenderImage>(renderer))
747 return false;
748
749 auto* cachedImage = downcast<RenderImage>(*renderer).cachedImage();
750 return cachedImage && !cachedImage->errorOccurred() && cachedImage->imageForRenderer(renderer);
751}
752
753#if ENABLE(ATTACHMENT_ELEMENT)
754
755static RefPtr<HTMLAttachmentElement> enclosingAttachmentElement(Element& element)
756{
757 if (is<HTMLAttachmentElement>(element))
758 return downcast<HTMLAttachmentElement>(&element);
759
760 if (is<HTMLAttachmentElement>(element.parentOrShadowHostElement()))
761 return downcast<HTMLAttachmentElement>(element.parentOrShadowHostElement());
762
763 return { };
764}
765
766#endif
767
768Element* DragController::draggableElement(const Frame* sourceFrame, Element* startElement, const IntPoint& dragOrigin, DragState& state) const
769{
770 state.type = (sourceFrame->selection().contains(dragOrigin)) ? DragSourceActionSelection : DragSourceActionNone;
771 if (!startElement)
772 return nullptr;
773#if ENABLE(ATTACHMENT_ELEMENT)
774 if (auto attachment = enclosingAttachmentElement(*startElement)) {
775 auto selection = sourceFrame->selection().selection();
776 bool isSingleAttachmentSelection = selection.start() == Position(attachment.get(), Position::PositionIsBeforeAnchor) && selection.end() == Position(attachment.get(), Position::PositionIsAfterAnchor);
777 bool isAttachmentElementInCurrentSelection = false;
778 if (auto selectedRange = selection.toNormalizedRange()) {
779 auto compareResult = selectedRange->compareNode(*attachment);
780 isAttachmentElementInCurrentSelection = !compareResult.hasException() && compareResult.releaseReturnValue() == Range::NODE_INSIDE;
781 }
782
783 if (!isAttachmentElementInCurrentSelection || isSingleAttachmentSelection) {
784 state.type = DragSourceActionAttachment;
785 return attachment.get();
786 }
787 }
788#endif
789
790 for (auto* element = startElement; element; element = element->parentOrShadowHostElement()) {
791 auto* renderer = element->renderer();
792 if (!renderer)
793 continue;
794
795 UserDrag dragMode = renderer->style().userDrag();
796 if ((m_dragSourceAction & DragSourceActionDHTML) && dragMode == UserDrag::Element) {
797 state.type = static_cast<DragSourceAction>(state.type | DragSourceActionDHTML);
798 return element;
799 }
800 if (dragMode == UserDrag::Auto) {
801 if ((m_dragSourceAction & DragSourceActionImage)
802 && is<HTMLImageElement>(*element)
803 && imageElementIsDraggable(downcast<HTMLImageElement>(*element), *sourceFrame)) {
804 state.type = static_cast<DragSourceAction>(state.type | DragSourceActionImage);
805 return element;
806 }
807 if ((m_dragSourceAction & DragSourceActionLink) && isDraggableLink(*element)) {
808 state.type = static_cast<DragSourceAction>(state.type | DragSourceActionLink);
809 return element;
810 }
811#if ENABLE(ATTACHMENT_ELEMENT)
812 if ((m_dragSourceAction & DragSourceActionAttachment)
813 && is<HTMLAttachmentElement>(*element)
814 && downcast<HTMLAttachmentElement>(*element).file()) {
815 state.type = static_cast<DragSourceAction>(state.type | DragSourceActionAttachment);
816 return element;
817 }
818#endif
819#if ENABLE(INPUT_TYPE_COLOR)
820 if ((m_dragSourceAction & DragSourceActionColor) && isEnabledColorInput(*element)) {
821 state.type = static_cast<DragSourceAction>(state.type | DragSourceActionColor);
822 return element;
823 }
824#endif
825 }
826 }
827
828 // We either have nothing to drag or we have a selection and we're not over a draggable element.
829 if (state.type & DragSourceActionSelection && m_dragSourceAction & DragSourceActionSelection)
830 return startElement;
831
832 return nullptr;
833}
834
835static CachedImage* getCachedImage(Element& element)
836{
837 RenderObject* renderer = element.renderer();
838 if (!is<RenderImage>(renderer))
839 return nullptr;
840 auto& image = downcast<RenderImage>(*renderer);
841 return image.cachedImage();
842}
843
844static Image* getImage(Element& element)
845{
846 CachedImage* cachedImage = getCachedImage(element);
847 // Don't use cachedImage->imageForRenderer() here as that may return BitmapImages for cached SVG Images.
848 // Users of getImage() want access to the SVGImage, in order to figure out the filename extensions,
849 // which would be empty when asking the cached BitmapImages.
850 return (cachedImage && !cachedImage->errorOccurred()) ?
851 cachedImage->image() : nullptr;
852}
853
854static void selectElement(Element& element)
855{
856 RefPtr<Range> range = element.document().createRange();
857 range->selectNode(element);
858 element.document().frame()->selection().setSelection(VisibleSelection(*range, DOWNSTREAM));
859}
860
861static IntPoint dragLocForDHTMLDrag(const IntPoint& mouseDraggedPoint, const IntPoint& dragOrigin, const IntPoint& dragImageOffset, bool isLinkImage)
862{
863 // dragImageOffset is the cursor position relative to the lower-left corner of the image.
864#if PLATFORM(MAC)
865 // We add in the Y dimension because we are a flipped view, so adding moves the image down.
866 const int yOffset = dragImageOffset.y();
867#else
868 const int yOffset = -dragImageOffset.y();
869#endif
870
871 if (isLinkImage)
872 return IntPoint(mouseDraggedPoint.x() - dragImageOffset.x(), mouseDraggedPoint.y() + yOffset);
873
874 return IntPoint(dragOrigin.x() - dragImageOffset.x(), dragOrigin.y() + yOffset);
875}
876
877static FloatPoint dragImageAnchorPointForSelectionDrag(Frame& frame, const IntPoint& mouseDraggedPoint)
878{
879 IntRect draggingRect = enclosingIntRect(frame.selection().selectionBounds());
880
881 float x = (mouseDraggedPoint.x() - draggingRect.x()) / (float)draggingRect.width();
882 float y = (mouseDraggedPoint.y() - draggingRect.y()) / (float)draggingRect.height();
883
884 return FloatPoint { x, y };
885}
886
887static IntPoint dragLocForSelectionDrag(Frame& src)
888{
889 IntRect draggingRect = enclosingIntRect(src.selection().selectionBounds());
890 int xpos = draggingRect.maxX();
891 xpos = draggingRect.x() < xpos ? draggingRect.x() : xpos;
892 int ypos = draggingRect.maxY();
893#if PLATFORM(COCOA)
894 // Deal with flipped coordinates on Mac
895 ypos = draggingRect.y() > ypos ? draggingRect.y() : ypos;
896#else
897 ypos = draggingRect.y() < ypos ? draggingRect.y() : ypos;
898#endif
899 return IntPoint(xpos, ypos);
900}
901
902bool DragController::startDrag(Frame& src, const DragState& state, DragOperation srcOp, const PlatformMouseEvent& dragEvent, const IntPoint& dragOrigin, HasNonDefaultPasteboardData hasData)
903{
904 if (!src.view() || !src.contentRenderer() || !state.source)
905 return false;
906
907 Ref<Frame> protector(src);
908 HitTestResult hitTestResult = src.eventHandler().hitTestResultAtPoint(dragOrigin, HitTestRequest::ReadOnly | HitTestRequest::Active);
909
910 bool sourceContainsHitNode = state.source->containsIncludingShadowDOM(hitTestResult.innerNode());
911 if (!sourceContainsHitNode) {
912 // The original node being dragged isn't under the drag origin anymore... maybe it was
913 // hidden or moved out from under the cursor. Regardless, we don't want to start a drag on
914 // something that's not actually under the drag origin.
915 return false;
916 }
917
918 URL linkURL = hitTestResult.absoluteLinkURL();
919 URL imageURL = hitTestResult.absoluteImageURL();
920
921 IntPoint mouseDraggedPoint = src.view()->windowToContents(dragEvent.position());
922
923 m_draggingImageURL = URL();
924 m_sourceDragOperation = srcOp;
925
926 DragImage dragImage;
927 IntPoint dragLoc(0, 0);
928 IntPoint dragImageOffset(0, 0);
929
930 ASSERT(state.dataTransfer);
931
932 DataTransfer& dataTransfer = *state.dataTransfer;
933 if (state.type == DragSourceActionDHTML) {
934 dragImage = DragImage { dataTransfer.createDragImage(dragImageOffset) };
935 // We allow DHTML/JS to set the drag image, even if its a link, image or text we're dragging.
936 // This is in the spirit of the IE API, which allows overriding of pasteboard data and DragOp.
937 if (dragImage) {
938 dragLoc = dragLocForDHTMLDrag(mouseDraggedPoint, dragOrigin, dragImageOffset, !linkURL.isEmpty());
939 m_dragOffset = dragImageOffset;
940 }
941 }
942
943 if (state.type == DragSourceActionSelection || !imageURL.isEmpty() || !linkURL.isEmpty()) {
944 // Selection, image, and link drags receive a default set of allowed drag operations that
945 // follows from:
946 // http://trac.webkit.org/browser/trunk/WebKit/mac/WebView/WebHTMLView.mm?rev=48526#L3430
947 m_sourceDragOperation = static_cast<DragOperation>(m_sourceDragOperation | DragOperationGeneric | DragOperationCopy);
948 }
949
950 ASSERT(state.source);
951 Element& element = *state.source;
952
953 bool mustUseLegacyDragClient = hasData == HasNonDefaultPasteboardData::Yes || m_client.useLegacyDragClient();
954
955 IntRect dragImageBounds;
956 Image* image = getImage(element);
957 if (state.type == DragSourceActionSelection) {
958 PasteboardWriterData pasteboardWriterData;
959
960 if (hasData == HasNonDefaultPasteboardData::No) {
961 if (src.selection().selection().isNone()) {
962 // The page may have cleared out the selection in the dragstart handler, in which case we should bail
963 // out of the drag, since there is no content to write to the pasteboard.
964 return false;
965 }
966
967 // FIXME: This entire block is almost identical to the code in Editor::copy, and the code should be shared.
968 RefPtr<Range> selectionRange = src.selection().toNormalizedRange();
969 ASSERT(selectionRange);
970
971 src.editor().willWriteSelectionToPasteboard(selectionRange.get());
972
973 if (enclosingTextFormControl(src.selection().selection().start())) {
974 if (mustUseLegacyDragClient)
975 dataTransfer.pasteboard().writePlainText(src.editor().selectedTextForDataTransfer(), Pasteboard::CannotSmartReplace);
976 else {
977 PasteboardWriterData::PlainText plainText;
978 plainText.canSmartCopyOrDelete = false;
979 plainText.text = src.editor().selectedTextForDataTransfer();
980 pasteboardWriterData.setPlainText(WTFMove(plainText));
981 }
982 } else {
983 if (mustUseLegacyDragClient) {
984#if PLATFORM(COCOA) || PLATFORM(GTK)
985 src.editor().writeSelectionToPasteboard(dataTransfer.pasteboard());
986#else
987 // FIXME: Convert all other platforms to match Mac and delete this.
988 dataTransfer.pasteboard().writeSelection(*selectionRange, src.editor().canSmartCopyOrDelete(), src, IncludeImageAltTextForDataTransfer);
989#endif
990 } else {
991#if PLATFORM(COCOA)
992 src.editor().writeSelection(pasteboardWriterData);
993#endif
994 }
995 }
996
997 src.editor().didWriteSelectionToPasteboard();
998 }
999 m_client.willPerformDragSourceAction(DragSourceActionSelection, dragOrigin, dataTransfer);
1000 if (!dragImage) {
1001 TextIndicatorData textIndicator;
1002 dragImage = DragImage { dissolveDragImageToFraction(createDragImageForSelection(src, textIndicator), DragImageAlpha) };
1003 if (textIndicator.contentImage)
1004 dragImage.setIndicatorData(textIndicator);
1005 dragLoc = dragLocForSelectionDrag(src);
1006 m_dragOffset = IntPoint(dragOrigin.x() - dragLoc.x(), dragOrigin.y() - dragLoc.y());
1007 }
1008
1009 if (!dragImage)
1010 return false;
1011
1012 if (mustUseLegacyDragClient) {
1013 doSystemDrag(WTFMove(dragImage), dragLoc, dragOrigin, src, state, { });
1014 return true;
1015 }
1016
1017 DragItem dragItem;
1018 dragItem.imageAnchorPoint = dragImageAnchorPointForSelectionDrag(src, mouseDraggedPoint);
1019 dragItem.image = WTFMove(dragImage);
1020 dragItem.data = WTFMove(pasteboardWriterData);
1021
1022 beginDrag(WTFMove(dragItem), src, dragOrigin, mouseDraggedPoint, dataTransfer, DragSourceActionSelection);
1023
1024 return true;
1025 }
1026
1027 if (!src.document()->securityOrigin().canDisplay(linkURL)) {
1028 src.document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Not allowed to drag local resource: " + linkURL.stringCenterEllipsizedToLength());
1029 return false;
1030 }
1031
1032 if (!imageURL.isEmpty() && image && !image->isNull() && (m_dragSourceAction & DragSourceActionImage)) {
1033 // We shouldn't be starting a drag for an image that can't provide an extension.
1034 // This is an early detection for problems encountered later upon drop.
1035 ASSERT(!image->filenameExtension().isEmpty());
1036
1037#if ENABLE(ATTACHMENT_ELEMENT)
1038 auto attachmentInfo = promisedAttachmentInfo(src, element);
1039#else
1040 PromisedAttachmentInfo attachmentInfo;
1041#endif
1042
1043 if (hasData == HasNonDefaultPasteboardData::No) {
1044 m_draggingImageURL = imageURL;
1045 if (element.isContentRichlyEditable())
1046 selectElement(element);
1047 if (!attachmentInfo)
1048 declareAndWriteDragImage(dataTransfer, element, !linkURL.isEmpty() ? linkURL : imageURL, hitTestResult.altDisplayString());
1049 }
1050
1051 m_client.willPerformDragSourceAction(DragSourceActionImage, dragOrigin, dataTransfer);
1052
1053 if (!dragImage)
1054 doImageDrag(element, dragOrigin, hitTestResult.imageRect(), src, m_dragOffset, state, WTFMove(attachmentInfo));
1055 else {
1056 // DHTML defined drag image
1057 doSystemDrag(WTFMove(dragImage), dragLoc, dragOrigin, src, state, WTFMove(attachmentInfo));
1058 }
1059
1060 return true;
1061 }
1062
1063 if (!linkURL.isEmpty() && (m_dragSourceAction & DragSourceActionLink)) {
1064 PasteboardWriterData pasteboardWriterData;
1065
1066 String textContentWithSimplifiedWhiteSpace = hitTestResult.textContent().simplifyWhiteSpace();
1067
1068 if (hasData == HasNonDefaultPasteboardData::No) {
1069 // Simplify whitespace so the title put on the dataTransfer resembles what the user sees
1070 // on the web page. This includes replacing newlines with spaces.
1071 if (mustUseLegacyDragClient)
1072 src.editor().copyURL(linkURL, textContentWithSimplifiedWhiteSpace, dataTransfer.pasteboard());
1073 else
1074 pasteboardWriterData.setURLData(src.editor().pasteboardWriterURL(linkURL, textContentWithSimplifiedWhiteSpace));
1075 } else {
1076 // Make sure the pasteboard also contains trustworthy link data
1077 // but don't overwrite more general pasteboard types.
1078 PasteboardURL pasteboardURL;
1079 pasteboardURL.url = linkURL;
1080 pasteboardURL.title = hitTestResult.textContent();
1081 dataTransfer.pasteboard().writeTrustworthyWebURLsPboardType(pasteboardURL);
1082 }
1083
1084 const VisibleSelection& sourceSelection = src.selection().selection();
1085 if (sourceSelection.isCaret() && sourceSelection.isContentEditable()) {
1086 // a user can initiate a drag on a link without having any text
1087 // selected. In this case, we should expand the selection to
1088 // the enclosing anchor element
1089 Position pos = sourceSelection.base();
1090 Node* node = enclosingAnchorElement(pos);
1091 if (node)
1092 src.selection().setSelection(VisibleSelection::selectionFromContentsOfNode(node));
1093 }
1094
1095 m_client.willPerformDragSourceAction(DragSourceActionLink, dragOrigin, dataTransfer);
1096 if (!dragImage) {
1097 TextIndicatorData textIndicator;
1098 dragImage = DragImage { createDragImageForLink(element, linkURL, textContentWithSimplifiedWhiteSpace, textIndicator, src.settings().fontRenderingMode(), m_page.deviceScaleFactor()) };
1099 if (dragImage) {
1100 m_dragOffset = dragOffsetForLinkDragImage(dragImage.get());
1101 dragLoc = IntPoint(dragOrigin.x() + m_dragOffset.x(), dragOrigin.y() + m_dragOffset.y());
1102 dragImage = DragImage { platformAdjustDragImageForDeviceScaleFactor(dragImage.get(), m_page.deviceScaleFactor()) };
1103 if (textIndicator.contentImage)
1104 dragImage.setIndicatorData(textIndicator);
1105 }
1106 }
1107
1108 if (mustUseLegacyDragClient) {
1109 doSystemDrag(WTFMove(dragImage), dragLoc, dragOrigin, src, state, { });
1110 return true;
1111 }
1112
1113 DragItem dragItem;
1114 dragItem.imageAnchorPoint = dragImage ? anchorPointForLinkDragImage(dragImage.get()) : FloatPoint();
1115 dragItem.image = WTFMove(dragImage);
1116 dragItem.data = WTFMove(pasteboardWriterData);
1117
1118 beginDrag(WTFMove(dragItem), src, dragOrigin, mouseDraggedPoint, dataTransfer, DragSourceActionSelection);
1119
1120 return true;
1121 }
1122
1123#if ENABLE(ATTACHMENT_ELEMENT)
1124 if (is<HTMLAttachmentElement>(element) && m_dragSourceAction & DragSourceActionAttachment) {
1125 auto& attachment = downcast<HTMLAttachmentElement>(element);
1126 auto* attachmentRenderer = attachment.renderer();
1127
1128 src.editor().setIgnoreSelectionChanges(true);
1129 auto previousSelection = src.selection().selection();
1130 selectElement(element);
1131
1132 PromisedAttachmentInfo promisedAttachment;
1133 if (hasData == HasNonDefaultPasteboardData::No) {
1134 promisedAttachment = promisedAttachmentInfo(src, attachment);
1135 auto& editor = src.editor();
1136 if (!promisedAttachment && editor.client()) {
1137#if PLATFORM(COCOA)
1138 // Otherwise, if no file URL is specified, call out to the injected bundle to populate the pasteboard with data.
1139 editor.willWriteSelectionToPasteboard(src.selection().toNormalizedRange().get());
1140 editor.writeSelectionToPasteboard(dataTransfer.pasteboard());
1141 editor.didWriteSelectionToPasteboard();
1142#endif
1143 }
1144 }
1145
1146 m_client.willPerformDragSourceAction(DragSourceActionAttachment, dragOrigin, dataTransfer);
1147
1148 if (!dragImage) {
1149 TextIndicatorData textIndicator;
1150 if (attachmentRenderer)
1151 attachmentRenderer->setShouldDrawBorder(false);
1152 dragImage = DragImage { dissolveDragImageToFraction(createDragImageForSelection(src, textIndicator), DragImageAlpha) };
1153 if (attachmentRenderer)
1154 attachmentRenderer->setShouldDrawBorder(true);
1155 if (textIndicator.contentImage)
1156 dragImage.setIndicatorData(textIndicator);
1157 dragLoc = dragLocForSelectionDrag(src);
1158 m_dragOffset = IntPoint(dragOrigin.x() - dragLoc.x(), dragOrigin.y() - dragLoc.y());
1159 }
1160 doSystemDrag(WTFMove(dragImage), dragLoc, dragOrigin, src, state, WTFMove(promisedAttachment));
1161 if (!element.isContentRichlyEditable())
1162 src.selection().setSelection(previousSelection);
1163 src.editor().setIgnoreSelectionChanges(false);
1164 return true;
1165 }
1166#endif
1167
1168#if ENABLE(INPUT_TYPE_COLOR)
1169 bool isColorControl = is<HTMLInputElement>(state.source) && downcast<HTMLInputElement>(*state.source).isColorControl();
1170 if (isColorControl && m_dragSourceAction & DragSourceActionColor) {
1171 auto& input = downcast<HTMLInputElement>(*state.source);
1172 auto color = input.valueAsColor();
1173
1174 Path visiblePath;
1175 dragImage = DragImage { createDragImageForColor(color, input.boundsInRootViewSpace(), input.document().page()->pageScaleFactor(), visiblePath) };
1176 dragImage.setVisiblePath(visiblePath);
1177 dataTransfer.pasteboard().write(color);
1178 dragImageOffset = IntPoint { dragImageSize(dragImage.get()) };
1179 dragLoc = dragLocForDHTMLDrag(mouseDraggedPoint, dragOrigin, dragImageOffset, false);
1180
1181 m_client.willPerformDragSourceAction(DragSourceActionColor, dragOrigin, dataTransfer);
1182 doSystemDrag(WTFMove(dragImage), dragLoc, dragOrigin, src, state, { });
1183 return true;
1184 }
1185#endif
1186
1187 if (state.type == DragSourceActionDHTML && dragImage) {
1188 ASSERT(m_dragSourceAction & DragSourceActionDHTML);
1189 m_client.willPerformDragSourceAction(DragSourceActionDHTML, dragOrigin, dataTransfer);
1190 doSystemDrag(WTFMove(dragImage), dragLoc, dragOrigin, src, state, { });
1191 return true;
1192 }
1193
1194 return false;
1195}
1196
1197void DragController::doImageDrag(Element& element, const IntPoint& dragOrigin, const IntRect& layoutRect, Frame& frame, IntPoint& dragImageOffset, const DragState& state, PromisedAttachmentInfo&& attachmentInfo)
1198{
1199 IntPoint mouseDownPoint = dragOrigin;
1200 DragImage dragImage;
1201 IntPoint scaledOrigin;
1202
1203 if (!element.renderer())
1204 return;
1205
1206 ImageOrientationDescription orientationDescription(element.renderer()->shouldRespectImageOrientation(), element.renderer()->style().imageOrientation());
1207
1208 Image* image = getImage(element);
1209 if (image && !layoutRect.isEmpty() && shouldUseCachedImageForDragImage(*image) && (dragImage = DragImage { createDragImageFromImage(image, element.renderer() ? orientationDescription : ImageOrientationDescription()) })) {
1210 dragImage = DragImage { fitDragImageToMaxSize(dragImage.get(), layoutRect.size(), maxDragImageSize()) };
1211 IntSize fittedSize = dragImageSize(dragImage.get());
1212
1213 dragImage = DragImage { platformAdjustDragImageForDeviceScaleFactor(dragImage.get(), m_page.deviceScaleFactor()) };
1214 dragImage = DragImage { dissolveDragImageToFraction(dragImage.get(), DragImageAlpha) };
1215
1216 // Properly orient the drag image and orient it differently if it's smaller than the original.
1217 float scale = fittedSize.width() / (float)layoutRect.width();
1218 float dx = scale * (layoutRect.x() - mouseDownPoint.x());
1219 float originY = layoutRect.y();
1220#if PLATFORM(COCOA)
1221 // Compensate for accursed flipped coordinates in Cocoa.
1222 originY += layoutRect.height();
1223#endif
1224 float dy = scale * (originY - mouseDownPoint.y());
1225 scaledOrigin = IntPoint((int)(dx + 0.5), (int)(dy + 0.5));
1226 } else {
1227 if (CachedImage* cachedImage = getCachedImage(element)) {
1228 dragImage = DragImage { createDragImageIconForCachedImageFilename(cachedImage->response().suggestedFilename()) };
1229 if (dragImage) {
1230 dragImage = DragImage { platformAdjustDragImageForDeviceScaleFactor(dragImage.get(), m_page.deviceScaleFactor()) };
1231 scaledOrigin = IntPoint(DragIconRightInset - dragImageSize(dragImage.get()).width(), DragIconBottomInset);
1232 }
1233 }
1234 }
1235
1236 if (!dragImage)
1237 return;
1238
1239 dragImageOffset = mouseDownPoint + scaledOrigin;
1240 doSystemDrag(WTFMove(dragImage), dragImageOffset, dragOrigin, frame, state, WTFMove(attachmentInfo));
1241}
1242
1243void DragController::beginDrag(DragItem dragItem, Frame& frame, const IntPoint& mouseDownPoint, const IntPoint& mouseDraggedPoint, DataTransfer& dataTransfer, DragSourceAction dragSourceAction)
1244{
1245 ASSERT(!m_client.useLegacyDragClient());
1246
1247 m_didInitiateDrag = true;
1248 m_dragInitiator = frame.document();
1249
1250 // Protect this frame and view, as a load may occur mid drag and attempt to unload this frame
1251 Ref<Frame> mainFrameProtector(m_page.mainFrame());
1252 RefPtr<FrameView> viewProtector = mainFrameProtector->view();
1253
1254 auto mouseDownPointInRootViewCoordinates = viewProtector->rootViewToContents(frame.view()->contentsToRootView(mouseDownPoint));
1255 auto mouseDraggedPointInRootViewCoordinates = viewProtector->rootViewToContents(frame.view()->contentsToRootView(mouseDraggedPoint));
1256
1257 m_client.beginDrag(WTFMove(dragItem), frame, mouseDownPointInRootViewCoordinates, mouseDraggedPointInRootViewCoordinates, dataTransfer, dragSourceAction);
1258}
1259
1260void DragController::doSystemDrag(DragImage image, const IntPoint& dragLoc, const IntPoint& eventPos, Frame& frame, const DragState& state, PromisedAttachmentInfo&& promisedAttachmentInfo)
1261{
1262 m_didInitiateDrag = true;
1263 m_dragInitiator = frame.document();
1264 // Protect this frame and view, as a load may occur mid drag and attempt to unload this frame
1265 Ref<Frame> frameProtector(m_page.mainFrame());
1266 RefPtr<FrameView> viewProtector = frameProtector->view();
1267
1268 DragItem item;
1269 item.image = WTFMove(image);
1270 item.sourceAction = state.type;
1271 item.promisedAttachmentInfo = WTFMove(promisedAttachmentInfo);
1272
1273 auto eventPositionInRootViewCoordinates = frame.view()->contentsToRootView(eventPos);
1274 auto dragLocationInRootViewCoordinates = frame.view()->contentsToRootView(dragLoc);
1275 item.eventPositionInContentCoordinates = viewProtector->rootViewToContents(eventPositionInRootViewCoordinates);
1276 item.dragLocationInContentCoordinates = viewProtector->rootViewToContents(dragLocationInRootViewCoordinates);
1277 item.dragLocationInWindowCoordinates = viewProtector->contentsToWindow(item.dragLocationInContentCoordinates);
1278 if (auto element = state.source) {
1279 auto dataTransferImageElement = state.dataTransfer->dragImageElement();
1280 if (state.type == DragSourceActionDHTML) {
1281 // If the drag image has been customized, fall back to positioning the preview relative to the drag event location.
1282 IntSize dragPreviewSize;
1283 if (dataTransferImageElement)
1284 dragPreviewSize = dataTransferImageElement->boundsInRootViewSpace().size();
1285 else {
1286 dragPreviewSize = dragImageSize(item.image.get());
1287 if (auto* page = frame.page())
1288 dragPreviewSize.scale(1 / page->deviceScaleFactor());
1289 }
1290 item.dragPreviewFrameInRootViewCoordinates = { dragLocationInRootViewCoordinates, WTFMove(dragPreviewSize) };
1291 } else {
1292 // We can position the preview using the bounds of the drag source element.
1293 item.dragPreviewFrameInRootViewCoordinates = element->boundsInRootViewSpace();
1294 }
1295
1296 RefPtr<Element> link;
1297 if (element->isLink())
1298 link = element;
1299 else {
1300 for (auto& currentElement : elementLineage(element.get())) {
1301 if (currentElement.isLink()) {
1302 link = &currentElement;
1303 break;
1304 }
1305 }
1306 }
1307 if (link) {
1308 auto titleAttribute = link->attributeWithoutSynchronization(HTMLNames::titleAttr);
1309 item.title = titleAttribute.isEmpty() ? link->innerText() : titleAttribute.string();
1310 item.url = frame.document()->completeURL(stripLeadingAndTrailingHTMLSpaces(link->getAttribute(HTMLNames::hrefAttr)));
1311 }
1312 }
1313 m_client.startDrag(WTFMove(item), *state.dataTransfer, frameProtector.get());
1314 // DragClient::startDrag can cause our Page to dispear, deallocating |this|.
1315 if (!frameProtector->page())
1316 return;
1317
1318 cleanupAfterSystemDrag();
1319}
1320
1321// Manual drag caret manipulation
1322void DragController::placeDragCaret(const IntPoint& windowPoint)
1323{
1324 mouseMovedIntoDocument(m_page.mainFrame().documentAtPoint(windowPoint));
1325 if (!m_documentUnderMouse)
1326 return;
1327 Frame* frame = m_documentUnderMouse->frame();
1328 FrameView* frameView = frame->view();
1329 if (!frameView)
1330 return;
1331 IntPoint framePoint = frameView->windowToContents(windowPoint);
1332
1333 m_page.dragCaretController().setCaretPosition(frame->visiblePositionForPoint(framePoint));
1334}
1335
1336bool DragController::shouldUseCachedImageForDragImage(const Image& image) const
1337{
1338#if ENABLE(DATA_INTERACTION)
1339 UNUSED_PARAM(image);
1340 return true;
1341#else
1342 return image.size().height() * image.size().width() <= MaxOriginalImageArea;
1343#endif
1344}
1345
1346#if !PLATFORM(COCOA)
1347
1348String DragController::platformContentTypeForBlobType(const String& type) const
1349{
1350 return type;
1351}
1352
1353#endif
1354
1355#if ENABLE(ATTACHMENT_ELEMENT)
1356
1357PromisedAttachmentInfo DragController::promisedAttachmentInfo(Frame& frame, Element& element)
1358{
1359 auto* client = frame.editor().client();
1360 if (!client || !client->supportsClientSideAttachmentData())
1361 return { };
1362
1363 RefPtr<HTMLAttachmentElement> attachment;
1364 if (is<HTMLAttachmentElement>(element))
1365 attachment = &downcast<HTMLAttachmentElement>(element);
1366 else if (is<HTMLImageElement>(element))
1367 attachment = downcast<HTMLImageElement>(element).attachmentElement();
1368
1369 if (!attachment)
1370 return { };
1371
1372 Vector<String> additionalTypes;
1373 Vector<RefPtr<SharedBuffer>> additionalData;
1374#if PLATFORM(COCOA)
1375 frame.editor().getPasteboardTypesAndDataForAttachment(element, additionalTypes, additionalData);
1376#endif
1377
1378 if (auto* file = attachment->file())
1379 return { file->url(), platformContentTypeForBlobType(file->type()), file->name(), { }, WTFMove(additionalTypes), WTFMove(additionalData) };
1380
1381 return { { }, { }, { }, attachment->uniqueIdentifier(), WTFMove(additionalTypes), WTFMove(additionalData) };
1382}
1383
1384#endif // ENABLE(ATTACHMENT_ELEMENT)
1385
1386#endif // ENABLE(DRAG_SUPPORT)
1387
1388} // namespace WebCore
1389