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 #include "nsClipboardProxy.h"
7 #if defined(ACCESSIBILITY) && defined(XP_WIN)
8 # include "mozilla/a11y/Compatibility.h"
10 #include "mozilla/ClipboardWriteRequestChild.h"
11 #include "mozilla/dom/ContentChild.h"
12 #include "mozilla/net/CookieJarSettings.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/Unused.h"
15 #include "nsArrayUtils.h"
16 #include "nsISupportsPrimitives.h"
18 #include "nsComponentManagerUtils.h"
19 #include "nsXULAppAPI.h"
20 #include "nsContentUtils.h"
21 #include "PermissionMessageUtils.h"
23 using namespace mozilla
;
24 using namespace mozilla::dom
;
26 NS_IMPL_ISUPPORTS(nsClipboardProxy
, nsIClipboard
, nsIClipboardProxy
)
28 nsClipboardProxy::nsClipboardProxy() : mClipboardCaps(false, false, false) {}
31 nsClipboardProxy::SetData(nsITransferable
* aTransferable
,
32 nsIClipboardOwner
* anOwner
, int32_t aWhichClipboard
) {
33 #if defined(ACCESSIBILITY) && defined(XP_WIN)
34 a11y::Compatibility::SuppressA11yForClipboardCopy();
37 ContentChild
* child
= ContentChild::GetSingleton();
38 IPCTransferable ipcTransferable
;
39 nsContentUtils::TransferableToIPCTransferable(aTransferable
, &ipcTransferable
,
41 child
->SendSetClipboard(std::move(ipcTransferable
), aWhichClipboard
);
45 NS_IMETHODIMP
nsClipboardProxy::AsyncSetData(
46 int32_t aWhichClipboard
, nsIAsyncSetClipboardDataCallback
* aCallback
,
47 nsIAsyncSetClipboardData
** _retval
) {
48 RefPtr
<ClipboardWriteRequestChild
> request
=
49 MakeRefPtr
<ClipboardWriteRequestChild
>(aCallback
);
50 ContentChild::GetSingleton()->SendPClipboardWriteRequestConstructor(
51 request
, aWhichClipboard
);
52 request
.forget(_retval
);
57 nsClipboardProxy::GetData(nsITransferable
* aTransferable
,
58 int32_t aWhichClipboard
) {
59 nsTArray
<nsCString
> types
;
60 aTransferable
->FlavorsTransferableCanImport(types
);
62 IPCTransferableData transferable
;
63 ContentChild::GetSingleton()->SendGetClipboard(types
, aWhichClipboard
,
65 return nsContentUtils::IPCTransferableDataToTransferable(
66 transferable
, false /* aAddDataFlavor */, aTransferable
,
67 false /* aFilterUnknownFlavors */);
71 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard
) {
72 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
77 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
78 int32_t aWhichClipboard
,
82 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList
,
83 aWhichClipboard
, aHasType
);
89 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard
,
91 switch (aWhichClipboard
) {
92 case kGlobalClipboard
:
93 // We always support the global clipboard.
96 case kSelectionClipboard
:
97 *aIsSupported
= mClipboardCaps
.supportsSelectionClipboard();
100 *aIsSupported
= mClipboardCaps
.supportsFindClipboard();
102 case kSelectionCache
:
103 *aIsSupported
= mClipboardCaps
.supportsSelectionCache();
107 *aIsSupported
= false;
111 void nsClipboardProxy::SetCapabilities(
112 const ClipboardCapabilities
& aClipboardCaps
) {
113 mClipboardCaps
= aClipboardCaps
;
116 RefPtr
<DataFlavorsPromise
> nsClipboardProxy::AsyncHasDataMatchingFlavors(
117 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
) {
118 auto promise
= MakeRefPtr
<DataFlavorsPromise::Private
>(__func__
);
119 ContentChild::GetSingleton()
120 ->SendClipboardHasTypesAsync(aFlavorList
, aWhichClipboard
)
122 GetMainThreadSerialEventTarget(), __func__
,
124 [promise
](nsTArray
<nsCString
> types
) {
125 promise
->Resolve(std::move(types
), __func__
);
128 [promise
](mozilla::ipc::ResponseRejectReason aReason
) {
129 promise
->Reject(NS_ERROR_FAILURE
, __func__
);
132 return promise
.forget();
135 RefPtr
<GenericPromise
> nsClipboardProxy::AsyncGetData(
136 nsITransferable
* aTransferable
, int32_t aWhichClipboard
) {
137 if (!aTransferable
) {
138 return GenericPromise::CreateAndReject(NS_ERROR_FAILURE
, __func__
);
141 // Get a list of flavors this transferable can import
142 nsTArray
<nsCString
> flavors
;
143 nsresult rv
= aTransferable
->FlavorsTransferableCanImport(flavors
);
145 return GenericPromise::CreateAndReject(rv
, __func__
);
148 nsCOMPtr
<nsITransferable
> transferable(aTransferable
);
149 auto promise
= MakeRefPtr
<GenericPromise::Private
>(__func__
);
150 ContentChild::GetSingleton()
151 ->SendGetClipboardAsync(flavors
, aWhichClipboard
)
153 GetMainThreadSerialEventTarget(), __func__
,
155 [promise
, transferable
](
156 const IPCTransferableDataOrError
& ipcTransferableDataOrError
) {
157 if (ipcTransferableDataOrError
.type() ==
158 IPCTransferableDataOrError::Tnsresult
) {
159 promise
->Reject(ipcTransferableDataOrError
.get_nsresult(),
164 nsresult rv
= nsContentUtils::IPCTransferableDataToTransferable(
165 ipcTransferableDataOrError
.get_IPCTransferableData(),
166 false /* aAddDataFlavor */, transferable
,
167 false /* aFilterUnknownFlavors */);
169 promise
->Reject(rv
, __func__
);
173 promise
->Resolve(true, __func__
);
176 [promise
](mozilla::ipc::ResponseRejectReason aReason
) {
177 promise
->Reject(NS_ERROR_FAILURE
, __func__
);
180 return promise
.forget();