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 "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/command_line.h" 10 #include "base/macros.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "chrome/browser/net/chrome_url_request_context.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 23 namespace { 24 25 void ClearCacheCallback(ChromeNetBenchmarkingMessageFilter* filter, 26 IPC::Message* reply_msg, 27 int result) { 28 ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, result); 29 filter->Send(reply_msg); 30 } 31 32 } // namespace 33 34 ChromeNetBenchmarkingMessageFilter::ChromeNetBenchmarkingMessageFilter( 35 Profile* profile, 36 net::URLRequestContextGetter* request_context) 37 : BrowserMessageFilter(ChromeBenchmarkingMsgStart), 38 profile_(profile), 39 request_context_(request_context) { 40 } 41 42 ChromeNetBenchmarkingMessageFilter::~ChromeNetBenchmarkingMessageFilter() { 43 } 44 45 bool ChromeNetBenchmarkingMessageFilter::OnMessageReceived( 46 const IPC::Message& message) { 47 bool handled = true; 48 IPC_BEGIN_MESSAGE_MAP(ChromeNetBenchmarkingMessageFilter, message) 49 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_CloseCurrentConnections, 50 OnCloseCurrentConnections) 51 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_ClearCache, OnClearCache) 52 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ClearHostResolverCache, 53 OnClearHostResolverCache) 54 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ClearPredictorCache, 55 OnClearPredictorCache) 56 IPC_MESSAGE_UNHANDLED(handled = false) 57 IPC_END_MESSAGE_MAP() 58 return handled; 59 } 60 61 void ChromeNetBenchmarkingMessageFilter::OnClearCache(IPC::Message* reply_msg) { 62 // This function is disabled unless the user has enabled 63 // benchmarking extensions. 64 if (!CheckBenchmarkingEnabled()) { 65 NOTREACHED() << "Received unexpected benchmarking IPC"; 66 return; 67 } 68 int rv = -1; 69 70 disk_cache::Backend* backend = request_context_->GetURLRequestContext()-> 71 http_transaction_factory()->GetCache()->GetCurrentBackend(); 72 if (backend) { 73 net::CompletionCallback callback = 74 base::Bind(&ClearCacheCallback, make_scoped_refptr(this), reply_msg); 75 rv = backend->DoomAllEntries(callback); 76 if (rv == net::ERR_IO_PENDING) { 77 // The callback will send the reply. 78 return; 79 } 80 } 81 ChromeViewHostMsg_ClearCache::WriteReplyParams(reply_msg, rv); 82 Send(reply_msg); 83 } 84 85 void ChromeNetBenchmarkingMessageFilter::OnClearHostResolverCache(int* result) { 86 // This function is disabled unless the user has enabled 87 // benchmarking extensions. 88 if (!CheckBenchmarkingEnabled()) { 89 NOTREACHED() << "Received unexpected benchmarking IPC"; 90 return; 91 } 92 *result = -1; 93 net::HostCache* cache = 94 request_context_->GetURLRequestContext()->host_resolver()->GetHostCache(); 95 if (cache) { 96 cache->clear(); 97 *result = 0; 98 } 99 } 100 101 void ChromeNetBenchmarkingMessageFilter::OnCloseCurrentConnections() { 102 // This function is disabled unless the user has enabled 103 // benchmarking extensions. 104 if (!CheckBenchmarkingEnabled()) { 105 NOTREACHED() << "Received unexpected benchmarking IPC"; 106 return; 107 } 108 request_context_->GetURLRequestContext()-> 109 http_transaction_factory()->GetCache()->CloseAllConnections(); 110 } 111 112 void ChromeNetBenchmarkingMessageFilter::OnSetCacheMode(bool enabled) { 113 // This function is disabled unless the user has enabled 114 // benchmarking extensions. 115 if (!CheckBenchmarkingEnabled()) { 116 NOTREACHED() << "Received unexpected benchmarking IPC"; 117 return; 118 } 119 net::HttpCache::Mode mode = enabled ? 120 net::HttpCache::NORMAL : net::HttpCache::DISABLE; 121 net::HttpCache* http_cache = request_context_->GetURLRequestContext()-> 122 http_transaction_factory()->GetCache(); 123 http_cache->set_mode(mode); 124 } 125 126 void ChromeNetBenchmarkingMessageFilter::OnClearPredictorCache(int* result) { 127 // This function is disabled unless the user has enabled 128 // benchmarking extensions. 129 if (!CheckBenchmarkingEnabled()) { 130 NOTREACHED() << "Received unexpected benchmarking IPC"; 131 return; 132 } 133 chrome_browser_net::Predictor* predictor = profile_->GetNetworkPredictor(); 134 if (predictor) 135 predictor->DiscardAllResults(); 136 *result = 0; 137 } 138 139 bool ChromeNetBenchmarkingMessageFilter::CheckBenchmarkingEnabled() const { 140 static bool checked = false; 141 static bool result = false; 142 if (!checked) { 143 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 144 result = command_line.HasSwitch(switches::kEnableNetBenchmarking); 145 checked = true; 146 } 147 return result; 148 } 149 150