1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsDragServiceProxy.h"
8 #include "nsIDocument.h"
9 #include "nsISupportsPrimitives.h"
10 #include "mozilla/dom/TabChild.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/Unused.h"
14 #include "nsContentUtils.h"
16 using mozilla::ipc::Shmem
;
17 using mozilla::dom::TabChild
;
18 using mozilla::dom::OptionalShmem
;
19 using mozilla::LayoutDeviceIntRect
;
22 nsDragServiceProxy::nsDragServiceProxy()
26 nsDragServiceProxy::~nsDragServiceProxy()
31 GetPrincipalURIFromNode(nsCOMPtr
<nsINode
>& sourceNode
,
32 nsCString
& aPrincipalURISpec
)
38 nsCOMPtr
<nsIPrincipal
> principal
= sourceNode
->NodePrincipal();
39 nsCOMPtr
<nsIURI
> principalURI
;
40 nsresult rv
= principal
->GetURI(getter_AddRefs(principalURI
));
41 if (NS_FAILED(rv
) || !principalURI
) {
45 principalURI
->GetSpec(aPrincipalURISpec
);
49 nsDragServiceProxy::InvokeDragSessionImpl(nsIArray
* aArrayTransferables
,
50 nsIScriptableRegion
* aRegion
,
53 NS_ENSURE_STATE(mSourceDocument
->GetDocShell());
54 TabChild
* child
= TabChild::GetFrom(mSourceDocument
->GetDocShell());
55 NS_ENSURE_STATE(child
);
56 nsTArray
<mozilla::dom::IPCDataTransfer
> dataTransfers
;
57 nsContentUtils::TransferablesToIPCTransferables(aArrayTransferables
,
63 nsCString principalURISpec
;
64 GetPrincipalURIFromNode(mSourceNode
, principalURISpec
);
66 LayoutDeviceIntRect dragRect
;
67 if (mHasImage
|| mSelection
) {
69 RefPtr
<mozilla::gfx::SourceSurface
> surface
;
70 DrawDrag(mSourceNode
, aRegion
, mScreenPosition
, &dragRect
, &surface
, &pc
);
73 RefPtr
<mozilla::gfx::DataSourceSurface
> dataSurface
=
74 surface
->GetDataSurface();
78 Maybe
<Shmem
> maybeShm
= nsContentUtils::GetSurfaceData(dataSurface
,
82 if (maybeShm
.isNothing()) {
83 return NS_ERROR_FAILURE
;
86 auto surfaceData
= maybeShm
.value();
88 // Save the surface data to shared memory.
89 if (!surfaceData
.IsReadable() || !surfaceData
.get
<char>()) {
90 NS_WARNING("Failed to create shared memory for drag session.");
91 return NS_ERROR_FAILURE
;
95 child
->SendInvokeDragSession(dataTransfers
, aActionType
, surfaceData
,
96 stride
, dataSurface
->GetFormat(),
97 dragRect
, principalURISpec
);
104 mozilla::Unused
<< child
->SendInvokeDragSession(dataTransfers
, aActionType
,
105 mozilla::void_t(), 0, static_cast<gfx::SurfaceFormat
>(0), dragRect
,