Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / widget / nsBaseDragService.cpp
blob3960cbb4a668c6bee51e4cf91dde89a25c6050ee
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 if (widget) {
707 // Send the drag event to APZ, which needs to know about them to be
708 // able to accurately detect the end of a drag gesture.
709 widget->DispatchEventToAPZOnly(&event);
712 nsCOMPtr<nsIContent> content = do_QueryInterface(mSourceNode);
713 return presShell->HandleDOMEventWithTarget(content, &event, &status);
716 /* This is used by Windows and Mac to update the position of a popup being
717 * used as a drag image during the drag. This isn't used on GTK as it manages
718 * the drag popup itself.
720 NS_IMETHODIMP
721 nsBaseDragService::DragMoved(int32_t aX, int32_t aY) {
722 if (mDragPopup) {
723 nsIFrame* frame = mDragPopup->GetPrimaryFrame();
724 if (frame && frame->IsMenuPopupFrame()) {
725 CSSIntPoint cssPos =
726 RoundedToInt(LayoutDeviceIntPoint(aX, aY) /
727 frame->PresContext()->CSSToDevPixelScale()) -
728 mImageOffset;
729 static_cast<nsMenuPopupFrame*>(frame)->MoveTo(cssPos, true);
733 return NS_OK;
736 static PresShell* GetPresShellForContent(nsINode* aDOMNode) {
737 nsCOMPtr<nsIContent> content = do_QueryInterface(aDOMNode);
738 if (!content) return nullptr;
740 RefPtr<Document> document = content->GetComposedDoc();
741 if (document) {
742 document->FlushPendingNotifications(FlushType::Display);
743 return document->GetPresShell();
746 return nullptr;
749 nsresult nsBaseDragService::DrawDrag(nsINode* aDOMNode,
750 const Maybe<CSSIntRegion>& aRegion,
751 CSSIntPoint aScreenPosition,
752 LayoutDeviceIntRect* aScreenDragRect,
753 RefPtr<SourceSurface>* aSurface,
754 nsPresContext** aPresContext) {
755 *aSurface = nullptr;
756 *aPresContext = nullptr;
758 // use a default size, in case of an error.
759 aScreenDragRect->SetRect(aScreenPosition.x - mImageOffset.x,
760 aScreenPosition.y - mImageOffset.y, 1, 1);
762 // if a drag image was specified, use that, otherwise, use the source node
763 nsCOMPtr<nsINode> dragNode = mImage ? mImage.get() : aDOMNode;
765 // get the presshell for the node being dragged. If the drag image is not in
766 // a document or has no frame, get the presshell from the source drag node
767 PresShell* presShell = GetPresShellForContent(dragNode);
768 if (!presShell && mImage) {
769 presShell = GetPresShellForContent(aDOMNode);
771 if (!presShell) {
772 return NS_ERROR_FAILURE;
775 *aPresContext = presShell->GetPresContext();
777 if (mDragStartData) {
778 if (mImage) {
779 // Just clear the surface if chrome has overridden it with an image.
780 *aSurface = nullptr;
781 } else {
782 *aSurface = mDragStartData->TakeVisualization(aScreenDragRect);
785 mDragStartData = nullptr;
786 return NS_OK;
789 // convert mouse position to dev pixels of the prescontext
790 const CSSIntPoint screenPosition = aScreenPosition - mImageOffset;
791 const auto screenPoint = LayoutDeviceIntPoint::Round(
792 screenPosition * (*aPresContext)->CSSToDevPixelScale());
793 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
795 // check if drag images are disabled
796 bool enableDragImages = Preferences::GetBool(DRAGIMAGES_PREF, true);
798 // didn't want an image, so just set the screen rectangle to the frame size
799 if (!enableDragImages || !mHasImage) {
800 // This holds a quantity in RelativeTo{presShell->GetRootFrame(),
801 // ViewportType::Layout} space.
802 nsRect presLayoutRect;
803 if (aRegion) {
804 // if a region was specified, set the screen rectangle to the area that
805 // the region occupies
806 presLayoutRect = ToAppUnits(aRegion->GetBounds(), AppUnitsPerCSSPixel());
807 } else {
808 // otherwise, there was no region so just set the rectangle to
809 // the size of the primary frame of the content.
810 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
811 if (nsIFrame* frame = content->GetPrimaryFrame()) {
812 presLayoutRect = frame->GetBoundingClientRect();
816 LayoutDeviceRect screenVisualRect = ViewportUtils::ToScreenRelativeVisual(
817 LayoutDeviceRect::FromAppUnits(presLayoutRect,
818 (*aPresContext)->AppUnitsPerDevPixel()),
819 *aPresContext);
820 aScreenDragRect->SizeTo(screenVisualRect.Width(),
821 screenVisualRect.Height());
822 return NS_OK;
825 // draw the image for selections
826 if (mSelection) {
827 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
828 *aSurface = presShell->RenderSelection(
829 mSelection, pnt, aScreenDragRect,
830 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale);
831 return NS_OK;
834 // if a custom image was specified, check if it is an image node and draw
835 // using the source rather than the displayed image. But if mImage isn't
836 // an image or canvas, fall through to RenderNode below.
837 if (mImage) {
838 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
839 HTMLCanvasElement* canvas = HTMLCanvasElement::FromNodeOrNull(content);
840 if (canvas) {
841 return DrawDragForImage(*aPresContext, nullptr, canvas, aScreenDragRect,
842 aSurface);
845 nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(dragNode);
846 // for image nodes, create the drag image from the actual image data
847 if (imageLoader) {
848 return DrawDragForImage(*aPresContext, imageLoader, nullptr,
849 aScreenDragRect, aSurface);
852 // If the image is a popup, use that as the image. This allows custom drag
853 // images that can change during the drag, but means that any platform
854 // default image handling won't occur.
855 // XXXndeakin this should be chrome-only
857 nsIFrame* frame = content->GetPrimaryFrame();
858 if (frame && frame->IsMenuPopupFrame()) {
859 mDragPopup = content->AsElement();
863 if (!mDragPopup) {
864 // otherwise, just draw the node
865 RenderImageFlags renderFlags =
866 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale;
867 if (renderFlags != RenderImageFlags::None) {
868 // check if the dragged node itself is an img element
869 if (dragNode->NodeName().LowerCaseEqualsLiteral("img")) {
870 renderFlags = renderFlags | RenderImageFlags::IsImage;
871 } else {
872 nsINodeList* childList = dragNode->ChildNodes();
873 uint32_t length = childList->Length();
874 // check every childnode for being an img element
875 // XXXbz why don't we need to check descendants recursively?
876 for (uint32_t count = 0; count < length; ++count) {
877 if (childList->Item(count)->NodeName().LowerCaseEqualsLiteral(
878 "img")) {
879 // if the dragnode contains an image, set RenderImageFlags::IsImage
880 // flag
881 renderFlags = renderFlags | RenderImageFlags::IsImage;
882 break;
887 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
888 *aSurface = presShell->RenderNode(dragNode, aRegion, pnt, aScreenDragRect,
889 renderFlags);
892 // If an image was specified, reset the position from the offset that was
893 // supplied.
894 if (mImage) {
895 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
898 return NS_OK;
901 nsresult nsBaseDragService::DrawDragForImage(
902 nsPresContext* aPresContext, nsIImageLoadingContent* aImageLoader,
903 HTMLCanvasElement* aCanvas, LayoutDeviceIntRect* aScreenDragRect,
904 RefPtr<SourceSurface>* aSurface) {
905 nsCOMPtr<imgIContainer> imgContainer;
906 if (aImageLoader) {
907 nsCOMPtr<imgIRequest> imgRequest;
908 nsresult rv = aImageLoader->GetRequest(
909 nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(imgRequest));
910 NS_ENSURE_SUCCESS(rv, rv);
911 if (!imgRequest) return NS_ERROR_NOT_AVAILABLE;
913 rv = imgRequest->GetImage(getter_AddRefs(imgContainer));
914 NS_ENSURE_SUCCESS(rv, rv);
915 if (!imgContainer) return NS_ERROR_NOT_AVAILABLE;
917 // use the size of the image as the size of the drag image
918 int32_t imageWidth, imageHeight;
919 rv = imgContainer->GetWidth(&imageWidth);
920 NS_ENSURE_SUCCESS(rv, rv);
922 rv = imgContainer->GetHeight(&imageHeight);
923 NS_ENSURE_SUCCESS(rv, rv);
925 aScreenDragRect->SizeTo(aPresContext->CSSPixelsToDevPixels(imageWidth),
926 aPresContext->CSSPixelsToDevPixels(imageHeight));
927 } else {
928 // XXX The canvas size should be converted to dev pixels.
929 NS_ASSERTION(aCanvas, "both image and canvas are null");
930 nsIntSize sz = aCanvas->GetSize();
931 aScreenDragRect->SizeTo(sz.width, sz.height);
934 nsIntSize destSize;
935 destSize.width = aScreenDragRect->Width();
936 destSize.height = aScreenDragRect->Height();
937 if (destSize.width == 0 || destSize.height == 0) return NS_ERROR_FAILURE;
939 nsresult result = NS_OK;
940 if (aImageLoader) {
941 RefPtr<DrawTarget> dt =
942 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
943 destSize, SurfaceFormat::B8G8R8A8);
944 if (!dt || !dt->IsValid()) return NS_ERROR_FAILURE;
946 gfxContext ctx(dt);
948 ImgDrawResult res = imgContainer->Draw(
949 &ctx, destSize, ImageRegion::Create(destSize),
950 imgIContainer::FRAME_CURRENT, SamplingFilter::GOOD, SVGImageContext(),
951 imgIContainer::FLAG_SYNC_DECODE, 1.0);
952 if (res == ImgDrawResult::BAD_IMAGE || res == ImgDrawResult::BAD_ARGS ||
953 res == ImgDrawResult::NOT_SUPPORTED) {
954 return NS_ERROR_FAILURE;
956 *aSurface = dt->Snapshot();
957 } else {
958 *aSurface = aCanvas->GetSurfaceSnapshot();
961 return result;
964 NS_IMETHODIMP
965 nsBaseDragService::Suppress() {
966 EndDragSession(false, 0);
967 ++mSuppressLevel;
968 return NS_OK;
971 NS_IMETHODIMP
972 nsBaseDragService::Unsuppress() {
973 --mSuppressLevel;
974 return NS_OK;
977 NS_IMETHODIMP
978 nsBaseDragService::UserCancelled() {
979 mUserCancelled = true;
980 return NS_OK;
983 NS_IMETHODIMP
984 nsBaseDragService::UpdateDragEffect() {
985 mDragActionFromChildProcess = mDragAction;
986 return NS_OK;
989 NS_IMETHODIMP
990 nsBaseDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
991 int32_t aImageY) {
992 // Don't change the image if this is a drag from another source or if there
993 // is a drag popup.
994 if (!mSourceNode || mDragPopup) return NS_OK;
996 mImage = aImage;
997 mImageOffset = CSSIntPoint(aImageX, aImageY);
998 return NS_OK;
1001 NS_IMETHODIMP
1002 nsBaseDragService::DragEventDispatchedToChildProcess() {
1003 mDragEventDispatchedToChildProcess = true;
1004 return NS_OK;
1007 bool nsBaseDragService::MaybeAddChildProcess(
1008 mozilla::dom::ContentParent* aChild) {
1009 if (!mChildProcesses.Contains(aChild)) {
1010 mChildProcesses.AppendElement(aChild);
1011 return true;
1013 return false;
1016 bool nsBaseDragService::RemoveAllChildProcesses() {
1017 for (uint32_t c = 0; c < mChildProcesses.Length(); c++) {
1018 mozilla::Unused << mChildProcesses[c]->SendEndDragSession(
1019 true, false, LayoutDeviceIntPoint(), 0,
1020 nsIDragService::DRAGDROP_ACTION_NONE);
1022 mChildProcesses.Clear();
1023 return true;
1026 NS_IMETHODIMP
1027 nsBaseDragService::MaybeEditorDeletedSourceNode(Element* aEditingHost) {
1028 // If builtin editor of Blink and WebKit deletes the source node,they retarget
1029 // the source node to the editing host.
1030 // https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/page/drag_controller.cc;l=724;drc=d9ba13b8cd8ac0faed7afc3d1f7e4b67ebac2a0b
1031 // That allows editor apps listens to "dragend" event in editing host or its
1032 // ancestors. Therefore, we should follow them for compatibility.
1033 if (mSourceNode && !mSourceNode->IsInComposedDoc()) {
1034 mSourceNode = aEditingHost;
1036 return NS_OK;