Move cansendstreamdata() from quicconnection to quicsession.  no functional change, not flag protected.

PiperOrigin-RevId: 312172952
Change-Id: I1bcff4cf48ca8f0e895e99ea76e90bfa911e0e62
diff --git a/quic/core/quic_connection.cc b/quic/core/quic_connection.cc
index d692913..b647916 100644
--- a/quic/core/quic_connection.cc
+++ b/quic/core/quic_connection.cc
@@ -3177,22 +3177,6 @@
          packet_creator_.HasPendingFrames() || !buffered_packets_.empty();
 }
 
-bool QuicConnection::CanWriteStreamData() {
-  // Don't write stream data if there are negotiation or queued data packets
-  // to send. Otherwise, continue and bundle as many frames as possible.
-  if (pending_version_negotiation_packet_ || !buffered_packets_.empty()) {
-    return false;
-  }
-
-  IsHandshake pending_handshake =
-      visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE;
-  // Sending queued packets may have caused the socket to become write blocked,
-  // or the congestion manager to prohibit sending.  If we've sent everything
-  // we had queued and we're still not blocked, let the visitor know it can
-  // write more.
-  return ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, pending_handshake);
-}
-
 void QuicConnection::SetNetworkTimeouts(QuicTime::Delta handshake_timeout,
                                         QuicTime::Delta idle_timeout) {
   QUIC_BUG_IF(idle_timeout > handshake_timeout)
diff --git a/quic/core/quic_connection.h b/quic/core/quic_connection.h
index f931567e..0f8f997 100644
--- a/quic/core/quic_connection.h
+++ b/quic/core/quic_connection.h
@@ -158,9 +158,6 @@
   // or yielded to other connections.
   virtual bool WillingAndAbleToWrite() const = 0;
 
-  // Called to ask if any handshake messages are pending in this visitor.
-  virtual bool HasPendingHandshake() const = 0;
-
   // Called to ask if the connection should be kept alive and prevented
   // from timing out, for example if there are outstanding application
   // transactions expecting a response.
@@ -672,14 +669,10 @@
     return server_supported_versions_;
   }
 
-  // Testing only.
+  bool HasQueuedPackets() const { return !buffered_packets_.empty(); }
+  // Testing only. TODO(ianswett): Use a peer instead.
   size_t NumQueuedPackets() const { return buffered_packets_.size(); }
 
-  // Returns true if the underlying UDP socket is writable, there is
-  // no queued data and the connection is not congestion-control
-  // blocked.
-  bool CanWriteStreamData();
-
   // Returns true if the connection has queued packets or frames.
   bool HasQueuedData() const;
 
diff --git a/quic/core/quic_connection_test.cc b/quic/core/quic_connection_test.cc
index 2e48f49..c53b730 100644
--- a/quic/core/quic_connection_test.cc
+++ b/quic/core/quic_connection_test.cc
@@ -740,7 +740,7 @@
 
   // Ensures the connection can write stream data before writing.
   QuicConsumedData EnsureWritableAndSendStreamData5() {
-    EXPECT_TRUE(CanWriteStreamData());
+    EXPECT_TRUE(CanWrite(HAS_RETRANSMITTABLE_DATA));
     return SendStreamData5();
   }
 
@@ -1092,7 +1092,6 @@
     EXPECT_CALL(*send_algorithm_, OnApplicationLimited(_)).Times(AnyNumber());
     EXPECT_CALL(visitor_, WillingAndAbleToWrite()).Times(AnyNumber());
     EXPECT_CALL(visitor_, OnPacketDecrypted(_)).Times(AnyNumber());
-    EXPECT_CALL(visitor_, HasPendingHandshake()).Times(AnyNumber());
     EXPECT_CALL(visitor_, OnCanWrite())
         .WillRepeatedly(Invoke(&notifier_, &SimpleSessionNotifier::OnCanWrite));
     EXPECT_CALL(visitor_, ShouldKeepConnectionAlive())
@@ -7226,7 +7225,7 @@
   connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason",
                               ConnectionCloseBehavior::SILENT_CLOSE);
   EXPECT_FALSE(connection_.connected());
-  EXPECT_FALSE(connection_.CanWriteStreamData());
+  EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA));
   std::unique_ptr<QuicPacket> packet =
       ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_INITIAL);
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _))
@@ -7250,7 +7249,7 @@
   connection_.CloseConnection(QUIC_PEER_GOING_AWAY, "no reason",
                               ConnectionCloseBehavior::SILENT_CLOSE);
   EXPECT_FALSE(connection_.connected());
-  EXPECT_FALSE(connection_.CanWriteStreamData());
+  EXPECT_FALSE(connection_.CanWrite(HAS_RETRANSMITTABLE_DATA));
 
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _))
       .Times(0);
