Bug 1769628 [wpt PR 34081] - Update wpt metadata, a=testonly
[gecko.git] / widget / nsDragServiceProxy.cpp
blob0974495abc27a3ee69b374ac51ac7d556d7487ab
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;
18 using mozilla::Maybe;
19 using mozilla::Nothing;
20 using mozilla::Some;
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;
42 if (mSourceNode) {
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) {
59 nsPresContext* pc;
60 RefPtr<SourceSurface> surface;
61 DrawDrag(mSourceNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
63 if (surface) {
64 RefPtr<DataSourceSurface> dataSurface = surface->GetDataSurface();
65 if (dataSurface) {
66 size_t length;
67 int32_t stride;
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);
86 StartDragSession();
87 return NS_OK;
92 mozilla::Unused << child->SendInvokeDragSession(
93 dataTransfers, aActionType, Nothing(), 0, static_cast<SurfaceFormat>(0),
94 dragRect, principal, csp, csArgs, mSourceWindowContext);
95 StartDragSession();
96 return NS_OK;