Bug 1867925 - Mark some storage-access-api tests as intermittent after wpt-sync....
[gecko.git] / widget / ClipboardReadRequestParent.cpp
blob3f045a800a6650efc609baffda97be6ccdb4c797
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;
18 namespace mozilla {
20 namespace {
22 class ClipboardGetDataCallback final : public nsIAsyncClipboardRequestCallback {
23 public:
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.
29 NS_DECL_ISUPPORTS
31 // nsIAsyncClipboardRequestCallback
32 NS_IMETHOD OnComplete(nsresult aResult) override {
33 mCallback(aResult);
34 return NS_OK;
37 protected:
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) {
47 nsresult rv;
48 nsCOMPtr<nsITransferable> trans =
49 do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
50 if (NS_FAILED(rv)) {
51 return Err(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);
69 } // namespace
71 IPCResult ClipboardReadRequestParent::RecvGetData(
72 const nsTArray<nsCString>& aFlavors, GetDataResolver&& aResolver) {
73 bool valid = false;
74 if (NS_FAILED(mAsyncGetClipboardData->GetValid(&valid)) || !valid) {
75 Unused << PClipboardReadRequestParent::Send__delete__(this);
76 aResolver(NS_ERROR_FAILURE);
77 return IPC_OK();
80 // Create transferable
81 auto result = CreateTransferable(aFlavors);
82 if (result.isErr()) {
83 aResolver(result.unwrapErr());
84 return IPC_OK();
87 nsCOMPtr<nsITransferable> trans = result.unwrap();
88 RefPtr<ClipboardGetDataCallback> callback =
89 MakeRefPtr<ClipboardGetDataCallback>([self = RefPtr{this},
90 resolver = std::move(aResolver),
91 trans,
92 manager = mManager](nsresult aRv) {
93 if (NS_FAILED(aRv)) {
94 bool valid = false;
95 if (NS_FAILED(self->mAsyncGetClipboardData->GetValid(&valid)) ||
96 !valid) {
97 Unused << PClipboardReadRequestParent::Send__delete__(self);
99 resolver(aRv);
100 return;
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);
109 if (NS_FAILED(rv)) {
110 callback->OnComplete(rv);
112 return IPC_OK();
115 } // namespace mozilla