This source file includes following definitions.
- dragTypeIsValid
- createMouseEvent
- createDraggingClipboard
- m_didInitiateDrag
- create
- documentFragmentFromDragData
- dragIsMove
- cancelDrag
- dragEnded
- dragEntered
- dragExited
- dragUpdated
- performDrag
- mouseMovedIntoDocument
- dragEnteredOrUpdated
- asFileInput
- elementUnderMouse
- tryDocumentDrag
- operationForLoad
- setSelectionToDragCaret
- dispatchTextInputEventFor
- concludeEditDrag
- canProcessDrag
- defaultOperationForDrag
- tryDHTMLDrag
- draggableNode
- getImageResource
- getImage
- prepareClipboardForImageDrag
- populateDragClipboard
- dragLocationForDHTMLDrag
- dragLocationForSelectionDrag
- maxDragImageSize
- dragImageForImage
- dragImageForLink
- startDrag
- doSystemDrag
- dragOperation
- isCopyKeyDown
- cleanupAfterSystemDrag
#include "config.h"
#include "core/page/DragController.h"
#include "HTMLNames.h"
#include "bindings/v8/ExceptionStatePlaceholder.h"
#include "core/clipboard/Clipboard.h"
#include "core/clipboard/ClipboardAccessPolicy.h"
#include "core/clipboard/DataObject.h"
#include "core/dom/Document.h"
#include "core/dom/DocumentFragment.h"
#include "core/dom/Element.h"
#include "core/dom/Node.h"
#include "core/dom/Text.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/editing/Editor.h"
#include "core/editing/FrameSelection.h"
#include "core/editing/MoveSelectionCommand.h"
#include "core/editing/ReplaceSelectionCommand.h"
#include "core/editing/htmlediting.h"
#include "core/editing/markup.h"
#include "core/events/TextEvent.h"
#include "core/fetch/ImageResource.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLAnchorElement.h"
#include "core/html/HTMLFormElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLPlugInElement.h"
#include "core/loader/FrameLoadRequest.h"
#include "core/loader/FrameLoader.h"
#include "core/page/DragClient.h"
#include "core/page/DragData.h"
#include "core/page/DragSession.h"
#include "core/page/DragState.h"
#include "core/page/EventHandler.h"
#include "core/page/Page.h"
#include "core/frame/Settings.h"
#include "core/rendering/HitTestRequest.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/RenderImage.h"
#include "core/rendering/RenderTheme.h"
#include "core/rendering/RenderView.h"
#include "platform/DragImage.h"
#include "platform/geometry/FloatRect.h"
#include "platform/graphics/Image.h"
#include "platform/graphics/ImageOrientation.h"
#include "platform/network/ResourceRequest.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "wtf/CurrentTime.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/RefPtr.h"
#if OS(WIN)
#include <windows.h>
#endif
namespace WebCore {
const int DragController::DragIconRightInset = 7;
const int DragController::DragIconBottomInset = 3;
static const int MaxOriginalImageArea = 1500 * 1500;
static const int LinkDragBorderInset = 2;
static const float DragImageAlpha = 0.75f;
#if !ASSERT_DISABLED
static bool dragTypeIsValid(DragSourceAction action)
{
switch (action) {
case DragSourceActionDHTML:
case DragSourceActionImage:
case DragSourceActionLink:
case DragSourceActionSelection:
return true;
case DragSourceActionNone:
return false;
}
return false;
}
#endif
static PlatformMouseEvent createMouseEvent(DragData* dragData)
{
int keyState = dragData->modifierKeyState();
bool shiftKey = static_cast<bool>(keyState & PlatformEvent::ShiftKey);
bool ctrlKey = static_cast<bool>(keyState & PlatformEvent::CtrlKey);
bool altKey = static_cast<bool>(keyState & PlatformEvent::AltKey);
bool metaKey = static_cast<bool>(keyState & PlatformEvent::MetaKey);
return PlatformMouseEvent(dragData->clientPosition(), dragData->globalPosition(),
LeftButton, PlatformEvent::MouseMoved, 0, shiftKey, ctrlKey, altKey,
metaKey, currentTime());
}
static PassRefPtrWillBeRawPtr<Clipboard> createDraggingClipboard(ClipboardAccessPolicy policy, DragData* dragData)
{
return Clipboard::create(Clipboard::DragAndDrop, policy, dragData->platformData());
}
DragController::DragController(Page* page, DragClient* client)
: m_page(page)
, m_client(client)
, m_documentUnderMouse(nullptr)
, m_dragInitiator(nullptr)
, m_fileInputElementUnderMouse(nullptr)
, m_documentIsHandlingDrag(false)
, m_dragDestinationAction(DragDestinationActionNone)
, m_didInitiateDrag(false)
{
ASSERT(m_client);
}
DragController::~DragController()
{
}
PassOwnPtr<DragController> DragController::create(Page* page, DragClient* client)
{
return adoptPtr(new DragController(page, client));
}
static PassRefPtr<DocumentFragment> documentFragmentFromDragData(DragData* dragData, LocalFrame* frame, RefPtrWillBeRawPtr<Range> context,
bool allowPlainText, bool& chosePlainText)
{
ASSERT(dragData);
chosePlainText = false;
Document& document = context->ownerDocument();
if (dragData->containsCompatibleContent()) {
if (PassRefPtr<DocumentFragment> fragment = dragData->asFragment(frame, context, allowPlainText, chosePlainText))
return fragment;
if (dragData->containsURL(DragData::DoNotConvertFilenames)) {
String title;
String url = dragData->asURL(DragData::DoNotConvertFilenames, &title);
if (!url.isEmpty()) {
RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::create(document);
anchor->setHref(AtomicString(url));
if (title.isEmpty()) {
if (dragData->containsPlainText())
title = dragData->asPlainText();
if (title.isEmpty())
title = url;
}
RefPtr<Node> anchorText = document.createTextNode(title);
anchor->appendChild(anchorText);
RefPtr<DocumentFragment> fragment = document.createDocumentFragment();
fragment->appendChild(anchor);
return fragment.release();
}
}
}
if (allowPlainText && dragData->containsPlainText()) {
chosePlainText = true;
return createFragmentFromText(context.get(), dragData->asPlainText()).get();
}
return nullptr;
}
bool DragController::dragIsMove(FrameSelection& selection, DragData* dragData)
{
return m_documentUnderMouse == m_dragInitiator && selection.isContentEditable() && selection.isRange() && !isCopyKeyDown(dragData);
}
void DragController::cancelDrag()
{
m_page->dragCaretController().clear();
}
void DragController::dragEnded()
{
m_dragInitiator = nullptr;
m_didInitiateDrag = false;
m_page->dragCaretController().clear();
}
DragSession DragController::dragEntered(DragData* dragData)
{
return dragEnteredOrUpdated(dragData);
}
void DragController::dragExited(DragData* dragData)
{
ASSERT(dragData);
LocalFrame* mainFrame = m_page->mainFrame();
if (RefPtr<FrameView> v = mainFrame->view()) {
ClipboardAccessPolicy policy = (!m_documentUnderMouse || m_documentUnderMouse->securityOrigin()->isLocal()) ? ClipboardReadable : ClipboardTypesReadable;
RefPtrWillBeRawPtr<Clipboard> clipboard = createDraggingClipboard(policy, dragData);
clipboard->setSourceOperation(dragData->draggingSourceOperationMask());
mainFrame->eventHandler().cancelDragAndDrop(createMouseEvent(dragData), clipboard.get());
clipboard->setAccessPolicy(ClipboardNumb);
}
mouseMovedIntoDocument(0);
if (m_fileInputElementUnderMouse)
m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
m_fileInputElementUnderMouse = nullptr;
}
DragSession DragController::dragUpdated(DragData* dragData)
{
return dragEnteredOrUpdated(dragData);
}
bool DragController::performDrag(DragData* dragData)
{
ASSERT(dragData);
m_documentUnderMouse = m_page->mainFrame()->documentAtPoint(dragData->clientPosition());
if ((m_dragDestinationAction & DragDestinationActionDHTML) && m_documentIsHandlingDrag) {
RefPtr<LocalFrame> mainFrame = m_page->mainFrame();
bool preventedDefault = false;
if (mainFrame->view()) {
RefPtrWillBeRawPtr<Clipboard> clipboard = createDraggingClipboard(ClipboardReadable, dragData);
clipboard->setSourceOperation(dragData->draggingSourceOperationMask());
preventedDefault = mainFrame->eventHandler().performDragAndDrop(createMouseEvent(dragData), clipboard.get());
clipboard->setAccessPolicy(ClipboardNumb);
}
if (preventedDefault) {
m_documentUnderMouse = nullptr;
return true;
}
}
if ((m_dragDestinationAction & DragDestinationActionEdit) && concludeEditDrag(dragData)) {
m_documentUnderMouse = nullptr;
return true;
}
m_documentUnderMouse = nullptr;
if (operationForLoad(dragData) == DragOperationNone)
return false;
if (m_page->settings().navigateOnDragDrop())
m_page->mainFrame()->loader().load(FrameLoadRequest(0, ResourceRequest(dragData->asURL())));
return true;
}
void DragController::mouseMovedIntoDocument(Document* newDocument)
{
if (m_documentUnderMouse == newDocument)
return;
if (m_documentUnderMouse)
cancelDrag();
m_documentUnderMouse = newDocument;
}
DragSession DragController::dragEnteredOrUpdated(DragData* dragData)
{
ASSERT(dragData);
ASSERT(m_page->mainFrame());
mouseMovedIntoDocument(m_page->mainFrame()->documentAtPoint(dragData->clientPosition()));
m_dragDestinationAction = m_client->actionMaskForDrag(dragData);
if (m_dragDestinationAction == DragDestinationActionNone) {
cancelDrag();
return DragSession();
}
DragSession dragSession;
m_documentIsHandlingDrag = tryDocumentDrag(dragData, m_dragDestinationAction, dragSession);
if (!m_documentIsHandlingDrag && (m_dragDestinationAction & DragDestinationActionLoad))
dragSession.operation = operationForLoad(dragData);
return dragSession;
}
static HTMLInputElement* asFileInput(Node* node)
{
ASSERT(node);
for (; node; node = node->shadowHost()) {
if (isHTMLInputElement(*node) && toHTMLInputElement(node)->isFileUpload())
return toHTMLInputElement(node);
}
return 0;
}
static Element* elementUnderMouse(Document* documentUnderMouse, const IntPoint& p)
{
LocalFrame* frame = documentUnderMouse->frame();
float zoomFactor = frame ? frame->pageZoomFactor() : 1;
LayoutPoint point = roundedLayoutPoint(FloatPoint(p.x() * zoomFactor, p.y() * zoomFactor));
HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent);
HitTestResult result(point);
documentUnderMouse->renderView()->hitTest(request, result);
Node* n = result.innerNode();
while (n && !n->isElementNode())
n = n->parentOrShadowHostNode();
if (n)
n = n->deprecatedShadowAncestorNode();
return toElement(n);
}
bool DragController::tryDocumentDrag(DragData* dragData, DragDestinationAction actionMask, DragSession& dragSession)
{
ASSERT(dragData);
if (!m_documentUnderMouse)
return false;
if (m_dragInitiator && !m_documentUnderMouse->securityOrigin()->canReceiveDragData(m_dragInitiator->securityOrigin()))
return false;
bool isHandlingDrag = false;
if (actionMask & DragDestinationActionDHTML) {
isHandlingDrag = tryDHTMLDrag(dragData, dragSession.operation);
if (!m_documentUnderMouse)
return false;
}
RefPtr<FrameView> frameView = m_documentUnderMouse->view();
if (!frameView)
return false;
if (isHandlingDrag) {
m_page->dragCaretController().clear();
return true;
}
if ((actionMask & DragDestinationActionEdit) && canProcessDrag(dragData)) {
IntPoint point = frameView->windowToContents(dragData->clientPosition());
Element* element = elementUnderMouse(m_documentUnderMouse.get(), point);
if (!element)
return false;
HTMLInputElement* elementAsFileInput = asFileInput(element);
if (m_fileInputElementUnderMouse != elementAsFileInput) {
if (m_fileInputElementUnderMouse)
m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
m_fileInputElementUnderMouse = elementAsFileInput;
}
if (!m_fileInputElementUnderMouse)
m_page->dragCaretController().setCaretPosition(m_documentUnderMouse->frame()->visiblePositionForPoint(point));
LocalFrame* innerFrame = element->document().frame();
dragSession.operation = dragIsMove(innerFrame->selection(), dragData) ? DragOperationMove : DragOperationCopy;
dragSession.mouseIsOverFileInput = m_fileInputElementUnderMouse;
dragSession.numberOfItemsToBeAccepted = 0;
unsigned numberOfFiles = dragData->numberOfFiles();
if (m_fileInputElementUnderMouse) {
if (m_fileInputElementUnderMouse->isDisabledFormControl())
dragSession.numberOfItemsToBeAccepted = 0;
else if (m_fileInputElementUnderMouse->multiple())
dragSession.numberOfItemsToBeAccepted = numberOfFiles;
else if (numberOfFiles > 1)
dragSession.numberOfItemsToBeAccepted = 0;
else
dragSession.numberOfItemsToBeAccepted = 1;
if (!dragSession.numberOfItemsToBeAccepted)
dragSession.operation = DragOperationNone;
m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(dragSession.numberOfItemsToBeAccepted);
} else {
dragSession.numberOfItemsToBeAccepted = numberOfFiles != 1 ? 0 : 1;
}
return true;
}
m_page->dragCaretController().clear();
if (m_fileInputElementUnderMouse)
m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
m_fileInputElementUnderMouse = nullptr;
return false;
}
DragOperation DragController::operationForLoad(DragData* dragData)
{
ASSERT(dragData);
Document* doc = m_page->mainFrame()->documentAtPoint(dragData->clientPosition());
if (doc && (m_didInitiateDrag || doc->isPluginDocument() || doc->rendererIsEditable()))
return DragOperationNone;
return dragOperation(dragData);
}
static bool setSelectionToDragCaret(LocalFrame* frame, VisibleSelection& dragCaret, RefPtrWillBeRawPtr<Range>& range, const IntPoint& point)
{
frame->selection().setSelection(dragCaret);
if (frame->selection().isNone()) {
dragCaret = VisibleSelection(frame->visiblePositionForPoint(point));
frame->selection().setSelection(dragCaret);
range = dragCaret.toNormalizedRange();
}
return !frame->selection().isNone() && frame->selection().isContentEditable();
}
bool DragController::dispatchTextInputEventFor(LocalFrame* innerFrame, DragData* dragData)
{
ASSERT(m_page->dragCaretController().hasCaret());
String text = m_page->dragCaretController().isContentRichlyEditable() ? "" : dragData->asPlainText();
Node* target = innerFrame->editor().findEventTargetFrom(VisibleSelection(m_page->dragCaretController().caretPosition()));
return target->dispatchEvent(TextEvent::createForDrop(innerFrame->domWindow(), text), IGNORE_EXCEPTION);
}
bool DragController::concludeEditDrag(DragData* dragData)
{
ASSERT(dragData);
RefPtr<HTMLInputElement> fileInput = m_fileInputElementUnderMouse;
if (m_fileInputElementUnderMouse) {
m_fileInputElementUnderMouse->setCanReceiveDroppedFiles(false);
m_fileInputElementUnderMouse = nullptr;
}
if (!m_documentUnderMouse)
return false;
IntPoint point = m_documentUnderMouse->view()->windowToContents(dragData->clientPosition());
Element* element = elementUnderMouse(m_documentUnderMouse.get(), point);
if (!element)
return false;
RefPtr<LocalFrame> innerFrame = element->ownerDocument()->frame();
ASSERT(innerFrame);
if (m_page->dragCaretController().hasCaret() && !dispatchTextInputEventFor(innerFrame.get(), dragData))
return true;
if (dragData->containsFiles() && fileInput) {
ASSERT(fileInput == element || !fileInput->renderer());
if (fileInput->isDisabledFormControl())
return false;
return fileInput->receiveDroppedFiles(dragData);
}
if (!m_page->dragController().canProcessDrag(dragData)) {
m_page->dragCaretController().clear();
return false;
}
VisibleSelection dragCaret(m_page->dragCaretController().caretPosition());
m_page->dragCaretController().clear();
RefPtrWillBeRawPtr<Range> range = dragCaret.toNormalizedRange();
RefPtr<Element> rootEditableElement = innerFrame->selection().rootEditableElement();
if (!range)
return false;
ResourceFetcher* fetcher = range->ownerDocument().fetcher();
ResourceCacheValidationSuppressor validationSuppressor(fetcher);
if (dragIsMove(innerFrame->selection(), dragData) || dragCaret.isContentRichlyEditable()) {
bool chosePlainText = false;
RefPtr<DocumentFragment> fragment = documentFragmentFromDragData(dragData, innerFrame.get(), range, true, chosePlainText);
if (!fragment)
return false;
if (dragIsMove(innerFrame->selection(), dragData)) {
bool smartDelete = innerFrame->editor().smartInsertDeleteEnabled();
bool smartInsert = smartDelete && innerFrame->selection().granularity() == WordGranularity && dragData->canSmartReplace();
MoveSelectionCommand::create(fragment, dragCaret.base(), smartInsert, smartDelete)->apply();
} else {
if (setSelectionToDragCaret(innerFrame.get(), dragCaret, range, point)) {
ReplaceSelectionCommand::CommandOptions options = ReplaceSelectionCommand::SelectReplacement | ReplaceSelectionCommand::PreventNesting;
if (dragData->canSmartReplace())
options |= ReplaceSelectionCommand::SmartReplace;
if (chosePlainText)
options |= ReplaceSelectionCommand::MatchStyle;
ASSERT(m_documentUnderMouse);
ReplaceSelectionCommand::create(*m_documentUnderMouse.get(), fragment, options)->apply();
}
}
} else {
String text = dragData->asPlainText();
if (text.isEmpty())
return false;
if (setSelectionToDragCaret(innerFrame.get(), dragCaret, range, point)) {
ASSERT(m_documentUnderMouse);
ReplaceSelectionCommand::create(*m_documentUnderMouse.get(), createFragmentFromText(range.get(), text), ReplaceSelectionCommand::SelectReplacement | ReplaceSelectionCommand::MatchStyle | ReplaceSelectionCommand::PreventNesting)->apply();
}
}
if (rootEditableElement) {
if (LocalFrame* frame = rootEditableElement->document().frame())
frame->eventHandler().updateDragStateAfterEditDragIfNeeded(rootEditableElement.get());
}
return true;
}
bool DragController::canProcessDrag(DragData* dragData)
{
ASSERT(dragData);
if (!dragData->containsCompatibleContent())
return false;
IntPoint point = m_page->mainFrame()->view()->windowToContents(dragData->clientPosition());
HitTestResult result = HitTestResult(point);
if (!m_page->mainFrame()->contentRenderer())
return false;
result = m_page->mainFrame()->eventHandler().hitTestResultAtPoint(point);
if (!result.innerNonSharedNode())
return false;
if (dragData->containsFiles() && asFileInput(result.innerNonSharedNode()))
return true;
if (isHTMLPlugInElement(*result.innerNonSharedNode())) {
HTMLPlugInElement* plugin = toHTMLPlugInElement(result.innerNonSharedNode());
if (!plugin->canProcessDrag() && !result.innerNonSharedNode()->rendererIsEditable())
return false;
} else if (!result.innerNonSharedNode()->rendererIsEditable())
return false;
if (m_didInitiateDrag && m_documentUnderMouse == m_dragInitiator && result.isSelected())
return false;
return true;
}
static DragOperation defaultOperationForDrag(DragOperation srcOpMask)
{
if (srcOpMask == DragOperationEvery)
return DragOperationCopy;
if (srcOpMask == DragOperationNone)
return DragOperationNone;
if (srcOpMask & DragOperationMove || srcOpMask & DragOperationGeneric)
return DragOperationMove;
if (srcOpMask & DragOperationCopy)
return DragOperationCopy;
if (srcOpMask & DragOperationLink)
return DragOperationLink;
return DragOperationGeneric;
}
bool DragController::tryDHTMLDrag(DragData* dragData, DragOperation& operation)
{
ASSERT(dragData);
ASSERT(m_documentUnderMouse);
RefPtr<LocalFrame> mainFrame = m_page->mainFrame();
RefPtr<FrameView> viewProtector = mainFrame->view();
if (!viewProtector)
return false;
ClipboardAccessPolicy policy = m_documentUnderMouse->securityOrigin()->isLocal() ? ClipboardReadable : ClipboardTypesReadable;
RefPtrWillBeRawPtr<Clipboard> clipboard = createDraggingClipboard(policy, dragData);
DragOperation srcOpMask = dragData->draggingSourceOperationMask();
clipboard->setSourceOperation(srcOpMask);
PlatformMouseEvent event = createMouseEvent(dragData);
if (!mainFrame->eventHandler().updateDragAndDrop(event, clipboard.get())) {
clipboard->setAccessPolicy(ClipboardNumb);
return false;
}
operation = clipboard->destinationOperation();
if (clipboard->dropEffectIsUninitialized())
operation = defaultOperationForDrag(srcOpMask);
else if (!(srcOpMask & operation)) {
operation = DragOperationNone;
}
clipboard->setAccessPolicy(ClipboardNumb);
return true;
}
Node* DragController::draggableNode(const LocalFrame* src, Node* startNode, const IntPoint& dragOrigin, SelectionDragPolicy selectionDragPolicy, DragSourceAction& dragType) const
{
if (src->selection().contains(dragOrigin)) {
dragType = DragSourceActionSelection;
if (selectionDragPolicy == ImmediateSelectionDragResolution)
return startNode;
} else {
dragType = DragSourceActionNone;
}
Node* node = 0;
DragSourceAction candidateDragType = DragSourceActionNone;
for (const RenderObject* renderer = startNode->renderer(); renderer; renderer = renderer->parent()) {
node = renderer->nonPseudoNode();
if (!node) {
continue;
}
if (dragType != DragSourceActionSelection && node->isTextNode() && node->canStartSelection()) {
return 0;
}
if (node->isElementNode()) {
EUserDrag dragMode = renderer->style()->userDrag();
if (dragMode == DRAG_NONE)
continue;
if (renderer->isImage()
&& src->settings()
&& src->settings()->loadsImagesAutomatically()) {
dragType = DragSourceActionImage;
return node;
}
if (isHTMLAnchorElement(*node) && toHTMLAnchorElement(node)->isLiveLink()) {
candidateDragType = DragSourceActionLink;
break;
}
if (dragMode == DRAG_ELEMENT) {
candidateDragType = DragSourceActionDHTML;
break;
}
}
}
if (candidateDragType == DragSourceActionNone) {
return 0;
}
ASSERT(node);
if (dragType == DragSourceActionSelection) {
ASSERT(selectionDragPolicy == DelayedSelectionDragResolution);
node = startNode;
} else {
ASSERT(dragType == DragSourceActionNone);
dragType = candidateDragType;
}
return node;
}
static ImageResource* getImageResource(Element* element)
{
ASSERT(element);
RenderObject* renderer = element->renderer();
if (!renderer || !renderer->isImage())
return 0;
RenderImage* image = toRenderImage(renderer);
return image->cachedImage();
}
static Image* getImage(Element* element)
{
ASSERT(element);
ImageResource* cachedImage = getImageResource(element);
return (cachedImage && !cachedImage->errorOccurred()) ?
cachedImage->image() : 0;
}
static void prepareClipboardForImageDrag(LocalFrame* source, Clipboard* clipboard, Element* node, const KURL& linkURL, const KURL& imageURL, const String& label)
{
if (node->isContentRichlyEditable()) {
RefPtrWillBeRawPtr<Range> range = source->document()->createRange();
range->selectNode(node, ASSERT_NO_EXCEPTION);
source->selection().setSelection(VisibleSelection(range.get(), DOWNSTREAM));
}
clipboard->declareAndWriteDragImage(node, !linkURL.isEmpty() ? linkURL : imageURL, label);
}
bool DragController::populateDragClipboard(LocalFrame* src, const DragState& state, const IntPoint& dragOrigin)
{
ASSERT(dragTypeIsValid(state.m_dragType));
ASSERT(src);
if (!src->view() || !src->contentRenderer())
return false;
HitTestResult hitTestResult = src->eventHandler().hitTestResultAtPoint(dragOrigin);
if (!state.m_dragSrc->contains(hitTestResult.innerNode())) {
return false;
}
const KURL& linkURL = hitTestResult.absoluteLinkURL();
const KURL& imageURL = hitTestResult.absoluteImageURL();
Clipboard* clipboard = state.m_dragClipboard.get();
Node* node = state.m_dragSrc.get();
if (state.m_dragType == DragSourceActionSelection) {
if (enclosingTextFormControl(src->selection().start())) {
clipboard->writePlainText(src->selectedTextForClipboard());
} else {
RefPtrWillBeRawPtr<Range> selectionRange = src->selection().toNormalizedRange();
ASSERT(selectionRange);
clipboard->writeRange(selectionRange.get(), src);
}
} else if (state.m_dragType == DragSourceActionImage) {
if (imageURL.isEmpty() || !node || !node->isElementNode())
return false;
Element* element = toElement(node);
prepareClipboardForImageDrag(src, clipboard, element, linkURL, imageURL, hitTestResult.altDisplayString());
} else if (state.m_dragType == DragSourceActionLink) {
if (linkURL.isEmpty())
return false;
clipboard->writeURL(linkURL, hitTestResult.textContent().simplifyWhiteSpace());
}
return true;
}
static IntPoint dragLocationForDHTMLDrag(const IntPoint& mouseDraggedPoint, const IntPoint& dragOrigin, const IntPoint& dragImageOffset, bool isLinkImage)
{
const int yOffset = -dragImageOffset.y();
if (isLinkImage)
return IntPoint(mouseDraggedPoint.x() - dragImageOffset.x(), mouseDraggedPoint.y() + yOffset);
return IntPoint(dragOrigin.x() - dragImageOffset.x(), dragOrigin.y() + yOffset);
}
static IntPoint dragLocationForSelectionDrag(LocalFrame* sourceFrame)
{
IntRect draggingRect = enclosingIntRect(sourceFrame->selection().bounds());
int xpos = draggingRect.maxX();
xpos = draggingRect.x() < xpos ? draggingRect.x() : xpos;
int ypos = draggingRect.maxY();
ypos = draggingRect.y() < ypos ? draggingRect.y() : ypos;
return IntPoint(xpos, ypos);
}
static const IntSize& maxDragImageSize()
{
#if OS(MACOSX)
static const IntSize maxDragImageSize(400, 400);
#else
static const IntSize maxDragImageSize(200, 200);
#endif
return maxDragImageSize;
}
static PassOwnPtr<DragImage> dragImageForImage(Element* element, Image* image, const IntPoint& dragOrigin, const IntRect& imageRect, IntPoint& dragLocation)
{
OwnPtr<DragImage> dragImage;
IntPoint origin;
if (image->size().height() * image->size().width() <= MaxOriginalImageArea
&& (dragImage = DragImage::create(image, element->renderer() ? element->renderer()->shouldRespectImageOrientation() : DoNotRespectImageOrientation))) {
IntSize originalSize = imageRect.size();
origin = imageRect.location();
dragImage->fitToMaxSize(imageRect.size(), maxDragImageSize());
dragImage->dissolveToFraction(DragImageAlpha);
IntSize newSize = dragImage->size();
float scale = newSize.width() / (float)originalSize.width();
float dx = origin.x() - dragOrigin.x();
dx *= scale;
origin.setX((int)(dx + 0.5));
float dy = origin.y() - dragOrigin.y();
dy *= scale;
origin.setY((int)(dy + 0.5));
}
dragLocation = dragOrigin + origin;
return dragImage.release();
}
static PassOwnPtr<DragImage> dragImageForLink(const KURL& linkURL, const String& linkText, float deviceScaleFactor, const IntPoint& mouseDraggedPoint, IntPoint& dragLoc)
{
FontDescription fontDescription;
RenderTheme::theme().systemFont(WebCore::CSSValueNone, fontDescription);
OwnPtr<DragImage> dragImage = DragImage::create(linkURL, linkText, fontDescription, deviceScaleFactor);
IntSize size = dragImage ? dragImage->size() : IntSize();
IntPoint dragImageOffset(-size.width() / 2, -LinkDragBorderInset);
dragLoc = IntPoint(mouseDraggedPoint.x() + dragImageOffset.x(), mouseDraggedPoint.y() + dragImageOffset.y());
return dragImage.release();
}
bool DragController::startDrag(LocalFrame* src, const DragState& state, const PlatformMouseEvent& dragEvent, const IntPoint& dragOrigin)
{
ASSERT(dragTypeIsValid(state.m_dragType));
ASSERT(src);
if (!src->view() || !src->contentRenderer())
return false;
HitTestResult hitTestResult = src->eventHandler().hitTestResultAtPoint(dragOrigin);
if (!state.m_dragSrc->contains(hitTestResult.innerNode())) {
return false;
}
const KURL& linkURL = hitTestResult.absoluteLinkURL();
const KURL& imageURL = hitTestResult.absoluteImageURL();
IntPoint mouseDraggedPoint = src->view()->windowToContents(dragEvent.position());
IntPoint dragLocation;
IntPoint dragOffset;
Clipboard* clipboard = state.m_dragClipboard.get();
OwnPtr<DragImage> dragImage = clipboard->createDragImage(dragOffset, src);
if (dragImage) {
dragLocation = dragLocationForDHTMLDrag(mouseDraggedPoint, dragOrigin, dragOffset, !linkURL.isEmpty());
}
Node* node = state.m_dragSrc.get();
if (state.m_dragType == DragSourceActionSelection) {
if (!dragImage) {
dragImage = src->dragImageForSelection();
if (dragImage)
dragImage->dissolveToFraction(DragImageAlpha);
dragLocation = dragLocationForSelectionDrag(src);
}
doSystemDrag(dragImage.get(), dragLocation, dragOrigin, clipboard, src, false);
} else if (state.m_dragType == DragSourceActionImage) {
if (imageURL.isEmpty() || !node || !node->isElementNode())
return false;
Element* element = toElement(node);
Image* image = getImage(element);
if (!image || image->isNull())
return false;
ASSERT(!image->filenameExtension().isEmpty());
if (!dragImage) {
dragImage = dragImageForImage(element, image, dragOrigin, hitTestResult.imageRect(), dragLocation);
}
doSystemDrag(dragImage.get(), dragLocation, dragOrigin, clipboard, src, false);
} else if (state.m_dragType == DragSourceActionLink) {
if (linkURL.isEmpty())
return false;
if (src->selection().isCaret() && src->selection().isContentEditable()) {
if (Node* node = enclosingAnchorElement(src->selection().base()))
src->selection().setSelection(VisibleSelection::selectionFromContentsOfNode(node));
}
if (!dragImage) {
ASSERT(src->page());
float deviceScaleFactor = src->page()->deviceScaleFactor();
dragImage = dragImageForLink(linkURL, hitTestResult.textContent(), deviceScaleFactor, mouseDraggedPoint, dragLocation);
}
doSystemDrag(dragImage.get(), dragLocation, mouseDraggedPoint, clipboard, src, true);
} else if (state.m_dragType == DragSourceActionDHTML) {
if (!dragImage)
return false;
doSystemDrag(dragImage.get(), dragLocation, dragOrigin, clipboard, src, false);
} else {
ASSERT_NOT_REACHED();
return false;
}
return true;
}
void DragController::doSystemDrag(DragImage* image, const IntPoint& dragLocation, const IntPoint& eventPos, Clipboard* clipboard, LocalFrame* frame, bool forLink)
{
m_didInitiateDrag = true;
m_dragInitiator = frame->document();
RefPtr<LocalFrame> frameProtector = m_page->mainFrame();
RefPtr<FrameView> viewProtector = frameProtector->view();
m_client->startDrag(image, viewProtector->rootViewToContents(frame->view()->contentsToRootView(dragLocation)),
viewProtector->rootViewToContents(frame->view()->contentsToRootView(eventPos)), clipboard, frameProtector.get(), forLink);
if (!frameProtector->page())
return;
cleanupAfterSystemDrag();
}
DragOperation DragController::dragOperation(DragData* dragData)
{
ASSERT(dragData);
return dragData->containsURL() && !m_didInitiateDrag ? DragOperationCopy : DragOperationNone;
}
bool DragController::isCopyKeyDown(DragData* dragData)
{
int keyState = dragData->modifierKeyState();
#if OS(MACOSX)
return keyState & PlatformEvent::AltKey;
#else
return keyState & PlatformEvent::CtrlKey;
#endif
}
void DragController::cleanupAfterSystemDrag()
{
}
}