Bug 1510695 - Fix URL comparisons in reftestWait r=jgraham
[gecko.git] / widget / nsBaseDragService.cpp
blob217e4f0e933752eae000450f51940c620e4a02ef
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 "nsIServiceManager.h"
11 #include "nsITransferable.h"
12 #include "nsSize.h"
13 #include "nsXPCOM.h"
14 #include "nsISupportsPrimitives.h"
15 #include "nsCOMPtr.h"
16 #include "nsIInterfaceRequestorUtils.h"
17 #include "nsIFrame.h"
18 #include "mozilla/dom/Document.h"
19 #include "nsIContent.h"
20 #include "nsIPresShell.h"
21 #include "nsViewManager.h"
22 #include "nsINode.h"
23 #include "nsPresContext.h"
24 #include "nsIImageLoadingContent.h"
25 #include "imgIContainer.h"
26 #include "imgIRequest.h"
27 #include "ImageRegion.h"
28 #include "nsQueryObject.h"
29 #include "nsRegion.h"
30 #include "nsXULPopupManager.h"
31 #include "nsMenuPopupFrame.h"
32 #include "SVGImageContext.h"
33 #ifdef MOZ_XUL
34 #include "nsTreeBodyFrame.h"
35 #endif
36 #include "mozilla/MouseEvents.h"
37 #include "mozilla/Preferences.h"
38 #include "mozilla/dom/BindingDeclarations.h"
39 #include "mozilla/dom/DataTransferItemList.h"
40 #include "mozilla/dom/DataTransfer.h"
41 #include "mozilla/dom/DragEvent.h"
42 #include "mozilla/dom/MouseEventBinding.h"
43 #include "mozilla/dom/Selection.h"
44 #include "mozilla/gfx/2D.h"
45 #include "mozilla/Unused.h"
46 #include "nsFrameLoader.h"
47 #include "TabParent.h"
49 #include "gfxContext.h"
50 #include "gfxPlatform.h"
51 #include <algorithm>
53 using namespace mozilla;
54 using namespace mozilla::dom;
55 using namespace mozilla::gfx;
56 using namespace mozilla::image;
58 #define DRAGIMAGES_PREF "nglayout.enable_drag_images"
60 nsBaseDragService::nsBaseDragService()
61 : mCanDrop(false),
62 mOnlyChromeDrop(false),
63 mDoingDrag(false),
64 mHasImage(false),
65 mUserCancelled(false),
66 mDragEventDispatchedToChildProcess(false),
67 mDragAction(DRAGDROP_ACTION_NONE),
68 mDragActionFromChildProcess(DRAGDROP_ACTION_UNINITIALIZED),
69 mContentPolicyType(nsIContentPolicy::TYPE_OTHER),
70 mSuppressLevel(0),
71 mInputSource(MouseEvent_Binding::MOZ_SOURCE_MOUSE) {}
73 nsBaseDragService::~nsBaseDragService() = default;
75 NS_IMPL_ISUPPORTS(nsBaseDragService, nsIDragService, nsIDragSession)
77 //---------------------------------------------------------
78 NS_IMETHODIMP
79 nsBaseDragService::SetCanDrop(bool aCanDrop) {
80 mCanDrop = aCanDrop;
81 return NS_OK;
84 //---------------------------------------------------------
85 NS_IMETHODIMP
86 nsBaseDragService::GetCanDrop(bool* aCanDrop) {
87 *aCanDrop = mCanDrop;
88 return NS_OK;
90 //---------------------------------------------------------
91 NS_IMETHODIMP
92 nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome) {
93 mOnlyChromeDrop = aOnlyChrome;
94 return NS_OK;
97 //---------------------------------------------------------
98 NS_IMETHODIMP
99 nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome) {
100 *aOnlyChrome = mOnlyChromeDrop;
101 return NS_OK;
104 //---------------------------------------------------------
105 NS_IMETHODIMP
106 nsBaseDragService::SetDragAction(uint32_t anAction) {
107 mDragAction = anAction;
108 return NS_OK;
111 //---------------------------------------------------------
112 NS_IMETHODIMP
113 nsBaseDragService::GetDragAction(uint32_t* anAction) {
114 *anAction = mDragAction;
115 return NS_OK;
118 //-------------------------------------------------------------------------
120 NS_IMETHODIMP
121 nsBaseDragService::GetNumDropItems(uint32_t* aNumItems) {
122 *aNumItems = 0;
123 return NS_ERROR_FAILURE;
127 // GetSourceDocument
129 // Returns the DOM document where the drag was initiated. This will be
130 // nullptr if the drag began outside of our application.
132 NS_IMETHODIMP
133 nsBaseDragService::GetSourceDocument(Document** aSourceDocument) {
134 *aSourceDocument = mSourceDocument.get();
135 NS_IF_ADDREF(*aSourceDocument);
137 return NS_OK;
141 // GetSourceNode
143 // Returns the DOM node where the drag was initiated. This will be
144 // nullptr if the drag began outside of our application.
146 NS_IMETHODIMP
147 nsBaseDragService::GetSourceNode(nsINode** aSourceNode) {
148 *aSourceNode = do_AddRef(mSourceNode).take();
149 return NS_OK;
152 NS_IMETHODIMP
153 nsBaseDragService::GetTriggeringPrincipal(nsIPrincipal** aPrincipal) {
154 NS_IF_ADDREF(*aPrincipal = mTriggeringPrincipal);
155 return NS_OK;
158 NS_IMETHODIMP
159 nsBaseDragService::SetTriggeringPrincipal(nsIPrincipal* aPrincipal) {
160 mTriggeringPrincipal = aPrincipal;
161 return NS_OK;
164 //-------------------------------------------------------------------------
166 NS_IMETHODIMP
167 nsBaseDragService::GetData(nsITransferable* aTransferable,
168 uint32_t aItemIndex) {
169 return NS_ERROR_FAILURE;
172 //-------------------------------------------------------------------------
173 NS_IMETHODIMP
174 nsBaseDragService::IsDataFlavorSupported(const char* aDataFlavor,
175 bool* _retval) {
176 return NS_ERROR_FAILURE;
179 NS_IMETHODIMP
180 nsBaseDragService::GetDataTransferXPCOM(DataTransfer** aDataTransfer) {
181 *aDataTransfer = mDataTransfer;
182 NS_IF_ADDREF(*aDataTransfer);
183 return NS_OK;
186 NS_IMETHODIMP
187 nsBaseDragService::SetDataTransferXPCOM(DataTransfer* aDataTransfer) {
188 NS_ENSURE_STATE(aDataTransfer);
189 mDataTransfer = aDataTransfer;
190 return NS_OK;
193 DataTransfer* nsBaseDragService::GetDataTransfer() { return mDataTransfer; }
195 void nsBaseDragService::SetDataTransfer(DataTransfer* aDataTransfer) {
196 mDataTransfer = aDataTransfer;
199 //-------------------------------------------------------------------------
200 NS_IMETHODIMP
201 nsBaseDragService::InvokeDragSession(
202 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIArray* aTransferableArray,
203 uint32_t aActionType,
204 nsContentPolicyType aContentPolicyType = nsIContentPolicy::TYPE_OTHER) {
205 AUTO_PROFILER_LABEL("nsBaseDragService::InvokeDragSession", OTHER);
207 NS_ENSURE_TRUE(aDOMNode, NS_ERROR_INVALID_ARG);
208 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
210 // stash the document of the dom node
211 mSourceDocument = aDOMNode->OwnerDoc();
212 mTriggeringPrincipal = aPrincipal;
213 mSourceNode = aDOMNode;
214 mContentPolicyType = aContentPolicyType;
215 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
217 // When the mouse goes down, the selection code starts a mouse
218 // capture. However, this gets in the way of determining drag
219 // feedback for things like trees because the event coordinates
220 // are in the wrong coord system, so turn off mouse capture.
221 nsIPresShell::ClearMouseCapture(nullptr);
223 uint32_t length = 0;
224 mozilla::Unused << aTransferableArray->GetLength(&length);
225 for (uint32_t i = 0; i < length; ++i) {
226 nsCOMPtr<nsITransferable> trans = do_QueryElementAt(aTransferableArray, i);
227 if (trans) {
228 // Set the requestingPrincipal on the transferable.
229 trans->SetRequestingPrincipal(mSourceNode->NodePrincipal());
230 trans->SetContentPolicyType(mContentPolicyType);
234 nsresult rv = InvokeDragSessionImpl(aTransferableArray, mRegion, aActionType);
236 if (NS_FAILED(rv)) {
237 // Set mDoingDrag so that EndDragSession cleans up and sends the dragend
238 // event after the aborted drag.
239 mDoingDrag = true;
240 EndDragSession(true, 0);
243 return rv;
246 NS_IMETHODIMP
247 nsBaseDragService::InvokeDragSessionWithImage(
248 nsINode* aDOMNode, nsIPrincipal* aPrincipal, nsIArray* aTransferableArray,
249 uint32_t aActionType, nsINode* aImage, int32_t aImageX, int32_t aImageY,
250 DragEvent* aDragEvent, DataTransfer* aDataTransfer) {
251 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
252 NS_ENSURE_TRUE(aDataTransfer, NS_ERROR_NULL_POINTER);
253 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
255 mDataTransfer = aDataTransfer;
256 mSelection = nullptr;
257 mHasImage = true;
258 mDragPopup = nullptr;
259 mImage = aImage;
260 mImageOffset = CSSIntPoint(aImageX, aImageY);
262 mScreenPosition.x = aDragEvent->ScreenX(CallerType::System);
263 mScreenPosition.y = aDragEvent->ScreenY(CallerType::System);
264 mInputSource = aDragEvent->MozInputSource();
266 // If dragging within a XUL tree and no custom drag image was
267 // set, the region argument to InvokeDragSessionWithImage needs
268 // to be set to the area encompassing the selected rows of the
269 // tree to ensure that the drag feedback gets clipped to those
270 // rows. For other content, region should be null.
271 mRegion = Nothing();
272 #ifdef MOZ_XUL
273 if (aDOMNode && aDOMNode->IsContent() && !aImage) {
274 if (aDOMNode->NodeInfo()->Equals(nsGkAtoms::treechildren,
275 kNameSpaceID_XUL)) {
276 nsTreeBodyFrame* treeBody =
277 do_QueryFrame(aDOMNode->AsContent()->GetPrimaryFrame());
278 if (treeBody) {
279 mRegion = treeBody->GetSelectionRegion();
283 #endif
285 nsresult rv =
286 InvokeDragSession(aDOMNode, aPrincipal, aTransferableArray, aActionType,
287 nsIContentPolicy::TYPE_INTERNAL_IMAGE);
288 mRegion = Nothing();
289 return rv;
292 NS_IMETHODIMP
293 nsBaseDragService::InvokeDragSessionWithSelection(Selection* aSelection,
294 nsIPrincipal* aPrincipal,
295 nsIArray* aTransferableArray,
296 uint32_t aActionType,
297 DragEvent* aDragEvent,
298 DataTransfer* aDataTransfer) {
299 NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
300 NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
301 NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
303 mDataTransfer = aDataTransfer;
304 mSelection = aSelection;
305 mHasImage = true;
306 mDragPopup = nullptr;
307 mImage = nullptr;
308 mImageOffset = CSSIntPoint();
309 mRegion = Nothing();
311 mScreenPosition.x = aDragEvent->ScreenX(CallerType::System);
312 mScreenPosition.y = aDragEvent->ScreenY(CallerType::System);
313 mInputSource = aDragEvent->MozInputSource();
315 // just get the focused node from the selection
316 // XXXndeakin this should actually be the deepest node that contains both
317 // endpoints of the selection
318 nsCOMPtr<nsINode> node = aSelection->GetFocusNode();
320 return InvokeDragSession(node, aPrincipal, aTransferableArray, aActionType,
321 nsIContentPolicy::TYPE_OTHER);
324 //-------------------------------------------------------------------------
325 NS_IMETHODIMP
326 nsBaseDragService::GetCurrentSession(nsIDragSession** aSession) {
327 if (!aSession) return NS_ERROR_INVALID_ARG;
329 // "this" also implements a drag session, so say we are one but only
330 // if there is currently a drag going on.
331 if (!mSuppressLevel && mDoingDrag) {
332 *aSession = this;
333 NS_ADDREF(*aSession); // addRef because we're a "getter"
334 } else
335 *aSession = nullptr;
337 return NS_OK;
340 //-------------------------------------------------------------------------
341 NS_IMETHODIMP
342 nsBaseDragService::StartDragSession() {
343 if (mDoingDrag) {
344 return NS_ERROR_FAILURE;
346 mDoingDrag = true;
347 // By default dispatch drop also to content.
348 mOnlyChromeDrop = false;
350 return NS_OK;
353 void nsBaseDragService::OpenDragPopup() {
354 if (mDragPopup) {
355 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
356 if (pm) {
357 pm->ShowPopupAtScreen(mDragPopup, mScreenPosition.x - mImageOffset.x,
358 mScreenPosition.y - mImageOffset.y, false, nullptr);
363 int32_t nsBaseDragService::TakeChildProcessDragAction() {
364 // If the last event was dispatched to the child process, use the drag action
365 // assigned from it instead and return it. DRAGDROP_ACTION_UNINITIALIZED is
366 // returned otherwise.
367 int32_t retval = DRAGDROP_ACTION_UNINITIALIZED;
368 if (TakeDragEventDispatchedToChildProcess() &&
369 mDragActionFromChildProcess != DRAGDROP_ACTION_UNINITIALIZED) {
370 retval = mDragActionFromChildProcess;
373 return retval;
376 //-------------------------------------------------------------------------
377 NS_IMETHODIMP
378 nsBaseDragService::EndDragSession(bool aDoneDrag, uint32_t aKeyModifiers) {
379 if (!mDoingDrag) {
380 return NS_ERROR_FAILURE;
383 if (aDoneDrag && !mSuppressLevel) {
384 FireDragEventAtSource(eDragEnd, aKeyModifiers);
387 if (mDragPopup) {
388 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
389 if (pm) {
390 pm->HidePopup(mDragPopup, false, true, false, false);
394 for (uint32_t i = 0; i < mChildProcesses.Length(); ++i) {
395 mozilla::Unused << mChildProcesses[i]->SendEndDragSession(
396 aDoneDrag, mUserCancelled, mEndDragPoint, aKeyModifiers);
397 // Continue sending input events with input priority when stopping the dnd
398 // session.
399 mChildProcesses[i]->SetInputPriorityEventEnabled(true);
401 mChildProcesses.Clear();
403 // mDataTransfer and the items it owns are going to die anyway, but we
404 // explicitly deref the contained data here so that we don't have to wait for
405 // CC to reclaim the memory.
406 if (XRE_IsParentProcess()) {
407 DiscardInternalTransferData();
410 mDoingDrag = false;
411 mCanDrop = false;
413 // release the source we've been holding on to.
414 mSourceDocument = nullptr;
415 mSourceNode = nullptr;
416 mTriggeringPrincipal = nullptr;
417 mSelection = nullptr;
418 mDataTransfer = nullptr;
419 mHasImage = false;
420 mUserCancelled = false;
421 mDragPopup = nullptr;
422 mImage = nullptr;
423 mImageOffset = CSSIntPoint();
424 mScreenPosition = CSSIntPoint();
425 mEndDragPoint = LayoutDeviceIntPoint(0, 0);
426 mInputSource = MouseEvent_Binding::MOZ_SOURCE_MOUSE;
427 mRegion = Nothing();
429 return NS_OK;
432 void nsBaseDragService::DiscardInternalTransferData() {
433 if (mDataTransfer && mSourceNode) {
434 MOZ_ASSERT(mDataTransfer);
436 DataTransferItemList* items = mDataTransfer->Items();
437 for (size_t i = 0; i < items->Length(); i++) {
438 bool found;
439 DataTransferItem* item = items->IndexedGetter(i, found);
441 // Non-OTHER items may still be needed by JS. Skip them.
442 if (!found || item->Kind() != DataTransferItem::KIND_OTHER) {
443 continue;
446 nsCOMPtr<nsIVariant> variant = item->DataNoSecurityCheck();
447 nsCOMPtr<nsIWritableVariant> writable = do_QueryInterface(variant);
449 if (writable) {
450 writable->SetAsEmpty();
456 NS_IMETHODIMP
457 nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage,
458 uint32_t aKeyModifiers) {
459 if (mSourceNode && mSourceDocument && !mSuppressLevel) {
460 nsCOMPtr<nsIPresShell> presShell = mSourceDocument->GetShell();
461 if (presShell) {
462 nsEventStatus status = nsEventStatus_eIgnore;
463 WidgetDragEvent event(true, aEventMessage, nullptr);
464 event.inputSource = mInputSource;
465 if (aEventMessage == eDragEnd) {
466 event.mRefPoint = mEndDragPoint;
467 event.mUserCancelled = mUserCancelled;
469 event.mModifiers = aKeyModifiers;
470 // Send the drag event to APZ, which needs to know about them to be
471 // able to accurately detect the end of a drag gesture.
472 if (nsPresContext* presContext = presShell->GetPresContext()) {
473 if (nsCOMPtr<nsIWidget> widget = presContext->GetRootWidget()) {
474 widget->DispatchEventToAPZOnly(&event);
478 nsCOMPtr<nsIContent> content = do_QueryInterface(mSourceNode);
479 return presShell->HandleDOMEventWithTarget(content, &event, &status);
483 return NS_OK;
486 /* This is used by Windows and Mac to update the position of a popup being
487 * used as a drag image during the drag. This isn't used on GTK as it manages
488 * the drag popup itself.
490 NS_IMETHODIMP
491 nsBaseDragService::DragMoved(int32_t aX, int32_t aY) {
492 if (mDragPopup) {
493 nsIFrame* frame = mDragPopup->GetPrimaryFrame();
494 if (frame && frame->IsMenuPopupFrame()) {
495 CSSIntPoint cssPos =
496 RoundedToInt(LayoutDeviceIntPoint(aX, aY) /
497 frame->PresContext()->CSSToDevPixelScale()) -
498 mImageOffset;
499 (static_cast<nsMenuPopupFrame*>(frame))->MoveTo(cssPos, true);
503 return NS_OK;
506 static nsIPresShell* GetPresShellForContent(nsINode* aDOMNode) {
507 nsCOMPtr<nsIContent> content = do_QueryInterface(aDOMNode);
508 if (!content) return nullptr;
510 RefPtr<Document> document = content->GetComposedDoc();
511 if (document) {
512 document->FlushPendingNotifications(FlushType::Display);
513 return document->GetShell();
516 return nullptr;
519 nsresult nsBaseDragService::DrawDrag(nsINode* aDOMNode,
520 const Maybe<CSSIntRegion>& aRegion,
521 CSSIntPoint aScreenPosition,
522 LayoutDeviceIntRect* aScreenDragRect,
523 RefPtr<SourceSurface>* aSurface,
524 nsPresContext** aPresContext) {
525 *aSurface = nullptr;
526 *aPresContext = nullptr;
528 // use a default size, in case of an error.
529 aScreenDragRect->SetRect(aScreenPosition.x - mImageOffset.x,
530 aScreenPosition.y - mImageOffset.y, 1, 1);
532 // if a drag image was specified, use that, otherwise, use the source node
533 nsCOMPtr<nsINode> dragNode = mImage ? mImage.get() : aDOMNode;
535 // get the presshell for the node being dragged. If the drag image is not in
536 // a document or has no frame, get the presshell from the source drag node
537 nsIPresShell* presShell = GetPresShellForContent(dragNode);
538 if (!presShell && mImage) presShell = GetPresShellForContent(aDOMNode);
539 if (!presShell) return NS_ERROR_FAILURE;
541 *aPresContext = presShell->GetPresContext();
543 nsCOMPtr<nsIFrameLoaderOwner> flo = do_QueryInterface(dragNode);
544 if (flo) {
545 RefPtr<nsFrameLoader> fl = flo->GetFrameLoader();
546 if (fl) {
547 auto* tp = static_cast<mozilla::dom::TabParent*>(fl->GetRemoteBrowser());
548 if (tp && tp->TakeDragVisualization(*aSurface, aScreenDragRect)) {
549 if (mImage) {
550 // Just clear the surface if chrome has overridden it with an image.
551 *aSurface = nullptr;
554 return NS_OK;
559 // convert mouse position to dev pixels of the prescontext
560 CSSIntPoint screenPosition(aScreenPosition);
561 screenPosition.x -= mImageOffset.x;
562 screenPosition.y -= mImageOffset.y;
563 LayoutDeviceIntPoint screenPoint =
564 ConvertToUnscaledDevPixels(*aPresContext, screenPosition);
565 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
567 // check if drag images are disabled
568 bool enableDragImages = Preferences::GetBool(DRAGIMAGES_PREF, true);
570 // didn't want an image, so just set the screen rectangle to the frame size
571 if (!enableDragImages || !mHasImage) {
572 // if a region was specified, set the screen rectangle to the area that
573 // the region occupies
574 CSSIntRect dragRect;
575 if (aRegion) {
576 // the region's coordinates are relative to the root frame
577 dragRect = aRegion->GetBounds();
579 nsIFrame* rootFrame = presShell->GetRootFrame();
580 CSSIntRect screenRect = rootFrame->GetScreenRect();
581 dragRect.MoveBy(screenRect.TopLeft());
582 } else {
583 // otherwise, there was no region so just set the rectangle to
584 // the size of the primary frame of the content.
585 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
586 nsIFrame* frame = content->GetPrimaryFrame();
587 if (frame) {
588 dragRect = frame->GetScreenRect();
592 nsIntRect dragRectDev =
593 ToAppUnits(dragRect, AppUnitsPerCSSPixel())
594 .ToOutsidePixels((*aPresContext)->AppUnitsPerDevPixel());
595 aScreenDragRect->SizeTo(dragRectDev.Width(), dragRectDev.Height());
596 return NS_OK;
599 // draw the image for selections
600 if (mSelection) {
601 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
602 *aSurface = presShell->RenderSelection(
603 mSelection, pnt, aScreenDragRect,
604 mImage ? 0 : nsIPresShell::RENDER_AUTO_SCALE);
605 return NS_OK;
608 // if a custom image was specified, check if it is an image node and draw
609 // using the source rather than the displayed image. But if mImage isn't
610 // an image or canvas, fall through to RenderNode below.
611 if (mImage) {
612 nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
613 HTMLCanvasElement* canvas = HTMLCanvasElement::FromNodeOrNull(content);
614 if (canvas) {
615 return DrawDragForImage(*aPresContext, nullptr, canvas, aScreenDragRect,
616 aSurface);
619 nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(dragNode);
620 // for image nodes, create the drag image from the actual image data
621 if (imageLoader) {
622 return DrawDragForImage(*aPresContext, imageLoader, nullptr,
623 aScreenDragRect, aSurface);
626 // If the image is a popup, use that as the image. This allows custom drag
627 // images that can change during the drag, but means that any platform
628 // default image handling won't occur.
629 // XXXndeakin this should be chrome-only
631 nsIFrame* frame = content->GetPrimaryFrame();
632 if (frame && frame->IsMenuPopupFrame()) {
633 mDragPopup = content;
637 if (!mDragPopup) {
638 // otherwise, just draw the node
639 uint32_t renderFlags = mImage ? 0 : nsIPresShell::RENDER_AUTO_SCALE;
640 if (renderFlags) {
641 // check if the dragged node itself is an img element
642 if (dragNode->NodeName().LowerCaseEqualsLiteral("img")) {
643 renderFlags = renderFlags | nsIPresShell::RENDER_IS_IMAGE;
644 } else {
645 nsINodeList* childList = dragNode->ChildNodes();
646 uint32_t length = childList->Length();
647 // check every childnode for being an img element
648 // XXXbz why don't we need to check descendants recursively?
649 for (uint32_t count = 0; count < length; ++count) {
650 if (childList->Item(count)->NodeName().LowerCaseEqualsLiteral(
651 "img")) {
652 // if the dragnode contains an image, set RENDER_IS_IMAGE flag
653 renderFlags = renderFlags | nsIPresShell::RENDER_IS_IMAGE;
654 break;
659 LayoutDeviceIntPoint pnt(aScreenDragRect->TopLeft());
660 *aSurface = presShell->RenderNode(dragNode, aRegion, pnt, aScreenDragRect,
661 renderFlags);
664 // If an image was specified, reset the position from the offset that was
665 // supplied.
666 if (mImage) {
667 aScreenDragRect->MoveTo(screenPoint.x, screenPoint.y);
670 return NS_OK;
673 nsresult nsBaseDragService::DrawDragForImage(
674 nsPresContext* aPresContext, nsIImageLoadingContent* aImageLoader,
675 HTMLCanvasElement* aCanvas, LayoutDeviceIntRect* aScreenDragRect,
676 RefPtr<SourceSurface>* aSurface) {
677 nsCOMPtr<imgIContainer> imgContainer;
678 if (aImageLoader) {
679 nsCOMPtr<imgIRequest> imgRequest;
680 nsresult rv = aImageLoader->GetRequest(
681 nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(imgRequest));
682 NS_ENSURE_SUCCESS(rv, rv);
683 if (!imgRequest) return NS_ERROR_NOT_AVAILABLE;
685 rv = imgRequest->GetImage(getter_AddRefs(imgContainer));
686 NS_ENSURE_SUCCESS(rv, rv);
687 if (!imgContainer) return NS_ERROR_NOT_AVAILABLE;
689 // use the size of the image as the size of the drag image
690 int32_t imageWidth, imageHeight;
691 rv = imgContainer->GetWidth(&imageWidth);
692 NS_ENSURE_SUCCESS(rv, rv);
694 rv = imgContainer->GetHeight(&imageHeight);
695 NS_ENSURE_SUCCESS(rv, rv);
697 aScreenDragRect->SizeTo(aPresContext->CSSPixelsToDevPixels(imageWidth),
698 aPresContext->CSSPixelsToDevPixels(imageHeight));
699 } else {
700 // XXX The canvas size should be converted to dev pixels.
701 NS_ASSERTION(aCanvas, "both image and canvas are null");
702 nsIntSize sz = aCanvas->GetSize();
703 aScreenDragRect->SizeTo(sz.width, sz.height);
706 nsIntSize destSize;
707 destSize.width = aScreenDragRect->Width();
708 destSize.height = aScreenDragRect->Height();
709 if (destSize.width == 0 || destSize.height == 0) return NS_ERROR_FAILURE;
711 nsresult result = NS_OK;
712 if (aImageLoader) {
713 RefPtr<DrawTarget> dt =
714 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
715 destSize, SurfaceFormat::B8G8R8A8);
716 if (!dt || !dt->IsValid()) return NS_ERROR_FAILURE;
718 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(dt);
719 if (!ctx) return NS_ERROR_FAILURE;
721 ImgDrawResult res =
722 imgContainer->Draw(ctx, destSize, ImageRegion::Create(destSize),
723 imgIContainer::FRAME_CURRENT, SamplingFilter::GOOD,
724 /* no SVGImageContext */ Nothing(),
725 imgIContainer::FLAG_SYNC_DECODE, 1.0);
726 if (res == ImgDrawResult::BAD_IMAGE || res == ImgDrawResult::BAD_ARGS ||
727 res == ImgDrawResult::NOT_SUPPORTED) {
728 return NS_ERROR_FAILURE;
730 *aSurface = dt->Snapshot();
731 } else {
732 *aSurface = aCanvas->GetSurfaceSnapshot();
735 return result;
738 LayoutDeviceIntPoint nsBaseDragService::ConvertToUnscaledDevPixels(
739 nsPresContext* aPresContext, CSSIntPoint aScreenPosition) {
740 int32_t adj =
741 aPresContext->DeviceContext()->AppUnitsPerDevPixelAtUnitFullZoom();
742 return LayoutDeviceIntPoint(
743 nsPresContext::CSSPixelsToAppUnits(aScreenPosition.x) / adj,
744 nsPresContext::CSSPixelsToAppUnits(aScreenPosition.y) / adj);
747 NS_IMETHODIMP
748 nsBaseDragService::Suppress() {
749 EndDragSession(false, 0);
750 ++mSuppressLevel;
751 return NS_OK;
754 NS_IMETHODIMP
755 nsBaseDragService::Unsuppress() {
756 --mSuppressLevel;
757 return NS_OK;
760 NS_IMETHODIMP
761 nsBaseDragService::UserCancelled() {
762 mUserCancelled = true;
763 return NS_OK;
766 NS_IMETHODIMP
767 nsBaseDragService::UpdateDragEffect() {
768 mDragActionFromChildProcess = mDragAction;
769 return NS_OK;
772 NS_IMETHODIMP
773 nsBaseDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
774 int32_t aImageY) {
775 // Don't change the image if this is a drag from another source or if there
776 // is a drag popup.
777 if (!mSourceNode || mDragPopup) return NS_OK;
779 mImage = aImage;
780 mImageOffset = CSSIntPoint(aImageX, aImageY);
781 return NS_OK;
784 NS_IMETHODIMP
785 nsBaseDragService::DragEventDispatchedToChildProcess() {
786 mDragEventDispatchedToChildProcess = true;
787 return NS_OK;
790 bool nsBaseDragService::MaybeAddChildProcess(
791 mozilla::dom::ContentParent* aChild) {
792 if (!mChildProcesses.Contains(aChild)) {
793 mChildProcesses.AppendElement(aChild);
794 return true;
796 return false;