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