2014-02-10 04:13:10 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2022-10-28 17:48:25 +03:00
|
|
|
#if defined(ACCESSIBILITY) && defined(XP_WIN)
|
|
|
|
# include "mozilla/a11y/Compatibility.h"
|
|
|
|
#endif
|
2014-02-10 04:13:10 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-10-16 22:43:56 +03:00
|
|
|
#include "nsArrayUtils.h"
|
2014-02-10 04:13:10 +04:00
|
|
|
#include "nsClipboardProxy.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2015-04-16 22:38:12 +03:00
|
|
|
#include "nsContentUtils.h"
|
2021-11-15 15:39:38 +03:00
|
|
|
#include "PermissionMessageUtils.h"
|
2014-02-10 04:13:10 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-10-22 13:50:00 +04:00
|
|
|
using namespace mozilla::dom;
|
2014-02-10 04:13:10 +04:00
|
|
|
|
2014-10-22 13:50:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsClipboardProxy, nsIClipboard, nsIClipboardProxy)
|
2014-02-10 04:13:10 +04:00
|
|
|
|
2023-01-16 22:50:17 +03:00
|
|
|
nsClipboardProxy::nsClipboardProxy() : mClipboardCaps(false, false, false) {}
|
2014-02-10 04:13:10 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsClipboardProxy::SetData(nsITransferable* aTransferable,
|
2014-10-22 13:50:00 +04:00
|
|
|
nsIClipboardOwner* anOwner, int32_t aWhichClipboard) {
|
2022-10-28 17:48:25 +03:00
|
|
|
#if defined(ACCESSIBILITY) && defined(XP_WIN)
|
|
|
|
a11y::Compatibility::SuppressA11yForClipboardCopy();
|
|
|
|
#endif
|
|
|
|
|
2015-04-16 22:38:12 +03:00
|
|
|
ContentChild* child = ContentChild::GetSingleton();
|
|
|
|
|
|
|
|
IPCDataTransfer ipcDataTransfer;
|
|
|
|
nsContentUtils::TransferableToIPCTransferable(aTransferable, &ipcDataTransfer,
|
2023-02-16 19:56:53 +03:00
|
|
|
false, nullptr);
|
2014-02-10 04:13:10 +04:00
|
|
|
|
2018-11-20 04:17:54 +03:00
|
|
|
bool isPrivateData = aTransferable->GetIsPrivateData();
|
|
|
|
nsCOMPtr<nsIPrincipal> requestingPrincipal =
|
|
|
|
aTransferable->GetRequestingPrincipal();
|
|
|
|
nsContentPolicyType contentPolicyType = aTransferable->GetContentPolicyType();
|
2023-03-23 00:55:08 +03:00
|
|
|
nsCOMPtr<nsIReferrerInfo> referrerInfo = aTransferable->GetReferrerInfo();
|
2022-08-26 19:08:05 +03:00
|
|
|
child->SendSetClipboard(std::move(ipcDataTransfer), isPrivateData,
|
2023-03-23 00:55:08 +03:00
|
|
|
requestingPrincipal, contentPolicyType, referrerInfo,
|
2022-08-26 19:08:05 +03:00
|
|
|
aWhichClipboard);
|
2014-02-10 04:13:10 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2022-02-16 13:14:58 +03:00
|
|
|
nsClipboardProxy::GetData(nsITransferable* aTransferable,
|
|
|
|
int32_t aWhichClipboard) {
|
2018-10-10 23:25:54 +03:00
|
|
|
nsTArray<nsCString> types;
|
|
|
|
aTransferable->FlavorsTransferableCanImport(types);
|
2014-02-10 04:13:10 +04:00
|
|
|
|
2015-04-16 22:38:12 +03:00
|
|
|
IPCDataTransfer dataTransfer;
|
|
|
|
ContentChild::GetSingleton()->SendGetClipboard(types, aWhichClipboard,
|
|
|
|
&dataTransfer);
|
2022-05-17 11:12:03 +03:00
|
|
|
return nsContentUtils::IPCTransferableToTransferable(
|
2022-11-21 13:18:38 +03:00
|
|
|
dataTransfer, false /* aAddDataFlavor */, aTransferable,
|
|
|
|
false /* aFilterUnknownFlavors */);
|
2014-02-10 04:13:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard) {
|
|
|
|
ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-06-09 04:06:39 +03:00
|
|
|
nsClipboardProxy::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
|
2015-04-16 22:38:12 +03:00
|
|
|
int32_t aWhichClipboard,
|
|
|
|
bool* aHasType) {
|
|
|
|
*aHasType = false;
|
|
|
|
|
2019-06-09 04:06:39 +03:00
|
|
|
ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList,
|
|
|
|
aWhichClipboard, aHasType);
|
2015-04-16 22:38:12 +03:00
|
|
|
|
2014-02-10 04:13:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2023-01-16 22:50:17 +03:00
|
|
|
nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard,
|
|
|
|
bool* aIsSupported) {
|
|
|
|
switch (aWhichClipboard) {
|
|
|
|
case kGlobalClipboard:
|
|
|
|
// We always support the global clipboard.
|
|
|
|
*aIsSupported = true;
|
|
|
|
return NS_OK;
|
|
|
|
case kSelectionClipboard:
|
|
|
|
*aIsSupported = mClipboardCaps.supportsSelectionClipboard();
|
|
|
|
return NS_OK;
|
|
|
|
case kFindClipboard:
|
|
|
|
*aIsSupported = mClipboardCaps.supportsFindClipboard();
|
|
|
|
return NS_OK;
|
|
|
|
case kSelectionCache:
|
|
|
|
*aIsSupported = mClipboardCaps.supportsSelectionCache();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aIsSupported = false;
|
2014-02-28 19:07:30 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-10-22 13:50:00 +04:00
|
|
|
|
|
|
|
void nsClipboardProxy::SetCapabilities(
|
|
|
|
const ClipboardCapabilities& aClipboardCaps) {
|
|
|
|
mClipboardCaps = aClipboardCaps;
|
|
|
|
}
|
2022-08-15 23:27:24 +03:00
|
|
|
|
2022-08-15 23:27:26 +03:00
|
|
|
RefPtr<DataFlavorsPromise> nsClipboardProxy::AsyncHasDataMatchingFlavors(
|
|
|
|
const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) {
|
|
|
|
auto promise = MakeRefPtr<DataFlavorsPromise::Private>(__func__);
|
|
|
|
ContentChild::GetSingleton()
|
|
|
|
->SendClipboardHasTypesAsync(aFlavorList, aWhichClipboard)
|
|
|
|
->Then(
|
|
|
|
GetMainThreadSerialEventTarget(), __func__,
|
|
|
|
/* resolve */
|
|
|
|
[promise](nsTArray<nsCString> types) {
|
|
|
|
promise->Resolve(std::move(types), __func__);
|
|
|
|
},
|
|
|
|
/* reject */
|
|
|
|
[promise](mozilla::ipc::ResponseRejectReason aReason) {
|
|
|
|
promise->Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:27:24 +03:00
|
|
|
RefPtr<GenericPromise> nsClipboardProxy::AsyncGetData(
|
|
|
|
nsITransferable* aTransferable, int32_t aWhichClipboard) {
|
|
|
|
if (!aTransferable) {
|
|
|
|
return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of flavors this transferable can import
|
|
|
|
nsTArray<nsCString> flavors;
|
|
|
|
nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return GenericPromise::CreateAndReject(rv, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> transferable(aTransferable);
|
|
|
|
auto promise = MakeRefPtr<GenericPromise::Private>(__func__);
|
|
|
|
ContentChild::GetSingleton()
|
|
|
|
->SendGetClipboardAsync(flavors, aWhichClipboard)
|
|
|
|
->Then(
|
|
|
|
GetMainThreadSerialEventTarget(), __func__,
|
|
|
|
/* resolve */
|
2022-11-28 15:22:51 +03:00
|
|
|
[promise,
|
|
|
|
transferable](const IPCDataTransferOrError& ipcDataTransferOrError) {
|
|
|
|
if (ipcDataTransferOrError.type() ==
|
|
|
|
IPCDataTransferOrError::Tnsresult) {
|
|
|
|
promise->Reject(ipcDataTransferOrError.get_nsresult(), __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:27:24 +03:00
|
|
|
nsresult rv = nsContentUtils::IPCTransferableToTransferable(
|
2022-11-28 15:22:51 +03:00
|
|
|
ipcDataTransferOrError.get_IPCDataTransfer(),
|
|
|
|
false /* aAddDataFlavor */, transferable,
|
2022-11-21 13:18:38 +03:00
|
|
|
false /* aFilterUnknownFlavors */);
|
2022-08-15 23:27:24 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
promise->Reject(rv, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise->Resolve(true, __func__);
|
|
|
|
},
|
|
|
|
/* reject */
|
|
|
|
[promise](mozilla::ipc::ResponseRejectReason aReason) {
|
|
|
|
promise->Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|