SharedWorkerService.h (2485B)
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_SharedWorkerService_h 8 #define mozilla_dom_SharedWorkerService_h 9 10 #include "mozilla/dom/quota/CheckedUnsafePtr.h" 11 #include "nsISupportsImpl.h" 12 #include "nsTArray.h" 13 14 class nsIEventTarget; 15 16 namespace mozilla { 17 18 namespace ipc { 19 class PrincipalInfo; 20 } 21 22 namespace dom { 23 24 class MessagePortIdentifier; 25 class RemoteWorkerData; 26 class SharedWorkerManager; 27 class SharedWorkerParent; 28 class UniqueMessagePortId; 29 30 /** 31 * PBackground service that creates and tracks the per-worker 32 * SharedWorkerManager instances, allowing rendezvous between SharedWorkerParent 33 * instances and the SharedWorkerManagers they want to talk to (1:1). 34 */ 35 class SharedWorkerService final { 36 public: 37 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedWorkerService); 38 39 // This can be called on PBackground thread only. 40 static already_AddRefed<SharedWorkerService> GetOrCreate(); 41 42 // The service, if already created, is available on any thread using this 43 // method. 44 static SharedWorkerService* Get(); 45 46 // PBackground method only. 47 void GetOrCreateWorkerManager(SharedWorkerParent* aActor, 48 const RemoteWorkerData& aData, 49 uint64_t aWindowID, 50 const MessagePortIdentifier& aPortIdentifier); 51 52 void GetOrCreateWorkerManagerOnMainThread( 53 nsIEventTarget* aBackgroundEventTarget, SharedWorkerParent* aActor, 54 const RemoteWorkerData& aData, uint64_t aWindowID, 55 UniqueMessagePortId& aPortIdentifier); 56 57 void RemoveWorkerManagerOnMainThread(SharedWorkerManager* aManager); 58 59 private: 60 SharedWorkerService() = default; 61 ~SharedWorkerService() = default; 62 63 void ErrorPropagationOnMainThread(nsIEventTarget* aBackgroundEventTarget, 64 SharedWorkerParent* aActor, 65 nsresult aError); 66 67 void MismatchOptionsErrorPropagationOnMainThread( 68 nsIEventTarget* aBackgroundEventTarget, SharedWorkerParent* aActor); 69 70 // Touched on main-thread only. 71 nsTArray<RefPtr<SharedWorkerManager>> mWorkerManagers; 72 }; 73 74 } // namespace dom 75 } // namespace mozilla 76 77 #endif // mozilla_dom_SharedWorkerService_h