Use the same HttpStreamFactoryImpl for Websockets.

Currently, HttpNetworkSession owns two HttpStreamFactoryImpl instances:
one for Websocket requests, one for other requests.  This is
unnecessary.  This CL makes HttpNetworkSession own and use a single
HttpStreamFactoryImpl for all requests.

HttpStreamFactoryImpl manages JobControllers, and JobController manages
Jobs.  Every JobController and Job corresponds to a request and is thus
either Websocket or non-Websocket.  Before this CL, Job called
JobController through the Job::Delegate interface to find out whether
it was for a Websocket request, and JobController called
HttpStreamFactoryImpl.  This CL removes these calls and instead passes a
Boolean in the constructors that is stored in const members of
JobController and Job.

Bug: 801564
Change-Id: I9e5784713f38dd5871455c96626e9e0d3354e160
Reviewed-on: https://chromium-review.googlesource.com/864562
Commit-Queue: Bence Béky <[email protected]>
Reviewed-by: Adam Rice <[email protected]>
Cr-Commit-Position: refs/heads/master@{#529314}
diff --git a/net/http/http_network_session.cc b/net/http/http_network_session.cc
index c5871b06..4b1fe0d 100644
--- a/net/http/http_network_session.cc
+++ b/net/http/http_network_session.cc
@@ -5,7 +5,7 @@
 #include "net/http/http_network_session.h"
 
 #include <inttypes.h>
-#include <memory>
+
 #include <utility>
 
 #include "base/atomic_sequence_num.h"
@@ -232,9 +232,8 @@
                          AddDefaultHttp2Settings(params.http2_settings),
                          params.time_func,
                          context.proxy_delegate),
-      http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
-      http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
-      network_stream_throttler_(new NetworkThrottleManagerImpl()),
+      http_stream_factory_(std::make_unique<HttpStreamFactoryImpl>(this)),
+      network_stream_throttler_(std::make_unique<NetworkThrottleManagerImpl>()),
       params_(params),
       context_(context) {
   DCHECK(proxy_service_);
diff --git a/net/http/http_network_session.h b/net/http/http_network_session.h
index 734a40e53..2a20aad3a 100644
--- a/net/http/http_network_session.h
+++ b/net/http/http_network_session.h
@@ -290,9 +290,6 @@
   HttpStreamFactory* http_stream_factory() {
     return http_stream_factory_.get();
   }
-  HttpStreamFactory* http_stream_factory_for_websocket() {
-    return http_stream_factory_for_websocket_.get();
-  }
   NetworkThrottleManager* throttler() {
     return network_stream_throttler_.get();
   }
@@ -371,7 +368,6 @@
   QuicStreamFactory quic_stream_factory_;
   SpdySessionPool spdy_session_pool_;
   std::unique_ptr<HttpStreamFactory> http_stream_factory_;
-  std::unique_ptr<HttpStreamFactory> http_stream_factory_for_websocket_;
   std::map<HttpResponseBodyDrainer*, std::unique_ptr<HttpResponseBodyDrainer>>
       response_drainers_;
   std::unique_ptr<NetworkThrottleManager> network_stream_throttler_;
diff --git a/net/http/http_network_session_peer.cc b/net/http/http_network_session_peer.cc
index 9b33d98..e9c629e8 100644
--- a/net/http/http_network_session_peer.cc
+++ b/net/http/http_network_session_peer.cc
@@ -29,11 +29,6 @@
   session_->http_stream_factory_.swap(http_stream_factory);
 }
 
