Commit 4f4de21e authored by Robert Ogden's avatar Robert Ogden Committed by Commit Bot

Report HTTPS Preview data savings to Data Saver

Uses the standard DRP utilities to do this.

Bug: 864658
Change-Id: Ib8b4d5a3df7d89df172138ac3d556e21b5387a81
Reviewed-on: https://chromium-review.googlesource.com/c/1262416
Commit-Queue: Robert Ogden <robertogden@chromium.org>
Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596939}
parent 28e01caf
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include <stdint.h>
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
...@@ -30,7 +31,11 @@ ...@@ -30,7 +31,11 @@
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service_client_test_utils.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service_client_test_utils.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/data_reduction_proxy/proto/data_store.pb.h"
#include "components/infobars/core/infobar.h" #include "components/infobars/core/infobar.h"
#include "components/prefs/pref_service.h"
#include "components/previews/core/previews_features.h" #include "components/previews/core/previews_features.h"
#include "content/public/browser/navigation_entry.h" #include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
...@@ -216,6 +221,47 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest { ...@@ -216,6 +221,47 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest {
EXPECT_EQ(content::PAGE_TYPE_ERROR, entry->GetPageType()); EXPECT_EQ(content::PAGE_TYPE_ERROR, entry->GetPageType());
} }
void ResetDataSavings() const {
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
browser()->profile())
->data_reduction_proxy_service()
->compression_stats()
->ResetStatistics();
}
// Gets the data usage recorded against the host the origin server runs on.
uint64_t GetDataUsage() const {
const auto& data_usage_map =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
browser()->profile())
->data_reduction_proxy_service()
->compression_stats()
->DataUsageMapForTesting();
const auto& it =
data_usage_map.find(https_server_->host_port_pair().host());
if (it != data_usage_map.end())
return it->second->data_used();
return 0;
}
// Gets the data usage recorded against all hosts.
uint64_t GetTotalDataUsage() const {
return DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
browser()->profile())
->data_reduction_proxy_service()
->compression_stats()
->GetHttpReceivedContentLength();
}
// Gets the original content length recorded against all hosts.
uint64_t GetTotalOriginalContentLength() const {
return DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
browser()->profile())
->data_reduction_proxy_service()
->compression_stats()
->GetHttpOriginalContentLength();
}
// Returns a HTTP URL that will respond with the given HTTP response code and // Returns a HTTP URL that will respond with the given HTTP response code and
// headers when used by the previews server. The response can be delayed a // headers when used by the previews server. The response can be delayed a
// number of milliseconds by passing a value > 0 for |delay_ms| or pass -1 to // number of milliseconds by passing a value > 0 for |delay_ms| or pass -1 to
...@@ -354,6 +400,8 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest { ...@@ -354,6 +400,8 @@ class PreviewsLitePageServerBrowserTest : public InProcessBrowserTest {
switch (return_code) { switch (return_code) {
case 200: case 200:
response->set_code(net::HTTP_OK); response->set_code(net::HTTP_OK);
response->set_content("porgporgporgporgporg" /* length = 20 */);
response->AddCustomHeader("chrome-proxy", "ofcl=60");
break; break;
case 307: case 307:
response->set_code(net::HTTP_TEMPORARY_REDIRECT); response->set_code(net::HTTP_TEMPORARY_REDIRECT);
...@@ -658,6 +706,32 @@ IN_PROC_BROWSER_TEST_F(PreviewsLitePageServerBrowserTest, ...@@ -658,6 +706,32 @@ IN_PROC_BROWSER_TEST_F(PreviewsLitePageServerBrowserTest,
VerifyPreviewLoaded(); VerifyPreviewLoaded();
} }
// Previews InfoBar (which these tests trigger) does not work on Mac.
// See https://crbug.com/782322 for detail.
// Also occasional flakes on win7 (https://crbug.com/789542).
#if defined(OS_ANDROID) || defined(OS_LINUX)
#define MAYBE_LitePagePreviewsReportSavings LitePagePreviewsReportSavings
#else
#define MAYBE_LitePagePreviewsReportSavings \
DISABLED_LitePagePreviewsReportSavings
#endif
IN_PROC_BROWSER_TEST_F(PreviewsLitePageServerBrowserTest,
MAYBE_LitePagePreviewsReportSavings) {
PrefService* prefs = browser()->profile()->GetPrefs();
prefs->SetBoolean(data_reduction_proxy::prefs::kDataUsageReportingEnabled,
true);
// Give the setting notification a chance to propagate.
base::RunLoop().RunUntilIdle();
ResetDataSavings();
ui_test_utils::NavigateToURL(browser(), HttpsLitePageURL(200));
VerifyPreviewLoaded();
EXPECT_EQ(GetTotalOriginalContentLength() - GetTotalDataUsage(), 40U);
EXPECT_EQ(GetDataUsage(), 20U);
}
class PreviewsLitePageServerTimeoutBrowserTest class PreviewsLitePageServerTimeoutBrowserTest
: public PreviewsLitePageServerBrowserTest { : public PreviewsLitePageServerBrowserTest {
public: public:
......
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
#include "chrome/browser/previews/previews_service.h" #include "chrome/browser/previews/previews_service.h"
#include "chrome/browser/previews/previews_service_factory.h" #include "chrome/browser/previews/previews_service_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/previews/core/previews_experiments.h" #include "components/previews/core/previews_experiments.h"
...@@ -247,6 +251,25 @@ uint64_t PreviewsLitePageDecider::GeneratePageID() { ...@@ -247,6 +251,25 @@ uint64_t PreviewsLitePageDecider::GeneratePageID() {
return ++page_id_; return ++page_id_;
} }
void PreviewsLitePageDecider::ReportDataSavings(int64_t network_bytes,
int64_t original_bytes,
const std::string& host) {
if (!drp_settings_ || !drp_settings_->data_reduction_proxy_service())
return;
drp_settings_->data_reduction_proxy_service()->UpdateDataUseForHost(
network_bytes, original_bytes, host);
drp_settings_->data_reduction_proxy_service()->UpdateContentLengths(
network_bytes, original_bytes, true /* data_reduction_proxy_enabled */,
data_reduction_proxy::DataReductionProxyRequestType::
VIA_DATA_REDUCTION_PROXY,
"text/html", true /* is_user_traffic */,
data_use_measurement::DataUseUserData::DataUseContentType::
MAIN_FRAME_HTML,
0);
}
bool PreviewsLitePageDecider::NeedsToNotifyUser() { bool PreviewsLitePageDecider::NeedsToNotifyUser() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return need_to_show_notification_; return need_to_show_notification_;
......
...@@ -75,6 +75,9 @@ class PreviewsLitePageDecider ...@@ -75,6 +75,9 @@ class PreviewsLitePageDecider
void AddSingleBypass(std::string url) override; void AddSingleBypass(std::string url) override;
bool CheckSingleBypass(std::string url) override; bool CheckSingleBypass(std::string url) override;
uint64_t GeneratePageID() override; uint64_t GeneratePageID() override;
void ReportDataSavings(int64_t network_bytes,
int64_t original_bytes,
const std::string& host) override;
bool NeedsToNotifyUser() override; bool NeedsToNotifyUser() override;
void NotifyUser(content::WebContents* web_contents) override; void NotifyUser(content::WebContents* web_contents) override;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/previews/previews_lite_page_navigation_throttle.h" #include "chrome/browser/previews/previews_lite_page_navigation_throttle.h"
#include <stdint.h>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
...@@ -430,8 +431,17 @@ PreviewsLitePageNavigationThrottle::WillProcessResponse() { ...@@ -430,8 +431,17 @@ PreviewsLitePageNavigationThrottle::WillProcessResponse() {
const int response_code = response_headers->response_code(); const int response_code = response_headers->response_code();
if (response_code == net::HTTP_OK) { if (response_code == net::HTTP_OK) {
// Attempt to get the original content length and report it to Data Saver.
const int64_t ofcl =
data_reduction_proxy::GetDataReductionProxyOFCL(response_headers);
if (ofcl > 0) {
manager_->ReportDataSavings(response_headers->GetContentLength(), ofcl,
GURL(original_url).host());
}
UMA_HISTOGRAM_ENUMERATION("Previews.ServerLitePage.ServerResponse", UMA_HISTOGRAM_ENUMERATION("Previews.ServerLitePage.ServerResponse",
ServerResponse::kOk); ServerResponse::kOk);
return content::NavigationThrottle::PROCEED; return content::NavigationThrottle::PROCEED;
} }
......
...@@ -38,6 +38,11 @@ class PreviewsLitePageNavigationThrottleManager { ...@@ -38,6 +38,11 @@ class PreviewsLitePageNavigationThrottleManager {
// Generates a new page id for a request to the previews server. // Generates a new page id for a request to the previews server.
virtual uint64_t GeneratePageID() = 0; virtual uint64_t GeneratePageID() = 0;
// Reports data savings to Data Saver.
virtual void ReportDataSavings(int64_t network_bytes,
int64_t original_bytes,
const std::string& host) = 0;
// Note: |NeedsToToNotify| is intentionally separate from |NotifyUser| for // Note: |NeedsToToNotify| is intentionally separate from |NotifyUser| for
// ease of testing and metrics collection without changing the notification // ease of testing and metrics collection without changing the notification
// state. // state.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment