Chromium Code Reviews| 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 // Net benchmarking binding is disabled unless the user has enabled | |
| 54 // benchmarking extensions. | |
| 55 if (!CheckBenchmarkingEnabled()) | |
| 56 return; | |
| 57 | |
| 58 mojo::MakeStrongBinding( | |
| 59 base::MakeUnique<NetBenchmarking>(profile, request_context), | |
| 60 std::move(request)); | |
| 61 } | |
| 62 | |
| 63 // static | |
| 64 bool NetBenchmarking::CheckBenchmarkingEnabled() { | |
| 65 static bool checked = false; | |
| 66 static bool result = false; | |
| 67 if (!checked) { | |
| 68 const base::CommandLine& command_line = | |
| 69 *base::CommandLine::ForCurrentProcess(); | |
| 70 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); | |
| 71 checked = true; | |
|
dcheng
2016/12/20 09:14:45
Given that we'll only need to call this once, I do
| |
| 72 } | |
| 73 return result; | |
| 74 } | |
| 75 | |
| 76 void NetBenchmarking::ClearCache(const ClearCacheCallback& callback) { | |
| 77 int rv = -1; | |
| 78 | |
| 79 disk_cache::Backend* backend = request_context_->GetURLRequestContext() | |
| 80 ->http_transaction_factory() | |
| 81 ->GetCache() | |
| 82 ->GetCurrentBackend(); | |
| 83 if (backend) { | |
| 84 rv = backend->DoomAllEntries(callback); | |
| 85 if (rv == net::ERR_IO_PENDING) { | |
| 86 // Callback is handled by backend. | |
| 87 return; | |
| 88 } | |
| 89 } | |
| 90 callback.Run(rv); | |
| 91 } | |
| 92 | |
| 93 void NetBenchmarking::ClearHostResolverCache( | |
| 94 const ClearHostResolverCacheCallback& callback) { | |
| 95 net::HostCache* cache = | |
| 96 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); | |
| 97 if (cache) { | |
| 98 cache->clear(); | |
| 99 } | |
|
dcheng
2016/12/20 09:14:45
Nit: omit { }
dvallet
2016/12/20 23:19:21
Done.
| |
| 100 callback.Run(); | |
| 101 } | |
| 102 | |
| 103 void NetBenchmarking::CloseCurrentConnections( | |
| 104 const CloseCurrentConnectionsCallback& callback) { | |
| 105 request_context_->GetURLRequestContext() | |
| 106 ->http_transaction_factory() | |
| 107 ->GetCache() | |
| 108 ->CloseAllConnections(); | |
| 109 callback.Run(); | |
| 110 } | |
| 111 | |
| 112 void NetBenchmarking::ClearPredictorCache( | |
| 113 const ClearPredictorCacheCallback& callback) { | |
| 114 BrowserThread::PostTaskAndReply( | |
| 115 BrowserThread::UI, FROM_HERE, | |
| 116 base::Bind(&ClearPredictorCacheOnUIThread, profile_), callback); | |
| 117 } | |
| OLD | NEW |