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/. */
6 
7 #ifndef mozilla_dom_dom_SharedWorkerParent_h
8 #define mozilla_dom_dom_SharedWorkerParent_h
9 
10 #include "mozilla/dom/PSharedWorkerParent.h"
11 #include "mozilla/dom/quota/CheckedUnsafePtr.h"
12 #include "mozilla/ipc/BackgroundUtils.h"
13 #include "nsISupportsImpl.h"
14 
15 namespace mozilla {
16 namespace dom {
17 
18 class MessagePortIdentifier;
19 class RemoteWorkerData;
20 class SharedWorkerManagerWrapper;
21 
22 /**
23  * PBackground actor that relays life-cycle events (freeze/thaw, suspend/resume,
24  * close) to the PBackground SharedWorkerManager and relays error/termination
25  * back to the child.
26  */
27 class SharedWorkerParent final
28     : public mozilla::dom::PSharedWorkerParent,
29       public SupportsCheckedUnsafePtr<CheckIf<DiagnosticAssertEnabled>> {
30  public:
31   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedWorkerParent)
32 
33   SharedWorkerParent();
34 
35   void Initialize(const RemoteWorkerData& aData, uint64_t aWindowID,
36                   const MessagePortIdentifier& aPortIdentifier);
37 
38   void ManagerCreated(
39       already_AddRefed<SharedWorkerManagerWrapper> aWorkerManagerWrapper);
40 
41   void ErrorPropagation(nsresult aError);
42 
43   mozilla::ipc::IPCResult RecvClose();
44 
45   mozilla::ipc::IPCResult RecvSuspend();
46 
47   mozilla::ipc::IPCResult RecvResume();
48 
49   mozilla::ipc::IPCResult RecvFreeze();
50 
51   mozilla::ipc::IPCResult RecvThaw();
52 
IsSuspended()53   bool IsSuspended() const { return mSuspended; }
54 
IsFrozen()55   bool IsFrozen() const { return mFrozen; }
56 
WindowID()57   uint64_t WindowID() const { return mWindowID; }
58 
59  private:
60   ~SharedWorkerParent();
61 
62   void ActorDestroy(IProtocol::ActorDestroyReason aReason) override;
63 
64   nsCOMPtr<nsIEventTarget> mBackgroundEventTarget;
65   RefPtr<SharedWorkerManagerWrapper> mWorkerManagerWrapper;
66 
67   enum {
68     eInit,
69     ePending,
70     eActive,
71     eClosed,
72   } mStatus;
73 
74   uint64_t mWindowID;
75 
76   bool mSuspended;
77   bool mFrozen;
78 };
79 
80 }  // namespace dom
81 }  // namespace mozilla
82 
83 #endif  // mozilla_dom_dom_SharedWorkerParent_h
84