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