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..315a7b3233b1ad5abcef5b512b2415d056171aa5 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,114 @@ |
// 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 - base::Time::UnixEpoch()).InSeconds()); |
+ timestamp->set_nanos(((time - base::Time::UnixEpoch()).InMicroseconds() % |
+ base::Time::kMicrosecondsPerSecond) * |
+ base::Time::kNanosecondsPerMicrosecond); |
+} |
+ |
+base::Time TimestampToTime(const Timestamp& timestamp) { |
+ base::Time t = base::Time::UnixEpoch(); |
+ t += base::TimeDelta::FromSeconds(timestamp.seconds()); |
+ t += base::TimeDelta::FromMicroseconds( |
+ timestamp.nanos() / base::Time::kNanosecondsPerMicrosecond); |
+ return t; |
+} |
+ |
+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 |