-void HttpNetworkSessionPeer::SetHttpStreamFactoryForWebSocket(
-    std::unique_ptr<HttpStreamFactory> http_stream_factory) {
-  session_->http_stream_factory_for_websocket_.swap(http_stream_factory);
-}
-
 void HttpNetworkSessionPeer::SetNetworkStreamThrottler(
     std::unique_ptr<NetworkThrottleManager> network_throttle_manager) {
   session_->network_stream_throttler_.swap(network_throttle_manager);
diff --git a/net/http/http_network_session_peer.h b/net/http/http_network_session_peer.h
index dae4f916..bb0d0c7d 100644
--- a/net/http/http_network_session_peer.h
+++ b/net/http/http_network_session_peer.h
@@ -29,8 +29,6 @@
 
   void SetHttpStreamFactory(
       std::unique_ptr<HttpStreamFactory> http_stream_factory);
-  void SetHttpStreamFactoryForWebSocket(
-      std::unique_ptr<HttpStreamFactory> http_stream_factory_for_websocket);
 
   void SetNetworkStreamThrottler(
       std::unique_ptr<NetworkThrottleManager> network_throttle_manager);
diff --git a/net/http/http_network_transaction.cc b/net/http/http_network_transaction.cc
index 46345079..fc8171b2 100644
--- a/net/http/http_network_transaction.cc
+++ b/net/http/http_network_transaction.cc
@@ -858,12 +858,10 @@
     DCHECK(!enable_alternative_services_);
   if (ForWebSocketHandshake()) {
     stream_request_ =
-        session_->http_stream_factory_for_websocket()
-            ->RequestWebSocketHandshakeStream(
-                *request_, priority_, server_ssl_config_, proxy_ssl_config_,
-                this, websocket_handshake_stream_base_create_helper_,
-                enable_ip_based_pooling_, enable_alternative_services_,
-                net_log_);
+        session_->http_stream_factory()->RequestWebSocketHandshakeStream(
+            *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
+            websocket_handshake_stream_base_create_helper_,
+            enable_ip_based_pooling_, enable_alternative_services_, net_log_);
   } else {
     stream_request_ = session_->http_stream_factory()->RequestStream(
         *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
diff --git a/net/http/http_network_transaction_unittest.cc b/net/http/http_network_transaction_unittest.cc
index 73a2055c..d50ada4a 100644
--- a/net/http/http_network_transaction_unittest.cc
+++ b/net/http/http_network_transaction_unittest.cc
@@ -15970,8 +15970,7 @@
     HttpNetworkSessionPeer peer(session.get());
     FakeStreamFactory* fake_factory = new FakeStreamFactory();
     FakeWebSocketStreamCreateHelper websocket_stream_create_helper;
-    peer.SetHttpStreamFactoryForWebSocket(
-        std::unique_ptr<HttpStreamFactory>(fake_factory));
+    peer.SetHttpStreamFactory(std::unique_ptr<HttpStreamFactory>(fake_factory));
 
     HttpRequestInfo request;
     HttpNetworkTransaction trans(LOW, session.get());
diff --git a/net/http/http_stream_factory_impl.cc b/net/http/http_stream_factory_impl.cc
index eafcc67..ffc73e2 100644
--- a/net/http/http_stream_factory_impl.cc
+++ b/net/http/http_stream_factory_impl.cc
@@ -31,11 +31,8 @@
 
 namespace net {
 
-HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session,
-                                             bool for_websockets)
-    : session_(session),
-      job_factory_(new JobFactory()),
-      for_websockets_(for_websockets) {}
+HttpStreamFactoryImpl::HttpStreamFactoryImpl(HttpNetworkSession* session)
+    : session_(session), job_factory_(new JobFactory()) {}
 
 HttpStreamFactoryImpl::~HttpStreamFactoryImpl() {}
 
@@ -48,11 +45,10 @@
     bool enable_ip_based_pooling,
     bool enable_alternative_services,
     const NetLogWithSource& net_log) {
-  DCHECK(!for_websockets_);
   return RequestStreamInternal(
       request_info, priority, server_ssl_config, proxy_ssl_config, delegate,
-      nullptr, HttpStreamRequest::HTTP_STREAM, enable_ip_based_pooling,
-      enable_alternative_services, net_log);
+      nullptr, HttpStreamRequest::HTTP_STREAM, false /* is_websocket */,
+      enable_ip_based_pooling, enable_alternative_services, net_log);
 }
 
 std::unique_ptr<HttpStreamRequest>
@@ -66,12 +62,11 @@
     bool enable_ip_based_pooling,
     bool enable_alternative_services,
     const NetLogWithSource& net_log) {
-  DCHECK(for_websockets_);
   DCHECK(create_helper);
   return RequestStreamInternal(
       request_info, priority, server_ssl_config, proxy_ssl_config, delegate,
-      create_helper, HttpStreamRequest::HTTP_STREAM, enable_ip_based_pooling,
-      enable_alternative_services, net_log);
+      create_helper, HttpStreamRequest::HTTP_STREAM, true /* is_websocket */,
+      enable_ip_based_pooling, enable_alternative_services, net_log);
 }
 
 std::unique_ptr<HttpStreamRequest>
@@ -84,12 +79,12 @@
     bool enable_ip_based_pooling,
     bool enable_alternative_services,
     const NetLogWithSource& net_log) {
-  DCHECK(!for_websockets_);
   DCHECK(request_info.url.SchemeIs(url::kHttpsScheme));
 
   return RequestStreamInternal(
       request_info, priority, server_ssl_config, proxy_ssl_config, delegate,
-      nullptr, HttpStreamRequest::BIDIRECTIONAL_STREAM, enable_ip_based_pooling,
+      nullptr, HttpStreamRequest::BIDIRECTIONAL_STREAM,
+      false /* is_websocket */, enable_ip_based_pooling,
       enable_alternative_services, net_log);
 }
 
@@ -102,12 +97,13 @@
     WebSocketHandshakeStreamBase::CreateHelper*
         websocket_handshake_stream_create_helper,
     HttpStreamRequest::StreamType stream_type,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     bool enable_alternative_services,
     const NetLogWithSource& net_log) {
   auto job_controller = std::make_unique<JobController>(
       this, delegate, session_, job_factory_.get(), request_info,
-      /* is_preconnect = */ false, enable_ip_based_pooling,
+      /* is_preconnect = */ false, is_websocket, enable_ip_based_pooling,
       enable_alternative_services, server_ssl_config, proxy_ssl_config);
   JobController* job_controller_raw_ptr = job_controller.get();
   job_controller_set_.insert(std::move(job_controller));
@@ -128,11 +124,10 @@
   server_ssl_config.verify_ev_cert = true;
   proxy_ssl_config.verify_ev_cert = true;
 
-  DCHECK(!for_websockets_);
-
   auto job_controller = std::make_unique<JobController>(
       this, nullptr, session_, job_factory_.get(), request_info,
       /* is_preconnect = */ true,
+      /* is_websocket = */ false,
       /* enable_ip_based_pooling = */ true,
       /* enable_alternative_services = */ true, server_ssl_config,
       proxy_ssl_config);
diff --git a/net/http/http_stream_factory_impl.h b/net/http/http_stream_factory_impl.h
index 9cdf592c..30480ed 100644
--- a/net/http/http_stream_factory_impl.h
+++ b/net/http/http_stream_factory_impl.h
@@ -36,10 +36,7 @@
   class NET_EXPORT_PRIVATE JobController;
   class NET_EXPORT_PRIVATE JobFactory;
   class NET_EXPORT_PRIVATE Request;
-  // RequestStream may only be called if |for_websockets| is false.
-  // RequestWebSocketHandshakeStream may only be called if |for_websockets|
-  // is true.
-  HttpStreamFactoryImpl(HttpNetworkSession* session, bool for_websockets);
+  HttpStreamFactoryImpl(HttpNetworkSession* session);
   ~HttpStreamFactoryImpl() override;
 
   // HttpStreamFactory interface
@@ -126,6 +123,7 @@
       HttpStreamRequest::Delegate* delegate,
       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
       HttpStreamRequest::StreamType stream_type,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       bool enable_alternative_services,
       const NetLogWithSource& net_log);
