Skip to content

Commit

Permalink
Bug 1858627 - Use ManagedEndpoint for PClipboardReadRequest construct…
Browse files Browse the repository at this point in the history
…ion; r=nika

Differential Revision: https://phabricator.services.mozilla.com/D201657
  • Loading branch information
EdgarChen committed Mar 13, 2024
1 parent ff6e638 commit 5c139ae
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 39 deletions.
6 changes: 0 additions & 6 deletions dom/ipc/ContentChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1984,12 +1984,6 @@ PRemotePrintJobChild* ContentChild::AllocPRemotePrintJobChild() {
#endif
}

already_AddRefed<PClipboardReadRequestChild>
ContentChild::AllocPClipboardReadRequestChild(
const nsTArray<nsCString>& aTypes) {
return MakeAndAddRef<ClipboardReadRequestChild>(aTypes);
}

media::PMediaChild* ContentChild::AllocPMediaChild() {
return media::AllocPMediaChild();
}
Expand Down
3 changes: 0 additions & 3 deletions dom/ipc/ContentChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,6 @@ class ContentChild final : public PContentChild,

PRemotePrintJobChild* AllocPRemotePrintJobChild();

already_AddRefed<PClipboardReadRequestChild> AllocPClipboardReadRequestChild(
const nsTArray<nsCString>& aTypes);

PMediaChild* AllocPMediaChild();

