1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/ClipboardReadRequestParent.h"
8 #include "mozilla/dom/ContentParent.h"
9 #include "mozilla/net/CookieJarSettings.h"
10 #include "nsComponentManagerUtils.h"
11 #include "nsIClipboard.h"
12 #include "nsITransferable.h"
13 #include "nsWidgetsCID.h"
15 using mozilla::dom::ContentParent
;
16 using mozilla::ipc::IPCResult
;
22 class ClipboardGetDataCallback final
: public nsIAsyncClipboardRequestCallback
{
24 explicit ClipboardGetDataCallback(std::function
<void(nsresult
)>&& aCallback
)
25 : mCallback(std::move(aCallback
)) {}
27 // This object will never be held by a cycle-collected object, so it doesn't
28 // need to be cycle-collected despite holding alive cycle-collected objects.
31 // nsIAsyncClipboardRequestCallback
32 NS_IMETHOD
OnComplete(nsresult aResult
) override
{
38 ~ClipboardGetDataCallback() = default;
40 std::function
<void(nsresult
)> mCallback
;
43 NS_IMPL_ISUPPORTS(ClipboardGetDataCallback
, nsIAsyncClipboardRequestCallback
)
45 static Result
<nsCOMPtr
<nsITransferable
>, nsresult
> CreateTransferable(
46 const nsTArray
<nsCString
>& aTypes
) {
48 nsCOMPtr
<nsITransferable
> trans
=
49 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv
);
54 MOZ_TRY(trans
->Init(nullptr));
55 // The private flag is only used to prevent the data from being cached to the
56 // disk. The flag is not exported to the IPCDataTransfer object.
57 // The flag is set because we are not sure whether the clipboard data is used
58 // in a private browsing context. The transferable is only used in this scope,
59 // so the cache would not reduce memory consumption anyway.
60 trans
->SetIsPrivateData(true);
61 // Fill out flavors for transferable
62 for (uint32_t t
= 0; t
< aTypes
.Length(); t
++) {
63 MOZ_TRY(trans
->AddDataFlavor(aTypes
[t
].get()));
66 return std::move(trans
);
71 IPCResult
ClipboardReadRequestParent::RecvGetData(
72 const nsTArray
<nsCString
>& aFlavors
, GetDataResolver
&& aResolver
) {
74 if (NS_FAILED(mAsyncGetClipboardData
->GetValid(&valid
)) || !valid
) {
75 Unused
<< PClipboardReadRequestParent::Send__delete__(this);
76 aResolver(NS_ERROR_FAILURE
);
80 // Create transferable
81 auto result
= CreateTransferable(aFlavors
);
83 aResolver(result
.unwrapErr());
87 nsCOMPtr
<nsITransferable
> trans
= result
.unwrap();
88 RefPtr
<ClipboardGetDataCallback
> callback
=
89 MakeRefPtr
<ClipboardGetDataCallback
>([self
= RefPtr
{this},
90 resolver
= std::move(aResolver
),
92 manager
= mManager
](nsresult aRv
) {
95 if (NS_FAILED(self
->mAsyncGetClipboardData
->GetValid(&valid
)) ||
97 Unused
<< PClipboardReadRequestParent::Send__delete__(self
);
103 dom::IPCTransferableData ipcTransferableData
;
104 nsContentUtils::TransferableToIPCTransferableData(
105 trans
, &ipcTransferableData
, false /* aInSyncMessage */, manager
);
106 resolver(std::move(ipcTransferableData
));
108 nsresult rv
= mAsyncGetClipboardData
->GetData(trans
, callback
);
110 callback
->OnComplete(rv
);
115 } // namespace mozilla