@@ -173,8 +171,6 @@
   // preconnects should be skipped.
   std::set<PreconnectingProxyServer> preconnecting_proxy_servers_;
 
-  const bool for_websockets_;
-
   DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImpl);
 };
 
diff --git a/net/http/http_stream_factory_impl_job.cc b/net/http/http_stream_factory_impl_job.cc
index 34393b3..0a5801a0 100644
--- a/net/http/http_stream_factory_impl_job.cc
+++ b/net/http/http_stream_factory_impl_job.cc
@@ -165,6 +165,7 @@
                                 NextProto alternative_protocol,
                                 QuicTransportVersion quic_version,
                                 const ProxyServer& alternative_proxy_server,
+                                bool is_websocket,
                                 bool enable_ip_based_pooling,
                                 NetLog* net_log)
     : request_info_(request_info),
@@ -181,6 +182,7 @@
       destination_(destination),
       origin_url_(origin_url),
       alternative_proxy_server_(alternative_proxy_server),
+      is_websocket_(is_websocket),
       enable_ip_based_pooling_(enable_ip_based_pooling),
       delegate_(delegate),
       job_type_(job_type),
@@ -245,7 +247,7 @@
   if (using_quic_) {
     DCHECK(session_->IsQuicEnabled());
   }
-  if (job_type_ == PRECONNECT || delegate_->for_websockets()) {
+  if (job_type_ == PRECONNECT || is_websocket_) {
     DCHECK(request_info_.socket_tag == SocketTag());
   }
 }
@@ -423,12 +425,12 @@
   }
 
   // We need to make sure that if a spdy session was created for
-  // https://somehost/ that we don't use that session for http://somehost:443/.
+  // https://somehost/ then we do not use that session for http://somehost:443/.
   // The only time we can use an existing session is if the request URL is
-  // https (the normal case) or if we're connection to a SPDY proxy.
+  // https (the normal case) or if we are connecting to a SPDY proxy.
   // https://crbug.com/133176
-  // TODO(ricea): Add "wss" back to this list when SPDY WebSocket support is
-  // working.
+  // TODO(bnc): Add kWssScheme back to this list when WebSockets over HTTP/2 is
+  // implemented.  https://crbug.com/801564.
   return origin_url_.SchemeIs(url::kHttpsScheme) ||
          proxy_info_.proxy_server().is_https();
 }
