Bug 1263991 part 5: Create API on FlyWebPublishedServer for getting back a nsITransport when accepting a websocket request, rather than getting back a WebSocket DOM object. r=djvj, a=lizzard
authorJonas Sicking <jonas@sicking.cc>
Tue, 07 Jun 2016 02:46:03 -0700
changeset 339625 c4da2d1e3a1084e5880a04bca3163e17623289b3
parent 339624 e9f5c7eb1b284786c66c674b9af7fbbf5b570f51
child 339626 54a3f8e44148dfe0428607bd264e82374de02a67
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdjvj, lizzard
bugs1263991
milestone49.0a2
Bug 1263991 part 5: Create API on FlyWebPublishedServer for getting back a nsITransport when accepting a websocket request, rather than getting back a WebSocket DOM object. r=djvj, a=lizzard
dom/flyweb/FlyWebPublishedServer.cpp
dom/flyweb/FlyWebPublishedServer.h
dom/flyweb/FlyWebPublishedServerIPC.h
dom/flyweb/HttpServer.cpp
dom/flyweb/HttpServer.h
--- a/dom/flyweb/FlyWebPublishedServer.cpp
+++ b/dom/flyweb/FlyWebPublishedServer.cpp
@@ -12,16 +12,17 @@
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/InternalResponse.h"
 #include "mozilla/ipc/IPCStreamUtils.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/unused.h"
 #include "nsGlobalWindow.h"
+#include "WebSocketChannel.h"
 
 namespace mozilla {
 namespace dom {
 
 static LazyLogModule gFlyWebPublishedServerLog("FlyWebPublishedServer");
 #undef LOG_I
 #define LOG_I(...) MOZ_LOG(mozilla::dom::gFlyWebPublishedServerLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
 #undef LOG_E
@@ -106,16 +107,61 @@ FlyWebPublishedServer::PublishedServerSt
   if (NS_SUCCEEDED(aStatus)) {
     mPublishPromise.Resolve(this, __func__);
   } else {
     Close();
     mPublishPromise.Reject(aStatus, __func__);
   }
 }
 
+already_AddRefed<WebSocket>
+FlyWebPublishedServer::OnWebSocketAccept(InternalRequest* aConnectRequest,
+                                         const Optional<nsAString>& aProtocol,
+                                         ErrorResult& aRv)
+{
+  MOZ_ASSERT(aConnectRequest);
+
+  LOG_I("FlyWebPublishedServer::OnWebSocketAccept(%p)", this);
+
+  nsCOMPtr<nsITransportProvider> provider =
+    OnWebSocketAcceptInternal(aConnectRequest,
+                              aProtocol,
+                              aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+  MOZ_ASSERT(provider);
+
+  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetOwner());
+  AutoJSContext cx;
+  GlobalObject global(cx, nsGlobalWindow::Cast(window)->FastGetGlobalJSObject());
+
+  nsAutoCString extensions, negotiatedExtensions;
+  aConnectRequest->Headers()->
+    Get(NS_LITERAL_CSTRING("Sec-WebSocket-Extensions"), extensions, aRv);
+  mozilla::net::ProcessServerWebSocketExtensions(extensions,
+                                                 negotiatedExtensions);
+
+  nsCString url;
+  aConnectRequest->GetURL(url);
+  Sequence<nsString> protocols;
+  if (aProtocol.WasPassed() &&
+      !protocols.AppendElement(aProtocol.Value(), fallible)) {
+    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
+    return nullptr;
+  }
+
+  return WebSocket::ConstructorCommon(global,
+                                      NS_ConvertUTF8toUTF16(url),
+                                      protocols,
+                                      provider,
+                                      negotiatedExtensions,
+                                      aRv);
+}
+
 /******** FlyWebPublishedServerImpl ********/
 
 NS_IMPL_ISUPPORTS_INHERITED0(FlyWebPublishedServerImpl, mozilla::DOMEventTargetHelper)
 
 FlyWebPublishedServerImpl::FlyWebPublishedServerImpl(nsPIDOMWindowInner* aOwner,
                                                      const nsAString& aName,
                                                      const FlyWebPublishOptions& aOptions)
   : FlyWebPublishedServer(aOwner, aName, aOptions)
@@ -161,76 +207,47 @@ FlyWebPublishedServerImpl::OnFetchRespon
 
   LOG_I("FlyWebPublishedServerImpl::OnFetchResponse(%p)", this);
 
   if (mHttpServer) {
     mHttpServer->SendResponse(aRequest, aResponse);
   }
 }
 
