[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 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/chrome_network_delegate.h" |
| 6 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 9 | #include "base/base_paths.h" |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 10 | #include "base/logging.h" |
| 11 | #include "base/metrics/histogram.h" |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 12 | #include "base/path_service.h" |
[email protected] | 0abb6712 | 2012-12-12 11:36:05 | [diff] [blame] | 13 | #include "base/prefs/public/pref_member.h" |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 14 | #include "base/string_split.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame^] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 17 | #include "chrome/browser/content_settings/cookie_settings.h" |
| 18 | #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 19 | #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 20 | #include "chrome/browser/extensions/api/proxy/proxy_api.h" |
[email protected] | 2b33dcd0 | 2012-03-18 01:34:16 | [diff] [blame] | 21 | #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 22 | #include "chrome/browser/extensions/event_router_forwarder.h" |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_info_map.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 25 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 26 | #include "chrome/browser/google/google_util.h" |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 27 | #include "chrome/browser/net/connect_interceptor.h" |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 28 | #include "chrome/browser/net/load_time_stats.h" |
[email protected] | 6f4b4b4 | 2012-08-27 21:59:07 | [diff] [blame] | 29 | #include "chrome/browser/performance_monitor/performance_monitor.h" |
[email protected] | fb8fdf1 | 2012-08-21 16:28:20 | [diff] [blame] | 30 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 31 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 32 | #include "chrome/browser/task_manager/task_manager.h" |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 33 | #include "chrome/common/pref_names.h" |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 34 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 35 | #include "content/public/browser/browser_thread.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 36 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 37 | #include "content/public/browser/resource_request_info.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 38 | #include "extensions/common/constants.h" |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 39 | #include "net/base/host_port_pair.h" |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 40 | #include "net/base/net_errors.h" |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 41 | #include "net/base/net_log.h" |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 42 | #include "net/cookies/canonical_cookie.h" |
| 43 | #include "net/cookies/cookie_options.h" |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 44 | #include "net/http/http_request_headers.h" |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 45 | #include "net/http/http_response_headers.h" |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 46 | #include "net/socket_stream/socket_stream.h" |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 47 | #include "net/url_request/url_request.h" |
| 48 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 49 | #if defined(OS_CHROMEOS) |
| 50 | #include "base/chromeos/chromeos_version.h" |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 51 | #include "base/command_line.h" |
| 52 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 53 | #endif |
| 54 | |
[email protected] | 3e598ff1 | 2011-09-06 11:22:34 | [diff] [blame] | 55 | #if defined(ENABLE_CONFIGURATION_POLICY) |
| 56 | #include "chrome/browser/policy/url_blacklist_manager.h" |
| 57 | #endif |
| 58 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 59 | using content::BrowserThread; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 60 | using content::RenderViewHost; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 61 | using content::ResourceRequestInfo; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 62 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 63 | // By default we don't allow access to all file:// urls on ChromeOS and |
| 64 | // Android. |
| 65 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 66 | bool ChromeNetworkDelegate::g_allow_file_access_ = false; |
| 67 | #else |
| 68 | bool ChromeNetworkDelegate::g_allow_file_access_ = true; |
| 69 | #endif |
| 70 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 71 | // This remains false unless the --disable-extensions-http-throttling |
| 72 | // flag is passed to the browser. |
| 73 | bool ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
| 74 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 75 | namespace { |
| 76 | |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 77 | const char kDNTHeader[] = "DNT"; |
| 78 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 79 | // If the |request| failed due to problems with a proxy, forward the error to |
| 80 | // the proxy extension API. |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 81 | void ForwardProxyErrors(net::URLRequest* request, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 82 | extensions::EventRouterForwarder* event_router, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 83 | void* profile) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 84 | if (request->status().status() == net::URLRequestStatus::FAILED) { |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 85 | switch (request->status().error()) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 86 | case net::ERR_PROXY_AUTH_UNSUPPORTED: |
| 87 | case net::ERR_PROXY_CONNECTION_FAILED: |
| 88 | case net::ERR_TUNNEL_CONNECTION_FAILED: |
[email protected] | c454fe67 | 2012-03-12 21:18:01 | [diff] [blame] | 89 | extensions::ProxyEventRouter::GetInstance()->OnProxyError( |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 90 | event_router, profile, request->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 95 | // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the |
| 96 | // same key as the the |second_parameter| (e.g. foo=baz). Both parameters |
| 97 | // must be in key=value form. |
| 98 | bool HasSameParameterKey(const std::string& first_parameter, |
| 99 | const std::string& second_parameter) { |
| 100 | DCHECK(second_parameter.find("=") != std::string::npos); |
| 101 | // Prefix for "foo=bar" is "foo=". |
| 102 | std::string parameter_prefix = second_parameter.substr( |
| 103 | 0, second_parameter.find("=") + 1); |
| 104 | return StartsWithASCII(first_parameter, parameter_prefix, false); |
| 105 | } |
| 106 | |
| 107 | // Examines the query string containing parameters and adds the necessary ones |
| 108 | // so that SafeSearch is active. |query| is the string to examine and the |
| 109 | // return value is the |query| string modified such that SafeSearch is active. |
| 110 | std::string AddSafeSearchParameters(const std::string& query) { |
| 111 | std::vector<std::string> new_parameters; |
| 112 | std::string safe_parameter = chrome::kSafeSearchSafeParameter; |
| 113 | std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; |
| 114 | |
| 115 | std::vector<std::string> parameters; |
| 116 | base::SplitString(query, '&', ¶meters); |
| 117 | |
| 118 | std::vector<std::string>::iterator it; |
| 119 | for (it = parameters.begin(); it < parameters.end(); ++it) { |
| 120 | if (!HasSameParameterKey(*it, safe_parameter) && |
| 121 | !HasSameParameterKey(*it, ssui_parameter)) { |
| 122 | new_parameters.push_back(*it); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | new_parameters.push_back(safe_parameter); |
| 127 | new_parameters.push_back(ssui_parameter); |
| 128 | return JoinString(new_parameters, '&'); |
| 129 | } |
| 130 | |
| 131 | // If |request| is a request to Google Web Search the function |
| 132 | // enforces that the SafeSearch query parameters are set to active. |
| 133 | // Sets the query part of |new_url| with the new value of the parameters. |
| 134 | void ForceGoogleSafeSearch(net::URLRequest* request, |
| 135 | GURL* new_url) { |
| 136 | if (!google_util::IsGoogleSearchUrl(request->url().spec()) && |
| 137 | !google_util::IsGoogleHomePageUrl(request->url().spec())) |
| 138 | return; |
| 139 | |
| 140 | std::string query = request->url().query(); |
| 141 | std::string new_query = AddSafeSearchParameters(query); |
| 142 | if (query == new_query) |
| 143 | return; |
| 144 | |
| 145 | GURL::Replacements replacements; |
| 146 | replacements.SetQueryStr(new_query); |
| 147 | *new_url = request->url().ReplaceComponents(replacements); |
| 148 | } |
| 149 | |
| 150 | // Gets called when the extensions finish work on the URL. If the extensions |
| 151 | // did not do a redirect (so |new_url| is empty) then we enforce the |
| 152 | // SafeSearch parameters. Otherwise we will get called again after the |
| 153 | // redirect and we enforce SafeSearch then. |
| 154 | void ForceGoogleSafeSearchCallbackWrapper( |
| 155 | const net::CompletionCallback& callback, |
| 156 | net::URLRequest* request, |
| 157 | GURL* new_url, |
| 158 | int rv) { |
| 159 | if (rv == net::OK && new_url->is_empty()) |
| 160 | ForceGoogleSafeSearch(request, new_url); |
| 161 | callback.Run(rv); |
| 162 | } |
| 163 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 164 | enum RequestStatus { REQUEST_STARTED, REQUEST_DONE }; |
| 165 | |
| 166 | // Notifies the ExtensionProcessManager that a request has started or stopped |
| 167 | // for a particular RenderView. |
| 168 | void NotifyEPMRequestStatus(RequestStatus status, |
| 169 | void* profile_id, |
| 170 | int process_id, |
| 171 | int render_view_id) { |
| 172 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 173 | Profile* profile = reinterpret_cast<Profile*>(profile_id); |
| 174 | if (!g_browser_process->profile_manager()->IsValidProfile(profile)) |
| 175 | return; |
| 176 | |
[email protected] | e5775a5 | 2012-03-17 04:59:57 | [diff] [blame] | 177 | ExtensionProcessManager* extension_process_manager = |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 178 | extensions::ExtensionSystem::Get(profile)->process_manager(); |
[email protected] | e5775a5 | 2012-03-17 04:59:57 | [diff] [blame] | 179 | // This may be NULL in unit tests. |
| 180 | if (!extension_process_manager) |
| 181 | return; |
| 182 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 183 | // Will be NULL if the request was not issued on behalf of a renderer (e.g. a |
| 184 | // system-level request). |
[email protected] | d3e898e | 2012-03-14 03:45:08 | [diff] [blame] | 185 | RenderViewHost* render_view_host = |
| 186 | RenderViewHost::FromID(process_id, render_view_id); |
[email protected] | e5775a5 | 2012-03-17 04:59:57 | [diff] [blame] | 187 | if (render_view_host) { |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 188 | if (status == REQUEST_STARTED) { |
[email protected] | d3e898e | 2012-03-14 03:45:08 | [diff] [blame] | 189 | extension_process_manager->OnNetworkRequestStarted(render_view_host); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 190 | } else if (status == REQUEST_DONE) { |
[email protected] | d3e898e | 2012-03-14 03:45:08 | [diff] [blame] | 191 | extension_process_manager->OnNetworkRequestDone(render_view_host); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 192 | } else { |
| 193 | NOTREACHED(); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void ForwardRequestStatus( |
| 199 | RequestStatus status, net::URLRequest* request, void* profile_id) { |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 200 | const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 201 | if (!info) |
| 202 | return; |
| 203 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 204 | int process_id, render_view_id; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 205 | if (info->GetAssociatedRenderView(&process_id, &render_view_id)) { |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 206 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 207 | base::Bind(&NotifyEPMRequestStatus, |
| 208 | status, profile_id, process_id, render_view_id)); |
| 209 | } |
| 210 | } |
| 211 | |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 212 | void UpdateContentLengthPrefs(int received_content_length, |
| 213 | int original_content_length) { |
| 214 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 215 | DCHECK_GE(received_content_length, 0); |
| 216 | DCHECK_GE(original_content_length, 0); |
| 217 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 218 | // Can be NULL in a unit test. |
| 219 | if (!g_browser_process) |
| 220 | return; |
| 221 | |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 222 | PrefService* prefs = g_browser_process->local_state(); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 223 | if (!prefs) |
| 224 | return; |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 225 | |
| 226 | int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength); |
| 227 | int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength); |
| 228 | total_received += received_content_length; |
| 229 | total_original += original_content_length; |
| 230 | prefs->SetInt64(prefs::kHttpReceivedContentLength, total_received); |
| 231 | prefs->SetInt64(prefs::kHttpOriginalContentLength, total_original); |
| 232 | } |
| 233 | |
| 234 | void StoreAccumulatedContentLength(int received_content_length, |
| 235 | int original_content_length) { |
| 236 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 237 | base::Bind(&UpdateContentLengthPrefs, |
| 238 | received_content_length, original_content_length)); |
| 239 | } |
| 240 | |
| 241 | void RecordContentLengthHistograms( |
| 242 | int64 received_content_length, int64 original_content_length) { |
| 243 | #if defined(OS_ANDROID) |
| 244 | // Add the current resource to these histograms only when a valid |
| 245 | // X-Original-Content-Length header is present. |
| 246 | if (original_content_length >= 0) { |
| 247 | UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthWithValidOCL", |
| 248 | received_content_length); |
| 249 | UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLengthWithValidOCL", |
| 250 | original_content_length); |
| 251 | UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifferenceWithValidOCL", |
| 252 | original_content_length - received_content_length); |
| 253 | } else { |
| 254 | // Presume the original content length is the same as the received content |
| 255 | // length if the X-Original-Content-Header is not present. |
| 256 | original_content_length = received_content_length; |
| 257 | } |
| 258 | UMA_HISTOGRAM_COUNTS("Net.HttpContentLength", received_content_length); |
| 259 | UMA_HISTOGRAM_COUNTS("Net.HttpOriginalContentLength", |
| 260 | original_content_length); |
| 261 | UMA_HISTOGRAM_COUNTS("Net.HttpContentLengthDifference", |
| 262 | original_content_length - received_content_length); |
| 263 | #endif |
| 264 | } |
| 265 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 266 | } // namespace |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 267 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 268 | ChromeNetworkDelegate::ChromeNetworkDelegate( |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 269 | extensions::EventRouterForwarder* event_router, |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 270 | BooleanPrefMember* enable_referrers) |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 271 | : event_router_(event_router), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 272 | profile_(NULL), |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 273 | enable_referrers_(enable_referrers), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 274 | enable_do_not_track_(NULL), |
| 275 | force_google_safe_search_(NULL), |
| 276 | url_blacklist_manager_(NULL), |
| 277 | load_time_stats_(NULL), |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 278 | received_content_length_(0), |
| 279 | original_content_length_(0) { |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 280 | DCHECK(event_router); |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 281 | DCHECK(enable_referrers); |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 284 | ChromeNetworkDelegate::~ChromeNetworkDelegate() {} |
| 285 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 286 | void ChromeNetworkDelegate::set_extension_info_map( |
| 287 | ExtensionInfoMap* extension_info_map) { |
| 288 | extension_info_map_ = extension_info_map; |
| 289 | } |
| 290 | |
| 291 | void ChromeNetworkDelegate::set_cookie_settings( |
| 292 | CookieSettings* cookie_settings) { |
| 293 | cookie_settings_ = cookie_settings; |
| 294 | } |
| 295 | |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 296 | void ChromeNetworkDelegate::set_predictor( |
| 297 | chrome_browser_net::Predictor* predictor) { |
| 298 | connect_interceptor_.reset( |
| 299 | new chrome_browser_net::ConnectInterceptor(predictor)); |
| 300 | } |
| 301 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 302 | // static |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 303 | void ChromeNetworkDelegate::NeverThrottleRequests() { |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 304 | g_never_throttle_requests_ = true; |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 307 | // static |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 308 | void ChromeNetworkDelegate::InitializePrefsOnUIThread( |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 309 | BooleanPrefMember* enable_referrers, |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 310 | BooleanPrefMember* enable_do_not_track, |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 311 | BooleanPrefMember* force_google_safe_search, |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 312 | PrefService* pref_service) { |
| 313 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 314 | enable_referrers->Init(prefs::kEnableReferrers, pref_service); |
[email protected] | 6a1c98e0 | 2012-10-24 21:49:43 | [diff] [blame] | 315 | enable_referrers->MoveToThread( |
| 316 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 317 | if (enable_do_not_track) { |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 318 | enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service); |
[email protected] | 6a1c98e0 | 2012-10-24 21:49:43 | [diff] [blame] | 319 | enable_do_not_track->MoveToThread( |
| 320 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 321 | } |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 322 | if (force_google_safe_search) { |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 323 | force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 324 | force_google_safe_search->MoveToThread( |
| 325 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 326 | } |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 327 | } |
| 328 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 329 | // static |
| 330 | void ChromeNetworkDelegate::AllowAccessToAllFiles() { |
| 331 | g_allow_file_access_ = true; |
| 332 | } |
| 333 | |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 334 | // static |
| 335 | Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() { |
| 336 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 337 | PrefService* prefs = g_browser_process->local_state(); |
| 338 | int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength); |
| 339 | int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength); |
| 340 | |
| 341 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ae638fbe2 | 2012-12-07 02:57:42 | [diff] [blame] | 342 | // Use strings to avoid overflow. base::Value only supports 32-bit integers. |
| 343 | dict->SetString("historic_received_content_length", |
| 344 | base::Int64ToString(total_received)); |
| 345 | dict->SetString("historic_original_content_length", |
| 346 | base::Int64ToString(total_original)); |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 347 | return dict; |
| 348 | } |
| 349 | |
| 350 | Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const { |
| 351 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ae638fbe2 | 2012-12-07 02:57:42 | [diff] [blame] | 352 | // Use strings to avoid overflow. base::Value only supports 32-bit integers. |
| 353 | dict->SetString("session_received_content_length", |
| 354 | base::Int64ToString(received_content_length_)); |
| 355 | dict->SetString("session_original_content_length", |
| 356 | base::Int64ToString(original_content_length_)); |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 357 | return dict; |
| 358 | } |
| 359 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 360 | int ChromeNetworkDelegate::OnBeforeURLRequest( |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 361 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 362 | const net::CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 363 | GURL* new_url) { |
[email protected] | 3e598ff1 | 2011-09-06 11:22:34 | [diff] [blame] | 364 | #if defined(ENABLE_CONFIGURATION_POLICY) |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 365 | // TODO(joaodasilva): This prevents extensions from seeing URLs that are |
| 366 | // blocked. However, an extension might redirect the request to another URL, |
| 367 | // which is not blocked. |
| 368 | if (url_blacklist_manager_ && |
| 369 | url_blacklist_manager_->IsURLBlocked(request->url())) { |
| 370 | // URL access blocked by policy. |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 371 | request->net_log().AddEvent( |
[email protected] | 2fa0891 | 2012-06-14 20:56:26 | [diff] [blame] | 372 | net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, |
| 373 | net::NetLog::StringCallback("url", |
| 374 | &request->url().possibly_invalid_spec())); |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 375 | return net::ERR_NETWORK_ACCESS_DENIED; |
| 376 | } |
[email protected] | 3e598ff1 | 2011-09-06 11:22:34 | [diff] [blame] | 377 | #endif |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 378 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 379 | ForwardRequestStatus(REQUEST_STARTED, request, profile_); |
| 380 | |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 381 | if (!enable_referrers_->GetValue()) |
| 382 | request->set_referrer(std::string()); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 383 | if (enable_do_not_track_ && enable_do_not_track_->GetValue()) |
| 384 | request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 385 | |
| 386 | bool force_safe_search = force_google_safe_search_ && |
| 387 | force_google_safe_search_->GetValue(); |
| 388 | |
| 389 | net::CompletionCallback wrapped_callback = callback; |
| 390 | if (force_safe_search) { |
| 391 | wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, |
| 392 | callback, |
| 393 | base::Unretained(request), |
| 394 | base::Unretained(new_url)); |
| 395 | } |
| 396 | |
| 397 | int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest( |
| 398 | profile_, extension_info_map_.get(), request, wrapped_callback, |
| 399 | new_url); |
| 400 | |
| 401 | if (force_safe_search && rv == net::OK && new_url->is_empty()) |
| 402 | ForceGoogleSafeSearch(request, new_url); |
| 403 | |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 404 | if (connect_interceptor_) |
| 405 | connect_interceptor_->WitnessURLRequest(request); |
| 406 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 407 | return rv; |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 410 | int ChromeNetworkDelegate::OnBeforeSendHeaders( |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 411 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 412 | const net::CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 413 | net::HttpRequestHeaders* headers) { |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 414 | return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 415 | profile_, extension_info_map_.get(), request, callback, headers); |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 416 | } |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 417 | |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 418 | void ChromeNetworkDelegate::OnSendHeaders( |
| 419 | net::URLRequest* request, |
[email protected] | 783573b | 2011-05-13 11:05:15 | [diff] [blame] | 420 | const net::HttpRequestHeaders& headers) { |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 421 | ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders( |
| 422 | profile_, extension_info_map_.get(), request, headers); |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 425 | int ChromeNetworkDelegate::OnHeadersReceived( |
| 426 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 427 | const net::CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 428 | const net::HttpResponseHeaders* original_response_headers, |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 429 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { |
| 430 | return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived( |
| 431 | profile_, extension_info_map_.get(), request, callback, |
| 432 | original_response_headers, override_response_headers); |
| 433 | } |
| 434 | |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 435 | void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request, |
| 436 | const GURL& new_location) { |
| 437 | ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 438 | profile_, extension_info_map_.get(), request, new_location); |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 442 | void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) { |
[email protected] | 62fecae | 2011-04-21 11:08:24 | [diff] [blame] | 443 | ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 444 | profile_, extension_info_map_.get(), request); |
| 445 | ForwardProxyErrors(request, event_router_.get(), profile_); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 448 | void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request, |
| 449 | int bytes_read) { |
[email protected] | 6f4b4b4 | 2012-08-27 21:59:07 | [diff] [blame] | 450 | performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread( |
| 451 | request, bytes_read); |
| 452 | |
[email protected] | 44879ed | 2012-04-06 01:11:02 | [diff] [blame] | 453 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 454 | TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read); |
[email protected] | 44879ed | 2012-04-06 01:11:02 | [diff] [blame] | 455 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 456 | } |
| 457 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 458 | void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request, |
| 459 | bool started) { |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 460 | if (request->status().status() == net::URLRequestStatus::SUCCESS) { |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 461 | // For better accuracy, we use the actual bytes read instead of the length |
| 462 | // specified with the Content-Length header, which may be inaccurate, |
| 463 | // or missing, as is the case with chunked encoding. |
| 464 | int64 received_content_length = request->received_response_content_length(); |
| 465 | |
| 466 | // Only record for http or https urls. |
| 467 | bool is_http = request->url().SchemeIs("http"); |
| 468 | bool is_https = request->url().SchemeIs("https"); |
| 469 | |
| 470 | if (!request->was_cached() && // Don't record cached content |
| 471 | received_content_length && // Zero-byte responses aren't useful. |
| 472 | (is_http || is_https)) { // Only record for HTTP or HTTPS urls. |
| 473 | int64 original_content_length = |
| 474 | request->response_info().headers->GetInt64HeaderValue( |
| 475 | "x-original-content-length"); |
| 476 | // Since there was no indication of the original content length, presume |
| 477 | // it is no different from the number of bytes read. |
| 478 | int64 adjusted_original_content_length = original_content_length; |
| 479 | if (adjusted_original_content_length == -1) |
| 480 | adjusted_original_content_length = received_content_length; |
| 481 | AccumulateContentLength(received_content_length, |
| 482 | adjusted_original_content_length); |
| 483 | RecordContentLengthHistograms(received_content_length, |
| 484 | original_content_length); |
| 485 | DVLOG(2) << __FUNCTION__ |
| 486 | << " received content length: " << received_content_length |
| 487 | << " original content length: " << original_content_length |
| 488 | << " url: " << request->url(); |
| 489 | } |
| 490 | |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 491 | bool is_redirect = request->response_headers() && |
| 492 | net::HttpResponseHeaders::IsRedirectResponseCode( |
| 493 | request->response_headers()->response_code()); |
| 494 | if (!is_redirect) { |
| 495 | ExtensionWebRequestEventRouter::GetInstance()->OnCompleted( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 496 | profile_, extension_info_map_.get(), request); |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 497 | } |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 498 | } else if (request->status().status() == net::URLRequestStatus::FAILED || |
| 499 | request->status().status() == net::URLRequestStatus::CANCELED) { |
[email protected] | 05b6ab4 | 2011-04-23 13:46:04 | [diff] [blame] | 500 | ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 501 | profile_, extension_info_map_.get(), request, started); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 502 | } else { |
| 503 | NOTREACHED(); |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 504 | } |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 505 | ForwardProxyErrors(request, event_router_.get(), profile_); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 506 | |
| 507 | ForwardRequestStatus(REQUEST_DONE, request, profile_); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 508 | } |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 509 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 510 | void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) { |
| 511 | ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 512 | profile_, request); |
[email protected] | 16d5c72b | 2012-09-14 20:42:37 | [diff] [blame] | 513 | if (load_time_stats_) |
| 514 | load_time_stats_->OnURLRequestDestroyed(*request); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 515 | } |
| 516 | |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 517 | void ChromeNetworkDelegate::OnPACScriptError(int line_number, |
| 518 | const string16& error) { |
[email protected] | c454fe67 | 2012-03-12 21:18:01 | [diff] [blame] | 519 | extensions::ProxyEventRouter::GetInstance()->OnPACScriptError( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 520 | event_router_.get(), profile_, line_number, error); |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 521 | } |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 522 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 523 | net::NetworkDelegate::AuthRequiredResponse |
| 524 | ChromeNetworkDelegate::OnAuthRequired( |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 525 | net::URLRequest* request, |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 526 | const net::AuthChallengeInfo& auth_info, |
| 527 | const AuthCallback& callback, |
| 528 | net::AuthCredentials* credentials) { |
[email protected] | 90449ab | 2011-10-11 15:36:45 | [diff] [blame] | 529 | return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired( |
| 530 | profile_, extension_info_map_.get(), request, auth_info, |
| 531 | callback, credentials); |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 532 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 533 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 534 | bool ChromeNetworkDelegate::OnCanGetCookies( |
| 535 | const net::URLRequest& request, |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 536 | const net::CookieList& cookie_list) { |
| 537 | // NULL during tests, or when we're running in the system context. |
| 538 | if (!cookie_settings_) |
| 539 | return true; |
| 540 | |
| 541 | bool allow = cookie_settings_->IsReadingCookieAllowed( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 542 | request.url(), request.first_party_for_cookies()); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 543 | |
| 544 | int render_process_id = -1; |
| 545 | int render_view_id = -1; |
| 546 | if (content::ResourceRequestInfo::GetRenderViewForRequest( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 547 | &request, &render_process_id, &render_view_id)) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 548 | BrowserThread::PostTask( |
| 549 | BrowserThread::UI, FROM_HERE, |
| 550 | base::Bind(&TabSpecificContentSettings::CookiesRead, |
| 551 | render_process_id, render_view_id, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 552 | request.url(), request.first_party_for_cookies(), |
[email protected] | fd473d1 | 2012-04-05 11:38:43 | [diff] [blame] | 553 | cookie_list, !allow)); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | return allow; |
| 557 | } |
| 558 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 559 | bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request, |
| 560 | const std::string& cookie_line, |
| 561 | net::CookieOptions* options) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 562 | // NULL during tests, or when we're running in the system context. |
| 563 | if (!cookie_settings_) |
| 564 | return true; |
| 565 | |
| 566 | bool allow = cookie_settings_->IsSettingCookieAllowed( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 567 | request.url(), request.first_party_for_cookies()); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 568 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 569 | int render_process_id = -1; |
| 570 | int render_view_id = -1; |
| 571 | if (content::ResourceRequestInfo::GetRenderViewForRequest( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 572 | &request, &render_process_id, &render_view_id)) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 573 | BrowserThread::PostTask( |
| 574 | BrowserThread::UI, FROM_HERE, |
| 575 | base::Bind(&TabSpecificContentSettings::CookieChanged, |
| 576 | render_process_id, render_view_id, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 577 | request.url(), request.first_party_for_cookies(), |
[email protected] | fd473d1 | 2012-04-05 11:38:43 | [diff] [blame] | 578 | cookie_line, *options, !allow)); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | return allow; |
| 582 | } |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 583 | |
| 584 | bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request, |
| 585 | const FilePath& path) const { |
| 586 | if (g_allow_file_access_) |
| 587 | return true; |
| 588 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 589 | #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
| 590 | return true; |
| 591 | #else |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 592 | #if defined(OS_CHROMEOS) |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 593 | // If we're running Chrome for ChromeOS on Linux, we want to allow file |
| 594 | // access. |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 595 | if (!base::chromeos::IsRunningOnChromeOS() || |
| 596 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 597 | return true; |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 598 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 599 | |
| 600 | // Use a whitelist to only allow access to files residing in the list of |
| 601 | // directories below. |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 602 | static const char* const kLocalAccessWhiteList[] = { |
| 603 | "/home/chronos/user/Downloads", |
| 604 | "/home/chronos/user/log", |
| 605 | "/media", |
| 606 | "/opt/oem", |
| 607 | "/usr/share/chromeos-assets", |
| 608 | "/tmp", |
| 609 | "/var/log", |
| 610 | }; |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 611 | #elif defined(OS_ANDROID) |
| 612 | // Access to files in external storage is allowed. |
| 613 | FilePath external_storage_path; |
| 614 | PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path); |
| 615 | if (external_storage_path.IsParent(path)) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 616 | return true; |
| 617 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 618 | // Whitelist of other allowed directories. |
| 619 | static const char* const kLocalAccessWhiteList[] = { |
| 620 | "/sdcard", |
| 621 | "/mnt/sdcard", |
| 622 | }; |
| 623 | #endif |
| 624 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 625 | for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) { |
| 626 | const FilePath white_listed_path(kLocalAccessWhiteList[i]); |
| 627 | // FilePath::operator== should probably handle trailing separators. |
| 628 | if (white_listed_path == path.StripTrailingSeparators() || |
| 629 | white_listed_path.IsParent(path)) { |
| 630 | return true; |
| 631 | } |
| 632 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 633 | |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 634 | DVLOG(1) << "File access denied - " << path.value().c_str(); |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 635 | return false; |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 636 | #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 637 | } |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 638 | |
| 639 | bool ChromeNetworkDelegate::OnCanThrottleRequest( |
| 640 | const net::URLRequest& request) const { |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 641 | if (g_never_throttle_requests_) { |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 645 | return request.first_party_for_cookies().scheme() == |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 646 | extensions::kExtensionScheme; |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 647 | } |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 648 | |
| 649 | int ChromeNetworkDelegate::OnBeforeSocketStreamConnect( |
| 650 | net::SocketStream* socket, |
| 651 | const net::CompletionCallback& callback) { |
| 652 | #if defined(ENABLE_CONFIGURATION_POLICY) |
| 653 | if (url_blacklist_manager_ && |
| 654 | url_blacklist_manager_->IsURLBlocked(socket->url())) { |
| 655 | // URL access blocked by policy. |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 656 | socket->net_log()->AddEvent( |
[email protected] | 2fa0891 | 2012-06-14 20:56:26 | [diff] [blame] | 657 | net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, |
| 658 | net::NetLog::StringCallback("url", |
| 659 | &socket->url().possibly_invalid_spec())); |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 660 | return net::ERR_NETWORK_ACCESS_DENIED; |
| 661 | } |
| 662 | #endif |
| 663 | return net::OK; |
| 664 | } |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 665 | |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 666 | void ChromeNetworkDelegate::OnRequestWaitStateChange( |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 667 | const net::URLRequest& request, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 668 | RequestWaitState state) { |
| 669 | if (load_time_stats_) |
| 670 | load_time_stats_->OnRequestWaitStateChange(request, state); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 671 | } |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 672 | |
| 673 | void ChromeNetworkDelegate::AccumulateContentLength( |
| 674 | int64 received_content_length, int64 original_content_length) { |
| 675 | DCHECK_GE(received_content_length, 0); |
| 676 | DCHECK_GE(original_content_length, 0); |
| 677 | StoreAccumulatedContentLength(received_content_length, |
| 678 | original_content_length); |
| 679 | received_content_length_ += received_content_length; |
| 680 | original_content_length_ += original_content_length; |
| 681 | } |