@@ -436,7 +438,7 @@
 void HttpStreamFactoryImpl::Job::OnStreamReadyCallback() {
   DCHECK(stream_.get());
   DCHECK_NE(job_type_, PRECONNECT);
-  DCHECK(!delegate_->for_websockets());
+  DCHECK(!is_websocket_);
 
   MaybeCopyConnectionAttemptsFromSocketOrHandle();
 
@@ -447,7 +449,7 @@
 void HttpStreamFactoryImpl::Job::OnWebSocketHandshakeStreamReadyCallback() {
   DCHECK(websocket_stream_);
   DCHECK_NE(job_type_, PRECONNECT);
-  DCHECK(delegate_->for_websockets());
+  DCHECK(is_websocket_);
 
   MaybeCopyConnectionAttemptsFromSocketOrHandle();
 
@@ -647,7 +649,7 @@
         base::ThreadTaskRunnerHandle::Get()->PostTask(
             FROM_HERE, base::Bind(&Job::OnNewSpdySessionReadyCallback,
                                   ptr_factory_.GetWeakPtr()));
-      } else if (delegate_->for_websockets()) {
+      } else if (is_websocket_) {
         DCHECK(websocket_stream_);
         base::ThreadTaskRunnerHandle::Get()->PostTask(
             FROM_HERE, base::Bind(&Job::OnWebSocketHandshakeStreamReadyCallback,
@@ -951,7 +953,7 @@
   }
 
   if (job_type_ == PRECONNECT) {
-    DCHECK(!delegate_->for_websockets());
+    DCHECK(!is_websocket_);
     DCHECK(request_info_.socket_tag == SocketTag());
     return PreconnectSocketsForHttpRequest(
         GetSocketGroup(), destination_, request_info_.extra_headers,
@@ -968,7 +970,7 @@
           ? base::Bind(&Job::OnHostResolution, session_->spdy_session_pool(),
                        spdy_session_key_, enable_ip_based_pooling_)
           : OnHostResolutionCallback();
-  if (delegate_->for_websockets()) {
+  if (is_websocket_) {
     DCHECK(request_info_.socket_tag == SocketTag());
     SSLConfig websocket_server_ssl_config = server_ssl_config_;
     websocket_server_ssl_config.alpn_protos.clear();
@@ -1144,9 +1146,9 @@
 int HttpStreamFactoryImpl::Job::SetSpdyHttpStreamOrBidirectionalStreamImpl(
     base::WeakPtr<SpdySession> session,
     bool direct) {
-  // TODO(ricea): Restore the code for WebSockets over SPDY once it's
-  // implemented.
-  if (delegate_->for_websockets())
+  // TODO(bnc): Restore the code for WebSockets over HTTP/2 once it is
+  // implemented.  https://crbug.com/801564.
+  if (is_websocket_)
     return ERR_NOT_IMPLEMENTED;
   if (stream_type_ == HttpStreamRequest::BIDIRECTIONAL_STREAM) {
     bidirectional_stream_impl_ = std::make_unique<BidirectionalStreamSpdyImpl>(
@@ -1184,7 +1186,7 @@
     bool using_proxy = (proxy_info_.is_http() || proxy_info_.is_https()) &&
                        (request_info_.url.SchemeIs(url::kHttpScheme) ||
                         request_info_.url.SchemeIs(url::kFtpScheme));
-    if (delegate_->for_websockets()) {
+    if (is_websocket_) {
       DCHECK_NE(job_type_, PRECONNECT);
       DCHECK(delegate_->websocket_handshake_stream_create_helper());
       websocket_stream_ =
@@ -1470,12 +1472,13 @@
     const SSLConfig& proxy_ssl_config,
     HostPortPair destination,
     GURL origin_url,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log) {
   return std::make_unique<HttpStreamFactoryImpl::Job>(
       delegate, job_type, session, request_info, priority, proxy_info,
       server_ssl_config, proxy_ssl_config, destination, origin_url,
-      kProtoUnknown, QUIC_VERSION_UNSUPPORTED, ProxyServer(),
+      kProtoUnknown, QUIC_VERSION_UNSUPPORTED, ProxyServer(), is_websocket,
       enable_ip_based_pooling, net_log);
 }
 
@@ -1493,12 +1496,13 @@
     GURL origin_url,
     NextProto alternative_protocol,
     QuicTransportVersion quic_version,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log) {
   return std::make_unique<HttpStreamFactoryImpl::Job>(
       delegate, job_type, session, request_info, priority, proxy_info,
       server_ssl_config, proxy_ssl_config, destination, origin_url,
-      alternative_protocol, quic_version, ProxyServer(),
+      alternative_protocol, quic_version, ProxyServer(), is_websocket,
       enable_ip_based_pooling, net_log);
 }
 
@@ -1515,13 +1519,14 @@
     HostPortPair destination,
     GURL origin_url,
     const ProxyServer& alternative_proxy_server,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log) {
   return std::make_unique<HttpStreamFactoryImpl::Job>(
       delegate, job_type, session, request_info, priority, proxy_info,
       server_ssl_config, proxy_ssl_config, destination, origin_url,
       kProtoUnknown, QUIC_VERSION_UNSUPPORTED, alternative_proxy_server,
-      enable_ip_based_pooling, net_log);
+      is_websocket, enable_ip_based_pooling, net_log);
 }
 
 }  // namespace net
diff --git a/net/http/http_stream_factory_impl_job.h b/net/http/http_stream_factory_impl_job.h
index ae5b449..aa8769964 100644
--- a/net/http/http_stream_factory_impl_job.h
+++ b/net/http/http_stream_factory_impl_job.h
@@ -152,8 +152,6 @@
     websocket_handshake_stream_create_helper() = 0;
 
     virtual void MaybeSetWaitTimeForMainJob(const base::TimeDelta& delay) = 0;
-
-    virtual bool for_websockets() = 0;
   };
 
   // Job is owned by |delegate|, hence |delegate| is valid for the lifetime of
@@ -192,6 +190,7 @@
       NextProto alternative_protocol,
       QuicTransportVersion quic_version,
       const ProxyServer& alternative_proxy_server,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log);
   virtual ~Job();
@@ -338,7 +337,7 @@
   void ResumeInitConnection();
   // Creates a SpdyHttpStream or a BidirectionalStreamImpl from the given values
   // and sets to |stream_| or |bidirectional_stream_impl_| respectively. Does
-  // nothing if |stream_factory_| is for WebSockets.
+  // nothing if |stream_factory_| is for WebSocket.
   int SetSpdyHttpStreamOrBidirectionalStreamImpl(
       base::WeakPtr<SpdySession> session,
       bool direct);
@@ -430,6 +429,9 @@
   // request.
   const ProxyServer alternative_proxy_server_;
 
+  // True if request is for Websocket.
+  const bool is_websocket_;
+
   // Enable pooling to a SpdySession with matching IP and certificate
   // even if the SpdySessionKey is different.
   const bool enable_ip_based_pooling_;
@@ -535,6 +537,7 @@
       const SSLConfig& proxy_ssl_config,
       HostPortPair destination,
       GURL origin_url,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log);
 
@@ -551,6 +554,7 @@
       GURL origin_url,
       NextProto alternative_protocol,
       QuicTransportVersion quic_version,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log);
 
@@ -566,6 +570,7 @@
       HostPortPair destination,
       GURL origin_url,
       const ProxyServer& alternative_proxy_server,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log);
 };
diff --git a/net/http/http_stream_factory_impl_job_controller.cc b/net/http/http_stream_factory_impl_job_controller.cc
index 1ab56db3..0722a63 100644
--- a/net/http/http_stream_factory_impl_job_controller.cc
+++ b/net/http/http_stream_factory_impl_job_controller.cc
@@ -65,6 +65,7 @@
     JobFactory* job_factory,
     const HttpRequestInfo& request_info,
     bool is_preconnect,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     bool enable_alternative_services,
     const SSLConfig& server_ssl_config,
@@ -75,6 +76,7 @@
       request_(nullptr),
       delegate_(delegate),
       is_preconnect_(is_preconnect),
+      is_websocket_(is_websocket),
       enable_ip_based_pooling_(enable_ip_based_pooling),
       enable_alternative_services_(enable_alternative_services),
       alternative_job_net_error_(OK),
@@ -113,10 +115,6 @@
   net_log_.EndEvent(NetLogEventType::HTTP_STREAM_JOB_CONTROLLER);
 }
 