-already_AddRefed<WebSocket>
-FlyWebPublishedServerImpl::OnWebSocketAccept(InternalRequest* aConnectRequest,
-                                             const Optional<nsAString>& aProtocol,
-                                             ErrorResult& aRv)
-{
-  MOZ_ASSERT(aConnectRequest);
-
-  LOG_I("FlyWebPublishedMDNSServer::OnWebSocketAccept(%p)", this);
-
-  if (!mHttpServer) {
-    aRv.Throw(NS_ERROR_UNEXPECTED);
-    return nullptr;
-  }
-
-  nsAutoCString negotiatedExtensions;
-  nsCOMPtr<nsITransportProvider> provider =
-    mHttpServer->AcceptWebSocket(aConnectRequest,
-                                 aProtocol,
-                                 negotiatedExtensions,
-                                 aRv);
-  if (aRv.Failed()) {
-    return nullptr;
-  }
-  MOZ_ASSERT(provider);
-
-  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetOwner());
-  AutoJSContext cx;
-  GlobalObject global(cx, nsGlobalWindow::Cast(window)->FastGetGlobalJSObject());
-
-  nsCString url;
-  aConnectRequest->GetURL(url);
-  Sequence<nsString> protocols;
-  if (aProtocol.WasPassed() &&
-      !protocols.AppendElement(aProtocol.Value(), fallible)) {
-    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
-    return nullptr;
-  }
-
-  return WebSocket::ConstructorCommon(global,
-                                      NS_ConvertUTF8toUTF16(url),
-                                      protocols,
-                                      provider,
-                                      negotiatedExtensions,
-                                      aRv);
-}
-
 void
 FlyWebPublishedServerImpl::OnWebSocketResponse(InternalRequest* aConnectRequest,
                                                InternalResponse* aResponse)
 {
   MOZ_ASSERT(aConnectRequest);
   MOZ_ASSERT(aResponse);
 
   LOG_I("FlyWebPublishedMDNSServer::OnWebSocketResponse(%p)", this);
 
   if (mHttpServer) {
     mHttpServer->SendWebSocketResponse(aConnectRequest, aResponse);
   }
 }
 
+already_AddRefed<nsITransportProvider>
+FlyWebPublishedServerImpl::OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
+                                                     const Optional<nsAString>& aProtocol,
+                                                     ErrorResult& aRv)
+{
+  LOG_I("FlyWebPublishedServerImpl::OnWebSocketAcceptInternal(%p)", this);
+
+  if (!mHttpServer) {
+    aRv.Throw(NS_ERROR_UNEXPECTED);
+    return nullptr;
+  }
+
+  return mHttpServer->AcceptWebSocket(aConnectRequest,
+                                      aProtocol,
+                                      aRv);
+}
+
 /******** FlyWebPublishedServerChild ********/
 
 FlyWebPublishedServerChild::FlyWebPublishedServerChild(nsPIDOMWindowInner* aOwner,
                                                        const nsAString& aName,
                                                        const FlyWebPublishOptions& aOptions)
   : FlyWebPublishedServer(aOwner, aName, aOptions)
   , mActorDestroyed(false)
 {
@@ -305,22 +322,24 @@ FlyWebPublishedServerChild::OnFetchRespo
   UniquePtr<mozilla::ipc::AutoIPCStream> autoStream;
   aResponse->ToIPC(&ipcResp, Manager(), autoStream);
   Unused << SendFetchResponse(ipcResp, id);
   if (autoStream) {
     autoStream->TakeOptionalValue();
   }
 }
 
