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
);
177 static Result
<RefPtr
<AsyncGetClipboardDataProxy
>, nsresult
>
178 CreateAsyncGetClipboardDataProxy(
179 ClipboardReadRequestOrError
&& aClipboardReadRequestOrError
) {
180 if (aClipboardReadRequestOrError
.type() ==
181 ClipboardReadRequestOrError::Tnsresult
) {
182 MOZ_ASSERT(NS_FAILED(aClipboardReadRequestOrError
.get_nsresult()));
183 return Err(aClipboardReadRequestOrError
.get_nsresult());
186 ClipboardReadRequest
& request
=
187 aClipboardReadRequestOrError
.get_ClipboardReadRequest();
188 auto requestChild
= MakeRefPtr
<ClipboardReadRequestChild
>(
189 std::move(request
.availableTypes()));
191 !ContentChild::GetSingleton()->BindPClipboardReadRequestEndpoint(
192 std::move(request
.childEndpoint()), requestChild
))) {
193 return Err(NS_ERROR_FAILURE
);
196 return MakeRefPtr
<AsyncGetClipboardDataProxy
>(requestChild
);
201 NS_IMETHODIMP
nsClipboardProxy::AsyncGetData(
202 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
203 mozilla::dom::WindowContext
* aRequestingWindowContext
,
204 nsIPrincipal
* aRequestingPrincipal
,
205 nsIAsyncClipboardGetCallback
* aCallback
) {
206 if (!aCallback
|| !aRequestingPrincipal
|| aFlavorList
.IsEmpty()) {
207 return NS_ERROR_INVALID_ARG
;
210 if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
)) {
211 MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__
,
213 return NS_ERROR_FAILURE
;
216 ContentChild::GetSingleton()
217 ->SendGetClipboardAsync(aFlavorList
, aWhichClipboard
,
218 aRequestingWindowContext
,
219 WrapNotNull(aRequestingPrincipal
))
221 GetMainThreadSerialEventTarget(), __func__
,
223 [callback
= nsCOMPtr
{aCallback
}](
224 ClipboardReadRequestOrError
&& aClipboardReadRequestOrError
) {
225 auto result
= CreateAsyncGetClipboardDataProxy(
226 std::move(aClipboardReadRequestOrError
));
227 if (result
.isErr()) {
228 callback
->OnError(result
.unwrapErr());
232 callback
->OnSuccess(result
.inspect());
235 [callback
= nsCOMPtr
{aCallback
}](
236 mozilla::ipc::ResponseRejectReason aReason
) {
237 callback
->OnError(NS_ERROR_FAILURE
);
242 NS_IMETHODIMP
nsClipboardProxy::GetDataSnapshotSync(
243 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
244 mozilla::dom::WindowContext
* aRequestingWindowContext
,
245 nsIAsyncGetClipboardData
** _retval
) {
248 if (aFlavorList
.IsEmpty()) {
249 return NS_ERROR_INVALID_ARG
;
252 if (!nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
)) {
253 MOZ_CLIPBOARD_LOG("%s: clipboard %d is not supported.", __FUNCTION__
,
255 return NS_ERROR_FAILURE
;
258 ContentChild
* contentChild
= ContentChild::GetSingleton();
259 ClipboardReadRequestOrError requestOrError
;
260 contentChild
->SendGetClipboardDataSnapshotSync(
261 aFlavorList
, aWhichClipboard
, aRequestingWindowContext
, &requestOrError
);
262 auto result
= CreateAsyncGetClipboardDataProxy(std::move(requestOrError
));
263 if (result
.isErr()) {
264 return result
.unwrapErr();
267 result
.unwrap().forget(_retval
);
272 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard
) {
273 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
278 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
279 int32_t aWhichClipboard
,
283 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList
,
284 aWhichClipboard
, aHasType
);
290 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard
,
291 bool* aIsSupported
) {
292 switch (aWhichClipboard
) {
293 case kGlobalClipboard
:
294 // We always support the global clipboard.
295 *aIsSupported
= true;
297 case kSelectionClipboard
:
298 *aIsSupported
= mClipboardCaps
.supportsSelectionClipboard();
301 *aIsSupported
= mClipboardCaps
.supportsFindClipboard();
303 case kSelectionCache
:
304 *aIsSupported
= mClipboardCaps
.supportsSelectionCache();
308 *aIsSupported
= false;
312 void nsClipboardProxy::SetCapabilities(
313 const ClipboardCapabilities
& aClipboardCaps
) {
314 mClipboardCaps
= aClipboardCaps
;