-bool HttpStreamFactoryImpl::JobController::for_websockets() {
-  return factory_->for_websockets_;
-}
-
 std::unique_ptr<HttpStreamFactoryImpl::Request>
 HttpStreamFactoryImpl::JobController::Start(
     HttpStreamRequest::Delegate* delegate,
@@ -211,7 +209,7 @@
     const ProxyInfo& proxy_info,
     std::unique_ptr<HttpStream> stream) {
   DCHECK(request_->completed());
-  DCHECK(!factory_->for_websockets_);
+  DCHECK(!is_websocket_);
   DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, request_->stream_type());
 
   main_job_.reset();
@@ -228,7 +226,7 @@
         const ProxyInfo& used_proxy_info,
         std::unique_ptr<BidirectionalStreamImpl> stream) {
   DCHECK(request_->completed());
-  DCHECK(!factory_->for_websockets_);
+  DCHECK(!is_websocket_);
   DCHECK_EQ(HttpStreamRequest::BIDIRECTIONAL_STREAM, request_->stream_type());
 
   main_job_.reset();
@@ -258,7 +256,7 @@
 
   if (!request_)
     return;
-  DCHECK(!factory_->for_websockets_);
+  DCHECK(!is_websocket_);
   DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, request_->stream_type());
   OnJobSucceeded(job);
   DCHECK(request_->completed());
@@ -286,7 +284,7 @@
   std::unique_ptr<BidirectionalStreamImpl> stream =
       job->ReleaseBidirectionalStream();
   DCHECK(stream);
-  DCHECK(!factory_->for_websockets_);
+  DCHECK(!is_websocket_);
   DCHECK_EQ(HttpStreamRequest::BIDIRECTIONAL_STREAM, request_->stream_type());
 
   OnJobSucceeded(job);
@@ -306,7 +304,7 @@
 
   if (!request_)
     return;
-  DCHECK(factory_->for_websockets_);
+  DCHECK(is_websocket_);
   DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, request_->stream_type());
   DCHECK(stream);
 
@@ -495,9 +493,9 @@
 
     MarkRequestComplete(was_alpn_negotiated, negotiated_protocol, using_spdy);
 
-    if (for_websockets()) {
-      // TODO(ricea): Re-instate this code when WebSockets over SPDY is
-      // implemented.
+    if (is_websocket_) {
+      // TODO(bnc): Re-instate this code when WebSockets over HTTP/2 is
+      // implemented.  https://crbug.com/801564.
       NOTREACHED();
     } else if (job->stream_type() == HttpStreamRequest::BIDIRECTIONAL_STREAM) {
       std::unique_ptr<BidirectionalStreamImpl> bidirectional_stream_impl =
@@ -818,12 +816,12 @@
           this, PRECONNECT, session_, request_info_, IDLE, proxy_info_,
           server_ssl_config_, proxy_ssl_config_, alternative_destination,
           origin_url, alternative_service_info_.protocol(), quic_version,
-          enable_ip_based_pooling_, session_->net_log());
+          is_websocket_, enable_ip_based_pooling_, session_->net_log());
     } else {
       main_job_ = job_factory_->CreateMainJob(
           this, PRECONNECT, session_, request_info_, IDLE, proxy_info_,
           server_ssl_config_, proxy_ssl_config_, destination, origin_url,
-          enable_ip_based_pooling_, session_->net_log());
+          is_websocket_, enable_ip_based_pooling_, session_->net_log());
     }
     main_job_->Preconnect(num_streams_);
     return OK;
@@ -831,7 +829,7 @@
   main_job_ = job_factory_->CreateMainJob(
       this, MAIN, session_, request_info_, priority_, proxy_info_,
       server_ssl_config_, proxy_ssl_config_, destination, origin_url,
-      enable_ip_based_pooling_, net_log_.net_log());
+      is_websocket_, enable_ip_based_pooling_, net_log_.net_log());
   // Alternative Service can only be set for HTTPS requests while Alternative
   // Proxy is set for HTTP requests.
   if (alternative_service_info_.protocol() != kProtoUnknown) {
@@ -849,7 +847,7 @@
         this, ALTERNATIVE, session_, request_info_, priority_, proxy_info_,
         server_ssl_config_, proxy_ssl_config_, alternative_destination,
         origin_url, alternative_service_info_.protocol(), quic_version,
-        enable_ip_based_pooling_, net_log_.net_log());
+        is_websocket_, enable_ip_based_pooling_, net_log_.net_log());
 
     main_job_is_blocked_ = true;
     alternative_job_->Start(request_->stream_type());