-already_AddRefed<WebSocket>
-FlyWebPublishedServerChild::OnWebSocketAccept(InternalRequest* aConnectRequest,
-                                              const Optional<nsAString>& aProtocol,
-                                              ErrorResult& aRv)
+already_AddRefed<nsITransportProvider>
+FlyWebPublishedServerChild::OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
+                                                      const Optional<nsAString>& aProtocol,
+                                                      ErrorResult& aRv)
 {
-  // Send ipdl message to parent
+  LOG_I("FlyWebPublishedServerChild::OnWebSocketAcceptInternal(%p)", this);
+
+  aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
   return nullptr;
 }
 
 void
 FlyWebPublishedServerChild::OnWebSocketResponse(InternalRequest* aConnectRequest,
                                                 InternalResponse* aResponse)
 {
   // Send ipdl message to parent
--- a/dom/flyweb/FlyWebPublishedServer.h
+++ b/dom/flyweb/FlyWebPublishedServer.h
@@ -6,16 +6,17 @@
 
 #ifndef mozilla_dom_FlyWebPublishedServer_h
 #define mozilla_dom_FlyWebPublishedServer_h
 
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/MozPromise.h"
 
 class nsPIDOMWindowInner;
+class nsITransportProvider;
 
 namespace mozilla {
 
 class ErrorResult;
 
 namespace dom {
 
 class InternalResponse;
@@ -53,22 +54,26 @@ public:
 
   void GetUiUrl(nsAString& aUiUrl)
   {
     aUiUrl = mUiUrl;
   }
 
   virtual void OnFetchResponse(InternalRequest* aRequest,
                                InternalResponse* aResponse) = 0;
-  virtual already_AddRefed<WebSocket>
+  already_AddRefed<WebSocket>
     OnWebSocketAccept(InternalRequest* aConnectRequest,
                       const Optional<nsAString>& aProtocol,
-                      ErrorResult& aRv) = 0;
+                      ErrorResult& aRv);
   virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
                                    InternalResponse* aResponse) = 0;
+  virtual already_AddRefed<nsITransportProvider>
+    OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
+                              const Optional<nsAString>& aProtocol,
+                              ErrorResult& aRv) = 0;
 
   virtual void Close();
 
   void FireFetchEvent(InternalRequest* aRequest);
   void FireWebsocketEvent(InternalRequest* aConnectRequest);
   void PublishedServerStarted(nsresult aStatus);
 
   IMPL_EVENT_HANDLER(fetch)
@@ -77,17 +82,16 @@ public:
 
   already_AddRefed<FlyWebPublishPromise>
   GetPublishPromise()
   {
     return mPublishPromise.Ensure(__func__);
   }
 
 protected:
-
   virtual ~FlyWebPublishedServer()
   {
     MOZ_ASSERT(!mIsRegistered, "Subclass dtor forgot to call Close()");
   }
 
   uint64_t mOwnerWindowID;
   MozPromiseHolder<FlyWebPublishPromise> mPublishPromise;
 
--- a/dom/flyweb/FlyWebPublishedServerIPC.h
+++ b/dom/flyweb/FlyWebPublishedServerIPC.h
@@ -42,22 +42,22 @@ public:
       mHttpServer->GetCertKey(aKey);
     } else {
       aKey.Truncate();
     }
   }
 
   virtual void OnFetchResponse(InternalRequest* aRequest,
                                InternalResponse* aResponse) override;
-  virtual already_AddRefed<WebSocket>
-    OnWebSocketAccept(InternalRequest* aConnectRequest,
-                      const Optional<nsAString>& aProtocol,
-                      ErrorResult& aRv) override;
   virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
                                    InternalResponse* aResponse) override;
+  virtual already_AddRefed<nsITransportProvider>
+    OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
+                              const Optional<nsAString>& aProtocol,
+                              ErrorResult& aRv) override;
 
   void SetCancelRegister(nsICancelable* aCancelRegister)
   {
     mMDNSCancelRegister = aCancelRegister;
   }
 
   virtual void Close() override;
 
@@ -95,22 +95,22 @@ public:
 
   virtual bool RecvServerReady(const nsresult& aStatus) override;
   virtual bool RecvServerClose() override;
   virtual bool RecvFetchRequest(const IPCInternalRequest& aRequest,
                                 const uint64_t& aRequestId) override;
 
   virtual void OnFetchResponse(InternalRequest* aRequest,
                                InternalResponse* aResponse) override;
-  virtual already_AddRefed<WebSocket>
-    OnWebSocketAccept(InternalRequest* aConnectRequest,
-                      const Optional<nsAString>& aProtocol,
-                      ErrorResult& aRv) override;
   virtual void OnWebSocketResponse(InternalRequest* aConnectRequest,
                                    InternalResponse* aResponse) override;
+  virtual already_AddRefed<nsITransportProvider>
+    OnWebSocketAcceptInternal(InternalRequest* aConnectRequest,
+                              const Optional<nsAString>& aProtocol,
+                              ErrorResult& aRv) override;
 
   virtual void Close() override;
 
   virtual void ActorDestroy(ActorDestroyReason aWhy) override;
 
 private:
   ~FlyWebPublishedServerChild() {}
 
