Bug 1826564 [wpt PR 39394] - Update mypy, a=testonly
[gecko.git] / widget / nsClipboardProxy.cpp
blob2590947cf5a406d582b0201f58e3150b98d2da8a
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 #if defined(ACCESSIBILITY) && defined(XP_WIN)
6 # include "mozilla/a11y/Compatibility.h"
7 #endif
8 #include "mozilla/dom/ContentChild.h"
9 #include "mozilla/Unused.h"
10 #include "nsArrayUtils.h"
11 #include "nsClipboardProxy.h"
12 #include "nsISupportsPrimitives.h"
13 #include "nsCOMPtr.h"
14 #include "nsComponentManagerUtils.h"
15 #include "nsXULAppAPI.h"
16 #include "nsContentUtils.h"
17 #include "PermissionMessageUtils.h"
19 using namespace mozilla;
20 using namespace mozilla::dom;
22 NS_IMPL_ISUPPORTS(nsClipboardProxy, nsIClipboard, nsIClipboardProxy)
24 nsClipboardProxy::nsClipboardProxy() : mClipboardCaps(false, false, false) {}
26 NS_IMETHODIMP
27 nsClipboardProxy::SetData(nsITransferable* aTransferable,
28 nsIClipboardOwner* anOwner, int32_t aWhichClipboard) {
29 #if defined(ACCESSIBILITY) && defined(XP_WIN)
30 a11y::Compatibility::SuppressA11yForClipboardCopy();
31 #endif
33 ContentChild* child = ContentChild::GetSingleton();
35 IPCDataTransfer ipcDataTransfer;
36 nsContentUtils::TransferableToIPCTransferable(aTransferable, &ipcDataTransfer,
37 false, nullptr);
39 bool isPrivateData = aTransferable->GetIsPrivateData();
40 nsCOMPtr<nsIPrincipal> requestingPrincipal =
41 aTransferable->GetRequestingPrincipal();
42 nsContentPolicyType contentPolicyType = aTransferable->GetContentPolicyType();
43 nsCOMPtr<nsIReferrerInfo> referrerInfo = aTransferable->GetReferrerInfo();
44 child->SendSetClipboard(std::move(ipcDataTransfer), isPrivateData,
45 requestingPrincipal, contentPolicyType, referrerInfo,
46 aWhichClipboard);
48 return NS_OK;
51 NS_IMETHODIMP
52 nsClipboardProxy::GetData(nsITransferable* aTransferable,
53 int32_t aWhichClipboard) {
54 nsTArray<nsCString> types;
55 aTransferable->FlavorsTransferableCanImport(types);
57 IPCDataTransfer dataTransfer;
58 ContentChild::GetSingleton()->SendGetClipboard(types, aWhichClipboard,
59 &dataTransfer);
60 return nsContentUtils::IPCTransferableToTransferable(
61 dataTransfer, false /* aAddDataFlavor */, aTransferable,
62 false /* aFilterUnknownFlavors */);
65 NS_IMETHODIMP
66 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard) {
67 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard);
68 return NS_OK;
71 NS_IMETHODIMP
72 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
73 int32_t aWhichClipboard,
74 bool* aHasType) {
75 *aHasType = false;
77 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList,
78 aWhichClipboard, aHasType);
80 return NS_OK;
83 NS_IMETHODIMP
84 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard,
85 bool* aIsSupported) {
86 switch (aWhichClipboard) {
87 case kGlobalClipboard:
88 // We always support the global clipboard.
89 *aIsSupported = true;
90 return NS_OK;
91 case kSelectionClipboard:
92 *aIsSupported = mClipboardCaps.supportsSelectionClipboard();
93 return NS_OK;
94 case kFindClipboard:
95 *aIsSupported = mClipboardCaps.supportsFindClipboard();
96 return NS_OK;
97 case kSelectionCache:
98 *aIsSupported = mClipboardCaps.supportsSelectionCache();
99 return NS_OK;
102 *aIsSupported = false;
103 return NS_OK;
106 void nsClipboardProxy::SetCapabilities(
107 const ClipboardCapabilities& aClipboardCaps) {
108 mClipboardCaps = aClipboardCaps;
111 RefPtr<DataFlavorsPromise> nsClipboardProxy::AsyncHasDataMatchingFlavors(
112 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) {
113 auto promise = MakeRefPtr<DataFlavorsPromise::Private>(__func__);
114 ContentChild::GetSingleton()
115 ->SendClipboardHasTypesAsync(aFlavorList, aWhichClipboard)
116 ->Then(
117 GetMainThreadSerialEventTarget(), __func__,
118 /* resolve */
119 [promise](nsTArray<nsCString> types) {
120 promise->Resolve(std::move(types), __func__);
122 /* reject */
123 [promise](mozilla::ipc::ResponseRejectReason aReason) {
124 promise->Reject(NS_ERROR_FAILURE, __func__);
127 return promise.forget();
130 RefPtr<GenericPromise> nsClipboardProxy::AsyncGetData(
131 nsITransferable* aTransferable, int32_t aWhichClipboard) {
132 if (!aTransferable) {
133 return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
136 // Get a list of flavors this transferable can import
137 nsTArray<nsCString> flavors;
138 nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
139 if (NS_FAILED(rv)) {
140 return GenericPromise::CreateAndReject(rv, __func__);
143 nsCOMPtr<nsITransferable> transferable(aTransferable);
144 auto promise = MakeRefPtr<GenericPromise::Private>(__func__);
145 ContentChild::GetSingleton()
146 ->SendGetClipboardAsync(flavors, aWhichClipboard)
147 ->Then(
148 GetMainThreadSerialEventTarget(), __func__,
149 /* resolve */
150 [promise,
151 transferable](const IPCDataTransferOrError& ipcDataTransferOrError) {
152 if (ipcDataTransferOrError.type() ==
153 IPCDataTransferOrError::Tnsresult) {
154 promise->Reject(ipcDataTransferOrError.get_nsresult(), __func__);
155 return;
158 nsresult rv = nsContentUtils::IPCTransferableToTransferable(
159 ipcDataTransferOrError.get_IPCDataTransfer(),
160 false /* aAddDataFlavor */, transferable,
161 false /* aFilterUnknownFlavors */);
162 if (NS_FAILED(rv)) {
163 promise->Reject(rv, __func__);
164 return;
167 promise->Resolve(true, __func__);
169 /* reject */
170 [promise](mozilla::ipc::ResponseRejectReason aReason) {
171 promise->Reject(NS_ERROR_FAILURE, __func__);
174 return promise.forget();