Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1120)

Unified Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc

Issue 791493015: Adding q=low to the Chrome-Proxy request header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed bengr comments Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc
similarity index 62%
rename from components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.cc
rename to components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc
index b4bad4fcee32e16f121f3c2a25ef6b9062c5ef41..869d08356a51eca3de43a161b098f60036201b0c 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc
@@ -2,12 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
+#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h"
+
+#include <vector>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
@@ -28,6 +31,15 @@
namespace data_reduction_proxy {
+namespace {
+const std::string kSession = "ps";
+const std::string kCredentials = "sid";
+const std::string kBuildNumber = "b";
+const std::string kPatchNumber = "p";
+const std::string kClient = "c";
+const std::string kLoFi = "q";
+}
+
// The empty version for the authentication protocol. Currently used by
// Android webview.
#if defined(OS_ANDROID)
@@ -46,37 +58,40 @@ const char* GetString(Client client) {
#undef CLIENT_ENUM
// static
-bool DataReductionProxyAuthRequestHandler::IsKeySetOnCommandLine() {
+bool DataReductionProxyRequestOptions::IsKeySetOnCommandLine() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
return command_line.HasSwitch(
data_reduction_proxy::switches::kDataReductionProxyKey);
}
-DataReductionProxyAuthRequestHandler::DataReductionProxyAuthRequestHandler(
+DataReductionProxyRequestOptions::DataReductionProxyRequestOptions(
Client client,
DataReductionProxyParams* params,
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
- : client_(GetString(client)),
- data_reduction_proxy_params_(params),
+ : data_reduction_proxy_params_(params),
network_task_runner_(network_task_runner) {
- GetChromiumBuildAndPatch(ChromiumVersion(), &build_number_, &patch_number_);
Init();
+ UpdateLoFi();
+ UpdateVersion(client, ChromiumVersion());
}
-DataReductionProxyAuthRequestHandler::DataReductionProxyAuthRequestHandler(
+DataReductionProxyRequestOptions::DataReductionProxyRequestOptions(
Client client,
const std::string& version,
DataReductionProxyParams* params,
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
- : client_(GetString(client)),
- data_reduction_proxy_params_(params),
+ : data_reduction_proxy_params_(params),
network_task_runner_(network_task_runner) {
- GetChromiumBuildAndPatch(version, &build_number_, &patch_number_);
Init();
+ UpdateLoFi();
+ UpdateVersion(client, version);
+}
+
+DataReductionProxyRequestOptions::~DataReductionProxyRequestOptions() {
}
-std::string DataReductionProxyAuthRequestHandler::ChromiumVersion() const {
+std::string DataReductionProxyRequestOptions::ChromiumVersion() const {
#if defined(PRODUCT_VERSION)
return PRODUCT_VERSION;
#else
@@ -84,8 +99,7 @@ std::string DataReductionProxyAuthRequestHandler::ChromiumVersion() const {
#endif
}
-
-void DataReductionProxyAuthRequestHandler::GetChromiumBuildAndPatch(
+void DataReductionProxyRequestOptions::GetChromiumBuildAndPatch(
const std::string& version,
std::string* build,
std::string* patch) const {
@@ -97,16 +111,43 @@ void DataReductionProxyAuthRequestHandler::GetChromiumBuildAndPatch(
*patch = version_parts[3];
}
-void DataReductionProxyAuthRequestHandler::Init() {
- InitAuthentication(GetDefaultKey());
+void DataReductionProxyRequestOptions::UpdateVersion(
+ const Client& client, const std::string& version) {
+ std::string build_number;
+ std::string patch_number;
+ GetChromiumBuildAndPatch(version, &build_number, &patch_number);
+ if (!build_number.empty() && !patch_number.empty()) {
+ header_options_[kBuildNumber] = build_number;
+ header_options_[kPatchNumber] = patch_number;
+ }
+ std::string client_string = GetString(client);
+ if (!client_string.empty())
+ header_options_[kClient] = client_string;
+ RegenerateRequestHeaderValue();
}
+void DataReductionProxyRequestOptions::UpdateLoFi() {
+ // LoFi was not enabled, but now is. Add the header option.
+ if (header_options_.find(kLoFi) == header_options_.end() &&
+ DataReductionProxyParams::IsDataReductionProxyLoFiEnabled()) {
+ header_options_[kLoFi] = "low";
+ RegenerateRequestHeaderValue();
+ return;
+ }
+ // LoFi was enabled, but no longer is. Remove the header option.
+ if (header_options_.find(kLoFi) != header_options_.end() &&
+ !DataReductionProxyParams::IsDataReductionProxyLoFiEnabled()) {
+ header_options_.erase(kLoFi);
+ RegenerateRequestHeaderValue();
+ }
+}
-DataReductionProxyAuthRequestHandler::~DataReductionProxyAuthRequestHandler() {
+void DataReductionProxyRequestOptions::Init() {
+ InitAuthentication(GetDefaultKey());
}
// static
-base::string16 DataReductionProxyAuthRequestHandler::AuthHashForSalt(
+base::string16 DataReductionProxyRequestOptions::AuthHashForSalt(
int64 salt,
const std::string& key) {
std::string salted_key =
@@ -119,16 +160,15 @@ base::string16 DataReductionProxyAuthRequestHandler::AuthHashForSalt(
-base::Time DataReductionProxyAuthRequestHandler::Now() const {
+base::Time DataReductionProxyRequestOptions::Now() const {
return base::Time::Now();
}
-void DataReductionProxyAuthRequestHandler::RandBytes(
- void* output, size_t length) {
+void DataReductionProxyRequestOptions::RandBytes(void* output, size_t length) {
crypto::RandBytes(output, length);
}
-void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeader(
+void DataReductionProxyRequestOptions::MaybeAddRequestHeader(
net::URLRequest* request,
const net::ProxyServer& proxy_server,
net::HttpRequestHeaders* request_headers) {
@@ -142,20 +182,22 @@ void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeader(
request_headers);
}
-void DataReductionProxyAuthRequestHandler::MaybeAddProxyTunnelRequestHandler(
+void DataReductionProxyRequestOptions::MaybeAddProxyTunnelRequestHandler(
const net::HostPortPair& proxy_server,
net::HttpRequestHeaders* request_headers) {
DCHECK(network_task_runner_->BelongsToCurrentThread());
MaybeAddRequestHeaderImpl(proxy_server, true, request_headers);
}
-void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader(
+void DataReductionProxyRequestOptions::SetHeader(
net::HttpRequestHeaders* headers) {
base::Time now = Now();
+ // Authorization credentials must be regenerated at least every 24 hours.
if (now - last_update_time_ > base::TimeDelta::FromHours(24)) {
last_update_time_ = now;
- ComputeCredentials(last_update_time_, &session_, &credentials_);
+ UpdateCredentials();
}
+ UpdateLoFi();
const char kChromeProxyHeader[] = "Chrome-Proxy";
std::string header_value;
if (headers->HasHeader(kChromeProxyHeader)) {
@@ -163,16 +205,11 @@ void DataReductionProxyAuthRequestHandler::AddAuthorizationHeader(
headers->RemoveHeader(kChromeProxyHeader);
header_value += ", ";
}
- header_value +=
- "ps=" + session_ + ", sid=" + credentials_;
- if (!build_number_.empty() && !patch_number_.empty())
- header_value += ", b=" + build_number_ + ", p=" + patch_number_;
- if (!client_.empty())
- header_value += ", c=" + client_;
+ header_value += header_value_;
headers->SetHeader(kChromeProxyHeader, header_value);
}
-void DataReductionProxyAuthRequestHandler::ComputeCredentials(
+void DataReductionProxyRequestOptions::ComputeCredentials(
const base::Time& now,
std::string* session,
std::string* credentials) {
@@ -194,12 +231,21 @@ void DataReductionProxyAuthRequestHandler::ComputeCredentials(
<< "password: [" << *credentials << "]";
}
-void DataReductionProxyAuthRequestHandler::InitAuthentication(
+void DataReductionProxyRequestOptions::UpdateCredentials() {
+ std::string session;
+ std::string credentials;
+ ComputeCredentials(last_update_time_, &session, &credentials);
+ header_options_[kSession] = session;
+ header_options_[kCredentials] = credentials;
+ RegenerateRequestHeaderValue();
+}
+
+void DataReductionProxyRequestOptions::InitAuthentication(
const std::string& key) {
if (!network_task_runner_->BelongsToCurrentThread()) {
network_task_runner_->PostTask(
FROM_HERE,
- base::Bind(&DataReductionProxyAuthRequestHandler::InitAuthentication,
+ base::Bind(&DataReductionProxyRequestOptions::InitAuthentication,
base::Unretained(this),
key));
return;
@@ -210,10 +256,10 @@ void DataReductionProxyAuthRequestHandler::InitAuthentication(
key_ = key;
last_update_time_ = Now();
- ComputeCredentials(last_update_time_, &session_, &credentials_);
+ UpdateCredentials();
}
-std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const {
+std::string DataReductionProxyRequestOptions::GetDefaultKey() const {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
std::string key =
@@ -233,7 +279,7 @@ std::string DataReductionProxyAuthRequestHandler::GetDefaultKey() const {
return key;
}
-void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeaderImpl(
+void DataReductionProxyRequestOptions::MaybeAddRequestHeaderImpl(
const net::HostPortPair& proxy_server,
bool expect_ssl,
net::HttpRequestHeaders* request_headers) {
@@ -244,8 +290,17 @@ void DataReductionProxyAuthRequestHandler::MaybeAddRequestHeaderImpl(
net::HostPortPair::FromURL(
data_reduction_proxy_params_->ssl_origin()).Equals(
proxy_server) == expect_ssl) {
- AddAuthorizationHeader(request_headers);
+ SetHeader(request_headers);
+ }
+}
+
+void DataReductionProxyRequestOptions::RegenerateRequestHeaderValue() {
+ std::vector <std::string> options;
+ for (std::map<std::string, std::string>::iterator
+ it = header_options_.begin(); it != header_options_.end(); ++it) {
+ options.push_back(it->first + "=" + it->second);
}
+ header_value_ = JoinString(options, ", ");
}
} // namespace data_reduction_proxy

Powered by Google App Engine
This is Rietveld 408576698