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 // static | |
50 void NetBenchmarkingImpl::Create( | |
51 Profile* profile, | |
52 net::URLRequestContextGetter* request_context, | |
53 chrome::mojom::NetBenchmarkingRequest request) { | |
54 // Net benchmarking binding is disabled unless the user has enabled | |
55 // benchmarking extensions. | |
56 if (!CheckBenchmarkingEnabled()) { | |
sky
2016/12/13 02:02:52
no {}
dvallet
2016/12/13 05:20:04
Done.
| |
57 return; | |
58 } | |
59 mojo::MakeStrongBinding( | |
60 base::MakeUnique<NetBenchmarkingImpl>(profile, request_context), | |
61 std::move(request)); | |
62 } | |
63 | |
64 // static | |
65 bool NetBenchmarkingImpl::CheckBenchmarkingEnabled() { | |
66 static bool checked = false; | |
67 static bool result = false; | |
68 if (!checked) { | |
69 const base::CommandLine& command_line = | |
70 *base::CommandLine::ForCurrentProcess(); | |
71 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); | |
72 checked = true; | |
73 } | |
74 return result; | |
75 } | |
76 | |
77 void NetBenchmarkingImpl::ClearCache(const ClearCacheCallback& callback) { | |
78 int rv = -1; | |
79 | |
80 disk_cache::Backend* backend = request_context_->GetURLRequestContext() | |
81 ->http_transaction_factory() | |
82 ->GetCache() | |
83 ->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 const ClearHostResolverCacheCallback& callback) { | |
96 net::HostCache* cache = | |
97 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); | |
98 if (cache) { | |
99 cache->clear(); | |
100 } | |
101 callback.Run(); | |
102 } | |
103 | |
104 void NetBenchmarkingImpl::CloseCurrentConnections( | |
105 const CloseCurrentConnectionsCallback& callback) { | |
106 request_context_->GetURLRequestContext() | |
107 ->http_transaction_factory() | |
108 ->GetCache() | |
109 ->CloseAllConnections(); | |
110 callback.Run(); | |
111 } | |
112 | |
113 void NetBenchmarkingImpl::ClearPredictorCache( | |
114 const ClearPredictorCacheCallback& callback) { | |
115 BrowserThread::PostTaskAndReply( | |
116 BrowserThread::UI, FROM_HERE, | |
117 base::Bind(&ClearPredictorCacheOnUIThread, profile_), callback); | |
118 } | |
OLD | NEW |