Use CompletionRepeatingCallback for io_callback_.

Many classes in //net have a private io_callback_ member that is
initialized upon class construction and is passed every time an async
operation is called.  This CL introduces CompletionRepeatingCallback and
uses it in seven classes, where this member is used multiple times.
(This part is no-op, since currently base::Callback is aliased to
base::RepeatingCallback, and base::Bind to base::BindRepeating.)

The rationale is that both a CompletionRepeatingCallback and a
CompletionOnceCallback has a scoped_refptr<BindStateBase> deep under the
hood (using a scoped_refptr for OnceCallback seems unnecessary, but this
allows efficient cast from RepeatingCallback to OnceCallback).
Instantiating such a scoped_refptr on the heap is more expensive than
copying an existing CompletionRepeatingCallback (which reuses the
BindStateBase object, only increases its reference count).  Therefore it
is better to create and reuse a CompletionRepeatingCallback then to bind
a new CompletionOnceCallback every time in classes that use io_callback_
often.

On the other hand, HttpStreamFactoryImpl::JobController::io_callback_ is
only used once, for proxy resolution, and only if there is a proxy
involved.  Therefore it is better to bind this callback at the call
site.  (This CompletionCallback will later be changed to a
CompletionOnceCallback once ProxyResolutionService::ResolveProxy()
supports that.)

Bug: 807724
Change-Id: Ib342b057b8960789514baa21afcda0820a633404
Reviewed-on: https://chromium-review.googlesource.com/1016671
Reviewed-by: Misha Efimov <mef@chromium.org>
Commit-Queue: Bence Béky <bnc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#553546}
diff --git a/net/BUILD.gn b/net/BUILD.gn
index d73d538..80ecf7f 100644
--- a/net/BUILD.gn
+++ b/net/BUILD.gn
@@ -109,6 +109,7 @@
     "base/auth.h",
     "base/completion_callback.h",
     "base/completion_once_callback.h",
+    "base/completion_repeating_callback.h",
     "base/datagram_buffer.cc",
     "base/datagram_buffer.h",
     "base/escape.cc",
diff --git a/net/base/completion_once_callback.h b/net/base/completion_once_callback.h
index bfe4e7c..f299bbf 100644
--- a/net/base/completion_once_callback.h
+++ b/net/base/completion_once_callback.h
@@ -14,15 +14,15 @@
 
 // A OnceCallback specialization that takes a single int parameter. Usually this
 // is used to report a byte count or network error code.
-typedef base::OnceCallback<void(int)> CompletionOnceCallback;
+using CompletionOnceCallback = base::OnceCallback<void(int)>;
 
 // 64bit version of the OnceCallback specialization that takes a single int64_t
 // parameter. Usually this is used to report a file offset, size or network
 // error code.
-typedef base::OnceCallback<void(int64_t)> Int64CompletionOnceCallback;
+using Int64CompletionOnceCallback = base::OnceCallback<void(int64_t)>;
 
-typedef base::CancelableOnceCallback<void(int)>
-    CancelableCompletionOnceCallback;
+using CancelableCompletionOnceCallback =
+    base::CancelableOnceCallback<void(int)>;
 
 }  // namespace net
 
diff --git a/net/base/completion_repeating_callback.h b/net/base/completion_repeating_callback.h
new file mode 100644
index 0000000..0e96e6c
--- /dev/null
+++ b/net/base/completion_repeating_callback.h
@@ -0,0 +1,29 @@
+// Copyright 2018 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NET_BASE_COMPLETION_REPEATING_CALLBACK_H_
+#define NET_BASE_COMPLETION_REPEATING_CALLBACK_H_
+
+#include <stdint.h>
+
+#include "base/callback.h"
+#include "base/cancelable_callback.h"
+
+namespace net {
+
+// A RepeatingCallback specialization that takes a single int parameter. Usually
+// this is used to report a byte count or network error code.
+using CompletionRepeatingCallback = base::RepeatingCallback<void(int)>;
+
+// 64bit version of the RepeatingCallback specialization that takes a single
+// int64_t parameter. Usually this is used to report a file offset, size or
+// network error code.
+using Int64CompletionRepeatingCallback = base::RepeatingCallback<void(int64_t)>;
+
+using CancelableCompletionRepeatingCallback =
+    base::CancelableRepeatingCallback<void(int)>;
+
+}  // namespace net
+
+#endif  // NET_BASE_COMPLETION_REPEATING_CALLBACK_H_
diff --git a/net/ftp/ftp_network_transaction.cc b/net/ftp/ftp_network_transaction.cc
index 1e2888c..086d0c7 100644
--- a/net/ftp/ftp_network_transaction.cc
+++ b/net/ftp/ftp_network_transaction.cc
@@ -217,8 +217,8 @@
     HostResolver* resolver,
     ClientSocketFactory* socket_factory)
     : command_sent_(COMMAND_NONE),
