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/chrome_net_benchmarking_message_filter.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/benchmarking_messages.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "net/base/net_errors.h" | |
18 #include "net/disk_cache/disk_cache.h" | |
19 #include "net/dns/host_cache.h" | |
20 #include "net/dns/host_resolver.h" | |
21 #include "net/http/http_cache.h" | |
22 #include "net/url_request/url_request_context.h" | |
23 #include "net/url_request/url_request_context_getter.h" | |
24 | |
25 namespace { | |
26 | |
27 void ClearCacheCallback(ChromeNetBenchmarkingMessageFilter* filter, | |
28 IPC::Message* reply_msg, | |
29 int result) { | |
30 ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, result); | |
31 filter->Send(reply_msg); | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 ChromeNetBenchmarkingMessageFilter::ChromeNetBenchmarkingMessageFilter( | |
37 Profile* profile, | |
38 net::URLRequestContextGetter* request_context) | |
39 : BrowserMessageFilter(ChromeBenchmarkingMsgStart), | |
40 profile_(profile), | |
41 request_context_(request_context) { | |
42 } | |
43 | |
44 ChromeNetBenchmarkingMessageFilter::~ChromeNetBenchmarkingMessageFilter() { | |
45 } | |
46 | |
47 bool ChromeNetBenchmarkingMessageFilter::OnMessageReceived( | |
48 const IPC::Message& message) { | |
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 | |
72 // benchmarking extensions. | |
73 if (!CheckBenchmarkingEnabled()) { | |
74 NOTREACHED() << "Received unexpected benchmarking IPC"; | |
75 return; | |
76 } | |
77 int rv = -1; | |
78 | |
79 disk_cache::Backend* backend = request_context_->GetURLRequestContext()-> | |
80 http_transaction_factory()->GetCache()->GetCurrentBackend(); | |
81 if (backend) { | |
82 net::CompletionCallback callback = | |
83 base::Bind(&ClearCacheCallback, base::RetainedRef(this), reply_msg); | |
84 rv = backend->DoomAllEntries(callback); | |
85 if (rv == net::ERR_IO_PENDING) { | |
86 // The callback will send the reply. | |
87 return; | |
88 } | |
89 } | |
90 ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, rv); | |
91 Send(reply_msg); | |
92 } | |
93 | |
94 void ChromeNetBenchmarkingMessageFilter::OnClearHostResolverCache() { | |
95 // This function is disabled unless the user has enabled | |
96 // benchmarking extensions. | |
97 if (!CheckBenchmarkingEnabled()) { | |
98 NOTREACHED() << "Received unexpected benchmarking IPC"; | |
99 return; | |
100 } | |
101 net::HostCache* cache = | |
102 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); | |
103 if (cache) { | |
104 cache->clear(); | |
105 } | |
106 } | |
107 | |
108 void ChromeNetBenchmarkingMessageFilter::OnCloseCurrentConnections() { | |
109 // This function is disabled unless the user has enabled | |
110 // benchmarking extensions. | |
111 if (!CheckBenchmarkingEnabled()) { | |
112 NOTREACHED() << "Received unexpected benchmarking IPC"; | |
113 return; | |
114 } | |
115 request_context_->GetURLRequestContext()-> | |
116 http_transaction_factory()->GetCache()->CloseAllConnections(); | |
117 } | |
118 | |
119 void ChromeNetBenchmarkingMessageFilter::OnSetCacheMode(bool enabled) { | |
120 // This function is disabled unless the user has enabled | |
121 // benchmarking extensions. | |
122 if (!CheckBenchmarkingEnabled()) { | |
123 NOTREACHED() << "Received unexpected benchmarking IPC"; | |
124 return; | |
125 } | |
126 net::HttpCache::Mode mode = enabled ? | |
127 net::HttpCache::NORMAL : net::HttpCache::DISABLE; | |
128 net::HttpCache* http_cache = request_context_->GetURLRequestContext()-> | |
129 http_transaction_factory()->GetCache(); | |
130 http_cache->set_mode(mode); | |
131 } | |
132 | |
133 void ChromeNetBenchmarkingMessageFilter::OnClearPredictorCache() { | |
134 // This function is disabled unless the user has enabled | |
135 // benchmarking extensions. | |
136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
137 if (!CheckBenchmarkingEnabled()) { | |
138 NOTREACHED() << "Received unexpected benchmarking IPC"; | |
139 return; | |
140 } | |
141 // TODO(623967): Ensure that the profile or predictor are not accessed after | |
142 // they have been shut down. | |
143 chrome_browser_net::Predictor* predictor = profile_->GetNetworkPredictor(); | |
144 if (predictor) | |
145 predictor->DiscardAllResultsAndClearPrefsOnUIThread(); | |
146 } | |
147 | |
148 bool ChromeNetBenchmarkingMessageFilter::CheckBenchmarkingEnabled() const { | |
149 static bool checked = false; | |
150 static bool result = false; | |
151 if (!checked) { | |
152 const base::CommandLine& command_line = | |
153 *base::CommandLine::ForCurrentProcess(); | |
154 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); | |
155 checked = true; | |
156 } | |
157 return result; | |
158 } | |
159 | |
OLD | NEW |