Chromium Code Reviews| Index: components/data_reduction_proxy/core/common/data_reduction_proxy_util.cc |
| diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_util.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_util.cc |
| index 40d46f7ab849f10d831287cb1b2b3f2e7ae86918..99819a488cef4bf4bffd68143c86242b3b010b64 100644 |
| --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_util.cc |
| +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_util.cc |
| @@ -1,14 +1,115 @@ |
| // Copyright 2016 The Chromium Authors. All rights reserved. |
| // 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/common/data_reduction_proxy_util.h" |
| +#include "base/time/time.h" |
| +#include "net/base/url_util.h" |
| + |
| +#if defined(USE_GOOGLE_API_KEYS) |
| +#include "google_apis/google_api_keys.h" |
| +#endif |
| + |
| namespace data_reduction_proxy { |
| +namespace { |
| + |
| +#if defined(USE_GOOGLE_API_KEYS) |
| +// Used in all Data Reduction Proxy URLs to specify API Key. |
| +const char kApiKeyName[] = "key"; |
| +#endif |
| +} |
| + |
| bool IsMethodIdempotent(const std::string& method) { |
| return method == "GET" || method == "OPTIONS" || method == "HEAD" || |
| method == "PUT" || method == "DELETE" || method == "TRACE"; |
| } |
| +GURL AddApiKeyToUrl(const GURL& url) { |
| + GURL new_url = url; |
| +#if defined(USE_GOOGLE_API_KEYS) |
| + std::string api_key = google_apis::GetAPIKey(); |
| + if (google_apis::HasKeysConfigured() && !api_key.empty()) { |
| + new_url = net::AppendOrReplaceQueryParameter(url, kApiKeyName, api_key); |
| + } |
| +#endif |
| + return net::AppendOrReplaceQueryParameter(new_url, "alt", "proto"); |
| +} |
| + |
| +namespace protobuf_parser { |
| + |
| +net::ProxyServer::Scheme SchemeFromProxyScheme( |
| + ProxyServer_ProxyScheme proxy_scheme) { |
| + switch (proxy_scheme) { |
| + case ProxyServer_ProxyScheme_HTTP: |
| + return net::ProxyServer::SCHEME_HTTP; |
| + case ProxyServer_ProxyScheme_HTTPS: |
| + return net::ProxyServer::SCHEME_HTTPS; |
| + case ProxyServer_ProxyScheme_QUIC: |
| + return net::ProxyServer::SCHEME_QUIC; |
| + default: |
| + return net::ProxyServer::SCHEME_INVALID; |
| + } |
| +} |
| + |
| +ProxyServer_ProxyScheme ProxySchemeFromScheme(net::ProxyServer::Scheme scheme) { |
| + switch (scheme) { |
| + case net::ProxyServer::SCHEME_HTTP: |
| + return ProxyServer_ProxyScheme_HTTP; |
| + case net::ProxyServer::SCHEME_HTTPS: |
| + return ProxyServer_ProxyScheme_HTTPS; |
| + case net::ProxyServer::SCHEME_QUIC: |
| + return ProxyServer_ProxyScheme_QUIC; |
| + default: |
| + return ProxyServer_ProxyScheme_UNSPECIFIED; |
| + } |
| +} |
| + |
| +void TimeDeltatoDuration(const base::TimeDelta& time_delta, |
| + Duration* duration) { |
| + duration->set_seconds(time_delta.InSeconds()); |
| + base::TimeDelta partial_seconds = |
| + time_delta - base::TimeDelta::FromSeconds(time_delta.InSeconds()); |
| + duration->set_nanos(partial_seconds.InMicroseconds() * |
| + base::Time::kNanosecondsPerMicrosecond); |
| +} |
| + |
| +base::TimeDelta DurationToTimeDelta(const Duration& duration) { |
| + return base::TimeDelta::FromSeconds(duration.seconds()) + |
| + base::TimeDelta::FromMicroseconds( |
| + duration.nanos() / base::Time::kNanosecondsPerMicrosecond); |
| +} |
| + |
| +void TimetoTimestamp(const base::Time& time, Timestamp* timestamp) { |
| + timestamp->set_seconds(time.ToJavaTime() / |
| + base::Time::kMillisecondsPerSecond); |
| + timestamp->set_nanos( |
| + (time.ToJavaTime() % base::Time::kMillisecondsPerSecond) * |
| + base::Time::kMicrosecondsPerMillisecond * |
| + base::Time::kNanosecondsPerMicrosecond); |
| +} |
| + |
| +base::Time TimestamptoTime(const Timestamp& timestamp) { |
| + return base::Time::FromJsTime( |
|
tbansal1
2016/05/27 23:01:58
Is there a difference between this implementation
RyanSturm
2016/05/31 20:10:51
I couldn't find a good way to build a proto from c
|
| + timestamp.seconds() * base::Time::kMillisecondsPerSecond + |
| + timestamp.nanos() * base::Time::kMillisecondsPerSecond / |
| + base::Time::kNanosecondsPerSecond); |
| +} |
| + |
| +std::unique_ptr<Duration> CreateDurationFromTimeDelta( |
| + const base::TimeDelta& time_delta) { |
| + std::unique_ptr<Duration> duration(new Duration); |
| + TimeDeltatoDuration(time_delta, duration.get()); |
| + return duration; |
| +} |
| + |
| +std::unique_ptr<Timestamp> CreateTimestampFromTime(const base::Time& time) { |
| + std::unique_ptr<Timestamp> timestamp(new Timestamp); |
| + TimetoTimestamp(time, timestamp.get()); |
| + return timestamp; |
| +} |
| + |
| +} // namespace protobuf_parser |
| + |
| } // namespace data_reduction_proxy |