Bug 1852149 Part 1 - Add margin-rule pref r=firefox-style-system-reviewers,emilio
[gecko.git] / widget / ClipboardWriteRequestParent.cpp
blob751befbe050a166d7c530d481ceecd84a3cbbdf2
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;
20 namespace mozilla {
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));
32 if (!clipboard) {
33 Unused << PClipboardWriteRequestParent::Send__delete__(this,
34 NS_ERROR_FAILURE);
35 return NS_ERROR_FAILURE;
38 nsresult rv = clipboard->AsyncSetData(aClipboardType, this,
39 getter_AddRefs(mAsyncSetClipboardData));
40 if (NS_FAILED(rv)) {
41 Unused << PClipboardWriteRequestParent::Send__delete__(this, rv);
42 return rv;
45 return NS_OK;
48 NS_IMETHODIMP ClipboardWriteRequestParent::OnComplete(nsresult aResult) {
49 nsCOMPtr<nsIAsyncSetClipboardData> clipboardData =
50 std::move(mAsyncSetClipboardData);
51 if (clipboardData) {
52 Unused << PClipboardWriteRequestParent::Send__delete__(this, aResult);
54 return NS_OK;
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) {
69 return IPC_OK();
72 nsresult rv = NS_ERROR_FAILURE;
73 nsCOMPtr<nsITransferable> trans =
74 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
75 if (NS_FAILED(rv)) {
76 mAsyncSetClipboardData->Abort(rv);
77 return IPC_OK();
80 trans->Init(nullptr);
81 rv = nsContentUtils::IPCTransferableToTransferable(
82 aTransferable, true /* aAddDataFlavor */, trans,
83 true /* aFilterUnknownFlavors */);
84 if (NS_FAILED(rv)) {
85 mAsyncSetClipboardData->Abort(rv);
86 return IPC_OK();
89 mAsyncSetClipboardData->SetData(trans, nullptr);
90 return IPC_OK();
93 IPCResult ClipboardWriteRequestParent::Recv__delete__(nsresult aReason) {
94 #ifndef FUZZING_SNAPSHOT
95 MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(aReason));
96 #endif
97 nsCOMPtr<nsIAsyncSetClipboardData> clipboardData =
98 std::move(mAsyncSetClipboardData);
99 if (clipboardData) {
100 clipboardData->Abort(aReason);
102 return IPC_OK();
105 void ClipboardWriteRequestParent::ActorDestroy(ActorDestroyReason aReason) {
106 nsCOMPtr<nsIAsyncSetClipboardData> clipboardData =
107 std::move(mAsyncSetClipboardData);
108 if (clipboardData) {
109 clipboardData->Abort(NS_ERROR_ABORT);
113 } // namespace mozilla