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 "mozilla/dom/Document.h"
9 #include "mozilla/dom/BrowserChild.h"
10 #include "mozilla/gfx/2D.h"
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/Unused.h"
13 #include "nsContentUtils.h"
15 using mozilla::CSSIntRegion
;
16 using mozilla::LayoutDeviceIntRect
;
18 using mozilla::Nothing
;
20 using mozilla::dom::BrowserChild
;
21 using mozilla::gfx::DataSourceSurface
;
22 using mozilla::gfx::SourceSurface
;
23 using mozilla::gfx::SurfaceFormat
;
24 using mozilla::ipc::Shmem
;
26 nsDragServiceProxy::nsDragServiceProxy() = default;
28 nsDragServiceProxy::~nsDragServiceProxy() = default;
30 nsresult
nsDragServiceProxy::InvokeDragSessionImpl(
31 nsIArray
* aArrayTransferables
, const Maybe
<CSSIntRegion
>& aRegion
,
32 uint32_t aActionType
) {
33 NS_ENSURE_STATE(mSourceDocument
->GetDocShell());
34 BrowserChild
* child
= BrowserChild::GetFrom(mSourceDocument
->GetDocShell());
35 NS_ENSURE_STATE(child
);
36 nsTArray
<mozilla::dom::IPCDataTransfer
> dataTransfers
;
37 nsContentUtils::TransferablesToIPCTransferables(
38 aArrayTransferables
, dataTransfers
, false, child
->Manager(), nullptr);
40 nsCOMPtr
<nsIPrincipal
> principal
;
42 principal
= mSourceNode
->NodePrincipal();
45 nsCOMPtr
<nsIContentSecurityPolicy
> csp
;
46 if (mSourceDocument
) {
47 csp
= mSourceDocument
->GetCsp();
50 LayoutDeviceIntRect dragRect
;
51 if (mHasImage
|| mSelection
) {
53 RefPtr
<SourceSurface
> surface
;
54 DrawDrag(mSourceNode
, aRegion
, mScreenPosition
, &dragRect
, &surface
, &pc
);
57 RefPtr
<DataSourceSurface
> dataSurface
= surface
->GetDataSurface();
61 Maybe
<Shmem
> maybeShm
= nsContentUtils::GetSurfaceData(
62 dataSurface
, &length
, &stride
, child
);
63 if (maybeShm
.isNothing()) {
64 return NS_ERROR_FAILURE
;
67 auto surfaceData
= maybeShm
.value();
69 // Save the surface data to shared memory.
70 if (!surfaceData
.IsReadable() || !surfaceData
.get
<char>()) {
71 NS_WARNING("Failed to create shared memory for drag session.");
72 return NS_ERROR_FAILURE
;
75 mozilla::Unused
<< child
->SendInvokeDragSession(
76 dataTransfers
, aActionType
, Some(std::move(surfaceData
)), stride
,
77 dataSurface
->GetFormat(), dragRect
, principal
, csp
);
84 mozilla::Unused
<< child
->SendInvokeDragSession(
85 dataTransfers
, aActionType
, Nothing(), 0, static_cast<SurfaceFormat
>(0),
86 dragRect
, principal
, csp
);