@@ -864,7 +862,7 @@
       alternative_job_ = job_factory_->CreateAltProxyJob(
           this, ALTERNATIVE, session_, request_info_, priority_,
           alternative_proxy_info, server_ssl_config_, proxy_ssl_config_,
-          destination, origin_url, alternative_proxy_server,
+          destination, origin_url, alternative_proxy_server, is_websocket_,
           enable_ip_based_pooling_, net_log_.net_log());
 
       can_start_alternative_proxy_job_ = false;
@@ -915,7 +913,7 @@
   RemoveRequestFromSpdySessionRequestMap();
 
   if (bound_job_->job_type() == MAIN && alternative_job_) {
-    DCHECK(!for_websockets());
+    DCHECK(!is_websocket_);
     // Allow |alternative_job_| to run to completion, rather than resetting it
     // to check if there is any broken alternative service to report.
     // OnOrphanedJobComplete() will clean up |this| when the job completes.
diff --git a/net/http/http_stream_factory_impl_job_controller.h b/net/http/http_stream_factory_impl_job_controller.h
index f23ccdb..48d5cc6 100644
--- a/net/http/http_stream_factory_impl_job_controller.h
+++ b/net/http/http_stream_factory_impl_job_controller.h
@@ -35,6 +35,7 @@
                 JobFactory* job_factory,
                 const HttpRequestInfo& request_info,
                 bool is_preconnect,
+                bool is_websocket,
                 bool enable_ip_based_pooling,
                 bool enable_alternative_services,
                 const SSLConfig& server_ssl_config,
@@ -42,8 +43,6 @@
 
   ~JobController() override;
 
-  bool for_websockets() override;
-
   // Used in tests only for verification purpose.
   const Job* main_job() const { return main_job_.get(); }
   const Job* alternative_job() const { return alternative_job_.get(); }
@@ -327,6 +326,9 @@
   // True if this JobController is used to preconnect streams.
   const bool is_preconnect_;
 
+  // True if request is for Websocket.
+  const bool is_websocket_;
+
   // Enable pooling to a SpdySession with matching IP and certificate even if
   // the SpdySessionKey is different.
   const bool enable_ip_based_pooling_;
diff --git a/net/http/http_stream_factory_impl_job_controller_unittest.cc b/net/http/http_stream_factory_impl_job_controller_unittest.cc
index 009aada..48bfe8c 100644
--- a/net/http/http_stream_factory_impl_job_controller_unittest.cc
+++ b/net/http/http_stream_factory_impl_job_controller_unittest.cc
@@ -245,8 +245,9 @@
     if (create_job_controller_) {
       job_controller_ = new HttpStreamFactoryImpl::JobController(
           factory_, &request_delegate_, session_.get(), &job_factory_,
-          request_info, is_preconnect_, enable_ip_based_pooling_,
-          enable_alternative_services_, SSLConfig(), SSLConfig());
+          request_info, is_preconnect_, false /* is_websocket */,
+          enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+          SSLConfig());
       HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller_);
     }
   }
