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_FlyWebPublishedServerIPC_h
8 : #define mozilla_dom_FlyWebPublishedServerIPC_h
9 :
10 : #include "HttpServer.h"
11 : #include "mozilla/dom/FlyWebPublishedServer.h"
12 : #include "mozilla/dom/PFlyWebPublishedServerParent.h"
13 : #include "mozilla/dom/PFlyWebPublishedServerChild.h"
14 : #include "mozilla/MozPromise.h"
15 : #include "nsICancelable.h"
16 : #include "nsIDOMEventListener.h"
17 : #include "nsISupportsImpl.h"
18 :
19 : class nsPIDOMWindowInner;
20 :
21 : namespace mozilla {
22 : namespace net {
23 : class TransportProviderParent;
24 : class TransportProviderChild;
25 : }
26 :
27 : namespace dom {
28 :
29 : class FlyWebPublishedServerParent;
30 :
31 : class FlyWebPublishedServerImpl final : public FlyWebPublishedServer
32 : , public HttpServerListener
33 : {
34 : public:
35 : FlyWebPublishedServerImpl(nsPIDOMWindowInner* aOwner,
36 : const nsAString& aName,
37 : const FlyWebPublishOptions& aOptions);
38 :
39 : NS_DECL_ISUPPORTS_INHERITED
40 :
41 0 : int32_t Port()
42 : {
43 0 : return mHttpServer ? mHttpServer->GetPort() : 0;
44 : }
45 0 : void GetCertKey(nsACString& aKey) {
46 0 : if (mHttpServer) {
47 0 : mHttpServer->GetCertKey(aKey);
48 : } else {
49 0 : aKey.Truncate();
50 : }
51 0 : }
52 :
53 : virtual void PermissionGranted(bool aGranted) override;
54 : virtual void OnFetchResponse(InternalRequest* aRequest,
55 : InternalResponse* aResponse) override;
56 : virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
57 : InternalResponse* aResponse) override;
58 : virtual already_AddRefed<nsITransportProvider>
59 : OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
60 : const Optional<nsAString>& aProtocol,
61 : ErrorResult& aRv) override;
62 :
63 0 : void SetCancelRegister(nsICancelable* aCancelRegister)
64 : {
65 0 : mMDNSCancelRegister = aCancelRegister;
66 0 : }
67 :
68 : virtual void Close() override;
69 :
70 : // HttpServerListener
71 : virtual void OnServerStarted(nsresult aStatus) override;
72 0 : virtual void OnRequest(InternalRequest* aRequest) override
73 : {
74 0 : FireFetchEvent(aRequest);
75 0 : }
76 0 : virtual void OnWebSocket(InternalRequest* aConnectRequest) override
77 : {
78 0 : FireWebsocketEvent(aConnectRequest);
79 0 : }
80 0 : virtual void OnServerClose() override
81 : {
82 0 : mHttpServer = nullptr;
83 0 : Close();
84 0 : }
85 :
86 : private:
87 0 : ~FlyWebPublishedServerImpl() {}
88 :
89 : RefPtr<HttpServer> mHttpServer;
90 : nsCOMPtr<nsICancelable> mMDNSCancelRegister;
91 : RefPtr<FlyWebPublishedServerParent> mServerParent;
92 : };
93 :
94 : class FlyWebPublishedServerChild final : public FlyWebPublishedServer
95 : , public PFlyWebPublishedServerChild
96 : {
97 : public:
98 : FlyWebPublishedServerChild(nsPIDOMWindowInner* aOwner,
99 : const nsAString& aName,
100 : const FlyWebPublishOptions& aOptions);
101 :
102 : virtual void PermissionGranted(bool aGranted) override;
103 : virtual mozilla::ipc::IPCResult RecvServerReady(const nsresult& aStatus) override;
104 : virtual mozilla::ipc::IPCResult RecvServerClose() override;
105 : virtual mozilla::ipc::IPCResult RecvFetchRequest(const IPCInternalRequest& aRequest,
106 : const uint64_t& aRequestId) override;
107 : virtual mozilla::ipc::IPCResult RecvWebSocketRequest(const IPCInternalRequest& aRequest,
108 : const uint64_t& aRequestId,
109 : PTransportProviderChild* aProvider) override;
110 :
111 : virtual void OnFetchResponse(InternalRequest* aRequest,
112 : InternalResponse* aResponse) override;
113 : virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
114 : InternalResponse* aResponse) override;
115 : virtual already_AddRefed<nsITransportProvider>
116 : OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
117 : const Optional<nsAString>& aProtocol,
118 : ErrorResult& aRv) override;
119 :
120 : virtual void Close() override;
121 :
122 : virtual void ActorDestroy(ActorDestroyReason aWhy) override;
123 :
124 : private:
125 0 : ~FlyWebPublishedServerChild() {}
126 :
127 : nsDataHashtable<nsRefPtrHashKey<InternalRequest>, uint64_t> mPendingRequests;
128 : nsRefPtrHashtable<nsUint64HashKey, TransportProviderChild>
129 : mPendingTransportProviders;
130 : bool mActorExists;
131 : };
132 :
133 : class FlyWebPublishedServerParent final : public PFlyWebPublishedServerParent
134 : , public nsIDOMEventListener
135 : {
136 : public:
137 : FlyWebPublishedServerParent(const nsAString& aName,
138 : const FlyWebPublishOptions& aOptions);
139 :
140 : NS_DECL_ISUPPORTS
141 : NS_DECL_NSIDOMEVENTLISTENER
142 :
143 : private:
144 : virtual void
145 : ActorDestroy(ActorDestroyReason aWhy) override;
146 :
147 : virtual mozilla::ipc::IPCResult
148 : Recv__delete__() override;
149 : virtual mozilla::ipc::IPCResult
150 : RecvFetchResponse(const IPCInternalResponse& aResponse,
151 : const uint64_t& aRequestId) override;
152 : virtual mozilla::ipc::IPCResult
153 : RecvWebSocketResponse(const IPCInternalResponse& aResponse,
154 : const uint64_t& aRequestId) override;
155 : virtual mozilla::ipc::IPCResult
156 : RecvWebSocketAccept(const nsString& aProtocol,
157 : const uint64_t& aRequestId) override;
158 :
159 0 : ~FlyWebPublishedServerParent() {}
160 :
161 : bool mActorDestroyed;
162 : uint64_t mNextRequestId;
163 : nsRefPtrHashtable<nsUint64HashKey, InternalRequest> mPendingRequests;
164 : nsRefPtrHashtable<nsUint64HashKey, TransportProviderParent>
165 : mPendingTransportProviders;
166 : RefPtr<FlyWebPublishedServerImpl> mPublishedServer;
167 : };
168 :
169 : } // namespace dom
170 : } // namespace mozilla
171 :
172 : #endif // mozilla_dom_FlyWebPublishedServerIPC_h
|