Bug 1847324 - Add use counter for rewritable YouTube flash embeds r=emilio
[gecko.git] / widget / nsDragServiceProxy.cpp
blobcb273a89fad916211363431cfd801f77a146d2ec
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::IPCTransferableData> transferables;
38 nsContentUtils::TransferablesToIPCTransferableDatas(
39 aArrayTransferables, transferables, false, nullptr);
41 nsCOMPtr<nsIPrincipal> principal;
42 if (mSourceNode) {
43 principal = mSourceNode->NodePrincipal();
46 nsCOMPtr<nsIContentSecurityPolicy> csp;
47 if (mSourceDocument) {
48 csp = mSourceDocument->GetCsp();
49 // XXX why do we need this here? Shouldn't they be set properly in
50 // nsBaseDragService already?
51 mSourceWindowContext = mSourceDocument->GetWindowContext();
52 mSourceTopWindowContext = mSourceWindowContext
53 ? mSourceWindowContext->TopWindowContext()
54 : nullptr;
57 nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
58 cookieJarSettings = mSourceDocument->CookieJarSettings();
59 mozilla::net::CookieJarSettingsArgs csArgs;
60 mozilla::net::CookieJarSettings::Cast(cookieJarSettings)->Serialize(csArgs);
62 LayoutDeviceIntRect dragRect;
63 if (mHasImage || mSelection) {
64 nsPresContext* pc;
65 RefPtr<SourceSurface> surface;
66 DrawDrag(mSourceNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
68 if (surface) {
69 RefPtr<DataSourceSurface> dataSurface = surface->GetDataSurface();
70 if (dataSurface) {
71 size_t length;
72 int32_t stride;
73 auto surfaceData =
74 nsContentUtils::GetSurfaceData(*dataSurface, &length, &stride);
75 if (surfaceData.isNothing()) {
76 NS_WARNING("Failed to create shared memory for drag session.");
77 return NS_ERROR_FAILURE;
80 mozilla::Unused << child->SendInvokeDragSession(
81 std::move(transferables), aActionType, std::move(surfaceData),
82 stride, dataSurface->GetFormat(), dragRect, principal, csp, csArgs,
83 mSourceWindowContext, mSourceTopWindowContext);
84 StartDragSession();
85 return NS_OK;
90 mozilla::Unused << child->SendInvokeDragSession(
91 std::move(transferables), aActionType, Nothing(), 0,
92 static_cast<SurfaceFormat>(0), dragRect, principal, csp, csArgs,
93 mSourceWindowContext, mSourceTopWindowContext);
94 StartDragSession();
95 return NS_OK;