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"
8 #include "mozilla/dom/ContentChild.h"
9 #include "mozilla/Unused.h"
10 #include "nsArrayUtils.h"
11 #include "nsClipboardProxy.h"
12 #include "nsISupportsPrimitives.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) {}
27 nsClipboardProxy::SetData(nsITransferable
* aTransferable
,
28 nsIClipboardOwner
* anOwner
, int32_t aWhichClipboard
) {
29 #if defined(ACCESSIBILITY) && defined(XP_WIN)
30 a11y::Compatibility::SuppressA11yForClipboardCopy();
33 ContentChild
* child
= ContentChild::GetSingleton();
35 IPCDataTransfer ipcDataTransfer
;
36 nsContentUtils::TransferableToIPCTransferable(aTransferable
, &ipcDataTransfer
,
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
,
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
,
60 return nsContentUtils::IPCTransferableToTransferable(
61 dataTransfer
, false /* aAddDataFlavor */, aTransferable
,
62 false /* aFilterUnknownFlavors */);
66 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard
) {
67 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
72 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
73 int32_t aWhichClipboard
,
77 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList
,
78 aWhichClipboard
, aHasType
);
84 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard
,
86 switch (aWhichClipboard
) {
87 case kGlobalClipboard
:
88 // We always support the global clipboard.
91 case kSelectionClipboard
:
92 *aIsSupported
= mClipboardCaps
.supportsSelectionClipboard();
95 *aIsSupported
= mClipboardCaps
.supportsFindClipboard();
98 *aIsSupported
= mClipboardCaps
.supportsSelectionCache();
102 *aIsSupported
= false;
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
)
117 GetMainThreadSerialEventTarget(), __func__
,
119 [promise
](nsTArray
<nsCString
> types
) {
120 promise
->Resolve(std::move(types
), __func__
);
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
);
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
)
148 GetMainThreadSerialEventTarget(), __func__
,
151 transferable
](const IPCDataTransferOrError
& ipcDataTransferOrError
) {
152 if (ipcDataTransferOrError
.type() ==
153 IPCDataTransferOrError::Tnsresult
) {
154 promise
->Reject(ipcDataTransferOrError
.get_nsresult(), __func__
);
158 nsresult rv
= nsContentUtils::IPCTransferableToTransferable(
159 ipcDataTransferOrError
.get_IPCDataTransfer(),
160 false /* aAddDataFlavor */, transferable
,
161 false /* aFilterUnknownFlavors */);
163 promise
->Reject(rv
, __func__
);
167 promise
->Resolve(true, __func__
);
170 [promise
](mozilla::ipc::ResponseRejectReason aReason
) {
171 promise
->Reject(NS_ERROR_FAILURE
, __func__
);
174 return promise
.forget();