bool DeallocPMediaChild(PMediaChild* aActor);
Expand Down
40 changes: 29 additions & 11 deletions dom/ipc/ContentParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3540,6 +3540,28 @@ mozilla::ipc::IPCResult ContentParent::RecvGetExternalClipboardFormats(

namespace {

static Result<ClipboardReadRequest, nsresult> CreateClipboardReadRequest(
ContentParent& aContentParent,
nsIAsyncGetClipboardData& aAsyncGetClipboardData) {
nsTArray<nsCString> flavors;
nsresult rv = aAsyncGetClipboardData.GetFlavorList(flavors);
if (NS_FAILED(rv)) {
return Err(rv);
}

auto requestParent = MakeNotNull<RefPtr<ClipboardReadRequestParent>>(
&aContentParent, &aAsyncGetClipboardData);

// Open a remote endpoint for our PClipboardReadRequest actor.
ManagedEndpoint<PClipboardReadRequestChild> childEndpoint =
aContentParent.OpenPClipboardReadRequestEndpoint(requestParent);
if (NS_WARN_IF(!childEndpoint.IsValid())) {
return Err(NS_ERROR_FAILURE);
}

return ClipboardReadRequest(std::move(childEndpoint), std::move(flavors));
}

class ClipboardGetCallback final : public nsIAsyncClipboardGetCallback {
public:
ClipboardGetCallback(ContentParent* aContentParent,
Expand All @@ -3553,20 +3575,16 @@ class ClipboardGetCallback final : public nsIAsyncClipboardGetCallback {
// nsIAsyncClipboardGetCallback
NS_IMETHOD OnSuccess(
nsIAsyncGetClipboardData* aAsyncGetClipboardData) override {
nsTArray<nsCString> flavors;
nsresult rv = aAsyncGetClipboardData->GetFlavorList(flavors);
if (NS_FAILED(rv)) {
return OnError(rv);
}
MOZ_ASSERT(mContentParent);
MOZ_ASSERT(aAsyncGetClipboardData);

auto requestParent = MakeNotNull<RefPtr<ClipboardReadRequestParent>>(
mContentParent, aAsyncGetClipboardData);
if (!mContentParent->SendPClipboardReadRequestConstructor(
requestParent, std::move(flavors))) {
return OnError(NS_ERROR_FAILURE);
auto result =
CreateClipboardReadRequest(*mContentParent, *aAsyncGetClipboardData);
if (result.isErr()) {
return OnError(result.unwrapErr());
}

mResolver(PClipboardReadRequestOrError(requestParent));
mResolver(result.unwrap());
return NS_OK;
}

Expand Down
13 changes: 8 additions & 5 deletions dom/ipc/PContent.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,13 @@ struct IPCImage {
ImageIntSize size;
};

union PClipboardReadRequestOrError {
PClipboardReadRequest;
struct ClipboardReadRequest {
ManagedEndpoint<PClipboardReadRequestChild> childEndpoint;
nsCString[] availableTypes;
};

union ClipboardReadRequestOrError {
ClipboardReadRequest;
nsresult;
};

Expand Down Expand Up @@ -1057,8 +1062,6 @@ child:

async PRemotePrintJob();

async PClipboardReadRequest(nsCString[] aTypes);

parent:
async SynchronizeLayoutHistoryState(MaybeDiscardedBrowsingContext aContext,
nullable nsILayoutHistoryState aState);
Expand Down Expand Up @@ -1233,7 +1236,7 @@ parent:
async GetClipboardAsync(nsCString[] aTypes, int32_t aWhichClipboard,
MaybeDiscardedWindowContext aRequestingWindowContext,
nsIPrincipal aRequestingPrincipal)
returns (PClipboardReadRequestOrError aClipboardReadRequest);
returns (ClipboardReadRequestOrError aClipboardReadRequestOrError);

// Clears the clipboard.
async EmptyClipboard(int32_t aWhichClipboard);
Expand Down
43 changes: 29 additions & 14 deletions widget/nsClipboardProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ NS_IMETHODIMP AsyncGetClipboardDataProxy::GetData(
return NS_OK;
}

static Result<RefPtr<AsyncGetClipboardDataProxy>, nsresult>
CreateAsyncGetClipboardDataProxy(
ClipboardReadRequestOrError&& aClipboardReadRequestOrError) {
if (aClipboardReadRequestOrError.type() ==
ClipboardReadRequestOrError::Tnsresult) {
MOZ_ASSERT(NS_FAILED(aClipboardReadRequestOrError.get_nsresult()));
return Err(aClipboardReadRequestOrError.get_nsresult());
}

ClipboardReadRequest& request =
aClipboardReadRequestOrError.get_ClipboardReadRequest();
auto requestChild = MakeRefPtr<ClipboardReadRequestChild>(
std::move(request.availableTypes()));
if (NS_WARN_IF(
!ContentChild::GetSingleton()->BindPClipboardReadRequestEndpoint(
std::move(request.childEndpoint()), requestChild))) {
return Err(NS_ERROR_FAILURE);
}

return MakeRefPtr<AsyncGetClipboardDataProxy>(requestChild);
}

} // namespace

NS_IMETHODIMP nsClipboardProxy::AsyncGetData(
Expand All @@ -198,23 +220,16 @@ NS_IMETHODIMP nsClipboardProxy::AsyncGetData(
->Then(
GetMainThreadSerialEventTarget(), __func__,
/* resolve */
[callback = nsCOMPtr{aCallback}](const PClipboardReadRequestOrError&
aClipboardReadRequestOrError) {
if (aClipboardReadRequestOrError.type() ==
PClipboardReadRequestOrError::Tnsresult) {
MOZ_ASSERT(
NS_FAILED(aClipboardReadRequestOrError.get_nsresult()));
callback->OnError(aClipboardReadRequestOrError.get_nsresult());
[callback = nsCOMPtr{aCallback}](
ClipboardReadRequestOrError&& aClipboardReadRequestOrError) {
auto result = CreateAsyncGetClipboardDataProxy(
std::move(aClipboardReadRequestOrError));
if (result.isErr()) {
callback->OnError(result.unwrapErr());
return;
}

auto asyncGetClipboardData = MakeRefPtr<AsyncGetClipboardDataProxy>(
static_cast<ClipboardReadRequestChild*>(
aClipboardReadRequestOrError.get_PClipboardReadRequest()
.AsChild()
.get()));

callback->OnSuccess(asyncGetClipboardData);
callback->OnSuccess(result.inspect());
},
/* reject */
[callback = nsCOMPtr{aCallback}](
Expand Down

0 comments on commit 5c139ae

Please sign in to comment.