@@ -438,8 +439,9 @@
     HttpStreamFactoryImpl::JobController* job_controller =
         new HttpStreamFactoryImpl::JobController(
             factory_, &request_delegate_, session_.get(), &default_job_factory_,
-            request_info, is_preconnect_, enable_ip_based_pooling_,
-            enable_alternative_services_, SSLConfig(), SSLConfig());
+            request_info, is_preconnect_, false /* is_websocket */,
+            enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+            SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
     return job_controller->Start(&request_delegate_, nullptr, net_log_.bound(),
                                  HttpStreamRequest::HTTP_STREAM,
@@ -2109,8 +2111,9 @@
     HttpStreamFactoryImpl::JobController* job_controller =
         new HttpStreamFactoryImpl::JobController(
             factory_, request_delegates[i].get(), session_.get(), &job_factory_,
-            request_info, is_preconnect_, enable_ip_based_pooling_,
-            enable_alternative_services_, SSLConfig(), SSLConfig());
+            request_info, is_preconnect_, false /* is_websocket */,
+            enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+            SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
     auto request = job_controller->Start(
         request_delegates[i].get(), nullptr, net_log_.bound(),
@@ -2182,8 +2185,9 @@
     HttpStreamFactoryImpl::JobController* job_controller =
         new HttpStreamFactoryImpl::JobController(
             factory_, request_delegates[i].get(), session_.get(), &job_factory_,
-            request_info, is_preconnect_, enable_ip_based_pooling_,
-            enable_alternative_services_, SSLConfig(), SSLConfig());
+            request_info, is_preconnect_, false /* is_websocket */,
+            enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+            SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
     auto request = job_controller->Start(
         request_delegates[i].get(), nullptr, net_log_.bound(),
@@ -2254,8 +2258,9 @@
     HttpStreamFactoryImpl::JobController* job_controller =
         new HttpStreamFactoryImpl::JobController(
             factory_, request_delegates[i].get(), session_.get(), &job_factory_,
-            request_info, is_preconnect_, enable_ip_based_pooling_,
-            enable_alternative_services_, SSLConfig(), SSLConfig());
+            request_info, is_preconnect_, false /* is_websocket */,
+            enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+            SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
     auto request = job_controller->Start(
         request_delegates[i].get(), nullptr, net_log_.bound(),
@@ -2307,8 +2312,9 @@
     HttpStreamFactoryImpl::JobController* job_controller =
         new HttpStreamFactoryImpl::JobController(
             factory_, request_delegates[i].get(), session_.get(), &job_factory_,
-            request_info, is_preconnect_, enable_ip_based_pooling_,
-            enable_alternative_services_, SSLConfig(), SSLConfig());
+            request_info, is_preconnect_, false /* is_websocket */,
+            enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+            SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
     job_controller->Preconnect(1);
     EXPECT_TRUE(job_controller->main_job());
@@ -2353,8 +2359,9 @@
     HttpStreamFactoryImpl::JobController* job_controller =
         new HttpStreamFactoryImpl::JobController(
             factory_, request_delegates[i].get(), session_.get(), &job_factory_,
-            request_info, is_preconnect_, enable_ip_based_pooling_,
-            enable_alternative_services_, SSLConfig(), SSLConfig());
+            request_info, is_preconnect_, false /* is_websocket */,
+            enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+            SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
     auto request = job_controller->Start(
         request_delegates[i].get(), nullptr, net_log_.bound(),
@@ -2413,8 +2420,9 @@
   HttpStreamFactoryImpl::JobController* job_controller =
       new HttpStreamFactoryImpl::JobController(
           factory_, &request_delegate_, session_.get(), &default_job_factory,
-          request_info, is_preconnect_, enable_ip_based_pooling_,
-          enable_alternative_services_, SSLConfig(), SSLConfig());
+          request_info, is_preconnect_, false /* is_websocket */,
+          enable_ip_based_pooling_, enable_alternative_services_, SSLConfig(),
+          SSLConfig());
   HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller);
   request_ =
       job_controller->Start(&request_delegate_, nullptr, net_log_.bound(),
@@ -2507,6 +2515,7 @@
     job_controller_ = new HttpStreamFactoryImpl::JobController(
         factory_, &request_delegate_, session_.get(), &job_factory_,
         request_info_, /* is_preconnect = */ true,
+        /* is_websocket = */ false,
         /* enable_ip_based_pooling = */ true,
         /* enable_alternative_services = */ true, SSLConfig(), SSLConfig());
     HttpStreamFactoryImplPeer::AddJobController(factory_, job_controller_);
diff --git a/net/http/http_stream_factory_impl_request_unittest.cc b/net/http/http_stream_factory_impl_request_unittest.cc
index 612b136d..7e96567 100644
--- a/net/http/http_stream_factory_impl_request_unittest.cc
+++ b/net/http/http_stream_factory_impl_request_unittest.cc
@@ -42,6 +42,7 @@
   auto job_controller = std::make_unique<HttpStreamFactoryImpl::JobController>(
       factory, &request_delegate, session.get(), &job_factory, request_info,
       /* is_preconnect = */ false,
+      /* is_websocket = */ false,
       /* enable_ip_based_pooling = */ true,
       /* enable_alternative_services = */ true, SSLConfig(), SSLConfig());
   HttpStreamFactoryImpl::JobController* job_controller_raw_ptr =
diff --git a/net/http/http_stream_factory_impl_unittest.cc b/net/http/http_stream_factory_impl_unittest.cc
index 995158ac..21f84d6 100644
--- a/net/http/http_stream_factory_impl_unittest.cc
+++ b/net/http/http_stream_factory_impl_unittest.cc
@@ -158,7 +158,7 @@
 class MockHttpStreamFactoryImplForPreconnect : public HttpStreamFactoryImpl {
  public:
   explicit MockHttpStreamFactoryImplForPreconnect(HttpNetworkSession* session)
-      : HttpStreamFactoryImpl(session, false),
+      : HttpStreamFactoryImpl(session),
         preconnect_done_(false),
         waiting_for_preconnect_(false) {}
 
@@ -1806,12 +1806,11 @@
   StreamRequestWaiter waiter;
   WebSocketStreamCreateHelper create_helper;
   std::unique_ptr<HttpStreamRequest> request(
-      session->http_stream_factory_for_websocket()
-          ->RequestWebSocketHandshakeStream(
-              request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, &waiter,
-              &create_helper,
-              /* enable_ip_based_pooling = */ true,
-              /* enable_alternative_services = */ true, NetLogWithSource()));
+      session->http_stream_factory()->RequestWebSocketHandshakeStream(
+          request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, &waiter,
+          &create_helper,
+          /* enable_ip_based_pooling = */ true,
+          /* enable_alternative_services = */ true, NetLogWithSource()));
   waiter.WaitForStream();
   EXPECT_TRUE(waiter.stream_done());
   EXPECT_TRUE(nullptr == waiter.stream());
@@ -1851,12 +1850,11 @@
   StreamRequestWaiter waiter;
   WebSocketStreamCreateHelper create_helper;
   std::unique_ptr<HttpStreamRequest> request(
-      session->http_stream_factory_for_websocket()
-          ->RequestWebSocketHandshakeStream(
-              request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, &waiter,
-              &create_helper,
-              /* enable_ip_based_pooling = */ true,
-              /* enable_alternative_services = */ true, NetLogWithSource()));
+      session->http_stream_factory()->RequestWebSocketHandshakeStream(
+          request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, &waiter,
+          &create_helper,
+          /* enable_ip_based_pooling = */ true,
+          /* enable_alternative_services = */ true, NetLogWithSource()));
   waiter.WaitForStream();
   EXPECT_TRUE(waiter.stream_done());
   EXPECT_TRUE(nullptr == waiter.stream());
@@ -1894,12 +1892,11 @@
   StreamRequestWaiter waiter;
   WebSocketStreamCreateHelper create_helper;
   std::unique_ptr<HttpStreamRequest> request(
-      session->http_stream_factory_for_websocket()
-          ->RequestWebSocketHandshakeStream(
-              request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, &waiter,
-              &create_helper,
-              /* enable_ip_based_pooling = */ true,
-              /* enable_alternative_services = */ true, NetLogWithSource()));
+      session->http_stream_factory()->RequestWebSocketHandshakeStream(
+          request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, &waiter,
+          &create_helper,
+          /* enable_ip_based_pooling = */ true,
+          /* enable_alternative_services = */ true, NetLogWithSource()));
   waiter.WaitForStream();
   EXPECT_TRUE(waiter.stream_done());
   EXPECT_TRUE(nullptr == waiter.stream());
diff --git a/net/http/http_stream_factory_test_util.cc b/net/http/http_stream_factory_test_util.cc
index 264030e..b3c0fae1 100644
--- a/net/http/http_stream_factory_test_util.cc
+++ b/net/http/http_stream_factory_test_util.cc
@@ -29,6 +29,7 @@
     NextProto alternative_protocol,
     QuicTransportVersion quic_version,
     const ProxyServer& alternative_proxy_server,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log)
     : HttpStreamFactoryImpl::Job(delegate,
@@ -44,6 +45,7 @@
                                  alternative_protocol,
                                  quic_version,
                                  alternative_proxy_server,
+                                 is_websocket,
                                  enable_ip_based_pooling,
                                  net_log) {
   DCHECK(!is_waiting());
@@ -69,6 +71,7 @@
     const SSLConfig& proxy_ssl_config,
     HostPortPair destination,
     GURL origin_url,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log) {
   if (override_main_job_url_)
@@ -77,8 +80,8 @@
   auto main_job = std::make_unique<MockHttpStreamFactoryImplJob>(
       delegate, job_type, session, request_info, priority, proxy_info,
       SSLConfig(), SSLConfig(), destination, origin_url, kProtoUnknown,
-      QUIC_VERSION_UNSUPPORTED, ProxyServer(), enable_ip_based_pooling,
-      net_log);
+      QUIC_VERSION_UNSUPPORTED, ProxyServer(), is_websocket,
+      enable_ip_based_pooling, net_log);
 
   // Keep raw pointer to Job but pass ownership.
   main_job_ = main_job.get();
@@ -99,12 +102,14 @@
     GURL origin_url,
     NextProto alternative_protocol,
     QuicTransportVersion quic_version,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log) {
   auto alternative_job = std::make_unique<MockHttpStreamFactoryImplJob>(
       delegate, job_type, session, request_info, priority, proxy_info,
       SSLConfig(), SSLConfig(), destination, origin_url, alternative_protocol,
-      quic_version, ProxyServer(), enable_ip_based_pooling, net_log);
+      quic_version, ProxyServer(), is_websocket, enable_ip_based_pooling,
+      net_log);
 
   // Keep raw pointer to Job but pass ownership.
   alternative_job_ = alternative_job.get();
