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 child
->SendSetClipboard(std::move(ipcDataTransfer
), isPrivateData
,
44 requestingPrincipal
, contentPolicyType
,
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
,
59 return nsContentUtils::IPCTransferableToTransferable(
60 dataTransfer
, false /* aAddDataFlavor */, aTransferable
,
61 false /* aFilterUnknownFlavors */);
65 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard
) {
66 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
71 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
72 int32_t aWhichClipboard
,
76 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList
,
77 aWhichClipboard
, aHasType
);
83 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard
,
85 switch (aWhichClipboard
) {
86 case kGlobalClipboard
:
87 // We always support the global clipboard.
90 case kSelectionClipboard
:
91 *aIsSupported
= mClipboardCaps
.supportsSelectionClipboard();
94 *aIsSupported
= mClipboardCaps
.supportsFindClipboard();
97 *aIsSupported
= mClipboardCaps
.supportsSelectionCache();
101 *aIsSupported
= false;
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
)
116 GetMainThreadSerialEventTarget(), __func__
,
118 [promise
](nsTArray
<nsCString
> types
) {
119 promise
->Resolve(std::move(types
), __func__
);
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
);
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
)
147 GetMainThreadSerialEventTarget(), __func__
,
150 transferable
](const IPCDataTransferOrError
& ipcDataTransferOrError
) {
151 if (ipcDataTransferOrError
.type() ==
152 IPCDataTransferOrError::Tnsresult
) {
153 promise
->Reject(ipcDataTransferOrError
.get_nsresult(), __func__
);
157 nsresult rv
= nsContentUtils::IPCTransferableToTransferable(
158 ipcDataTransferOrError
.get_IPCDataTransfer(),
159 false /* aAddDataFlavor */, transferable
,
160 false /* aFilterUnknownFlavors */);
162 promise
->Reject(rv
, __func__
);
166 promise
->Resolve(true, __func__
);
169 [promise
](mozilla::ipc::ResponseRejectReason aReason
) {
170 promise
->Reject(NS_ERROR_FAILURE
, __func__
);
173 return promise
.forget();