| Index: chrome/browser/net_benchmarking_impl.cc
|
| diff --git a/chrome/browser/net_benchmarking_impl.cc b/chrome/browser/net_benchmarking_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ddf76c4b906c540d43d8e61c713e9e495c7d607d
|
| --- /dev/null
|
| +++ b/chrome/browser/net_benchmarking_impl.cc
|
| @@ -0,0 +1,112 @@
|
| +// Copyright (c) 2012 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 "chrome/browser/net_benchmarking_impl.h"
|
| +
|
| +#include <memory>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/command_line.h"
|
| +#include "base/macros.h"
|
| +#include "chrome/browser/net/predictor.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/common/chrome_switches.h"
|
| +#include "chrome/common/net_benchmarking.mojom.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "mojo/public/cpp/bindings/strong_binding.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/disk_cache/disk_cache.h"
|
| +#include "net/dns/host_cache.h"
|
| +#include "net/dns/host_resolver.h"
|
| +#include "net/http/http_cache.h"
|
| +#include "net/url_request/url_request_context.h"
|
| +#include "net/url_request/url_request_context_getter.h"
|
| +
|
| +using content::BrowserThread;
|
| +
|
| +namespace {
|
| +
|
| +void ClearPredictorCacheOnUIThread(Profile* profile) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| + // TODO(623967): Ensure that the profile or predictor are not accessed after
|
| + // they have been shut down.
|
| + chrome_browser_net::Predictor* predictor = profile->GetNetworkPredictor();
|
| + if (predictor)
|
| + predictor->DiscardAllResultsAndClearPrefsOnUIThread();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +NetBenchmarkingImpl::NetBenchmarkingImpl(
|
| + Profile* profile,
|
| + net::URLRequestContextGetter* request_context)
|
| + : profile_(profile), request_context_(request_context) {}
|
| +
|
| +NetBenchmarkingImpl::~NetBenchmarkingImpl() {
|
| +}
|
| +
|
| +// static
|
| +void NetBenchmarkingImpl::Create(
|
| + Profile* profile,
|
| + net::URLRequestContextGetter* request_context,
|
| + chrome::mojom::NetBenchmarkingRequest request) {
|
| + // Net benchmarking binding is disabled unless the user has enabled
|
| + // benchmarking extensions.
|
| + if (!CheckBenchmarkingEnabled()) {
|
| + return;
|
| + }
|
| + mojo::MakeStrongBinding(
|
| + base::MakeUnique<NetBenchmarkingImpl>(profile, request_context),
|
| + std::move(request));
|
| +}
|
| +
|
| +// static
|
| +bool NetBenchmarkingImpl::CheckBenchmarkingEnabled() {
|
| + static bool checked = false;
|
| + static bool result = false;
|
| + if (!checked) {
|
| + const base::CommandLine& command_line =
|
| + *base::CommandLine::ForCurrentProcess();
|
| + result = command_line.HasSwitch(switches::kEnableNetBenchmarking);
|
| + checked = true;
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +void NetBenchmarkingImpl::ClearCache(
|
| + const ClearCacheCallback& callback) {
|
| + int rv = -1;
|
| +
|
| + disk_cache::Backend* backend = request_context_->GetURLRequestContext()->
|
| + http_transaction_factory()->GetCache()->GetCurrentBackend();
|
| + if (backend) {
|
| + rv = backend->DoomAllEntries(callback);
|
| + if (rv == net::ERR_IO_PENDING) {
|
| + // Callback is handled by backend.
|
| + return;
|
| + }
|
| + }
|
| + callback.Run(rv);
|
| +}
|
| +
|
| +void NetBenchmarkingImpl::ClearHostResolverCache() {
|
| + net::HostCache* cache =
|
| + request_context_->GetURLRequestContext()->host_resolver()->GetHostCache();
|
| + if (cache) {
|
| + cache->clear();
|
| + }
|
| +}
|
| +
|
| +void NetBenchmarkingImpl::CloseCurrentConnections(
|
| + const CloseCurrentConnectionsCallback& callback) {
|
| + request_context_->GetURLRequestContext()->
|
| + http_transaction_factory()->GetCache()->CloseAllConnections();
|
| +}
|
| +
|
| +void NetBenchmarkingImpl::ClearPredictorCache() {
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
| + base::Bind(&ClearPredictorCacheOnUIThread, profile_));
|
| +}
|
| +
|
|
|