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/ClipboardWriteRequestParent.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 static NS_DEFINE_CID(kCClipboardCID
, NS_CLIPBOARD_CID
);
17 using mozilla::dom::ContentParent
;
18 using mozilla::ipc::IPCResult
;
22 NS_IMPL_ISUPPORTS(ClipboardWriteRequestParent
, nsIAsyncClipboardRequestCallback
)
24 ClipboardWriteRequestParent::ClipboardWriteRequestParent(
25 ContentParent
* aManager
)
26 : mManager(aManager
) {}
28 ClipboardWriteRequestParent::~ClipboardWriteRequestParent() = default;
30 nsresult
ClipboardWriteRequestParent::Init(const int32_t& aClipboardType
) {
31 nsCOMPtr
<nsIClipboard
> clipboard(do_GetService(kCClipboardCID
));
33 Unused
<< PClipboardWriteRequestParent::Send__delete__(this,
35 return NS_ERROR_FAILURE
;
38 nsresult rv
= clipboard
->AsyncSetData(aClipboardType
, this,
39 getter_AddRefs(mAsyncSetClipboardData
));
41 Unused
<< PClipboardWriteRequestParent::Send__delete__(this, rv
);
48 NS_IMETHODIMP
ClipboardWriteRequestParent::OnComplete(nsresult aResult
) {
49 nsCOMPtr
<nsIAsyncSetClipboardData
> clipboardData
=
50 std::move(mAsyncSetClipboardData
);
52 Unused
<< PClipboardWriteRequestParent::Send__delete__(this, aResult
);
57 IPCResult
ClipboardWriteRequestParent::RecvSetData(
58 const IPCTransferable
& aTransferable
) {
59 if (!mManager
->ValidatePrincipal(
60 aTransferable
.requestingPrincipal(),
61 {ContentParent::ValidatePrincipalOptions::AllowNullPtr
,
62 ContentParent::ValidatePrincipalOptions::AllowExpanded
,
63 ContentParent::ValidatePrincipalOptions::AllowSystem
})) {
64 ContentParent::LogAndAssertFailedPrincipalValidationInfo(
65 aTransferable
.requestingPrincipal(), __func__
);
68 if (!mAsyncSetClipboardData
) {
72 nsresult rv
= NS_ERROR_FAILURE
;
73 nsCOMPtr
<nsITransferable
> trans
=
74 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv
);
76 mAsyncSetClipboardData
->Abort(rv
);
81 rv
= nsContentUtils::IPCTransferableToTransferable(
82 aTransferable
, true /* aAddDataFlavor */, trans
,
83 true /* aFilterUnknownFlavors */);
85 mAsyncSetClipboardData
->Abort(rv
);
89 mAsyncSetClipboardData
->SetData(trans
, nullptr);
93 IPCResult
ClipboardWriteRequestParent::Recv__delete__(nsresult aReason
) {
94 #ifndef FUZZING_SNAPSHOT
95 MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(aReason
));
97 nsCOMPtr
<nsIAsyncSetClipboardData
> clipboardData
=
98 std::move(mAsyncSetClipboardData
);
100 clipboardData
->Abort(aReason
);
105 void ClipboardWriteRequestParent::ActorDestroy(ActorDestroyReason aReason
) {
106 nsCOMPtr
<nsIAsyncSetClipboardData
> clipboardData
=
107 std::move(mAsyncSetClipboardData
);
109 clipboardData
->Abort(NS_ERROR_ABORT
);
113 } // namespace mozilla