Chromium Code Reviews| 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_request_options.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc |
| index ca7c7e92106d7ccd740e37c1cf1a2865444f9657..5c65d708f0c36a9bf2c62985a9a8f5bee7451551 100644 |
| --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc |
| +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.cc |
| @@ -4,13 +4,10 @@ |
| #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h" |
| -#include <algorithm> |
| -#include <map> |
| -#include <vector> |
| - |
| #include "base/bind.h" |
| #include "base/command_line.h" |
| #include "base/single_thread_task_runner.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_piece.h" |
| #include "base/strings/string_split.h" |
| #include "base/strings/string_tokenizer.h" |
| @@ -18,6 +15,7 @@ |
| #include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/time/time.h" |
| +#include "base/version.h" |
| #include "build/build_config.h" |
| #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h" |
| #include "components/data_reduction_proxy/core/common/data_reduction_proxy_client_config_parser.h" |
| @@ -43,6 +41,46 @@ std::string FormatOption(const std::string& name, const std::string& value) { |
| return name + "=" + value; |
| } |
| +// Returns the version of Chromium that is being used, e.g. "1.2.3.4". |
| +const char* ChromiumVersion() { |
| + // Assert at compile time that the Chromium version is at least somewhat |
| + // properly formed, e.g. the version string is at least as long as "0.0.0.0", |
| + // and starts and ends with numeric digits. This is to prevent another |
| + // regression like http://crbug.com/595471. |
| + static_assert(arraysize(PRODUCT_VERSION) >= arraysize("0.0.0.0") && |
| + '0' <= PRODUCT_VERSION[0] && PRODUCT_VERSION[0] <= '9' && |
| + '0' <= PRODUCT_VERSION[arraysize(PRODUCT_VERSION) - 2] && |
| + PRODUCT_VERSION[arraysize(PRODUCT_VERSION) - 2] <= '9', |
| + "PRODUCT_VERSION must be a string of the form " |
| + "'MAJOR.MINOR.BUILD.PATCH', e.g. '1.2.3.4'. " |
| + "PRODUCT_VERSION='" PRODUCT_VERSION "' is badly formed."); |
|
brucedawson
2016/03/19 00:24:32
Huh. Interesting fact: VS 2013 cannot compile this
sclittle
2016/03/19 00:29:46
Wow, I had no idea. I was relying on the CQ to cat
|
| + |
| + return PRODUCT_VERSION; |
| +} |
| + |
| +// Returns the build and patch numbers of |version_string|. |version_string| |
| +// must be a properly formed Chromium version number, e.g. "1.2.3.4". |
| +void GetChromiumBuildAndPatch(const std::string& version_string, |
| + std::string* build, |
| + std::string* patch) { |
| + base::Version version(version_string); |
| + DCHECK(version.IsValid()); |
| + DCHECK_EQ(4U, version.components().size()); |
| + |
| + *build = base::Uint64ToString(version.components()[2]); |
| + *patch = base::Uint64ToString(version.components()[3]); |
| +} |
| + |
| +#define CLIENT_ENUM(name, str_value) \ |
| + case name: \ |
| + return str_value; |
| +const char* GetString(Client client) { |
| + switch (client) { CLIENT_ENUMS_LIST } |
| + NOTREACHED(); |
| + return ""; |
| +} |
| +#undef CLIENT_ENUM |
| + |
| } // namespace |
| const char kSessionHeaderOption[] = "ps"; |
| @@ -59,17 +97,6 @@ const char kExperimentsOption[] = "exp"; |
| const char kAndroidWebViewProtocolVersion[] = ""; |
| #endif |
| -#define CLIENT_ENUM(name, str_value) \ |
| - case name: return str_value; |
| -const char* GetString(Client client) { |
| - switch (client) { |
| - CLIENT_ENUMS_LIST |
| - } |
| - NOTREACHED(); |
| - return ""; |
| -} |
| -#undef CLIENT_ENUM |
| - |
| // static |
| bool DataReductionProxyRequestOptions::IsKeySetOnCommandLine() { |
| const base::CommandLine& command_line = |
| @@ -81,14 +108,7 @@ bool DataReductionProxyRequestOptions::IsKeySetOnCommandLine() { |
| DataReductionProxyRequestOptions::DataReductionProxyRequestOptions( |
| Client client, |
| DataReductionProxyConfig* config) |
| - : client_(GetString(client)), |
| - use_assigned_credentials_(false), |
| - data_reduction_proxy_config_(config) { |
| - DCHECK(data_reduction_proxy_config_); |
| - GetChromiumBuildAndPatch(ChromiumVersion(), &build_, &patch_); |
| - // Constructed on the UI thread, but should be checked on the IO thread. |
| - thread_checker_.DetachFromThread(); |
| -} |
| + : DataReductionProxyRequestOptions(client, ChromiumVersion(), config) {} |
| DataReductionProxyRequestOptions::DataReductionProxyRequestOptions( |
| Client client, |
| @@ -109,35 +129,9 @@ DataReductionProxyRequestOptions::~DataReductionProxyRequestOptions() { |
| void DataReductionProxyRequestOptions::Init() { |
| key_ = GetDefaultKey(), |
| UpdateCredentials(); |
| - UpdateVersion(); |
| UpdateExperiments(); |
| } |
| -std::string DataReductionProxyRequestOptions::ChromiumVersion() const { |
| -#if defined(PRODUCT_VERSION) |
| - return PRODUCT_VERSION; |
| -#else |
| - return std::string(); |
| -#endif |
| -} |
| - |
| -void DataReductionProxyRequestOptions::GetChromiumBuildAndPatch( |
| - const std::string& version, |
| - std::string* build, |
| - std::string* patch) const { |
| - std::vector<base::StringPiece> version_parts = base::SplitStringPiece( |
| - version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| - if (version_parts.size() != 4) |
| - return; |
| - version_parts[2].CopyToString(build); |
| - version_parts[3].CopyToString(patch); |
| -} |
| - |
| -void DataReductionProxyRequestOptions::UpdateVersion() { |
| - GetChromiumBuildAndPatch(version_, &build_, &patch_); |
| - RegenerateRequestHeaderValue(); |
| -} |
| - |
| void DataReductionProxyRequestOptions::UpdateExperiments() { |
| std::string experiments = |
| base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| @@ -321,10 +315,13 @@ void DataReductionProxyRequestOptions::RegenerateRequestHeaderValue() { |
| } |
| if (!client_.empty()) |
| headers.push_back(FormatOption(kClientHeaderOption, client_)); |
| - if (!build_.empty() && !patch_.empty()) { |
| - headers.push_back(FormatOption(kBuildNumberHeaderOption, build_)); |
| - headers.push_back(FormatOption(kPatchNumberHeaderOption, patch_)); |
| - } |
| + |
| + DCHECK(!build_.empty()); |
| + headers.push_back(FormatOption(kBuildNumberHeaderOption, build_)); |
| + |
| + DCHECK(!patch_.empty()); |
| + headers.push_back(FormatOption(kPatchNumberHeaderOption, patch_)); |
| + |
| for (const auto& experiment : experiments_) |
| headers.push_back(FormatOption(kExperimentsOption, experiment)); |