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"
15 using mozilla::ipc::AssertIsOnBackgroundThread
;
17 ServiceWorkerProxy::~ServiceWorkerProxy()
20 MOZ_DIAGNOSTIC_ASSERT(!mActor
);
21 MOZ_DIAGNOSTIC_ASSERT(!mInfo
);
25 ServiceWorkerProxy::MaybeShutdownOnBGThread()
27 AssertIsOnBackgroundThread();
31 mActor
->MaybeSendDelete();
35 ServiceWorkerProxy::InitOnMainThread()
37 AssertIsOnMainThread();
39 auto scopeExit
= MakeScopeExit([&] {
40 MaybeShutdownOnMainThread();
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
);
55 mInfo
= new nsMainThreadPtrHolder
<ServiceWorkerInfo
>("ServiceWorkerProxy::mInfo",
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
));
72 ServiceWorkerProxy::StopListeningOnMainThread()
74 AssertIsOnMainThread();
78 ServiceWorkerProxy::ServiceWorkerProxy(const ServiceWorkerDescriptor
& aDescriptor
)
80 , mEventTarget(GetCurrentThreadSerialEventTarget())
81 , mDescriptor(aDescriptor
)
86 ServiceWorkerProxy::Init(ServiceWorkerParent
* aActor
)
88 AssertIsOnBackgroundThread();
89 MOZ_DIAGNOSTIC_ASSERT(aActor
);
90 MOZ_DIAGNOSTIC_ASSERT(!mActor
);
91 MOZ_DIAGNOSTIC_ASSERT(mEventTarget
);
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
99 nsCOMPtr
<nsIRunnable
> r
= NewRunnableMethod("ServiceWorkerProxy::Init", this,
100 &ServiceWorkerProxy::InitOnMainThread
);
101 MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other
, r
.forget()));
105 ServiceWorkerProxy::RevokeActor(ServiceWorkerParent
* aActor
)
107 AssertIsOnBackgroundThread();
108 MOZ_DIAGNOSTIC_ASSERT(mActor
);
109 MOZ_DIAGNOSTIC_ASSERT(mActor
== aActor
);
112 nsCOMPtr
<nsIRunnable
> r
=
113 NewRunnableMethod(__func__
, this,
114 &ServiceWorkerProxy::StopListeningOnMainThread
);
115 MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other
, r
.forget()));
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 {
130 self
->mInfo
->PostMessage(std::move(data
), aClientInfo
, aClientState
);
132 MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other
, r
.forget()));
136 } // namespace mozilla