Commit 60e45539 authored by Paul Jensen's avatar Paul Jensen Committed by Commit Bot

Plumb SocketTag from URLRequest to HttpRequestInfo

Bug: 520198
Change-Id: I4313f295c704d852c98feadc009e5720c0aea5a9
Reviewed-on: https://chromium-review.googlesource.com/893804
Commit-Queue: Paul Jensen <pauljensen@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537660}
parent 04972bf5
......@@ -1219,6 +1219,12 @@ void URLRequest::SetResponseHeadersCallback(ResponseHeadersCallback callback) {
response_headers_callback_ = std::move(callback);
}
void URLRequest::set_socket_tag(const SocketTag& socket_tag) {
DCHECK(!is_pending_);
DCHECK(url().SchemeIsHTTPOrHTTPS());
socket_tag_ = socket_tag;
}
void URLRequest::set_status(URLRequestStatus status) {
DCHECK(status_.is_io_pending() || status_.is_success() ||
(!status.is_success() && !status.is_io_pending()));
......
......@@ -37,6 +37,7 @@
#include "net/log/net_log_with_source.h"
#include "net/net_features.h"
#include "net/socket/connection_attempts.h"
#include "net/socket/socket_tag.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h"
......@@ -693,6 +694,17 @@ class NET_EXPORT URLRequest : public base::SupportsUserData {
// called with a response from the server.
void SetResponseHeadersCallback(ResponseHeadersCallback callback);
// Sets socket tag to be applied to all sockets used to execute this request.
// Must be set before Start() is called. Only currently supported for HTTP
// and HTTPS requests on Android; UID tagging requires
// MODIFY_NETWORK_ACCOUNTING permission.
// NOTE(pauljensen): Setting a tag disallows sharing of sockets with requests
// with other tags, which may adversely effect performance by prohibiting
// connection sharing. In other words use of multiplexed sockets (e.g. HTTP/2
// and QUIC) will only be allowed if all requests have the same socket tag.
void set_socket_tag(const SocketTag& socket_tag);
const SocketTag& socket_tag() const { return socket_tag_; }
protected:
// Allow the URLRequestJob class to control the is_pending() flag.
void set_is_pending(bool value) { is_pending_ = value; }
......@@ -904,6 +916,8 @@ class NET_EXPORT URLRequest : public base::SupportsUserData {
const NetworkTrafficAnnotationTag traffic_annotation_;
SocketTag socket_tag_;
// See Set{Request|Response}HeadersCallback() above for details.
RequestHeadersCallback request_headers_callback_;
ResponseHeadersCallback response_headers_callback_;
......
......@@ -350,6 +350,7 @@ void URLRequestHttpJob::Start() {
request_info_.load_flags = request_->load_flags();
request_info_.traffic_annotation =
net::MutableNetworkTrafficAnnotationTag(request_->traffic_annotation());
request_info_.socket_tag = request_->socket_tag();
// Enable privacy mode if cookie settings or flags tell us not send or
// save cookies.
......
......@@ -91,6 +91,9 @@ URLRequestJob::URLRequestJob(URLRequest* request,
last_notified_total_received_bytes_(0),
last_notified_total_sent_bytes_(0),
weak_factory_(this) {
// Socket tagging only supported for HTTP/HTTPS.
DCHECK(request == nullptr || SocketTag() == request->socket_tag() ||
request->url().SchemeIsHTTPOrHTTPS());
base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
if (power_monitor)
power_monitor->AddObserver(this);
......
......@@ -12213,4 +12213,42 @@ TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) {
EXPECT_FALSE(r->is_pending());
}
// Test that URLRequests get properly tagged.
#if defined(OS_ANDROID)
TEST_F(URLRequestTestHTTP, TestTagging) {
ASSERT_TRUE(http_test_server()->Start());
// The tag under which the system reports untagged traffic.
static const int32_t UNTAGGED_TAG = 0;
uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG);
// Untagged traffic should be tagged with tag UNTAGGED_TAG.
TestDelegate delegate;
std::unique_ptr<URLRequest> req(default_context_.CreateRequest(
http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate,
TRAFFIC_ANNOTATION_FOR_TESTS));
EXPECT_EQ(SocketTag(), req->socket_tag());
req->Start();
base::RunLoop().Run();
EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic);
int32_t tag_val1 = 0x12345678;
SocketTag tag1(SocketTag::UNSET_UID, tag_val1);
old_traffic = GetTaggedBytes(tag_val1);
// Test specific tag value.
req = default_context_.CreateRequest(http_test_server()->GetURL("/"),
DEFAULT_PRIORITY, &delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
req->set_socket_tag(tag1);
EXPECT_EQ(tag1, req->socket_tag());
req->Start();
base::RunLoop().Run();
EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
}
#endif
} // namespace net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment