Chromium Code Reviews| 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..82772ea6da7dc8bde12aaa996121cad76e5e79c3 | 
| --- /dev/null | 
| +++ b/chrome/browser/net_benchmarking_impl.cc | 
| @@ -0,0 +1,118 @@ | 
| +// 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()) { | 
| 
 
sky
2016/12/13 02:02:52
no {}
 
dvallet
2016/12/13 05:20:04
Done.
 
 | 
| + 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( | 
| + const ClearHostResolverCacheCallback& callback) { | 
| + net::HostCache* cache = | 
| + request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); | 
| + if (cache) { | 
| + cache->clear(); | 
| + } | 
| + callback.Run(); | 
| +} | 
| + | 
| +void NetBenchmarkingImpl::CloseCurrentConnections( | 
| + const CloseCurrentConnectionsCallback& callback) { | 
| + request_context_->GetURLRequestContext() | 
| + ->http_transaction_factory() | 
| + ->GetCache() | 
| + ->CloseAllConnections(); | 
| + callback.Run(); | 
| +} | 
| + | 
| +void NetBenchmarkingImpl::ClearPredictorCache( | 
| + const ClearPredictorCacheCallback& callback) { | 
| + BrowserThread::PostTaskAndReply( | 
| + BrowserThread::UI, FROM_HERE, | 
| + base::Bind(&ClearPredictorCacheOnUIThread, profile_), callback); | 
| +} |