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"
14 #include "nsISupportsPrimitives.h"
16 #include "nsIInterfaceRequestorUtils.h"
18 #include "mozilla/dom/Document.h"
19 #include "nsIContent.h"
20 #include "nsIPresShell.h"
21 #include "nsViewManager.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"
30 #include "nsXULPopupManager.h"
31 #include "nsMenuPopupFrame.h"
32 #include "SVGImageContext.h"
34 #include "nsTreeBodyFrame.h"
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"
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()
62 mOnlyChromeDrop(false),
65 mUserCancelled(false),
66 mDragEventDispatchedToChildProcess(false),
67 mDragAction(DRAGDROP_ACTION_NONE
),
68 mDragActionFromChildProcess(DRAGDROP_ACTION_UNINITIALIZED
),
69 mContentPolicyType(nsIContentPolicy::TYPE_OTHER
),
71 mInputSource(MouseEvent_Binding::MOZ_SOURCE_MOUSE
) {}
73 nsBaseDragService::~nsBaseDragService() = default;
75 NS_IMPL_ISUPPORTS(nsBaseDragService
, nsIDragService
, nsIDragSession
)
77 //---------------------------------------------------------
79 nsBaseDragService::SetCanDrop(bool aCanDrop
) {
84 //---------------------------------------------------------
86 nsBaseDragService::GetCanDrop(bool* aCanDrop
) {
90 //---------------------------------------------------------
92 nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome
) {
93 mOnlyChromeDrop
= aOnlyChrome
;
97 //---------------------------------------------------------
99 nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome
) {
100 *aOnlyChrome
= mOnlyChromeDrop
;
104 //---------------------------------------------------------
106 nsBaseDragService::SetDragAction(uint32_t anAction
) {
107 mDragAction
= anAction
;
111 //---------------------------------------------------------
113 nsBaseDragService::GetDragAction(uint32_t* anAction
) {
114 *anAction
= mDragAction
;
118 //-------------------------------------------------------------------------
121 nsBaseDragService::GetNumDropItems(uint32_t* aNumItems
) {
123 return NS_ERROR_FAILURE
;
129 // Returns the DOM document where the drag was initiated. This will be
130 // nullptr if the drag began outside of our application.
133 nsBaseDragService::GetSourceDocument(Document
** aSourceDocument
) {
134 *aSourceDocument
= mSourceDocument
.get();
135 NS_IF_ADDREF(*aSourceDocument
);
143 // Returns the DOM node where the drag was initiated. This will be
144 // nullptr if the drag began outside of our application.
147 nsBaseDragService::GetSourceNode(nsINode
** aSourceNode
) {
148 *aSourceNode
= do_AddRef(mSourceNode
).take();
153 nsBaseDragService::GetTriggeringPrincipal(nsIPrincipal
** aPrincipal
) {
154 NS_IF_ADDREF(*aPrincipal
= mTriggeringPrincipal
);
159 nsBaseDragService::SetTriggeringPrincipal(nsIPrincipal
* aPrincipal
) {
160 mTriggeringPrincipal
= aPrincipal
;
164 //-------------------------------------------------------------------------
167 nsBaseDragService::GetData(nsITransferable
* aTransferable
,
168 uint32_t aItemIndex
) {
169 return NS_ERROR_FAILURE
;
172 //-------------------------------------------------------------------------
174 nsBaseDragService::IsDataFlavorSupported(const char* aDataFlavor
,
176 return NS_ERROR_FAILURE
;
180 nsBaseDragService::GetDataTransferXPCOM(DataTransfer
** aDataTransfer
) {
181 *aDataTransfer
= mDataTransfer
;
182 NS_IF_ADDREF(*aDataTransfer
);
187 nsBaseDragService::SetDataTransferXPCOM(DataTransfer
* aDataTransfer
) {
188 NS_ENSURE_STATE(aDataTransfer
);
189 mDataTransfer
= aDataTransfer
;
193 DataTransfer
* nsBaseDragService::GetDataTransfer() { return mDataTransfer
; }
195 void nsBaseDragService::SetDataTransfer(DataTransfer
* aDataTransfer
) {
196 mDataTransfer
= aDataTransfer
;
199 //-------------------------------------------------------------------------
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);
224 mozilla::Unused
<< aTransferableArray
->GetLength(&length
);
225 for (uint32_t i
= 0; i
< length
; ++i
) {
226 nsCOMPtr
<nsITransferable
> trans
= do_QueryElementAt(aTransferableArray
, i
);
228 // Set the requestingPrincipal on the transferable.
229 trans
->SetRequestingPrincipal(mSourceNode
->NodePrincipal());
230 trans
->SetContentPolicyType(mContentPolicyType
);
234 nsresult rv
= InvokeDragSessionImpl(aTransferableArray
, mRegion
, aActionType
);
237 // Set mDoingDrag so that EndDragSession cleans up and sends the dragend
238 // event after the aborted drag.
240 EndDragSession(true, 0);
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;
258 mDragPopup
= nullptr;
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.
273 if (aDOMNode
&& aDOMNode
->IsContent() && !aImage
) {
274 if (aDOMNode
->NodeInfo()->Equals(nsGkAtoms::treechildren
,
276 nsTreeBodyFrame
* treeBody
=
277 do_QueryFrame(aDOMNode
->AsContent()->GetPrimaryFrame());
279 mRegion
= treeBody
->GetSelectionRegion();
286 InvokeDragSession(aDOMNode
, aPrincipal
, aTransferableArray
, aActionType
,
287 nsIContentPolicy::TYPE_INTERNAL_IMAGE
);
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
;
306 mDragPopup
= nullptr;
308 mImageOffset
= CSSIntPoint();
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 //-------------------------------------------------------------------------
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
) {
333 NS_ADDREF(*aSession
); // addRef because we're a "getter"
340 //-------------------------------------------------------------------------
342 nsBaseDragService::StartDragSession() {
344 return NS_ERROR_FAILURE
;
347 // By default dispatch drop also to content.
348 mOnlyChromeDrop
= false;
353 void nsBaseDragService::OpenDragPopup() {
355 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
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
;
376 //-------------------------------------------------------------------------
378 nsBaseDragService::EndDragSession(bool aDoneDrag
, uint32_t aKeyModifiers
) {
380 return NS_ERROR_FAILURE
;
383 if (aDoneDrag
&& !mSuppressLevel
) {
384 FireDragEventAtSource(eDragEnd
, aKeyModifiers
);
388 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
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
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();
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;
420 mUserCancelled
= false;
421 mDragPopup
= nullptr;
423 mImageOffset
= CSSIntPoint();
424 mScreenPosition
= CSSIntPoint();
425 mEndDragPoint
= LayoutDeviceIntPoint(0, 0);
426 mInputSource
= MouseEvent_Binding::MOZ_SOURCE_MOUSE
;
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
++) {
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
) {
446 nsCOMPtr
<nsIVariant
> variant
= item
->DataNoSecurityCheck();
447 nsCOMPtr
<nsIWritableVariant
> writable
= do_QueryInterface(variant
);
450 writable
->SetAsEmpty();
457 nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage
,
458 uint32_t aKeyModifiers
) {
459 if (mSourceNode
&& mSourceDocument
&& !mSuppressLevel
) {
460 nsCOMPtr
<nsIPresShell
> presShell
= mSourceDocument
->GetShell();
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
);
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.
491 nsBaseDragService::DragMoved(int32_t aX
, int32_t aY
) {
493 nsIFrame
* frame
= mDragPopup
->GetPrimaryFrame();
494 if (frame
&& frame
->IsMenuPopupFrame()) {
496 RoundedToInt(LayoutDeviceIntPoint(aX
, aY
) /
497 frame
->PresContext()->CSSToDevPixelScale()) -
499 (static_cast<nsMenuPopupFrame
*>(frame
))->MoveTo(cssPos
, true);
506 static nsIPresShell
* GetPresShellForContent(nsINode
* aDOMNode
) {
507 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(aDOMNode
);
508 if (!content
) return nullptr;
510 RefPtr
<Document
> document
= content
->GetComposedDoc();
512 document
->FlushPendingNotifications(FlushType::Display
);
513 return document
->GetShell();
519 nsresult
nsBaseDragService::DrawDrag(nsINode
* aDOMNode
,
520 const Maybe
<CSSIntRegion
>& aRegion
,
521 CSSIntPoint aScreenPosition
,
522 LayoutDeviceIntRect
* aScreenDragRect
,
523 RefPtr
<SourceSurface
>* aSurface
,
524 nsPresContext
** aPresContext
) {
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
);
545 RefPtr
<nsFrameLoader
> fl
= flo
->GetFrameLoader();
547 auto* tp
= static_cast<mozilla::dom::TabParent
*>(fl
->GetRemoteBrowser());
548 if (tp
&& tp
->TakeDragVisualization(*aSurface
, aScreenDragRect
)) {
550 // Just clear the surface if chrome has overridden it with an image.
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
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());
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();
588 dragRect
= frame
->GetScreenRect();
592 nsIntRect dragRectDev
=
593 ToAppUnits(dragRect
, AppUnitsPerCSSPixel())
594 .ToOutsidePixels((*aPresContext
)->AppUnitsPerDevPixel());
595 aScreenDragRect
->SizeTo(dragRectDev
.Width(), dragRectDev
.Height());
599 // draw the image for selections
601 LayoutDeviceIntPoint
pnt(aScreenDragRect
->TopLeft());
602 *aSurface
= presShell
->RenderSelection(
603 mSelection
, pnt
, aScreenDragRect
,
604 mImage
? 0 : nsIPresShell::RENDER_AUTO_SCALE
);
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.
612 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(dragNode
);
613 HTMLCanvasElement
* canvas
= HTMLCanvasElement::FromNodeOrNull(content
);
615 return DrawDragForImage(*aPresContext
, nullptr, canvas
, aScreenDragRect
,
619 nsCOMPtr
<nsIImageLoadingContent
> imageLoader
= do_QueryInterface(dragNode
);
620 // for image nodes, create the drag image from the actual image data
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
;
638 // otherwise, just draw the node
639 uint32_t renderFlags
= mImage
? 0 : nsIPresShell::RENDER_AUTO_SCALE
;
641 // check if the dragged node itself is an img element
642 if (dragNode
->NodeName().LowerCaseEqualsLiteral("img")) {
643 renderFlags
= renderFlags
| nsIPresShell::RENDER_IS_IMAGE
;
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(
652 // if the dragnode contains an image, set RENDER_IS_IMAGE flag
653 renderFlags
= renderFlags
| nsIPresShell::RENDER_IS_IMAGE
;
659 LayoutDeviceIntPoint
pnt(aScreenDragRect
->TopLeft());
660 *aSurface
= presShell
->RenderNode(dragNode
, aRegion
, pnt
, aScreenDragRect
,
664 // If an image was specified, reset the position from the offset that was
667 aScreenDragRect
->MoveTo(screenPoint
.x
, screenPoint
.y
);
673 nsresult
nsBaseDragService::DrawDragForImage(
674 nsPresContext
* aPresContext
, nsIImageLoadingContent
* aImageLoader
,
675 HTMLCanvasElement
* aCanvas
, LayoutDeviceIntRect
* aScreenDragRect
,
676 RefPtr
<SourceSurface
>* aSurface
) {
677 nsCOMPtr
<imgIContainer
> imgContainer
;
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
));
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
);
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
;
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
;
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();
732 *aSurface
= aCanvas
->GetSurfaceSnapshot();
738 LayoutDeviceIntPoint
nsBaseDragService::ConvertToUnscaledDevPixels(
739 nsPresContext
* aPresContext
, CSSIntPoint aScreenPosition
) {
741 aPresContext
->DeviceContext()->AppUnitsPerDevPixelAtUnitFullZoom();
742 return LayoutDeviceIntPoint(
743 nsPresContext::CSSPixelsToAppUnits(aScreenPosition
.x
) / adj
,
744 nsPresContext::CSSPixelsToAppUnits(aScreenPosition
.y
) / adj
);
748 nsBaseDragService::Suppress() {
749 EndDragSession(false, 0);
755 nsBaseDragService::Unsuppress() {
761 nsBaseDragService::UserCancelled() {
762 mUserCancelled
= true;
767 nsBaseDragService::UpdateDragEffect() {
768 mDragActionFromChildProcess
= mDragAction
;
773 nsBaseDragService::UpdateDragImage(nsINode
* aImage
, int32_t aImageX
,
775 // Don't change the image if this is a drag from another source or if there
777 if (!mSourceNode
|| mDragPopup
) return NS_OK
;
780 mImageOffset
= CSSIntPoint(aImageX
, aImageY
);
785 nsBaseDragService::DragEventDispatchedToChildProcess() {
786 mDragEventDispatchedToChildProcess
= true;
790 bool nsBaseDragService::MaybeAddChildProcess(
791 mozilla::dom::ContentParent
* aChild
) {
792 if (!mChildProcesses
.Contains(aChild
)) {
793 mChildProcesses
.AppendElement(aChild
);