--- a/dom/flyweb/HttpServer.cpp
+++ b/dom/flyweb/HttpServer.cpp
@@ -170,25 +170,24 @@ HttpServer::SendResponse(InternalRequest
   }
 
   MOZ_ASSERT(false, "Unknown request");
 }
 
 already_AddRefed<nsITransportProvider>
 HttpServer::AcceptWebSocket(InternalRequest* aConnectRequest,
                             const Optional<nsAString>& aProtocol,
-                            nsACString& aNegotiatedExtensions,
                             ErrorResult& aRv)
 {
   for (Connection* conn : mConnections) {
     if (!conn->HasPendingWebSocketRequest(aConnectRequest)) {
       continue;
     }
     nsCOMPtr<nsITransportProvider> provider =
-      conn->HandleAcceptWebSocket(aProtocol, aNegotiatedExtensions, aRv);
+      conn->HandleAcceptWebSocket(aProtocol, aRv);
     if (aRv.Failed()) {
       conn->Close();
     }
     // This connection is now owned by the websocket, or we just closed it
     mConnections.RemoveElement(conn);
     return provider.forget();
   }
 
@@ -785,17 +784,16 @@ HttpServer::Connection::TryHandleRespons
     }
   }
 
   return handledResponse;
 }
 
 already_AddRefed<nsITransportProvider>
 HttpServer::Connection::HandleAcceptWebSocket(const Optional<nsAString>& aProtocol,
-                                              nsACString& aNegotiatedExtensions,
                                               ErrorResult& aRv)
 {
   MOZ_ASSERT(mPendingWebSocketRequest);
 
   RefPtr<InternalResponse> response =
     new InternalResponse(101, NS_LITERAL_CSTRING("Switching Protocols"));
 
   InternalHeaders* headers = response->Headers();
@@ -825,24 +823,24 @@ HttpServer::Connection::HandleAcceptWebS
     Get(NS_LITERAL_CSTRING("Sec-WebSocket-Key"), key, aRv);
   nsresult rv = mozilla::net::CalculateWebSocketHashedSecret(key, hash);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
     return nullptr;
   }
   headers->Set(NS_LITERAL_CSTRING("Sec-WebSocket-Accept"), hash, aRv);
 
-  nsAutoCString extensions;
+  nsAutoCString extensions, negotiatedExtensions;
   mPendingWebSocketRequest->Headers()->
     Get(NS_LITERAL_CSTRING("Sec-WebSocket-Extensions"), extensions, aRv);
   mozilla::net::ProcessServerWebSocketExtensions(extensions,
-                                                 aNegotiatedExtensions);
-  if (!aNegotiatedExtensions.IsEmpty()) {
+                                                 negotiatedExtensions);
+  if (!negotiatedExtensions.IsEmpty()) {
     headers->Set(NS_LITERAL_CSTRING("Sec-WebSocket-Extensions"),
-                 aNegotiatedExtensions, aRv);
+                 negotiatedExtensions, aRv);
   }
 
   RefPtr<TransportProvider> result = new TransportProvider();
   mWebSocketTransportProvider = result;
 
   QueueResponse(response);
 
   return result.forget();
--- a/dom/flyweb/HttpServer.h
+++ b/dom/flyweb/HttpServer.h
@@ -50,17 +50,16 @@ public:
   NS_DECL_NSILOCALCERTGETCALLBACK
 
   void Init(int32_t aPort, bool aHttps, HttpServerListener* aListener);
 
   void SendResponse(InternalRequest* aRequest, InternalResponse* aResponse);
   already_AddRefed<nsITransportProvider>
     AcceptWebSocket(InternalRequest* aConnectRequest,
                     const Optional<nsAString>& aProtocol,
-                    nsACString& aNegotiatedExtensions,
                     ErrorResult& aRv);
   void SendWebSocketResponse(InternalRequest* aConnectRequest,
                              InternalResponse* aResponse);
 
   void Close();
 
   void GetCertKey(nsACString& aKey);
 
@@ -108,17 +107,16 @@ private:
     NS_DECL_NSIINPUTSTREAMCALLBACK
     NS_DECL_NSIOUTPUTSTREAMCALLBACK
     NS_DECL_NSITLSSERVERSECURITYOBSERVER
 
     bool TryHandleResponse(InternalRequest* aRequest,
                            InternalResponse* aResponse);
     already_AddRefed<nsITransportProvider>
       HandleAcceptWebSocket(const Optional<nsAString>& aProtocol,
-                            nsACString& aNegotiatedExtensions,
                             ErrorResult& aRv);
     void HandleWebSocketResponse(InternalResponse* aResponse);
     bool HasPendingWebSocketRequest(InternalRequest* aRequest)
     {
       return aRequest == mPendingWebSocketRequest;
     }
 
     void Close();