Bug 1477919 [wpt PR 12154] - url: DecodeURLEscapeSequences() should not apply UTF...
[gecko.git] / widget / nsDragServiceProxy.cpp
blob60142b0b505586757036dd95d9a2b94501ca0097
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 "nsIDocument.h"
9 #include "nsISupportsPrimitives.h"
10 #include "mozilla/dom/TabChild.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/Unused.h"
14 #include "nsContentUtils.h"
16 using mozilla::ipc::Shmem;
17 using mozilla::dom::TabChild;
18 using mozilla::dom::OptionalShmem;
19 using mozilla::LayoutDeviceIntRect;
20 using mozilla::Maybe;
22 nsDragServiceProxy::nsDragServiceProxy()
26 nsDragServiceProxy::~nsDragServiceProxy()
30 static void
31 GetPrincipalURIFromNode(nsCOMPtr<nsINode>& sourceNode,
32 nsCString& aPrincipalURISpec)
34 if (!sourceNode) {
35 return;
38 nsCOMPtr<nsIPrincipal> principal = sourceNode->NodePrincipal();
39 nsCOMPtr<nsIURI> principalURI;
40 nsresult rv = principal->GetURI(getter_AddRefs(principalURI));
41 if (NS_FAILED(rv) || !principalURI) {
42 return;
45 principalURI->GetSpec(aPrincipalURISpec);
48 nsresult
49 nsDragServiceProxy::InvokeDragSessionImpl(nsIArray* aArrayTransferables,
50 nsIScriptableRegion* aRegion,
51 uint32_t aActionType)
53 NS_ENSURE_STATE(mSourceDocument->GetDocShell());
54 TabChild* child = TabChild::GetFrom(mSourceDocument->GetDocShell());
55 NS_ENSURE_STATE(child);
56 nsTArray<mozilla::dom::IPCDataTransfer> dataTransfers;
57 nsContentUtils::TransferablesToIPCTransferables(aArrayTransferables,
58 dataTransfers,
59 false,
60 child->Manager(),
61 nullptr);
63 nsCString principalURISpec;
64 GetPrincipalURIFromNode(mSourceNode, principalURISpec);
66 LayoutDeviceIntRect dragRect;
67 if (mHasImage || mSelection) {
68 nsPresContext* pc;
69 RefPtr<mozilla::gfx::SourceSurface> surface;
70 DrawDrag(mSourceNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
72 if (surface) {
73 RefPtr<mozilla::gfx::DataSourceSurface> dataSurface =
74 surface->GetDataSurface();
75 if (dataSurface) {
76 size_t length;
77 int32_t stride;
78 Maybe<Shmem> maybeShm = nsContentUtils::GetSurfaceData(dataSurface,
79 &length,
80 &stride,
81 child);
82 if (maybeShm.isNothing()) {
83 return NS_ERROR_FAILURE;
86 auto surfaceData = maybeShm.value();
88 // Save the surface data to shared memory.
89 if (!surfaceData.IsReadable() || !surfaceData.get<char>()) {
90 NS_WARNING("Failed to create shared memory for drag session.");
91 return NS_ERROR_FAILURE;
94 mozilla::Unused <<
95 child->SendInvokeDragSession(dataTransfers, aActionType, surfaceData,
96 stride, dataSurface->GetFormat(),
97 dragRect, principalURISpec);
98 StartDragSession();
99 return NS_OK;
104 mozilla::Unused << child->SendInvokeDragSession(dataTransfers, aActionType,
105 mozilla::void_t(), 0, static_cast<gfx::SurfaceFormat>(0), dragRect,
106 principalURISpec);
107 StartDragSession();
108 return NS_OK;