Bug 1715716 [wpt PR 29323] - Use unittest.mock instead of the mock package, a=testonly
[gecko.git] / widget / nsDragServiceProxy.cpp
blob428b114ce598fb5f034e462c32b4d4a69e7f2b3b
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();
51 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
52 cookieJarSettings = mSourceDocument->CookieJarSettings();
53 net::CookieJarSettingsArgs csArgs;
54 net::CookieJarSettings::Cast(cookieJarSettings)->Serialize(csArgs);
56 LayoutDeviceIntRect dragRect;
57 if (mHasImage || mSelection) {
58 nsPresContext* pc;
59 RefPtr<SourceSurface> surface;
60 DrawDrag(mSourceNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
62 if (surface) {
63 RefPtr<DataSourceSurface> dataSurface = surface->GetDataSurface();
64 if (dataSurface) {
65 size_t length;
66 int32_t stride;
67 Maybe<Shmem> maybeShm = nsContentUtils::GetSurfaceData(
68 dataSurface, &length, &stride, child);
69 if (maybeShm.isNothing()) {
70 return NS_ERROR_FAILURE;
73 auto surfaceData = maybeShm.value();
75 // Save the surface data to shared memory.
76 if (!surfaceData.IsReadable() || !surfaceData.get<char>()) {
77 NS_WARNING("Failed to create shared memory for drag session.");
78 return NS_ERROR_FAILURE;
81 mozilla::Unused << child->SendInvokeDragSession(
82 dataTransfers, aActionType, Some(std::move(surfaceData)), stride,
83 dataSurface->GetFormat(), dragRect, principal, csp, csArgs);
84 StartDragSession();
85 return NS_OK;
90 mozilla::Unused << child->SendInvokeDragSession(
91 dataTransfers, aActionType, Nothing(), 0, static_cast<SurfaceFormat>(0),
92 dragRect, principal, csp, csArgs);
93 StartDragSession();
94 return NS_OK;