Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / widget / nsBaseDragService.cpp
blob46f66ca89db2f9a9ff384458118b51e8b48abf3a
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 #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"
52 #include "nscore.h"
53 #include <algorithm>
55 using namespace mozilla;
56 using namespace mozilla::dom;
57 using namespace mozilla::gfx;
58 using namespace mozilla::image;
60 LazyLogModule sWidgetDragServiceLog("WidgetDragService");
62 #define DRAGIMAGES_PREF "nglayout.enable_drag_images"
64 nsBaseDragService::nsBaseDragService()
65 : mCanDrop(false),
66 mOnlyChromeDrop(false),
67 mDoingDrag(false),
68 mSessionIsSynthesizedForTests(false),
69 mIsDraggingTextInTextControl(false),
70 mEndingSession(false),
71 mHasImage(false),
72 mUserCancelled(false),
73 mDragEventDispatchedToChildProcess(false),
74 mDragAction(DRAGDROP_ACTION_NONE),
75 mDragActionFromChildProcess(DRAGDROP_ACTION_UNINITIALIZED),
76 mEffectAllowedForTests(DRAGDROP_ACTION_UNINITIALIZED),
77 mContentPolicyType(nsIContentPolicy::TYPE_OTHER),
78 mSuppressLevel(0),
79 mInputSource(MouseEvent_Binding::MOZ_SOURCE_MOUSE) {}
81 nsBaseDragService::~nsBaseDragService() = default;
83 NS_IMPL_ISUPPORTS(nsBaseDragService, nsIDragService, nsIDragSession)
85 //---------------------------------------------------------
86 NS_IMETHODIMP
87 nsBaseDragService::SetCanDrop(bool aCanDrop) {
88 mCanDrop = aCanDrop;
89 return NS_OK;
92 //---------------------------------------------------------
93 NS_IMETHODIMP
94 nsBaseDragService::GetCanDrop(bool* aCanDrop) {
95 *aCanDrop = mCanDrop;
96 return NS_OK;
98 //---------------------------------------------------------
99 NS_IMETHODIMP
100 nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome) {
101 mOnlyChromeDrop = aOnlyChrome;
102 return NS_OK;
105 //---------------------------------------------------------
106 NS_IMETHODIMP
107 nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome) {
108 *aOnlyChrome = mOnlyChromeDrop;
109 return NS_OK;
112 //---------------------------------------------------------
113 NS_IMETHODIMP
114 nsBaseDragService::SetDragAction(uint32_t anAction) {
115 mDragAction = anAction;
116 return NS_OK;
119 //---------------------------------------------------------
120 NS_IMETHODIMP
121 nsBaseDragService::GetDragAction(uint32_t* anAction) {
122 *anAction = mDragAction;
123 return NS_OK;
126 //-------------------------------------------------------------------------
128 NS_IMETHODIMP
129 nsBaseDragService::GetNumDropItems(uint32_t* aNumItems) {
130 *aNumItems = 0;
131 return NS_ERROR_FAILURE;
135 // GetSourceWindowContext
137 // Returns the window context where the drag was initiated. This will be
138 // nullptr if the drag began outside of our application.
140 NS_IMETHODIMP
141 nsBaseDragService::GetSourceWindowContext(
142 WindowContext** aSourceWindowContext) {
143 *aSourceWindowContext = mSourceWindowContext.get();
144 NS_IF_ADDREF(*aSourceWindowContext);
145 return NS_OK;
148 NS_IMETHODIMP
149 nsBaseDragService::SetSourceWindowContext(WindowContext* aSourceWindowContext) {
150 // This should only be called in a child process.
151 MOZ_ASSERT(!XRE_IsParentProcess());
152 mSourceWindowContext = aSourceWindowContext;
153 return NS_OK;
157 // GetSourceTopWindowContext
159 // Returns the top-level window context where the drag was initiated. This will
160 // be nullptr if the drag began outside of our application.
162 NS_IMETHODIMP
163 nsBaseDragService::GetSourceTopWindowContext(
164 WindowContext** aSourceTopWindowContext) {
165 *aSourceTopWindowContext = mSourceTopWindowContext.get();
166 NS_IF_ADDREF(*aSourceTopWindowContext);
167 return NS_OK;
170 NS_IMETHODIMP
171 nsBaseDragService::SetSourceTopWindowContext(
172 WindowContext* aSourceTopWindowContext) {
173 // This should only be called in a child process.
174 MOZ_ASSERT(!XRE_IsParentProcess());
175 mSourceTopWindowContext = aSourceTopWindowContext;
176 return NS_OK;
180 // GetSourceNode
182 // Returns the DOM node where the drag was initiated. This will be
183 // nullptr if the drag began outside of our application.
185 NS_IMETHODIMP
186 nsBaseDragService::GetSourceNode(nsINode** aSourceNode) {
187 *aSourceNode = do_AddRef(mSourceNode).take();
188 return NS_OK;
191 void nsBaseDragService::UpdateSource(nsINode* aNewSourceNode,
192 Selection* aNewSelection) {
193 MOZ_ASSERT(mSourceNode);
194 MOZ_ASSERT(aNewSourceNode);
195 MOZ_ASSERT(mSourceNode->IsInNativeAnonymousSubtree() ||
196 aNewSourceNode->IsInNativeAnonymousSubtree());
197 MOZ_ASSERT(mSourceDocument == aNewSourceNode->OwnerDoc());
198 mSourceNode = aNewSourceNode;
199 // Don't set mSelection if the session was invoked without selection or
200 // making it becomes nullptr. The latter occurs when the old frame is
201 // being destroyed.
202 if (mSelection && aNewSelection) {
203 // XXX If the dragging image is created once (e.g., at drag start), the
204 // image won't be updated unless we notify `DrawDrag` callers.
205 // However, it must be okay for now to keep using older image of
206 // Selection.
207 mSelection = aNewSelection;
211 NS_IMETHODIMP
212 nsBaseDragService::GetTriggeringPrincipal(nsIPrincipal** aPrincipal) {
213 NS_IF_ADDREF(*aPrincipal = mTriggeringPrincipal);
214 return NS_OK;
217 NS_IMETHODIMP
218 nsBaseDragService::SetTriggeringPrincipal(nsIPrincipal* aPrincipal) {
219 mTriggeringPrincipal = aPrincipal;
220 return NS_OK;
223 NS_IMETHODIMP
224 nsBaseDragService::GetCsp(nsIContentSecurityPolicy** aCsp) {
225 NS_IF_ADDREF(*aCsp = mCsp);
226 return NS_OK;
229 NS_IMETHODIMP
230 nsBaseDragService::SetCsp(nsIContentSecurityPolicy* aCsp) {
231 mCsp = aCsp;
232 return NS_OK;
235 //-------------------------------------------------------------------------
237 NS_IMETHODIMP
238 nsBaseDragService::GetData(nsITransferable* aTransferable,
239 uint32_t aItemIndex) {
240 return NS_ERROR_FAILURE;
243 //-------------------------------------------------------------------------
244 NS_IMETHODIMP
245 nsBaseDragService::IsDataFlavorSupported(const char* aDataFlavor,
246 bool* _retval) {
247 return NS_ERROR_FAILURE;
250 NS_IMETHODIMP
251 nsBaseDragService::GetDataTransferXPCOM(DataTransfer** aDataTransfer) {
252 *aDataTransfer = mDataTransfer;
253 NS_IF_ADDREF(*aDataTransfer);
254 return NS_OK;
257 NS_IMETHODIMP
258 nsBaseDragService::SetDataTransferXPCOM(DataTransfer* aDataTransfer) {
259 NS_ENSURE_STATE(aDataTransfer);
260 mDataTransfer = aDataTransfer;
261 return NS_OK;
264 DataTransfer* nsBaseDragService::GetDataTransfer() { return mDataTransfer; }
266 void nsBaseDragService::SetDataTransfer(DataTransfer* aDataTransfer) {
267 mDataTransfer = aDataTransfer;
270 bool nsBaseDragService::IsSynthesizedForTests() {
271 return mSessionIsSynthesizedForTests;
274 bool nsBaseDragService::IsDraggingTextInTextControl() {
275 return mIsDraggingTextInTextControl;
278 uint32_t nsBaseDragService::GetEffectAllowedForTests() {
279 MOZ_ASSERT(mSessionIsSynthesizedForTests);
280 return mEffectAllowedForTests;
283 NS_IMETHODIMP nsBaseDragService::SetDragEndPointForTests(int32_t aScreenX,
284 int32_t aScreenY) {
285 MOZ_ASSERT(mDoingDrag);
286 MOZ_ASSERT(mSourceDocument);
287 MOZ_ASSERT(mSessionIsSynthesizedForTests);
289 if (!mDoingDrag || !mSourceDocument || !mSessionIsSynthesizedForTests) {
290 return NS_ERROR_FAILURE;
292 nsPresContext* pc = mSourceDocument->GetPresContext();
293 if (NS_WARN_IF(!pc)) {
294 return NS_ERROR_FAILURE;
296 auto p = LayoutDeviceIntPoint::Round(CSSIntPoint(aScreenX, aScreenY) *
297 pc->CSSToDevPixelScale());
298 // p is screen-relative, and we want them to be top-level-widget-relative.
299 if (nsCOMPtr<nsIWidget> widget = pc->GetRootWidget()) {
300 p -= widget->WidgetToScreenOffset();
301 p += widget->WidgetToTopLevelWidgetOffset();
303 SetDragEndPoint(p);
304 return NS_OK;
307 //-------------------------------------------------------------------------
308 NS_IMETHODIMP
309 nsBaseDragService::InvokeDragSession(
310 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCsp,
311 nsICookieJarSettings* aCookieJarSettings, nsIArray* aTransferableArray,
312 uint32_t aActionType,
313 nsContentPolicyType aContentPolicyType = nsIContentPolicy::TYPE_OTHER) {
314 AUTO_PROFILER_LABEL("nsBaseDragService::InvokeDragSession", OTHER);
316 NS_ENSURE_TRUE(aDOMNode, NS_ERROR_INVALID_ARG);
317 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
319 // stash the document of the dom node
320 mSourceDocument = aDOMNode->OwnerDoc();
321 mTriggeringPrincipal = aPrincipal;
322 mCsp = aCsp;
323 mSourceNode = aDOMNode;
324 mIsDraggingTextInTextControl =
325 mSourceNode->IsInNativeAnonymousSubtree() &&
326 TextControlElement::FromNodeOrNull(
327 mSourceNode->GetClosestNativeAnonymousSubtreeRootParentOrHost());
328 mContentPolicyType = aContentPolicyType;
329 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
331 // When the mouse goes down, the selection code starts a mouse
332 // capture. However, this gets in the way of determining drag
333 // feedback for things like trees because the event coordinates
334 // are in the wrong coord system, so turn off mouse capture.
335 PresShell::ClearMouseCapture();
337 if (mSessionIsSynthesizedForTests) {
338 mDoingDrag = true;
339 mDragAction = aActionType;
340 mEffectAllowedForTests = aActionType;
341 return NS_OK;
344 // If you're hitting this, a test is causing the browser to attempt to enter
345 // the drag-drop native nested event loop, which will put the browser in a
346 // state that won't run tests properly until there's manual intervention
347 // to exit the drag-drop loop (either by moving the mouse or hitting escape),
348 // which can't be done from script since we're in the nested loop.
350 // The best way to avoid this is to catch the dragstart event on the item
351 // being dragged, and then to call preventDefault() and stopPropagating() on
352 // it.
353 if (XRE_IsParentProcess()) {
354 MOZ_ASSERT(
355 !xpc::IsInAutomation(),
356 "About to start drag-drop native loop on which will prevent later "
357 "tests from running properly.");
360 uint32_t length = 0;
361 mozilla::Unused << aTransferableArray->GetLength(&length);
362 if (!length) {
363 nsCOMPtr<nsIMutableArray> mutableArray =
364 do_QueryInterface(aTransferableArray);
365 if (mutableArray) {
366 // In order to be able trigger dnd, we need to have some transferable
367 // object.
368 nsCOMPtr<nsITransferable> trans =
369 do_CreateInstance("@mozilla.org/widget/transferable;1");
370 trans->Init(nullptr);
371 trans->SetRequestingPrincipal(mSourceNode->NodePrincipal());
372 trans->SetContentPolicyType(mContentPolicyType);
373 trans->SetCookieJarSettings(aCookieJarSettings);
374 mutableArray->AppendElement(trans);
376 } else {
377 for (uint32_t i = 0; i < length; ++i) {
378 nsCOMPtr<nsITransferable> trans =
379 do_QueryElementAt(aTransferableArray, i);
380 if (trans) {
381 // Set the requestingPrincipal on the transferable.
382 trans->SetRequestingPrincipal(mSourceNode->NodePrincipal());
383 trans->SetContentPolicyType(mContentPolicyType);
384 trans->SetCookieJarSettings(aCookieJarSettings);
389 nsresult rv = InvokeDragSessionImpl(aTransferableArray, mRegion, aActionType);
391 if (NS_FAILED(rv)) {
392 // Set mDoingDrag so that EndDragSession cleans up and sends the dragend
393 // event after the aborted drag.
394 mDoingDrag = true;
395 EndDragSession(true, 0);
398 return rv;
401 NS_IMETHODIMP
402 nsBaseDragService::InvokeDragSessionWithImage(
403 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCsp,
404 nsICookieJarSettings* aCookieJarSettings, nsIArray* aTransferableArray,
405 uint32_t aActionType, nsINode* aImage, int32_t aImageX, int32_t aImageY,
406 DragEvent* aDragEvent, DataTransfer* aDataTransfer) {
407 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
408 NS_ENSURE_TRUE(aDataTransfer, NS_ERROR_NULL_POINTER);
409 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
411 mSessionIsSynthesizedForTests =
412 aDragEvent->WidgetEventPtr()->mFlags.mIsSynthesizedForTests;
413 mDataTransfer = aDataTransfer;
414 mSelection = nullptr;
415 mHasImage = true;
416 mDragPopup = nullptr;
417 mImage = aImage;
418 mImageOffset = CSSIntPoint(aImageX, aImageY);
419 mDragStartData = nullptr;
420 mSourceWindowContext =
421 aDOMNode ? aDOMNode->OwnerDoc()->GetWindowContext() : nullptr;
422 mSourceTopWindowContext =
423 mSourceWindowContext ? mSourceWindowContext->TopWindowContext() : nullptr;
425 mScreenPosition = aDragEvent->ScreenPoint(CallerType::System);
426 mInputSource = aDragEvent->InputSource();
428 // If dragging within a XUL tree and no custom drag image was
429 // set, the region argument to InvokeDragSessionWithImage needs
430 // to be set to the area encompassing the selected rows of the
431 // tree to ensure that the drag feedback gets clipped to those
432 // rows. For other content, region should be null.
433 mRegion = Nothing();
434 if (aDOMNode && aDOMNode->IsContent() && !aImage) {
435 if (aDOMNode->NodeInfo()->Equals(nsGkAtoms::treechildren,
436 kNameSpaceID_XUL)) {
437 nsTreeBodyFrame* treeBody =
438 do_QueryFrame(aDOMNode->AsContent()->GetPrimaryFrame());
439 if (treeBody) {
440 mRegion = treeBody->GetSelectionRegion();
445 nsresult rv = InvokeDragSession(
446 aDOMNode, aPrincipal, aCsp, aCookieJarSettings, aTransferableArray,
447 aActionType, nsIContentPolicy::TYPE_INTERNAL_IMAGE);
448 mRegion = Nothing();
449 return rv;
452 NS_IMETHODIMP
453 nsBaseDragService::InvokeDragSessionWithRemoteImage(
454 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCsp,
455 nsICookieJarSettings* aCookieJarSettings, nsIArray* aTransferableArray,
456 uint32_t aActionType, RemoteDragStartData* aDragStartData,
457 DragEvent* aDragEvent, DataTransfer* aDataTransfer) {
458 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
459 NS_ENSURE_TRUE(aDataTransfer, NS_ERROR_NULL_POINTER);
460 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
462 mSessionIsSynthesizedForTests =
463 aDragEvent->WidgetEventPtr()->mFlags.mIsSynthesizedForTests;
464 mDataTransfer = aDataTransfer;
465 mSelection = nullptr;
466 mHasImage = true;
467 mDragPopup = nullptr;
468 mImage = nullptr;
469 mDragStartData = aDragStartData;
470 mImageOffset = CSSIntPoint(0, 0);
471 mSourceWindowContext = mDragStartData->GetSourceWindowContext();
472 mSourceTopWindowContext = mDragStartData->GetSourceTopWindowContext();
474 mScreenPosition = aDragEvent->ScreenPoint(CallerType::System);
475 mInputSource = aDragEvent->InputSource();
477 nsresult rv = InvokeDragSession(
478 aDOMNode, aPrincipal, aCsp, aCookieJarSettings, aTransferableArray,
479 aActionType, nsIContentPolicy::TYPE_INTERNAL_IMAGE);
480 mRegion = Nothing();
481 return rv;
484 NS_IMETHODIMP
485 nsBaseDragService::InvokeDragSessionWithSelection(
486 Selection* aSelection, nsIPrincipal* aPrincipal,
487 nsIContentSecurityPolicy* aCsp, nsICookieJarSettings* aCookieJarSettings,
488 nsIArray* aTransferableArray, uint32_t aActionType, DragEvent* aDragEvent,
489 DataTransfer* aDataTransfer) {
490 NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
491 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
492 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
494 mSessionIsSynthesizedForTests =
495 aDragEvent->WidgetEventPtr()->mFlags.mIsSynthesizedForTests;
496 mDataTransfer = aDataTransfer;
497 mSelection = aSelection;
498 mHasImage = true;
499 mDragPopup = nullptr;
500 mImage = nullptr;
501 mImageOffset = CSSIntPoint();
502 mDragStartData = nullptr;
503 mRegion = Nothing();
505 mScreenPosition.x = aDragEvent->ScreenX(CallerType::System);
506 mScreenPosition.y = aDragEvent->ScreenY(CallerType::System);
507 mInputSource = aDragEvent->InputSource();
509 // just get the focused node from the selection
510 // XXXndeakin this should actually be the deepest node that contains both
511 // endpoints of the selection
512 nsCOMPtr<nsINode> node = aSelection->GetFocusNode();
513 mSourceWindowContext = node ? node->OwnerDoc()->GetWindowContext() : nullptr;
514 mSourceTopWindowContext =
515 mSourceWindowContext ? mSourceWindowContext->TopWindowContext() : nullptr;
517 return InvokeDragSession(node, aPrincipal, aCsp, aCookieJarSettings,
518 aTransferableArray, aActionType,
519 nsIContentPolicy::TYPE_OTHER);
522 //-------------------------------------------------------------------------
523 NS_IMETHODIMP
524 nsBaseDragService::GetCurrentSession(nsIDragSession** aSession) {
525 if (!aSession) return NS_ERROR_INVALID_ARG;
527 // "this" also implements a drag session, so say we are one but only
528 // if there is currently a drag going on.
529 if (!mSuppressLevel && mDoingDrag) {
530 *aSession = this;
531 NS_ADDREF(*aSession); // addRef because we're a "getter"
532 } else
533 *aSession = nullptr;
535 return NS_OK;
538 //-------------------------------------------------------------------------
539 NS_IMETHODIMP
540 nsBaseDragService::StartDragSession() {
541 if (mDoingDrag) {
542 return NS_ERROR_FAILURE;
544 mDoingDrag = true;
545 // By default dispatch drop also to content.
546 mOnlyChromeDrop = false;
548 return NS_OK;
551 NS_IMETHODIMP nsBaseDragService::StartDragSessionForTests(
552 uint32_t aAllowedEffect) {
553 if (NS_WARN_IF(NS_FAILED(StartDragSession()))) {
554 return NS_ERROR_FAILURE;
556 mDragAction = aAllowedEffect;
557 mEffectAllowedForTests = aAllowedEffect;
558 mSessionIsSynthesizedForTests = true;
559 return NS_OK;
562 void nsBaseDragService::OpenDragPopup() {
563 if (mDragPopup) {
564 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
565 if (pm) {
566 pm->ShowPopupAtScreen(mDragPopup, mScreenPosition.x - mImageOffset.x,
567 mScreenPosition.y - mImageOffset.y, false, nullptr);
572 int32_t nsBaseDragService::TakeChildProcessDragAction() {
573 // If the last event was dispatched to the child process, use the drag action
574 // assigned from it instead and return it. DRAGDROP_ACTION_UNINITIALIZED is
575 // returned otherwise.
576 int32_t retval = DRAGDROP_ACTION_UNINITIALIZED;
577 if (TakeDragEventDispatchedToChildProcess() &&
578 mDragActionFromChildProcess != DRAGDROP_ACTION_UNINITIALIZED) {
579 retval = mDragActionFromChildProcess;
582 return retval;
585 //-------------------------------------------------------------------------
586 NS_IMETHODIMP
587 nsBaseDragService::EndDragSession(bool aDoneDrag, uint32_t aKeyModifiers) {
588 if (!mDoingDrag || mEndingSession) {
589 return NS_ERROR_FAILURE;
592 mEndingSession = true;
594 if (aDoneDrag && !mSuppressLevel) {
595 FireDragEventAtSource(eDragEnd, aKeyModifiers);
598 if (mDragPopup) {
599 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
600 if (pm) {
601 pm->HidePopup(mDragPopup, {HidePopupOption::DeselectMenu});
605 uint32_t dropEffect = nsIDragService::DRAGDROP_ACTION_NONE;
606 if (mDataTransfer) {
607 dropEffect = mDataTransfer->DropEffectInt();
610 for (uint32_t i = 0; i < mChildProcesses.Length(); ++i) {
611 mozilla::Unused << mChildProcesses[i]->SendEndDragSession(
612 aDoneDrag, mUserCancelled, mEndDragPoint, aKeyModifiers, dropEffect);
613 // Continue sending input events with input priority when stopping the dnd
614 // session.
615 mChildProcesses[i]->SetInputPriorityEventEnabled(true);
617 mChildProcesses.Clear();
619 // mDataTransfer and the items it owns are going to die anyway, but we
620 // explicitly deref the contained data here so that we don't have to wait for
621 // CC to reclaim the memory.
622 if (XRE_IsParentProcess()) {
623 DiscardInternalTransferData();
626 mDoingDrag = false;
627 mSessionIsSynthesizedForTests = false;
628 mIsDraggingTextInTextControl = false;
629 mEffectAllowedForTests = nsIDragService::DRAGDROP_ACTION_UNINITIALIZED;
630 mEndingSession = false;
631 mCanDrop = false;
633 // release the source we've been holding on to.
634 mSourceDocument = nullptr;
635 mSourceNode = nullptr;
636 mSourceWindowContext = nullptr;
637 mTriggeringPrincipal = nullptr;
638 mCsp = nullptr;
639 mSelection = nullptr;
640 mDataTransfer = nullptr;
641 mHasImage = false;
642 mUserCancelled = false;
643 mDragPopup = nullptr;
644 mDragStartData = nullptr;
645 mImage = nullptr;
646 mImageOffset = CSSIntPoint();
647 mScreenPosition = CSSIntPoint();
648 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
649 mInputSource = MouseEvent_Binding::MOZ_SOURCE_MOUSE;
650 mRegion = Nothing();
652 return NS_OK;
655 void nsBaseDragService::DiscardInternalTransferData() {
656 if (mDataTransfer && mSourceNode) {
657 MOZ_ASSERT(mDataTransfer);
659 DataTransferItemList* items = mDataTransfer->Items();
660 for (size_t i = 0; i < items->Length(); i++) {
661 bool found;
662 DataTransferItem* item = items->IndexedGetter(i, found);
664 // Non-OTHER items may still be needed by JS. Skip them.
665 if (!found || item->Kind() != DataTransferItem::KIND_OTHER) {
666 continue;
669 nsCOMPtr<nsIVariant> variant = item->DataNoSecurityCheck();
670 nsCOMPtr<nsIWritableVariant> writable = do_QueryInterface(variant);
672 if (writable) {
673 writable->SetAsEmpty();
679 NS_IMETHODIMP
680 nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage,
681 uint32_t aKeyModifiers) {
682 if (!mSourceNode || !mSourceDocument || mSuppressLevel) {
683 return NS_OK;
685 RefPtr<PresShell> presShell = mSourceDocument->GetPresShell();
686 if (!presShell) {
687 return NS_OK;
690 RefPtr<nsPresContext> pc = presShell->GetPresContext();
691 nsCOMPtr<nsIWidget> widget = pc ? pc->GetRootWidget() : nullptr;
693 nsEventStatus status = nsEventStatus_eIgnore;
694 WidgetDragEvent event(true, aEventMessage, widget);
695 event.mFlags.mIsSynthesizedForTests = mSessionIsSynthesizedForTests;
696 event.mInputSource = mInputSource;
697 if (aEventMessage == eDragEnd) {
698 event.mRefPoint = mEndDragPoint;
699 if (widget) {
700 event.mRefPoint -= widget->WidgetToTopLevelWidgetOffset();
702 event.mUserCancelled = mUserCancelled;
704 event.mModifiers = aKeyModifiers;
706 // Most drag events aren't able to converted to MouseEvent except to
707 // eDragStart and eDragEnd.
708 if (widget && event.CanConvertToInputData()) {
709 // Send the drag event to APZ, which needs to know about them to be
710 // able to accurately detect the end of a drag gesture.
711 widget->DispatchEventToAPZOnly(&event);
714 nsCOMPtr<nsIContent> content = do_QueryInterface(mSourceNode);
715 return presShell->HandleDOMEventWithTarget(content, &event, &status);
718 /* This is used by Windows and Mac to update the position of a popup being
719 * used as a drag image during the drag. This isn't used on GTK as it manages
720 * the drag popup itself.
722 NS_IMETHODIMP
723 nsBaseDragService::DragMoved(int32_t aX, int32_t aY) {
724 if (mDragPopup) {
725 nsIFrame* frame = mDragPopup->GetPrimaryFrame();
726 if (frame && frame->IsMenuPopupFrame()) {
727 CSSIntPoint cssPos =
728 RoundedToInt(LayoutDeviceIntPoint(aX, aY) /
729 frame->PresContext()->CSSToDevPixelScale()) -
730 mImageOffset;
731 static_cast<nsMenuPopupFrame*>(frame)->MoveTo(cssPos, true);
735 return NS_OK;
738 static PresShell* GetPresShellForContent(nsINode* aDOMNode) {
739 nsCOMPtr<nsIContent> content = do_QueryInterface(aDOMNode);
740 if (!content) return nullptr;
742 RefPtr<Document> document = content->GetComposedDoc();
743 if (document) {
744 document->FlushPendingNotifications(FlushType::Display);
745 return document->GetPresShell();
748 return nullptr;
751 nsresult nsBaseDragService::DrawDrag(nsINode* aDOMNode,
752 const Maybe<CSSIntRegion>& aRegion,
753 CSSIntPoint aScreenPosition,
754 LayoutDeviceIntRect* aScreenDragRect,
755 RefPtr<SourceSurface>* aSurface,
756 nsPresContext** aPresContext) {
757 *aSurface = nullptr;
758 *aPresContext = nullptr;
760 // use a default size, in case of an error.
761 aScreenDragRect->SetRect(aScreenPosition.x - mImageOffset.x,
762 aScreenPosition.y - mImageOffset.y, 1, 1);
764 // if a drag image was specified, use that, otherwise, use the source node
765 nsCOMPtr<nsINode> dragNode = mImage ? mImage.get() : aDOMNode;
767 // get the presshell for the node being dragged. If the drag image is not in
768 // a document or has no frame, get the presshell from the source drag node
769 PresShell* presShell = GetPresShellForContent(dragNode);
770 if (!presShell && mImage) {
771 presShell = GetPresShellForContent(aDOMNode);
773 if (!presShell) {
774 return NS_ERROR_FAILURE;
777 *aPresContext = presShell->GetPresContext();
779 if (mDragStartData) {
780 if (mImage) {
781 // Just clear the surface if chrome has overridden it with an image.
782 *aSurface = nullptr;
783 } else {
784 *aSurface = mDragStartData->TakeVisualization(aScreenDragRect);
787 mDragStartData = nullptr;
788 return NS_OK;
791 // convert mouse position to dev pixels of the prescontext
792 const CSSIntPoint screenPosition = aScreenPosition - mImageOffset;
793 const auto screenPoint = LayoutDeviceIntPoint::Round(
794 screenPosition * (*aPresContext)->CSSToDevPixelScale());
795 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
797 // check if drag images are disabled
798 bool enableDragImages = Preferences::GetBool(DRAGIMAGES_PREF, true);
800 // didn't want an image, so just set the screen rectangle to the frame size
801 if (!enableDragImages || !mHasImage) {
802 // This holds a quantity in RelativeTo{presShell->GetRootFrame(),
803 // ViewportType::Layout} space.
804 nsRect presLayoutRect;
805 if (aRegion) {
806 // if a region was specified, set the screen rectangle to the area that
807 // the region occupies
808 presLayoutRect = ToAppUnits(aRegion->GetBounds(), AppUnitsPerCSSPixel());
809 } else {
810 // otherwise, there was no region so just set the rectangle to
811 // the size of the primary frame of the content.
812 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
813 if (nsIFrame* frame = content->GetPrimaryFrame()) {
814 presLayoutRect = frame->GetBoundingClientRect();
818 LayoutDeviceRect screenVisualRect = ViewportUtils::ToScreenRelativeVisual(
819 LayoutDeviceRect::FromAppUnits(presLayoutRect,
820 (*aPresContext)->AppUnitsPerDevPixel()),
821 *aPresContext);
822 aScreenDragRect->SizeTo(screenVisualRect.Width(),
823 screenVisualRect.Height());
824 return NS_OK;
827 // draw the image for selections
828 if (mSelection) {
829 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
830 *aSurface = presShell->RenderSelection(
831 mSelection, pnt, aScreenDragRect,
832 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale);
833 return NS_OK;
836 // if a custom image was specified, check if it is an image node and draw
837 // using the source rather than the displayed image. But if mImage isn't
838 // an image or canvas, fall through to RenderNode below.
839 if (mImage) {
840 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
841 HTMLCanvasElement* canvas = HTMLCanvasElement::FromNodeOrNull(content);
842 if (canvas) {
843 return DrawDragForImage(*aPresContext, nullptr, canvas, aScreenDragRect,
844 aSurface);
847 nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(dragNode);
848 // for image nodes, create the drag image from the actual image data
849 if (imageLoader) {
850 return DrawDragForImage(*aPresContext, imageLoader, nullptr,
851 aScreenDragRect, aSurface);
854 // If the image is a popup, use that as the image. This allows custom drag
855 // images that can change during the drag, but means that any platform
856 // default image handling won't occur.
857 // XXXndeakin this should be chrome-only
859 nsIFrame* frame = content->GetPrimaryFrame();
860 if (frame && frame->IsMenuPopupFrame()) {
861 mDragPopup = content->AsElement();
865 if (!mDragPopup) {
866 // otherwise, just draw the node
867 RenderImageFlags renderFlags =
868 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale;
869 if (renderFlags != RenderImageFlags::None) {
870 // check if the dragged node itself is an img element
871 if (dragNode->NodeName().LowerCaseEqualsLiteral("img")) {
872 renderFlags = renderFlags | RenderImageFlags::IsImage;
873 } else {
874 nsINodeList* childList = dragNode->ChildNodes();
875 uint32_t length = childList->Length();
876 // check every childnode for being an img element
877 // XXXbz why don't we need to check descendants recursively?
878 for (uint32_t count = 0; count < length; ++count) {
879 if (childList->Item(count)->NodeName().LowerCaseEqualsLiteral(
880 "img")) {
881 // if the dragnode contains an image, set RenderImageFlags::IsImage
882 // flag
883 renderFlags = renderFlags | RenderImageFlags::IsImage;
884 break;
889 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
890 *aSurface = presShell->RenderNode(dragNode, aRegion, pnt, aScreenDragRect,
891 renderFlags);
894 // If an image was specified, reset the position from the offset that was
895 // supplied.
896 if (mImage) {
897 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
900 return NS_OK;
903 nsresult nsBaseDragService::DrawDragForImage(
904 nsPresContext* aPresContext, nsIImageLoadingContent* aImageLoader,
905 HTMLCanvasElement* aCanvas, LayoutDeviceIntRect* aScreenDragRect,
906 RefPtr<SourceSurface>* aSurface) {
907 nsCOMPtr<imgIContainer> imgContainer;
908 if (aImageLoader) {
909 nsCOMPtr<imgIRequest> imgRequest;
910 nsresult rv = aImageLoader->GetRequest(
911 nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(imgRequest));
912 NS_ENSURE_SUCCESS(rv, rv);
913 if (!imgRequest) return NS_ERROR_NOT_AVAILABLE;
915 rv = imgRequest->GetImage(getter_AddRefs(imgContainer));
916 NS_ENSURE_SUCCESS(rv, rv);
917 if (!imgContainer) return NS_ERROR_NOT_AVAILABLE;
919 // use the size of the image as the size of the drag image
920 int32_t imageWidth, imageHeight;
921 rv = imgContainer->GetWidth(&imageWidth);
922 NS_ENSURE_SUCCESS(rv, rv);
924 rv = imgContainer->GetHeight(&imageHeight);
925 NS_ENSURE_SUCCESS(rv, rv);
927 aScreenDragRect->SizeTo(aPresContext->CSSPixelsToDevPixels(imageWidth),
928 aPresContext->CSSPixelsToDevPixels(imageHeight));
929 } else {
930 // XXX The canvas size should be converted to dev pixels.
931 NS_ASSERTION(aCanvas, "both image and canvas are null");
932 nsIntSize sz = aCanvas->GetSize();
933 aScreenDragRect->SizeTo(sz.width, sz.height);
936 nsIntSize destSize;
937 destSize.width = aScreenDragRect->Width();
938 destSize.height = aScreenDragRect->Height();
939 if (destSize.width == 0 || destSize.height == 0) return NS_ERROR_FAILURE;
941 nsresult result = NS_OK;
942 if (aImageLoader) {
943 RefPtr<DrawTarget> dt =
944 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
945 destSize, SurfaceFormat::B8G8R8A8);
946 if (!dt || !dt->IsValid()) return NS_ERROR_FAILURE;
948 gfxContext ctx(dt);
950 ImgDrawResult res = imgContainer->Draw(
951 &ctx, destSize, ImageRegion::Create(destSize),
952 imgIContainer::FRAME_CURRENT, SamplingFilter::GOOD, SVGImageContext(),
953 imgIContainer::FLAG_SYNC_DECODE, 1.0);
954 if (res == ImgDrawResult::BAD_IMAGE || res == ImgDrawResult::BAD_ARGS ||
955 res == ImgDrawResult::NOT_SUPPORTED) {
956 return NS_ERROR_FAILURE;
958 *aSurface = dt->Snapshot();
959 } else {
960 *aSurface = aCanvas->GetSurfaceSnapshot();
963 return result;
966 NS_IMETHODIMP
967 nsBaseDragService::Suppress() {
968 EndDragSession(false, 0);
969 ++mSuppressLevel;
970 return NS_OK;
973 NS_IMETHODIMP
974 nsBaseDragService::Unsuppress() {
975 --mSuppressLevel;
976 return NS_OK;
979 NS_IMETHODIMP
980 nsBaseDragService::UserCancelled() {
981 mUserCancelled = true;
982 return NS_OK;
985 NS_IMETHODIMP
986 nsBaseDragService::UpdateDragEffect() {
987 mDragActionFromChildProcess = mDragAction;
988 return NS_OK;
991 NS_IMETHODIMP
992 nsBaseDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
993 int32_t aImageY) {
994 // Don't change the image if this is a drag from another source or if there
995 // is a drag popup.
996 if (!mSourceNode || mDragPopup) return NS_OK;
998 mImage = aImage;
999 mImageOffset = CSSIntPoint(aImageX, aImageY);
1000 return NS_OK;
1003 NS_IMETHODIMP
1004 nsBaseDragService::DragEventDispatchedToChildProcess() {
1005 mDragEventDispatchedToChildProcess = true;
1006 return NS_OK;
1009 bool nsBaseDragService::MaybeAddChildProcess(
1010 mozilla::dom::ContentParent* aChild) {
1011 if (!mChildProcesses.Contains(aChild)) {
1012 mChildProcesses.AppendElement(aChild);
1013 return true;
1015 return false;
1018 bool nsBaseDragService::RemoveAllChildProcesses() {
1019 for (uint32_t c = 0; c < mChildProcesses.Length(); c++) {
1020 mozilla::Unused << mChildProcesses[c]->SendEndDragSession(
1021 true, false, LayoutDeviceIntPoint(), 0,
1022 nsIDragService::DRAGDROP_ACTION_NONE);
1024 mChildProcesses.Clear();
1025 return true;
1028 bool nsBaseDragService::MustUpdateDataTransfer(EventMessage aMessage) {
1029 return false;
1032 NS_IMETHODIMP
1033 nsBaseDragService::MaybeEditorDeletedSourceNode(Element* aEditingHost) {
1034 // If builtin editor of Blink and WebKit deletes the source node,they retarget
1035 // the source node to the editing host.
1036 // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/page/drag_controller.cc;l=724;drc=d9ba13b8cd8ac0faed7afc3d1f7e4b67ebac2a0b
1037 // That allows editor apps listens to "dragend" event in editing host or its
1038 // ancestors. Therefore, we should follow them for compatibility.
1039 if (mSourceNode && !mSourceNode->IsInComposedDoc()) {
1040 mSourceNode = aEditingHost;
1042 return NS_OK;