Bug 1692971 [wpt PR 27638] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / widget / nsBaseDragService.cpp
blobceecc1a74b623478a15d5c9d40d177d4e7d0ce28
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsBaseDragService.h"
7 #include "nsITransferable.h"
9 #include "nsArrayUtils.h"
10 #include "nsITransferable.h"
11 #include "nsSize.h"
12 #include "nsXPCOM.h"
13 #include "nsCOMPtr.h"
14 #include "nsIInterfaceRequestorUtils.h"
15 #include "nsIFrame.h"
16 #include "nsFrameLoaderOwner.h"
17 #include "nsIContent.h"
18 #include "nsViewManager.h"
19 #include "nsINode.h"
20 #include "nsPresContext.h"
21 #include "nsIImageLoadingContent.h"
22 #include "imgIContainer.h"
23 #include "imgIRequest.h"
24 #include "ImageRegion.h"
25 #include "nsQueryObject.h"
26 #include "nsRegion.h"
27 #include "nsXULPopupManager.h"
28 #include "nsMenuPopupFrame.h"
29 #ifdef MOZ_XUL
30 # include "nsTreeBodyFrame.h"
31 #endif
32 #include "mozilla/MouseEvents.h"
33 #include "mozilla/Preferences.h"
34 #include "mozilla/PresShell.h"
35 #include "mozilla/ProfilerLabels.h"
36 #include "mozilla/SVGImageContext.h"
37 #include "mozilla/Unused.h"
38 #include "mozilla/ViewportUtils.h"
39 #include "mozilla/dom/BindingDeclarations.h"
40 #include "mozilla/dom/DataTransferItemList.h"
41 #include "mozilla/dom/DataTransfer.h"
42 #include "mozilla/dom/Document.h"
43 #include "mozilla/dom/DocumentInlines.h"
44 #include "mozilla/dom/DragEvent.h"
45 #include "mozilla/dom/MouseEventBinding.h"
46 #include "mozilla/dom/Selection.h"
47 #include "mozilla/gfx/2D.h"
48 #include "nsFrameLoader.h"
49 #include "BrowserParent.h"
50 #include "nsIMutableArray.h"
51 #include "gfxContext.h"
52 #include "gfxPlatform.h"
53 #include <algorithm>
55 using namespace mozilla;
56 using namespace mozilla::dom;
57 using namespace mozilla::gfx;
58 using namespace mozilla::image;
60 #define DRAGIMAGES_PREF "nglayout.enable_drag_images"
62 nsBaseDragService::nsBaseDragService()
63 : mCanDrop(false),
64 mOnlyChromeDrop(false),
65 mDoingDrag(false),
66 mSessionIsSynthesizedForTests(false),
67 mEndingSession(false),
68 mHasImage(false),
69 mUserCancelled(false),
70 mDragEventDispatchedToChildProcess(false),
71 mDragAction(DRAGDROP_ACTION_NONE),
72 mDragActionFromChildProcess(DRAGDROP_ACTION_UNINITIALIZED),
73 mEffectAllowedForTests(DRAGDROP_ACTION_UNINITIALIZED),
74 mContentPolicyType(nsIContentPolicy::TYPE_OTHER),
75 mSuppressLevel(0),
76 mInputSource(MouseEvent_Binding::MOZ_SOURCE_MOUSE) {}
78 nsBaseDragService::~nsBaseDragService() = default;
80 NS_IMPL_ISUPPORTS(nsBaseDragService, nsIDragService, nsIDragSession)
82 //---------------------------------------------------------
83 NS_IMETHODIMP
84 nsBaseDragService::SetCanDrop(bool aCanDrop) {
85 mCanDrop = aCanDrop;
86 return NS_OK;
89 //---------------------------------------------------------
90 NS_IMETHODIMP
91 nsBaseDragService::GetCanDrop(bool* aCanDrop) {
92 *aCanDrop = mCanDrop;
93 return NS_OK;
95 //---------------------------------------------------------
96 NS_IMETHODIMP
97 nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome) {
98 mOnlyChromeDrop = aOnlyChrome;
99 return NS_OK;
102 //---------------------------------------------------------
103 NS_IMETHODIMP
104 nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome) {
105 *aOnlyChrome = mOnlyChromeDrop;
106 return NS_OK;
109 //---------------------------------------------------------
110 NS_IMETHODIMP
111 nsBaseDragService::SetDragAction(uint32_t anAction) {
112 mDragAction = anAction;
113 return NS_OK;
116 //---------------------------------------------------------
117 NS_IMETHODIMP
118 nsBaseDragService::GetDragAction(uint32_t* anAction) {
119 *anAction = mDragAction;
120 return NS_OK;
123 //-------------------------------------------------------------------------
125 NS_IMETHODIMP
126 nsBaseDragService::GetNumDropItems(uint32_t* aNumItems) {
127 *aNumItems = 0;
128 return NS_ERROR_FAILURE;
132 // GetSourceDocument
134 // Returns the DOM document where the drag was initiated. This will be
135 // nullptr if the drag began outside of our application.
137 NS_IMETHODIMP
138 nsBaseDragService::GetSourceDocument(Document** aSourceDocument) {
139 *aSourceDocument = mSourceDocument.get();
140 NS_IF_ADDREF(*aSourceDocument);
142 return NS_OK;
146 // GetSourceNode
148 // Returns the DOM node where the drag was initiated. This will be
149 // nullptr if the drag began outside of our application.
151 NS_IMETHODIMP
152 nsBaseDragService::GetSourceNode(nsINode** aSourceNode) {
153 *aSourceNode = do_AddRef(mSourceNode).take();
154 return NS_OK;
157 NS_IMETHODIMP
158 nsBaseDragService::GetTriggeringPrincipal(nsIPrincipal** aPrincipal) {
159 NS_IF_ADDREF(*aPrincipal = mTriggeringPrincipal);
160 return NS_OK;
163 NS_IMETHODIMP
164 nsBaseDragService::SetTriggeringPrincipal(nsIPrincipal* aPrincipal) {
165 mTriggeringPrincipal = aPrincipal;
166 return NS_OK;
169 NS_IMETHODIMP
170 nsBaseDragService::GetCsp(nsIContentSecurityPolicy** aCsp) {
171 NS_IF_ADDREF(*aCsp = mCsp);
172 return NS_OK;
175 NS_IMETHODIMP
176 nsBaseDragService::SetCsp(nsIContentSecurityPolicy* aCsp) {
177 mCsp = aCsp;
178 return NS_OK;
181 //-------------------------------------------------------------------------
183 NS_IMETHODIMP
184 nsBaseDragService::GetData(nsITransferable* aTransferable,
185 uint32_t aItemIndex) {
186 return NS_ERROR_FAILURE;
189 //-------------------------------------------------------------------------
190 NS_IMETHODIMP
191 nsBaseDragService::IsDataFlavorSupported(const char* aDataFlavor,
192 bool* _retval) {
193 return NS_ERROR_FAILURE;
196 NS_IMETHODIMP
197 nsBaseDragService::GetDataTransferXPCOM(DataTransfer** aDataTransfer) {
198 *aDataTransfer = mDataTransfer;
199 NS_IF_ADDREF(*aDataTransfer);
200 return NS_OK;
203 NS_IMETHODIMP
204 nsBaseDragService::SetDataTransferXPCOM(DataTransfer* aDataTransfer) {
205 NS_ENSURE_STATE(aDataTransfer);
206 mDataTransfer = aDataTransfer;
207 return NS_OK;
210 DataTransfer* nsBaseDragService::GetDataTransfer() { return mDataTransfer; }
212 void nsBaseDragService::SetDataTransfer(DataTransfer* aDataTransfer) {
213 mDataTransfer = aDataTransfer;
216 bool nsBaseDragService::IsSynthesizedForTests() {
217 return mSessionIsSynthesizedForTests;
220 uint32_t nsBaseDragService::GetEffectAllowedForTests() {
221 MOZ_ASSERT(mSessionIsSynthesizedForTests);
222 return mEffectAllowedForTests;
225 NS_IMETHODIMP nsBaseDragService::SetDragEndPointForTests(int32_t aScreenX,
226 int32_t aScreenY) {
227 MOZ_ASSERT(mDoingDrag);
228 MOZ_ASSERT(mSourceDocument);
229 MOZ_ASSERT(mSessionIsSynthesizedForTests);
230 if (!mDoingDrag || !mSourceDocument || !mSessionIsSynthesizedForTests) {
231 return NS_ERROR_FAILURE;
233 nsPresContext* presContext = mSourceDocument->GetPresContext();
234 if (NS_WARN_IF(!presContext)) {
235 return NS_ERROR_FAILURE;
237 SetDragEndPoint(
238 LayoutDeviceIntPoint(presContext->CSSPixelsToDevPixels(aScreenX),
239 presContext->CSSPixelsToDevPixels(aScreenY)));
240 return NS_OK;
243 //-------------------------------------------------------------------------
244 NS_IMETHODIMP
245 nsBaseDragService::InvokeDragSession(
246 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCsp,
247 nsICookieJarSettings* aCookieJarSettings, nsIArray* aTransferableArray,
248 uint32_t aActionType,
249 nsContentPolicyType aContentPolicyType = nsIContentPolicy::TYPE_OTHER) {
250 AUTO_PROFILER_LABEL("nsBaseDragService::InvokeDragSession", OTHER);
252 NS_ENSURE_TRUE(aDOMNode, NS_ERROR_INVALID_ARG);
253 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
255 // stash the document of the dom node
256 mSourceDocument = aDOMNode->OwnerDoc();
257 mTriggeringPrincipal = aPrincipal;
258 mCsp = aCsp;
259 mSourceNode = aDOMNode;
260 mContentPolicyType = aContentPolicyType;
261 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
263 // When the mouse goes down, the selection code starts a mouse
264 // capture. However, this gets in the way of determining drag
265 // feedback for things like trees because the event coordinates
266 // are in the wrong coord system, so turn off mouse capture.
267 PresShell::ClearMouseCapture(nullptr);
269 if (mSessionIsSynthesizedForTests) {
270 mDoingDrag = true;
271 mDragAction = aActionType;
272 mEffectAllowedForTests = aActionType;
273 return NS_OK;
276 // If you're hitting this, a test is causing the browser to attempt to enter
277 // the drag-drop native nested event loop, which will put the browser in a
278 // state that won't run tests properly until there's manual intervention
279 // to exit the drag-drop loop (either by moving the mouse or hitting escape),
280 // which can't be done from script since we're in the nested loop.
282 // The best way to avoid this is to catch the dragstart event on the item
283 // being dragged, and then to call preventDefault() and stopPropagating() on
284 // it.
285 if (XRE_IsParentProcess()) {
286 MOZ_ASSERT(
287 !xpc::IsInAutomation(),
288 "About to start drag-drop native loop on which will prevent later "
289 "tests from running properly.");
292 uint32_t length = 0;
293 mozilla::Unused << aTransferableArray->GetLength(&length);
294 if (!length) {
295 nsCOMPtr<nsIMutableArray> mutableArray =
296 do_QueryInterface(aTransferableArray);
297 if (mutableArray) {
298 // In order to be able trigger dnd, we need to have some transferable
299 // object.
300 nsCOMPtr<nsITransferable> trans =
301 do_CreateInstance("@mozilla.org/widget/transferable;1");
302 trans->Init(nullptr);
303 trans->SetRequestingPrincipal(mSourceNode->NodePrincipal());
304 trans->SetContentPolicyType(mContentPolicyType);
305 trans->SetCookieJarSettings(aCookieJarSettings);
306 mutableArray->AppendElement(trans);
308 } else {
309 for (uint32_t i = 0; i < length; ++i) {
310 nsCOMPtr<nsITransferable> trans =
311 do_QueryElementAt(aTransferableArray, i);
312 if (trans) {
313 // Set the requestingPrincipal on the transferable.
314 trans->SetRequestingPrincipal(mSourceNode->NodePrincipal());
315 trans->SetContentPolicyType(mContentPolicyType);
316 trans->SetCookieJarSettings(aCookieJarSettings);
321 nsresult rv = InvokeDragSessionImpl(aTransferableArray, mRegion, aActionType);
323 if (NS_FAILED(rv)) {
324 // Set mDoingDrag so that EndDragSession cleans up and sends the dragend
325 // event after the aborted drag.
326 mDoingDrag = true;
327 EndDragSession(true, 0);
330 return rv;
333 NS_IMETHODIMP
334 nsBaseDragService::InvokeDragSessionWithImage(
335 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCsp,
336 nsICookieJarSettings* aCookieJarSettings, nsIArray* aTransferableArray,
337 uint32_t aActionType, nsINode* aImage, int32_t aImageX, int32_t aImageY,
338 DragEvent* aDragEvent, DataTransfer* aDataTransfer) {
339 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
340 NS_ENSURE_TRUE(aDataTransfer, NS_ERROR_NULL_POINTER);
341 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
343 mSessionIsSynthesizedForTests =
344 aDragEvent->WidgetEventPtr()->mFlags.mIsSynthesizedForTests;
345 mDataTransfer = aDataTransfer;
346 mSelection = nullptr;
347 mHasImage = true;
348 mDragPopup = nullptr;
349 mImage = aImage;
350 mImageOffset = CSSIntPoint(aImageX, aImageY);
351 mDragStartData = nullptr;
353 mScreenPosition.x = aDragEvent->ScreenX(CallerType::System);
354 mScreenPosition.y = aDragEvent->ScreenY(CallerType::System);
355 mInputSource = aDragEvent->MozInputSource();
357 // If dragging within a XUL tree and no custom drag image was
358 // set, the region argument to InvokeDragSessionWithImage needs
359 // to be set to the area encompassing the selected rows of the
360 // tree to ensure that the drag feedback gets clipped to those
361 // rows. For other content, region should be null.
362 mRegion = Nothing();
363 #ifdef MOZ_XUL
364 if (aDOMNode && aDOMNode->IsContent() && !aImage) {
365 if (aDOMNode->NodeInfo()->Equals(nsGkAtoms::treechildren,
366 kNameSpaceID_XUL)) {
367 nsTreeBodyFrame* treeBody =
368 do_QueryFrame(aDOMNode->AsContent()->GetPrimaryFrame());
369 if (treeBody) {
370 mRegion = treeBody->GetSelectionRegion();
374 #endif
376 nsresult rv = InvokeDragSession(
377 aDOMNode, aPrincipal, aCsp, aCookieJarSettings, aTransferableArray,
378 aActionType, nsIContentPolicy::TYPE_INTERNAL_IMAGE);
379 mRegion = Nothing();
380 return rv;
383 NS_IMETHODIMP
384 nsBaseDragService::InvokeDragSessionWithRemoteImage(
385 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCsp,
386 nsICookieJarSettings* aCookieJarSettings, nsIArray* aTransferableArray,
387 uint32_t aActionType, RemoteDragStartData* aDragStartData,
388 DragEvent* aDragEvent, DataTransfer* aDataTransfer) {
389 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
390 NS_ENSURE_TRUE(aDataTransfer, NS_ERROR_NULL_POINTER);
391 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
393 mSessionIsSynthesizedForTests =
394 aDragEvent->WidgetEventPtr()->mFlags.mIsSynthesizedForTests;
395 mDataTransfer = aDataTransfer;
396 mSelection = nullptr;
397 mHasImage = true;
398 mDragPopup = nullptr;
399 mImage = nullptr;
400 mDragStartData = aDragStartData;
401 mImageOffset = CSSIntPoint(0, 0);
403 mScreenPosition.x = aDragEvent->ScreenX(CallerType::System);
404 mScreenPosition.y = aDragEvent->ScreenY(CallerType::System);
405 mInputSource = aDragEvent->MozInputSource();
407 nsresult rv = InvokeDragSession(
408 aDOMNode, aPrincipal, aCsp, aCookieJarSettings, aTransferableArray,
409 aActionType, nsIContentPolicy::TYPE_INTERNAL_IMAGE);
410 mRegion = Nothing();
411 return rv;
414 NS_IMETHODIMP
415 nsBaseDragService::InvokeDragSessionWithSelection(
416 Selection* aSelection, nsIPrincipal* aPrincipal,
417 nsIContentSecurityPolicy* aCsp, nsICookieJarSettings* aCookieJarSettings,
418 nsIArray* aTransferableArray, uint32_t aActionType, DragEvent* aDragEvent,
419 DataTransfer* aDataTransfer) {
420 NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
421 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
422 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
424 mSessionIsSynthesizedForTests =
425 aDragEvent->WidgetEventPtr()->mFlags.mIsSynthesizedForTests;
426 mDataTransfer = aDataTransfer;
427 mSelection = aSelection;
428 mHasImage = true;
429 mDragPopup = nullptr;
430 mImage = nullptr;
431 mImageOffset = CSSIntPoint();
432 mDragStartData = nullptr;
433 mRegion = Nothing();
435 mScreenPosition.x = aDragEvent->ScreenX(CallerType::System);
436 mScreenPosition.y = aDragEvent->ScreenY(CallerType::System);
437 mInputSource = aDragEvent->MozInputSource();
439 // just get the focused node from the selection
440 // XXXndeakin this should actually be the deepest node that contains both
441 // endpoints of the selection
442 nsCOMPtr<nsINode> node = aSelection->GetFocusNode();
444 return InvokeDragSession(node, aPrincipal, aCsp, aCookieJarSettings,
445 aTransferableArray, aActionType,
446 nsIContentPolicy::TYPE_OTHER);
449 //-------------------------------------------------------------------------
450 NS_IMETHODIMP
451 nsBaseDragService::GetCurrentSession(nsIDragSession** aSession) {
452 if (!aSession) return NS_ERROR_INVALID_ARG;
454 // "this" also implements a drag session, so say we are one but only
455 // if there is currently a drag going on.
456 if (!mSuppressLevel && mDoingDrag) {
457 *aSession = this;
458 NS_ADDREF(*aSession); // addRef because we're a "getter"
459 } else
460 *aSession = nullptr;
462 return NS_OK;
465 //-------------------------------------------------------------------------
466 NS_IMETHODIMP
467 nsBaseDragService::StartDragSession() {
468 if (mDoingDrag) {
469 return NS_ERROR_FAILURE;
471 mDoingDrag = true;
472 // By default dispatch drop also to content.
473 mOnlyChromeDrop = false;
475 return NS_OK;
478 NS_IMETHODIMP nsBaseDragService::StartDragSessionForTests(
479 uint32_t aAllowedEffect) {
480 if (NS_WARN_IF(NS_FAILED(StartDragSession()))) {
481 return NS_ERROR_FAILURE;
483 mDragAction = aAllowedEffect;
484 mEffectAllowedForTests = aAllowedEffect;
485 mSessionIsSynthesizedForTests = true;
486 return NS_OK;
489 void nsBaseDragService::OpenDragPopup() {
490 if (mDragPopup) {
491 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
492 if (pm) {
493 pm->ShowPopupAtScreen(mDragPopup, mScreenPosition.x - mImageOffset.x,
494 mScreenPosition.y - mImageOffset.y, false, nullptr);
499 int32_t nsBaseDragService::TakeChildProcessDragAction() {
500 // If the last event was dispatched to the child process, use the drag action
501 // assigned from it instead and return it. DRAGDROP_ACTION_UNINITIALIZED is
502 // returned otherwise.
503 int32_t retval = DRAGDROP_ACTION_UNINITIALIZED;
504 if (TakeDragEventDispatchedToChildProcess() &&
505 mDragActionFromChildProcess != DRAGDROP_ACTION_UNINITIALIZED) {
506 retval = mDragActionFromChildProcess;
509 return retval;
512 //-------------------------------------------------------------------------
513 NS_IMETHODIMP
514 nsBaseDragService::EndDragSession(bool aDoneDrag, uint32_t aKeyModifiers) {
515 if (!mDoingDrag || mEndingSession) {
516 return NS_ERROR_FAILURE;
519 mEndingSession = true;
521 if (aDoneDrag && !mSuppressLevel) {
522 FireDragEventAtSource(eDragEnd, aKeyModifiers);
525 if (mDragPopup) {
526 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
527 if (pm) {
528 pm->HidePopup(mDragPopup, false, true, false, false);
532 for (uint32_t i = 0; i < mChildProcesses.Length(); ++i) {
533 mozilla::Unused << mChildProcesses[i]->SendEndDragSession(
534 aDoneDrag, mUserCancelled, mEndDragPoint, aKeyModifiers);
535 // Continue sending input events with input priority when stopping the dnd
536 // session.
537 mChildProcesses[i]->SetInputPriorityEventEnabled(true);
539 mChildProcesses.Clear();
541 // mDataTransfer and the items it owns are going to die anyway, but we
542 // explicitly deref the contained data here so that we don't have to wait for
543 // CC to reclaim the memory.
544 if (XRE_IsParentProcess()) {
545 DiscardInternalTransferData();
548 mDoingDrag = false;
549 mSessionIsSynthesizedForTests = false;
550 mEffectAllowedForTests = nsIDragService::DRAGDROP_ACTION_UNINITIALIZED;
551 mEndingSession = false;
552 mCanDrop = false;
554 // release the source we've been holding on to.
555 mSourceDocument = nullptr;
556 mSourceNode = nullptr;
557 mTriggeringPrincipal = nullptr;
558 mCsp = nullptr;
559 mSelection = nullptr;
560 mDataTransfer = nullptr;
561 mHasImage = false;
562 mUserCancelled = false;
563 mDragPopup = nullptr;
564 mDragStartData = nullptr;
565 mImage = nullptr;
566 mImageOffset = CSSIntPoint();
567 mScreenPosition = CSSIntPoint();
568 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
569 mInputSource = MouseEvent_Binding::MOZ_SOURCE_MOUSE;
570 mRegion = Nothing();
572 return NS_OK;
575 void nsBaseDragService::DiscardInternalTransferData() {
576 if (mDataTransfer && mSourceNode) {
577 MOZ_ASSERT(mDataTransfer);
579 DataTransferItemList* items = mDataTransfer->Items();
580 for (size_t i = 0; i < items->Length(); i++) {
581 bool found;
582 DataTransferItem* item = items->IndexedGetter(i, found);
584 // Non-OTHER items may still be needed by JS. Skip them.
585 if (!found || item->Kind() != DataTransferItem::KIND_OTHER) {
586 continue;
589 nsCOMPtr<nsIVariant> variant = item->DataNoSecurityCheck();
590 nsCOMPtr<nsIWritableVariant> writable = do_QueryInterface(variant);
592 if (writable) {
593 writable->SetAsEmpty();
599 NS_IMETHODIMP
600 nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage,
601 uint32_t aKeyModifiers) {
602 if (mSourceNode && mSourceDocument && !mSuppressLevel) {
603 RefPtr<PresShell> presShell = mSourceDocument->GetPresShell();
604 if (presShell) {
605 nsEventStatus status = nsEventStatus_eIgnore;
606 WidgetDragEvent event(true, aEventMessage, nullptr);
607 event.mFlags.mIsSynthesizedForTests = mSessionIsSynthesizedForTests;
608 event.mInputSource = mInputSource;
609 if (aEventMessage == eDragEnd) {
610 event.mRefPoint = mEndDragPoint;
611 event.mUserCancelled = mUserCancelled;
613 event.mModifiers = aKeyModifiers;
614 // Send the drag event to APZ, which needs to know about them to be
615 // able to accurately detect the end of a drag gesture.
616 if (nsPresContext* presContext = presShell->GetPresContext()) {
617 if (nsCOMPtr<nsIWidget> widget = presContext->GetRootWidget()) {
618 widget->DispatchEventToAPZOnly(&event);
622 nsCOMPtr<nsIContent> content = do_QueryInterface(mSourceNode);
623 return presShell->HandleDOMEventWithTarget(content, &event, &status);
627 return NS_OK;
630 /* This is used by Windows and Mac to update the position of a popup being
631 * used as a drag image during the drag. This isn't used on GTK as it manages
632 * the drag popup itself.
634 NS_IMETHODIMP
635 nsBaseDragService::DragMoved(int32_t aX, int32_t aY) {
636 if (mDragPopup) {
637 nsIFrame* frame = mDragPopup->GetPrimaryFrame();
638 if (frame && frame->IsMenuPopupFrame()) {
639 CSSIntPoint cssPos =
640 RoundedToInt(LayoutDeviceIntPoint(aX, aY) /
641 frame->PresContext()->CSSToDevPixelScale()) -
642 mImageOffset;
643 (static_cast<nsMenuPopupFrame*>(frame))->MoveTo(cssPos, true);
647 return NS_OK;
650 static PresShell* GetPresShellForContent(nsINode* aDOMNode) {
651 nsCOMPtr<nsIContent> content = do_QueryInterface(aDOMNode);
652 if (!content) return nullptr;
654 RefPtr<Document> document = content->GetComposedDoc();
655 if (document) {
656 document->FlushPendingNotifications(FlushType::Display);
657 return document->GetPresShell();
660 return nullptr;
663 nsresult nsBaseDragService::DrawDrag(nsINode* aDOMNode,
664 const Maybe<CSSIntRegion>& aRegion,
665 CSSIntPoint aScreenPosition,
666 LayoutDeviceIntRect* aScreenDragRect,
667 RefPtr<SourceSurface>* aSurface,
668 nsPresContext** aPresContext) {
669 *aSurface = nullptr;
670 *aPresContext = nullptr;
672 // use a default size, in case of an error.
673 aScreenDragRect->SetRect(aScreenPosition.x - mImageOffset.x,
674 aScreenPosition.y - mImageOffset.y, 1, 1);
676 // if a drag image was specified, use that, otherwise, use the source node
677 nsCOMPtr<nsINode> dragNode = mImage ? mImage.get() : aDOMNode;
679 // get the presshell for the node being dragged. If the drag image is not in
680 // a document or has no frame, get the presshell from the source drag node
681 PresShell* presShell = GetPresShellForContent(dragNode);
682 if (!presShell && mImage) {
683 presShell = GetPresShellForContent(aDOMNode);
685 if (!presShell) {
686 return NS_ERROR_FAILURE;
689 *aPresContext = presShell->GetPresContext();
691 if (mDragStartData) {
692 if (mImage) {
693 // Just clear the surface if chrome has overridden it with an image.
694 *aSurface = nullptr;
695 } else {
696 *aSurface = mDragStartData->TakeVisualization(aScreenDragRect);
699 mDragStartData = nullptr;
700 return NS_OK;
703 // convert mouse position to dev pixels of the prescontext
704 CSSIntPoint screenPosition(aScreenPosition);
705 screenPosition.x -= mImageOffset.x;
706 screenPosition.y -= mImageOffset.y;
707 LayoutDeviceIntPoint screenPoint =
708 ConvertToUnscaledDevPixels(*aPresContext, screenPosition);
709 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
711 // check if drag images are disabled
712 bool enableDragImages = Preferences::GetBool(DRAGIMAGES_PREF, true);
714 // didn't want an image, so just set the screen rectangle to the frame size
715 if (!enableDragImages || !mHasImage) {
716 // This holds a quantity in RelativeTo{presShell->GetRootFrame(),
717 // ViewportType::Layout} space.
718 nsRect presLayoutRect;
719 if (aRegion) {
720 // if a region was specified, set the screen rectangle to the area that
721 // the region occupies
722 presLayoutRect = ToAppUnits(aRegion->GetBounds(), AppUnitsPerCSSPixel());
723 } else {
724 // otherwise, there was no region so just set the rectangle to
725 // the size of the primary frame of the content.
726 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
727 nsIFrame* frame = content->GetPrimaryFrame();
728 if (frame) {
729 presLayoutRect = frame->GetRect();
733 LayoutDeviceRect screenVisualRect = ViewportUtils::ToScreenRelativeVisual(
734 LayoutDeviceRect::FromAppUnits(presLayoutRect,
735 (*aPresContext)->AppUnitsPerDevPixel()),
736 *aPresContext);
737 aScreenDragRect->SizeTo(screenVisualRect.Width(),
738 screenVisualRect.Height());
739 return NS_OK;
742 // draw the image for selections
743 if (mSelection) {
744 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
745 *aSurface = presShell->RenderSelection(
746 mSelection, pnt, aScreenDragRect,
747 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale);
748 return NS_OK;
751 // if a custom image was specified, check if it is an image node and draw
752 // using the source rather than the displayed image. But if mImage isn't
753 // an image or canvas, fall through to RenderNode below.
754 if (mImage) {
755 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
756 HTMLCanvasElement* canvas = HTMLCanvasElement::FromNodeOrNull(content);
757 if (canvas) {
758 return DrawDragForImage(*aPresContext, nullptr, canvas, aScreenDragRect,
759 aSurface);
762 nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(dragNode);
763 // for image nodes, create the drag image from the actual image data
764 if (imageLoader) {
765 return DrawDragForImage(*aPresContext, imageLoader, nullptr,
766 aScreenDragRect, aSurface);
769 // If the image is a popup, use that as the image. This allows custom drag
770 // images that can change during the drag, but means that any platform
771 // default image handling won't occur.
772 // XXXndeakin this should be chrome-only
774 nsIFrame* frame = content->GetPrimaryFrame();
775 if (frame && frame->IsMenuPopupFrame()) {
776 mDragPopup = content;
780 if (!mDragPopup) {
781 // otherwise, just draw the node
782 RenderImageFlags renderFlags =
783 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale;
784 if (renderFlags != RenderImageFlags::None) {
785 // check if the dragged node itself is an img element
786 if (dragNode->NodeName().LowerCaseEqualsLiteral("img")) {
787 renderFlags = renderFlags | RenderImageFlags::IsImage;
788 } else {
789 nsINodeList* childList = dragNode->ChildNodes();
790 uint32_t length = childList->Length();
791 // check every childnode for being an img element
792 // XXXbz why don't we need to check descendants recursively?
793 for (uint32_t count = 0; count < length; ++count) {
794 if (childList->Item(count)->NodeName().LowerCaseEqualsLiteral(
795 "img")) {
796 // if the dragnode contains an image, set RenderImageFlags::IsImage
797 // flag
798 renderFlags = renderFlags | RenderImageFlags::IsImage;
799 break;
804 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
805 *aSurface = presShell->RenderNode(dragNode, aRegion, pnt, aScreenDragRect,
806 renderFlags);
809 // If an image was specified, reset the position from the offset that was
810 // supplied.
811 if (mImage) {
812 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
815 return NS_OK;
818 nsresult nsBaseDragService::DrawDragForImage(
819 nsPresContext* aPresContext, nsIImageLoadingContent* aImageLoader,
820 HTMLCanvasElement* aCanvas, LayoutDeviceIntRect* aScreenDragRect,
821 RefPtr<SourceSurface>* aSurface) {
822 nsCOMPtr<imgIContainer> imgContainer;
823 if (aImageLoader) {
824 nsCOMPtr<imgIRequest> imgRequest;
825 nsresult rv = aImageLoader->GetRequest(
826 nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(imgRequest));
827 NS_ENSURE_SUCCESS(rv, rv);
828 if (!imgRequest) return NS_ERROR_NOT_AVAILABLE;
830 rv = imgRequest->GetImage(getter_AddRefs(imgContainer));
831 NS_ENSURE_SUCCESS(rv, rv);
832 if (!imgContainer) return NS_ERROR_NOT_AVAILABLE;
834 // use the size of the image as the size of the drag image
835 int32_t imageWidth, imageHeight;
836 rv = imgContainer->GetWidth(&imageWidth);
837 NS_ENSURE_SUCCESS(rv, rv);
839 rv = imgContainer->GetHeight(&imageHeight);
840 NS_ENSURE_SUCCESS(rv, rv);
842 aScreenDragRect->SizeTo(aPresContext->CSSPixelsToDevPixels(imageWidth),
843 aPresContext->CSSPixelsToDevPixels(imageHeight));
844 } else {
845 // XXX The canvas size should be converted to dev pixels.
846 NS_ASSERTION(aCanvas, "both image and canvas are null");
847 nsIntSize sz = aCanvas->GetSize();
848 aScreenDragRect->SizeTo(sz.width, sz.height);
851 nsIntSize destSize;
852 destSize.width = aScreenDragRect->Width();
853 destSize.height = aScreenDragRect->Height();
854 if (destSize.width == 0 || destSize.height == 0) return NS_ERROR_FAILURE;
856 nsresult result = NS_OK;
857 if (aImageLoader) {
858 RefPtr<DrawTarget> dt =
859 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
860 destSize, SurfaceFormat::B8G8R8A8);
861 if (!dt || !dt->IsValid()) return NS_ERROR_FAILURE;
863 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(dt);
864 if (!ctx) return NS_ERROR_FAILURE;
866 ImgDrawResult res =
867 imgContainer->Draw(ctx, destSize, ImageRegion::Create(destSize),
868 imgIContainer::FRAME_CURRENT, SamplingFilter::GOOD,
869 /* no SVGImageContext */ Nothing(),
870 imgIContainer::FLAG_SYNC_DECODE, 1.0);
871 if (res == ImgDrawResult::BAD_IMAGE || res == ImgDrawResult::BAD_ARGS ||
872 res == ImgDrawResult::NOT_SUPPORTED) {
873 return NS_ERROR_FAILURE;
875 *aSurface = dt->Snapshot();
876 } else {
877 *aSurface = aCanvas->GetSurfaceSnapshot();
880 return result;
883 LayoutDeviceIntPoint nsBaseDragService::ConvertToUnscaledDevPixels(
884 nsPresContext* aPresContext, CSSIntPoint aScreenPosition) {
885 int32_t adj =
886 aPresContext->DeviceContext()->AppUnitsPerDevPixelAtUnitFullZoom();
887 return LayoutDeviceIntPoint(
888 nsPresContext::CSSPixelsToAppUnits(aScreenPosition.x) / adj,
889 nsPresContext::CSSPixelsToAppUnits(aScreenPosition.y) / adj);
892 NS_IMETHODIMP
893 nsBaseDragService::Suppress() {
894 EndDragSession(false, 0);
895 ++mSuppressLevel;
896 return NS_OK;
899 NS_IMETHODIMP
900 nsBaseDragService::Unsuppress() {
901 --mSuppressLevel;
902 return NS_OK;
905 NS_IMETHODIMP
906 nsBaseDragService::UserCancelled() {
907 mUserCancelled = true;
908 return NS_OK;
911 NS_IMETHODIMP
912 nsBaseDragService::UpdateDragEffect() {
913 mDragActionFromChildProcess = mDragAction;
914 return NS_OK;
917 NS_IMETHODIMP
918 nsBaseDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
919 int32_t aImageY) {
920 // Don't change the image if this is a drag from another source or if there
921 // is a drag popup.
922 if (!mSourceNode || mDragPopup) return NS_OK;
924 mImage = aImage;
925 mImageOffset = CSSIntPoint(aImageX, aImageY);
926 return NS_OK;
929 NS_IMETHODIMP
930 nsBaseDragService::DragEventDispatchedToChildProcess() {
931 mDragEventDispatchedToChildProcess = true;
932 return NS_OK;
935 bool nsBaseDragService::MaybeAddChildProcess(
936 mozilla::dom::ContentParent* aChild) {
937 if (!mChildProcesses.Contains(aChild)) {
938 mChildProcesses.AppendElement(aChild);
939 return true;
941 return false;
944 bool nsBaseDragService::RemoveAllChildProcesses() {
945 for (uint32_t c = 0; c < mChildProcesses.Length(); c++) {
946 mozilla::Unused << mChildProcesses[c]->SendEndDragSession(
947 true, false, LayoutDeviceIntPoint(), 0);
949 mChildProcesses.Clear();
950 return true;