1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsContentAreaDragDrop.h"
6 #include "RemoteDragStartData.h"
7 #include "nsContentUtils.h"
8 #include "nsICookieJarSettings.h"
10 #include "mozilla/dom/BlobImpl.h"
11 #include "mozilla/dom/BrowserParent.h"
12 #include "mozilla/dom/IPCBlobUtils.h"
13 #include "mozilla/dom/DOMTypes.h"
14 #include "ProtocolUtils.h"
16 using namespace mozilla::ipc
;
18 namespace mozilla::dom
{
20 RemoteDragStartData::~RemoteDragStartData() = default;
22 RemoteDragStartData::RemoteDragStartData(
23 BrowserParent
* aBrowserParent
, nsTArray
<IPCDataTransfer
>&& aDataTransfer
,
24 const LayoutDeviceIntRect
& aRect
, nsIPrincipal
* aPrincipal
,
25 nsIContentSecurityPolicy
* aCsp
, nsICookieJarSettings
* aCookieJarSettings
)
26 : mBrowserParent(aBrowserParent
),
27 mDataTransfer(std::move(aDataTransfer
)),
29 mPrincipal(aPrincipal
),
31 mCookieJarSettings(aCookieJarSettings
) {}
33 void RemoteDragStartData::AddInitialDnDDataTo(
34 DataTransfer
* aDataTransfer
, nsIPrincipal
** aPrincipal
,
35 nsIContentSecurityPolicy
** aCsp
,
36 nsICookieJarSettings
** aCookieJarSettings
) {
37 NS_IF_ADDREF(*aPrincipal
= mPrincipal
);
38 NS_IF_ADDREF(*aCsp
= mCsp
);
39 NS_IF_ADDREF(*aCookieJarSettings
= mCookieJarSettings
);
41 for (uint32_t i
= 0; i
< mDataTransfer
.Length(); ++i
) {
42 nsTArray
<IPCDataTransferItem
>& itemArray
= mDataTransfer
[i
].items();
43 for (auto& item
: itemArray
) {
44 RefPtr
<nsVariantCC
> variant
= new nsVariantCC();
45 // Special case kFilePromiseMime so that we get the right
46 // nsIFlavorDataProvider for it.
47 if (item
.flavor().EqualsLiteral(kFilePromiseMime
)) {
48 RefPtr
<nsISupports
> flavorDataProvider
=
49 new nsContentAreaDragDropDataProvider();
50 variant
->SetAsISupports(flavorDataProvider
);
51 } else if (item
.data().type() == IPCDataTransferData::TnsString
) {
52 variant
->SetAsAString(item
.data().get_nsString());
53 } else if (item
.data().type() == IPCDataTransferData::TIPCBlob
) {
54 RefPtr
<BlobImpl
> impl
=
55 IPCBlobUtils::Deserialize(item
.data().get_IPCBlob());
56 variant
->SetAsISupports(impl
);
57 } else if (item
.data().type() == IPCDataTransferData::TShmem
) {
58 if (nsContentUtils::IsFlavorImage(item
.flavor())) {
59 // An image! Get the imgIContainer for it and set it in the variant.
60 nsCOMPtr
<imgIContainer
> imageContainer
;
61 nsresult rv
= nsContentUtils::DataTransferItemToImage(
62 item
, getter_AddRefs(imageContainer
));
66 variant
->SetAsISupports(imageContainer
);
68 Shmem data
= item
.data().get_Shmem();
69 variant
->SetAsACString(
70 nsDependentCSubstring(data
.get
<char>(), data
.Size
<char>()));
73 mozilla::Unused
<< mBrowserParent
->DeallocShmem(
74 item
.data().get_Shmem());
77 // We set aHidden to false, as we don't need to worry about hiding data
78 // from content in the parent process where there is no content.
79 aDataTransfer
->SetDataWithPrincipalFromOtherProcess(
80 NS_ConvertUTF8toUTF16(item
.flavor()), variant
, i
, mPrincipal
,
81 /* aHidden = */ false);
85 // Clear things that are no longer needed.
86 mDataTransfer
.Clear();
90 } // namespace mozilla::dom