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"
14 #include "nsIInterfaceRequestorUtils.h"
16 #include "nsFrameLoaderOwner.h"
17 #include "nsIContent.h"
18 #include "nsViewManager.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"
27 #include "nsXULPopupManager.h"
28 #include "nsMenuPopupFrame.h"
29 #include "nsTreeBodyFrame.h"
30 #include "mozilla/MouseEvents.h"
31 #include "mozilla/Preferences.h"
32 #include "mozilla/PresShell.h"
33 #include "mozilla/ProfilerLabels.h"
34 #include "mozilla/SVGImageContext.h"
35 #include "mozilla/TextControlElement.h"
36 #include "mozilla/Unused.h"
37 #include "mozilla/ViewportUtils.h"
38 #include "mozilla/dom/BindingDeclarations.h"
39 #include "mozilla/dom/DataTransferItemList.h"
40 #include "mozilla/dom/DataTransfer.h"
41 #include "mozilla/dom/Document.h"
42 #include "mozilla/dom/DocumentInlines.h"
43 #include "mozilla/dom/DragEvent.h"
44 #include "mozilla/dom/MouseEventBinding.h"
45 #include "mozilla/dom/Selection.h"
46 #include "mozilla/gfx/2D.h"
47 #include "nsFrameLoader.h"
48 #include "BrowserParent.h"
49 #include "nsIMutableArray.h"
50 #include "gfxContext.h"
51 #include "gfxPlatform.h"
54 using namespace mozilla
;
55 using namespace mozilla::dom
;
56 using namespace mozilla::gfx
;
57 using namespace mozilla::image
;
59 #define DRAGIMAGES_PREF "nglayout.enable_drag_images"
61 nsBaseDragService::nsBaseDragService()
63 mOnlyChromeDrop(false),
65 mSessionIsSynthesizedForTests(false),
66 mIsDraggingTextInTextControl(false),
67 mEndingSession(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
),
76 mInputSource(MouseEvent_Binding::MOZ_SOURCE_MOUSE
) {}
78 nsBaseDragService::~nsBaseDragService() = default;
80 NS_IMPL_ISUPPORTS(nsBaseDragService
, nsIDragService
, nsIDragSession
)
82 //---------------------------------------------------------
84 nsBaseDragService::SetCanDrop(bool aCanDrop
) {
89 //---------------------------------------------------------
91 nsBaseDragService::GetCanDrop(bool* aCanDrop
) {
95 //---------------------------------------------------------
97 nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome
) {
98 mOnlyChromeDrop
= aOnlyChrome
;
102 //---------------------------------------------------------
104 nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome
) {
105 *aOnlyChrome
= mOnlyChromeDrop
;
109 //---------------------------------------------------------
111 nsBaseDragService::SetDragAction(uint32_t anAction
) {
112 mDragAction
= anAction
;
116 //---------------------------------------------------------
118 nsBaseDragService::GetDragAction(uint32_t* anAction
) {
119 *anAction
= mDragAction
;
123 //-------------------------------------------------------------------------
126 nsBaseDragService::GetNumDropItems(uint32_t* aNumItems
) {
128 return NS_ERROR_FAILURE
;
132 // GetSourceWindowContext
134 // Returns the window context where the drag was initiated. This will be
135 // nullptr if the drag began outside of our application.
138 nsBaseDragService::GetSourceWindowContext(
139 WindowContext
** aSourceWindowContext
) {
140 *aSourceWindowContext
= mSourceWindowContext
.get();
141 NS_IF_ADDREF(*aSourceWindowContext
);
146 nsBaseDragService::SetSourceWindowContext(WindowContext
* aSourceWindowContext
) {
147 // This should only be called in a child process.
148 MOZ_ASSERT(!XRE_IsParentProcess());
149 mSourceWindowContext
= aSourceWindowContext
;
156 // Returns the DOM node where the drag was initiated. This will be
157 // nullptr if the drag began outside of our application.
160 nsBaseDragService::GetSourceNode(nsINode
** aSourceNode
) {
161 *aSourceNode
= do_AddRef(mSourceNode
).take();
165 void nsBaseDragService::UpdateSource(nsINode
* aNewSourceNode
,
166 Selection
* aNewSelection
) {
167 MOZ_ASSERT(mSourceNode
);
168 MOZ_ASSERT(aNewSourceNode
);
169 MOZ_ASSERT(mSourceNode
->IsInNativeAnonymousSubtree() ||
170 aNewSourceNode
->IsInNativeAnonymousSubtree());
171 MOZ_ASSERT(mSourceDocument
== aNewSourceNode
->OwnerDoc());
172 mSourceNode
= aNewSourceNode
;
173 // Don't set mSelection if the session was invoked without selection or
174 // making it becomes nullptr. The latter occurs when the old frame is
176 if (mSelection
&& aNewSelection
) {
177 // XXX If the dragging image is created once (e.g., at drag start), the
178 // image won't be updated unless we notify `DrawDrag` callers.
179 // However, it must be okay for now to keep using older image of
181 mSelection
= aNewSelection
;
186 nsBaseDragService::GetTriggeringPrincipal(nsIPrincipal
** aPrincipal
) {
187 NS_IF_ADDREF(*aPrincipal
= mTriggeringPrincipal
);
192 nsBaseDragService::SetTriggeringPrincipal(nsIPrincipal
* aPrincipal
) {
193 mTriggeringPrincipal
= aPrincipal
;
198 nsBaseDragService::GetCsp(nsIContentSecurityPolicy
** aCsp
) {
199 NS_IF_ADDREF(*aCsp
= mCsp
);
204 nsBaseDragService::SetCsp(nsIContentSecurityPolicy
* aCsp
) {
209 //-------------------------------------------------------------------------
212 nsBaseDragService::GetData(nsITransferable
* aTransferable
,
213 uint32_t aItemIndex
) {
214 return NS_ERROR_FAILURE
;
217 //-------------------------------------------------------------------------
219 nsBaseDragService::IsDataFlavorSupported(const char* aDataFlavor
,
221 return NS_ERROR_FAILURE
;
225 nsBaseDragService::GetDataTransferXPCOM(DataTransfer
** aDataTransfer
) {
226 *aDataTransfer
= mDataTransfer
;
227 NS_IF_ADDREF(*aDataTransfer
);
232 nsBaseDragService::SetDataTransferXPCOM(DataTransfer
* aDataTransfer
) {
233 NS_ENSURE_STATE(aDataTransfer
);
234 mDataTransfer
= aDataTransfer
;
238 DataTransfer
* nsBaseDragService::GetDataTransfer() { return mDataTransfer
; }
240 void nsBaseDragService::SetDataTransfer(DataTransfer
* aDataTransfer
) {
241 mDataTransfer
= aDataTransfer
;
244 bool nsBaseDragService::IsSynthesizedForTests() {
245 return mSessionIsSynthesizedForTests
;
248 bool nsBaseDragService::IsDraggingTextInTextControl() {
249 return mIsDraggingTextInTextControl
;
252 uint32_t nsBaseDragService::GetEffectAllowedForTests() {
253 MOZ_ASSERT(mSessionIsSynthesizedForTests
);
254 return mEffectAllowedForTests
;
257 NS_IMETHODIMP
nsBaseDragService::SetDragEndPointForTests(int32_t aScreenX
,
259 MOZ_ASSERT(mDoingDrag
);
260 MOZ_ASSERT(mSourceDocument
);
261 MOZ_ASSERT(mSessionIsSynthesizedForTests
);
263 if (!mDoingDrag
|| !mSourceDocument
|| !mSessionIsSynthesizedForTests
) {
264 return NS_ERROR_FAILURE
;
266 nsPresContext
* pc
= mSourceDocument
->GetPresContext();
267 if (NS_WARN_IF(!pc
)) {
268 return NS_ERROR_FAILURE
;
270 auto p
= LayoutDeviceIntPoint::Round(CSSIntPoint(aScreenX
, aScreenY
) *
271 pc
->CSSToDevPixelScale());
272 // p is screen-relative, and we want them to be top-level-widget-relative.
273 if (nsCOMPtr
<nsIWidget
> widget
= pc
->GetRootWidget()) {
274 p
-= widget
->WidgetToScreenOffset();
275 p
+= widget
->WidgetToTopLevelWidgetOffset();
281 //-------------------------------------------------------------------------
283 nsBaseDragService::InvokeDragSession(
284 nsINode
* aDOMNode
, nsIPrincipal
* aPrincipal
, nsIContentSecurityPolicy
* aCsp
,
285 nsICookieJarSettings
* aCookieJarSettings
, nsIArray
* aTransferableArray
,
286 uint32_t aActionType
,
287 nsContentPolicyType aContentPolicyType
= nsIContentPolicy::TYPE_OTHER
) {
288 AUTO_PROFILER_LABEL("nsBaseDragService::InvokeDragSession", OTHER
);
290 NS_ENSURE_TRUE(aDOMNode
, NS_ERROR_INVALID_ARG
);
291 NS_ENSURE_TRUE(mSuppressLevel
== 0, NS_ERROR_FAILURE
);
293 // stash the document of the dom node
294 mSourceDocument
= aDOMNode
->OwnerDoc();
295 mTriggeringPrincipal
= aPrincipal
;
297 mSourceNode
= aDOMNode
;
298 mIsDraggingTextInTextControl
=
299 mSourceNode
->IsInNativeAnonymousSubtree() &&
300 TextControlElement::FromNodeOrNull(
301 mSourceNode
->GetClosestNativeAnonymousSubtreeRootParent());
302 mContentPolicyType
= aContentPolicyType
;
303 mEndDragPoint
= LayoutDeviceIntPoint(0, 0);
305 // When the mouse goes down, the selection code starts a mouse
306 // capture. However, this gets in the way of determining drag
307 // feedback for things like trees because the event coordinates
308 // are in the wrong coord system, so turn off mouse capture.
309 PresShell::ClearMouseCapture();
311 if (mSessionIsSynthesizedForTests
) {
313 mDragAction
= aActionType
;
314 mEffectAllowedForTests
= aActionType
;
318 // If you're hitting this, a test is causing the browser to attempt to enter
319 // the drag-drop native nested event loop, which will put the browser in a
320 // state that won't run tests properly until there's manual intervention
321 // to exit the drag-drop loop (either by moving the mouse or hitting escape),
322 // which can't be done from script since we're in the nested loop.
324 // The best way to avoid this is to catch the dragstart event on the item
325 // being dragged, and then to call preventDefault() and stopPropagating() on
327 if (XRE_IsParentProcess()) {
329 !xpc::IsInAutomation(),
330 "About to start drag-drop native loop on which will prevent later "
331 "tests from running properly.");
335 mozilla::Unused
<< aTransferableArray
->GetLength(&length
);
337 nsCOMPtr
<nsIMutableArray
> mutableArray
=
338 do_QueryInterface(aTransferableArray
);
340 // In order to be able trigger dnd, we need to have some transferable
342 nsCOMPtr
<nsITransferable
> trans
=
343 do_CreateInstance("@mozilla.org/widget/transferable;1");
344 trans
->Init(nullptr);
345 trans
->SetRequestingPrincipal(mSourceNode
->NodePrincipal());
346 trans
->SetContentPolicyType(mContentPolicyType
);
347 trans
->SetCookieJarSettings(aCookieJarSettings
);
348 mutableArray
->AppendElement(trans
);
351 for (uint32_t i
= 0; i
< length
; ++i
) {
352 nsCOMPtr
<nsITransferable
> trans
=
353 do_QueryElementAt(aTransferableArray
, i
);
355 // Set the requestingPrincipal on the transferable.
356 trans
->SetRequestingPrincipal(mSourceNode
->NodePrincipal());
357 trans
->SetContentPolicyType(mContentPolicyType
);
358 trans
->SetCookieJarSettings(aCookieJarSettings
);
363 nsresult rv
= InvokeDragSessionImpl(aTransferableArray
, mRegion
, aActionType
);
366 // Set mDoingDrag so that EndDragSession cleans up and sends the dragend
367 // event after the aborted drag.
369 EndDragSession(true, 0);
376 nsBaseDragService::InvokeDragSessionWithImage(
377 nsINode
* aDOMNode
, nsIPrincipal
* aPrincipal
, nsIContentSecurityPolicy
* aCsp
,
378 nsICookieJarSettings
* aCookieJarSettings
, nsIArray
* aTransferableArray
,
379 uint32_t aActionType
, nsINode
* aImage
, int32_t aImageX
, int32_t aImageY
,
380 DragEvent
* aDragEvent
, DataTransfer
* aDataTransfer
) {
381 NS_ENSURE_TRUE(aDragEvent
, NS_ERROR_NULL_POINTER
);
382 NS_ENSURE_TRUE(aDataTransfer
, NS_ERROR_NULL_POINTER
);
383 NS_ENSURE_TRUE(mSuppressLevel
== 0, NS_ERROR_FAILURE
);
385 mSessionIsSynthesizedForTests
=
386 aDragEvent
->WidgetEventPtr()->mFlags
.mIsSynthesizedForTests
;
387 mDataTransfer
= aDataTransfer
;
388 mSelection
= nullptr;
390 mDragPopup
= nullptr;
392 mImageOffset
= CSSIntPoint(aImageX
, aImageY
);
393 mDragStartData
= nullptr;
394 mSourceWindowContext
=
395 aDOMNode
? aDOMNode
->OwnerDoc()->GetWindowContext() : nullptr;
397 mScreenPosition
= aDragEvent
->ScreenPoint(CallerType::System
);
398 mInputSource
= aDragEvent
->MozInputSource();
400 // If dragging within a XUL tree and no custom drag image was
401 // set, the region argument to InvokeDragSessionWithImage needs
402 // to be set to the area encompassing the selected rows of the
403 // tree to ensure that the drag feedback gets clipped to those
404 // rows. For other content, region should be null.
406 if (aDOMNode
&& aDOMNode
->IsContent() && !aImage
) {
407 if (aDOMNode
->NodeInfo()->Equals(nsGkAtoms::treechildren
,
409 nsTreeBodyFrame
* treeBody
=
410 do_QueryFrame(aDOMNode
->AsContent()->GetPrimaryFrame());
412 mRegion
= treeBody
->GetSelectionRegion();
417 nsresult rv
= InvokeDragSession(
418 aDOMNode
, aPrincipal
, aCsp
, aCookieJarSettings
, aTransferableArray
,
419 aActionType
, nsIContentPolicy::TYPE_INTERNAL_IMAGE
);
425 nsBaseDragService::InvokeDragSessionWithRemoteImage(
426 nsINode
* aDOMNode
, nsIPrincipal
* aPrincipal
, nsIContentSecurityPolicy
* aCsp
,
427 nsICookieJarSettings
* aCookieJarSettings
, nsIArray
* aTransferableArray
,
428 uint32_t aActionType
, RemoteDragStartData
* aDragStartData
,
429 DragEvent
* aDragEvent
, DataTransfer
* aDataTransfer
) {
430 NS_ENSURE_TRUE(aDragEvent
, NS_ERROR_NULL_POINTER
);
431 NS_ENSURE_TRUE(aDataTransfer
, NS_ERROR_NULL_POINTER
);
432 NS_ENSURE_TRUE(mSuppressLevel
== 0, NS_ERROR_FAILURE
);
434 mSessionIsSynthesizedForTests
=
435 aDragEvent
->WidgetEventPtr()->mFlags
.mIsSynthesizedForTests
;
436 mDataTransfer
= aDataTransfer
;
437 mSelection
= nullptr;
439 mDragPopup
= nullptr;
441 mDragStartData
= aDragStartData
;
442 mImageOffset
= CSSIntPoint(0, 0);
443 mSourceWindowContext
= mDragStartData
->GetSourceWindowContext();
445 mScreenPosition
= aDragEvent
->ScreenPoint(CallerType::System
);
446 mInputSource
= aDragEvent
->MozInputSource();
448 nsresult rv
= InvokeDragSession(
449 aDOMNode
, aPrincipal
, aCsp
, aCookieJarSettings
, aTransferableArray
,
450 aActionType
, nsIContentPolicy::TYPE_INTERNAL_IMAGE
);
456 nsBaseDragService::InvokeDragSessionWithSelection(
457 Selection
* aSelection
, nsIPrincipal
* aPrincipal
,
458 nsIContentSecurityPolicy
* aCsp
, nsICookieJarSettings
* aCookieJarSettings
,
459 nsIArray
* aTransferableArray
, uint32_t aActionType
, DragEvent
* aDragEvent
,
460 DataTransfer
* aDataTransfer
) {
461 NS_ENSURE_TRUE(aSelection
, NS_ERROR_NULL_POINTER
);
462 NS_ENSURE_TRUE(aDragEvent
, NS_ERROR_NULL_POINTER
);
463 NS_ENSURE_TRUE(mSuppressLevel
== 0, NS_ERROR_FAILURE
);
465 mSessionIsSynthesizedForTests
=
466 aDragEvent
->WidgetEventPtr()->mFlags
.mIsSynthesizedForTests
;
467 mDataTransfer
= aDataTransfer
;
468 mSelection
= aSelection
;
470 mDragPopup
= nullptr;
472 mImageOffset
= CSSIntPoint();
473 mDragStartData
= nullptr;
476 mScreenPosition
.x
= aDragEvent
->ScreenX(CallerType::System
);
477 mScreenPosition
.y
= aDragEvent
->ScreenY(CallerType::System
);
478 mInputSource
= aDragEvent
->MozInputSource();
480 // just get the focused node from the selection
481 // XXXndeakin this should actually be the deepest node that contains both
482 // endpoints of the selection
483 nsCOMPtr
<nsINode
> node
= aSelection
->GetFocusNode();
484 mSourceWindowContext
= node
? node
->OwnerDoc()->GetWindowContext() : nullptr;
486 return InvokeDragSession(node
, aPrincipal
, aCsp
, aCookieJarSettings
,
487 aTransferableArray
, aActionType
,
488 nsIContentPolicy::TYPE_OTHER
);
491 //-------------------------------------------------------------------------
493 nsBaseDragService::GetCurrentSession(nsIDragSession
** aSession
) {
494 if (!aSession
) return NS_ERROR_INVALID_ARG
;
496 // "this" also implements a drag session, so say we are one but only
497 // if there is currently a drag going on.
498 if (!mSuppressLevel
&& mDoingDrag
) {
500 NS_ADDREF(*aSession
); // addRef because we're a "getter"
507 //-------------------------------------------------------------------------
509 nsBaseDragService::StartDragSession() {
511 return NS_ERROR_FAILURE
;
514 // By default dispatch drop also to content.
515 mOnlyChromeDrop
= false;
520 NS_IMETHODIMP
nsBaseDragService::StartDragSessionForTests(
521 uint32_t aAllowedEffect
) {
522 if (NS_WARN_IF(NS_FAILED(StartDragSession()))) {
523 return NS_ERROR_FAILURE
;
525 mDragAction
= aAllowedEffect
;
526 mEffectAllowedForTests
= aAllowedEffect
;
527 mSessionIsSynthesizedForTests
= true;
531 void nsBaseDragService::OpenDragPopup() {
533 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
535 pm
->ShowPopupAtScreen(mDragPopup
, mScreenPosition
.x
- mImageOffset
.x
,
536 mScreenPosition
.y
- mImageOffset
.y
, false, nullptr);
541 int32_t nsBaseDragService::TakeChildProcessDragAction() {
542 // If the last event was dispatched to the child process, use the drag action
543 // assigned from it instead and return it. DRAGDROP_ACTION_UNINITIALIZED is
544 // returned otherwise.
545 int32_t retval
= DRAGDROP_ACTION_UNINITIALIZED
;
546 if (TakeDragEventDispatchedToChildProcess() &&
547 mDragActionFromChildProcess
!= DRAGDROP_ACTION_UNINITIALIZED
) {
548 retval
= mDragActionFromChildProcess
;
554 //-------------------------------------------------------------------------
556 nsBaseDragService::EndDragSession(bool aDoneDrag
, uint32_t aKeyModifiers
) {
557 if (!mDoingDrag
|| mEndingSession
) {
558 return NS_ERROR_FAILURE
;
561 mEndingSession
= true;
563 if (aDoneDrag
&& !mSuppressLevel
) {
564 FireDragEventAtSource(eDragEnd
, aKeyModifiers
);
568 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
570 pm
->HidePopup(mDragPopup
, false, true, false, false);
574 for (uint32_t i
= 0; i
< mChildProcesses
.Length(); ++i
) {
575 mozilla::Unused
<< mChildProcesses
[i
]->SendEndDragSession(
576 aDoneDrag
, mUserCancelled
, mEndDragPoint
, aKeyModifiers
);
577 // Continue sending input events with input priority when stopping the dnd
579 mChildProcesses
[i
]->SetInputPriorityEventEnabled(true);
581 mChildProcesses
.Clear();
583 // mDataTransfer and the items it owns are going to die anyway, but we
584 // explicitly deref the contained data here so that we don't have to wait for
585 // CC to reclaim the memory.
586 if (XRE_IsParentProcess()) {
587 DiscardInternalTransferData();
591 mSessionIsSynthesizedForTests
= false;
592 mIsDraggingTextInTextControl
= false;
593 mEffectAllowedForTests
= nsIDragService::DRAGDROP_ACTION_UNINITIALIZED
;
594 mEndingSession
= false;
597 // release the source we've been holding on to.
598 mSourceDocument
= nullptr;
599 mSourceNode
= nullptr;
600 mSourceWindowContext
= nullptr;
601 mTriggeringPrincipal
= nullptr;
603 mSelection
= nullptr;
604 mDataTransfer
= nullptr;
606 mUserCancelled
= false;
607 mDragPopup
= nullptr;
608 mDragStartData
= nullptr;
610 mImageOffset
= CSSIntPoint();
611 mScreenPosition
= CSSIntPoint();
612 mEndDragPoint
= LayoutDeviceIntPoint(0, 0);
613 mInputSource
= MouseEvent_Binding::MOZ_SOURCE_MOUSE
;
619 void nsBaseDragService::DiscardInternalTransferData() {
620 if (mDataTransfer
&& mSourceNode
) {
621 MOZ_ASSERT(mDataTransfer
);
623 DataTransferItemList
* items
= mDataTransfer
->Items();
624 for (size_t i
= 0; i
< items
->Length(); i
++) {
626 DataTransferItem
* item
= items
->IndexedGetter(i
, found
);
628 // Non-OTHER items may still be needed by JS. Skip them.
629 if (!found
|| item
->Kind() != DataTransferItem::KIND_OTHER
) {
633 nsCOMPtr
<nsIVariant
> variant
= item
->DataNoSecurityCheck();
634 nsCOMPtr
<nsIWritableVariant
> writable
= do_QueryInterface(variant
);
637 writable
->SetAsEmpty();
644 nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage
,
645 uint32_t aKeyModifiers
) {
646 if (!mSourceNode
|| !mSourceDocument
|| mSuppressLevel
) {
649 RefPtr
<PresShell
> presShell
= mSourceDocument
->GetPresShell();
654 RefPtr
<nsPresContext
> pc
= presShell
->GetPresContext();
655 nsCOMPtr
<nsIWidget
> widget
= pc
? pc
->GetRootWidget() : nullptr;
657 nsEventStatus status
= nsEventStatus_eIgnore
;
658 WidgetDragEvent
event(true, aEventMessage
, widget
);
659 event
.mFlags
.mIsSynthesizedForTests
= mSessionIsSynthesizedForTests
;
660 event
.mInputSource
= mInputSource
;
661 if (aEventMessage
== eDragEnd
) {
662 event
.mRefPoint
= mEndDragPoint
;
664 event
.mRefPoint
-= widget
->WidgetToTopLevelWidgetOffset();
666 event
.mUserCancelled
= mUserCancelled
;
668 event
.mModifiers
= aKeyModifiers
;
671 // Send the drag event to APZ, which needs to know about them to be
672 // able to accurately detect the end of a drag gesture.
673 widget
->DispatchEventToAPZOnly(&event
);
676 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(mSourceNode
);
677 return presShell
->HandleDOMEventWithTarget(content
, &event
, &status
);
680 /* This is used by Windows and Mac to update the position of a popup being
681 * used as a drag image during the drag. This isn't used on GTK as it manages
682 * the drag popup itself.
685 nsBaseDragService::DragMoved(int32_t aX
, int32_t aY
) {
687 nsIFrame
* frame
= mDragPopup
->GetPrimaryFrame();
688 if (frame
&& frame
->IsMenuPopupFrame()) {
690 RoundedToInt(LayoutDeviceIntPoint(aX
, aY
) /
691 frame
->PresContext()->CSSToDevPixelScale()) -
693 static_cast<nsMenuPopupFrame
*>(frame
)->MoveTo(cssPos
, true);
700 static PresShell
* GetPresShellForContent(nsINode
* aDOMNode
) {
701 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(aDOMNode
);
702 if (!content
) return nullptr;
704 RefPtr
<Document
> document
= content
->GetComposedDoc();
706 document
->FlushPendingNotifications(FlushType::Display
);
707 return document
->GetPresShell();
713 nsresult
nsBaseDragService::DrawDrag(nsINode
* aDOMNode
,
714 const Maybe
<CSSIntRegion
>& aRegion
,
715 CSSIntPoint aScreenPosition
,
716 LayoutDeviceIntRect
* aScreenDragRect
,
717 RefPtr
<SourceSurface
>* aSurface
,
718 nsPresContext
** aPresContext
) {
720 *aPresContext
= nullptr;
722 // use a default size, in case of an error.
723 aScreenDragRect
->SetRect(aScreenPosition
.x
- mImageOffset
.x
,
724 aScreenPosition
.y
- mImageOffset
.y
, 1, 1);
726 // if a drag image was specified, use that, otherwise, use the source node
727 nsCOMPtr
<nsINode
> dragNode
= mImage
? mImage
.get() : aDOMNode
;
729 // get the presshell for the node being dragged. If the drag image is not in
730 // a document or has no frame, get the presshell from the source drag node
731 PresShell
* presShell
= GetPresShellForContent(dragNode
);
732 if (!presShell
&& mImage
) {
733 presShell
= GetPresShellForContent(aDOMNode
);
736 return NS_ERROR_FAILURE
;
739 *aPresContext
= presShell
->GetPresContext();
741 if (mDragStartData
) {
743 // Just clear the surface if chrome has overridden it with an image.
746 *aSurface
= mDragStartData
->TakeVisualization(aScreenDragRect
);
749 mDragStartData
= nullptr;
753 // convert mouse position to dev pixels of the prescontext
754 const CSSIntPoint screenPosition
= aScreenPosition
- mImageOffset
;
755 const auto screenPoint
= LayoutDeviceIntPoint::Round(
756 screenPosition
* (*aPresContext
)->CSSToDevPixelScale());
757 aScreenDragRect
->MoveTo(screenPoint
.x
, screenPoint
.y
);
759 // check if drag images are disabled
760 bool enableDragImages
= Preferences::GetBool(DRAGIMAGES_PREF
, true);
762 // didn't want an image, so just set the screen rectangle to the frame size
763 if (!enableDragImages
|| !mHasImage
) {
764 // This holds a quantity in RelativeTo{presShell->GetRootFrame(),
765 // ViewportType::Layout} space.
766 nsRect presLayoutRect
;
768 // if a region was specified, set the screen rectangle to the area that
769 // the region occupies
770 presLayoutRect
= ToAppUnits(aRegion
->GetBounds(), AppUnitsPerCSSPixel());
772 // otherwise, there was no region so just set the rectangle to
773 // the size of the primary frame of the content.
774 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(dragNode
);
775 if (nsIFrame
* frame
= content
->GetPrimaryFrame()) {
776 presLayoutRect
= frame
->GetBoundingClientRect();
780 LayoutDeviceRect screenVisualRect
= ViewportUtils::ToScreenRelativeVisual(
781 LayoutDeviceRect::FromAppUnits(presLayoutRect
,
782 (*aPresContext
)->AppUnitsPerDevPixel()),
784 aScreenDragRect
->SizeTo(screenVisualRect
.Width(),
785 screenVisualRect
.Height());
789 // draw the image for selections
791 LayoutDeviceIntPoint
pnt(aScreenDragRect
->TopLeft());
792 *aSurface
= presShell
->RenderSelection(
793 mSelection
, pnt
, aScreenDragRect
,
794 mImage
? RenderImageFlags::None
: RenderImageFlags::AutoScale
);
798 // if a custom image was specified, check if it is an image node and draw
799 // using the source rather than the displayed image. But if mImage isn't
800 // an image or canvas, fall through to RenderNode below.
802 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(dragNode
);
803 HTMLCanvasElement
* canvas
= HTMLCanvasElement::FromNodeOrNull(content
);
805 return DrawDragForImage(*aPresContext
, nullptr, canvas
, aScreenDragRect
,
809 nsCOMPtr
<nsIImageLoadingContent
> imageLoader
= do_QueryInterface(dragNode
);
810 // for image nodes, create the drag image from the actual image data
812 return DrawDragForImage(*aPresContext
, imageLoader
, nullptr,
813 aScreenDragRect
, aSurface
);
816 // If the image is a popup, use that as the image. This allows custom drag
817 // images that can change during the drag, but means that any platform
818 // default image handling won't occur.
819 // XXXndeakin this should be chrome-only
821 nsIFrame
* frame
= content
->GetPrimaryFrame();
822 if (frame
&& frame
->IsMenuPopupFrame()) {
823 mDragPopup
= content
;
828 // otherwise, just draw the node
829 RenderImageFlags renderFlags
=
830 mImage
? RenderImageFlags::None
: RenderImageFlags::AutoScale
;
831 if (renderFlags
!= RenderImageFlags::None
) {
832 // check if the dragged node itself is an img element
833 if (dragNode
->NodeName().LowerCaseEqualsLiteral("img")) {
834 renderFlags
= renderFlags
| RenderImageFlags::IsImage
;
836 nsINodeList
* childList
= dragNode
->ChildNodes();
837 uint32_t length
= childList
->Length();
838 // check every childnode for being an img element
839 // XXXbz why don't we need to check descendants recursively?
840 for (uint32_t count
= 0; count
< length
; ++count
) {
841 if (childList
->Item(count
)->NodeName().LowerCaseEqualsLiteral(
843 // if the dragnode contains an image, set RenderImageFlags::IsImage
845 renderFlags
= renderFlags
| RenderImageFlags::IsImage
;
851 LayoutDeviceIntPoint
pnt(aScreenDragRect
->TopLeft());
852 *aSurface
= presShell
->RenderNode(dragNode
, aRegion
, pnt
, aScreenDragRect
,
856 // If an image was specified, reset the position from the offset that was
859 aScreenDragRect
->MoveTo(screenPoint
.x
, screenPoint
.y
);
865 nsresult
nsBaseDragService::DrawDragForImage(
866 nsPresContext
* aPresContext
, nsIImageLoadingContent
* aImageLoader
,
867 HTMLCanvasElement
* aCanvas
, LayoutDeviceIntRect
* aScreenDragRect
,
868 RefPtr
<SourceSurface
>* aSurface
) {
869 nsCOMPtr
<imgIContainer
> imgContainer
;
871 nsCOMPtr
<imgIRequest
> imgRequest
;
872 nsresult rv
= aImageLoader
->GetRequest(
873 nsIImageLoadingContent::CURRENT_REQUEST
, getter_AddRefs(imgRequest
));
874 NS_ENSURE_SUCCESS(rv
, rv
);
875 if (!imgRequest
) return NS_ERROR_NOT_AVAILABLE
;
877 rv
= imgRequest
->GetImage(getter_AddRefs(imgContainer
));
878 NS_ENSURE_SUCCESS(rv
, rv
);
879 if (!imgContainer
) return NS_ERROR_NOT_AVAILABLE
;
881 // use the size of the image as the size of the drag image
882 int32_t imageWidth
, imageHeight
;
883 rv
= imgContainer
->GetWidth(&imageWidth
);
884 NS_ENSURE_SUCCESS(rv
, rv
);
886 rv
= imgContainer
->GetHeight(&imageHeight
);
887 NS_ENSURE_SUCCESS(rv
, rv
);
889 aScreenDragRect
->SizeTo(aPresContext
->CSSPixelsToDevPixels(imageWidth
),
890 aPresContext
->CSSPixelsToDevPixels(imageHeight
));
892 // XXX The canvas size should be converted to dev pixels.
893 NS_ASSERTION(aCanvas
, "both image and canvas are null");
894 nsIntSize sz
= aCanvas
->GetSize();
895 aScreenDragRect
->SizeTo(sz
.width
, sz
.height
);
899 destSize
.width
= aScreenDragRect
->Width();
900 destSize
.height
= aScreenDragRect
->Height();
901 if (destSize
.width
== 0 || destSize
.height
== 0) return NS_ERROR_FAILURE
;
903 nsresult result
= NS_OK
;
905 RefPtr
<DrawTarget
> dt
=
906 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
907 destSize
, SurfaceFormat::B8G8R8A8
);
908 if (!dt
|| !dt
->IsValid()) return NS_ERROR_FAILURE
;
910 RefPtr
<gfxContext
> ctx
= gfxContext::CreateOrNull(dt
);
911 if (!ctx
) return NS_ERROR_FAILURE
;
914 imgContainer
->Draw(ctx
, destSize
, ImageRegion::Create(destSize
),
915 imgIContainer::FRAME_CURRENT
, SamplingFilter::GOOD
,
916 /* no SVGImageContext */ Nothing(),
917 imgIContainer::FLAG_SYNC_DECODE
, 1.0);
918 if (res
== ImgDrawResult::BAD_IMAGE
|| res
== ImgDrawResult::BAD_ARGS
||
919 res
== ImgDrawResult::NOT_SUPPORTED
) {
920 return NS_ERROR_FAILURE
;
922 *aSurface
= dt
->Snapshot();
924 *aSurface
= aCanvas
->GetSurfaceSnapshot();
931 nsBaseDragService::Suppress() {
932 EndDragSession(false, 0);
938 nsBaseDragService::Unsuppress() {
944 nsBaseDragService::UserCancelled() {
945 mUserCancelled
= true;
950 nsBaseDragService::UpdateDragEffect() {
951 mDragActionFromChildProcess
= mDragAction
;
956 nsBaseDragService::UpdateDragImage(nsINode
* aImage
, int32_t aImageX
,
958 // Don't change the image if this is a drag from another source or if there
960 if (!mSourceNode
|| mDragPopup
) return NS_OK
;
963 mImageOffset
= CSSIntPoint(aImageX
, aImageY
);
968 nsBaseDragService::DragEventDispatchedToChildProcess() {
969 mDragEventDispatchedToChildProcess
= true;
973 bool nsBaseDragService::MaybeAddChildProcess(
974 mozilla::dom::ContentParent
* aChild
) {
975 if (!mChildProcesses
.Contains(aChild
)) {
976 mChildProcesses
.AppendElement(aChild
);
982 bool nsBaseDragService::RemoveAllChildProcesses() {
983 for (uint32_t c
= 0; c
< mChildProcesses
.Length(); c
++) {
984 mozilla::Unused
<< mChildProcesses
[c
]->SendEndDragSession(
985 true, false, LayoutDeviceIntPoint(), 0);
987 mChildProcesses
.Clear();