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

Unified Diff: components/data_reduction_proxy/core/common/data_reduction_proxy_util.cc

Issue 2619243003: Adding ExtraRequest info and exposing request to PLM observers (Closed)
Patch Set: comments Created 3 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
« no previous file with comments | « components/data_reduction_proxy/core/common/data_reduction_proxy_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 7dfa41158be550aeba78ff441dfd9578707ee803..451aa970e9ea4580fbb7f2821d19e1e2cd58aa7a 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
@@ -4,13 +4,18 @@
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_util.h"
+#include <stdint.h>
+
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/version.h"
#include "components/data_reduction_proxy/core/common/version.h"
+#include "net/base/net_errors.h"
#include "net/base/url_util.h"
+#include "net/http/http_response_headers.h"
#include "net/proxy/proxy_config.h"
#include "net/proxy/proxy_info.h"
+#include "net/url_request/url_request.h"
#if defined(USE_GOOGLE_API_KEYS)
#include "google_apis/google_api_keys.h"
@@ -24,6 +29,34 @@ namespace {
// Used in all Data Reduction Proxy URLs to specify API Key.
const char kApiKeyName[] = "key";
#endif
+
+// Scales |byte_count| by the ratio of |numerator|:|denomenator|.
+int64_t ScaleByteCountByRatio(int64_t byte_count,
+ int64_t numerator,
+ int64_t denomenator) {
+ DCHECK_LE(0, byte_count);
+ DCHECK_LE(0, numerator);
+ DCHECK_LT(0, denomenator);
+
+ // As an optimization, use integer arithmetic if it won't overflow.
+ if (byte_count <= std::numeric_limits<int32_t>::max() &&
+ numerator <= std::numeric_limits<int32_t>::max()) {
+ return byte_count * numerator / denomenator;
+ }
+
+ double scaled_byte_count = static_cast<double>(byte_count) *
+ static_cast<double>(numerator) /
+ static_cast<double>(denomenator);
+ if (scaled_byte_count >
+ static_cast<double>(std::numeric_limits<int64_t>::max())) {
+ // If this ever triggers, then byte counts can no longer be safely stored in
+ // 64-bit ints.
+ NOTREACHED();
+ return byte_count;
+ }
+ return static_cast<int64_t>(scaled_byte_count);
+}
+
} // namespace
namespace util {
@@ -133,6 +166,29 @@ bool ApplyProxyConfigToProxyInfo(const net::ProxyConfig& proxy_config,
return !data_reduction_proxy_info->proxy_server().is_direct();
}
+int64_t CalculateEffectiveOCL(const net::URLRequest& request) {
+ int64_t original_content_length_from_header =
+ request.response_headers()->GetInt64HeaderValue(
+ "x-original-content-length");
+
+ if (original_content_length_from_header < 0)
+ return request.received_response_content_length();
+ if (request.status().error() == net::OK)
+ return original_content_length_from_header;
+
+ int64_t content_length_from_header =
+ request.response_headers()->GetContentLength();
+
+ if (content_length_from_header < 0)
+ return request.received_response_content_length();
+ if (content_length_from_header == 0)
+ return original_content_length_from_header;
+
+ return ScaleByteCountByRatio(request.received_response_content_length(),
+ original_content_length_from_header,
+ content_length_from_header);
+}
+
} // namespace util
namespace protobuf_parser {
« no previous file with comments | « components/data_reduction_proxy/core/common/data_reduction_proxy_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698