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/ClipboardReadRequestChild.h"
11 #include "mozilla/ClipboardWriteRequestChild.h"
12 #include "mozilla/dom/ContentChild.h"
13 #include "mozilla/net/CookieJarSettings.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/dom/WindowGlobalChild.h"
16 #include "mozilla/Unused.h"
17 #include "nsArrayUtils.h"
18 #include "nsBaseClipboard.h"
19 #include "nsISupportsPrimitives.h"
21 #include "nsComponentManagerUtils.h"
22 #include "nsXULAppAPI.h"
23 #include "nsContentUtils.h"
24 #include "PermissionMessageUtils.h"
26 using namespace mozilla
;
27 using namespace mozilla::dom
;
29 NS_IMPL_ISUPPORTS(nsClipboardProxy
, nsIClipboard
, nsIClipboardProxy
)
31 nsClipboardProxy::nsClipboardProxy() : mClipboardCaps(false, false, false) {}
34 nsClipboardProxy::SetData(nsITransferable
* aTransferable
,
35 nsIClipboardOwner
* anOwner
, int32_t aWhichClipboard
) {
36 #if defined(ACCESSIBILITY) && defined(XP_WIN)
37 a11y::Compatibility::SuppressA11yForClipboardCopy();
40 ContentChild
* child
= ContentChild::GetSingleton();
41 IPCTransferable ipcTransferable
;
42 nsContentUtils::TransferableToIPCTransferable(aTransferable
, &ipcTransferable
,
44 child
->SendSetClipboard(std::move(ipcTransferable
), aWhichClipboard
);
48 NS_IMETHODIMP
nsClipboardProxy::AsyncSetData(
49 int32_t aWhichClipboard
, nsIAsyncClipboardRequestCallback
* aCallback
,
50 nsIAsyncSetClipboardData
** _retval
) {
51 RefPtr
<ClipboardWriteRequestChild
> request
=
52 MakeRefPtr
<ClipboardWriteRequestChild
>(aCallback
);
53 ContentChild::GetSingleton()->SendPClipboardWriteRequestConstructor(
54 request
, aWhichClipboard
);
55 request
.forget(_retval
);
60 nsClipboardProxy::GetData(nsITransferable
* aTransferable
,
61 int32_t aWhichClipboard
,
62 mozilla::dom::WindowContext
* aWindowContext
) {
63 MOZ_DIAGNOSTIC_ASSERT(aWindowContext
&& aWindowContext
->IsInProcess(),
64 "content clipboard reads must be associated with an "
65 "in-process WindowContext");
66 if (aWindowContext
->IsDiscarded()) {
67 return NS_ERROR_NOT_AVAILABLE
;
69 nsTArray
<nsCString
> types
;
70 aTransferable
->FlavorsTransferableCanImport(types
);
72 IPCTransferableData transferable
;
73 ContentChild::GetSingleton()->SendGetClipboard(types
, aWhichClipboard
,
74 aWindowContext
, &transferable
);
75 return nsContentUtils::IPCTransferableDataToTransferable(
76 transferable
, false /* aAddDataFlavor */, aTransferable
,
77 false /* aFilterUnknownFlavors */);
82 class AsyncGetClipboardDataProxy final
: public nsIAsyncGetClipboardData
{
84 explicit AsyncGetClipboardDataProxy(ClipboardReadRequestChild
* aActor
)
90 NS_DECL_NSIASYNCGETCLIPBOARDDATA
93 virtual ~AsyncGetClipboardDataProxy() {
95 if (mActor
->CanSend()) {
96 PClipboardReadRequestChild::Send__delete__(mActor
);
100 RefPtr
<ClipboardReadRequestChild
> mActor
;
103 NS_IMPL_ISUPPORTS(AsyncGetClipboardDataProxy
, nsIAsyncGetClipboardData
)
105 NS_IMETHODIMP
AsyncGetClipboardDataProxy::GetValid(bool* aOutResult
) {
107 *aOutResult
= mActor
->CanSend();
111 NS_IMETHODIMP
AsyncGetClipboardDataProxy::GetFlavorList(
112 nsTArray
<nsCString
>& aFlavorList
) {
114 aFlavorList
.AppendElements(mActor
->FlavorList());
118 NS_IMETHODIMP
AsyncGetClipboardDataProxy::GetData(
119 nsITransferable
* aTransferable
,
120 nsIAsyncClipboardRequestCallback
* aCallback
) {
121 if (!aTransferable
|| !aCallback
) {
122 return NS_ERROR_INVALID_ARG
;
125 // Get a list of flavors this transferable can import
126 nsTArray
<nsCString
> flavors
;
127 nsresult rv
= aTransferable
->FlavorsTransferableCanImport(flavors
);
133 // If the requested flavor is not in the list, throw an error.
134 for (const auto& flavor
: flavors
) {
135 if (!mActor
->FlavorList().Contains(flavor
)) {
136 return NS_ERROR_FAILURE
;
140 if (!mActor
->CanSend()) {
141 return aCallback
->OnComplete(NS_ERROR_FAILURE
);
144 mActor
->SendGetData(flavors
)->Then(
145 GetMainThreadSerialEventTarget(), __func__
,
147 [self
= RefPtr
{this}, callback
= nsCOMPtr
{aCallback
},
148 transferable
= nsCOMPtr
{aTransferable
}](
149 const IPCTransferableDataOrError
& aIpcTransferableDataOrError
) {
150 if (aIpcTransferableDataOrError
.type() ==
151 IPCTransferableDataOrError::Tnsresult
) {
152 MOZ_ASSERT(NS_FAILED(aIpcTransferableDataOrError
.get_nsresult()));
153 callback
->OnComplete(aIpcTransferableDataOrError
.get_nsresult());
157 nsresult rv
= nsContentUtils::IPCTransferableDataToTransferable(
158 aIpcTransferableDataOrError
.get_IPCTransferableData(),
159 false /* aAddDataFlavor */, transferable
,
160 false /* aFilterUnknownFlavors */);
162 callback
->OnComplete(rv
);
166 callback
->OnComplete(NS_OK
);
170 nsCOMPtr
{aCallback
}](mozilla::ipc::ResponseRejectReason aReason
) {
171 callback
->OnComplete(NS_ERROR_FAILURE
);
179 NS_IMETHODIMP
nsClipboardProxy::AsyncGetData(
180 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
181 mozilla::dom::WindowContext
* aRequestingWindowContext
,
182 nsIPrincipal
* aRequestingPrincipal
,
183 nsIAsyncClipboardGetCallback
* aCallback
) {
184 if (!aCallback
|| !aRequestingPrincipal
|| aFlavorList
.IsEmpty()) {
185 return NS_ERROR_INVALID_ARG
;
188 if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
)) {
189 MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__
,
191 return NS_ERROR_FAILURE
;
194 ContentChild::GetSingleton()
195 ->SendGetClipboardAsync(aFlavorList
, aWhichClipboard
,
196 aRequestingWindowContext
,
197 WrapNotNull(aRequestingPrincipal
))
199 GetMainThreadSerialEventTarget(), __func__
,
201 [callback
= nsCOMPtr
{aCallback
}](const PClipboardReadRequestOrError
&
202 aClipboardReadRequestOrError
) {
203 if (aClipboardReadRequestOrError
.type() ==
204 PClipboardReadRequestOrError::Tnsresult
) {
206 NS_FAILED(aClipboardReadRequestOrError
.get_nsresult()));
207 callback
->OnError(aClipboardReadRequestOrError
.get_nsresult());
211 auto asyncGetClipboardData
= MakeRefPtr
<AsyncGetClipboardDataProxy
>(
212 static_cast<ClipboardReadRequestChild
*>(
213 aClipboardReadRequestOrError
.get_PClipboardReadRequest()
217 callback
->OnSuccess(asyncGetClipboardData
);
220 [callback
= nsCOMPtr
{aCallback
}](
221 mozilla::ipc::ResponseRejectReason aReason
) {
222 callback
->OnError(NS_ERROR_FAILURE
);
228 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard
) {
229 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
234 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
235 int32_t aWhichClipboard
,
239 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList
,
240 aWhichClipboard
, aHasType
);
246 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard
,
247 bool* aIsSupported
) {
248 switch (aWhichClipboard
) {
249 case kGlobalClipboard
:
250 // We always support the global clipboard.
251 *aIsSupported
= true;
253 case kSelectionClipboard
:
254 *aIsSupported
= mClipboardCaps
.supportsSelectionClipboard();
257 *aIsSupported
= mClipboardCaps
.supportsFindClipboard();
259 case kSelectionCache
:
260 *aIsSupported
= mClipboardCaps
.supportsSelectionCache();
264 *aIsSupported
= false;
268 void nsClipboardProxy::SetCapabilities(
269 const ClipboardCapabilities
& aClipboardCaps
) {
270 mClipboardCaps
= aClipboardCaps
;