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.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/command_line.h" |
| 13 #include "base/macros.h" |
| 14 #include "chrome/browser/net/predictor.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/net_benchmarking.mojom.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 20 #include "net/base/net_errors.h" |
| 21 #include "net/disk_cache/disk_cache.h" |
| 22 #include "net/dns/host_cache.h" |
| 23 #include "net/dns/host_resolver.h" |
| 24 #include "net/http/http_cache.h" |
| 25 #include "net/url_request/url_request_context.h" |
| 26 #include "net/url_request/url_request_context_getter.h" |
| 27 |
| 28 using content::BrowserThread; |
| 29 |
| 30 namespace { |
| 31 |
| 32 void ClearPredictorCacheOnUIThread(Profile* profile) { |
| 33 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 34 // TODO(623967): Ensure that the profile or predictor are not accessed after |
| 35 // they have been shut down. |
| 36 chrome_browser_net::Predictor* predictor = profile->GetNetworkPredictor(); |
| 37 if (predictor) |
| 38 predictor->DiscardAllResultsAndClearPrefsOnUIThread(); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 NetBenchmarking::NetBenchmarking(Profile* profile, |
| 44 net::URLRequestContextGetter* request_context) |
| 45 : profile_(profile), request_context_(request_context) {} |
| 46 |
| 47 NetBenchmarking::~NetBenchmarking() {} |
| 48 |
| 49 // static |
| 50 void NetBenchmarking::Create(Profile* profile, |
| 51 net::URLRequestContextGetter* request_context, |
| 52 chrome::mojom::NetBenchmarkingRequest request) { |
| 53 mojo::MakeStrongBinding( |
| 54 base::MakeUnique<NetBenchmarking>(profile, request_context), |
| 55 std::move(request)); |
| 56 } |
| 57 |
| 58 // static |
| 59 bool NetBenchmarking::CheckBenchmarkingEnabled() { |
| 60 const base::CommandLine& command_line = |
| 61 *base::CommandLine::ForCurrentProcess(); |
| 62 return command_line.HasSwitch(switches::kEnableNetBenchmarking); |
| 63 } |
| 64 |
| 65 void NetBenchmarking::ClearCache(const ClearCacheCallback& callback) { |
| 66 int rv = -1; |
| 67 |
| 68 disk_cache::Backend* backend = request_context_->GetURLRequestContext() |
| 69 ->http_transaction_factory() |
| 70 ->GetCache() |
| 71 ->GetCurrentBackend(); |
| 72 if (backend) { |
| 73 rv = backend->DoomAllEntries(callback); |
| 74 if (rv == net::ERR_IO_PENDING) { |
| 75 // Callback is handled by backend. |
| 76 return; |
| 77 } |
| 78 } |
| 79 callback.Run(rv); |
| 80 } |
| 81 |
| 82 void NetBenchmarking::ClearHostResolverCache( |
| 83 const ClearHostResolverCacheCallback& callback) { |
| 84 net::HostCache* cache = |
| 85 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); |
| 86 if (cache) |
| 87 cache->clear(); |
| 88 callback.Run(); |
| 89 } |
| 90 |
| 91 void NetBenchmarking::CloseCurrentConnections( |
| 92 const CloseCurrentConnectionsCallback& callback) { |
| 93 request_context_->GetURLRequestContext() |
| 94 ->http_transaction_factory() |
| 95 ->GetCache() |
| 96 ->CloseAllConnections(); |
| 97 callback.Run(); |
| 98 } |
| 99 |
| 100 void NetBenchmarking::ClearPredictorCache( |
| 101 const ClearPredictorCacheCallback& callback) { |
| 102 BrowserThread::PostTaskAndReply( |
| 103 BrowserThread::UI, FROM_HERE, |
| 104 base::Bind(&ClearPredictorCacheOnUIThread, profile_), callback); |
| 105 } |
OLD | NEW |