diff --git a/quic/core/quic_session.cc b/quic/core/quic_session.cc
index 033495f..32ada39 100644
--- a/quic/core/quic_session.cc
+++ b/quic/core/quic_session.cc
@@ -619,7 +619,7 @@
                                    ConnectionCloseBehavior::SILENT_CLOSE);
       return;
     }
-    if (!connection_->CanWriteStreamData()) {
+    if (!CanWriteStreamData()) {
       return;
     }
     currently_writing_stream_id_ = write_blocked_streams_.PopFront();
@@ -2279,6 +2279,18 @@
   return QuicUtils::GenerateStatelessResetToken(connection_->connection_id());
 }
 
+bool QuicSession::CanWriteStreamData() const {
+  // Don't write stream data if there are queued data packets.
+  if (connection_->HasQueuedPackets()) {
+    return false;
+  }
+  // Immediately write handshake data.
+  if (HasPendingHandshake()) {
+    return true;
+  }
+  return connection_->CanWrite(HAS_RETRANSMITTABLE_DATA);
+}
+
 bool QuicSession::RetransmitLostData() {
   QuicConnection::ScopedPacketFlusher retransmission_flusher(connection_);
   // Retransmit crypto data first.
@@ -2311,7 +2323,7 @@
     }
   }
   while (!streams_with_pending_retransmission_.empty()) {
-    if (!connection_->CanWriteStreamData()) {
+    if (!CanWriteStreamData()) {
       break;
     }
     // Retransmit lost data on headers and data streams.
diff --git a/quic/core/quic_session.h b/quic/core/quic_session.h
index 626349c..490f4d0 100644
--- a/quic/core/quic_session.h
+++ b/quic/core/quic_session.h
@@ -117,7 +117,6 @@
   void OnAckNeedsRetransmittableFrame() override;
   void SendPing() override;
   bool WillingAndAbleToWrite() const override;
-  bool HasPendingHandshake() const override;
   void OnPathDegrading() override;
   bool AllowSelfAddressChange() const override;
   HandshakeState GetHandshakeState() const override;
@@ -364,6 +363,10 @@
   // Called when stream |id| is newly waiting for acks.
   void OnStreamWaitingForAcks(QuicStreamId id);
 
+  // Returns true if there is pending handshake data in the crypto stream.
+  // TODO(ianswett): Make this private or remove.
+  bool HasPendingHandshake() const;
+
   // Returns true if the session has data to be sent, either queued in the
   // connection, or in a write-blocked stream.
   bool HasDataToWrite() const;
@@ -705,6 +708,9 @@
   // if all lost data is retransmitted. Returns false otherwise.
   bool RetransmitLostData();
 
+  // Returns true if stream data should be written.
+  bool CanWriteStreamData() const;
+
   // Closes the pending stream |stream_id| before it has been created.
   void ClosePendingStream(QuicStreamId stream_id);
 
diff --git a/quic/test_tools/quic_test_utils.h b/quic/test_tools/quic_test_utils.h
index df91aab..bec5692 100644
--- a/quic/test_tools/quic_test_utils.h
+++ b/quic/test_tools/quic_test_utils.h
@@ -506,7 +506,6 @@
               (override));
   MOCK_METHOD(void, OnPathDegrading, (), (override));
   MOCK_METHOD(bool, WillingAndAbleToWrite, (), (const, override));
-  MOCK_METHOD(bool, HasPendingHandshake, (), (const, override));
   MOCK_METHOD(bool, ShouldKeepConnectionAlive, (), (const, override));
   MOCK_METHOD(void,
               OnSuccessfulVersionNegotiation,
diff --git a/quic/test_tools/simulator/quic_endpoint.cc b/quic/test_tools/simulator/quic_endpoint.cc
index c908f98..5f8ee56 100644
--- a/quic/test_tools/simulator/quic_endpoint.cc
+++ b/quic/test_tools/simulator/quic_endpoint.cc
@@ -153,9 +153,6 @@
   }
   return bytes_to_transfer_ != 0;
 }
-bool QuicEndpoint::HasPendingHandshake() const {
-  return false;
-}
 bool QuicEndpoint::ShouldKeepConnectionAlive() const {
   return true;
 }
diff --git a/quic/test_tools/simulator/quic_endpoint.h b/quic/test_tools/simulator/quic_endpoint.h
index b5be76f..9a3277b 100644
--- a/quic/test_tools/simulator/quic_endpoint.h
+++ b/quic/test_tools/simulator/quic_endpoint.h
@@ -53,7 +53,6 @@
   bool SendProbingData() override;
   void OnStatelessResetForProbing() override {}
   bool WillingAndAbleToWrite() const override;
-  bool HasPendingHandshake() const override;
   bool ShouldKeepConnectionAlive() const override;
 
   void OnWindowUpdateFrame(const QuicWindowUpdateFrame& /*frame*/) override {}