From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 28 Aug 2019 12:21:25 -0700 Subject: expose SetUserAgent on NetworkContext Applying https://chromium-review.googlesource.com/c/chromium/src/+/1585083 before it's merged. There may end up being a better way of doing this, or the patch may be merged upstream, at which point this patch should be removed. diff --git a/net/url_request/static_http_user_agent_settings.h b/net/url_request/static_http_user_agent_settings.h index 0ab8187b0db8ae6db46d81738f653a2bc4c566f6..de3d55e85c22317f7f9375eb94d0d5d41b0dbd3b 100644 --- a/net/url_request/static_http_user_agent_settings.h +++ b/net/url_request/static_http_user_agent_settings.h @@ -30,13 +30,17 @@ class NET_EXPORT StaticHttpUserAgentSettings : public HttpUserAgentSettings { accept_language_ = new_accept_language; } + void set_user_agent(const std::string& new_user_agent) { + user_agent_ = new_user_agent; + } + // HttpUserAgentSettings implementation std::string GetAcceptLanguage() const override; std::string GetUserAgent() const override; private: std::string accept_language_; - const std::string user_agent_; + std::string user_agent_; }; } // namespace net diff --git a/services/network/network_context.cc b/services/network/network_context.cc index 4e0ae6719069da886db193642df9f90d52a332d3..0473bd61eae4afc903c3cbc55ec4bffcd3485d3d 100644 --- a/services/network/network_context.cc +++ b/services/network/network_context.cc @@ -1389,6 +1389,13 @@ void NetworkContext::SetNetworkConditions( std::move(network_conditions)); } +void NetworkContext::SetUserAgent(const std::string& new_user_agent) { + // This may only be called on NetworkContexts created with a constructor that + // calls ApplyContextParamsToBuilder. + DCHECK(user_agent_settings_); + user_agent_settings_->set_user_agent(new_user_agent); +} + void NetworkContext::SetAcceptLanguage(const std::string& new_accept_language) { // This may only be called on NetworkContexts created with the constructor // that calls MakeURLRequestContext(). diff --git a/services/network/network_context.h b/services/network/network_context.h index 8b41b28c036837a35a732a63aa6e1ebeaf416970..3f132d81a36276aa6b6c9455eb8f26ecfa156286 100644 --- a/services/network/network_context.h +++ b/services/network/network_context.h @@ -301,6 +301,7 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkContext void CloseIdleConnections(CloseIdleConnectionsCallback callback) override; void SetNetworkConditions(const base::UnguessableToken& throttling_profile_id, mojom::NetworkConditionsPtr conditions) override; + void SetUserAgent(const std::string& new_user_agent) override; void SetAcceptLanguage(const std::string& new_accept_language) override; void SetEnableReferrers(bool enable_referrers) override; void SetEnablePreconnect(bool enable_preconnect) override; diff --git a/services/network/public/mojom/network_context.mojom b/services/network/public/mojom/network_context.mojom index 28745d0a28062fba30f31342c430524aab6a2a4a..bd15f0c63b7c435910ca10e72d5311a5ae3f923c 100644 --- a/services/network/public/mojom/network_context.mojom +++ b/services/network/public/mojom/network_context.mojom @@ -1108,6 +1108,9 @@ interface NetworkContext { SetNetworkConditions(mojo_base.mojom.UnguessableToken throttling_profile_id, NetworkConditions? conditions); + // Updates the user agent to be used for requests. + SetUserAgent(string new_user_agent); + // Updates the Accept-Language header to be used for requests. SetAcceptLanguage(string new_accept_language); diff --git a/services/network/test/test_network_context.h b/services/network/test/test_network_context.h index 9e757600c4a9e4cac9bfafc7393b20188efc2a5a..dafff85abaa9776de271822ff13da2bdbdce7a0d 100644 --- a/services/network/test/test_network_context.h +++ b/services/network/test/test_network_context.h @@ -136,6 +136,7 @@ class TestNetworkContext : public mojom::NetworkContext { void CloseIdleConnections(CloseIdleConnectionsCallback callback) override {} void SetNetworkConditions(const base::UnguessableToken& throttling_profile_id, mojom::NetworkConditionsPtr conditions) override {} + void SetUserAgent(const std::string& new_user_agent) override {} void SetAcceptLanguage(const std::string& new_accept_language) override {} void SetEnableReferrers(bool enable_referrers) override {} void SetEnablePreconnect(bool enable_preconnect) override {}