Bug 1494333 - index crons just like artifacts r=Callek
[gecko.git] / dom / serviceworkers / ServiceWorkerProxy.cpp
blobdac23d7fcef421042426fa16ccdbf79e0f9b3432
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ServiceWorkerProxy.h"
9 #include "mozilla/ipc/BackgroundParent.h"
10 #include "ServiceWorkerInfo.h"
12 namespace mozilla {
13 namespace dom {
15 using mozilla::ipc::AssertIsOnBackgroundThread;
17 ServiceWorkerProxy::~ServiceWorkerProxy()
19 // Any thread
20 MOZ_DIAGNOSTIC_ASSERT(!mActor);
21 MOZ_DIAGNOSTIC_ASSERT(!mInfo);
24 void
25 ServiceWorkerProxy::MaybeShutdownOnBGThread()
27 AssertIsOnBackgroundThread();
28 if (!mActor) {
29 return;
31 mActor->MaybeSendDelete();
34 void
35 ServiceWorkerProxy::InitOnMainThread()
37 AssertIsOnMainThread();
39 auto scopeExit = MakeScopeExit([&] {
40 MaybeShutdownOnMainThread();
41 });
43 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
44 NS_ENSURE_TRUE_VOID(swm);
46 RefPtr<ServiceWorkerRegistrationInfo> reg =
47 swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
48 NS_ENSURE_TRUE_VOID(reg);
50 RefPtr<ServiceWorkerInfo> info = reg->GetByDescriptor(mDescriptor);
51 NS_ENSURE_TRUE_VOID(info);
53 scopeExit.release();
55 mInfo = new nsMainThreadPtrHolder<ServiceWorkerInfo>("ServiceWorkerProxy::mInfo",
56 info);
59 void
60 ServiceWorkerProxy::MaybeShutdownOnMainThread()
62 AssertIsOnMainThread();
64 nsCOMPtr<nsIRunnable> r =
65 NewRunnableMethod(__func__, this,
66 &ServiceWorkerProxy::MaybeShutdownOnBGThread);
68 MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
71 void
72 ServiceWorkerProxy::StopListeningOnMainThread()
74 AssertIsOnMainThread();
75 mInfo = nullptr;
78 ServiceWorkerProxy::ServiceWorkerProxy(const ServiceWorkerDescriptor& aDescriptor)
79 : mActor(nullptr)
80 , mEventTarget(GetCurrentThreadSerialEventTarget())
81 , mDescriptor(aDescriptor)
85 void
86 ServiceWorkerProxy::Init(ServiceWorkerParent* aActor)
88 AssertIsOnBackgroundThread();
89 MOZ_DIAGNOSTIC_ASSERT(aActor);
90 MOZ_DIAGNOSTIC_ASSERT(!mActor);
91 MOZ_DIAGNOSTIC_ASSERT(mEventTarget);
93 mActor = aActor;
95 // Note, this must be done from a separate Init() method and not in
96 // the constructor. If done from the constructor the runnable can
97 // execute, complete, and release its reference before the constructor
98 // returns.
99 nsCOMPtr<nsIRunnable> r = NewRunnableMethod("ServiceWorkerProxy::Init", this,
100 &ServiceWorkerProxy::InitOnMainThread);
101 MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
104 void
105 ServiceWorkerProxy::RevokeActor(ServiceWorkerParent* aActor)
107 AssertIsOnBackgroundThread();
108 MOZ_DIAGNOSTIC_ASSERT(mActor);
109 MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
110 mActor = nullptr;
112 nsCOMPtr<nsIRunnable> r =
113 NewRunnableMethod(__func__, this,
114 &ServiceWorkerProxy::StopListeningOnMainThread);
115 MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
118 void
119 ServiceWorkerProxy::PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
120 const ClientInfo& aClientInfo,
121 const ClientState& aClientState)
123 AssertIsOnBackgroundThread();
124 RefPtr<ServiceWorkerProxy> self = this;
125 nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(__func__,
126 [self, data = std::move(aData), aClientInfo, aClientState] () mutable {
127 if (!self->mInfo) {
128 return;
130 self->mInfo->PostMessage(std::move(data), aClientInfo, aClientState);
132 MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
135 } // namespace dom
136 } // namespace mozilla