| Index: components/data_reduction_proxy/core/browser/data_saver_service.cc
|
| diff --git a/components/data_reduction_proxy/core/browser/data_saver_service.cc b/components/data_reduction_proxy/core/browser/data_saver_service.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1c4b11c91c50f0d65c953170eb738b6aea10618e
|
| --- /dev/null
|
| +++ b/components/data_reduction_proxy/core/browser/data_saver_service.cc
|
| @@ -0,0 +1,83 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/data_reduction_proxy/core/browser/data_saver_service.h"
|
| +
|
| +#include "base/sequenced_task_runner.h"
|
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
|
| +#include "net/base/load_flags.h"
|
| +#include "net/url_request/url_fetcher.h"
|
| +#include "net/url_request/url_request_status.h"
|
| +
|
| +namespace data_reduction_proxy {
|
| +
|
| +DataSaverService::DataSaverService(
|
| + scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs,
|
| + DataReductionProxySettings* settings,
|
| + net::URLRequestContextGetter* request_context)
|
| + : url_request_context_getter_(request_context),
|
| + settings_(settings),
|
| + weak_factory_(this) {
|
| + DCHECK(settings);
|
| + statistics_prefs_ = statistics_prefs.Pass();
|
| +}
|
| +
|
| +DataSaverService::~DataSaverService() {
|
| +}
|
| +
|
| +void DataSaverService::Shutdown() {
|
| + DCHECK(CalledOnValidThread());
|
| + weak_factory_.InvalidateWeakPtrs();
|
| +}
|
| +
|
| +void DataSaverService::EnableCompressionStatisticsLogging(
|
| + PrefService* prefs,
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + const base::TimeDelta& commit_delay) {
|
| + DCHECK(!statistics_prefs_);
|
| + statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
|
| + prefs, ui_task_runner, commit_delay));
|
| +}
|
| +
|
| +base::WeakPtr<DataSaverService> DataSaverService::GetWeakPtr() {
|
| + DCHECK(CalledOnValidThread());
|
| + return weak_factory_.GetWeakPtr();
|
| +}
|
| +
|
| +void DataSaverService::OnURLFetchComplete(const net::URLFetcher* source) {
|
| + DCHECK(source == fetcher_.get());
|
| + net::URLRequestStatus status = source->GetStatus();
|
| +
|
| + std::string response;
|
| + source->GetResponseAsString(&response);
|
| +
|
| + fetcher_callback_.Run(response, status);
|
| +}
|
| +
|
| +net::URLFetcher* DataSaverService::GetURLFetcherForProbe(
|
| + const GURL& probe_url) {
|
| + net::URLFetcher* fetcher =
|
| + net::URLFetcher::Create(probe_url, net::URLFetcher::GET, this);
|
| + fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY);
|
| + DCHECK(url_request_context_getter_);
|
| + fetcher->SetRequestContext(url_request_context_getter_);
|
| + // Configure max retries to be at most kMaxRetries times for 5xx errors.
|
| + static const int kMaxRetries = 5;
|
| + fetcher->SetMaxRetriesOn5xx(kMaxRetries);
|
| + fetcher->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries);
|
| + return fetcher;
|
| +}
|
| +
|
| +void DataSaverService::CheckProbeURL(const GURL& probe_url,
|
| + FetcherResponseCallback fetcher_callback) {
|
| + DCHECK(CalledOnValidThread());
|
| + net::URLFetcher* fetcher = GetURLFetcherForProbe(probe_url);
|
| + if (!fetcher)
|
| + return;
|
| + fetcher_.reset(fetcher);
|
| + fetcher_callback_ = fetcher_callback;
|
| + fetcher_->Start();
|
| +}
|
| +
|
| +} // namespace data_reduction_proxy
|
|
|