OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net_benchmarking_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/macros.h" |
| 13 #include "chrome/browser/net/predictor.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/net_benchmarking.mojom.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 19 #include "net/base/net_errors.h" |
| 20 #include "net/disk_cache/disk_cache.h" |
| 21 #include "net/dns/host_cache.h" |
| 22 #include "net/dns/host_resolver.h" |
| 23 #include "net/http/http_cache.h" |
| 24 #include "net/url_request/url_request_context.h" |
| 25 #include "net/url_request/url_request_context_getter.h" |
| 26 |
| 27 using content::BrowserThread; |
| 28 |
| 29 namespace { |
| 30 |
| 31 void ClearPredictorCacheOnUIThread(Profile* profile) { |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 33 // TODO(623967): Ensure that the profile or predictor are not accessed after |
| 34 // they have been shut down. |
| 35 chrome_browser_net::Predictor* predictor = profile->GetNetworkPredictor(); |
| 36 if (predictor) |
| 37 predictor->DiscardAllResultsAndClearPrefsOnUIThread(); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 NetBenchmarkingImpl::NetBenchmarkingImpl( |
| 43 Profile* profile, |
| 44 net::URLRequestContextGetter* request_context) |
| 45 : profile_(profile), request_context_(request_context) {} |
| 46 |
| 47 NetBenchmarkingImpl::~NetBenchmarkingImpl() { |
| 48 } |
| 49 |
| 50 // static |
| 51 void NetBenchmarkingImpl::Create( |
| 52 Profile* profile, |
| 53 net::URLRequestContextGetter* request_context, |
| 54 chrome::mojom::NetBenchmarkingRequest request) { |
| 55 // Net benchmarking binding is disabled unless the user has enabled |
| 56 // benchmarking extensions. |
| 57 if (!CheckBenchmarkingEnabled()) { |
| 58 return; |
| 59 } |
| 60 mojo::MakeStrongBinding( |
| 61 base::MakeUnique<NetBenchmarkingImpl>(profile, request_context), |
| 62 std::move(request)); |
| 63 } |
| 64 |
| 65 // static |
| 66 bool NetBenchmarkingImpl::CheckBenchmarkingEnabled() { |
| 67 static bool checked = false; |
| 68 static bool result = false; |
| 69 if (!checked) { |
| 70 const base::CommandLine& command_line = |
| 71 *base::CommandLine::ForCurrentProcess(); |
| 72 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); |
| 73 checked = true; |
| 74 } |
| 75 return result; |
| 76 } |
| 77 |
| 78 void NetBenchmarkingImpl::ClearCache( |
| 79 const ClearCacheCallback& callback) { |
| 80 int rv = -1; |
| 81 |
| 82 disk_cache::Backend* backend = request_context_->GetURLRequestContext()-> |
| 83 http_transaction_factory()->GetCache()->GetCurrentBackend(); |
| 84 if (backend) { |
| 85 rv = backend->DoomAllEntries(callback); |
| 86 if (rv == net::ERR_IO_PENDING) { |
| 87 // Callback is handled by backend. |
| 88 return; |
| 89 } |
| 90 } |
| 91 callback.Run(rv); |
| 92 } |
| 93 |
| 94 void NetBenchmarkingImpl::ClearHostResolverCache() { |
| 95 net::HostCache* cache = |
| 96 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); |
| 97 if (cache) { |
| 98 cache->clear(); |
| 99 } |
| 100 } |
| 101 |
| 102 void NetBenchmarkingImpl::CloseCurrentConnections( |
| 103 const CloseCurrentConnectionsCallback& callback) { |
| 104 request_context_->GetURLRequestContext()-> |
| 105 http_transaction_factory()->GetCache()->CloseAllConnections(); |
| 106 } |
| 107 |
| 108 void NetBenchmarkingImpl::ClearPredictorCache() { |
| 109 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 110 base::Bind(&ClearPredictorCacheOnUIThread, profile_)); |
| 111 } |
| 112 |
OLD | NEW |