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( | |
32 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 NetBenchmarkingImpl::NetBenchmarkingImpl( | |
44 Profile* profile, | |
45 net::URLRequestContextGetter* request_context) | |
46 : profile_(profile), request_context_(request_context) {} | |
47 | |
48 NetBenchmarkingImpl::~NetBenchmarkingImpl() {} | |
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(const ClearCacheCallback& callback) { | |
79 int rv = -1; | |
80 | |
81 disk_cache::Backend* backend = request_context_->GetURLRequestContext() | |
82 ->http_transaction_factory() | |
83 ->GetCache() | |
84 ->GetCurrentBackend(); | |
85 if (backend) { | |
86 rv = backend->DoomAllEntries(callback); | |
87 if (rv == net::ERR_IO_PENDING) { | |
88 // Callback is handled by backend. | |
89 return; | |
90 } | |
91 } | |
92 callback.Run(rv); | |
93 } | |
94 | |
95 void NetBenchmarkingImpl::ClearHostResolverCache( | |
96 const ClearHostResolverCacheCallback& callback) { | |
97 net::HostCache* cache = | |
98 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); | |
99 if (cache) { | |
100 cache->clear(); | |
101 } | |
102 callback.Run(); | |
103 } | |
104 | |
105 void NetBenchmarkingImpl::CloseCurrentConnections( | |
106 const CloseCurrentConnectionsCallback& callback) { | |
107 request_context_->GetURLRequestContext() | |
108 ->http_transaction_factory() | |
109 ->GetCache() | |
110 ->CloseAllConnections(); | |
111 callback.Run(); | |
112 } | |
113 | |
114 void NetBenchmarkingImpl::ClearPredictorCache( | |
115 const ClearPredictorCacheCallback& callback) { | |
116 BrowserThread::PostTaskAndReply(BrowserThread::UI, FROM_HERE, | |
117 base::Bind(&ClearPredictorCacheOnUIThread, profile_), | |
Sam McNally
2016/12/12 22:23:52
Reformat.
dvallet
2016/12/12 22:47:26
Done
| |
118 callback); | |
119 } | |
OLD | NEW |