Bug 1755863 - Part 1: Add async API for getting data on nsIClipboard; r=geckoview-reviewers,NeilDeakin,m_kato,nika

Differential Revision: https://phabricator.services.mozilla.com/D145378
This commit is contained in:
Edgar Chen 2022-08-15 20:27:24 +00:00
Родитель 75c893aee1
Коммит f6014ee7c6
6 изменённых файлов: 101 добавлений и 6 удалений

Просмотреть файл

@ -114,6 +114,16 @@ nsClipboard::GetData(nsITransferable* aTransferable, int32_t aWhichClipboard) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
RefPtr<GenericPromise> nsClipboard::AsyncGetData(nsITransferable* aTransferable,
int32_t aWhichClipboard) {
nsresult rv = GetData(aTransferable, aWhichClipboard);
if (NS_FAILED(rv)) {
return GenericPromise::CreateAndReject(rv, __func__);
}
return GenericPromise::CreateAndResolve(true, __func__);
}
NS_IMETHODIMP NS_IMETHODIMP
nsClipboard::EmptyClipboard(int32_t aWhichClipboard) { nsClipboard::EmptyClipboard(int32_t aWhichClipboard) {
if (aWhichClipboard != kGlobalClipboard) return NS_ERROR_NOT_IMPLEMENTED; if (aWhichClipboard != kGlobalClipboard) return NS_ERROR_NOT_IMPLEMENTED;

Просмотреть файл

@ -585,6 +585,17 @@ nsClipboard::GetData(nsITransferable* aTransferable, int32_t aWhichClipboard) {
return NS_OK; return NS_OK;
} }
RefPtr<GenericPromise> nsClipboard::AsyncGetData(nsITransferable* aTransferable,
int32_t aWhichClipboard) {
// XXX we should read the clipboard data asynchronously instead, bug 1778201.
nsresult rv = GetData(aTransferable, aWhichClipboard);
if (NS_FAILED(rv)) {
return GenericPromise::CreateAndReject(rv, __func__);
}
return GenericPromise::CreateAndResolve(true, __func__);
}
NS_IMETHODIMP NS_IMETHODIMP
nsClipboard::EmptyClipboard(int32_t aWhichClipboard) { nsClipboard::EmptyClipboard(int32_t aWhichClipboard) {
LOGCLIP("nsClipboard::EmptyClipboard (%s)\n", LOGCLIP("nsClipboard::EmptyClipboard (%s)\n",

Просмотреть файл

@ -108,5 +108,15 @@ HeadlessClipboard::SupportsFindClipboard(bool* _retval) {
return NS_OK; return NS_OK;
} }
RefPtr<GenericPromise> HeadlessClipboard::AsyncGetData(
nsITransferable* aTransferable, int32_t aWhichClipboard) {
nsresult rv = GetData(aTransferable, aWhichClipboard);
if (NS_FAILED(rv)) {
return GenericPromise::CreateAndReject(rv, __func__);
}
return GenericPromise::CreateAndResolve(true, __func__);
}
} // namespace widget } // namespace widget
} // namespace mozilla } // namespace mozilla

Просмотреть файл

@ -12,6 +12,7 @@
#include "nsError.h" #include "nsError.h"
#include "nsXPCOM.h" #include "nsXPCOM.h"
using mozilla::GenericPromise;
using mozilla::LogLevel; using mozilla::LogLevel;
nsBaseClipboard::nsBaseClipboard() nsBaseClipboard::nsBaseClipboard()
@ -92,6 +93,16 @@ NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable* aTransferable,
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
RefPtr<GenericPromise> nsBaseClipboard::AsyncGetData(
nsITransferable* aTransferable, int32_t aWhichClipboard) {
nsresult rv = GetData(aTransferable, aWhichClipboard);
if (NS_FAILED(rv)) {
return GenericPromise::CreateAndReject(rv, __func__);
}
return GenericPromise::CreateAndResolve(true, __func__);
}
NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) { NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) {
CLIPBOARD_LOG("%s: clipboard=%i", __FUNCTION__, aWhichClipboard); CLIPBOARD_LOG("%s: clipboard=%i", __FUNCTION__, aWhichClipboard);

Просмотреть файл

@ -87,3 +87,42 @@ void nsClipboardProxy::SetCapabilities(
const ClipboardCapabilities& aClipboardCaps) { const ClipboardCapabilities& aClipboardCaps) {
mClipboardCaps = aClipboardCaps; mClipboardCaps = aClipboardCaps;
} }
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 */
[promise, transferable](const IPCDataTransfer& ipcDataTransfer) {
nsresult rv = nsContentUtils::IPCTransferableToTransferable(
ipcDataTransfer, false /* aAddDataFlavor */, transferable,
ContentChild::GetSingleton());
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();
}

Просмотреть файл

@ -9,9 +9,15 @@
#include "nsITransferable.idl" #include "nsITransferable.idl"
#include "nsIClipboardOwner.idl" #include "nsIClipboardOwner.idl"
%{C++
#include "mozilla/MozPromise.h"
%}
interface nsIArray; interface nsIArray;
[scriptable, uuid(ceaa0047-647f-4b8e-ad1c-aff9fa62aa51)] native AsyncGetDataPromise(RefPtr<mozilla::GenericPromise>);
[scriptable, builtinclass, uuid(ceaa0047-647f-4b8e-ad1c-aff9fa62aa51)]
interface nsIClipboard : nsISupports interface nsIClipboard : nsISupports
{ {
const long kSelectionClipboard = 0; const long kSelectionClipboard = 0;
@ -84,9 +90,17 @@ interface nsIClipboard : nsISupports
* @result NS_OK if successful. * @result NS_OK if successful.
*/ */
boolean supportsFindClipboard ( ) ; boolean supportsFindClipboard ( ) ;
/**
* Filters the flavors aTransferable can import (see
* `nsITransferable::flavorsTransferableCanImport`) and gets the data for the
* first flavor. That data is set for aTransferable.
*
* @param aTransferable The transferable
* @param aWhichClipboard Specifies the clipboard to which this operation applies.
* @return MozPromise The returned promise will resolve when the data is ready or reject
* if any error occurs.
*/
[noscript, notxpcom, nostdcall]
AsyncGetDataPromise asyncGetData(in nsITransferable aTransferable, in long aWhichClipboard);
}; };
%{ C++
%}