Bug 1824753 [wpt PR 39216] - [FLEDGE] Add WPT test that FLEDGE is not allowed in...
[gecko.git] / dom / clients / manager / ClientSource.h
blobdc89a61175cb511f217611e7848afe14c79bb038
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 #ifndef _mozilla_dom_ClientSource_h
7 #define _mozilla_dom_ClientSource_h
9 #include "mozilla/dom/ClientInfo.h"
10 #include "mozilla/dom/ClientOpPromise.h"
11 #include "mozilla/dom/ClientThing.h"
12 #include "mozilla/dom/ServiceWorkerDescriptor.h"
13 #include "mozilla/ResultVariant.h"
14 #include "mozilla/Variant.h"
16 #ifdef XP_WIN
17 # undef PostMessage
18 #endif
20 class nsIContentSecurityPolicy;
21 class nsIDocShell;
22 class nsIGlobalObject;
23 class nsISerialEventTarget;
24 class nsPIDOMWindowInner;
26 namespace mozilla {
27 class ErrorResult;
29 namespace dom {
31 class ClientControlledArgs;
32 class ClientFocusArgs;
33 class ClientGetInfoAndStateArgs;
34 class ClientManager;
35 class ClientPostMessageArgs;
36 class ClientSourceChild;
37 class ClientSourceConstructorArgs;
38 class ClientSourceExecutionReadyArgs;
39 class ClientState;
40 class ClientWindowState;
41 class PClientManagerChild;
42 class WorkerPrivate;
44 // ClientSource is an RAII style class that is designed to be held via
45 // a UniquePtr<>. When created ClientSource will register the existence
46 // of a client in the cross-process ClientManagerService. When the
47 // ClientSource is destroyed then client entry will be removed. Code
48 // that represents globals or browsing environments, such as nsGlobalWindow
49 // or WorkerPrivate, should use ClientManager to create a ClientSource.
50 class ClientSource final : public ClientThing<ClientSourceChild> {
51 friend class ClientManager;
53 NS_DECL_OWNINGTHREAD
55 RefPtr<ClientManager> mManager;
56 nsCOMPtr<nsISerialEventTarget> mEventTarget;
58 Variant<Nothing, RefPtr<nsPIDOMWindowInner>, nsCOMPtr<nsIDocShell>,
59 WorkerPrivate*>
60 mOwner;
62 ClientInfo mClientInfo;
63 Maybe<ServiceWorkerDescriptor> mController;
64 Maybe<nsCOMPtr<nsIPrincipal>> mPrincipal;
66 // Contained a de-duplicated list of ServiceWorker scope strings
67 // for which this client has called navigator.serviceWorker.register().
68 // Typically there will be either be zero or one scope strings, but
69 // there could be more. We keep this list until the client is closed.
70 AutoTArray<nsCString, 1> mRegisteringScopeList;
72 void Shutdown();
74 void ExecutionReady(const ClientSourceExecutionReadyArgs& aArgs);
76 WorkerPrivate* GetWorkerPrivate() const;
78 nsIDocShell* GetDocShell() const;
80 nsIGlobalObject* GetGlobal() const;
82 Result<bool, ErrorResult> MaybeCreateInitialDocument();
84 Result<ClientState, ErrorResult> SnapshotWindowState();
86 // Private methods called by ClientManager
87 ClientSource(ClientManager* aManager, nsISerialEventTarget* aEventTarget,
88 const ClientSourceConstructorArgs& aArgs);
90 void Activate(PClientManagerChild* aActor);
92 public:
93 ~ClientSource();
95 nsPIDOMWindowInner* GetInnerWindow() const;
97 void WorkerExecutionReady(WorkerPrivate* aWorkerPrivate);
99 nsresult WindowExecutionReady(nsPIDOMWindowInner* aInnerWindow);
101 nsresult DocShellExecutionReady(nsIDocShell* aDocShell);
103 void Freeze();
105 void Thaw();
107 void EvictFromBFCache();
109 RefPtr<ClientOpPromise> EvictFromBFCacheOp();
111 const ClientInfo& Info() const;
113 // Trigger a synchronous IPC ping to the parent process to confirm that
114 // the ClientSource actor has been created. This should only be used
115 // by the WorkerPrivate startup code to deal with a ClientHandle::Control()
116 // call racing on the main thread. Do not call this in other circumstances!
117 void WorkerSyncPing(WorkerPrivate* aWorkerPrivate);
119 // Synchronously mark the ClientSource as controlled by the given service
120 // worker. This can happen as a result of a remote operation or directly
121 // by local code. For example, if a client's initial network load is
122 // intercepted by a controlling service worker then this should be called
123 // immediately.
125 // Note, there is no way to clear the controlling service worker because
126 // the specification does not allow that operation.
127 void SetController(const ServiceWorkerDescriptor& aServiceWorker);
129 // Mark the ClientSource as controlled using the remote operation arguments.
130 // This will in turn call SetController().
131 RefPtr<ClientOpPromise> Control(const ClientControlledArgs& aArgs);
133 // Inherit the controller from a local parent client. This requires both
134 // setting our immediate controller field and also updating the parent-side
135 // data structure.
136 void InheritController(const ServiceWorkerDescriptor& aServiceWorker);
138 // Get the ClientSource's current controlling service worker, if one has
139 // been set.
140 const Maybe<ServiceWorkerDescriptor>& GetController() const;
142 // Note that the client has reached DOMContentLoaded. Only applies to window
143 // clients.
144 void NoteDOMContentLoaded();
146 // TODO: Convert Focus() to MOZ_CAN_RUN_SCRIPT
147 MOZ_CAN_RUN_SCRIPT_BOUNDARY RefPtr<ClientOpPromise> Focus(
148 const ClientFocusArgs& aArgs);
150 RefPtr<ClientOpPromise> PostMessage(const ClientPostMessageArgs& aArgs);
152 RefPtr<ClientOpPromise> GetInfoAndState(
153 const ClientGetInfoAndStateArgs& aArgs);
155 Result<ClientState, ErrorResult> SnapshotState();
157 nsISerialEventTarget* EventTarget() const;
159 void SetCsp(nsIContentSecurityPolicy* aCsp);
160 void SetPreloadCsp(nsIContentSecurityPolicy* aPreloadCSP);
161 void SetCspInfo(const mozilla::ipc::CSPInfo& aCSPInfo);
162 const Maybe<mozilla::ipc::CSPInfo>& GetCspInfo();
164 void SetAgentClusterId(const nsID& aId) {
165 mClientInfo.SetAgentClusterId(aId);
168 void Traverse(nsCycleCollectionTraversalCallback& aCallback,
169 const char* aName, uint32_t aFlags);
171 void NoteCalledRegisterForServiceWorkerScope(const nsACString& aScope);
173 bool CalledRegisterForServiceWorkerScope(const nsACString& aScope);
175 nsIPrincipal* GetPrincipal();
178 inline void ImplCycleCollectionUnlink(UniquePtr<ClientSource>& aField) {
179 aField.reset();
182 inline void ImplCycleCollectionTraverse(
183 nsCycleCollectionTraversalCallback& aCallback,
184 UniquePtr<ClientSource>& aField, const char* aName, uint32_t aFlags) {
185 if (aField) {
186 aField->Traverse(aCallback, aName, aFlags);
190 } // namespace dom
191 } // namespace mozilla
193 #endif // _mozilla_dom_ClientSource_h