From 32a19e6ad65292e1efc67986e6a61c0f29e393d3 Mon Sep 17 00:00:00 2001 From: jeremyim Date: Fri, 30 Jan 2015 12:36:57 -0800 Subject: [PATCH] Create a new DataReductionProxyConfig class This instance will be created by the DataReductionProxySettings class, and take ownership of the DataReductionProxyParams (currently owned by DataReductionProxySettings). BUG=452773 Review URL: https://codereview.chromium.org/880963006 Cr-Commit-Position: refs/heads/master@{#313980} --- android_webview/browser/aw_browser_context.cc | 6 +- .../data_reduction_proxy_chrome_settings.cc | 3 +- .../data_reduction_proxy_chrome_settings.h | 2 +- ...data_reduction_proxy_chrome_settings_factory.cc | 3 +- components/components_tests.gyp | 1 + components/data_reduction_proxy.gypi | 6 + .../data_reduction_proxy/core/browser/BUILD.gn | 7 + ...ata_reduction_proxy_bypass_protocol_unittest.cc | 23 +- .../core/browser/data_reduction_proxy_config.cc | 292 ++++++++++++++++++++ .../core/browser/data_reduction_proxy_config.h | 183 +++++++++++++ .../data_reduction_proxy_config_test_utils.cc | 54 ++++ .../data_reduction_proxy_config_test_utils.h | 49 ++++ .../data_reduction_proxy_config_unittest.cc | 293 +++++++++++++++++++++ .../browser/data_reduction_proxy_configurator.h | 2 +- ...data_reduction_proxy_configurator_test_utils.cc | 45 ++++ .../data_reduction_proxy_configurator_test_utils.h | 90 +++++++ .../data_reduction_proxy_configurator_unittest.cc | 18 +- .../data_reduction_proxy_interceptor_unittest.cc | 12 +- .../data_reduction_proxy_io_data_unittest.cc | 12 +- .../core/browser/data_reduction_proxy_settings.cc | 47 ++-- .../core/browser/data_reduction_proxy_settings.h | 36 +-- .../data_reduction_proxy_settings_test_utils.cc | 54 +--- .../data_reduction_proxy_settings_test_utils.h | 52 +--- .../data_reduction_proxy_settings_unittest.cc | 55 ++-- .../data_reduction_proxy_usage_stats_unittest.cc | 3 +- 25 files changed, 1140 insertions(+), 208 deletions(-) create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_config.cc create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.cc create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.cc create mode 100644 components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h diff --git a/android_webview/browser/aw_browser_context.cc b/android_webview/browser/aw_browser_context.cc index 16d472455f14..f20bd9ce62cf 100644 --- a/android_webview/browser/aw_browser_context.cc +++ b/android_webview/browser/aw_browser_context.cc @@ -160,8 +160,10 @@ void AwBrowserContext::PreMainMessageLoopRun() { data_reduction_proxy_settings_.reset( new data_reduction_proxy::DataReductionProxySettings( - new data_reduction_proxy::DataReductionProxyParams( - data_reduction_proxy::DataReductionProxyParams::kAllowed))); + scoped_ptr( + new data_reduction_proxy::DataReductionProxyParams( + data_reduction_proxy::DataReductionProxyParams::kAllowed)) + .Pass())); data_reduction_proxy_io_data_.reset( new data_reduction_proxy::DataReductionProxyIOData( data_reduction_proxy::Client::WEBVIEW_ANDROID, diff --git a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.cc b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.cc index 65607f356d8d..ce1f7c96096f 100644 --- a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.cc +++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.cc @@ -52,7 +52,8 @@ void MigrateDataReductionProxyOffProxyPrefs(PrefService* prefs) { } DataReductionProxyChromeSettings::DataReductionProxyChromeSettings( - DataReductionProxyParams* params) : DataReductionProxySettings(params) { + scoped_ptr params) + : DataReductionProxySettings(params.Pass()) { } DataReductionProxyChromeSettings::~DataReductionProxyChromeSettings() { diff --git a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h index d77d0fd03548..57fbf651ca85 100644 --- a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h +++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h @@ -37,7 +37,7 @@ class DataReductionProxyChromeSettings // Constructs a settings object with the given configuration parameters. // Construction and destruction must happen on the UI thread. explicit DataReductionProxyChromeSettings( - data_reduction_proxy::DataReductionProxyParams* params); + scoped_ptr params); // Destructs the settings object. ~DataReductionProxyChromeSettings() override; diff --git a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.cc b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.cc index 3c47e852f642..90f23841975f 100644 --- a/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.cc +++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.cc @@ -71,5 +71,6 @@ KeyedService* DataReductionProxyChromeSettingsFactory::BuildServiceInstanceFor( #endif return new DataReductionProxyChromeSettings( - new DataReductionProxyParams(flags)); + scoped_ptr(new DataReductionProxyParams(flags)) + .Pass()); } diff --git a/components/components_tests.gyp b/components/components_tests.gyp index 87097936aaa7..5088ba3c2537 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -91,6 +91,7 @@ 'crx_file/id_util_unittest.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler_unittest.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc', + 'data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_configurator_unittest.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc', diff --git a/components/data_reduction_proxy.gypi b/components/data_reduction_proxy.gypi index 5cd2834ae754..c3785d624a20 100644 --- a/components/data_reduction_proxy.gypi +++ b/components/data_reduction_proxy.gypi @@ -34,6 +34,8 @@ 'data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h', 'data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h', + 'data_reduction_proxy/core/browser/data_reduction_proxy_config.cc', + 'data_reduction_proxy/core/browser/data_reduction_proxy_config.h', 'data_reduction_proxy/core/browser/data_reduction_proxy_configurator.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h', 'data_reduction_proxy/core/browser/data_reduction_proxy_delegate.cc', @@ -102,6 +104,10 @@ ], 'sources': [ # Note: sources list duplicated in GN build. + 'data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.cc', + 'data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h', + 'data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.cc', + 'data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h', 'data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h', 'data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.cc', diff --git a/components/data_reduction_proxy/core/browser/BUILD.gn b/components/data_reduction_proxy/core/browser/BUILD.gn index f014d0ff4c89..a8b742471226 100644 --- a/components/data_reduction_proxy/core/browser/BUILD.gn +++ b/components/data_reduction_proxy/core/browser/BUILD.gn @@ -8,6 +8,8 @@ static_library("browser") { "data_reduction_proxy_auth_request_handler.h", "data_reduction_proxy_bypass_protocol.cc", "data_reduction_proxy_bypass_protocol.h", + "data_reduction_proxy_config.cc", + "data_reduction_proxy_config.h", "data_reduction_proxy_configurator.cc", "data_reduction_proxy_configurator.h", "data_reduction_proxy_delegate.cc", @@ -49,6 +51,10 @@ static_library("browser") { source_set("test_support") { testonly = true sources = [ + "data_reduction_proxy_config_test_utils.cc", + "data_reduction_proxy_config_test_utils.h", + "data_reduction_proxy_configurator_test_utils.cc", + "data_reduction_proxy_configurator_test_utils.h", "data_reduction_proxy_settings_test_utils.cc", "data_reduction_proxy_settings_test_utils.h", ] @@ -73,6 +79,7 @@ source_set("unit_tests") { sources = [ "data_reduction_proxy_auth_request_handler_unittest.cc", "data_reduction_proxy_bypass_protocol_unittest.cc", + "data_reduction_proxy_config_unittest.cc", "data_reduction_proxy_configurator_unittest.cc", "data_reduction_proxy_interceptor_unittest.cc", "data_reduction_proxy_metrics_unittest.cc", diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc index 157f0d82b325..406ece1ea47d 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol_unittest.cc @@ -81,12 +81,13 @@ class DataReductionProxyProtocolTest : public testing::Test { public: DataReductionProxyProtocolTest() : http_user_agent_settings_("", "") { settings_.reset( - new DataReductionProxySettings(CreateTestDataReductionProxyParams())); - proxy_params_.reset(CreateTestDataReductionProxyParams()); + new DataReductionProxySettings(CreateDataReductionProxyParams())); + proxy_params_.reset(CreateDataReductionProxyParams().release()); simple_interceptor_.reset(new SimpleURLRequestInterceptor()); net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( "http", "www.google.com", simple_interceptor_.Pass()); } + ~DataReductionProxyProtocolTest() override { // URLRequestJobs may post clean-up tasks on destruction. net::URLRequestFilter::GetInstance()->RemoveHostnameHandler( @@ -94,14 +95,16 @@ class DataReductionProxyProtocolTest : public testing::Test { base::RunLoop().RunUntilIdle(); } - TestDataReductionProxyParams* CreateTestDataReductionProxyParams() { - return new TestDataReductionProxyParams( - DataReductionProxyParams::kAllowed | - DataReductionProxyParams::kFallbackAllowed | - DataReductionProxyParams::kPromoAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN); + scoped_ptr CreateDataReductionProxyParams() { + return scoped_ptr( + new TestDataReductionProxyParams( + DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed, + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)) + .Pass(); } void SetUp() override { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.cc new file mode 100644 index 000000000000..0fbd744faeea --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.cc @@ -0,0 +1,292 @@ +// Copyright 2015 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/browser/data_reduction_proxy_config.h" + +#include "base/metrics/histogram.h" +#include "base/metrics/sparse_histogram.h" +#include "base/single_thread_task_runner.h" +#include "base/strings/string_util.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" +#include "net/base/load_flags.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_request_context_getter.h" +#include "net/url_request/url_request_status.h" + +namespace { + +// Values of the UMA DataReductionProxy.NetworkChangeEvents histograms. +// This enum must remain synchronized with the enum of the same +// name in metrics/histograms/histograms.xml. +enum DataReductionProxyNetworkChangeEvent { + IP_CHANGED = 0, // The client IP address changed. + DISABLED_ON_VPN = 1, // The proxy is disabled because a VPN is running. + CHANGE_EVENT_COUNT = 2 // This must always be last. +}; + +// Key of the UMA DataReductionProxy.ProbeURL histogram. +const char kUMAProxyProbeURL[] = "DataReductionProxy.ProbeURL"; + +// Key of the UMA DataReductionProxy.ProbeURLNetError histogram. +const char kUMAProxyProbeURLNetError[] = "DataReductionProxy.ProbeURLNetError"; + +// Record a network change event. +void RecordNetworkChangeEvent(DataReductionProxyNetworkChangeEvent event) { + UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.NetworkChangeEvents", event, + CHANGE_EVENT_COUNT); +} + +} // namespace + +namespace data_reduction_proxy { + +DataReductionProxyConfig::DataReductionProxyConfig( + scoped_ptr params) + : restricted_by_carrier_(false), + disabled_on_vpn_(false), + unreachable_(false), + enabled_by_user_(false), + alternative_enabled_by_user_(false), + net_log_(nullptr), + url_request_context_getter_(nullptr), + configurator_(nullptr), + event_store_(nullptr) { + params_.reset(params.release()); +} + +DataReductionProxyConfig::~DataReductionProxyConfig() { + net::NetworkChangeNotifier::RemoveIPAddressObserver(this); +} + +void DataReductionProxyConfig::InitDataReductionProxyConfig( + scoped_refptr io_task_runner, + net::NetLog* net_log, + net::URLRequestContextGetter* url_request_context_getter, + DataReductionProxyConfigurator* configurator, + DataReductionProxyEventStore* event_store) { + DCHECK(io_task_runner); + DCHECK(configurator); + DCHECK(event_store); + DCHECK(io_task_runner->BelongsToCurrentThread()); + io_task_runner_ = io_task_runner; + net_log_ = net_log; + url_request_context_getter_ = url_request_context_getter; + configurator_ = configurator; + event_store_ = event_store; + net::NetworkChangeNotifier::AddIPAddressObserver(this); +} + +void DataReductionProxyConfig::SetProxyConfigs(bool enabled, + bool alternative_enabled, + bool restricted, + bool at_startup) { + DCHECK(configurator_); + + LogProxyState(enabled, restricted, at_startup); + // The alternative is only configured if the standard configuration is + // is enabled. + if (enabled & !params()->holdback()) { + if (alternative_enabled) { + configurator_->Enable(restricted, + !params()->alternative_fallback_allowed(), + params()->alt_origin().spec(), std::string(), + params()->ssl_origin().spec()); + } else { + configurator_->Enable(restricted, !params()->fallback_allowed(), + params()->origin().spec(), + params()->fallback_origin().spec(), std::string()); + } + } else { + configurator_->Disable(); + } +} + +void DataReductionProxyConfig::LogProxyState(bool enabled, + bool restricted, + bool at_startup) { + // This must stay a LOG(WARNING); the output is used in processing customer + // feedback. + const char kAtStartup[] = "at startup"; + const char kByUser[] = "by user action"; + const char kOn[] = "ON"; + const char kOff[] = "OFF"; + const char kRestricted[] = "(Restricted)"; + const char kUnrestricted[] = "(Unrestricted)"; + + std::string annotated_on = + kOn + std::string(" ") + (restricted ? kRestricted : kUnrestricted); + + LOG(WARNING) << "SPDY proxy " << (enabled ? annotated_on : kOff) << " " + << (at_startup ? kAtStartup : kByUser); +} + +void DataReductionProxyConfig::OnURLFetchComplete( + const net::URLFetcher* source) { + DCHECK(source == fetcher_.get()); + net::URLRequestStatus status = source->GetStatus(); + + if (event_store_) { + event_store_->EndCanaryRequest(bound_net_log_, status.error()); + } + + if (status.status() == net::URLRequestStatus::FAILED) { + if (status.error() == net::ERR_INTERNET_DISCONNECTED) { + RecordProbeURLFetchResult(INTERNET_DISCONNECTED); + return; + } + // TODO(bengr): Remove once we understand the reasons probes are failing. + // Probe errors are either due to fetcher-level errors or modified + // responses. This only tracks the former. + UMA_HISTOGRAM_SPARSE_SLOWLY(kUMAProxyProbeURLNetError, + std::abs(status.error())); + } + + std::string response; + source->GetResponseAsString(&response); + + if ("OK" == response.substr(0, 2)) { + DVLOG(1) << "The data reduction proxy is unrestricted."; + + if (enabled_by_user_) { + if (restricted_by_carrier_) { + // The user enabled the proxy, but sometime previously in the session, + // the network operator had blocked the canary and restricted the user. + // The current network doesn't block the canary, so don't restrict the + // proxy configurations. + SetProxyConfigs(true /* enabled */, false /* alternative_enabled */, + false /* restricted */, false /* at_startup */); + RecordProbeURLFetchResult(SUCCEEDED_PROXY_ENABLED); + } else { + RecordProbeURLFetchResult(SUCCEEDED_PROXY_ALREADY_ENABLED); + } + } + restricted_by_carrier_ = false; + return; + } + DVLOG(1) << "The data reduction proxy is restricted to the configured " + << "fallback proxy."; + if (enabled_by_user_) { + if (!restricted_by_carrier_) { + // Restrict the proxy. + SetProxyConfigs(true /* enabled */, false /* alternative_enabled */, + true /* restricted */, false /* at_startup */); + RecordProbeURLFetchResult(FAILED_PROXY_DISABLED); + } else { + RecordProbeURLFetchResult(FAILED_PROXY_ALREADY_DISABLED); + } + } + restricted_by_carrier_ = true; +} + +void DataReductionProxyConfig::OnIPAddressChanged() { + if (enabled_by_user_) { + DCHECK(params()->allowed()); + RecordNetworkChangeEvent(IP_CHANGED); + if (DisableIfVPN()) + return; + if (alternative_enabled_by_user_ && + !params()->alternative_fallback_allowed()) { + return; + } + ProbeWhetherDataReductionProxyIsAvailable(); + } +} + +void DataReductionProxyConfig::AddDefaultProxyBypassRules() { + // localhost + DCHECK(configurator_); + configurator_->AddHostPatternToBypass(""); + // RFC6890 loopback addresses. + // TODO(tbansal): Remove this once crbug/446705 is fixed. + configurator_->AddHostPatternToBypass("127.0.0.0/8"); + + // RFC6890 current network (only valid as source address). + configurator_->AddHostPatternToBypass("0.0.0.0/8"); + + // RFC1918 private addresses. + configurator_->AddHostPatternToBypass("10.0.0.0/8"); + configurator_->AddHostPatternToBypass("172.16.0.0/12"); + configurator_->AddHostPatternToBypass("192.168.0.0/16"); + + // RFC3513 unspecified address. + configurator_->AddHostPatternToBypass("::/128"); + + // RFC4193 private addresses. + configurator_->AddHostPatternToBypass("fc00::/7"); + // IPV6 probe addresses. + configurator_->AddHostPatternToBypass("*-ds.metric.gstatic.com"); + configurator_->AddHostPatternToBypass("*-v4.metric.gstatic.com"); +} + +void DataReductionProxyConfig::RecordProbeURLFetchResult( + ProbeURLFetchResult result) { + UMA_HISTOGRAM_ENUMERATION(kUMAProxyProbeURL, result, + PROBE_URL_FETCH_RESULT_COUNT); +} + +net::URLFetcher* DataReductionProxyConfig::GetURLFetcherForProbe() { + net::URLFetcher* fetcher = + net::URLFetcher::Create(params_->probe_url(), net::URLFetcher::GET, this); + fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY); + DCHECK(url_request_context_getter_); + fetcher->SetRequestContext(url_request_context_getter_); + // Configure max retries to be at most kMaxRetries times for 5xx errors. + static const int kMaxRetries = 5; + fetcher->SetMaxRetriesOn5xx(kMaxRetries); + fetcher->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries); + return fetcher; +} + +void DataReductionProxyConfig::ProbeWhetherDataReductionProxyIsAvailable() { + net::URLFetcher* fetcher = GetURLFetcherForProbe(); + if (!fetcher) + return; + fetcher_.reset(fetcher); + + bound_net_log_ = net::BoundNetLog::Make( + net_log_, net::NetLog::SOURCE_DATA_REDUCTION_PROXY); + if (event_store_) { + event_store_->BeginCanaryRequest(bound_net_log_, + fetcher_->GetOriginalURL()); + } + + fetcher_->Start(); +} + +void DataReductionProxyConfig::GetNetworkList( + net::NetworkInterfaceList* interfaces, + int policy) { + net::GetNetworkList(interfaces, policy); +} + +bool DataReductionProxyConfig::DisableIfVPN() { + net::NetworkInterfaceList network_interfaces; + GetNetworkList(&network_interfaces, 0); + // VPNs use a "tun" interface, so the presence of a "tun" interface indicates + // a VPN is in use. + // TODO(kundaji): Verify this works on Windows. + const std::string vpn_interface_name_prefix = "tun"; + for (size_t i = 0; i < network_interfaces.size(); ++i) { + std::string interface_name = network_interfaces[i].name; + if (LowerCaseEqualsASCII( + interface_name.begin(), + interface_name.begin() + vpn_interface_name_prefix.size(), + vpn_interface_name_prefix.c_str())) { + SetProxyConfigs(false, alternative_enabled_by_user_, false, false); + disabled_on_vpn_ = true; + RecordNetworkChangeEvent(DISABLED_ON_VPN); + return true; + } + } + if (disabled_on_vpn_) { + SetProxyConfigs(enabled_by_user_, alternative_enabled_by_user_, + restricted_by_carrier_, false); + } + disabled_on_vpn_ = false; + return false; +} + +} // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h new file mode 100644 index 000000000000..ac7f758172f1 --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h @@ -0,0 +1,183 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIG_H_ +#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIG_H_ + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/threading/thread_checker.h" +#include "net/base/net_errors.h" +#include "net/base/net_log.h" +#include "net/base/net_util.h" +#include "net/base/network_change_notifier.h" +#include "net/url_request/url_fetcher_delegate.h" + +namespace base { +class SingleThreadTaskRunner; +} + +namespace net { +class NetLog; +class URLFetcher; +class URLRequestContextGetter; +} + +namespace data_reduction_proxy { + +class DataReductionProxyConfigurator; +class DataReductionProxyEventStore; +class DataReductionProxyParams; + +// Values of the UMA DataReductionProxy.ProbeURL histogram. +// This enum must remain synchronized with +// DataReductionProxyProbeURLFetchResult in metrics/histograms/histograms.xml. +// TODO(marq): Rename these histogram buckets with s/DISABLED/RESTRICTED/, so +// their names match the behavior they track. +enum ProbeURLFetchResult { + // The probe failed because the Internet was disconnected. + INTERNET_DISCONNECTED = 0, + + // The probe failed for any other reason, and as a result, the proxy was + // disabled. + FAILED_PROXY_DISABLED, + + // The probe failed, but the proxy was already restricted. + FAILED_PROXY_ALREADY_DISABLED, + + // The probe succeeded, and as a result the proxy was restricted. + SUCCEEDED_PROXY_ENABLED, + + // The probe succeeded, but the proxy was already restricted. + SUCCEEDED_PROXY_ALREADY_ENABLED, + + // This must always be last. + PROBE_URL_FETCH_RESULT_COUNT +}; + +// Central point for holding the Data Reduction Proxy configuration. +// This object lives on the IO thread and all of its methods are expected to be +// called from there. +class DataReductionProxyConfig + : public net::URLFetcherDelegate, + public net::NetworkChangeNotifier::IPAddressObserver { + public: + // DataReductionProxyConfig will take ownership of |params|. + DataReductionProxyConfig(scoped_ptr params); + ~DataReductionProxyConfig() override; + + // Initializes the Data Reduction Proxy config with an |io_task_runner|, + // |net_log|, a |UrlRequestContextGetter| for canary probes, a + // |DataReductionProxyConfigurator| for setting the proxy configuration, and a + // |DataReductionProxyEventStore| for logging event changes. The caller must + // ensure that all parameters remain alive for the lifetime of the + // |DataReductionProxyConfig| instance. + void InitDataReductionProxyConfig( + scoped_refptr io_task_runner, + net::NetLog* net_log, + net::URLRequestContextGetter* url_request_context_getter, + DataReductionProxyConfigurator* configurator, + DataReductionProxyEventStore* event_store); + + // Returns the underlying |DataReductionProxyParams| instance. + DataReductionProxyParams* params() const { + return params_.get(); + } + + protected: + // Virtualized for testing. Returns a fetcher for the probe to check if OK for + // the proxy to use TLS. + virtual net::URLFetcher* GetURLFetcherForProbe(); + + // Sets the proxy configs, enabling or disabling the proxy according to + // the value of |enabled| and |alternative_enabled|. Use the alternative + // configuration only if |enabled| and |alternative_enabled| are true. If + // |restricted| is true, only enable the fallback proxy. |at_startup| is true + // when this method is called from InitDataReductionProxySettings. + virtual void SetProxyConfigs(bool enabled, + bool alternative_enabled, + bool restricted, + bool at_startup); + + // Writes a warning to the log that is used in backend processing of + // customer feedback. Virtual so tests can mock it for verification. + virtual void LogProxyState(bool enabled, bool restricted, bool at_startup); + + // Virtualized for mocking. Records UMA containing the result of requesting + // the probe URL. + virtual void RecordProbeURLFetchResult(ProbeURLFetchResult result); + + // Virtualized for mocking. Returns the list of network interfaces in use. + // |interfaces| can be null. + virtual void GetNetworkList(net::NetworkInterfaceList* interfaces, + int policy); + + private: + friend class DataReductionProxyConfigTest; + FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigTest, + TestOnIPAddressChanged); + FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigTest, TestSetProxyConfigs); + FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigTest, + TestSetProxyConfigsHoldback); + + // net::URLFetcherDelegate: + void OnURLFetchComplete(const net::URLFetcher* source) override; + + // NetworkChangeNotifier::IPAddressObserver: + void OnIPAddressChanged() override; + + // Adds the default proxy bypass rules for the Data Reduction Proxy. + void AddDefaultProxyBypassRules(); + + // Requests the proxy probe URL, if one is set. If unable to do so, disables + // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe + // failure. + void ProbeWhetherDataReductionProxyIsAvailable(); + + // Disables use of the Data Reduction Proxy on VPNs. Returns true if the + // Data Reduction Proxy has been disabled. + bool DisableIfVPN(); + + bool restricted_by_carrier_; + bool disabled_on_vpn_; + bool unreachable_; + bool enabled_by_user_; + bool alternative_enabled_by_user_; + + // Contains the configuration data being used. + scoped_ptr params_; + + // |io_task_runner_| should be the task runner for running operations on the + // IO thread. + scoped_refptr io_task_runner_; + + // The caller must ensure that the |net_log_|, if set, outlives this instance. + // It is used to create new instances of |bound_net_log_| on canary + // requests. |bound_net_log_| permits the correlation of the begin and end + // phases of a given canary request, and a new one is created for each + // canary check (with |net_log_| as a parameter). + net::NetLog* net_log_; + net::BoundNetLog bound_net_log_; + + // Used to retrieve a URLRequestContext for performing the canary check. + net::URLRequestContextGetter* url_request_context_getter_; + + // The caller must ensure that the |configurator_| outlives this instance. + DataReductionProxyConfigurator* configurator_; + + // The caller must ensure that the |event_store_| outlives this instance. + DataReductionProxyEventStore* event_store_; + + base::ThreadChecker thread_checker_; + + // The URLFetcher being used for the canary check. + scoped_ptr fetcher_; + + DISALLOW_COPY_AND_ASSIGN(DataReductionProxyConfig); +}; + +} // namespace data_reduction_proxy + +#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIG_H_ diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.cc new file mode 100644 index 000000000000..03ba9d6b0004 --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.cc @@ -0,0 +1,54 @@ +// Copyright 2015 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/browser/data_reduction_proxy_config_test_utils.h" + +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" +#include "net/base/net_util.h" +#include "net/url_request/test_url_fetcher_factory.h" +#include "net/url_request/url_request_test_util.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace data_reduction_proxy { + +MockDataReductionProxyConfig::MockDataReductionProxyConfig() + : MockDataReductionProxyConfig(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) { +} + +MockDataReductionProxyConfig::MockDataReductionProxyConfig(int flags) + : DataReductionProxyConfig( + scoped_ptr( + new TestDataReductionProxyParams( + flags, + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)) + .Pass()) { + network_interfaces_.reset(new net::NetworkInterfaceList()); +} + +MockDataReductionProxyConfig::~MockDataReductionProxyConfig() { +} + +void MockDataReductionProxyConfig::SetProxyConfigs(bool enabled, + bool alternative_enabled, + bool restricted, + bool at_startup) { + EXPECT_CALL(*this, LogProxyState(enabled, restricted, at_startup)).Times(1); + DataReductionProxyConfig::SetProxyConfigs(enabled, alternative_enabled, + restricted, at_startup); +} + +void MockDataReductionProxyConfig::GetNetworkList( + net::NetworkInterfaceList* interfaces, + int policy) { + if (!network_interfaces_.get()) + return; + for (size_t i = 0; i < network_interfaces_->size(); ++i) + interfaces->push_back(network_interfaces_->at(i)); +} + +} // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h new file mode 100644 index 000000000000..e28ee88db4ca --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h @@ -0,0 +1,49 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIG_TEST_UTILS_H_ +#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIG_TEST_UTILS_H_ + +#include "base/memory/scoped_ptr.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h" +#include "net/base/net_util.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace net { +class URLFetcher; +} + +namespace data_reduction_proxy { + +class MockDataReductionProxyConfig : public DataReductionProxyConfig { + public: + MockDataReductionProxyConfig(); + MockDataReductionProxyConfig(int flags); + ~MockDataReductionProxyConfig() override; + + MOCK_METHOD0(GetURLFetcherForProbe, net::URLFetcher*()); + MOCK_METHOD1(RecordProbeURLFetchResult, void(ProbeURLFetchResult result)); + MOCK_METHOD3(LogProxyState, + void(bool enabled, bool restricted, bool at_startup)); + + // SetProxyConfigs should always call LogProxyState exactly once. + virtual void SetProxyConfigs(bool enabled, + bool alternative_enabled, + bool restricted, + bool at_startup) override; + + virtual void GetNetworkList(net::NetworkInterfaceList* interfaces, + int policy) override; + + net::NetworkInterfaceList* interfaces() { + return network_interfaces_.get(); + } + + private: + scoped_ptr network_interfaces_; +}; + +} // namespace data_reduction_proxy + +#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIG_TEST_UTILS_H_ diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc new file mode 100644 index 000000000000..024ebd04f047 --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config_unittest.cc @@ -0,0 +1,293 @@ +// Copyright 2015 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/browser/data_reduction_proxy_config.h" + +#include "base/command_line.h" +#include "base/message_loop/message_loop.h" +#include "base/test/test_simple_task_runner.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" +#include "net/base/capturing_net_log.h" +#include "net/url_request/test_url_fetcher_factory.h" +#include "net/url_request/url_request_test_util.h" + +using testing::_; +using testing::AnyNumber; +using testing::Return; + +namespace { + +const char kProbeURLWithOKResponse[] = "http://ok.org/"; +const char kProbeURLWithBadResponse[] = "http://bad.org/"; + +} // namespace + +namespace data_reduction_proxy { + +class DataReductionProxyConfigTest : public testing::Test { + public: + static void AddTestProxyToCommandLine(); + + DataReductionProxyConfigTest() {} + ~DataReductionProxyConfigTest() override {} + + void SetUp() override { + task_runner_ = new base::TestSimpleTaskRunner(); + request_context_ = new net::TestURLRequestContextGetter(task_runner_.get()); + event_store_.reset(new DataReductionProxyEventStore(task_runner_.get())); + configurator_.reset(new TestDataReductionProxyConfigurator( + task_runner_.get(), &net_log_, event_store_.get())); + + ResetSettings(true, true, false, true, false); + + expected_params_.reset(new TestDataReductionProxyParams( + DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed, + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)); + } + + void ResetSettings(bool allowed, + bool fallback_allowed, + bool alt_allowed, + bool promo_allowed, + bool holdback) { + int flags = 0; + if (allowed) + flags |= DataReductionProxyParams::kAllowed; + if (fallback_allowed) + flags |= DataReductionProxyParams::kFallbackAllowed; + if (alt_allowed) + flags |= DataReductionProxyParams::kAlternativeAllowed; + if (promo_allowed) + flags |= DataReductionProxyParams::kPromoAllowed; + if (holdback) + flags |= DataReductionProxyParams::kHoldback; + config_.reset(new MockDataReductionProxyConfig(flags)); + MockDataReductionProxyConfig* config = + static_cast(config_.get()); + EXPECT_CALL(*config, GetURLFetcherForProbe()).Times(0); + EXPECT_CALL(*config, LogProxyState(_, _, _)).Times(0); + config_->InitDataReductionProxyConfig( + task_runner_.get(), &net_log_, request_context_.get(), + configurator_.get(), event_store_.get()); + } + + void SetProbeResult(const std::string& test_url, + const std::string& response, + ProbeURLFetchResult result, + bool success, + int expected_calls) { + if (0 == expected_calls) { + EXPECT_CALL(*config(), GetURLFetcherForProbe()).Times(0); + EXPECT_CALL(*config(), RecordProbeURLFetchResult(_)).Times(0); + } else { + EXPECT_CALL(*config(), RecordProbeURLFetchResult(result)).Times(1); + EXPECT_CALL(*config(), GetURLFetcherForProbe()) + .Times(expected_calls) + .WillRepeatedly(Return(new net::FakeURLFetcher( + GURL(test_url), config(), response, + success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, + success ? net::URLRequestStatus::SUCCESS + : net::URLRequestStatus::FAILED))); + } + } + + void CheckProxyConfigs(bool expected_enabled, + bool expected_restricted, + bool expected_fallback_restricted) { + ASSERT_EQ(expected_restricted, configurator()->restricted()); + ASSERT_EQ(expected_fallback_restricted, + configurator()->fallback_restricted()); + ASSERT_EQ(expected_enabled, configurator()->enabled()); + } + + void CheckProbeOnIPChange(const std::string& probe_url, + const std::string& response, + bool request_succeeded, + bool expected_restricted, + bool expected_fallback_restricted) { + SetProbeResult(probe_url, response, + FetchResult(!config_->restricted_by_carrier_, + request_succeeded && (response == "OK")), + request_succeeded, 1); + config_->OnIPAddressChanged(); + base::MessageLoop::current()->RunUntilIdle(); + CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted); + } + + ProbeURLFetchResult FetchResult(bool enabled, bool success) { + if (enabled) { + if (success) + return SUCCEEDED_PROXY_ALREADY_ENABLED; + return FAILED_PROXY_DISABLED; + } + if (success) + return SUCCEEDED_PROXY_ENABLED; + return FAILED_PROXY_ALREADY_DISABLED; + } + + void CheckInitDataReductionProxy(bool enabled_at_startup) { + config_->InitDataReductionProxyConfig( + task_runner_.get(), &net_log_, request_context_.get(), + configurator_.get(), event_store_.get()); + + base::MessageLoop::current()->RunUntilIdle(); + } + + MockDataReductionProxyConfig* config() { return config_.get(); } + + TestDataReductionProxyConfigurator* configurator() { + return configurator_.get(); + } + + TestDataReductionProxyParams* params() { return expected_params_.get(); } + + private: + scoped_refptr task_runner_; + scoped_refptr request_context_; + scoped_ptr configurator_; + scoped_ptr config_; + scoped_ptr expected_params_; + base::Time last_update_time_; + net::CapturingNetLog net_log_; + scoped_ptr event_store_; +}; + +TEST_F(DataReductionProxyConfigTest, TestGetDataReductionProxyOrigin) { + std::string result = config()->params()->origin().spec(); + EXPECT_EQ(GURL(params()->DefaultOrigin()), GURL(result)); +} + +TEST_F(DataReductionProxyConfigTest, TestGetDataReductionProxyDevOrigin) { + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( + switches::kDataReductionProxyDev, params()->DefaultDevOrigin()); + ResetSettings(true, true, false, true, false); + std::string result = config()->params()->origin().spec(); + EXPECT_EQ(GURL(params()->DefaultDevOrigin()), GURL(result)); +} + +TEST_F(DataReductionProxyConfigTest, TestGetDataReductionProxies) { + DataReductionProxyParams::DataReductionProxyList proxies = + params()->GetAllowedProxies(); + + unsigned int expected_proxy_size = 2u; + EXPECT_EQ(expected_proxy_size, proxies.size()); + + net::HostPortPair expected_origin = + net::HostPortPair::FromURL(GURL(params()->DefaultOrigin())); + net::HostPortPair expected_fallback_origin = + net::HostPortPair::FromURL(GURL(params()->DefaultFallbackOrigin())); + EXPECT_EQ(expected_origin.host(), proxies[0].host()); + EXPECT_EQ(expected_origin.port(), proxies[0].EffectiveIntPort()); + EXPECT_EQ(expected_fallback_origin.host(), proxies[1].host()); + EXPECT_EQ(expected_fallback_origin.port(), proxies[1].EffectiveIntPort()); +} + +TEST_F(DataReductionProxyConfigTest, TestSetProxyConfigs) { + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( + switches::kDataReductionProxyAlt, params()->DefaultAltOrigin()); + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( + switches::kDataReductionProxyAltFallback, + params()->DefaultAltFallbackOrigin()); + base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( + switches::kDataReductionSSLProxy, params()->DefaultSSLOrigin()); + ResetSettings(true, true, true, true, false); + + config()->SetProxyConfigs(true, true, false, false); + EXPECT_TRUE(configurator()->enabled()); + EXPECT_TRUE( + net::HostPortPair::FromString(params()->DefaultAltOrigin()) + .Equals(net::HostPortPair::FromString(configurator()->origin()))); + EXPECT_TRUE( + net::HostPortPair::FromString(params()->DefaultAltFallbackOrigin()) + .Equals(net::HostPortPair::FromString( + configurator()->fallback_origin()))); + EXPECT_TRUE( + net::HostPortPair::FromString(params()->DefaultSSLOrigin()) + .Equals(net::HostPortPair::FromString(configurator()->ssl_origin()))); + + config()->SetProxyConfigs(true, false, false, false); + EXPECT_TRUE(configurator()->enabled()); + EXPECT_TRUE( + net::HostPortPair::FromString(params()->DefaultOrigin()) + .Equals(net::HostPortPair::FromString(configurator()->origin()))); + EXPECT_TRUE(net::HostPortPair::FromString(params()->DefaultFallbackOrigin()) + .Equals(net::HostPortPair::FromString( + configurator()->fallback_origin()))); + EXPECT_EQ("", configurator()->ssl_origin()); + + config()->SetProxyConfigs(false, true, false, false); + EXPECT_FALSE(configurator()->enabled()); + EXPECT_EQ("", configurator()->origin()); + EXPECT_EQ("", configurator()->fallback_origin()); + EXPECT_EQ("", configurator()->ssl_origin()); + + config()->SetProxyConfigs(false, false, false, false); + EXPECT_FALSE(configurator()->enabled()); + EXPECT_EQ("", configurator()->origin()); + EXPECT_EQ("", configurator()->fallback_origin()); + EXPECT_EQ("", configurator()->ssl_origin()); +} + +TEST_F(DataReductionProxyConfigTest, TestSetProxyConfigsHoldback) { + ResetSettings(true, true, true, true, true); + + config()->SetProxyConfigs(true, true, false, false); + EXPECT_FALSE(configurator()->enabled()); + EXPECT_EQ("", configurator()->origin()); + EXPECT_EQ("", configurator()->fallback_origin()); + EXPECT_EQ("", configurator()->ssl_origin()); +} + +TEST_F(DataReductionProxyConfigTest, TestOnIPAddressChanged) { + base::MessageLoopForUI loop; + // The proxy is enabled initially. + config()->enabled_by_user_ = true; + config()->restricted_by_carrier_ = false; + config()->SetProxyConfigs(true, false, false, true); + // IP address change triggers a probe that succeeds. Proxy remains + // unrestricted. + CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false, false); + // IP address change triggers a probe that fails. Proxy is restricted. + CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true, false); + // IP address change triggers a probe that fails. Proxy remains restricted. + CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true, false); + // IP address change triggers a probe that succeeds. Proxy is unrestricted. + CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false, false); + // Simulate a VPN connection. The proxy should be disabled. + config()->interfaces()->clear(); + config()->interfaces()->push_back(net::NetworkInterface( + "tun0", /* network interface name */ + "tun0", /* network interface friendly name */ + 0, /* interface index */ + net::NetworkChangeNotifier::CONNECTION_WIFI, + net::IPAddressNumber(), /* IP address */ + 0, /* network prefix */ + net::IP_ADDRESS_ATTRIBUTE_NONE /* ip address attribute */ + )); + config()->OnIPAddressChanged(); + base::MessageLoop::current()->RunUntilIdle(); + CheckProxyConfigs(false, false, false); + + // Check that the proxy is re-enabled if a non-VPN connection is later used. + config()->interfaces()->clear(); + config()->interfaces()->push_back(net::NetworkInterface( + "eth0", /* network interface name */ + "eth0", /* network interface friendly name */ + 0, /* interface index */ + net::NetworkChangeNotifier::CONNECTION_WIFI, net::IPAddressNumber(), + 0, /* network prefix */ + net::IP_ADDRESS_ATTRIBUTE_NONE /* ip address attribute */ + )); + CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false, false); +} + +} // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h index 8c4f5cef019f..a9a4255a8d23 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h @@ -83,7 +83,7 @@ class DataReductionProxyConfigurator { const net::ProxyConfig& GetProxyConfigOnIOThread() const; private: - FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigTest, TestBypassList); + FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfiguratorTest, TestBypassList); // Used for updating the proxy config on the IO thread. scoped_refptr network_task_runner_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.cc new file mode 100644 index 000000000000..a86845f904f2 --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.cc @@ -0,0 +1,45 @@ +// Copyright 2015 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/browser/data_reduction_proxy_configurator_test_utils.h" + +namespace data_reduction_proxy { + +TestDataReductionProxyConfigurator::TestDataReductionProxyConfigurator( + scoped_refptr network_task_runner, + net::NetLog* net_log, + DataReductionProxyEventStore* event_store) + : DataReductionProxyConfigurator(network_task_runner, net_log, event_store), + enabled_(false), + restricted_(false), + fallback_restricted_(false) { +} + +TestDataReductionProxyConfigurator::~TestDataReductionProxyConfigurator() { +} + +void TestDataReductionProxyConfigurator::Enable( + bool restricted, + bool fallback_restricted, + const std::string& primary_origin, + const std::string& fallback_origin, + const std::string& ssl_origin) { + enabled_ = true; + restricted_ = restricted; + fallback_restricted_ = fallback_restricted; + origin_ = primary_origin; + fallback_origin_ = fallback_origin; + ssl_origin_ = ssl_origin; +} + +void TestDataReductionProxyConfigurator::Disable() { + enabled_ = false; + restricted_ = false; + fallback_restricted_ = false; + origin_ = std::string(); + fallback_origin_ = std::string(); + ssl_origin_ = std::string(); +} + +} // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h new file mode 100644 index 000000000000..5b0a82c9e76b --- /dev/null +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h @@ -0,0 +1,90 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIGURATOR_TEST_UTILS_H_ +#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIGURATOR_TEST_UTILS_H_ + +#include + +#include "base/memory/ref_counted.h" +#include "base/sequenced_task_runner.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h" + +namespace net { +class NetLog; +} + +namespace data_reduction_proxy { + +class DataReductionProxyEventStore; + +class TestDataReductionProxyConfigurator + : public DataReductionProxyConfigurator { + public: + TestDataReductionProxyConfigurator( + scoped_refptr network_task_runner, + net::NetLog* net_log, + DataReductionProxyEventStore* event_store); + ~TestDataReductionProxyConfigurator() override; + + // Overrides of DataReductionProxyConfigurator + void Enable(bool restricted, + bool fallback_restricted, + const std::string& primary_origin, + const std::string& fallback_origin, + const std::string& ssl_origin) override; + + void Disable() override; + + void AddHostPatternToBypass(const std::string& pattern) override {} + + void AddURLPatternToBypass(const std::string& pattern) override {} + + bool enabled() const { + return enabled_; + } + + bool restricted() const { + return restricted_; + } + + bool fallback_restricted() const { + return fallback_restricted_; + } + + const std::string& origin() const { + return origin_; + } + + const std::string& fallback_origin() const { + return fallback_origin_; + } + + const std::string& ssl_origin() const { + return ssl_origin_; + } + + private: + // True if the proxy has been enabled, i.e., only after |Enable| has been + // called. Defaults to false. + bool enabled_; + + // Describes whether the proxy has been put in a restricted mode. True if + // |Enable| is called with |restricted| set to true. Defaults to false. + bool restricted_; + + // Describes whether the proxy has been put in a mode where the fallback + // configuration has been disallowed. True if |Enable| is called with + // |fallback_restricted| set to true. Defaults to false. + bool fallback_restricted_; + + // The origins that are passed to |Enable|. + std::string origin_; + std::string fallback_origin_; + std::string ssl_origin_; +}; + +} // namespace data_reduction_proxy + +#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_CONFIGURATOR_TEST_UTILS_H_ diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_unittest.cc index 9b431a955956..eaa4b63a065c 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_unittest.cc @@ -17,7 +17,7 @@ namespace data_reduction_proxy { -class DataReductionProxyConfigTest : public testing::Test { +class DataReductionProxyConfiguratorTest : public testing::Test { public: void SetUp() override { task_runner_ = new base::TestSimpleTaskRunner(); @@ -54,7 +54,7 @@ class DataReductionProxyConfigTest : public testing::Test { data_reduction_proxy_event_store_; }; -TEST_F(DataReductionProxyConfigTest, TestUnrestricted) { +TEST_F(DataReductionProxyConfiguratorTest, TestUnrestricted) { config_->Enable(false, false, "https://www.foo.com:443/", @@ -66,7 +66,7 @@ TEST_F(DataReductionProxyConfigTest, TestUnrestricted) { "", ""); } -TEST_F(DataReductionProxyConfigTest, TestUnrestrictedSSL) { +TEST_F(DataReductionProxyConfiguratorTest, TestUnrestrictedSSL) { config_->Enable(false, false, "https://www.foo.com:443/", @@ -79,7 +79,7 @@ TEST_F(DataReductionProxyConfigTest, TestUnrestrictedSSL) { ""); } -TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithBypassRule) { +TEST_F(DataReductionProxyConfiguratorTest, TestUnrestrictedWithBypassRule) { config_->AddHostPatternToBypass(""); config_->AddHostPatternToBypass("*.goo.com"); config_->Enable(false, @@ -93,13 +93,13 @@ TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithBypassRule) { ";*.goo.com;"); } -TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithoutFallback) { +TEST_F(DataReductionProxyConfiguratorTest, TestUnrestrictedWithoutFallback) { config_->Enable(false, false, "https://www.foo.com:443/", "", ""); CheckProxyConfig(net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, "HTTPS www.foo.com:443;DIRECT", "", ""); } -TEST_F(DataReductionProxyConfigTest, TestRestricted) { +TEST_F(DataReductionProxyConfiguratorTest, TestRestricted) { config_->Enable(true, false, "https://www.foo.com:443/", @@ -109,7 +109,7 @@ TEST_F(DataReductionProxyConfigTest, TestRestricted) { "PROXY www.bar.com:80;DIRECT", "", ""); } -TEST_F(DataReductionProxyConfigTest, TestFallbackRestricted) { +TEST_F(DataReductionProxyConfiguratorTest, TestFallbackRestricted) { config_->Enable(false, true, "https://www.foo.com:443/", @@ -119,7 +119,7 @@ TEST_F(DataReductionProxyConfigTest, TestFallbackRestricted) { "HTTPS www.foo.com:443;DIRECT", "", ""); } -TEST_F(DataReductionProxyConfigTest, TestDisable) { +TEST_F(DataReductionProxyConfiguratorTest, TestDisable) { data_reduction_proxy::DataReductionProxyParams params( data_reduction_proxy::DataReductionProxyParams:: kAllowAllProxyConfigurations); @@ -132,7 +132,7 @@ TEST_F(DataReductionProxyConfigTest, TestDisable) { CheckProxyConfig(net::ProxyConfig::ProxyRules::TYPE_NO_RULES, "", "", ""); } -TEST_F(DataReductionProxyConfigTest, TestBypassList) { +TEST_F(DataReductionProxyConfiguratorTest, TestBypassList) { config_->AddHostPatternToBypass("http://www.google.com"); config_->AddHostPatternToBypass("fefe:13::abc/33"); config_->AddURLPatternToBypass("foo.org/images/*"); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc index d577fcee4471..26de12c3044d 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor_unittest.cc @@ -181,12 +181,12 @@ class DataReductionProxyInterceptorWithServerTest : public testing::Test { ASSERT_TRUE(direct_.InitializeAndWaitUntilReady()); // Owned by settings_. - TestDataReductionProxyParams* params = - new TestDataReductionProxyParams( - DataReductionProxyParams::kAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & + scoped_ptr params; + params.reset(new TestDataReductionProxyParams( + DataReductionProxyParams::kAllowed, + TestDataReductionProxyParams::HAS_EVERYTHING & ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN); + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)); params->set_origin(proxy_.GetURL("/")); std::string proxy_name = net::HostPortPair::FromURL(GURL(params->origin())).ToString(); @@ -196,7 +196,7 @@ class DataReductionProxyInterceptorWithServerTest : public testing::Test { context_.set_proxy_service(proxy_service_.get()); - settings_.reset(new DataReductionProxySettings(params)); + settings_.reset(new DataReductionProxySettings(params.Pass())); io_data_.reset( new DataReductionProxyIOData( data_reduction_proxy::Client::UNKNOWN, diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc index 8aa47d8a57fe..641f625974ab 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data_unittest.cc @@ -54,13 +54,13 @@ namespace data_reduction_proxy { class DataReductionProxyIODataTest : public testing::Test { public: void SetUp() override { - DataReductionProxyParams* params = - new TestDataReductionProxyParams( - TestDataReductionProxyParams::kAllowed, - TestDataReductionProxyParams::HAS_EVERYTHING & + scoped_ptr params; + params.reset(new TestDataReductionProxyParams( + TestDataReductionProxyParams::kAllowed, + TestDataReductionProxyParams::HAS_EVERYTHING & ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN); - settings_.reset(new DataReductionProxySettings(params)); + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)); + settings_.reset(new DataReductionProxySettings(params.Pass())); RegisterSimpleProfilePrefs(prefs_.registry()); } diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc index 6db23912782f..88aa3176d0f6 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.cc @@ -90,7 +90,7 @@ bool IsEnabledOnCommandLine() { namespace data_reduction_proxy { DataReductionProxySettings::DataReductionProxySettings( - DataReductionProxyParams* params) + scoped_ptr params) : restricted_by_carrier_(false), enabled_by_user_(false), disabled_on_vpn_(false), @@ -100,12 +100,12 @@ DataReductionProxySettings::DataReductionProxySettings( net_log_(NULL), event_store_(NULL), configurator_(NULL) { - DCHECK(params); - params_.reset(params); + DCHECK(params.get()); + config_.reset(new DataReductionProxyConfig(params.Pass())); } DataReductionProxySettings::~DataReductionProxySettings() { - if (params_->allowed()) + if (params()->allowed()) spdy_proxy_auth_enabled_.Destroy(); net::NetworkChangeNotifier::RemoveIPAddressObserver(this); } @@ -142,7 +142,7 @@ void DataReductionProxySettings::InitDataReductionProxySettings( RecordDataReductionInit(); // Disable the proxy if it is not allowed to be used. - if (!params_->allowed()) + if (!params()->allowed()) return; AddDefaultProxyBypassRules(); @@ -182,7 +182,7 @@ bool DataReductionProxySettings::IsDataReductionProxyManaged() { void DataReductionProxySettings::SetDataReductionProxyEnabled(bool enabled) { DCHECK(thread_checker_.CalledOnValidThread()); // Prevent configuring the proxy when it is not allowed to be used. - if (!params_->allowed()) + if (!params()->allowed()) return; if (spdy_proxy_auth_enabled_.GetValue() != enabled) { @@ -195,7 +195,7 @@ void DataReductionProxySettings::SetDataReductionProxyAlternativeEnabled( bool enabled) { DCHECK(thread_checker_.CalledOnValidThread()); // Prevent configuring the proxy when it is not allowed to be used. - if (!params_->alternative_allowed()) + if (!params()->alternative_allowed()) return; if (data_reduction_proxy_alternative_enabled_.GetValue() != enabled) { data_reduction_proxy_alternative_enabled_.SetValue(enabled); @@ -349,12 +349,12 @@ void DataReductionProxySettings::LogProxyState( void DataReductionProxySettings::OnIPAddressChanged() { DCHECK(thread_checker_.CalledOnValidThread()); if (enabled_by_user_) { - DCHECK(params_->allowed()); + DCHECK(params()->allowed()); RecordNetworkChangeEvent(IP_CHANGED); if (DisableIfVPN()) return; if (IsDataReductionProxyAlternativeEnabled() && - !params_->alternative_fallback_allowed()) { + !params()->alternative_fallback_allowed()) { return; } ProbeWhetherDataReductionProxyIsAvailable(); @@ -365,14 +365,14 @@ void DataReductionProxySettings::OnProxyEnabledPrefChange() { DCHECK(thread_checker_.CalledOnValidThread()); if (!on_data_reduction_proxy_enabled_.is_null()) on_data_reduction_proxy_enabled_.Run(IsDataReductionProxyEnabled()); - if (!params_->allowed()) + if (!params()->allowed()) return; MaybeActivateDataReductionProxy(false); } void DataReductionProxySettings::OnProxyAlternativeEnabledPrefChange() { DCHECK(thread_checker_.CalledOnValidThread()); - if (!params_->alternative_allowed()) + if (!params()->alternative_allowed()) return; MaybeActivateDataReductionProxy(false); } @@ -418,7 +418,7 @@ void DataReductionProxySettings::MaybeActivateDataReductionProxy( // Check if the proxy has been restricted explicitly by the carrier. if (enabled_by_user_ && !disabled_on_vpn_ && !(IsDataReductionProxyAlternativeEnabled() && - !params_->alternative_fallback_allowed())) { + !params()->alternative_fallback_allowed())) { ProbeWhetherDataReductionProxyIsAvailable(); } } @@ -433,18 +433,18 @@ void DataReductionProxySettings::SetProxyConfigs(bool enabled, LogProxyState(enabled, restricted, at_startup); // The alternative is only configured if the standard configuration is // is enabled. - if (enabled & !params_->holdback()) { + if (enabled & !params()->holdback()) { if (alternative_enabled) { configurator_->Enable(restricted, - !params_->alternative_fallback_allowed(), - params_->alt_origin().spec(), + !params()->alternative_fallback_allowed(), + params()->alt_origin().spec(), std::string(), - params_->ssl_origin().spec()); + params()->ssl_origin().spec()); } else { configurator_->Enable(restricted, - !params_->fallback_allowed(), - params_->origin().spec(), - params_->fallback_origin().spec(), + !params()->fallback_allowed(), + params()->origin().spec(), + params()->fallback_origin().spec(), std::string()); } } else { @@ -456,7 +456,7 @@ void DataReductionProxySettings::SetProxyConfigs(bool enabled, void DataReductionProxySettings::RecordDataReductionInit() { DCHECK(thread_checker_.CalledOnValidThread()); ProxyStartupState state = PROXY_NOT_AVAILABLE; - if (params_->allowed()) { + if (params()->allowed()) { if (IsDataReductionProxyEnabled()) state = PROXY_ENABLED; else @@ -485,11 +485,6 @@ void DataReductionProxySettings::GetNetworkList( net::GetNetworkList(interfaces, policy); } -void DataReductionProxySettings::ResetParamsForTest( - DataReductionProxyParams* params) { - params_.reset(params); -} - DataReductionProxySettings::ContentLengthList DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) { DCHECK(thread_checker_.CalledOnValidThread()); @@ -560,7 +555,7 @@ net::URLFetcher* DataReductionProxySettings::GetBaseURLFetcher( net::URLFetcher* DataReductionProxySettings::GetURLFetcherForAvailabilityCheck() { - return GetBaseURLFetcher(params_->probe_url(), + return GetBaseURLFetcher(params()->probe_url(), net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY); } diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h index 3608f97bd12a..911668705fed 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h @@ -14,6 +14,7 @@ #include "base/memory/scoped_ptr.h" #include "base/prefs/pref_member.h" #include "base/threading/thread_checker.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" @@ -56,32 +57,6 @@ enum ProxyStartupState { PROXY_STARTUP_STATE_COUNT, }; -// Values of the UMA DataReductionProxy.ProbeURL histogram. -// This enum must remain synchronized with -// DataReductionProxyProbeURLFetchResult in metrics/histograms/histograms.xml. -// TODO(marq): Rename these histogram buckets with s/DISABLED/RESTRICTED/, so -// their names match the behavior they track. -enum ProbeURLFetchResult { - // The probe failed because the Internet was disconnected. - INTERNET_DISCONNECTED = 0, - - // The probe failed for any other reason, and as a result, the proxy was - // disabled. - FAILED_PROXY_DISABLED, - - // The probe failed, but the proxy was already restricted. - FAILED_PROXY_ALREADY_DISABLED, - - // The probe succeeded, and as a result the proxy was restricted. - SUCCEEDED_PROXY_ENABLED, - - // The probe succeeded, but the proxy was already restricted. - SUCCEEDED_PROXY_ALREADY_ENABLED, - - // This must always be last. - PROBE_URL_FETCH_RESULT_COUNT -}; - // Central point for configuring the data reduction proxy. // This object lives on the UI thread and all of its methods are expected to // be called from there. @@ -95,11 +70,11 @@ class DataReductionProxySettings static bool IsProxyKeySetOnCommandLine(); - DataReductionProxySettings(DataReductionProxyParams* params); + DataReductionProxySettings(scoped_ptr params); ~DataReductionProxySettings() override; DataReductionProxyParams* params() const { - return params_.get(); + return config_->params(); } // Initializes the data reduction proxy with profile and local state prefs, @@ -236,9 +211,6 @@ class DataReductionProxySettings return configurator_; } - // Reset params for tests. - void ResetParamsForTest(DataReductionProxyParams* params); - private: friend class DataReductionProxySettingsTestBase; friend class DataReductionProxySettingsTest; @@ -329,7 +301,7 @@ class DataReductionProxySettings base::ThreadChecker thread_checker_; - scoped_ptr params_; + scoped_ptr config_; DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); }; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc index 18f87408f5b7..053b0bcde26d 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.cc @@ -12,6 +12,7 @@ #include "base/strings/string_number_conversions.h" #include "base/test/test_simple_task_runner.h" #include "base/time/time.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" @@ -33,6 +34,8 @@ const char kProxy[] = "proxy"; namespace data_reduction_proxy { +namespace { + ProbeURLFetchResult FetchResult(bool enabled, bool success) { if (enabled) { if (success) @@ -44,41 +47,7 @@ ProbeURLFetchResult FetchResult(bool enabled, bool success) { return FAILED_PROXY_ALREADY_DISABLED; } -TestDataReductionProxyConfig::TestDataReductionProxyConfig( - scoped_refptr network_task_runner, - net::NetLog* net_log, - data_reduction_proxy::DataReductionProxyEventStore* event_store) - : DataReductionProxyConfigurator(network_task_runner, net_log, event_store), - enabled_(false), - restricted_(false), - fallback_restricted_(false) { -} - -TestDataReductionProxyConfig::~TestDataReductionProxyConfig() { -} - -void TestDataReductionProxyConfig::Enable( - bool restricted, - bool fallback_restricted, - const std::string& primary_origin, - const std::string& fallback_origin, - const std::string& ssl_origin) { - enabled_ = true; - restricted_ = restricted; - fallback_restricted_ = fallback_restricted; - origin_ = primary_origin; - fallback_origin_ = fallback_origin; - ssl_origin_ = ssl_origin; -} - -void TestDataReductionProxyConfig::Disable() { - enabled_ = false; - restricted_ = false; - fallback_restricted_ = false; - origin_ = ""; - fallback_origin_ = ""; - ssl_origin_ = ""; -} +} // namespace DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() : testing::Test() { @@ -161,7 +130,7 @@ void DataReductionProxySettingsTestBase::ResetSettings(bool allowed, EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0); EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); settings_.reset(settings); - configurator_.reset(new TestDataReductionProxyConfig( + configurator_.reset(new TestDataReductionProxyConfigurator( scoped_refptr( new base::TestSimpleTaskRunner()), &net_log_, event_store_.get())); settings_->configurator_ = configurator_.get(); @@ -216,11 +185,12 @@ void DataReductionProxySettingsTestBase::CheckProxyConfigs( bool expected_enabled, bool expected_restricted, bool expected_fallback_restricted) { - TestDataReductionProxyConfig* config = - static_cast(settings_->configurator_); - ASSERT_EQ(expected_restricted, config->restricted_); - ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); - ASSERT_EQ(expected_enabled, config->enabled_); + TestDataReductionProxyConfigurator* config = + static_cast( + settings_->configurator_); + ASSERT_EQ(expected_restricted, config->restricted()); + ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted()); + ASSERT_EQ(expected_enabled, config->enabled()); } void DataReductionProxySettingsTestBase::CheckProbe( @@ -291,7 +261,7 @@ void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( bool enabled_at_startup) { base::MessageLoopForUI loop; scoped_ptr configurator( - new TestDataReductionProxyConfig( + new TestDataReductionProxyConfigurator( scoped_refptr( new base::TestSimpleTaskRunner()), &net_log_, event_store_.get())); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h index ccd8170f8780..c364706e8cde 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h @@ -5,10 +5,8 @@ #ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ - #include "base/memory/scoped_ptr.h" #include "base/prefs/testing_pref_service.h" -#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" #include "net/base/capturing_net_log.h" @@ -23,59 +21,27 @@ class TestingPrefServiceSimple; namespace data_reduction_proxy { +class DataReductionProxyConfigurator; class DataReductionProxyStatisticsPrefs; -class TestDataReductionProxyConfig : public DataReductionProxyConfigurator { - public: - TestDataReductionProxyConfig( - scoped_refptr network_task_runner, - net::NetLog* net_log, - data_reduction_proxy::DataReductionProxyEventStore* event_store); - ~TestDataReductionProxyConfig() override; - void Enable(bool restricted, - bool fallback_restricted, - const std::string& primary_origin, - const std::string& fallback_origin, - const std::string& ssl_origin) override; - void Disable() override; - void AddHostPatternToBypass(const std::string& pattern) override {} - void AddURLPatternToBypass(const std::string& pattern) override {} - - // True if the proxy has been enabled, i.e., only after |Enable| has been - // called. Defaults to false. - bool enabled_; - - // Describes whether the proxy has been put in a restricted mode. True if - // |Enable| is called with |restricted| set to true. Defaults to false. - bool restricted_; - - // Describes whether the proxy has been put in a mode where the fallback - // configuration has been disallowed. True if |Enable| is called with - // |fallback_restricted| set to true. Defaults to false. - bool fallback_restricted_; - - // The origins that are passed to |Enable|. - std::string origin_; - std::string fallback_origin_; - std::string ssl_origin_; -}; - template class MockDataReductionProxySettings : public C { public: MockDataReductionProxySettings() : DataReductionProxySettings( - new TestDataReductionProxyParams( + scoped_ptr(new TestDataReductionProxyParams( DataReductionProxyParams::kAllowed | DataReductionProxyParams::kFallbackAllowed | DataReductionProxyParams::kPromoAllowed, TestDataReductionProxyParams::HAS_EVERYTHING & ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)) {} + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)).Pass()) {} MockDataReductionProxySettings(int flags) - : C(new TestDataReductionProxyParams(flags, - TestDataReductionProxyParams::HAS_EVERYTHING & - ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & - ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)) {} + : C(scoped_ptr( + new TestDataReductionProxyParams(flags, + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)) + .Pass()) {} MOCK_METHOD0(GetURLFetcherForAvailabilityCheck, net::URLFetcher*()); MOCK_METHOD0(GetOriginalProfilePrefs, PrefService*()); MOCK_METHOD0(GetLocalStatePrefs, PrefService*()); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc index 307fb5087d76..8519ebbbe767 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_unittest.cc @@ -9,6 +9,7 @@ #include "base/message_loop/message_loop.h" #include "base/strings/utf_string_conversions.h" #include "base/test/test_simple_task_runner.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" @@ -85,56 +86,56 @@ TEST_F(DataReductionProxySettingsTest, TestSetProxyConfigs) { base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( switches::kDataReductionSSLProxy, drp_params.DefaultSSLOrigin()); ResetSettings(true, true, true, true, false); - TestDataReductionProxyConfig* config = - static_cast( + TestDataReductionProxyConfigurator* configurator = + static_cast( settings_->configurator()); settings_->SetProxyConfigs(true, true, false, false); - EXPECT_TRUE(config->enabled_); + EXPECT_TRUE(configurator->enabled()); EXPECT_TRUE(net::HostPortPair::FromString( expected_params_->DefaultAltOrigin()).Equals( - net::HostPortPair::FromString(config->origin_))); + net::HostPortPair::FromString(configurator->origin()))); EXPECT_TRUE(net::HostPortPair::FromString( expected_params_->DefaultAltFallbackOrigin()).Equals( - net::HostPortPair::FromString(config->fallback_origin_))); + net::HostPortPair::FromString(configurator->fallback_origin()))); EXPECT_TRUE(net::HostPortPair::FromString( expected_params_->DefaultSSLOrigin()).Equals( - net::HostPortPair::FromString(config->ssl_origin_))); + net::HostPortPair::FromString(configurator->ssl_origin()))); settings_->SetProxyConfigs(true, false, false, false); - EXPECT_TRUE(config->enabled_); + EXPECT_TRUE(configurator->enabled()); EXPECT_TRUE(net::HostPortPair::FromString(drp_params.DefaultOrigin()).Equals( - net::HostPortPair::FromString(config->origin_))); + net::HostPortPair::FromString(configurator->origin()))); EXPECT_TRUE(net::HostPortPair::FromString( drp_params.DefaultFallbackOrigin()).Equals( - net::HostPortPair::FromString(config->fallback_origin_))); - EXPECT_EQ("", config->ssl_origin_); + net::HostPortPair::FromString(configurator->fallback_origin()))); + EXPECT_EQ("", configurator->ssl_origin()); settings_->SetProxyConfigs(false, true, false, false); - EXPECT_FALSE(config->enabled_); - EXPECT_EQ("", config->origin_); - EXPECT_EQ("", config->fallback_origin_); - EXPECT_EQ("", config->ssl_origin_); + EXPECT_FALSE(configurator->enabled()); + EXPECT_EQ("", configurator->origin()); + EXPECT_EQ("", configurator->fallback_origin()); + EXPECT_EQ("", configurator->ssl_origin()); settings_->SetProxyConfigs(false, false, false, false); - EXPECT_FALSE(config->enabled_); - EXPECT_EQ("", config->origin_); - EXPECT_EQ("", config->fallback_origin_); - EXPECT_EQ("", config->ssl_origin_); + EXPECT_FALSE(configurator->enabled()); + EXPECT_EQ("", configurator->origin()); + EXPECT_EQ("", configurator->fallback_origin()); + EXPECT_EQ("", configurator->ssl_origin()); } TEST_F(DataReductionProxySettingsTest, TestSetProxyConfigsHoldback) { ResetSettings(true, true, true, true, true); - TestDataReductionProxyConfig* config = - static_cast( + TestDataReductionProxyConfigurator* configurator = + static_cast( settings_->configurator()); // Holdback. settings_->SetProxyConfigs(true, true, false, false); - EXPECT_FALSE(config->enabled_); - EXPECT_EQ("", config->origin_); - EXPECT_EQ("", config->fallback_origin_); - EXPECT_EQ("", config->ssl_origin_); + EXPECT_FALSE(configurator->enabled()); + EXPECT_EQ("", configurator->origin()); + EXPECT_EQ("", configurator->fallback_origin()); + EXPECT_EQ("", configurator->ssl_origin()); } TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) { @@ -410,10 +411,10 @@ TEST_F(DataReductionProxySettingsTest, CheckInitMetricsWhenNotAllowed) { EXPECT_CALL(*settings, RecordStartupState(PROXY_NOT_AVAILABLE)); scoped_ptr configurator( - new TestDataReductionProxyConfig( + new TestDataReductionProxyConfigurator( scoped_refptr( - new base::TestSimpleTaskRunner()), &net_log_, - event_store_.get())); + new base::TestSimpleTaskRunner()), + &net_log_, event_store_.get())); settings_->SetProxyConfigurator(configurator.get()); scoped_refptr request_context = new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc index 360042176a44..786788482987 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats_unittest.cc @@ -71,7 +71,8 @@ class DataReductionProxyUsageStatsTest : public testing::Test { mock_url_request_ = context_.CreateRequest(GURL(), net::IDLE, &delegate_, NULL); settings_.reset(new DataReductionProxySettings( - new DataReductionProxyParamsMock())); + scoped_ptr( + new DataReductionProxyParamsMock()).Pass())); } scoped_ptr CreateURLRequestWithResponseHeaders( -- 2.11.4.GIT