diff options
36 files changed, 882 insertions, 614 deletions
diff --git a/components/data_reduction_proxy.gypi b/components/data_reduction_proxy.gypi index 8f5c4a0..1a8bf6d 100644 --- a/components/data_reduction_proxy.gypi +++ b/components/data_reduction_proxy.gypi @@ -147,6 +147,9 @@ 'data_reduction_proxy/core/common/data_reduction_proxy_client_config_parser.cc', 'data_reduction_proxy/core/common/data_reduction_proxy_client_config_parser.h', 'data_reduction_proxy/core/common/data_reduction_proxy_config_values.h', + 'data_reduction_proxy/core/common/data_reduction_proxy_event_creator.cc', + 'data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h', + 'data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h', 'data_reduction_proxy/core/common/data_reduction_proxy_event_store.cc', 'data_reduction_proxy/core/common/data_reduction_proxy_event_store.h', 'data_reduction_proxy/core/common/data_reduction_proxy_headers.cc', @@ -186,6 +189,8 @@ 'data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h', 'data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc', 'data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h', + 'data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.cc', + 'data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.h', 'data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.cc', 'data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.h', 'data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.cc', diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.cc index cd9c1c3..cdb3e30 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.cc @@ -8,7 +8,7 @@ #include "base/time/time.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" #include "net/base/load_flags.h" @@ -70,10 +70,10 @@ namespace data_reduction_proxy { DataReductionProxyBypassProtocol::DataReductionProxyBypassProtocol( DataReductionProxyConfig* config, - DataReductionProxyEventStore* event_store) - : config_(config), event_store_(event_store) { + DataReductionProxyEventCreator* event_creator) + : config_(config), event_creator_(event_creator) { DCHECK(config_); - DCHECK(event_store_); + DCHECK(event_creator_); net::NetworkChangeNotifier::AddIPAddressObserver(this); } @@ -128,10 +128,9 @@ bool DataReductionProxyBypassProtocol::MaybeBypassProxyAndPrepareToRetry( // command was sent via the data reduction proxy headers bool event_logged = false; DataReductionProxyInfo data_reduction_proxy_info; - DataReductionProxyBypassType bypass_type = - GetDataReductionProxyBypassType( - response_headers, request->url(), request->net_log(), - &data_reduction_proxy_info, event_store_, &event_logged); + DataReductionProxyBypassType bypass_type = GetDataReductionProxyBypassType( + response_headers, request->url(), request->net_log(), + &data_reduction_proxy_info, event_creator_, &event_logged); if (bypass_type == BYPASS_EVENT_TYPE_MISSING_VIA_HEADER_OTHER) { if (DataReductionProxyParams:: @@ -155,7 +154,7 @@ bool DataReductionProxyBypassProtocol::MaybeBypassProxyAndPrepareToRetry( return false; if (!event_logged) { - event_store_->AddBypassTypeEvent( + event_creator_->AddBypassTypeEvent( request->net_log(), bypass_type, request->url(), data_reduction_proxy_info.bypass_duration); } diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h index 44ffbc7..49111c8 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h @@ -18,7 +18,7 @@ class URLRequest; namespace data_reduction_proxy { class DataReductionProxyConfig; -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; // Class responsible for determining when a response should or should not cause // the data reduction proxy to be bypassed, and to what degree. Owned by the @@ -27,9 +27,10 @@ class DataReductionProxyBypassProtocol : public net::NetworkChangeNotifier::IPAddressObserver { public: // Constructs a DataReductionProxyBypassProtocol object. |config| and - // |event_store| must be non-NULL and outlive |this|. - DataReductionProxyBypassProtocol(DataReductionProxyConfig* config, - DataReductionProxyEventStore* event_store); + // |event_creator| must be non-NULL and outlive |this|. + DataReductionProxyBypassProtocol( + DataReductionProxyConfig* config, + DataReductionProxyEventCreator* event_creator); ~DataReductionProxyBypassProtocol() override; @@ -54,7 +55,7 @@ class DataReductionProxyBypassProtocol DataReductionProxyConfig* config_; // Must outlive |this|. - DataReductionProxyEventStore* event_store_; + DataReductionProxyEventCreator* event_creator_; // The set of data reduction proxies through which a response has come back // with the data reduction proxy via header since the last network change. 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 c638bdd..0934c3e 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 @@ -116,7 +116,7 @@ class DataReductionProxyProtocolTest : public testing::Test { DataReductionProxyInterceptor* interceptor = new DataReductionProxyInterceptor(test_context_->config(), bypass_stats_.get(), - test_context_->event_store()); + test_context_->event_creator()); scoped_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( new net::URLRequestJobFactoryImpl()); job_factory_.reset(new net::URLRequestInterceptingJobFactory( 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 index 989a4e7d..9b9ba22 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.cc @@ -6,13 +6,15 @@ #include <string> +#include "base/bind.h" +#include "base/bind_helpers.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_config_values.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" #include "net/base/load_flags.h" #include "net/proxy/proxy_server.h" @@ -103,7 +105,7 @@ DataReductionProxyConfig::DataReductionProxyConfig( net::NetLog* net_log, scoped_ptr<DataReductionProxyConfigValues> config_values, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store) + DataReductionProxyEventCreator* event_creator) : restricted_by_carrier_(false), disabled_on_vpn_(false), unreachable_(false), @@ -113,11 +115,11 @@ DataReductionProxyConfig::DataReductionProxyConfig( io_task_runner_(io_task_runner), net_log_(net_log), configurator_(configurator), - event_store_(event_store), + event_creator_(event_creator), url_request_context_getter_(nullptr) { DCHECK(io_task_runner); DCHECK(configurator); - DCHECK(event_store); + DCHECK(event_creator); } DataReductionProxyConfig::~DataReductionProxyConfig() { @@ -375,10 +377,9 @@ void DataReductionProxyConfig::HandleSecureProxyCheckResponse( int http_response_code) { DCHECK(io_task_runner_->BelongsToCurrentThread()); bool success_response = ("OK" == response.substr(0, 2)); - if (event_store_) { - event_store_->EndSecureProxyCheck(bound_net_log_, status.error(), - http_response_code, success_response); - } + if (event_creator_) + event_creator_->EndSecureProxyCheck(bound_net_log_, status.error(), + http_response_code, success_response); if (status.status() == net::URLRequestStatus::FAILED) { if (status.error() == net::ERR_INTERNET_DISCONNECTED) { @@ -486,9 +487,8 @@ void DataReductionProxyConfig::SecureProxyCheck( DCHECK(io_task_runner_->BelongsToCurrentThread()); bound_net_log_ = net::BoundNetLog::Make( net_log_, net::NetLog::SOURCE_DATA_REDUCTION_PROXY); - - if (event_store_) { - event_store_->BeginSecureProxyCheck( + if (event_creator_) { + event_creator_->BeginSecureProxyCheck( bound_net_log_, config_values_->secure_proxy_check_url()); } 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 index 23e67341..4499ee4 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_config.h @@ -44,7 +44,7 @@ typedef base::Callback<void(const std::string&, class DataReductionProxyConfigValues; class DataReductionProxyConfigurator; -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class DataReductionProxyService; class SecureProxyChecker; struct DataReductionProxyTypeInfo; @@ -82,14 +82,19 @@ class DataReductionProxyConfig : public net::NetworkChangeNotifier::IPAddressObserver { public: // The caller must ensure that all parameters remain alive for the lifetime - // of the |DataReductionProxyConfig| instance, with the exception of |params| - // which this instance will own. + // of the |DataReductionProxyConfig| instance, with the exception of + // |config_values| which is owned by |this|. |io_task_runner| is used to + // validate calls on the correct thread. |event_creator| is used for logging + // the start and end of a secure proxy check; |net_log| is used to create a + // net::BoundNetLog for correlating the start and end of the check. + // |config_values| contains the Data Reduction Proxy configuration values. + // |configurator| is the target for a configuration update. DataReductionProxyConfig( scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, net::NetLog* net_log, scoped_ptr<DataReductionProxyConfigValues> config_values, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); ~DataReductionProxyConfig() override; // Performs initialization on the IO thread. @@ -280,8 +285,8 @@ class DataReductionProxyConfig // 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_; + // The caller must ensure that the |event_creator_| outlives this instance. + DataReductionProxyEventCreator* event_creator_; // Used for performing the secure proxy check. net::URLRequestContextGetter* url_request_context_getter_; 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 index c28aaf2..43b5140 100644 --- 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 @@ -21,15 +21,15 @@ TestDataReductionProxyConfig::TestDataReductionProxyConfig( scoped_refptr<base::SingleThreadTaskRunner> task_runner, net::NetLog* net_log, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store) + DataReductionProxyEventCreator* event_creator) : TestDataReductionProxyConfig( - make_scoped_ptr( - new TestDataReductionProxyParams(params_flags, - params_definitions)).Pass(), + make_scoped_ptr(new TestDataReductionProxyParams(params_flags, + params_definitions)) + .Pass(), task_runner, net_log, configurator, - event_store) { + event_creator) { } TestDataReductionProxyConfig::TestDataReductionProxyConfig( @@ -37,12 +37,12 @@ TestDataReductionProxyConfig::TestDataReductionProxyConfig( scoped_refptr<base::SingleThreadTaskRunner> task_runner, net::NetLog* net_log, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store) + DataReductionProxyEventCreator* event_creator) : DataReductionProxyConfig(task_runner, net_log, config_values.Pass(), configurator, - event_store) { + event_creator) { network_interfaces_.reset(new net::NetworkInterfaceList()); } @@ -92,12 +92,12 @@ MockDataReductionProxyConfig::MockDataReductionProxyConfig( scoped_refptr<base::SingleThreadTaskRunner> task_runner, net::NetLog* net_log, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store) + DataReductionProxyEventCreator* event_creator) : TestDataReductionProxyConfig(config_values.Pass(), task_runner, net_log, configurator, - event_store) { + event_creator) { } MockDataReductionProxyConfig::~MockDataReductionProxyConfig() { 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 index d8259df..a07dc59 100644 --- 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 @@ -22,7 +22,7 @@ class NetLog; namespace data_reduction_proxy { class DataReductionProxyConfigurator; -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class DataReductionProxyMutableConfigValues; class TestDataReductionProxyParams; @@ -39,7 +39,7 @@ class TestDataReductionProxyConfig : public DataReductionProxyConfig { scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, net::NetLog* net_log, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); // Creates a |TestDataReductionProxyConfig| with the provided |config_values|. // This permits any DataReductionProxyConfigValues to be used (such as @@ -49,7 +49,7 @@ class TestDataReductionProxyConfig : public DataReductionProxyConfig { scoped_refptr<base::SingleThreadTaskRunner> task_runner, net::NetLog* net_log, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); ~TestDataReductionProxyConfig() override; @@ -92,7 +92,7 @@ class MockDataReductionProxyConfig : public TestDataReductionProxyConfig { scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, net::NetLog* net_log, DataReductionProxyConfigurator* configurator, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); ~MockDataReductionProxyConfig(); MOCK_METHOD1(RecordSecureProxyCheckFetchResult, 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 index 4318e59..6dfb7b8 100644 --- 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 @@ -10,7 +10,7 @@ #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator_test_utils.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_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_event_creator.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/http/http_status_code.h" @@ -163,7 +163,7 @@ class DataReductionProxyConfigTest : public testing::Test { params->EnableQuic(false); return make_scoped_ptr(new DataReductionProxyConfig( test_context_->task_runner(), test_context_->net_log(), params.Pass(), - test_context_->configurator(), test_context_->event_store())); + test_context_->configurator(), test_context_->event_creator())); } MockDataReductionProxyConfig* config() { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.cc index 2a804f1..35c8828 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.cc @@ -6,17 +6,17 @@ #include "base/strings/string_util.h" #include "base/values.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" #include "net/proxy/proxy_config.h" namespace data_reduction_proxy { DataReductionProxyConfigurator::DataReductionProxyConfigurator( net::NetLog* net_log, - DataReductionProxyEventStore* event_store) - : net_log_(net_log), data_reduction_proxy_event_store_(event_store) { + DataReductionProxyEventCreator* event_creator) + : net_log_(net_log), data_reduction_proxy_event_creator_(event_creator) { DCHECK(net_log); - DCHECK(event_store); + DCHECK(event_creator); // Constructed on the UI thread, but should be checked on the IO thread. thread_checker_.DetachFromThread(); } @@ -63,7 +63,7 @@ void DataReductionProxyConfigurator::Enable( // config will return invalid. net::ProxyConfig::ID unused_id = 1; config.set_id(unused_id); - data_reduction_proxy_event_store_->AddProxyEnabledEvent( + data_reduction_proxy_event_creator_->AddProxyEnabledEvent( net_log_, primary_restricted, fallback_restricted, primary_origin, fallback_origin, ssl_origin); config_ = config; @@ -72,7 +72,7 @@ void DataReductionProxyConfigurator::Enable( void DataReductionProxyConfigurator::Disable() { DCHECK(thread_checker_.CalledOnValidThread()); net::ProxyConfig config = net::ProxyConfig::CreateDirect(); - data_reduction_proxy_event_store_->AddProxyDisabledEvent(net_log_); + data_reduction_proxy_event_creator_->AddProxyDisabledEvent(net_log_); config_ = config; } 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 e239b54..f62bb7f 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 @@ -22,16 +22,15 @@ class PrefService; namespace data_reduction_proxy { -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class DataReductionProxyConfigurator { public: - // Constructs a configurator. |net_log| and |event_store| are used to + // Constructs a configurator. |net_log| and |event_creator| are used to // track network and Data Reduction Proxy events respectively, must not be // null, and must outlive this instance. - DataReductionProxyConfigurator( - net::NetLog* net_log, - DataReductionProxyEventStore* event_store); + DataReductionProxyConfigurator(net::NetLog* net_log, + DataReductionProxyEventCreator* event_creator); virtual ~DataReductionProxyConfigurator(); @@ -76,7 +75,7 @@ class DataReductionProxyConfigurator { // Used for logging of network- and Data Reduction Proxy-related events. net::NetLog* net_log_; - DataReductionProxyEventStore* data_reduction_proxy_event_store_; + DataReductionProxyEventCreator* data_reduction_proxy_event_creator_; // Enforce usage on the IO thread. base::ThreadChecker thread_checker_; 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 index e373aad..27d9d38 100644 --- 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 @@ -8,8 +8,8 @@ namespace data_reduction_proxy { TestDataReductionProxyConfigurator::TestDataReductionProxyConfigurator( net::NetLog* net_log, - DataReductionProxyEventStore* event_store) - : DataReductionProxyConfigurator(net_log, event_store), + DataReductionProxyEventCreator* event_creator) + : DataReductionProxyConfigurator(net_log, event_creator), enabled_(false), restricted_(false), fallback_restricted_(false) { 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 index bcfccd5..9125bfd 100644 --- 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 @@ -15,14 +15,14 @@ class NetLog; namespace data_reduction_proxy { -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class TestDataReductionProxyConfigurator : public DataReductionProxyConfigurator { public: TestDataReductionProxyConfigurator( net::NetLog* net_log, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); ~TestDataReductionProxyConfigurator() override; // Overrides of DataReductionProxyConfigurator 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 ee10e1d..d669dcd 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 @@ -7,10 +7,10 @@ #include <string> #include "base/memory/scoped_ptr.h" -#include "base/test/test_simple_task_runner.h" #include "base/values.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" -#include "net/log/test_net_log.h" +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_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_params_test_utils.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -19,12 +19,18 @@ namespace data_reduction_proxy { class DataReductionProxyConfiguratorTest : public testing::Test { public: void SetUp() override { - task_runner_ = new base::TestSimpleTaskRunner(); - net_log_.reset(new net::TestNetLog()); - data_reduction_proxy_event_store_.reset( - new data_reduction_proxy::DataReductionProxyEventStore(task_runner_)); + test_context_ = + DataReductionProxyTestContext::Builder() + .WithParamsFlags(DataReductionProxyParams::kAllowed | + DataReductionProxyParams::kFallbackAllowed | + DataReductionProxyParams::kPromoAllowed) + .WithParamsDefinitions( + TestDataReductionProxyParams::HAS_EVERYTHING & + ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & + ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN) + .Build(); config_.reset(new DataReductionProxyConfigurator( - net_log_.get(), data_reduction_proxy_event_store_.get())); + test_context_->net_log(), test_context_->event_creator())); } void CheckProxyConfig( @@ -32,7 +38,7 @@ class DataReductionProxyConfiguratorTest : public testing::Test { const std::string& expected_http_proxies, const std::string& expected_https_proxies, const std::string& expected_bypass_list) { - task_runner_->RunUntilIdle(); + test_context_->RunUntilIdle(); const net::ProxyConfig::ProxyRules& rules = config_->GetProxyConfig().proxy_rules(); ASSERT_EQ(expected_rules_type, rules.type); @@ -44,11 +50,8 @@ class DataReductionProxyConfiguratorTest : public testing::Test { } } + scoped_ptr<DataReductionProxyTestContext> test_context_; scoped_ptr<DataReductionProxyConfigurator> config_; - scoped_ptr<net::NetLog> net_log_; - scoped_refptr<base::TestSimpleTaskRunner> task_runner_; - scoped_ptr<data_reduction_proxy::DataReductionProxyEventStore> - data_reduction_proxy_event_store_; }; TEST_F(DataReductionProxyConfiguratorTest, TestUnrestricted) { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.cc index 28de78c..8a1145a 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.cc @@ -6,6 +6,7 @@ #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_protocol.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_bypass_stats.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" #include "net/http/http_response_headers.h" #include "net/url_request/url_request.h" @@ -19,10 +20,10 @@ namespace data_reduction_proxy { DataReductionProxyInterceptor::DataReductionProxyInterceptor( DataReductionProxyConfig* config, DataReductionProxyBypassStats* stats, - DataReductionProxyEventStore* event_store) + DataReductionProxyEventCreator* event_creator) : bypass_stats_(stats), bypass_protocol_( - new DataReductionProxyBypassProtocol(config, event_store)) { + new DataReductionProxyBypassProtocol(config, event_creator)) { } DataReductionProxyInterceptor::~DataReductionProxyInterceptor() { diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h index ab7ece8..33c360a 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h @@ -11,7 +11,7 @@ namespace data_reduction_proxy { class DataReductionProxyBypassProtocol; class DataReductionProxyConfig; -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class DataReductionProxyBypassStats; // Used to intercept responses that contain explicit and implicit signals @@ -20,11 +20,11 @@ class DataReductionProxyBypassStats; // without use of the proxy. class DataReductionProxyInterceptor : public net::URLRequestInterceptor { public: - // Constructs the interceptor. |config|, |stats|, and |event_store| must + // Constructs the interceptor. |config|, |stats|, and |event_creator| must // outlive |this|. |stats| may be NULL. DataReductionProxyInterceptor(DataReductionProxyConfig* config, DataReductionProxyBypassStats* stats, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); // Destroys the interceptor. ~DataReductionProxyInterceptor() override; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc index 470eda0..5a4c5d8 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.cc @@ -22,7 +22,8 @@ #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h" #include "net/log/net_log.h" @@ -109,9 +110,9 @@ DataReductionProxyIOData::DataReductionProxyIOData( scoped_ptr<DataReductionProxyParams> params( new DataReductionProxyParams(param_flags)); params->EnableQuic(enable_quic); - event_store_.reset(new DataReductionProxyEventStore(ui_task_runner)); + event_creator_.reset(new DataReductionProxyEventCreator(this)); configurator_.reset( - new DataReductionProxyConfigurator(net_log, event_store_.get())); + new DataReductionProxyConfigurator(net_log, event_creator_.get())); bool use_config_client = DataReductionProxyParams::IsConfigClientEnabled(); DataReductionProxyMutableConfigValues* raw_mutable_config = nullptr; if (use_config_client) { @@ -120,11 +121,11 @@ DataReductionProxyIOData::DataReductionProxyIOData( raw_mutable_config = mutable_config.get(); config_.reset(new DataReductionProxyConfig( io_task_runner_, net_log, mutable_config.Pass(), configurator_.get(), - event_store_.get())); + event_creator_.get())); } else { - config_.reset( - new DataReductionProxyConfig(io_task_runner_, net_log, params.Pass(), - configurator_.get(), event_store_.get())); + config_.reset(new DataReductionProxyConfig( + io_task_runner_, net_log, params.Pass(), configurator_.get(), + event_creator_.get())); } // It is safe to use base::Unretained here, since it gets executed @@ -211,7 +212,7 @@ scoped_ptr<net::URLRequestInterceptor> DataReductionProxyIOData::CreateInterceptor() { DCHECK(io_task_runner_->BelongsToCurrentThread()); return make_scoped_ptr(new DataReductionProxyInterceptor( - config_.get(), bypass_stats_.get(), event_store_.get())); + config_.get(), bypass_stats_.get(), event_creator_.get())); } scoped_ptr<DataReductionProxyNetworkDelegate> @@ -248,6 +249,34 @@ void DataReductionProxyIOData::UpdateContentLengths( data_reduction_proxy_enabled, request_type)); } +void DataReductionProxyIOData::AddEnabledEvent(scoped_ptr<base::Value> entry, + bool enabled) { + DCHECK(io_task_runner_->BelongsToCurrentThread()); + ui_task_runner_->PostTask( + FROM_HERE, base::Bind(&DataReductionProxyService::AddEnabledEvent, + service_, base::Passed(&entry), enabled)); +} + +void DataReductionProxyIOData::AddEventAndSecureProxyCheckState( + scoped_ptr<base::Value> entry, + SecureProxyCheckState state) { + DCHECK(io_task_runner_->BelongsToCurrentThread()); + ui_task_runner_->PostTask( + FROM_HERE, + base::Bind(&DataReductionProxyService::AddEventAndSecureProxyCheckState, + service_, base::Passed(&entry), state)); +} + +void DataReductionProxyIOData::AddAndSetLastBypassEvent( + scoped_ptr<base::Value> entry, + int64 expiration_ticks) { + DCHECK(io_task_runner_->BelongsToCurrentThread()); + ui_task_runner_->PostTask( + FROM_HERE, + base::Bind(&DataReductionProxyService::AddAndSetLastBypassEvent, service_, + base::Passed(&entry), expiration_ticks)); +} + void DataReductionProxyIOData::SetUnreachable(bool unreachable) { DCHECK(io_task_runner_->BelongsToCurrentThread()); ui_task_runner_->PostTask( diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h index 1567b4f..9a6f3b8 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h @@ -15,6 +15,11 @@ #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" + +namespace base { +class Value; +} namespace net { class NetLog; @@ -28,12 +33,12 @@ class DataReductionProxyBypassStats; class DataReductionProxyConfig; class DataReductionProxyConfigServiceClient; class DataReductionProxyConfigurator; -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class DataReductionProxyService; // Contains and initializes all Data Reduction Proxy objects that operate on // the IO thread. -class DataReductionProxyIOData { +class DataReductionProxyIOData : public DataReductionProxyEventStorageDelegate { public: // Constructs a DataReductionProxyIOData object. |param_flags| is used to // set information about the DNS names used by the proxy, and allowable @@ -88,6 +93,14 @@ class DataReductionProxyIOData { bool data_reduction_proxy_enabled, DataReductionProxyRequestType request_type); + // Overrides of DataReductionProxyEventStorageDelegate. Bridges to the UI + // thread objects. + void AddEnabledEvent(scoped_ptr<base::Value> entry, bool enabled) override; + void AddEventAndSecureProxyCheckState(scoped_ptr<base::Value> entry, + SecureProxyCheckState state) override; + void AddAndSetLastBypassEvent(scoped_ptr<base::Value> entry, + int64 expiration_ticks) override; + // Returns true if the Data Reduction Proxy is enabled and false otherwise. bool IsEnabled() const; @@ -100,8 +113,8 @@ class DataReductionProxyIOData { return config_.get(); } - DataReductionProxyEventStore* event_store() const { - return event_store_.get(); + DataReductionProxyEventCreator* event_creator() const { + return event_creator_.get(); } DataReductionProxyRequestOptions* request_options() const { @@ -163,8 +176,8 @@ class DataReductionProxyIOData { // interstitials. mutable scoped_ptr<DataReductionProxyDebugUIService> debug_ui_service_; - // Tracker of Data Reduction Proxy-related events, e.g., for logging. - scoped_ptr<DataReductionProxyEventStore> event_store_; + // Creates Data Reduction Proxy-related events for logging. + scoped_ptr<DataReductionProxyEventCreator> event_creator_; // Setter of the Data Reduction Proxy-specific proxy configuration. scoped_ptr<DataReductionProxyConfigurator> configurator_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc index c5a4e54..cf25df9 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.cc @@ -11,6 +11,7 @@ #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_io_data.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service_observer.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" namespace data_reduction_proxy { @@ -26,6 +27,7 @@ DataReductionProxyService::DataReductionProxyService( weak_factory_(this) { DCHECK(settings); compression_stats_ = compression_stats.Pass(); + event_store_.reset(new DataReductionProxyEventStore()); } DataReductionProxyService::~DataReductionProxyService() { @@ -68,6 +70,26 @@ void DataReductionProxyService::UpdateContentLengths( } } +void DataReductionProxyService::AddEnabledEvent(scoped_ptr<base::Value> entry, + bool enabled) { + DCHECK(CalledOnValidThread()); + event_store_->AddEnabledEvent(entry.Pass(), enabled); +} + +void DataReductionProxyService::AddEventAndSecureProxyCheckState( + scoped_ptr<base::Value> entry, + SecureProxyCheckState state) { + DCHECK(CalledOnValidThread()); + event_store_->AddEventAndSecureProxyCheckState(entry.Pass(), state); +} + +void DataReductionProxyService::AddAndSetLastBypassEvent( + scoped_ptr<base::Value> entry, + int64 expiration_ticks) { + DCHECK(CalledOnValidThread()); + event_store_->AddAndSetLastBypassEvent(entry.Pass(), expiration_ticks); +} + void DataReductionProxyService::SetUnreachable(bool unreachable) { DCHECK(CalledOnValidThread()); settings_->SetUnreachable(unreachable); diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h index f107881..54a3365 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h @@ -14,6 +14,7 @@ #include "base/single_thread_task_runner.h" #include "base/threading/non_thread_safe.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" class GURL; class PrefService; @@ -21,6 +22,7 @@ class PrefService; namespace base { class SequencedTaskRunner; class TimeDelta; +class Value; } namespace net { @@ -30,13 +32,16 @@ class URLRequestContextGetter; namespace data_reduction_proxy { class DataReductionProxyCompressionStats; +class DataReductionProxyEventStore; class DataReductionProxyIOData; class DataReductionProxyServiceObserver; class DataReductionProxySettings; // Contains and initializes all Data Reduction Proxy objects that have a // lifetime based on the UI thread. -class DataReductionProxyService : public base::NonThreadSafe { +class DataReductionProxyService + : public base::NonThreadSafe, + public DataReductionProxyEventStorageDelegate { public: // The caller must ensure that |settings| and |request_context| remain alive // for the lifetime of the |DataReductionProxyService| instance. This instance @@ -73,6 +78,13 @@ class DataReductionProxyService : public base::NonThreadSafe { bool data_reduction_proxy_enabled, DataReductionProxyRequestType request_type); + // Overrides of DataReductionProxyEventStorageDelegate. + void AddEnabledEvent(scoped_ptr<base::Value> entry, bool enabled) override; + void AddEventAndSecureProxyCheckState(scoped_ptr<base::Value> entry, + SecureProxyCheckState state) override; + void AddAndSetLastBypassEvent(scoped_ptr<base::Value> entry, + int64 expiration_ticks) override; + // Records whether the Data Reduction Proxy is unreachable or not. void SetUnreachable(bool unreachable); @@ -96,6 +108,10 @@ class DataReductionProxyService : public base::NonThreadSafe { return settings_; } + DataReductionProxyEventStore* event_store() const { + return event_store_.get(); + } + net::URLRequestContextGetter* url_request_context_getter() const { return url_request_context_getter_; } @@ -108,6 +124,8 @@ class DataReductionProxyService : public base::NonThreadSafe { // Tracks compression statistics to be displayed to the user. scoped_ptr<DataReductionProxyCompressionStats> compression_stats_; + scoped_ptr<DataReductionProxyEventStore> event_store_; + DataReductionProxySettings* settings_; // Used to post tasks to |io_data_|. 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 33c1433..57f18bb 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 @@ -49,7 +49,6 @@ DataReductionProxySettings::DataReductionProxySettings() alternative_allowed_(false), promo_allowed_(false), prefs_(NULL), - event_store_(NULL), config_(nullptr) { } @@ -88,11 +87,9 @@ void DataReductionProxySettings::InitDataReductionProxySettings( DCHECK(prefs); DCHECK(io_data); DCHECK(io_data->config()); - DCHECK(io_data->event_store()); DCHECK(data_reduction_proxy_service.get()); prefs_ = prefs; config_ = io_data->config(); - event_store_ = io_data->event_store(); data_reduction_proxy_service_ = data_reduction_proxy_service.Pass(); data_reduction_proxy_service_->AddObserver(this); InitPrefMembers(); @@ -252,6 +249,14 @@ void DataReductionProxySettings::MaybeActivateDataReductionProxy( UpdateIOData(at_startup); } +DataReductionProxyEventStore* DataReductionProxySettings::GetEventStore() + const { + if (data_reduction_proxy_service_) + return data_reduction_proxy_service_->event_store(); + + return nullptr; +} + // Metrics methods void DataReductionProxySettings::RecordDataReductionInit() { DCHECK(thread_checker_.CalledOnValidThread()); 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 c7f3685..213a248 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 @@ -126,9 +126,7 @@ class DataReductionProxySettings : public DataReductionProxyServiceObserver { // Returns the event store being used. May be null if // InitDataReductionProxySettings has not been called. - DataReductionProxyEventStore* GetEventStore() const { - return event_store_; - } + DataReductionProxyEventStore* GetEventStore() const; // Returns true if the data reduction proxy configuration may be used. bool Allowed() const { @@ -253,9 +251,6 @@ class DataReductionProxySettings : public DataReductionProxyServiceObserver { PrefService* prefs_; - // The caller must ensure that the |event_store_| outlives this instance. - DataReductionProxyEventStore* event_store_; - // The caller must ensure that the |config_| outlives this instance. DataReductionProxyConfig* config_; diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc index ae43031..f070f13 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.cc @@ -15,6 +15,8 @@ #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h" #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_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.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" @@ -173,7 +175,7 @@ MockDataReductionProxyService::~MockDataReductionProxyService() { TestDataReductionProxyIOData::TestDataReductionProxyIOData( scoped_refptr<base::SingleThreadTaskRunner> task_runner, scoped_ptr<DataReductionProxyConfig> config, - scoped_ptr<DataReductionProxyEventStore> event_store, + scoped_ptr<DataReductionProxyEventCreator> event_creator, scoped_ptr<DataReductionProxyRequestOptions> request_options, scoped_ptr<DataReductionProxyConfigurator> configurator, scoped_ptr<DataReductionProxyConfigServiceClient> config_client) @@ -181,7 +183,7 @@ TestDataReductionProxyIOData::TestDataReductionProxyIOData( io_task_runner_ = task_runner; ui_task_runner_ = task_runner; config_ = config.Pass(); - event_store_ = event_store.Pass(); + event_creator_ = event_creator.Pass(); request_options_ = request_options.Pass(); configurator_ = configurator.Pass(); config_client_ = config_client.Pass(); @@ -313,16 +315,18 @@ DataReductionProxyTestContext::Builder::Build() { task_runner, test_request_context.Pass()); } - scoped_ptr<DataReductionProxyEventStore> event_store( - new DataReductionProxyEventStore(task_runner)); + scoped_ptr<TestDataReductionProxyEventStorageDelegate> storage_delegate( + new TestDataReductionProxyEventStorageDelegate()); + scoped_ptr<DataReductionProxyEventCreator> event_creator( + new DataReductionProxyEventCreator(storage_delegate.get())); scoped_ptr<DataReductionProxyConfigurator> configurator; if (use_test_configurator_) { test_context_flags |= USE_TEST_CONFIGURATOR; configurator.reset(new TestDataReductionProxyConfigurator( - net_log.get(), event_store.get())); + net_log.get(), event_creator.get())); } else { configurator.reset( - new DataReductionProxyConfigurator(net_log.get(), event_store.get())); + new DataReductionProxyConfigurator(net_log.get(), event_creator.get())); } scoped_ptr<TestDataReductionProxyConfig> config; @@ -338,16 +342,16 @@ DataReductionProxyTestContext::Builder::Build() { raw_mutable_config = mutable_config.get(); config.reset(new TestDataReductionProxyConfig( mutable_config.Pass(), task_runner, net_log.get(), configurator.get(), - event_store.get())); + event_creator.get())); } else if (use_mock_config_) { test_context_flags |= USE_MOCK_CONFIG; config.reset(new MockDataReductionProxyConfig( params.Pass(), task_runner, net_log.get(), configurator.get(), - event_store.get())); + event_creator.get())); } else { config.reset(new TestDataReductionProxyConfig( params.Pass(), task_runner, net_log.get(), configurator.get(), - event_store.get())); + event_creator.get())); } scoped_ptr<DataReductionProxyRequestOptions> request_options; @@ -383,7 +387,7 @@ DataReductionProxyTestContext::Builder::Build() { scoped_ptr<TestDataReductionProxyIOData> io_data( new TestDataReductionProxyIOData( - task_runner, config.Pass(), event_store.Pass(), + task_runner, config.Pass(), event_creator.Pass(), request_options.Pass(), configurator.Pass(), config_client.Pass())); io_data->InitOnUIThread(pref_service.get()); io_data->SetSimpleURLRequestContextGetter(request_context_getter); @@ -392,7 +396,8 @@ DataReductionProxyTestContext::Builder::Build() { new DataReductionProxyTestContext( loop.Pass(), task_runner, pref_service.Pass(), net_log.Pass(), request_context_getter, mock_socket_factory_, io_data.Pass(), - settings.Pass(), raw_params, test_context_flags)); + settings.Pass(), storage_delegate.Pass(), raw_params, + test_context_flags)); if (!skip_settings_initialization_) test_context->InitSettingsWithoutCheck(); @@ -409,6 +414,7 @@ DataReductionProxyTestContext::DataReductionProxyTestContext( net::MockClientSocketFactory* mock_socket_factory, scoped_ptr<TestDataReductionProxyIOData> io_data, scoped_ptr<DataReductionProxySettings> settings, + scoped_ptr<TestDataReductionProxyEventStorageDelegate> storage_delegate, TestDataReductionProxyParams* params, unsigned int test_context_flags) : test_context_flags_(test_context_flags), @@ -420,6 +426,7 @@ DataReductionProxyTestContext::DataReductionProxyTestContext( mock_socket_factory_(mock_socket_factory), io_data_(io_data.Pass()), settings_(settings.Pass()), + storage_delegate_(storage_delegate.Pass()), params_(params) { } @@ -440,6 +447,8 @@ void DataReductionProxyTestContext::InitSettingsWithoutCheck() { settings_->InitDataReductionProxySettings( simple_pref_service_.get(), io_data_.get(), CreateDataReductionProxyServiceInternal()); + storage_delegate_->SetStorageDelegate( + settings_->data_reduction_proxy_service()->event_store()); io_data_->SetDataReductionProxyService( settings_->data_reduction_proxy_service()->GetWeakPtr()); if (io_data_->config_client()) diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h index 7c41356..e5c4ca0 100644 --- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h @@ -42,7 +42,7 @@ class URLRequestContextStorage; namespace data_reduction_proxy { class DataReductionProxyConfigurator; -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; class DataReductionProxyMutableConfigValues; class DataReductionProxyRequestOptions; class DataReductionProxySettings; @@ -50,6 +50,7 @@ class DataReductionProxyCompressionStats; class MockDataReductionProxyConfig; class TestDataReductionProxyConfig; class TestDataReductionProxyConfigurator; +class TestDataReductionProxyEventStorageDelegate; class TestDataReductionProxyParams; // Test version of |DataReductionProxyRequestOptions|. @@ -163,7 +164,7 @@ class TestDataReductionProxyIOData : public DataReductionProxyIOData { TestDataReductionProxyIOData( scoped_refptr<base::SingleThreadTaskRunner> task_runner, scoped_ptr<DataReductionProxyConfig> config, - scoped_ptr<DataReductionProxyEventStore> event_store, + scoped_ptr<DataReductionProxyEventCreator> event_creator, scoped_ptr<DataReductionProxyRequestOptions> request_options, scoped_ptr<DataReductionProxyConfigurator> configurator, scoped_ptr<DataReductionProxyConfigServiceClient> config_client); @@ -343,8 +344,8 @@ class DataReductionProxyTestContext { return request_context_getter_.get(); } - DataReductionProxyEventStore* event_store() const { - return io_data_->event_store(); + DataReductionProxyEventCreator* event_creator() const { + return io_data_->event_creator(); } DataReductionProxyConfigurator* configurator() const { @@ -393,6 +394,7 @@ class DataReductionProxyTestContext { net::MockClientSocketFactory* mock_socket_factory, scoped_ptr<TestDataReductionProxyIOData> io_data, scoped_ptr<DataReductionProxySettings> settings, + scoped_ptr<TestDataReductionProxyEventStorageDelegate> storage_delegate, TestDataReductionProxyParams* params, unsigned int test_context_flags); @@ -415,6 +417,7 @@ class DataReductionProxyTestContext { scoped_ptr<TestDataReductionProxyIOData> io_data_; scoped_ptr<DataReductionProxySettings> settings_; + scoped_ptr<TestDataReductionProxyEventStorageDelegate> storage_delegate_; TestDataReductionProxyParams* params_; diff --git a/components/data_reduction_proxy/core/common/BUILD.gn b/components/data_reduction_proxy/core/common/BUILD.gn index 6976004..c0788de 100644 --- a/components/data_reduction_proxy/core/common/BUILD.gn +++ b/components/data_reduction_proxy/core/common/BUILD.gn @@ -10,6 +10,9 @@ static_library("common") { "data_reduction_proxy_client_config_parser.cc", "data_reduction_proxy_client_config_parser.h", "data_reduction_proxy_config_values.h", + "data_reduction_proxy_event_creator.cc", + "data_reduction_proxy_event_creator.h", + "data_reduction_proxy_event_storage_delegate.h", "data_reduction_proxy_event_store.cc", "data_reduction_proxy_event_store.h", "data_reduction_proxy_headers.cc", @@ -36,6 +39,8 @@ static_library("common") { source_set("test_support") { testonly = true sources = [ + "data_reduction_proxy_event_storage_delegate_test_utils.cc", + "data_reduction_proxy_event_storage_delegate_test_utils.h", "data_reduction_proxy_headers_test_utils.cc", "data_reduction_proxy_headers_test_utils.h", "data_reduction_proxy_params_test_utils.cc", diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.cc new file mode 100644 index 0000000..e1f41d7 --- /dev/null +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.cc @@ -0,0 +1,264 @@ +// 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/common/data_reduction_proxy_event_creator.h" + +#include "base/bind.h" +#include "base/strings/string_number_conversions.h" +#include "base/time/time.h" +#include "base/values.h" +#include "net/proxy/proxy_server.h" + +namespace { + +scoped_ptr<base::Value> BuildDataReductionProxyEvent( + net::NetLog::EventType type, + const net::NetLog::Source& source, + net::NetLog::EventPhase phase, + const net::NetLog::ParametersCallback& parameters_callback) { + base::TimeTicks ticks_now = base::TimeTicks::Now(); + net::NetLog::EntryData entry_data(type, source, phase, ticks_now, + ¶meters_callback); + net::NetLog::Entry entry(&entry_data, net::NetLog::LOG_ALL); + scoped_ptr<base::Value> entry_value(entry.ToValue()); + + return entry_value; +} + +int64 GetExpirationTicks(int bypass_seconds) { + base::TimeTicks expiration_ticks = + base::TimeTicks::Now() + base::TimeDelta::FromSeconds(bypass_seconds); + return (expiration_ticks - base::TimeTicks()).InMilliseconds(); +} + +// The following method creates a string resembling the output of +// net::ProxyServer::ToURI(). +std::string GetNormalizedProxyString(const std::string& proxy_origin) { + net::ProxyServer proxy_server = + net::ProxyServer::FromURI(proxy_origin, net::ProxyServer::SCHEME_HTTP); + if (proxy_server.is_valid()) + return proxy_origin; + + return std::string(); +} + +// A callback which creates a base::Value containing information about enabling +// the Data Reduction Proxy. Ownership of the base::Value is passed to the +// caller. +base::Value* EnableDataReductionProxyCallback( + bool primary_restricted, + bool fallback_restricted, + const std::string& primary_origin, + const std::string& fallback_origin, + const std::string& ssl_origin, + net::NetLog::LogLevel /* log_level */) { + base::DictionaryValue* dict = new base::DictionaryValue(); + dict->SetBoolean("enabled", true); + dict->SetBoolean("primary_restricted", primary_restricted); + dict->SetBoolean("fallback_restricted", fallback_restricted); + dict->SetString("primary_origin", GetNormalizedProxyString(primary_origin)); + dict->SetString("fallback_origin", GetNormalizedProxyString(fallback_origin)); + dict->SetString("ssl_origin", GetNormalizedProxyString(ssl_origin)); + return dict; +} + +// A callback which creates a base::Value containing information about disabling +// the Data Reduction Proxy. Ownership of the base::Value is passed to the +// caller. +base::Value* DisableDataReductionProxyCallback( + net::NetLog::LogLevel /* log_level */) { + base::DictionaryValue* dict = new base::DictionaryValue(); + dict->SetBoolean("enabled", false); + return dict; +} + +// A callback which creates a base::Value containing information about bypassing +// the Data Reduction Proxy. Ownership of the base::Value is passed to the +// caller. +base::Value* UrlBypassActionCallback(const std::string& action, + const GURL& url, + int bypass_seconds, + int64 expiration_ticks, + net::NetLog::LogLevel /* log_level */) { + base::DictionaryValue* dict = new base::DictionaryValue(); + dict->SetString("action", action); + dict->SetString("url", url.spec()); + dict->SetString("bypass_duration_seconds", + base::Int64ToString(bypass_seconds)); + dict->SetString("expiration", base::Int64ToString(expiration_ticks)); + return dict; +} + +// A callback which creates a base::Value containing information about bypassing +// the Data Reduction Proxy. Ownership of the base::Value is passed to the +// caller. +base::Value* UrlBypassTypeCallback( + data_reduction_proxy::DataReductionProxyBypassType bypass_type, + const GURL& url, + int bypass_seconds, + int64 expiration_ticks, + net::NetLog::LogLevel /* log_level */) { + base::DictionaryValue* dict = new base::DictionaryValue(); + dict->SetInteger("bypass_type", bypass_type); + dict->SetString("url", url.spec()); + dict->SetString("bypass_duration_seconds", + base::Int64ToString(bypass_seconds)); + dict->SetString("expiration", base::Int64ToString(expiration_ticks)); + return dict; +} + +// A callback which creates a base::Value containing information about +// completing the Data Reduction Proxy secure proxy check. Ownership of the +// base::Value is passed to the caller. +base::Value* EndCanaryRequestCallback(int net_error, + int http_response_code, + bool succeeded, + net::NetLog::LogLevel /* log_level */) { + base::DictionaryValue* dict = new base::DictionaryValue(); + dict->SetInteger("net_error", net_error); + dict->SetInteger("http_response_code", http_response_code); + dict->SetBoolean("check_succeeded", succeeded); + return dict; +} + +} // namespace + +namespace data_reduction_proxy { + +DataReductionProxyEventCreator::DataReductionProxyEventCreator( + DataReductionProxyEventStorageDelegate* storage_delegate) + : storage_delegate_(storage_delegate) { + DCHECK(storage_delegate); + // Constructed on the UI thread, but should be checked on the IO thread. + thread_checker_.DetachFromThread(); +} + +DataReductionProxyEventCreator::~DataReductionProxyEventCreator() { +} + +void DataReductionProxyEventCreator::AddProxyEnabledEvent( + net::NetLog* net_log, + bool primary_restricted, + bool fallback_restricted, + const std::string& primary_origin, + const std::string& fallback_origin, + const std::string& ssl_origin) { + DCHECK(thread_checker_.CalledOnValidThread()); + const net::NetLog::ParametersCallback& parameters_callback = base::Bind( + &EnableDataReductionProxyCallback, primary_restricted, + fallback_restricted, primary_origin, fallback_origin, ssl_origin); + PostEnabledEvent(net_log, net::NetLog::TYPE_DATA_REDUCTION_PROXY_ENABLED, + true, parameters_callback); +} + +void DataReductionProxyEventCreator::AddProxyDisabledEvent( + net::NetLog* net_log) { + DCHECK(thread_checker_.CalledOnValidThread()); + const net::NetLog::ParametersCallback& parameters_callback = + base::Bind(&DisableDataReductionProxyCallback); + PostEnabledEvent(net_log, net::NetLog::TYPE_DATA_REDUCTION_PROXY_ENABLED, + false, parameters_callback); +} + +void DataReductionProxyEventCreator::AddBypassActionEvent( + const net::BoundNetLog& net_log, + const std::string& bypass_action, + const GURL& url, + const base::TimeDelta& bypass_duration) { + DCHECK(thread_checker_.CalledOnValidThread()); + int64 expiration_ticks = GetExpirationTicks(bypass_duration.InSeconds()); + const net::NetLog::ParametersCallback& parameters_callback = + base::Bind(&UrlBypassActionCallback, bypass_action, url, + bypass_duration.InSeconds(), expiration_ticks); + PostBoundNetLogBypassEvent( + net_log, net::NetLog::TYPE_DATA_REDUCTION_PROXY_BYPASS_REQUESTED, + net::NetLog::PHASE_NONE, expiration_ticks, parameters_callback); +} + +void DataReductionProxyEventCreator::AddBypassTypeEvent( + const net::BoundNetLog& net_log, + DataReductionProxyBypassType bypass_type, + const GURL& url, + const base::TimeDelta& bypass_duration) { + DCHECK(thread_checker_.CalledOnValidThread()); + int64 expiration_ticks = GetExpirationTicks(bypass_duration.InSeconds()); + const net::NetLog::ParametersCallback& parameters_callback = + base::Bind(&UrlBypassTypeCallback, bypass_type, url, + bypass_duration.InSeconds(), expiration_ticks); + PostBoundNetLogBypassEvent( + net_log, net::NetLog::TYPE_DATA_REDUCTION_PROXY_BYPASS_REQUESTED, + net::NetLog::PHASE_NONE, expiration_ticks, parameters_callback); +} + +void DataReductionProxyEventCreator::BeginSecureProxyCheck( + const net::BoundNetLog& net_log, + const GURL& url) { + DCHECK(thread_checker_.CalledOnValidThread()); + // This callback must be invoked synchronously + const net::NetLog::ParametersCallback& parameters_callback = + net::NetLog::StringCallback("url", &url.spec()); + PostBoundNetLogSecureProxyCheckEvent( + net_log, net::NetLog::TYPE_DATA_REDUCTION_PROXY_CANARY_REQUEST, + net::NetLog::PHASE_BEGIN, + DataReductionProxyEventStorageDelegate::CHECK_PENDING, + parameters_callback); +} + +void DataReductionProxyEventCreator::EndSecureProxyCheck( + const net::BoundNetLog& net_log, + int net_error, + int http_response_code, + bool succeeded) { + DCHECK(thread_checker_.CalledOnValidThread()); + const net::NetLog::ParametersCallback& parameters_callback = base::Bind( + &EndCanaryRequestCallback, net_error, http_response_code, succeeded); + PostBoundNetLogSecureProxyCheckEvent( + net_log, net::NetLog::TYPE_DATA_REDUCTION_PROXY_CANARY_REQUEST, + net::NetLog::PHASE_END, + net_error == 0 ? DataReductionProxyEventStorageDelegate::CHECK_SUCCESS + : DataReductionProxyEventStorageDelegate::CHECK_FAILED, + parameters_callback); +} + +void DataReductionProxyEventCreator::PostEnabledEvent( + net::NetLog* net_log, + net::NetLog::EventType type, + bool enabled, + const net::NetLog::ParametersCallback& callback) { + scoped_ptr<base::Value> event = BuildDataReductionProxyEvent( + type, net::NetLog::Source(), net::NetLog::PHASE_NONE, callback); + if (event) + storage_delegate_->AddEnabledEvent(event.Pass(), enabled); + + if (net_log) + net_log->AddGlobalEntry(type, callback); +} + +void DataReductionProxyEventCreator::PostBoundNetLogBypassEvent( + const net::BoundNetLog& net_log, + net::NetLog::EventType type, + net::NetLog::EventPhase phase, + int64 expiration_ticks, + const net::NetLog::ParametersCallback& callback) { + scoped_ptr<base::Value> event = + BuildDataReductionProxyEvent(type, net_log.source(), phase, callback); + if (event) + storage_delegate_->AddAndSetLastBypassEvent(event.Pass(), expiration_ticks); + net_log.AddEntry(type, phase, callback); +} + +void DataReductionProxyEventCreator::PostBoundNetLogSecureProxyCheckEvent( + const net::BoundNetLog& net_log, + net::NetLog::EventType type, + net::NetLog::EventPhase phase, + DataReductionProxyEventStorageDelegate::SecureProxyCheckState state, + const net::NetLog::ParametersCallback& callback) { + scoped_ptr<base::Value> event( + BuildDataReductionProxyEvent(type, net_log.source(), phase, callback)); + if (event) + storage_delegate_->AddEventAndSecureProxyCheckState(event.Pass(), state); + net_log.AddEntry(type, phase, callback); +} + +} // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h new file mode 100644 index 0000000..cd37e83 --- /dev/null +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h @@ -0,0 +1,119 @@ +// 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_COMMON_DATA_REDUCTION_PROXY_EVENT_CREATOR_H_ +#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_CREATOR_H_ + +#include <string> + +#include "base/basictypes.h" +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" +#include "base/threading/thread_checker.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" +#include "net/log/net_log.h" + +class GURL; + +namespace base { +class TimeDelta; +class Value; +} + +namespace net { +class BoundNetLog; +} + +namespace data_reduction_proxy { + +// Central location for creating debug events for the Data Reduction Proxy. +// This object lives on the IO thread and all of its methods are expected to be +// called from there. +class DataReductionProxyEventCreator { + public: + // Constructs a DataReductionProxyEventCreator object. |storage_delegate| must + // outlive |this| and can be used to store Data Reduction Proxy events for + // debugging without requiring a net::NetLog. + explicit DataReductionProxyEventCreator( + DataReductionProxyEventStorageDelegate* storage_delegate); + + ~DataReductionProxyEventCreator(); + + // Adds the DATA_REDUCTION_PROXY_ENABLED event (with enabled=true) to the + // event store. + void AddProxyEnabledEvent(net::NetLog* net_log, + bool primary_restricted, + bool fallback_restricted, + const std::string& primary_origin, + const std::string& fallback_origin, + const std::string& ssl_origin); + + // Adds the DATA_REDUCTION_PROXY_ENABLED event (with enabled=false) to the + // event store. + void AddProxyDisabledEvent(net::NetLog* net_log); + + // Adds a DATA_REDUCTION_PROXY_BYPASS_REQUESTED event to the event store + // when the bypass reason is initiated by the data reduction proxy. + void AddBypassActionEvent(const net::BoundNetLog& net_log, + const std::string& bypass_action, + const GURL& gurl, + const base::TimeDelta& bypass_duration); + + // Adds a DATA_REDUCTION_PROXY_BYPASS_REQUESTED event to the event store + // when the bypass reason is not initiated by the data reduction proxy, such + // as network errors. + void AddBypassTypeEvent(const net::BoundNetLog& net_log, + DataReductionProxyBypassType bypass_type, + const GURL& gurl, + const base::TimeDelta& bypass_duration); + + // Adds a DATA_REDUCTION_PROXY_CANARY_REQUEST event to the event store + // when the secure proxy request has started. + void BeginSecureProxyCheck(const net::BoundNetLog& net_log, const GURL& gurl); + + // Adds a DATA_REDUCTION_PROXY_CANARY_REQUEST event to the event store + // when the secure proxy request has ended. + void EndSecureProxyCheck(const net::BoundNetLog& net_log, + int net_error, + int http_response_code, + bool succeeded); + + private: + // Prepare and post enabling/disabling proxy events for the event store on the + // a net::NetLog. + void PostEnabledEvent(net::NetLog* net_log, + net::NetLog::EventType type, + bool enable, + const net::NetLog::ParametersCallback& callback); + + // Prepare and post a Data Reduction Proxy bypass event for the event store + // on a BoundNetLog. + void PostBoundNetLogBypassEvent( + const net::BoundNetLog& net_log, + net::NetLog::EventType type, + net::NetLog::EventPhase phase, + int64 expiration_ticks, + const net::NetLog::ParametersCallback& callback); + + // Prepare and post a secure proxy check event for the event store on a + // BoundNetLog. + void PostBoundNetLogSecureProxyCheckEvent( + const net::BoundNetLog& net_log, + net::NetLog::EventType type, + net::NetLog::EventPhase phase, + DataReductionProxyEventStorageDelegate::SecureProxyCheckState state, + const net::NetLog::ParametersCallback& callback); + + // Must outlive |this|. Used for posting calls to the UI thread. + DataReductionProxyEventStorageDelegate* storage_delegate_; + + // Enforce usage on the IO thread. + base::ThreadChecker thread_checker_; + + DISALLOW_COPY_AND_ASSIGN(DataReductionProxyEventCreator); +}; + +} // namespace data_reduction_proxy +#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_CREATOR_H_ diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h new file mode 100644 index 0000000..d80bcaf --- /dev/null +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h @@ -0,0 +1,42 @@ +// 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_COMMON_DATA_REDUCTION_PROXY_EVENT_STORAGE_DELEGATE_H_ +#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_STORAGE_DELEGATE_H_ + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" + +namespace base { +class Value; +} + +namespace data_reduction_proxy { + +// Defines an interface for storing Data Reduction Proxy events. +class DataReductionProxyEventStorageDelegate { + public: + enum SecureProxyCheckState { + CHECK_UNKNOWN, + CHECK_PENDING, + CHECK_SUCCESS, + CHECK_FAILED, + }; + + // Stores a DATA_REDUCTION_PROXY_ENABLED event. + virtual void AddEnabledEvent(scoped_ptr<base::Value> event, bool enabled) = 0; + + // Stores a DATA_REDUCTION_PROXY_BYPASS_REQUESTED event. + virtual void AddAndSetLastBypassEvent(scoped_ptr<base::Value> event, + int64 expiration_ticks) = 0; + + // Stores a DATA_REDUCTION_PROXY_CANARY_REQUEST event. + virtual void AddEventAndSecureProxyCheckState( + scoped_ptr<base::Value> event, + SecureProxyCheckState state) = 0; +}; + +} // namespace data_reduction_proxy + +#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_STORAGE_DELEGATE_H_ diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.cc new file mode 100644 index 0000000..a20a68e --- /dev/null +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.cc @@ -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. + +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.h" + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "base/values.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" + +namespace data_reduction_proxy { + +TestDataReductionProxyEventStorageDelegate:: + TestDataReductionProxyEventStorageDelegate() + : delegate_(nullptr) { +} + +TestDataReductionProxyEventStorageDelegate:: + ~TestDataReductionProxyEventStorageDelegate() { +} + +void TestDataReductionProxyEventStorageDelegate::SetStorageDelegate( + DataReductionProxyEventStorageDelegate* delegate) { + delegate_ = delegate; +} + +void TestDataReductionProxyEventStorageDelegate::AddEnabledEvent( + scoped_ptr<base::Value> event, + bool enabled) { + if (delegate_) + delegate_->AddEnabledEvent(event.Pass(), enabled); +} + +void TestDataReductionProxyEventStorageDelegate::AddAndSetLastBypassEvent( + scoped_ptr<base::Value> event, + int64 expiration_ticks) { + if (delegate_) + delegate_->AddAndSetLastBypassEvent(event.Pass(), expiration_ticks); +} + +void TestDataReductionProxyEventStorageDelegate:: + AddEventAndSecureProxyCheckState(scoped_ptr<base::Value> event, + SecureProxyCheckState state) { + if (delegate_) + delegate_->AddEventAndSecureProxyCheckState(event.Pass(), state); +} + +} // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.h b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.h new file mode 100644 index 0000000..65d3116 --- /dev/null +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.h @@ -0,0 +1,44 @@ +// 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_COMMON_DATA_REDUCTION_PROXY_EVENT_STORE_TEST_UTILS_H_ +#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_STORE_TEST_UTILS_H_ + +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" + +namespace base { +class Value; +} + +namespace data_reduction_proxy { + +class TestDataReductionProxyEventStorageDelegate + : public DataReductionProxyEventStorageDelegate { + public: + TestDataReductionProxyEventStorageDelegate(); + + virtual ~TestDataReductionProxyEventStorageDelegate(); + + // Sets |delegate_| at a later point in time. + void SetStorageDelegate(DataReductionProxyEventStorageDelegate* delegate); + + // Overrides of DataReductionProxyEventStorageDelegate: + void AddEnabledEvent(scoped_ptr<base::Value> event, bool enabled) override; + void AddAndSetLastBypassEvent(scoped_ptr<base::Value> event, + int64 expiration_ticks) override; + void AddEventAndSecureProxyCheckState(scoped_ptr<base::Value> event, + SecureProxyCheckState state) override; + + private: + // If not null, |this| will send DataReductionProxyEventStorageDelegate + // calls to |delegate_|. + DataReductionProxyEventStorageDelegate* delegate_; +}; + +} // namespace data_reduction_proxy + +#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_STORE_TEST_UTILS_H_ diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.cc index 9612dee..b0734b6 100644 --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.cc +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.cc @@ -5,19 +5,10 @@ #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" #include "base/basictypes.h" -#include "base/bind.h" -#include "base/location.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "base/single_thread_task_runner.h" #include "base/stl_util.h" -#include "base/strings/string_number_conversions.h" #include "base/time/time.h" #include "base/values.h" -#include "net/base/host_port_pair.h" -#include "net/log/net_log.h" -#include "net/proxy/proxy_server.h" -#include "url/gurl.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" namespace { @@ -35,105 +26,6 @@ const StringToConstant kDataReductionProxyBypassEventTypeTable[] = { #undef BYPASS_EVENT_TYPE }; -scoped_ptr<base::Value> BuildDataReductionProxyEvent( - net::NetLog::EventType type, - const net::NetLog::Source& source, - net::NetLog::EventPhase phase, - const net::NetLog::ParametersCallback& parameters_callback) { - base::TimeTicks ticks_now = base::TimeTicks::Now(); - net::NetLog::EntryData entry_data( - type, source, phase, ticks_now, ¶meters_callback); - net::NetLog::Entry entry(&entry_data, net::NetLog::LOG_ALL); - scoped_ptr<base::Value> entry_value(entry.ToValue()); - - return entry_value; -} - -int64 GetExpirationTicks(int bypass_seconds) { - base::TimeTicks ticks_now = base::TimeTicks::Now(); - base::TimeTicks expiration_ticks = - ticks_now + base::TimeDelta::FromSeconds(bypass_seconds); - return (expiration_ticks - base::TimeTicks()).InMilliseconds(); -} - -// The following method creates a string resembling the output of -// net::ProxyServer::ToURI(). -std::string GetNormalizedProxyString(const std::string& proxy_origin) { - net::ProxyServer proxy_server = net::ProxyServer::FromURI( - proxy_origin, net::ProxyServer::SCHEME_HTTP); - if (proxy_server.is_valid()) - return proxy_origin; - else - return std::string(); -} - -// The following callbacks create a base::Value which contains information -// about various data reduction proxy events. Ownership of the base::Value is -// passed to the caller. -base::Value* EnableDataReductionProxyCallback( - bool primary_restricted, - bool fallback_restricted, - const std::string& primary_origin, - const std::string& fallback_origin, - const std::string& ssl_origin, - net::NetLog::LogLevel /* log_level */) { - base::DictionaryValue* dict = new base::DictionaryValue(); - dict->SetBoolean("enabled", true); - dict->SetBoolean("primary_restricted", primary_restricted); - dict->SetBoolean("fallback_restricted", fallback_restricted); - dict->SetString("primary_origin", GetNormalizedProxyString(primary_origin)); - dict->SetString("fallback_origin", GetNormalizedProxyString(fallback_origin)); - dict->SetString("ssl_origin", GetNormalizedProxyString(ssl_origin)); - return dict; -} - -base::Value* DisableDataReductionProxyCallback( - net::NetLog::LogLevel /* log_level */) { - base::DictionaryValue* dict = new base::DictionaryValue(); - dict->SetBoolean("enabled", false); - return dict; -} - -base::Value* UrlBypassActionCallback(const std::string& action, - const GURL& url, - int bypass_seconds, - int64 expiration_ticks, - net::NetLog::LogLevel /* log_level */) { - base::DictionaryValue* dict = new base::DictionaryValue(); - dict->SetString("action", action); - dict->SetString("url", url.spec()); - dict->SetString("bypass_duration_seconds", - base::Int64ToString(bypass_seconds)); - dict->SetString("expiration", base::Int64ToString(expiration_ticks)); - return dict; -} - -base::Value* UrlBypassTypeCallback( - data_reduction_proxy::DataReductionProxyBypassType bypass_type, - const GURL& url, - int bypass_seconds, - int64 expiration_ticks, - net::NetLog::LogLevel /* log_level */) { - base::DictionaryValue* dict = new base::DictionaryValue(); - dict->SetInteger("bypass_type", bypass_type); - dict->SetString("url", url.spec()); - dict->SetString("bypass_duration_seconds", - base::Int64ToString(bypass_seconds)); - dict->SetString("expiration", base::Int64ToString(expiration_ticks)); - return dict; -} - -base::Value* EndCanaryRequestCallback(int net_error, - int http_response_code, - bool succeeded, - net::NetLog::LogLevel /* log_level */) { - base::DictionaryValue* dict = new base::DictionaryValue(); - dict->SetInteger("net_error", net_error); - dict->SetInteger("http_response_code", http_response_code); - dict->SetBoolean("check_succeeded", succeeded); - return dict; -} - } // namespace namespace data_reduction_proxy { @@ -151,10 +43,8 @@ void DataReductionProxyEventStore::AddConstants( constants_dict->Set("dataReductionProxyBypassEventType", dict); } -DataReductionProxyEventStore::DataReductionProxyEventStore( - const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) - : ui_task_runner_(ui_task_runner), - enabled_(false), +DataReductionProxyEventStore::DataReductionProxyEventStore() + : enabled_(false), secure_proxy_check_state_(CHECK_UNKNOWN), expiration_ticks_(0) { } @@ -163,161 +53,8 @@ DataReductionProxyEventStore::~DataReductionProxyEventStore() { STLDeleteElements(&stored_events_); } -void DataReductionProxyEventStore::AddProxyEnabledEvent( - net::NetLog* net_log, - bool primary_restricted, - bool fallback_restricted, - const std::string& primary_origin, - const std::string& fallback_origin, - const std::string& ssl_origin) { - const net::NetLog::ParametersCallback& parameters_callback = - base::Bind(&EnableDataReductionProxyCallback, primary_restricted, - fallback_restricted, primary_origin, fallback_origin, - ssl_origin); - PostEnabledEvent(net_log, - net::NetLog::TYPE_DATA_REDUCTION_PROXY_ENABLED, - true, - parameters_callback); -} - -void DataReductionProxyEventStore::AddProxyDisabledEvent( - net::NetLog* net_log) { - const net::NetLog::ParametersCallback& parameters_callback = - base::Bind(&DisableDataReductionProxyCallback); - PostEnabledEvent(net_log, - net::NetLog::TYPE_DATA_REDUCTION_PROXY_ENABLED, - false, - parameters_callback); -} - -void DataReductionProxyEventStore::AddBypassActionEvent( - const net::BoundNetLog& net_log, - const std::string& bypass_action, - const GURL& url, - const base::TimeDelta& bypass_duration) { - int64 expiration_ticks = GetExpirationTicks(bypass_duration.InSeconds()); - const net::NetLog::ParametersCallback& parameters_callback = - base::Bind(&UrlBypassActionCallback, bypass_action, url, - bypass_duration.InSeconds(), expiration_ticks); - PostBoundNetLogBypassEvent( - net_log, - net::NetLog::TYPE_DATA_REDUCTION_PROXY_BYPASS_REQUESTED, - net::NetLog::PHASE_NONE, - expiration_ticks, - parameters_callback); -} - -void DataReductionProxyEventStore::AddBypassTypeEvent( - const net::BoundNetLog& net_log, - DataReductionProxyBypassType bypass_type, - const GURL& url, - const base::TimeDelta& bypass_duration) { - int64 expiration_ticks = GetExpirationTicks(bypass_duration.InSeconds()); - const net::NetLog::ParametersCallback& parameters_callback = - base::Bind(&UrlBypassTypeCallback, bypass_type, url, - bypass_duration.InSeconds(), expiration_ticks); - PostBoundNetLogBypassEvent( - net_log, - net::NetLog::TYPE_DATA_REDUCTION_PROXY_BYPASS_REQUESTED, - net::NetLog::PHASE_NONE, - expiration_ticks, - parameters_callback); -} - -void DataReductionProxyEventStore::BeginSecureProxyCheck( - const net::BoundNetLog& net_log, - const GURL& url) { - // This callback must be invoked synchronously - const net::NetLog::ParametersCallback& parameters_callback = - net::NetLog::StringCallback("url", &url.spec()); - PostBoundNetLogSecureProxyCheckEvent( - net_log, - net::NetLog::TYPE_DATA_REDUCTION_PROXY_CANARY_REQUEST, - net::NetLog::PHASE_BEGIN, - CHECK_PENDING, - parameters_callback); -} - -void DataReductionProxyEventStore::EndSecureProxyCheck( - const net::BoundNetLog& net_log, - int net_error, - int http_response_code, - bool succeeded) { - const net::NetLog::ParametersCallback& parameters_callback = base::Bind( - &EndCanaryRequestCallback, net_error, http_response_code, succeeded); - PostBoundNetLogSecureProxyCheckEvent( - net_log, - net::NetLog::TYPE_DATA_REDUCTION_PROXY_CANARY_REQUEST, - net::NetLog::PHASE_END, - net_error == 0 ? CHECK_SUCCESS : CHECK_FAILED, - parameters_callback); -} - -void DataReductionProxyEventStore::PostEnabledEvent( - net::NetLog* net_log, - net::NetLog::EventType type, - bool enabled, - const net::NetLog::ParametersCallback& callback) { - scoped_ptr<base::Value> event = BuildDataReductionProxyEvent( - type, net::NetLog::Source(), net::NetLog::PHASE_NONE, callback); - if (event.get()) { - ui_task_runner_->PostTask( - FROM_HERE, - base::Bind(&DataReductionProxyEventStore::AddEnabledEventOnUIThread, - base::Unretained(this), - base::Passed(&event), - enabled)); - } - - if (net_log) - net_log->AddGlobalEntry(type, callback); -} - -void DataReductionProxyEventStore::PostBoundNetLogBypassEvent( - const net::BoundNetLog& net_log, - net::NetLog::EventType type, - net::NetLog::EventPhase phase, - int64 expiration_ticks, - const net::NetLog::ParametersCallback& callback) { - scoped_ptr<base::Value> event = BuildDataReductionProxyEvent( - type, net_log.source(), phase, callback); - if (event.get()) { - ui_task_runner_->PostTask( - FROM_HERE, - base::Bind( - &DataReductionProxyEventStore::AddAndSetLastBypassEventOnUIThread, - base::Unretained(this), - base::Passed(&event), - expiration_ticks)); - } - - net_log.AddEntry(type, phase, callback); -} - -void DataReductionProxyEventStore::PostBoundNetLogSecureProxyCheckEvent( - const net::BoundNetLog& net_log, - net::NetLog::EventType type, - net::NetLog::EventPhase phase, - SecureProxyCheckState state, - const net::NetLog::ParametersCallback& callback) { - scoped_ptr<base::Value> event( - BuildDataReductionProxyEvent(type, net_log.source(), phase, callback)); - if (event.get()) { - ui_task_runner_->PostTask( - FROM_HERE, - base::Bind( - &DataReductionProxyEventStore:: - AddEventAndSecureProxyCheckStateOnUIThread, - base::Unretained(this), - base::Passed(&event), - state)); - } - net_log.AddEntry(type, phase, callback); -} - base::Value* DataReductionProxyEventStore::GetSummaryValue() const { - DCHECK(ui_task_runner_->BelongsToCurrentThread()); - + DCHECK(thread_checker_.CalledOnValidThread()); scoped_ptr<base::DictionaryValue> data_reduction_proxy_values( new base::DictionaryValue()); data_reduction_proxy_values->SetBoolean("enabled", enabled_); @@ -364,9 +101,7 @@ base::Value* DataReductionProxyEventStore::GetSummaryValue() const { return data_reduction_proxy_values.release(); } -void DataReductionProxyEventStore::AddEventOnUIThread( - scoped_ptr<base::Value> entry) { - DCHECK(ui_task_runner_->BelongsToCurrentThread()); +void DataReductionProxyEventStore::AddEvent(scoped_ptr<base::Value> entry) { if (stored_events_.size() == kMaxEventsToStore) { base::Value* head = stored_events_.front(); stored_events_.pop_front(); @@ -376,33 +111,33 @@ void DataReductionProxyEventStore::AddEventOnUIThread( stored_events_.push_back(entry.release()); } -void DataReductionProxyEventStore::AddEnabledEventOnUIThread( +void DataReductionProxyEventStore::AddEnabledEvent( scoped_ptr<base::Value> entry, bool enabled) { - DCHECK(ui_task_runner_->BelongsToCurrentThread()); + DCHECK(thread_checker_.CalledOnValidThread()); enabled_ = enabled; if (enabled) current_configuration_.reset(entry->DeepCopy()); else current_configuration_.reset(); - AddEventOnUIThread(entry.Pass()); + AddEvent(entry.Pass()); } -void DataReductionProxyEventStore::AddEventAndSecureProxyCheckStateOnUIThread( +void DataReductionProxyEventStore::AddEventAndSecureProxyCheckState( scoped_ptr<base::Value> entry, SecureProxyCheckState state) { - DCHECK(ui_task_runner_->BelongsToCurrentThread()); + DCHECK(thread_checker_.CalledOnValidThread()); secure_proxy_check_state_ = state; - AddEventOnUIThread(entry.Pass()); + AddEvent(entry.Pass()); } -void DataReductionProxyEventStore::AddAndSetLastBypassEventOnUIThread( +void DataReductionProxyEventStore::AddAndSetLastBypassEvent( scoped_ptr<base::Value> entry, int64 expiration_ticks) { - DCHECK(ui_task_runner_->BelongsToCurrentThread()); + DCHECK(thread_checker_.CalledOnValidThread()); last_bypass_event_.reset(entry->DeepCopy()); expiration_ticks_ = expiration_ticks; - AddEventOnUIThread(entry.Pass()); + AddEvent(entry.Pass()); } } // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h index 3105f67..e55eb04 100644 --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h @@ -6,92 +6,33 @@ #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_COMMON_DATA_REDUCTION_PROXY_EVENT_STORE_H_ #include <deque> -#include <string> #include "base/basictypes.h" #include "base/gtest_prod_util.h" -#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "base/values.h" +#include "base/threading/thread_checker.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" -#include "net/log/net_log.h" - -class GURL; namespace base { -class SingleThreadTaskRunner; +class DictionaryValue; class TimeDelta; class Value; } -namespace net { -class BoundNetLog; -class NetLog; -} - namespace data_reduction_proxy { -enum SecureProxyCheckState { - CHECK_UNKNOWN, - CHECK_PENDING, - CHECK_SUCCESS, - CHECK_FAILED, -}; - -class DataReductionProxyEventStore { +class DataReductionProxyEventStore + : public DataReductionProxyEventStorageDelegate { public: // Adds data reduction proxy specific constants to the net_internals // constants dictionary. static void AddConstants(base::DictionaryValue* constants_dict); - // Constructs a DataReductionProxyEventStore object with the given UI - // task runner. - explicit DataReductionProxyEventStore( - const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); - - ~DataReductionProxyEventStore(); - - // Adds the DATA_REDUCTION_PROXY_ENABLED event (with enabled=true) to the - // event store. - void AddProxyEnabledEvent( - net::NetLog* net_log, - bool primary_restricted, - bool fallback_restricted, - const std::string& primary_origin, - const std::string& fallback_origin, - const std::string& ssl_origin); - - // Adds the DATA_REDUCTION_PROXY_ENABLED event (with enabled=false) to the - // event store. - void AddProxyDisabledEvent(net::NetLog* net_log); - - // Adds a DATA_REDUCTION_PROXY_BYPASS_REQUESTED event to the event store - // when the bypass reason is initiated by the data reduction proxy. - void AddBypassActionEvent( - const net::BoundNetLog& net_log, - const std::string& bypass_action, - const GURL& gurl, - const base::TimeDelta& bypass_duration); - - // Adds a DATA_REDUCTION_PROXY_BYPASS_REQUESTED event to the event store - // when the bypass reason is not initiated by the data reduction proxy, such - // as network errors. - void AddBypassTypeEvent( - const net::BoundNetLog& net_log, - DataReductionProxyBypassType bypass_type, - const GURL& gurl, - const base::TimeDelta& bypass_duration); - - // Adds a DATA_REDUCTION_PROXY_CANARY_REQUEST event to the event store - // when the secure proxy request has started. - void BeginSecureProxyCheck(const net::BoundNetLog& net_log, const GURL& gurl); - - // Adds a DATA_REDUCTION_PROXY_CANARY_REQUEST event to the event store - // when the secure proxy request has ended. - void EndSecureProxyCheck(const net::BoundNetLog& net_log, - int net_error, - int http_response_code, - bool succeeded); + // Constructs a DataReductionProxyEventStore object + explicit DataReductionProxyEventStore(); + + virtual ~DataReductionProxyEventStore(); // Creates a Value summary of Data Reduction Proxy related information: // - Whether the proxy is enabled @@ -101,6 +42,22 @@ class DataReductionProxyEventStore { // The caller is responsible for deleting the returned value. base::Value* GetSummaryValue() const; + // Override of DataReductionProxyEventStorageDelegate. + // Put |entry| on the deque of stored events and set |current_configuration_|. + void AddEnabledEvent(scoped_ptr<base::Value> entry, bool enabled) override; + + // Override of DataReductionProxyEventStorageDelegate. + // Put |entry| on a deque of events to store and set + // |secure_proxy_check_state_| + void AddEventAndSecureProxyCheckState(scoped_ptr<base::Value> entry, + SecureProxyCheckState state) override; + + // Override of DataReductionProxyEventStorageDelegate. + // Put |entry| on a deque of events to store and set |last_bypass_event_| and + // |expiration_ticks_| + void AddAndSetLastBypassEvent(scoped_ptr<base::Value> entry, + int64 expiration_ticks) override; + private: friend class DataReductionProxyEventStoreTest; FRIEND_TEST_ALL_PREFIXES(DataReductionProxyEventStoreTest, @@ -116,50 +73,9 @@ class DataReductionProxyEventStore { FRIEND_TEST_ALL_PREFIXES(DataReductionProxyEventStoreTest, TestEndSecureProxyCheck); - // Prepare and post enabling/disabling proxy events for the event_store on the - // global net_log. - void PostEnabledEvent(net::NetLog* net_log, - net::NetLog::EventType type, - bool enable, - const net::NetLog::ParametersCallback& callback); - - // Prepare and post a Data Reduction Proxy bypass event for the event_store - // on a BoundNetLog. - void PostBoundNetLogBypassEvent( - const net::BoundNetLog& net_log, - net::NetLog::EventType type, - net::NetLog::EventPhase phase, - int64 expiration_ticks, - const net::NetLog::ParametersCallback& callback); - - // Prepare and post a secure proxy check event for the event_store on a - // BoundNetLog. - void PostBoundNetLogSecureProxyCheckEvent( - const net::BoundNetLog& net_log, - net::NetLog::EventType type, - net::NetLog::EventPhase phase, - SecureProxyCheckState state, - const net::NetLog::ParametersCallback& callback); - // Put |entry| on a deque of events to store - void AddEventOnUIThread(scoped_ptr<base::Value> entry); - - // Put |entry| on the deque of stored events and set |current_configuration_|. - void AddEnabledEventOnUIThread(scoped_ptr<base::Value> entry, bool enabled); - - // Put |entry| on a deque of events to store and set - // |secure_proxy_check_state_| - void AddEventAndSecureProxyCheckStateOnUIThread(scoped_ptr<base::Value> entry, - SecureProxyCheckState state); - - // Put |entry| on a deque of events to store and set |last_bypass_event_| and - // |expiration_ticks_| - void AddAndSetLastBypassEventOnUIThread(scoped_ptr<base::Value> entry, - int64 expiration_ticks); + void AddEvent(scoped_ptr<base::Value> entry); - // A task runner to ensure that all reads/writes to |stored_events_| takes - // place on the UI thread. - scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; // A deque of data reduction proxy related events. It is used as a circular // buffer to prevent unbounded memory utilization. std::deque<base::Value*> stored_events_; @@ -174,6 +90,9 @@ class DataReductionProxyEventStore { // The expiration time of the |last_bypass_event_|. int64 expiration_ticks_; + // Enforce usage on the UI thread. + base::ThreadChecker thread_checker_; + DISALLOW_COPY_AND_ASSIGN(DataReductionProxyEventStore); }; diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store_unittest.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store_unittest.cc index f476479..05c996b 100644 --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store_unittest.cc +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_event_store_unittest.cc @@ -5,10 +5,10 @@ #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" #include "base/bind.h" -#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "base/strings/string_number_conversions.h" -#include "base/test/test_simple_task_runner.h" +#include "base/time/time.h" +#include "base/values.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h" #include "net/http/http_status_code.h" #include "net/log/net_log.h" @@ -19,16 +19,15 @@ namespace data_reduction_proxy { class DataReductionProxyEventStoreTest : public testing::Test { public: - DataReductionProxyEventStoreTest() - : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>( - new base::TestSimpleTaskRunner())), - net_log_(new net::TestNetLog()) { + DataReductionProxyEventStoreTest() : net_log_(new net::TestNetLog()) { bound_net_log_ = net::BoundNetLog::Make( net_log_.get(), net::NetLog::SOURCE_DATA_REDUCTION_PROXY); } void SetUp() override { - proxy_.reset(new DataReductionProxyEventStore(task_runner_)); + event_store_.reset(new DataReductionProxyEventStore()); + event_creator_.reset( + new DataReductionProxyEventCreator(event_store_.get())); } net::TestNetLog::CapturedEntry GetSingleEntry() const { @@ -38,12 +37,10 @@ class DataReductionProxyEventStoreTest : public testing::Test { return entries[0]; } - DataReductionProxyEventStore* proxy() { - return proxy_.get(); - } + DataReductionProxyEventStore* event_store() { return event_store_.get(); } - base::TestSimpleTaskRunner* task_runner() { - return task_runner_.get(); + DataReductionProxyEventCreator* event_creator() { + return event_creator_.get(); } net::TestNetLog* net_log() { return net_log_.get(); } @@ -53,87 +50,84 @@ class DataReductionProxyEventStoreTest : public testing::Test { } private: - scoped_refptr<base::TestSimpleTaskRunner> task_runner_; scoped_ptr<net::TestNetLog> net_log_; - scoped_ptr<DataReductionProxyEventStore> proxy_; + scoped_ptr<DataReductionProxyEventStore> event_store_; + scoped_ptr<DataReductionProxyEventCreator> event_creator_; net::BoundNetLog bound_net_log_; }; TEST_F(DataReductionProxyEventStoreTest, TestAddProxyEnabledEvent) { - EXPECT_EQ(0u, proxy()->stored_events_.size()); - proxy()->AddProxyEnabledEvent( - net_log(), false, false, - TestDataReductionProxyParams::DefaultOrigin(), + EXPECT_EQ(0u, event_store()->stored_events_.size()); + event_creator()->AddProxyEnabledEvent( + net_log(), false, false, TestDataReductionProxyParams::DefaultOrigin(), TestDataReductionProxyParams::DefaultFallbackOrigin(), TestDataReductionProxyParams::DefaultSSLOrigin()); - task_runner()->RunPendingTasks(); - EXPECT_EQ(1u, proxy()->stored_events_.size()); + EXPECT_EQ(1u, event_store()->stored_events_.size()); net::TestNetLog::CapturedEntry entry = GetSingleEntry(); EXPECT_EQ(net::NetLog::TYPE_DATA_REDUCTION_PROXY_ENABLED, entry.type); } TEST_F(DataReductionProxyEventStoreTest, TestAddProxyDisabledEvent) { - EXPECT_EQ(0u, proxy()->stored_events_.size()); - proxy()->AddProxyDisabledEvent(net_log()); - task_runner()->RunPendingTasks(); - EXPECT_EQ(1u, proxy()->stored_events_.size()); + EXPECT_EQ(0u, event_store()->stored_events_.size()); + event_creator()->AddProxyDisabledEvent(net_log()); + EXPECT_EQ(1u, event_store()->stored_events_.size()); net::TestNetLog::CapturedEntry entry = GetSingleEntry(); EXPECT_EQ(net::NetLog::TYPE_DATA_REDUCTION_PROXY_ENABLED, entry.type); } TEST_F(DataReductionProxyEventStoreTest, TestAddBypassActionEvent) { - EXPECT_EQ(0u, proxy()->stored_events_.size()); - EXPECT_EQ(nullptr, proxy()->last_bypass_event_.get()); - proxy()->AddBypassActionEvent(bound_net_log(), "bypass", GURL(), - base::TimeDelta::FromMinutes(1)); - task_runner()->RunPendingTasks(); - EXPECT_EQ(1u, proxy()->stored_events_.size()); + EXPECT_EQ(0u, event_store()->stored_events_.size()); + EXPECT_EQ(nullptr, event_store()->last_bypass_event_.get()); + event_creator()->AddBypassActionEvent(bound_net_log(), "bypass", GURL(), + base::TimeDelta::FromMinutes(1)); + EXPECT_EQ(1u, event_store()->stored_events_.size()); net::TestNetLog::CapturedEntry entry = GetSingleEntry(); EXPECT_EQ(net::NetLog::TYPE_DATA_REDUCTION_PROXY_BYPASS_REQUESTED, entry.type); - EXPECT_NE(nullptr, proxy()->last_bypass_event_.get()); + EXPECT_NE(nullptr, event_store()->last_bypass_event_.get()); } TEST_F(DataReductionProxyEventStoreTest, TestAddBypassTypeEvent) { - EXPECT_EQ(0u, proxy()->stored_events_.size()); - EXPECT_EQ(nullptr, proxy()->last_bypass_event_.get()); - proxy()->AddBypassTypeEvent(bound_net_log(), BYPASS_EVENT_TYPE_LONG, GURL(), - base::TimeDelta::FromMinutes(1)); - task_runner()->RunPendingTasks(); - EXPECT_EQ(1u, proxy()->stored_events_.size()); + EXPECT_EQ(0u, event_store()->stored_events_.size()); + EXPECT_EQ(nullptr, event_store()->last_bypass_event_.get()); + event_creator()->AddBypassTypeEvent(bound_net_log(), BYPASS_EVENT_TYPE_LONG, + GURL(), base::TimeDelta::FromMinutes(1)); + EXPECT_EQ(1u, event_store()->stored_events_.size()); EXPECT_EQ(1u, net_log()->GetSize()); net::TestNetLog::CapturedEntry entry = GetSingleEntry(); EXPECT_EQ(net::NetLog::TYPE_DATA_REDUCTION_PROXY_BYPASS_REQUESTED, entry.type); - EXPECT_NE(nullptr, proxy()->last_bypass_event_.get()); + EXPECT_NE(nullptr, event_store()->last_bypass_event_.get()); } TEST_F(DataReductionProxyEventStoreTest, TestBeginSecureProxyCheck) { - EXPECT_EQ(0u, proxy()->stored_events_.size()); - EXPECT_EQ(CHECK_UNKNOWN, proxy()->secure_proxy_check_state_); - proxy()->BeginSecureProxyCheck(bound_net_log(), GURL()); - task_runner()->RunPendingTasks(); - EXPECT_EQ(1u, proxy()->stored_events_.size()); + EXPECT_EQ(0u, event_store()->stored_events_.size()); + EXPECT_EQ(DataReductionProxyEventStorageDelegate::CHECK_UNKNOWN, + event_store()->secure_proxy_check_state_); + event_creator()->BeginSecureProxyCheck(bound_net_log(), GURL()); + EXPECT_EQ(1u, event_store()->stored_events_.size()); EXPECT_EQ(1u, net_log()->GetSize()); net::TestNetLog::CapturedEntry entry = GetSingleEntry(); EXPECT_EQ(net::NetLog::TYPE_DATA_REDUCTION_PROXY_CANARY_REQUEST, entry.type); - EXPECT_EQ(CHECK_PENDING, proxy()->secure_proxy_check_state_); + EXPECT_EQ(DataReductionProxyEventStorageDelegate::CHECK_PENDING, + event_store()->secure_proxy_check_state_); } TEST_F(DataReductionProxyEventStoreTest, TestEndSecureProxyCheck) { - EXPECT_EQ(0u, proxy()->stored_events_.size()); - EXPECT_EQ(CHECK_UNKNOWN, proxy()->secure_proxy_check_state_); - proxy()->EndSecureProxyCheck(bound_net_log(), 0, net::HTTP_OK, true); - task_runner()->RunPendingTasks(); - EXPECT_EQ(1u, proxy()->stored_events_.size()); + EXPECT_EQ(0u, event_store()->stored_events_.size()); + EXPECT_EQ(DataReductionProxyEventStorageDelegate::CHECK_UNKNOWN, + event_store()->secure_proxy_check_state_); + event_creator()->EndSecureProxyCheck(bound_net_log(), 0, net::HTTP_OK, true); + EXPECT_EQ(1u, event_store()->stored_events_.size()); EXPECT_EQ(1u, net_log()->GetSize()); net::TestNetLog::CapturedEntry entry = GetSingleEntry(); EXPECT_EQ(net::NetLog::TYPE_DATA_REDUCTION_PROXY_CANARY_REQUEST, entry.type); - EXPECT_EQ(CHECK_SUCCESS, proxy()->secure_proxy_check_state_); + EXPECT_EQ(DataReductionProxyEventStorageDelegate::CHECK_SUCCESS, + event_store()->secure_proxy_check_state_); } } // namespace data_reduction_proxy diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.cc index e555cff..12e4c76 100644 --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.cc +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.cc @@ -12,7 +12,7 @@ #include "base/strings/string_piece.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" #include "net/http/http_response_headers.h" #include "net/http/http_status_code.h" @@ -111,13 +111,14 @@ bool ParseHeadersAndSetBypassDuration(const net::HttpResponseHeaders* headers, return false; } -bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers, - const GURL& url, - const net::BoundNetLog& bound_net_log, - DataReductionProxyInfo* proxy_info, - DataReductionProxyEventStore* event_store) { +bool ParseHeadersAndSetProxyInfo( + const net::HttpResponseHeaders* headers, + const GURL& url, + const net::BoundNetLog& bound_net_log, + DataReductionProxyInfo* proxy_info, + DataReductionProxyEventCreator* event_creator) { DCHECK(proxy_info); - DCHECK(event_store); + DCHECK(event_creator); // Support header of the form Chrome-Proxy: bypass|block=<duration>, where // <duration> is the number of seconds to wait before retrying @@ -134,8 +135,8 @@ bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers, headers, kChromeProxyActionBlock, &proxy_info->bypass_duration)) { proxy_info->bypass_all = true; proxy_info->mark_proxies_as_bad = true; - event_store->AddBypassActionEvent(bound_net_log, kChromeProxyActionBlock, - url, proxy_info->bypass_duration); + event_creator->AddBypassActionEvent(bound_net_log, kChromeProxyActionBlock, + url, proxy_info->bypass_duration); return true; } @@ -144,8 +145,8 @@ bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers, headers, kChromeProxyActionBypass, &proxy_info->bypass_duration)) { proxy_info->bypass_all = false; proxy_info->mark_proxies_as_bad = true; - event_store->AddBypassActionEvent(bound_net_log, kChromeProxyActionBypass, - url, proxy_info->bypass_duration); + event_creator->AddBypassActionEvent(bound_net_log, kChromeProxyActionBypass, + url, proxy_info->bypass_duration); return true; } @@ -159,9 +160,9 @@ bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers, proxy_info->bypass_all = true; proxy_info->mark_proxies_as_bad = false; proxy_info->bypass_duration = TimeDelta(); - event_store->AddBypassActionEvent(bound_net_log, - kChromeProxyActionBlockOnce, url, - proxy_info->bypass_duration); + event_creator->AddBypassActionEvent(bound_net_log, + kChromeProxyActionBlockOnce, url, + proxy_info->bypass_duration); return true; } @@ -198,14 +199,11 @@ DataReductionProxyBypassType GetDataReductionProxyBypassType( const GURL& url, const net::BoundNetLog& bound_net_log, DataReductionProxyInfo* data_reduction_proxy_info, - DataReductionProxyEventStore* event_store, + DataReductionProxyEventCreator* event_creator, bool* event_logged) { DCHECK(data_reduction_proxy_info); - if (ParseHeadersAndSetProxyInfo(headers, - url, - bound_net_log, - data_reduction_proxy_info, - event_store)) { + if (ParseHeadersAndSetProxyInfo(headers, url, bound_net_log, + data_reduction_proxy_info, event_creator)) { *event_logged = true; // A chrome-proxy response header is only present in a 502. For proper diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h b/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h index 3df1496..3d37778 100644 --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h @@ -21,7 +21,7 @@ class HttpResponseHeaders; namespace data_reduction_proxy { -class DataReductionProxyEventStore; +class DataReductionProxyEventCreator; // Values of the UMA DataReductionProxy.BypassType{Primary|Fallback} and // DataReductionProxy.BlockType{Primary|Fallback} histograms. This enum must @@ -61,7 +61,7 @@ bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers, const GURL& url, const net::BoundNetLog& bound_net_log, DataReductionProxyInfo* proxy_info, - DataReductionProxyEventStore* event_store); + DataReductionProxyEventCreator* event_creator); // Returns true if the response contains the data reduction proxy Via header // value. If non-NULL, sets |has_intermediary| to true if another server added @@ -79,7 +79,7 @@ DataReductionProxyBypassType GetDataReductionProxyBypassType( const GURL& url, const net::BoundNetLog& bound_net_log, DataReductionProxyInfo* proxy_info, - DataReductionProxyEventStore* event_store, + DataReductionProxyEventCreator* event_creator, bool* event_logged); // Searches for the specified Chrome-Proxy action, and if present saves its diff --git a/components/data_reduction_proxy/core/common/data_reduction_proxy_headers_unittest.cc b/components/data_reduction_proxy/core/common/data_reduction_proxy_headers_unittest.cc index cda2159..460459a 100644 --- a/components/data_reduction_proxy/core/common/data_reduction_proxy_headers_unittest.cc +++ b/components/data_reduction_proxy/core/common/data_reduction_proxy_headers_unittest.cc @@ -6,8 +6,8 @@ #include <vector> -#include "base/test/test_simple_task_runner.h" -#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_creator.h" +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate_test_utils.h" #include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers_test_utils.h" #include "net/http/http_response_headers.h" #include "net/proxy/proxy_service.h" @@ -16,17 +16,20 @@ namespace data_reduction_proxy { class DataReductionProxyHeadersTest : public testing::Test { - public: - DataReductionProxyHeadersTest() - : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>( - new base::TestSimpleTaskRunner())) {} - + protected: void SetUp() override { - event_store_.reset(new DataReductionProxyEventStore(task_runner_)); + storage_delegate_.reset(new TestDataReductionProxyEventStorageDelegate()); + event_creator_.reset( + new DataReductionProxyEventCreator(storage_delegate_.get())); + } + + DataReductionProxyEventCreator* event_creator() const { + return event_creator_.get(); } - scoped_refptr<base::TestSimpleTaskRunner> task_runner_; - scoped_ptr<DataReductionProxyEventStore> event_store_; + private: + scoped_ptr<DataReductionProxyEventCreator> event_creator_; + scoped_ptr<TestDataReductionProxyEventStorageDelegate> storage_delegate_; }; TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyActionValue) { @@ -364,13 +367,10 @@ TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) { new net::HttpResponseHeaders(headers)); DataReductionProxyInfo data_reduction_proxy_info; - EXPECT_EQ( - tests[i].expected_result, - ParseHeadersAndSetProxyInfo(parsed.get(), - GURL(), - net::BoundNetLog(), - &data_reduction_proxy_info, - event_store_.get())); + EXPECT_EQ(tests[i].expected_result, + ParseHeadersAndSetProxyInfo( + parsed.get(), GURL(), net::BoundNetLog(), + &data_reduction_proxy_info, event_creator())); EXPECT_EQ(tests[i].expected_retry_delay, data_reduction_proxy_info.bypass_duration.InSeconds()); EXPECT_EQ(tests[i].expected_bypass_all, @@ -392,11 +392,8 @@ TEST_F(DataReductionProxyHeadersTest, ParseHeadersAndSetProxyInfo) { DataReductionProxyInfo data_reduction_proxy_info; EXPECT_TRUE( - ParseHeadersAndSetProxyInfo(parsed.get(), - GURL(), - net::BoundNetLog(), - &data_reduction_proxy_info, - event_store_.get())); + ParseHeadersAndSetProxyInfo(parsed.get(), GURL(), net::BoundNetLog(), + &data_reduction_proxy_info, event_creator())); EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds()); EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds()); EXPECT_FALSE(data_reduction_proxy_info.bypass_all); @@ -608,15 +605,10 @@ TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { new net::HttpResponseHeaders(headers)); DataReductionProxyInfo chrome_proxy_info; bool event_was_logged; - EXPECT_EQ( - tests[i].expected_result, - GetDataReductionProxyBypassType( - parsed.get(), - GURL(), - net::BoundNetLog(), - &chrome_proxy_info, - event_store_.get(), - &event_was_logged)); + EXPECT_EQ(tests[i].expected_result, + GetDataReductionProxyBypassType( + parsed.get(), GURL(), net::BoundNetLog(), &chrome_proxy_info, + event_creator(), &event_was_logged)); } } |