@@ -124,12 +129,13 @@
     HostPortPair destination,
     GURL origin_url,
     const ProxyServer& alternative_proxy_server,
+    bool is_websocket,
     bool enable_ip_based_pooling,
     NetLog* net_log) {
   auto alternative_job = std::make_unique<MockHttpStreamFactoryImplJob>(
       delegate, job_type, session, request_info, priority, proxy_info,
       SSLConfig(), SSLConfig(), destination, origin_url, kProtoUnknown,
-      QUIC_VERSION_UNSUPPORTED, alternative_proxy_server,
+      QUIC_VERSION_UNSUPPORTED, alternative_proxy_server, is_websocket,
       enable_ip_based_pooling, net_log);
 
   // Keep raw pointer to Job but pass ownership.
diff --git a/net/http/http_stream_factory_test_util.h b/net/http/http_stream_factory_test_util.h
index 238cc50..b22d53a 100644
--- a/net/http/http_stream_factory_test_util.h
+++ b/net/http/http_stream_factory_test_util.h
@@ -120,6 +120,7 @@
                                NextProto alternative_protocol,
                                QuicTransportVersion quic_version,
                                const ProxyServer& alternative_proxy_server,
+                               bool is_websocket,
                                bool enable_ip_based_pooling,
                                NetLog* net_log);
 
@@ -147,6 +148,7 @@
       const SSLConfig& proxy_ssl_config,
       HostPortPair destination,
       GURL origin_url,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log) override;
 
@@ -163,6 +165,7 @@
       GURL origin_url,
       NextProto alternative_protocol,
       QuicTransportVersion quic_version,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log) override;
 
@@ -178,6 +181,7 @@
       HostPortPair destination,
       GURL origin_url,
       const ProxyServer& alternative_proxy_server,
+      bool is_websocket,
       bool enable_ip_based_pooling,
       NetLog* net_log) override;