From 6323d7c413dbecca678ef48f2977c53929c674d9 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Mon, 9 Jul 2018 16:02:40 -0700 Subject: [PATCH] Bug 1459209 P8 Implement RemoteServiceWorkerImpl::PostMessage() across IPC and to the parent-side SWM. r=mrbkap --HG-- extra : rebase_source : d38628cc7b8822b4213c8034f0c1dd783585ddfb --- dom/serviceworkers/PServiceWorker.ipdl | 5 +++++ dom/serviceworkers/RemoteServiceWorkerImpl.cpp | 17 ++++++++++++++++- dom/serviceworkers/ServiceWorkerParent.cpp | 18 ++++++++++++++++++ dom/serviceworkers/ServiceWorkerParent.h | 4 ++++ dom/serviceworkers/ServiceWorkerProxy.cpp | 17 +++++++++++++++++ dom/serviceworkers/ServiceWorkerProxy.h | 6 ++++++ 6 files changed, 66 insertions(+), 1 deletion(-) diff --git a/dom/serviceworkers/PServiceWorker.ipdl b/dom/serviceworkers/PServiceWorker.ipdl index 05e8c95c54fa..7ebcbe2225e2 100644 --- a/dom/serviceworkers/PServiceWorker.ipdl +++ b/dom/serviceworkers/PServiceWorker.ipdl @@ -4,6 +4,9 @@ include protocol PBackground; +include ClientIPCTypes; +include DOMTypes; + namespace mozilla { namespace dom { @@ -14,6 +17,8 @@ protocol PServiceWorker parent: async Teardown(); + async PostMessage(ClonedMessageData aClonedData, ClientInfoAndState aSource); + child: async __delete__(); }; diff --git a/dom/serviceworkers/RemoteServiceWorkerImpl.cpp b/dom/serviceworkers/RemoteServiceWorkerImpl.cpp index 4e919cb4c98c..4341b52fed23 100644 --- a/dom/serviceworkers/RemoteServiceWorkerImpl.cpp +++ b/dom/serviceworkers/RemoteServiceWorkerImpl.cpp @@ -6,9 +6,12 @@ #include "RemoteServiceWorkerImpl.h" +#include "mozilla/dom/ClientInfo.h" +#include "mozilla/dom/ClientState.h" #include "mozilla/ipc/BackgroundChild.h" #include "mozilla/ipc/PBackgroundChild.h" #include "ServiceWorkerChild.h" +#include "ServiceWorkerCloneData.h" namespace mozilla { namespace dom { @@ -67,7 +70,19 @@ RemoteServiceWorkerImpl::PostMessage(RefPtr&& aData, const ClientInfo& aClientInfo, const ClientState& aClientState) { - // TODO + NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl); + if (!mActor) { + return; + } + + ClonedMessageData data; + if (!aData->BuildClonedMessageDataForBackgroundChild(mActor->Manager(), + data)) { + return; + } + + mActor->SendPostMessage(data, ClientInfoAndState(aClientInfo.ToIPC(), + aClientState.ToIPC())); } RemoteServiceWorkerImpl::RemoteServiceWorkerImpl(const ServiceWorkerDescriptor& aDescriptor) diff --git a/dom/serviceworkers/ServiceWorkerParent.cpp b/dom/serviceworkers/ServiceWorkerParent.cpp index 4aa2f55d4f51..8fb180604645 100644 --- a/dom/serviceworkers/ServiceWorkerParent.cpp +++ b/dom/serviceworkers/ServiceWorkerParent.cpp @@ -6,11 +6,16 @@ #include "ServiceWorkerParent.h" +#include "ServiceWorkerCloneData.h" #include "ServiceWorkerProxy.h" +#include "mozilla/dom/ClientInfo.h" +#include "mozilla/dom/ClientState.h" +#include "mozilla/dom/ipc/StructuredCloneData.h" namespace mozilla { namespace dom { +using mozilla::dom::ipc::StructuredCloneData; using mozilla::ipc::IPCResult; void @@ -29,6 +34,19 @@ ServiceWorkerParent::RecvTeardown() return IPC_OK(); } +IPCResult +ServiceWorkerParent::RecvPostMessage(const ClonedMessageData& aClonedData, + const ClientInfoAndState& aSource) +{ + RefPtr data = new ServiceWorkerCloneData(); + data->CopyFromClonedMessageDataForBackgroundParent(aClonedData); + + mProxy->PostMessage(std::move(data), ClientInfo(aSource.info()), + ClientState::FromIPC(aSource.state())); + + return IPC_OK(); +} + ServiceWorkerParent::ServiceWorkerParent() : mDeleteSent(false) { diff --git a/dom/serviceworkers/ServiceWorkerParent.h b/dom/serviceworkers/ServiceWorkerParent.h index 8cbcf63d358e..d112b689a221 100644 --- a/dom/serviceworkers/ServiceWorkerParent.h +++ b/dom/serviceworkers/ServiceWorkerParent.h @@ -27,6 +27,10 @@ class ServiceWorkerParent final : public PServiceWorkerParent mozilla::ipc::IPCResult RecvTeardown() override; + mozilla::ipc::IPCResult + RecvPostMessage(const ClonedMessageData& aClonedData, + const ClientInfoAndState& aSource) override; + public: ServiceWorkerParent(); ~ServiceWorkerParent(); diff --git a/dom/serviceworkers/ServiceWorkerProxy.cpp b/dom/serviceworkers/ServiceWorkerProxy.cpp index 64b195ab99f5..dac23d7fcef4 100644 --- a/dom/serviceworkers/ServiceWorkerProxy.cpp +++ b/dom/serviceworkers/ServiceWorkerProxy.cpp @@ -115,5 +115,22 @@ ServiceWorkerProxy::RevokeActor(ServiceWorkerParent* aActor) MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget())); } +void +ServiceWorkerProxy::PostMessage(RefPtr&& aData, + const ClientInfo& aClientInfo, + const ClientState& aClientState) +{ + AssertIsOnBackgroundThread(); + RefPtr self = this; + nsCOMPtr r = NS_NewRunnableFunction(__func__, + [self, data = std::move(aData), aClientInfo, aClientState] () mutable { + if (!self->mInfo) { + return; + } + self->mInfo->PostMessage(std::move(data), aClientInfo, aClientState); + }); + MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget())); +} + } // namespace dom } // namespace mozilla diff --git a/dom/serviceworkers/ServiceWorkerProxy.h b/dom/serviceworkers/ServiceWorkerProxy.h index 6aee20ede3e7..80e2e5554097 100644 --- a/dom/serviceworkers/ServiceWorkerProxy.h +++ b/dom/serviceworkers/ServiceWorkerProxy.h @@ -13,6 +13,7 @@ namespace mozilla { namespace dom { +class ServiceWorkerCloneData; class ServiceWorkerInfo; class ServiceWorkerParent; @@ -56,6 +57,11 @@ public: void RevokeActor(ServiceWorkerParent* aActor); + void + PostMessage(RefPtr&& aData, + const ClientInfo& aClientInfo, + const ClientState& aState); + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ServiceWorkerProxy); };