OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chrome_net_benchmarking_message_filter.h" | 5 #include "chrome/browser/chrome_net_benchmarking_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "chrome/browser/net/predictor.h" | 13 #include "chrome/browser/net/predictor.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/benchmarking_messages.h" | |
16 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
16 #include "chrome/common/chrome_net_benchmarking.mojom.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 #include "mojo/public/cpp/bindings/strong_binding.h" | |
17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
18 #include "net/disk_cache/disk_cache.h" | 20 #include "net/disk_cache/disk_cache.h" |
19 #include "net/dns/host_cache.h" | 21 #include "net/dns/host_cache.h" |
20 #include "net/dns/host_resolver.h" | 22 #include "net/dns/host_resolver.h" |
21 #include "net/http/http_cache.h" | 23 #include "net/http/http_cache.h" |
22 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
23 #include "net/url_request/url_request_context_getter.h" | 25 #include "net/url_request/url_request_context_getter.h" |
24 | 26 |
27 using content::BrowserThread; | |
28 | |
25 namespace { | 29 namespace { |
26 | 30 |
27 void ClearCacheCallback(ChromeNetBenchmarkingMessageFilter* filter, | 31 void ClearPredictorCacheOnUIThread(Profile* profile) { |
28 IPC::Message* reply_msg, | 32 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
29 int result) { | 33 // TODO(623967): Ensure that the profile or predictor are not accessed after |
30 ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, result); | 34 // they have been shut down. |
31 filter->Send(reply_msg); | 35 chrome_browser_net::Predictor* predictor = profile->GetNetworkPredictor(); |
36 if (predictor) | |
37 predictor->DiscardAllResultsAndClearPrefsOnUIThread(); | |
32 } | 38 } |
33 | 39 |
34 } // namespace | 40 } // namespace |
35 | 41 |
36 ChromeNetBenchmarkingMessageFilter::ChromeNetBenchmarkingMessageFilter( | 42 ChromeNetBenchmarkingImpl::ChromeNetBenchmarkingImpl( |
37 Profile* profile, | 43 Profile* profile, |
38 net::URLRequestContextGetter* request_context) | 44 net::URLRequestContextGetter* request_context) |
39 : BrowserMessageFilter(ChromeBenchmarkingMsgStart), | 45 : profile_(profile), request_context_(request_context) {} |
40 profile_(profile), | 46 |
41 request_context_(request_context) { | 47 ChromeNetBenchmarkingImpl::~ChromeNetBenchmarkingImpl() { |
42 } | 48 } |
43 | 49 |
44 ChromeNetBenchmarkingMessageFilter::~ChromeNetBenchmarkingMessageFilter() { | 50 // static |
51 void ChromeNetBenchmarkingImpl::Create( | |
52 Profile* profile, | |
53 net::URLRequestContextGetter* request_context, | |
54 chrome::mojom::ChromeNetBenchmarkingRequest request) { | |
55 mojo::MakeStrongBinding( | |
56 base::MakeUnique<ChromeNetBenchmarkingImpl>(profile, request_context), | |
57 std::move(request)); | |
45 } | 58 } |
46 | 59 |
47 bool ChromeNetBenchmarkingMessageFilter::OnMessageReceived( | 60 void ChromeNetBenchmarkingImpl::ClearCache( |
48 const IPC::Message& message) { | 61 const ClearCacheCallback& callback) { |
49 bool handled = true; | |
50 IPC_BEGIN_MESSAGE_MAP(ChromeNetBenchmarkingMessageFilter, message) | |
51 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_CloseCurrentConnections, | |
52 OnCloseCurrentConnections) | |
53 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_ClearCache, OnClearCache) | |
54 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ClearHostResolverCache, | |
55 OnClearHostResolverCache) | |
56 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ClearPredictorCache, | |
57 OnClearPredictorCache) | |
58 IPC_MESSAGE_UNHANDLED(handled = false) | |
59 IPC_END_MESSAGE_MAP() | |
60 return handled; | |
61 } | |
62 | |
63 void ChromeNetBenchmarkingMessageFilter::OverrideThreadForMessage( | |
64 const IPC::Message& message, | |
65 content::BrowserThread::ID* thread) { | |
66 if (message.type() == ChromeViewHostMsg_ClearPredictorCache::ID) | |
67 *thread = content::BrowserThread::UI; | |
68 } | |
69 | |
70 void ChromeNetBenchmarkingMessageFilter::OnClearCache(IPC::Message* reply_msg) { | |
71 // This function is disabled unless the user has enabled | 62 // This function is disabled unless the user has enabled |
72 // benchmarking extensions. | 63 // benchmarking extensions. |
73 if (!CheckBenchmarkingEnabled()) { | 64 if (!CheckBenchmarkingEnabled()) { |
Sam McNally
2016/12/05 06:57:09
Check this in Create() instead and if not enabled,
dvallet
2016/12/07 02:21:02
Done. I've remove te NOTREACHED message since I gu
| |
74 NOTREACHED() << "Received unexpected benchmarking IPC"; | 65 NOTREACHED() << "Received unexpected benchmarking IPC"; |
66 // No callback called. | |
75 return; | 67 return; |
76 } | 68 } |
77 int rv = -1; | 69 int rv = -1; |
78 | 70 |
79 disk_cache::Backend* backend = request_context_->GetURLRequestContext()-> | 71 disk_cache::Backend* backend = request_context_->GetURLRequestContext()-> |
80 http_transaction_factory()->GetCache()->GetCurrentBackend(); | 72 http_transaction_factory()->GetCache()->GetCurrentBackend(); |
81 if (backend) { | 73 if (backend) { |
82 net::CompletionCallback callback = | |
83 base::Bind(&ClearCacheCallback, base::RetainedRef(this), reply_msg); | |
84 rv = backend->DoomAllEntries(callback); | 74 rv = backend->DoomAllEntries(callback); |
85 if (rv == net::ERR_IO_PENDING) { | 75 if (rv == net::ERR_IO_PENDING) { |
86 // The callback will send the reply. | 76 // Callback is handled by backend. |
87 return; | 77 return; |
88 } | 78 } |
89 } | 79 } |
90 ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, rv); | 80 callback.Run(rv); |
91 Send(reply_msg); | |
92 } | 81 } |
93 | 82 |
94 void ChromeNetBenchmarkingMessageFilter::OnClearHostResolverCache() { | 83 void ChromeNetBenchmarkingImpl::ClearHostResolverCache() { |
95 // This function is disabled unless the user has enabled | 84 // This function is disabled unless the user has enabled |
96 // benchmarking extensions. | 85 // benchmarking extensions. |
97 if (!CheckBenchmarkingEnabled()) { | 86 if (!CheckBenchmarkingEnabled()) { |
98 NOTREACHED() << "Received unexpected benchmarking IPC"; | 87 NOTREACHED() << "Received unexpected benchmarking IPC"; |
99 return; | 88 return; |
100 } | 89 } |
101 net::HostCache* cache = | 90 net::HostCache* cache = |
102 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); | 91 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); |
103 if (cache) { | 92 if (cache) { |
104 cache->clear(); | 93 cache->clear(); |
105 } | 94 } |
106 } | 95 } |
107 | 96 |
108 void ChromeNetBenchmarkingMessageFilter::OnCloseCurrentConnections() { | 97 void ChromeNetBenchmarkingImpl::CloseCurrentConnections() { |
109 // This function is disabled unless the user has enabled | 98 // This function is disabled unless the user has enabled |
110 // benchmarking extensions. | 99 // benchmarking extensions. |
111 if (!CheckBenchmarkingEnabled()) { | 100 if (!CheckBenchmarkingEnabled()) { |
112 NOTREACHED() << "Received unexpected benchmarking IPC"; | 101 NOTREACHED() << "Received unexpected benchmarking IPC"; |
113 return; | 102 return; |
114 } | 103 } |
115 request_context_->GetURLRequestContext()-> | 104 request_context_->GetURLRequestContext()-> |
116 http_transaction_factory()->GetCache()->CloseAllConnections(); | 105 http_transaction_factory()->GetCache()->CloseAllConnections(); |
117 } | 106 } |
118 | 107 |
119 void ChromeNetBenchmarkingMessageFilter::OnSetCacheMode(bool enabled) { | 108 void ChromeNetBenchmarkingImpl::SetCacheMode(bool enabled) { |
120 // This function is disabled unless the user has enabled | 109 // This function is disabled unless the user has enabled |
121 // benchmarking extensions. | 110 // benchmarking extensions. |
122 if (!CheckBenchmarkingEnabled()) { | 111 if (!CheckBenchmarkingEnabled()) { |
123 NOTREACHED() << "Received unexpected benchmarking IPC"; | 112 NOTREACHED() << "Received unexpected benchmarking IPC"; |
124 return; | 113 return; |
125 } | 114 } |
126 net::HttpCache::Mode mode = enabled ? | 115 net::HttpCache::Mode mode = enabled ? |
127 net::HttpCache::NORMAL : net::HttpCache::DISABLE; | 116 net::HttpCache::NORMAL : net::HttpCache::DISABLE; |
128 net::HttpCache* http_cache = request_context_->GetURLRequestContext()-> | 117 net::HttpCache* http_cache = request_context_->GetURLRequestContext()-> |
129 http_transaction_factory()->GetCache(); | 118 http_transaction_factory()->GetCache(); |
130 http_cache->set_mode(mode); | 119 http_cache->set_mode(mode); |
131 } | 120 } |
132 | 121 |
133 void ChromeNetBenchmarkingMessageFilter::OnClearPredictorCache() { | 122 void ChromeNetBenchmarkingImpl::ClearPredictorCache() { |
134 // This function is disabled unless the user has enabled | 123 // This function is disabled unless the user has enabled |
135 // benchmarking extensions. | 124 // benchmarking extensions. |
136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
137 if (!CheckBenchmarkingEnabled()) { | 125 if (!CheckBenchmarkingEnabled()) { |
138 NOTREACHED() << "Received unexpected benchmarking IPC"; | 126 NOTREACHED() << "Received unexpected benchmarking IPC"; |
139 return; | 127 return; |
140 } | 128 } |
141 // TODO(623967): Ensure that the profile or predictor are not accessed after | 129 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
142 // they have been shut down. | 130 base::Bind(&ClearPredictorCacheOnUIThread, profile_)); |
143 chrome_browser_net::Predictor* predictor = profile_->GetNetworkPredictor(); | |
144 if (predictor) | |
145 predictor->DiscardAllResultsAndClearPrefsOnUIThread(); | |
146 } | 131 } |
147 | 132 |
148 bool ChromeNetBenchmarkingMessageFilter::CheckBenchmarkingEnabled() const { | 133 bool ChromeNetBenchmarkingImpl::CheckBenchmarkingEnabled() const { |
149 static bool checked = false; | 134 static bool checked = false; |
150 static bool result = false; | 135 static bool result = false; |
151 if (!checked) { | 136 if (!checked) { |
152 const base::CommandLine& command_line = | 137 const base::CommandLine& command_line = |
153 *base::CommandLine::ForCurrentProcess(); | 138 *base::CommandLine::ForCurrentProcess(); |
154 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); | 139 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); |
155 checked = true; | 140 checked = true; |
156 } | 141 } |
157 return result; | 142 return result; |
158 } | 143 } |
159 | 144 |
OLD | NEW |