summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsclittle <sclittle@chromium.org>2015-09-28 14:40:52 -0700
committerCommit bot <commit-bot@chromium.org>2015-09-28 21:41:45 +0000
commit28d558b8eb033054feddec53cfa667ac4652de0d (patch)
tree0adf0cbf44d5b2b96e91be02a5c5b8b753161d42
parent82b329e8cdbac775a5257989fd335d3e4641d713 (diff)
downloadchromium_src-28d558b8eb033054feddec53cfa667ac4652de0d.zip
chromium_src-28d558b8eb033054feddec53cfa667ac4652de0d.tar.gz
chromium_src-28d558b8eb033054feddec53cfa667ac4652de0d.tar.bz2
Notify NetworkDelegate when bytes have been sent over the network.
This CL adds and implements NetworkDelegate::OnNetworkBytesSent(), which is used to notify the network delegate when bytes have been sent over the network. BUG=518051 Review URL: https://codereview.chromium.org/1362793003 Cr-Commit-Position: refs/heads/master@{#351165}
-rw-r--r--net/base/layered_network_delegate.cc10
-rw-r--r--net/base/layered_network_delegate.h4
-rw-r--r--net/base/layered_network_delegate_unittest.cc12
-rw-r--r--net/base/network_delegate.cc7
-rw-r--r--net/base/network_delegate.h11
-rw-r--r--net/base/network_delegate_impl.cc3
-rw-r--r--net/base/network_delegate_impl.h10
-rw-r--r--net/url_request/url_request_http_job_unittest.cc13
-rw-r--r--net/url_request/url_request_job.cc11
-rw-r--r--net/url_request/url_request_job.h12
-rw-r--r--net/url_request/url_request_test_util.cc7
-rw-r--r--net/url_request/url_request_test_util.h4
12 files changed, 100 insertions, 4 deletions
diff --git a/net/base/layered_network_delegate.cc b/net/base/layered_network_delegate.cc
index f84b468..d384d5ea 100644
--- a/net/base/layered_network_delegate.cc
+++ b/net/base/layered_network_delegate.cc
@@ -148,6 +148,16 @@ void LayeredNetworkDelegate::OnNetworkBytesReceivedInternal(
const URLRequest& request,
int64_t bytes_received) {}
+void LayeredNetworkDelegate::OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) {
+ OnNetworkBytesSentInternal(request, bytes_sent);
+ nested_network_delegate_->NotifyNetworkBytesSent(request, bytes_sent);
+}
+
+void LayeredNetworkDelegate::OnNetworkBytesSentInternal(
+ const URLRequest& request,
+ int64_t bytes_sent) {}
+
void LayeredNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
OnCompletedInternal(request, started);
nested_network_delegate_->NotifyCompleted(request, started);
diff --git a/net/base/layered_network_delegate.h b/net/base/layered_network_delegate.h
index f5899e9..dabe681 100644
--- a/net/base/layered_network_delegate.h
+++ b/net/base/layered_network_delegate.h
@@ -66,6 +66,7 @@ class NET_EXPORT LayeredNetworkDelegate : public NetworkDelegate {
void OnResponseStarted(URLRequest* request) final;
void OnNetworkBytesReceived(const URLRequest& request,
int64_t bytes_received) final;
+ void OnNetworkBytesSent(const URLRequest& request, int64_t bytes_sent) final;
void OnCompleted(URLRequest* request, bool started) final;
void OnURLRequestDestroyed(URLRequest* request) final;
void OnURLRequestJobOrphaned(URLRequest* request) final;
@@ -128,6 +129,9 @@ class NET_EXPORT LayeredNetworkDelegate : public NetworkDelegate {
virtual void OnNetworkBytesReceivedInternal(const URLRequest& request,
int64_t bytes_received);
+ virtual void OnNetworkBytesSentInternal(const URLRequest& request,
+ int64_t bytes_sent);
+
virtual void OnCompletedInternal(URLRequest* request, bool started);
virtual void OnURLRequestDestroyedInternal(URLRequest* request);
diff --git a/net/base/layered_network_delegate_unittest.cc b/net/base/layered_network_delegate_unittest.cc
index a74a2b6..bfdbe7b 100644
--- a/net/base/layered_network_delegate_unittest.cc
+++ b/net/base/layered_network_delegate_unittest.cc
@@ -98,6 +98,11 @@ class TestNetworkDelegateImpl : public NetworkDelegateImpl {
IncrementAndCompareCounter("on_network_bytes_received_count");
}
+ void OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) override {
+ IncrementAndCompareCounter("on_network_bytes_sent_count");
+ }
+
void OnCompleted(URLRequest* request, bool started) override {
IncrementAndCompareCounter("on_completed_count");
}
@@ -197,6 +202,7 @@ class TestLayeredNetworkDelegate : public LayeredNetworkDelegate {
request_headers.get()));
OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get());
OnSendHeaders(NULL, *request_headers);
+ OnNetworkBytesSent(*request, 42);
EXPECT_EQ(OK, OnHeadersReceived(NULL, completion_callback.callback(),
response_headers.get(), NULL, NULL));
OnResponseStarted(request.get());
@@ -284,6 +290,12 @@ class TestLayeredNetworkDelegate : public LayeredNetworkDelegate {
EXPECT_EQ(1, (*counters_)["on_network_bytes_received_count"]);
}
+ void OnNetworkBytesSentInternal(const URLRequest& request,
+ int64_t bytes_sent) override {
+ ++(*counters_)["on_network_bytes_sent_count"];
+ EXPECT_EQ(1, (*counters_)["on_network_bytes_sent_count"]);
+ }
+
void OnCompletedInternal(URLRequest* request, bool started) override {
++(*counters_)["on_completed_count"];
EXPECT_EQ(1, (*counters_)["on_completed_count"]);
diff --git a/net/base/network_delegate.cc b/net/base/network_delegate.cc
index 745336f..1538028 100644
--- a/net/base/network_delegate.cc
+++ b/net/base/network_delegate.cc
@@ -96,6 +96,13 @@ void NetworkDelegate::NotifyNetworkBytesReceived(const URLRequest& request,
OnNetworkBytesReceived(request, bytes_received);
}
+void NetworkDelegate::NotifyNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) {
+ DCHECK(CalledOnValidThread());
+ DCHECK_GT(bytes_sent, 0);
+ OnNetworkBytesSent(request, bytes_sent);
+}
+
void NetworkDelegate::NotifyBeforeRedirect(URLRequest* request,
const GURL& new_location) {
DCHECK(CalledOnValidThread());
diff --git a/net/base/network_delegate.h b/net/base/network_delegate.h
index 3a29e26..cad221e 100644
--- a/net/base/network_delegate.h
+++ b/net/base/network_delegate.h
@@ -89,6 +89,7 @@ class NET_EXPORT NetworkDelegate : public base::NonThreadSafe {
void NotifyResponseStarted(URLRequest* request);
void NotifyNetworkBytesReceived(const URLRequest& request,
int64_t bytes_received);
+ void NotifyNetworkBytesSent(const URLRequest& request, int64_t bytes_sent);
void NotifyCompleted(URLRequest* request, bool started);
void NotifyURLRequestDestroyed(URLRequest* request);
void NotifyURLRequestJobOrphaned(URLRequest* request);
@@ -213,6 +214,16 @@ class NET_EXPORT NetworkDelegate : public base::NonThreadSafe {
virtual void OnNetworkBytesReceived(const URLRequest& request,
int64_t bytes_received) = 0;
+ // Called when bytes are sent over the network, such as when sending request
+ // headers or uploading request body bytes. This includes localhost requests.
+ // |bytes_sent| is the number of bytes measured at the application layer that
+ // have been sent over the network for this request since the last time
+ // OnNetworkBytesSent was called. |bytes_sent| will always be greater than 0.
+ // Currently, this is only implemented for HTTP transactions, and |bytes_sent|
+ // does not include TLS overhead or TCP retransmits.
+ virtual void OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) = 0;
+
// Indicates that the URL request has been completed or failed.
// |started| indicates whether the request has been started. If false,
// some information like the socket address is not available.
diff --git a/net/base/network_delegate_impl.cc b/net/base/network_delegate_impl.cc
index 3cc8c21..af4d708 100644
--- a/net/base/network_delegate_impl.cc
+++ b/net/base/network_delegate_impl.cc
@@ -59,6 +59,9 @@ void NetworkDelegateImpl::OnResponseStarted(URLRequest* request) {
void NetworkDelegateImpl::OnNetworkBytesReceived(const URLRequest& request,
int64_t bytes_received) {}
+void NetworkDelegateImpl::OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) {}
+
void NetworkDelegateImpl::OnCompleted(URLRequest* request, bool started) {
}
diff --git a/net/base/network_delegate_impl.h b/net/base/network_delegate_impl.h
index 84e898d..b0345dd 100644
--- a/net/base/network_delegate_impl.h
+++ b/net/base/network_delegate_impl.h
@@ -128,6 +128,16 @@ class NET_EXPORT NetworkDelegateImpl : public NetworkDelegate {
void OnNetworkBytesReceived(const URLRequest& request,
int64_t bytes_received) override;
+ // Called when bytes are sent over the network, such as when sending request
+ // headers or uploading request body bytes. This includes localhost requests.
+ // |bytes_sent| is the number of bytes measured at the application layer that
+ // have been sent over the network for this request since the last time
+ // OnNetworkBytesSent was called. |bytes_sent| will always be greater than 0.
+ // Currently, this is only implemented for HTTP transactions, and |bytes_sent|
+ // does not include TLS overhead or TCP retransmits.
+ void OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) override;
+
// Indicates that the URL request has been completed or failed.
// |started| indicates whether the request has been started. If false,
// some information like the socket address is not available.
diff --git a/net/url_request/url_request_http_job_unittest.cc b/net/url_request/url_request_http_job_unittest.cc
index 43b7e2b..3ad193c 100644
--- a/net/url_request/url_request_http_job_unittest.cc
+++ b/net/url_request/url_request_http_job_unittest.cc
@@ -149,6 +149,8 @@ TEST_F(URLRequestHttpJobWithMockSocketsTest,
request->GetTotalSentBytes());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
request->GetTotalReceivedBytes());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ network_delegate_.total_network_bytes_sent());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
network_delegate_.total_network_bytes_received());
}
@@ -178,6 +180,8 @@ TEST_F(URLRequestHttpJobWithMockSocketsTest,
request->GetTotalSentBytes());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
request->GetTotalReceivedBytes());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ network_delegate_.total_network_bytes_sent());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
network_delegate_.total_network_bytes_received());
}
@@ -209,6 +213,8 @@ TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthFailedRequest) {
request->GetTotalSentBytes());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
request->GetTotalReceivedBytes());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ network_delegate_.total_network_bytes_sent());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
network_delegate_.total_network_bytes_received());
}
@@ -241,6 +247,8 @@ TEST_F(URLRequestHttpJobWithMockSocketsTest,
request->GetTotalSentBytes());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
request->GetTotalReceivedBytes());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ network_delegate_.total_network_bytes_sent());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
network_delegate_.total_network_bytes_received());
}
@@ -291,6 +299,9 @@ TEST_F(URLRequestHttpJobWithMockSocketsTest,
EXPECT_EQ(CountReadBytes(final_reads, arraysize(final_reads)),
request->GetTotalReceivedBytes());
// Should include the redirect as well as the final response.
+ EXPECT_EQ(CountWriteBytes(redirect_writes, arraysize(redirect_writes)) +
+ CountWriteBytes(final_writes, arraysize(final_writes)),
+ network_delegate_.total_network_bytes_sent());
EXPECT_EQ(CountReadBytes(redirect_reads, arraysize(redirect_reads)) +
CountReadBytes(final_reads, arraysize(final_reads)),
network_delegate_.total_network_bytes_received());
@@ -320,6 +331,8 @@ TEST_F(URLRequestHttpJobWithMockSocketsTest,
request->GetTotalSentBytes());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
request->GetTotalReceivedBytes());
+ EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)),
+ network_delegate_.total_network_bytes_sent());
EXPECT_EQ(CountReadBytes(reads, arraysize(reads)),
network_delegate_.total_network_bytes_received());
}
diff --git a/net/url_request/url_request_job.cc b/net/url_request/url_request_job.cc
index 5adda9a..fd6ed75 100644
--- a/net/url_request/url_request_job.cc
+++ b/net/url_request/url_request_job.cc
@@ -73,6 +73,7 @@ URLRequestJob::URLRequestJob(URLRequest* request,
expected_content_size_(-1),
network_delegate_(network_delegate),
last_notified_total_received_bytes_(0),
+ last_notified_total_sent_bytes_(0),
weak_factory_(this) {
base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
if (power_monitor)
@@ -948,6 +949,7 @@ void URLRequestJob::MaybeNotifyNetworkBytes() {
if (!request_ || !network_delegate_)
return;
+ // Report any new received bytes.
int64_t total_received_bytes = GetTotalReceivedBytes();
DCHECK_GE(total_received_bytes, last_notified_total_received_bytes_);
if (total_received_bytes > last_notified_total_received_bytes_) {
@@ -955,6 +957,15 @@ void URLRequestJob::MaybeNotifyNetworkBytes() {
*request_, total_received_bytes - last_notified_total_received_bytes_);
}
last_notified_total_received_bytes_ = total_received_bytes;
+
+ // Report any new sent bytes.
+ int64_t total_sent_bytes = GetTotalSentBytes();
+ DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_);
+ if (total_sent_bytes > last_notified_total_sent_bytes_) {
+ network_delegate_->NotifyNetworkBytesSent(
+ *request_, total_sent_bytes - last_notified_total_sent_bytes_);
+ }
+ last_notified_total_sent_bytes_ = total_sent_bytes;
}
} // namespace net
diff --git a/net/url_request/url_request_job.h b/net/url_request/url_request_job.h
index 8524bf0..0b96bfc 100644
--- a/net/url_request/url_request_job.h
+++ b/net/url_request/url_request_job.h
@@ -402,10 +402,9 @@ class NET_EXPORT URLRequestJob
// |location| and |http_status_code|.
RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code);
- // Notify the network delegate that more bytes have been received over the
- // network, if bytes have been received since the previous notification.
- // TODO(sclittle): Have this method also notify about sent bytes once
- // URLRequestJob::GetTotalSentBytes has been implemented (crbug.com/518897).
+ // Notify the network delegate that more bytes have been received or sent over
+ // the network, if bytes have been received or sent since the previous
+ // notification.
void MaybeNotifyNetworkBytes();
// Indicates that the job is done producing data, either it has completed
@@ -452,6 +451,11 @@ class NET_EXPORT URLRequestJob
// newly received since the last notification.
int64_t last_notified_total_received_bytes_;
+ // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes()
+ // was called. Used to calculate how bytes have been newly sent since the last
+ // notification.
+ int64_t last_notified_total_sent_bytes_;
+
base::WeakPtrFactory<URLRequestJob> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequestJob);
diff --git a/net/url_request/url_request_test_util.cc b/net/url_request/url_request_test_util.cc
index af8d139..94086df 100644
--- a/net/url_request/url_request_test_util.cc
+++ b/net/url_request/url_request_test_util.cc
@@ -330,6 +330,7 @@ TestNetworkDelegate::TestNetworkDelegate()
before_send_headers_count_(0),
headers_received_count_(0),
total_network_bytes_received_(0),
+ total_network_bytes_sent_(0),
has_load_timing_info_before_redirect_(false),
has_load_timing_info_before_auth_(false),
can_access_files_(true),
@@ -512,6 +513,12 @@ void TestNetworkDelegate::OnNetworkBytesReceived(const URLRequest& request,
total_network_bytes_received_ += bytes_received;
}
+void TestNetworkDelegate::OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) {
+ event_order_[request.identifier()] += "OnNetworkBytesSent\n";
+ total_network_bytes_sent_ += bytes_sent;
+}
+
void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
int req_id = request->identifier();
InitRequestStatesIfNew(req_id);
diff --git a/net/url_request/url_request_test_util.h b/net/url_request/url_request_test_util.h
index 924635e..09d44df 100644
--- a/net/url_request/url_request_test_util.h
+++ b/net/url_request/url_request_test_util.h
@@ -289,6 +289,7 @@ class TestNetworkDelegate : public NetworkDelegateImpl {
int64_t total_network_bytes_received() const {
return total_network_bytes_received_;
}
+ int64_t total_network_bytes_sent() const { return total_network_bytes_sent_; }
// Last observed proxy in proxy header sent callback.
HostPortPair last_observed_proxy() {
@@ -322,6 +323,8 @@ class TestNetworkDelegate : public NetworkDelegateImpl {
void OnResponseStarted(URLRequest* request) override;
void OnNetworkBytesReceived(const URLRequest& request,
int64_t bytes_received) override;
+ void OnNetworkBytesSent(const URLRequest& request,
+ int64_t bytes_sent) override;
void OnCompleted(URLRequest* request, bool started) override;
void OnURLRequestDestroyed(URLRequest* request) override;
void OnPACScriptError(int line_number, const base::string16& error) override;
@@ -363,6 +366,7 @@ class TestNetworkDelegate : public NetworkDelegateImpl {
int before_send_headers_count_;
int headers_received_count_;
int64_t total_network_bytes_received_;
+ int64_t total_network_bytes_sent_;
// Last observed proxy in before proxy header sent callback.
HostPortPair last_observed_proxy_;