-      io_callback_(base::Bind(&FtpNetworkTransaction::OnIOComplete,
-                              base::Unretained(this))),
+      io_callback_(base::BindRepeating(&FtpNetworkTransaction::OnIOComplete,
+                                       base::Unretained(this))),
       request_(nullptr),
       resolver_(resolver),
       read_ctrl_buf_(new IOBuffer(kCtrlBufLen)),
diff --git a/net/ftp/ftp_network_transaction.h b/net/ftp/ftp_network_transaction.h
index a6769ce..64f710a 100644
--- a/net/ftp/ftp_network_transaction.h
+++ b/net/ftp/ftp_network_transaction.h
@@ -16,6 +16,7 @@
 #include "base/memory/ref_counted.h"
 #include "net/base/address_list.h"
 #include "net/base/auth.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/net_export.h"
 #include "net/dns/host_resolver.h"
 #include "net/ftp/ftp_ctrl_response_buffer.h"
@@ -201,7 +202,7 @@
 
   Command command_sent_;
 
-  CompletionCallback io_callback_;
+  CompletionRepeatingCallback io_callback_;
   CompletionCallback user_callback_;
 
   NetLogWithSource net_log_;
diff --git a/net/http/http_cache_transaction.cc b/net/http/http_cache_transaction.cc
index 10eafcf..1a9fbedb 100644
--- a/net/http/http_cache_transaction.cc
+++ b/net/http/http_cache_transaction.cc
@@ -190,8 +190,8 @@
                     arraysize(kValidationHeaders),
                 "invalid number of validation headers");
 
-  io_callback_ = base::Bind(&Transaction::OnIOComplete,
-                              weak_factory_.GetWeakPtr());
+  io_callback_ = base::BindRepeating(&Transaction::OnIOComplete,
+                                     weak_factory_.GetWeakPtr());
 }
 
 HttpCache::Transaction::~Transaction() {
diff --git a/net/http/http_cache_transaction.h b/net/http/http_cache_transaction.h
index fe8ac18..3a67dfd 100644
--- a/net/http/http_cache_transaction.h
+++ b/net/http/http_cache_transaction.h
@@ -19,6 +19,7 @@
 #include "base/memory/weak_ptr.h"
 #include "base/time/time.h"
 #include "net/base/completion_callback.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/io_buffer.h"
 #include "net/base/ip_endpoint.h"
 #include "net/base/load_states.h"
@@ -609,7 +610,7 @@
   int read_offset_;
   int effective_load_flags_;
   std::unique_ptr<PartialData> partial_;  // We are dealing with range requests.
-  CompletionCallback io_callback_;
+  CompletionRepeatingCallback io_callback_;
 
   // Error code to be returned from a subsequent Read call if shared writing
   // failed in a separate transaction.
diff --git a/net/http/http_network_transaction.cc b/net/http/http_network_transaction.cc
index 9d96d1a8..1e67636 100644
--- a/net/http/http_network_transaction.cc
+++ b/net/http/http_network_transaction.cc
@@ -94,8 +94,8 @@
 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
                                                HttpNetworkSession* session)
     : pending_auth_target_(HttpAuth::AUTH_NONE),
-      io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
-                              base::Unretained(this))),
+      io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
+                                       base::Unretained(this))),
       session_(session),
       request_(NULL),
       priority_(priority),
diff --git a/net/http/http_network_transaction.h b/net/http/http_network_transaction.h
index 9f3e95e..796b289 100644
--- a/net/http/http_network_transaction.h
+++ b/net/http/http_network_transaction.h
@@ -15,6 +15,7 @@
 #include "base/memory/ref_counted.h"
 #include "base/time/time.h"
 #include "crypto/ec_private_key.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/net_error_details.h"
 #include "net/base/net_export.h"
 #include "net/base/request_priority.h"
@@ -307,7 +308,7 @@
   // cleared by RestartWithAuth().
   HttpAuth::Target pending_auth_target_;
 
