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