Line data Source code
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_PresentationSessionInfo_h
8 : #define mozilla_dom_PresentationSessionInfo_h
9 :
10 : #include "base/process.h"
11 : #include "mozilla/dom/nsIContentParent.h"
12 : #include "mozilla/dom/Promise.h"
13 : #include "mozilla/dom/PromiseNativeHandler.h"
14 : #include "mozilla/DebugOnly.h"
15 : #include "mozilla/RefPtr.h"
16 : #include "nsCOMPtr.h"
17 : #include "nsINetworkInfoService.h"
18 : #include "nsIPresentationControlChannel.h"
19 : #include "nsIPresentationDevice.h"
20 : #include "nsIPresentationListener.h"
21 : #include "nsIPresentationService.h"
22 : #include "nsIPresentationSessionTransport.h"
23 : #include "nsIPresentationSessionTransportBuilder.h"
24 : #include "nsIServerSocket.h"
25 : #include "nsITimer.h"
26 : #include "nsString.h"
27 : #include "PresentationCallbacks.h"
28 :
29 : namespace mozilla {
30 : namespace dom {
31 :
32 : class PresentationSessionInfo : public nsIPresentationSessionTransportCallback
33 : , public nsIPresentationControlChannelListener
34 : , public nsIPresentationSessionTransportBuilderListener
35 : {
36 : public:
37 : NS_DECL_ISUPPORTS
38 : NS_DECL_NSIPRESENTATIONSESSIONTRANSPORTCALLBACK
39 : NS_DECL_NSIPRESENTATIONSESSIONTRANSPORTBUILDERLISTENER
40 :
41 0 : PresentationSessionInfo(const nsAString& aUrl,
42 : const nsAString& aSessionId,
43 : const uint8_t aRole)
44 0 : : mUrl(aUrl)
45 : , mSessionId(aSessionId)
46 : , mIsResponderReady(false)
47 : , mIsTransportReady(false)
48 : , mState(nsIPresentationSessionListener::STATE_CONNECTING)
49 0 : , mReason(NS_OK)
50 : {
51 0 : MOZ_ASSERT(!mUrl.IsEmpty());
52 0 : MOZ_ASSERT(!mSessionId.IsEmpty());
53 0 : MOZ_ASSERT(aRole == nsIPresentationService::ROLE_CONTROLLER ||
54 : aRole == nsIPresentationService::ROLE_RECEIVER);
55 0 : mRole = aRole;
56 0 : }
57 :
58 : virtual nsresult Init(nsIPresentationControlChannel* aControlChannel);
59 :
60 0 : const nsAString& GetUrl() const
61 : {
62 0 : return mUrl;
63 : }
64 :
65 0 : const nsAString& GetSessionId() const
66 : {
67 0 : return mSessionId;
68 : }
69 :
70 : uint8_t GetRole() const
71 : {
72 : return mRole;
73 : }
74 :
75 : nsresult SetListener(nsIPresentationSessionListener* aListener);
76 :
77 0 : void SetDevice(nsIPresentationDevice* aDevice)
78 : {
79 0 : mDevice = aDevice;
80 0 : }
81 :
82 0 : already_AddRefed<nsIPresentationDevice> GetDevice() const
83 : {
84 0 : nsCOMPtr<nsIPresentationDevice> device = mDevice;
85 0 : return device.forget();
86 : }
87 :
88 0 : void SetControlChannel(nsIPresentationControlChannel* aControlChannel)
89 : {
90 0 : if (mControlChannel) {
91 0 : mControlChannel->SetListener(nullptr);
92 : }
93 :
94 0 : mControlChannel = aControlChannel;
95 0 : if (mControlChannel) {
96 0 : mControlChannel->SetListener(this);
97 : }
98 0 : }
99 :
100 : nsresult Send(const nsAString& aData);
101 :
102 : nsresult SendBinaryMsg(const nsACString& aData);
103 :
104 : nsresult SendBlob(nsIDOMBlob* aBlob);
105 :
106 : nsresult Close(nsresult aReason,
107 : uint32_t aState);
108 :
109 : nsresult OnTerminate(nsIPresentationControlChannel* aControlChannel);
110 :
111 : nsresult ReplyError(nsresult aReason);
112 :
113 : virtual bool IsAccessible(base::ProcessId aProcessId);
114 :
115 0 : void SetTransportBuilderConstructor(
116 : nsIPresentationTransportBuilderConstructor* aBuilderConstructor)
117 : {
118 0 : mBuilderConstructor = aBuilderConstructor;
119 0 : }
120 :
121 : protected:
122 0 : virtual ~PresentationSessionInfo()
123 0 : {
124 0 : Shutdown(NS_OK);
125 0 : }
126 :
127 : virtual void Shutdown(nsresult aReason);
128 :
129 : nsresult ReplySuccess();
130 :
131 0 : bool IsSessionReady()
132 : {
133 0 : return mIsResponderReady && mIsTransportReady;
134 : }
135 :
136 : virtual nsresult UntrackFromService();
137 :
138 0 : void SetStateWithReason(uint32_t aState, nsresult aReason)
139 : {
140 0 : if (mState == aState) {
141 0 : return;
142 : }
143 :
144 0 : mState = aState;
145 0 : mReason = aReason;
146 :
147 : // Notify session state change.
148 0 : if (mListener) {
149 : DebugOnly<nsresult> rv =
150 0 : mListener->NotifyStateChange(mSessionId, mState, aReason);
151 0 : NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "NotifyStateChanged");
152 : }
153 : }
154 :
155 : void ContinueTermination();
156 :
157 0 : void ResetBuilder()
158 : {
159 0 : mBuilder = nullptr;
160 0 : }
161 :
162 : // Should be nsIPresentationChannelDescription::TYPE_TCP/TYPE_DATACHANNEL
163 : uint8_t mTransportType = 0;
164 :
165 : nsPIDOMWindowInner* GetWindow();
166 :
167 : nsString mUrl;
168 : nsString mSessionId;
169 : // mRole should be nsIPresentationService::ROLE_CONTROLLER
170 : // or nsIPresentationService::ROLE_RECEIVER.
171 : uint8_t mRole;
172 : bool mIsResponderReady;
173 : bool mIsTransportReady;
174 : bool mIsOnTerminating = false;
175 : uint32_t mState; // CONNECTED, CLOSED, TERMINATED
176 : nsresult mReason;
177 : nsCOMPtr<nsIPresentationSessionListener> mListener;
178 : nsCOMPtr<nsIPresentationDevice> mDevice;
179 : nsCOMPtr<nsIPresentationSessionTransport> mTransport;
180 : nsCOMPtr<nsIPresentationControlChannel> mControlChannel;
181 : nsCOMPtr<nsIPresentationSessionTransportBuilder> mBuilder;
182 : nsCOMPtr<nsIPresentationTransportBuilderConstructor> mBuilderConstructor;
183 : };
184 :
185 : // Session info with controlling browsing context (sender side) behaviors.
186 : class PresentationControllingInfo final : public PresentationSessionInfo
187 : , public nsIServerSocketListener
188 : , public nsIListNetworkAddressesListener
189 : {
190 : public:
191 : NS_DECL_ISUPPORTS_INHERITED
192 : NS_DECL_NSIPRESENTATIONCONTROLCHANNELLISTENER
193 : NS_DECL_NSISERVERSOCKETLISTENER
194 : NS_DECL_NSILISTNETWORKADDRESSESLISTENER
195 : NS_DECL_NSIPRESENTATIONSESSIONTRANSPORTCALLBACK
196 :
197 0 : PresentationControllingInfo(const nsAString& aUrl,
198 : const nsAString& aSessionId)
199 0 : : PresentationSessionInfo(aUrl,
200 : aSessionId,
201 0 : nsIPresentationService::ROLE_CONTROLLER)
202 0 : {}
203 :
204 : nsresult Init(nsIPresentationControlChannel* aControlChannel) override;
205 :
206 : nsresult Reconnect(nsIPresentationServiceCallback* aCallback);
207 :
208 : nsresult BuildTransport();
209 :
210 : private:
211 0 : ~PresentationControllingInfo()
212 0 : {
213 0 : Shutdown(NS_OK);
214 0 : }
215 :
216 : void Shutdown(nsresult aReason) override;
217 :
218 : nsresult GetAddress();
219 :
220 : nsresult OnGetAddress(const nsACString& aAddress);
221 :
222 : nsresult ContinueReconnect();
223 :
224 : nsresult NotifyReconnectResult(nsresult aStatus);
225 :
226 : nsCOMPtr<nsIServerSocket> mServerSocket;
227 : nsCOMPtr<nsIPresentationServiceCallback> mReconnectCallback;
228 : bool mIsReconnecting = false;
229 : bool mDoReconnectAfterClose = false;
230 : };
231 :
232 : // Session info with presenting browsing context (receiver side) behaviors.
233 : class PresentationPresentingInfo final : public PresentationSessionInfo
234 : , public PromiseNativeHandler
235 : , public nsITimerCallback
236 : {
237 : public:
238 : NS_DECL_ISUPPORTS_INHERITED
239 : NS_DECL_NSIPRESENTATIONCONTROLCHANNELLISTENER
240 : NS_DECL_NSITIMERCALLBACK
241 :
242 0 : PresentationPresentingInfo(const nsAString& aUrl,
243 : const nsAString& aSessionId,
244 : nsIPresentationDevice* aDevice)
245 0 : : PresentationSessionInfo(aUrl,
246 : aSessionId,
247 0 : nsIPresentationService::ROLE_RECEIVER)
248 : {
249 0 : MOZ_ASSERT(aDevice);
250 0 : SetDevice(aDevice);
251 0 : }
252 :
253 : nsresult Init(nsIPresentationControlChannel* aControlChannel) override;
254 :
255 : nsresult NotifyResponderReady();
256 : nsresult NotifyResponderFailure();
257 :
258 : NS_IMETHODIMP OnSessionTransport(nsIPresentationSessionTransport* transport) override;
259 :
260 : void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
261 :
262 : void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
263 :
264 0 : void SetPromise(Promise* aPromise)
265 : {
266 0 : mPromise = aPromise;
267 0 : mPromise->AppendNativeHandler(this);
268 0 : }
269 :
270 : bool IsAccessible(base::ProcessId aProcessId) override;
271 :
272 : nsresult DoReconnect();
273 :
274 : private:
275 0 : ~PresentationPresentingInfo()
276 0 : {
277 0 : Shutdown(NS_OK);
278 0 : }
279 :
280 : void Shutdown(nsresult aReason) override;
281 :
282 : nsresult InitTransportAndSendAnswer();
283 :
284 : nsresult UntrackFromService() override;
285 :
286 : NS_IMETHODIMP
287 : FlushPendingEvents(nsIPresentationDataChannelSessionTransportBuilder* builder);
288 :
289 : bool mHasFlushPendingEvents = false;
290 : RefPtr<PresentationResponderLoadingCallback> mLoadingCallback;
291 : nsCOMPtr<nsITimer> mTimer;
292 : nsCOMPtr<nsIPresentationChannelDescription> mRequesterDescription;
293 : nsTArray<nsString> mPendingCandidates;
294 : RefPtr<Promise> mPromise;
295 :
296 : // The content parent communicating with the content process which the OOP
297 : // receiver page belongs to.
298 : nsCOMPtr<nsIContentParent> mContentParent;
299 : };
300 :
301 : } // namespace dom
302 : } // namespace mozilla
303 :
304 : #endif // mozilla_dom_PresentationSessionInfo_h
|