-  CompletionCallback io_callback_;
+  CompletionRepeatingCallback io_callback_;
   CompletionCallback callback_;
 
   HttpNetworkSession* session_;
diff --git a/net/http/http_proxy_client_socket.cc b/net/http/http_proxy_client_socket.cc
index ec44a6cb..84b8dd8 100644
--- a/net/http/http_proxy_client_socket.cc
+++ b/net/http/http_proxy_client_socket.cc
@@ -37,8 +37,8 @@
     NextProto negotiated_protocol,
     bool is_https_proxy,
     const NetworkTrafficAnnotationTag& traffic_annotation)
-    : io_callback_(base::Bind(&HttpProxyClientSocket::OnIOComplete,
-                              base::Unretained(this))),
+    : io_callback_(base::BindRepeating(&HttpProxyClientSocket::OnIOComplete,
+                                       base::Unretained(this))),
       next_state_(STATE_NONE),
       transport_(std::move(transport_socket)),
       endpoint_(endpoint),
diff --git a/net/http/http_proxy_client_socket.h b/net/http/http_proxy_client_socket.h
index ac11966..a3be764 100644
--- a/net/http/http_proxy_client_socket.h
+++ b/net/http/http_proxy_client_socket.h
@@ -14,6 +14,7 @@
 #include "base/memory/ref_counted.h"
 #include "net/base/completion_callback.h"
 #include "net/base/completion_once_callback.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/host_port_pair.h"
 #include "net/base/load_timing_info.h"
 #include "net/base/net_export.h"
@@ -128,7 +129,7 @@
   int DoDrainBody();
   int DoDrainBodyComplete(int result);
 
-  CompletionCallback io_callback_;
+  CompletionRepeatingCallback io_callback_;
   State next_state_;
 
   // Stores the callback provided by the caller of async operations.
diff --git a/net/http/http_stream_factory_job.cc b/net/http/http_stream_factory_job.cc
index 16c40cd5..f1450ef 100644
--- a/net/http/http_stream_factory_job.cc
+++ b/net/http/http_stream_factory_job.cc
@@ -175,7 +175,8 @@
       proxy_ssl_config_(proxy_ssl_config),
       net_log_(
           NetLogWithSource::Make(net_log, NetLogSourceType::HTTP_STREAM_JOB)),
-      io_callback_(base::Bind(&Job::OnIOComplete, base::Unretained(this))),
+      io_callback_(
+          base::BindRepeating(&Job::OnIOComplete, base::Unretained(this))),
       connection_(new ClientSocketHandle),
       session_(session),
       next_state_(STATE_NONE),
diff --git a/net/http/http_stream_factory_job.h b/net/http/http_stream_factory_job.h
index f42b95f..31d6977 100644
--- a/net/http/http_stream_factory_job.h
+++ b/net/http/http_stream_factory_job.h
@@ -12,7 +12,7 @@
 #include "base/memory/ref_counted.h"
 #include "base/memory/weak_ptr.h"
 #include "base/time/time.h"
-#include "net/base/completion_callback.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/net_export.h"
 #include "net/base/proxy_server.h"
 #include "net/base/request_priority.h"
@@ -410,7 +410,7 @@
   SSLConfig proxy_ssl_config_;
   const NetLogWithSource net_log_;
 
-  const CompletionCallback io_callback_;
+  const CompletionRepeatingCallback io_callback_;
   std::unique_ptr<ClientSocketHandle> connection_;
   HttpNetworkSession* const session_;
 
diff --git a/net/http/http_stream_factory_job_controller.cc b/net/http/http_stream_factory_job_controller.cc
index dc1a8f2..3fb25a6 100644
--- a/net/http/http_stream_factory_job_controller.cc
+++ b/net/http/http_stream_factory_job_controller.cc
@@ -86,8 +86,6 @@
       can_start_alternative_proxy_job_(true),
       next_state_(STATE_RESOLVE_PROXY),
       proxy_resolve_request_(nullptr),
-      io_callback_(
-          base::Bind(&JobController::OnIOComplete, base::Unretained(this))),
       request_info_(request_info),
       server_ssl_config_(server_ssl_config),
       proxy_ssl_config_(proxy_ssl_config),
@@ -751,8 +749,10 @@
   HostPortPair destination(HostPortPair::FromURL(request_info_.url));
   GURL origin_url = ApplyHostMappingRules(request_info_.url, &destination);
 
