Bug 1812348 [wpt PR 38171] - wake lock: Move tests in web_tests/wake-lock to either...
[gecko.git] / widget / nsClipboardProxy.cpp
blob7f5f2336678b15a58aaaae56417942209eae73ef
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, child, nullptr);
39 bool isPrivateData = aTransferable->GetIsPrivateData();
40 nsCOMPtr<nsIPrincipal> requestingPrincipal =
41 aTransferable->GetRequestingPrincipal();
42 nsContentPolicyType contentPolicyType = aTransferable->GetContentPolicyType();
43 child->SendSetClipboard(std::move(ipcDataTransfer), isPrivateData,
44 requestingPrincipal, contentPolicyType,
45 aWhichClipboard);
47 return NS_OK;
50 NS_IMETHODIMP
51 nsClipboardProxy::GetData(nsITransferable* aTransferable,
52 int32_t aWhichClipboard) {
53 nsTArray<nsCString> types;
54 aTransferable->FlavorsTransferableCanImport(types);
56 IPCDataTransfer dataTransfer;
57 ContentChild::GetSingleton()->SendGetClipboard(types, aWhichClipboard,
58 &dataTransfer);
59 return nsContentUtils::IPCTransferableToTransferable(
60 dataTransfer, false /* aAddDataFlavor */, aTransferable,
61 false /* aFilterUnknownFlavors */);
64 NS_IMETHODIMP
65 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard) {
66 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard);
67 return NS_OK;
70 NS_IMETHODIMP
71 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
72 int32_t aWhichClipboard,
73 bool* aHasType) {
74 *aHasType = false;
76 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList,
77 aWhichClipboard, aHasType);
79 return NS_OK;
82 NS_IMETHODIMP
83 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard,
84 bool* aIsSupported) {
85 switch (aWhichClipboard) {
86 case kGlobalClipboard:
87 // We always support the global clipboard.
88 *aIsSupported = true;
89 return NS_OK;
90 case kSelectionClipboard:
91 *aIsSupported = mClipboardCaps.supportsSelectionClipboard();
92 return NS_OK;
93 case kFindClipboard:
94 *aIsSupported = mClipboardCaps.supportsFindClipboard();
95 return NS_OK;
96 case kSelectionCache:
97 *aIsSupported = mClipboardCaps.supportsSelectionCache();
98 return NS_OK;
101 *aIsSupported = false;
102 return NS_OK;
105 void nsClipboardProxy::SetCapabilities(
106 const ClipboardCapabilities& aClipboardCaps) {
107 mClipboardCaps = aClipboardCaps;
110 RefPtr<DataFlavorsPromise> nsClipboardProxy::AsyncHasDataMatchingFlavors(
111 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) {
112 auto promise = MakeRefPtr<DataFlavorsPromise::Private>(__func__);
113 ContentChild::GetSingleton()
114 ->SendClipboardHasTypesAsync(aFlavorList, aWhichClipboard)
115 ->Then(
116 GetMainThreadSerialEventTarget(), __func__,
117 /* resolve */
118 [promise](nsTArray<nsCString> types) {
119 promise->Resolve(std::move(types), __func__);
121 /* reject */
122 [promise](mozilla::ipc::ResponseRejectReason aReason) {
123 promise->Reject(NS_ERROR_FAILURE, __func__);
126 return promise.forget();
129 RefPtr<GenericPromise> nsClipboardProxy::AsyncGetData(
130 nsITransferable* aTransferable, int32_t aWhichClipboard) {
131 if (!aTransferable) {
132 return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
135 // Get a list of flavors this transferable can import
136 nsTArray<nsCString> flavors;
137 nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
138 if (NS_FAILED(rv)) {
139 return GenericPromise::CreateAndReject(rv, __func__);
142 nsCOMPtr<nsITransferable> transferable(aTransferable);
143 auto promise = MakeRefPtr<GenericPromise::Private>(__func__);
144 ContentChild::GetSingleton()
145 ->SendGetClipboardAsync(flavors, aWhichClipboard)
146 ->Then(
147 GetMainThreadSerialEventTarget(), __func__,
148 /* resolve */
149 [promise,
150 transferable](const IPCDataTransferOrError& ipcDataTransferOrError) {
151 if (ipcDataTransferOrError.type() ==
152 IPCDataTransferOrError::Tnsresult) {
153 promise->Reject(ipcDataTransferOrError.get_nsresult(), __func__);
154 return;
157 nsresult rv = nsContentUtils::IPCTransferableToTransferable(
158 ipcDataTransferOrError.get_IPCDataTransfer(),
159 false /* aAddDataFlavor */, transferable,
160 false /* aFilterUnknownFlavors */);
161 if (NS_FAILED(rv)) {
162 promise->Reject(rv, __func__);
163 return;
166 promise->Resolve(true, __func__);
168 /* reject */
169 [promise](mozilla::ipc::ResponseRejectReason aReason) {
170 promise->Reject(NS_ERROR_FAILURE, __func__);
173 return promise.forget();