Refactor QuicStream::WriteBufferedData by moving the logic to calculate send window size into its own function.

PiperOrigin-RevId: 580523729
diff --git a/quiche/quic/core/quic_stream.cc b/quiche/quic/core/quic_stream.cc
index 3f8fbe0..8bdf9d0 100644
--- a/quiche/quic/core/quic_stream.cc
+++ b/quiche/quic/core/quic_stream.cc
@@ -1211,20 +1211,11 @@
 
   bool fin = fin_buffered_;
 
+  QUIC_BUG_IF(quic_bug_10586_13, !flow_controller_.has_value())
+      << ENDPOINT << "WriteBufferedData called on stream without flow control";
+
   // How much data flow control permits to be written.
-  QuicByteCount send_window;
-  if (flow_controller_.has_value()) {
-    send_window = flow_controller_->SendWindowSize();
-  } else {
-    send_window = std::numeric_limits<QuicByteCount>::max();
-    QUIC_BUG(quic_bug_10586_13)
-        << ENDPOINT
-        << "WriteBufferedData called on stream without flow control";
-  }
-  if (stream_contributes_to_connection_flow_control_) {
-    send_window =
-        std::min(send_window, connection_flow_controller_->SendWindowSize());
-  }
+  QuicByteCount send_window = CalculateSendWindowSize();
 
   if (send_window == 0 && !fin_with_zero_data) {
     // Quick return if nothing can be sent.
@@ -1431,4 +1422,18 @@
   sequencer()->SetUnblocked();
 }
 
+QuicByteCount QuicStream::CalculateSendWindowSize() const {
+  QuicByteCount send_window;
+  if (flow_controller_.has_value()) {
+    send_window = flow_controller_->SendWindowSize();
+  } else {
+    send_window = std::numeric_limits<QuicByteCount>::max();
+  }
+  if (stream_contributes_to_connection_flow_control_) {
+    send_window =
+        std::min(send_window, connection_flow_controller_->SendWindowSize());
+  }
+  return send_window;
+}
+
 }  // namespace quic
diff --git a/quiche/quic/core/quic_stream.h b/quiche/quic/core/quic_stream.h
index 42f1775..f5c7d8a 100644
--- a/quiche/quic/core/quic_stream.h
+++ b/quiche/quic/core/quic_stream.h
@@ -497,6 +497,10 @@
   // Write buffered data (in send buffer) at |level|.
   void WriteBufferedData(EncryptionLevel level);
 
+  // Returns the min of stream level flow control window size and connection
+  // level flow control window size.
+  QuicByteCount CalculateSendWindowSize() const;
+
   // Called when bytes are sent to the peer.
   void AddBytesSent(QuicByteCount bytes);