+  CompletionCallback io_callback =
+      base::Bind(&JobController::OnIOComplete, base::Unretained(this));
   return session_->proxy_resolution_service()->ResolveProxy(
-      origin_url, request_info_.method, &proxy_info_, io_callback_,
+      origin_url, request_info_.method, &proxy_info_, io_callback,
       &proxy_resolve_request_, session_->context().proxy_delegate, net_log_);
 }
 
diff --git a/net/http/http_stream_factory_job_controller.h b/net/http/http_stream_factory_job_controller.h
index 29d05d1..81089ea 100644
--- a/net/http/http_stream_factory_job_controller.h
+++ b/net/http/http_stream_factory_job_controller.h
@@ -371,7 +371,6 @@
 
   State next_state_;
   ProxyResolutionService::Request* proxy_resolve_request_;
-  CompletionCallback io_callback_;
   const HttpRequestInfo request_info_;
   ProxyInfo proxy_info_;
   const SSLConfig server_ssl_config_;
diff --git a/net/http/http_stream_parser.cc b/net/http/http_stream_parser.cc
index ae01cf4..e71814d 100644
--- a/net/http/http_stream_parser.cc
+++ b/net/http/http_stream_parser.cc
@@ -211,8 +211,8 @@
       weak_ptr_factory_(this) {
   CHECK(connection_) << "ClientSocketHandle passed to HttpStreamParser must "
                         "not be NULL. See crbug.com/790776";
-  io_callback_ = base::Bind(&HttpStreamParser::OnIOComplete,
-                            weak_ptr_factory_.GetWeakPtr());
+  io_callback_ = base::BindRepeating(&HttpStreamParser::OnIOComplete,
+                                     weak_ptr_factory_.GetWeakPtr());
 }
 
 HttpStreamParser::~HttpStreamParser() = default;
diff --git a/net/http/http_stream_parser.h b/net/http/http_stream_parser.h
index 5763fd55..603db77 100644
--- a/net/http/http_stream_parser.h
+++ b/net/http/http_stream_parser.h
@@ -16,8 +16,8 @@
 #include "base/memory/weak_ptr.h"
 #include "base/strings/string_piece.h"
 #include "crypto/ec_private_key.h"
-#include "net/base/completion_callback.h"
 #include "net/base/completion_once_callback.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/net_errors.h"
 #include "net/base/net_export.h"
 #include "net/log/net_log_with_source.h"
@@ -274,7 +274,7 @@
   NetLogWithSource net_log_;
 
   // Callback to be used when doing IO.
-  CompletionCallback io_callback_;
+  CompletionRepeatingCallback io_callback_;
 
   // Buffer used to read the request body from UploadDataStream.
   scoped_refptr<SeekableIOBuffer> request_body_read_buf_;
diff --git a/net/socket/socks5_client_socket.cc b/net/socket/socks5_client_socket.cc
index 6b43687..d550055 100644
--- a/net/socket/socks5_client_socket.cc
+++ b/net/socket/socks5_client_socket.cc
@@ -34,8 +34,8 @@
     std::unique_ptr<ClientSocketHandle> transport_socket,
     const HostResolver::RequestInfo& req_info,
     const NetworkTrafficAnnotationTag& traffic_annotation)
-    : io_callback_(base::Bind(&SOCKS5ClientSocket::OnIOComplete,
-                              base::Unretained(this))),
+    : io_callback_(base::BindRepeating(&SOCKS5ClientSocket::OnIOComplete,
+                                       base::Unretained(this))),
       transport_(std::move(transport_socket)),
       next_state_(STATE_NONE),
       completed_handshake_(false),
diff --git a/net/socket/socks5_client_socket.h b/net/socket/socks5_client_socket.h
index 364ac53..5f64eaf 100644
--- a/net/socket/socks5_client_socket.h
+++ b/net/socket/socks5_client_socket.h
@@ -15,6 +15,7 @@
 #include "base/memory/ref_counted.h"
 #include "net/base/address_list.h"
 #include "net/base/completion_callback.h"
+#include "net/base/completion_repeating_callback.h"
 #include "net/base/net_errors.h"
 #include "net/base/net_export.h"
 #include "net/dns/host_resolver.h"
@@ -124,7 +125,7 @@
   // and return OK on success.
   int BuildHandshakeWriteBuffer(std::string* handshake) const;
 
-  CompletionCallback io_callback_;
+  CompletionRepeatingCallback io_callback_;
 
   // Stores the underlying socket.
   std::unique_ptr<ClientSocketHandle> transport_;