diff options
Diffstat (limited to 'dom/serviceworkers/RemoteServiceWorkerImpl.cpp')
-rw-r--r-- | dom/serviceworkers/RemoteServiceWorkerImpl.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/dom/serviceworkers/RemoteServiceWorkerImpl.cpp b/dom/serviceworkers/RemoteServiceWorkerImpl.cpp new file mode 100644 index 0000000000..9fa898cb55 --- /dev/null +++ b/dom/serviceworkers/RemoteServiceWorkerImpl.cpp @@ -0,0 +1,118 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#include "RemoteServiceWorkerImpl.h" + +#include <utility> + +#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 { + +using mozilla::ipc::BackgroundChild; +using mozilla::ipc::PBackgroundChild; + +RemoteServiceWorkerImpl::~RemoteServiceWorkerImpl() { + MOZ_DIAGNOSTIC_ASSERT(!mWorker); + Shutdown(); +} + +void RemoteServiceWorkerImpl::Shutdown() { + if (mShutdown) { + return; + } + mShutdown = true; + + if (mActor) { + mActor->RevokeOwner(this); + mActor->MaybeStartTeardown(); + mActor = nullptr; + } +} + +void RemoteServiceWorkerImpl::AddServiceWorker(ServiceWorker* aWorker) { + NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl); + MOZ_DIAGNOSTIC_ASSERT(!mWorker); + MOZ_DIAGNOSTIC_ASSERT(aWorker); + mWorker = aWorker; +} + +void RemoteServiceWorkerImpl::RemoveServiceWorker(ServiceWorker* aWorker) { + NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl); + MOZ_DIAGNOSTIC_ASSERT(mWorker); + MOZ_DIAGNOSTIC_ASSERT(aWorker == mWorker); + mWorker = nullptr; +} + +void RemoteServiceWorkerImpl::GetRegistration( + ServiceWorkerRegistrationCallback&& aSuccessCB, + ServiceWorkerFailureCallback&& aFailureCB) { + // TODO +} + +void RemoteServiceWorkerImpl::PostMessage( + RefPtr<ServiceWorkerCloneData>&& aData, const ClientInfo& aClientInfo, + const ClientState& aClientState) { + NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl); + if (!mActor) { + return; + } + + ClonedOrErrorMessageData data; + if (!aData->BuildClonedMessageDataForBackgroundChild(mActor->Manager(), + data)) { + return; + } + + mActor->SendPostMessage( + data, ClientInfoAndState(aClientInfo.ToIPC(), aClientState.ToIPC())); +} + +RemoteServiceWorkerImpl::RemoteServiceWorkerImpl( + const ServiceWorkerDescriptor& aDescriptor) + : mWorker(nullptr), mShutdown(false) { + PBackgroundChild* parentActor = + BackgroundChild::GetOrCreateForCurrentThread(); + if (NS_WARN_IF(!parentActor)) { + Shutdown(); + return; + } + + RefPtr<ServiceWorkerChild> actor = ServiceWorkerChild::Create(); + if (NS_WARN_IF(!actor)) { + Shutdown(); + return; + } + + PServiceWorkerChild* sentActor = + parentActor->SendPServiceWorkerConstructor(actor, aDescriptor.ToIPC()); + if (NS_WARN_IF(!sentActor)) { + Shutdown(); + return; + } + MOZ_DIAGNOSTIC_ASSERT(sentActor == actor); + + mActor = std::move(actor); + mActor->SetOwner(this); +} + +void RemoteServiceWorkerImpl::RevokeActor(ServiceWorkerChild* aActor) { + MOZ_DIAGNOSTIC_ASSERT(mActor); + MOZ_DIAGNOSTIC_ASSERT(mActor == aActor); + mActor->RevokeOwner(this); + mActor = nullptr; + + mShutdown = true; +} + +} // namespace dom +} // namespace mozilla |