Bug 1777562 [wpt PR 34663] - [FedCM] Rename FederatedCredential to IdentityCredential...
[gecko.git] / widget / nsBaseDragService.cpp
blob65d9cc1b647c46f6b037553ff4fc388b678597a0
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 <algorithm>
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()
62 : mCanDrop(false),
63 mOnlyChromeDrop(false),
64 mDoingDrag(false),
65 mSessionIsSynthesizedForTests(false),
66 mIsDraggingTextInTextControl(false),
67 mEndingSession(false),
68 mHasImage(false),
69 mUserCancelled(false),
70 mDragEventDispatchedToChildProcess(false),
71 mDragAction(DRAGDROP_ACTION_NONE),
72 mDragActionFromChildProcess(DRAGDROP_ACTION_UNINITIALIZED),
73 mEffectAllowedForTests(DRAGDROP_ACTION_UNINITIALIZED),
74 mContentPolicyType(nsIContentPolicy::TYPE_OTHER),
75 mSuppressLevel(0),
76 mInputSource(MouseEvent_Binding::MOZ_SOURCE_MOUSE) {}
78 nsBaseDragService::~nsBaseDragService() = default;
80 NS_IMPL_ISUPPORTS(nsBaseDragService, nsIDragService, nsIDragSession)
82 //---------------------------------------------------------
83 NS_IMETHODIMP
84 nsBaseDragService::SetCanDrop(bool aCanDrop) {
85 mCanDrop = aCanDrop;
86 return NS_OK;
89 //---------------------------------------------------------
90 NS_IMETHODIMP
91 nsBaseDragService::GetCanDrop(bool* aCanDrop) {
92 *aCanDrop = mCanDrop;
93 return NS_OK;
95 //---------------------------------------------------------
96 NS_IMETHODIMP
97 nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome) {
98 mOnlyChromeDrop = aOnlyChrome;
99 return NS_OK;
102 //---------------------------------------------------------
103 NS_IMETHODIMP
104 nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome) {
105 *aOnlyChrome = mOnlyChromeDrop;
106 return NS_OK;
109 //---------------------------------------------------------
110 NS_IMETHODIMP
111 nsBaseDragService::SetDragAction(uint32_t anAction) {
112 mDragAction = anAction;
113 return NS_OK;
116 //---------------------------------------------------------
117 NS_IMETHODIMP
118 nsBaseDragService::GetDragAction(uint32_t* anAction) {
119 *anAction = mDragAction;
120 return NS_OK;
123 //-------------------------------------------------------------------------
125 NS_IMETHODIMP
126 nsBaseDragService::GetNumDropItems(uint32_t* aNumItems) {
127 *aNumItems = 0;
128 return NS_ERROR_FAILURE;
132 // GetSourceWindowContext
134 // Returns the window context where the drag was initiated. This will be
135 // nullptr if the drag began outside of our application.
137 NS_IMETHODIMP
138 nsBaseDragService::GetSourceWindowContext(
139 WindowContext** aSourceWindowContext) {
140 *aSourceWindowContext = mSourceWindowContext.get();
141 NS_IF_ADDREF(*aSourceWindowContext);
142 return NS_OK;
145 NS_IMETHODIMP
146 nsBaseDragService::SetSourceWindowContext(WindowContext* aSourceWindowContext) {
147 // This should only be called in a child process.
148 MOZ_ASSERT(!XRE_IsParentProcess());
149 mSourceWindowContext = aSourceWindowContext;
150 return NS_OK;
154 // GetSourceNode
156 // Returns the DOM node where the drag was initiated. This will be
157 // nullptr if the drag began outside of our application.
159 NS_IMETHODIMP
160 nsBaseDragService::GetSourceNode(nsINode** aSourceNode) {
161 *aSourceNode = do_AddRef(mSourceNode).take();
162 return NS_OK;
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
175 // being destroyed.
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
180 // Selection.
181 mSelection = aNewSelection;
185 NS_IMETHODIMP
186 nsBaseDragService::GetTriggeringPrincipal(nsIPrincipal** aPrincipal) {
187 NS_IF_ADDREF(*aPrincipal = mTriggeringPrincipal);
188 return NS_OK;
191 NS_IMETHODIMP
192 nsBaseDragService::SetTriggeringPrincipal(nsIPrincipal* aPrincipal) {
193 mTriggeringPrincipal = aPrincipal;
194 return NS_OK;
197 NS_IMETHODIMP
198 nsBaseDragService::GetCsp(nsIContentSecurityPolicy** aCsp) {
199 NS_IF_ADDREF(*aCsp = mCsp);
200 return NS_OK;
203 NS_IMETHODIMP
204 nsBaseDragService::SetCsp(nsIContentSecurityPolicy* aCsp) {
205 mCsp = aCsp;
206 return NS_OK;
209 //-------------------------------------------------------------------------
211 NS_IMETHODIMP
212 nsBaseDragService::GetData(nsITransferable* aTransferable,
213 uint32_t aItemIndex) {
214 return NS_ERROR_FAILURE;
217 //-------------------------------------------------------------------------
218 NS_IMETHODIMP
219 nsBaseDragService::IsDataFlavorSupported(const char* aDataFlavor,
220 bool* _retval) {
221 return NS_ERROR_FAILURE;
224 NS_IMETHODIMP
225 nsBaseDragService::GetDataTransferXPCOM(DataTransfer** aDataTransfer) {
226 *aDataTransfer = mDataTransfer;
227 NS_IF_ADDREF(*aDataTransfer);
228 return NS_OK;
231 NS_IMETHODIMP
232 nsBaseDragService::SetDataTransferXPCOM(DataTransfer* aDataTransfer) {
233 NS_ENSURE_STATE(aDataTransfer);
234 mDataTransfer = aDataTransfer;
235 return NS_OK;
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,
258 int32_t aScreenY) {
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();
277 SetDragEndPoint(p);
278 return NS_OK;
281 //-------------------------------------------------------------------------
282 NS_IMETHODIMP
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;
296 mCsp = aCsp;
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) {
312 mDoingDrag = true;
313 mDragAction = aActionType;
314 mEffectAllowedForTests = aActionType;
315 return NS_OK;
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
326 // it.
327 if (XRE_IsParentProcess()) {
328 MOZ_ASSERT(
329 !xpc::IsInAutomation(),
330 "About to start drag-drop native loop on which will prevent later "
331 "tests from running properly.");
334 uint32_t length = 0;
335 mozilla::Unused << aTransferableArray->GetLength(&length);
336 if (!length) {
337 nsCOMPtr<nsIMutableArray> mutableArray =
338 do_QueryInterface(aTransferableArray);
339 if (mutableArray) {
340 // In order to be able trigger dnd, we need to have some transferable
341 // object.
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);
350 } else {
351 for (uint32_t i = 0; i < length; ++i) {
352 nsCOMPtr<nsITransferable> trans =
353 do_QueryElementAt(aTransferableArray, i);
354 if (trans) {
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);
365 if (NS_FAILED(rv)) {
366 // Set mDoingDrag so that EndDragSession cleans up and sends the dragend
367 // event after the aborted drag.
368 mDoingDrag = true;
369 EndDragSession(true, 0);
372 return rv;
375 NS_IMETHODIMP
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;
389 mHasImage = true;
390 mDragPopup = nullptr;
391 mImage = aImage;
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.
405 mRegion = Nothing();
406 if (aDOMNode && aDOMNode->IsContent() && !aImage) {
407 if (aDOMNode->NodeInfo()->Equals(nsGkAtoms::treechildren,
408 kNameSpaceID_XUL)) {
409 nsTreeBodyFrame* treeBody =
410 do_QueryFrame(aDOMNode->AsContent()->GetPrimaryFrame());
411 if (treeBody) {
412 mRegion = treeBody->GetSelectionRegion();
417 nsresult rv = InvokeDragSession(
418 aDOMNode, aPrincipal, aCsp, aCookieJarSettings, aTransferableArray,
419 aActionType, nsIContentPolicy::TYPE_INTERNAL_IMAGE);
420 mRegion = Nothing();
421 return rv;
424 NS_IMETHODIMP
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;
438 mHasImage = true;
439 mDragPopup = nullptr;
440 mImage = 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);
451 mRegion = Nothing();
452 return rv;
455 NS_IMETHODIMP
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;
469 mHasImage = true;
470 mDragPopup = nullptr;
471 mImage = nullptr;
472 mImageOffset = CSSIntPoint();
473 mDragStartData = nullptr;
474 mRegion = Nothing();
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 //-------------------------------------------------------------------------
492 NS_IMETHODIMP
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) {
499 *aSession = this;
500 NS_ADDREF(*aSession); // addRef because we're a "getter"
501 } else
502 *aSession = nullptr;
504 return NS_OK;
507 //-------------------------------------------------------------------------
508 NS_IMETHODIMP
509 nsBaseDragService::StartDragSession() {
510 if (mDoingDrag) {
511 return NS_ERROR_FAILURE;
513 mDoingDrag = true;
514 // By default dispatch drop also to content.
515 mOnlyChromeDrop = false;
517 return NS_OK;
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;
528 return NS_OK;
531 void nsBaseDragService::OpenDragPopup() {
532 if (mDragPopup) {
533 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
534 if (pm) {
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;
551 return retval;
554 //-------------------------------------------------------------------------
555 NS_IMETHODIMP
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);
567 if (mDragPopup) {
568 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
569 if (pm) {
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
578 // session.
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();
590 mDoingDrag = false;
591 mSessionIsSynthesizedForTests = false;
592 mIsDraggingTextInTextControl = false;
593 mEffectAllowedForTests = nsIDragService::DRAGDROP_ACTION_UNINITIALIZED;
594 mEndingSession = false;
595 mCanDrop = false;
597 // release the source we've been holding on to.
598 mSourceDocument = nullptr;
599 mSourceNode = nullptr;
600 mSourceWindowContext = nullptr;
601 mTriggeringPrincipal = nullptr;
602 mCsp = nullptr;
603 mSelection = nullptr;
604 mDataTransfer = nullptr;
605 mHasImage = false;
606 mUserCancelled = false;
607 mDragPopup = nullptr;
608 mDragStartData = nullptr;
609 mImage = nullptr;
610 mImageOffset = CSSIntPoint();
611 mScreenPosition = CSSIntPoint();
612 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
613 mInputSource = MouseEvent_Binding::MOZ_SOURCE_MOUSE;
614 mRegion = Nothing();
616 return NS_OK;
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++) {
625 bool found;
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) {
630 continue;
633 nsCOMPtr<nsIVariant> variant = item->DataNoSecurityCheck();
634 nsCOMPtr<nsIWritableVariant> writable = do_QueryInterface(variant);
636 if (writable) {
637 writable->SetAsEmpty();
643 NS_IMETHODIMP
644 nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage,
645 uint32_t aKeyModifiers) {
646 if (!mSourceNode || !mSourceDocument || mSuppressLevel) {
647 return NS_OK;
649 RefPtr<PresShell> presShell = mSourceDocument->GetPresShell();
650 if (!presShell) {
651 return NS_OK;
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;
663 if (widget) {
664 event.mRefPoint -= widget->WidgetToTopLevelWidgetOffset();
666 event.mUserCancelled = mUserCancelled;
668 event.mModifiers = aKeyModifiers;
670 if (widget) {
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.
684 NS_IMETHODIMP
685 nsBaseDragService::DragMoved(int32_t aX, int32_t aY) {
686 if (mDragPopup) {
687 nsIFrame* frame = mDragPopup->GetPrimaryFrame();
688 if (frame && frame->IsMenuPopupFrame()) {
689 CSSIntPoint cssPos =
690 RoundedToInt(LayoutDeviceIntPoint(aX, aY) /
691 frame->PresContext()->CSSToDevPixelScale()) -
692 mImageOffset;
693 static_cast<nsMenuPopupFrame*>(frame)->MoveTo(cssPos, true);
697 return NS_OK;
700 static PresShell* GetPresShellForContent(nsINode* aDOMNode) {
701 nsCOMPtr<nsIContent> content = do_QueryInterface(aDOMNode);
702 if (!content) return nullptr;
704 RefPtr<Document> document = content->GetComposedDoc();
705 if (document) {
706 document->FlushPendingNotifications(FlushType::Display);
707 return document->GetPresShell();
710 return nullptr;
713 nsresult nsBaseDragService::DrawDrag(nsINode* aDOMNode,
714 const Maybe<CSSIntRegion>& aRegion,
715 CSSIntPoint aScreenPosition,
716 LayoutDeviceIntRect* aScreenDragRect,
717 RefPtr<SourceSurface>* aSurface,
718 nsPresContext** aPresContext) {
719 *aSurface = nullptr;
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);
735 if (!presShell) {
736 return NS_ERROR_FAILURE;
739 *aPresContext = presShell->GetPresContext();
741 if (mDragStartData) {
742 if (mImage) {
743 // Just clear the surface if chrome has overridden it with an image.
744 *aSurface = nullptr;
745 } else {
746 *aSurface = mDragStartData->TakeVisualization(aScreenDragRect);
749 mDragStartData = nullptr;
750 return NS_OK;
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;
767 if (aRegion) {
768 // if a region was specified, set the screen rectangle to the area that
769 // the region occupies
770 presLayoutRect = ToAppUnits(aRegion->GetBounds(), AppUnitsPerCSSPixel());
771 } else {
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()),
783 *aPresContext);
784 aScreenDragRect->SizeTo(screenVisualRect.Width(),
785 screenVisualRect.Height());
786 return NS_OK;
789 // draw the image for selections
790 if (mSelection) {
791 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
792 *aSurface = presShell->RenderSelection(
793 mSelection, pnt, aScreenDragRect,
794 mImage ? RenderImageFlags::None : RenderImageFlags::AutoScale);
795 return NS_OK;
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.
801 if (mImage) {
802 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
803 HTMLCanvasElement* canvas = HTMLCanvasElement::FromNodeOrNull(content);
804 if (canvas) {
805 return DrawDragForImage(*aPresContext, nullptr, canvas, aScreenDragRect,
806 aSurface);
809 nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(dragNode);
810 // for image nodes, create the drag image from the actual image data
811 if (imageLoader) {
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;
827 if (!mDragPopup) {
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;
835 } else {
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(
842 "img")) {
843 // if the dragnode contains an image, set RenderImageFlags::IsImage
844 // flag
845 renderFlags = renderFlags | RenderImageFlags::IsImage;
846 break;
851 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
852 *aSurface = presShell->RenderNode(dragNode, aRegion, pnt, aScreenDragRect,
853 renderFlags);
856 // If an image was specified, reset the position from the offset that was
857 // supplied.
858 if (mImage) {
859 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
862 return NS_OK;
865 nsresult nsBaseDragService::DrawDragForImage(
866 nsPresContext* aPresContext, nsIImageLoadingContent* aImageLoader,
867 HTMLCanvasElement* aCanvas, LayoutDeviceIntRect* aScreenDragRect,
868 RefPtr<SourceSurface>* aSurface) {
869 nsCOMPtr<imgIContainer> imgContainer;
870 if (aImageLoader) {
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));
891 } else {
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);
898 nsIntSize destSize;
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;
904 if (aImageLoader) {
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;
913 ImgDrawResult res = imgContainer->Draw(
914 ctx, destSize, ImageRegion::Create(destSize),
915 imgIContainer::FRAME_CURRENT, SamplingFilter::GOOD, SVGImageContext(),
916 imgIContainer::FLAG_SYNC_DECODE, 1.0);
917 if (res == ImgDrawResult::BAD_IMAGE || res == ImgDrawResult::BAD_ARGS ||
918 res == ImgDrawResult::NOT_SUPPORTED) {
919 return NS_ERROR_FAILURE;
921 *aSurface = dt->Snapshot();
922 } else {
923 *aSurface = aCanvas->GetSurfaceSnapshot();
926 return result;
929 NS_IMETHODIMP
930 nsBaseDragService::Suppress() {
931 EndDragSession(false, 0);
932 ++mSuppressLevel;
933 return NS_OK;
936 NS_IMETHODIMP
937 nsBaseDragService::Unsuppress() {
938 --mSuppressLevel;
939 return NS_OK;
942 NS_IMETHODIMP
943 nsBaseDragService::UserCancelled() {
944 mUserCancelled = true;
945 return NS_OK;
948 NS_IMETHODIMP
949 nsBaseDragService::UpdateDragEffect() {
950 mDragActionFromChildProcess = mDragAction;
951 return NS_OK;
954 NS_IMETHODIMP
955 nsBaseDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
956 int32_t aImageY) {
957 // Don't change the image if this is a drag from another source or if there
958 // is a drag popup.
959 if (!mSourceNode || mDragPopup) return NS_OK;
961 mImage = aImage;
962 mImageOffset = CSSIntPoint(aImageX, aImageY);
963 return NS_OK;
966 NS_IMETHODIMP
967 nsBaseDragService::DragEventDispatchedToChildProcess() {
968 mDragEventDispatchedToChildProcess = true;
969 return NS_OK;
972 bool nsBaseDragService::MaybeAddChildProcess(
973 mozilla::dom::ContentParent* aChild) {
974 if (!mChildProcesses.Contains(aChild)) {
975 mChildProcesses.AppendElement(aChild);
976 return true;
978 return false;
981 bool nsBaseDragService::RemoveAllChildProcesses() {
982 for (uint32_t c = 0; c < mChildProcesses.Length(); c++) {
983 mozilla::Unused << mChildProcesses[c]->SendEndDragSession(
984 true, false, LayoutDeviceIntPoint(), 0);
986 mChildProcesses.Clear();
987 return true;