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/net/CookieJarSettings.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/Unused.h"
14 #include "nsContentUtils.h"
16 using mozilla::CSSIntRegion
;
17 using mozilla::LayoutDeviceIntRect
;
19 using mozilla::Nothing
;
21 using mozilla::dom::BrowserChild
;
22 using mozilla::gfx::DataSourceSurface
;
23 using mozilla::gfx::SourceSurface
;
24 using mozilla::gfx::SurfaceFormat
;
25 using mozilla::ipc::Shmem
;
27 nsDragServiceProxy::nsDragServiceProxy() = default;
29 nsDragServiceProxy::~nsDragServiceProxy() = default;
31 nsresult
nsDragServiceProxy::InvokeDragSessionImpl(
32 nsIArray
* aArrayTransferables
, const Maybe
<CSSIntRegion
>& aRegion
,
33 uint32_t aActionType
) {
34 NS_ENSURE_STATE(mSourceDocument
->GetDocShell());
35 BrowserChild
* child
= BrowserChild::GetFrom(mSourceDocument
->GetDocShell());
36 NS_ENSURE_STATE(child
);
37 nsTArray
<mozilla::dom::IPCDataTransfer
> dataTransfers
;
38 nsContentUtils::TransferablesToIPCTransferables(
39 aArrayTransferables
, dataTransfers
, false, child
->Manager(), nullptr);
41 nsCOMPtr
<nsIPrincipal
> principal
;
43 principal
= mSourceNode
->NodePrincipal();
46 nsCOMPtr
<nsIContentSecurityPolicy
> csp
;
47 if (mSourceDocument
) {
48 csp
= mSourceDocument
->GetCsp();
49 mSourceWindowContext
= mSourceDocument
->GetWindowContext();
52 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
;
53 cookieJarSettings
= mSourceDocument
->CookieJarSettings();
54 mozilla::net::CookieJarSettingsArgs csArgs
;
55 mozilla::net::CookieJarSettings::Cast(cookieJarSettings
)->Serialize(csArgs
);
57 LayoutDeviceIntRect dragRect
;
58 if (mHasImage
|| mSelection
) {
60 RefPtr
<SourceSurface
> surface
;
61 DrawDrag(mSourceNode
, aRegion
, mScreenPosition
, &dragRect
, &surface
, &pc
);
64 RefPtr
<DataSourceSurface
> dataSurface
= surface
->GetDataSurface();
68 Maybe
<Shmem
> maybeShm
= nsContentUtils::GetSurfaceData(
69 *dataSurface
, &length
, &stride
, child
);
70 if (maybeShm
.isNothing()) {
71 return NS_ERROR_FAILURE
;
74 auto surfaceData
= maybeShm
.value();
76 // Save the surface data to shared memory.
77 if (!surfaceData
.IsReadable() || !surfaceData
.get
<char>()) {
78 NS_WARNING("Failed to create shared memory for drag session.");
79 return NS_ERROR_FAILURE
;
82 mozilla::Unused
<< child
->SendInvokeDragSession(
83 dataTransfers
, aActionType
, Some(std::move(surfaceData
)), stride
,
84 dataSurface
->GetFormat(), dragRect
, principal
, csp
, csArgs
,
85 mSourceWindowContext
);
92 mozilla::Unused
<< child
->SendInvokeDragSession(
93 dataTransfers
, aActionType
, Nothing(), 0, static_cast<SurfaceFormat
>(0),
94 dragRect
, principal
, csp
, csArgs
, mSourceWindowContext
);