[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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | eac11e1 | 2013-03-19 22:04:32 | [diff] [blame] | 8 | #include <stdlib.h> |
| 9 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 12 | #include "base/base_paths.h" |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 13 | #include "base/command_line.h" |
jochen | b93dba3c | 2014-11-11 05:28:41 | [diff] [blame] | 14 | #include "base/debug/alias.h" |
jochen | b5680e6 | 2014-10-13 08:43:41 | [diff] [blame] | 15 | #include "base/debug/dump_without_crashing.h" |
jochen | b93dba3c | 2014-11-11 05:28:41 | [diff] [blame] | 16 | #include "base/debug/stack_trace.h" |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 17 | #include "base/logging.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 18 | #include "base/macros.h" |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 19 | #include "base/metrics/field_trial.h" |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 20 | #include "base/metrics/histogram.h" |
sclittle | 3c16159e | 2014-12-15 19:53:17 | [diff] [blame] | 21 | #include "base/metrics/sparse_histogram.h" |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 22 | #include "base/metrics/user_metrics.h" |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 23 | #include "base/path_service.h" |
mmenke | 376b878 | 2015-05-05 04:41:30 | [diff] [blame] | 24 | #include "base/profiler/scoped_tracker.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 25 | #include "base/strings/string_number_conversions.h" |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
[email protected] | 069c4916 | 2013-09-11 20:46:35 | [diff] [blame] | 27 | #include "base/time/time.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 28 | #include "build/build_config.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 29 | #include "chrome/browser/browser_process.h" |
droger | b171697 | 2015-06-30 09:04:09 | [diff] [blame] | 30 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 31 | #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 32 | #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 33 | #include "chrome/browser/net/chrome_extensions_network_delegate.h" |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 34 | #include "chrome/browser/net/connect_interceptor.h" |
ttuttle | 05ae3f34 | 2015-07-13 17:38:35 | [diff] [blame] | 35 | #include "chrome/browser/net/request_source_bandwidth_histograms.h" |
[email protected] | 184799df | 2014-07-22 16:03:24 | [diff] [blame] | 36 | #include "chrome/browser/net/safe_search_util.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 37 | #include "chrome/browser/profiles/profile_manager.h" |
afakhry | 0501503 | 2015-08-14 01:09:56 | [diff] [blame] | 38 | #include "chrome/browser/task_management/task_manager_interface.h" |
fgorski | 708eadb | 2015-11-12 01:00:43 | [diff] [blame] | 39 | #include "chrome/common/chrome_constants.h" |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 40 | #include "chrome/common/features.h" |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 41 | #include "chrome/common/pref_names.h" |
droger | b171697 | 2015-06-30 09:04:09 | [diff] [blame] | 42 | #include "components/content_settings/core/browser/cookie_settings.h" |
sclittle | ae932be | 2015-10-08 20:53:50 | [diff] [blame] | 43 | #include "components/data_usage/core/data_use_aggregator.h" |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 44 | #include "components/domain_reliability/monitor.h" |
brettw | 03e6196 | 2016-03-15 06:27:04 | [diff] [blame] | 45 | #include "components/policy/core/browser/url_blacklist_manager.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 46 | #include "components/prefs/pref_member.h" |
| 47 | #include "components/prefs/pref_service.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 48 | #include "content/public/browser/browser_thread.h" |
[email protected] | 9dfed87 | 2013-12-30 23:08:56 | [diff] [blame] | 49 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 50 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 51 | #include "content/public/browser/resource_request_info.h" |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 52 | #include "content/public/common/content_switches.h" |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 53 | #include "content/public/common/process_type.h" |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 54 | #include "net/base/host_port_pair.h" |
sclittle | 3c16159e | 2014-12-15 19:53:17 | [diff] [blame] | 55 | #include "net/base/load_flags.h" |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 56 | #include "net/base/net_errors.h" |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 57 | #include "net/cookies/canonical_cookie.h" |
| 58 | #include "net/cookies/cookie_options.h" |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 59 | #include "net/http/http_request_headers.h" |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 60 | #include "net/http/http_response_headers.h" |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 61 | #include "net/http/http_status_code.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 62 | #include "net/log/net_log.h" |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 63 | #include "net/url_request/url_request.h" |
| 64 | |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 65 | #if BUILDFLAG(ANDROID_JAVA_UI) |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 66 | #include "chrome/browser/io_thread.h" |
twifkak | 85f9787 | 2015-06-24 01:00:13 | [diff] [blame] | 67 | #include "chrome/browser/precache/precache_manager_factory.h" |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 68 | #include "components/precache/content/precache_manager.h" |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 69 | #endif |
| 70 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 71 | #if defined(OS_CHROMEOS) |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 72 | #include "base/sys_info.h" |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 73 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 74 | #endif |
| 75 | |
[email protected] | 84b7a55 | 2014-07-19 04:52:06 | [diff] [blame] | 76 | #if defined(ENABLE_EXTENSIONS) |
| 77 | #include "extensions/common/constants.h" |
| 78 | #endif |
| 79 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 80 | using content::BrowserThread; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 81 | using content::RenderViewHost; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 82 | using content::ResourceRequestInfo; |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 83 | using content::ResourceType; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 84 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 85 | // By default we don't allow access to all file:// urls on ChromeOS and |
| 86 | // Android. |
| 87 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 88 | bool ChromeNetworkDelegate::g_allow_file_access_ = false; |
| 89 | #else |
| 90 | bool ChromeNetworkDelegate::g_allow_file_access_ = true; |
| 91 | #endif |
| 92 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 93 | namespace { |
| 94 | |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 95 | const char kDNTHeader[] = "DNT"; |
| 96 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 97 | // Gets called when the extensions finish work on the URL. If the extensions |
| 98 | // did not do a redirect (so |new_url| is empty) then we enforce the |
| 99 | // SafeSearch parameters. Otherwise we will get called again after the |
| 100 | // redirect and we enforce SafeSearch then. |
| 101 | void ForceGoogleSafeSearchCallbackWrapper( |
| 102 | const net::CompletionCallback& callback, |
| 103 | net::URLRequest* request, |
| 104 | GURL* new_url, |
| 105 | int rv) { |
| 106 | if (rv == net::OK && new_url->is_empty()) |
[email protected] | 184799df | 2014-07-22 16:03:24 | [diff] [blame] | 107 | safe_search_util::ForceGoogleSafeSearch(request, new_url); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 108 | callback.Run(rv); |
| 109 | } |
| 110 | |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 111 | #if BUILDFLAG(ANDROID_JAVA_UI) |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 112 | void RecordPrecacheStatsOnUIThread(const GURL& url, |
twifkak | 981c7ea | 2015-08-13 17:56:08 | [diff] [blame] | 113 | const GURL& referrer, |
twifkak | 173e751 | 2015-08-11 18:53:30 | [diff] [blame] | 114 | base::TimeDelta latency, |
| 115 | const base::Time& fetch_time, |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 116 | int64_t size, |
twifkak | 173e751 | 2015-08-11 18:53:30 | [diff] [blame] | 117 | bool was_cached, |
| 118 | void* profile_id) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 119 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 120 | |
krasin | 991cf746 | 2015-11-09 19:35:54 | [diff] [blame] | 121 | if (!g_browser_process->profile_manager()->IsValidProfile(profile_id)) |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 122 | return; |
krasin | 991cf746 | 2015-11-09 19:35:54 | [diff] [blame] | 123 | Profile* profile = reinterpret_cast<Profile*>(profile_id); |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 124 | |
| 125 | precache::PrecacheManager* precache_manager = |
| 126 | precache::PrecacheManagerFactory::GetForBrowserContext(profile); |
twifkak | b217543 | 2015-08-07 20:19:07 | [diff] [blame] | 127 | // |precache_manager| could be NULL if the profile is off the record. |
twifkak | d0487ff | 2015-08-28 16:08:22 | [diff] [blame] | 128 | if (!precache_manager || !precache_manager->IsPrecachingAllowed()) |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 129 | return; |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 130 | |
twifkak | 981c7ea | 2015-08-13 17:56:08 | [diff] [blame] | 131 | precache_manager->RecordStatsForFetch(url, referrer, latency, fetch_time, |
| 132 | size, was_cached); |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 133 | } |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 134 | #endif // BUILDFLAG(ANDROID_JAVA_UI) |
[email protected] | 3555881 | 2013-12-18 21:58:36 | [diff] [blame] | 135 | |
jochen | 65227537 | 2015-01-16 11:35:27 | [diff] [blame] | 136 | void ReportInvalidReferrerSendOnUI() { |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 137 | base::RecordAction( |
| 138 | base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer")); |
jochen | 65227537 | 2015-01-16 11:35:27 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void ReportInvalidReferrerSend(const GURL& target_url, |
| 142 | const GURL& referrer_url) { |
jochen | 4c874d2 | 2015-04-13 14:01:04 | [diff] [blame] | 143 | LOG(ERROR) << "Cancelling request to " << target_url |
| 144 | << " with invalid referrer " << referrer_url; |
jochen | 65227537 | 2015-01-16 11:35:27 | [diff] [blame] | 145 | // Record information to help debug http://crbug.com/422871 |
| 146 | if (!target_url.SchemeIsHTTPOrHTTPS()) |
| 147 | return; |
| 148 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 149 | base::Bind(&ReportInvalidReferrerSendOnUI)); |
jochen | b5680e6 | 2014-10-13 08:43:41 | [diff] [blame] | 150 | base::debug::DumpWithoutCrashing(); |
jochen | 4c874d2 | 2015-04-13 14:01:04 | [diff] [blame] | 151 | NOTREACHED(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 152 | } |
| 153 | |
sclittle | 3c16159e | 2014-12-15 19:53:17 | [diff] [blame] | 154 | // Record network errors that HTTP requests complete with, including OK and |
| 155 | // ABORTED. |
| 156 | void RecordNetworkErrorHistograms(const net::URLRequest* request) { |
| 157 | if (request->url().SchemeIs("http")) { |
| 158 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes", |
| 159 | std::abs(request->status().error())); |
| 160 | |
| 161 | if (request->load_flags() & net::LOAD_MAIN_FRAME) { |
| 162 | UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 163 | "Net.HttpRequestCompletionErrorCodes.MainFrame", |
| 164 | std::abs(request->status().error())); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 169 | // Returns whether |request| is likely to be eligible for delta-encoding. |
| 170 | // This is only a rough approximation right now, based on MIME type. |
| 171 | bool CanRequestBeDeltaEncoded(const net::URLRequest* request) { |
| 172 | struct { |
| 173 | const char *prefix; |
| 174 | const char *suffix; |
| 175 | } kEligibleMasks[] = { |
| 176 | // All text/ types are eligible, even if not displayable. |
| 177 | { "text/", NULL }, |
| 178 | // JSON (application/json and application/*+json) is eligible. |
| 179 | { "application/", "json" }, |
| 180 | // Javascript is eligible. |
| 181 | { "application/", "javascript" }, |
| 182 | // XML (application/xml and application/*+xml) is eligible. |
| 183 | { "application/", "xml" }, |
| 184 | }; |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 185 | |
| 186 | std::string mime_type; |
| 187 | request->GetMimeType(&mime_type); |
| 188 | |
| 189 | for (size_t i = 0; i < arraysize(kEligibleMasks); i++) { |
| 190 | const char *prefix = kEligibleMasks[i].prefix; |
| 191 | const char *suffix = kEligibleMasks[i].suffix; |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 192 | if (prefix && |
| 193 | !base::StartsWith(mime_type, prefix, base::CompareCase::SENSITIVE)) |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 194 | continue; |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 195 | if (suffix && |
| 196 | !base::EndsWith(mime_type, suffix, base::CompareCase::SENSITIVE)) |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 197 | continue; |
| 198 | return true; |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | // Returns whether |request| was issued by a renderer process, as opposed to |
| 204 | // the browser process or a plugin process. |
| 205 | bool IsRendererInitiatedRequest(const net::URLRequest* request) { |
| 206 | const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 207 | return info && info->GetProcessType() == content::PROCESS_TYPE_RENDERER; |
| 208 | } |
| 209 | |
| 210 | // Uploads UMA histograms for delta encoding eligibility. This method can only |
| 211 | // be safely called after the network stack has called both OnStarted and |
| 212 | // OnCompleted, since it needs the received response content length and the |
| 213 | // response headers. |
| 214 | void RecordCacheStateStats(const net::URLRequest* request) { |
| 215 | net::HttpRequestHeaders request_headers; |
| 216 | if (!request->GetFullRequestHeaders(&request_headers)) { |
| 217 | // GetFullRequestHeaders is guaranteed to succeed if OnResponseStarted() has |
| 218 | // been called on |request|, so if GetFullRequestHeaders() fails, |
| 219 | // RecordCacheStateStats must have been called before |
| 220 | // OnResponseStarted(). |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | if (!IsRendererInitiatedRequest(request)) { |
| 225 | // Ignore browser-initiated requests. These are internal requests like safe |
| 226 | // browsing and sync, and so on. Some of these could be eligible for |
| 227 | // delta-encoding, but to be conservative this function ignores all of them. |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | const int kCacheAffectingFlags = net::LOAD_BYPASS_CACHE | |
| 232 | net::LOAD_DISABLE_CACHE | |
| 233 | net::LOAD_PREFERRING_CACHE; |
| 234 | |
| 235 | if (request->load_flags() & kCacheAffectingFlags) { |
| 236 | // Ignore requests with cache-affecting flags, which would otherwise mess up |
| 237 | // these stats. |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | enum { |
| 242 | CACHE_STATE_FROM_CACHE, |
| 243 | CACHE_STATE_STILL_VALID, |
| 244 | CACHE_STATE_NO_LONGER_VALID, |
| 245 | CACHE_STATE_NO_ENTRY, |
| 246 | CACHE_STATE_MAX, |
| 247 | } state = CACHE_STATE_NO_ENTRY; |
| 248 | bool had_cache_headers = |
| 249 | request_headers.HasHeader(net::HttpRequestHeaders::kIfModifiedSince) || |
| 250 | request_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch) || |
| 251 | request_headers.HasHeader(net::HttpRequestHeaders::kIfRange); |
| 252 | if (request->was_cached() && !had_cache_headers) { |
| 253 | // Entry was served directly from cache. |
| 254 | state = CACHE_STATE_FROM_CACHE; |
| 255 | } else if (request->was_cached() && had_cache_headers) { |
| 256 | // Expired entry was present in cache, and server responded with NOT |
| 257 | // MODIFIED, indicating the expired entry is still valid. |
| 258 | state = CACHE_STATE_STILL_VALID; |
| 259 | } else if (!request->was_cached() && had_cache_headers) { |
| 260 | // Expired entry was present in cache, and server responded with something |
| 261 | // other than NOT MODIFIED, indicating the entry is no longer valid. |
| 262 | state = CACHE_STATE_NO_LONGER_VALID; |
| 263 | } else if (!request->was_cached() && !had_cache_headers) { |
| 264 | // Neither |was_cached| nor |had_cache_headers|, so there's no local cache |
| 265 | // entry for this content at all. |
| 266 | state = CACHE_STATE_NO_ENTRY; |
| 267 | } |
| 268 | |
| 269 | UMA_HISTOGRAM_ENUMERATION("Net.CacheState.AllRequests", state, |
| 270 | CACHE_STATE_MAX); |
| 271 | if (CanRequestBeDeltaEncoded(request)) { |
| 272 | UMA_HISTOGRAM_ENUMERATION("Net.CacheState.EncodeableRequests", state, |
| 273 | CACHE_STATE_MAX); |
| 274 | } |
| 275 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 276 | int64_t size = request->received_response_content_length(); |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 277 | if (size >= 0 && state == CACHE_STATE_NO_LONGER_VALID) { |
| 278 | UMA_HISTOGRAM_COUNTS("Net.CacheState.AllBytes", size); |
| 279 | if (CanRequestBeDeltaEncoded(request)) { |
| 280 | UMA_HISTOGRAM_COUNTS("Net.CacheState.EncodeableBytes", size); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 285 | } // namespace |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 286 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 287 | ChromeNetworkDelegate::ChromeNetworkDelegate( |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 288 | extensions::EventRouterForwarder* event_router, |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame^] | 289 | BooleanPrefMember* enable_referrers, |
| 290 | const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder) |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 291 | : profile_(NULL), |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 292 | enable_referrers_(enable_referrers), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 293 | enable_do_not_track_(NULL), |
| 294 | force_google_safe_search_(NULL), |
treib | 4e26f665 | 2014-12-01 14:34:18 | [diff] [blame] | 295 | force_youtube_safety_mode_(NULL), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 296 | url_blacklist_manager_(NULL), |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 297 | domain_reliability_monitor_(NULL), |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame^] | 298 | data_use_measurement_(metrics_data_use_forwarder), |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 299 | experimental_web_platform_features_enabled_( |
gayane | 0b46091c | 2016-04-07 21:01:05 | [diff] [blame^] | 300 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 301 | switches::kEnableExperimentalWebPlatformFeatures)), |
sclittle | ae932be | 2015-10-08 20:53:50 | [diff] [blame] | 302 | data_use_aggregator_(nullptr), |
| 303 | is_data_usage_off_the_record_(true) { |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 304 | DCHECK(enable_referrers); |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 305 | extensions_delegate_.reset( |
| 306 | ChromeExtensionsNetworkDelegate::Create(event_router)); |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 309 | ChromeNetworkDelegate::~ChromeNetworkDelegate() {} |
| 310 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 311 | void ChromeNetworkDelegate::set_extension_info_map( |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 312 | extensions::InfoMap* extension_info_map) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 313 | extensions_delegate_->set_extension_info_map(extension_info_map); |
| 314 | } |
| 315 | |
| 316 | void ChromeNetworkDelegate::set_profile(void* profile) { |
| 317 | profile_ = profile; |
| 318 | extensions_delegate_->set_profile(profile); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void ChromeNetworkDelegate::set_cookie_settings( |
droger | b171697 | 2015-06-30 09:04:09 | [diff] [blame] | 322 | content_settings::CookieSettings* cookie_settings) { |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 323 | cookie_settings_ = cookie_settings; |
| 324 | } |
| 325 | |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 326 | void ChromeNetworkDelegate::set_predictor( |
| 327 | chrome_browser_net::Predictor* predictor) { |
| 328 | connect_interceptor_.reset( |
| 329 | new chrome_browser_net::ConnectInterceptor(predictor)); |
| 330 | } |
| 331 | |
sclittle | ae932be | 2015-10-08 20:53:50 | [diff] [blame] | 332 | void ChromeNetworkDelegate::set_data_use_aggregator( |
| 333 | data_usage::DataUseAggregator* data_use_aggregator, |
| 334 | bool is_data_usage_off_the_record) { |
| 335 | data_use_aggregator_ = data_use_aggregator; |
| 336 | is_data_usage_off_the_record_ = is_data_usage_off_the_record; |
| 337 | } |
| 338 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 339 | // static |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 340 | void ChromeNetworkDelegate::InitializePrefsOnUIThread( |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 341 | BooleanPrefMember* enable_referrers, |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 342 | BooleanPrefMember* enable_do_not_track, |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 343 | BooleanPrefMember* force_google_safe_search, |
treib | 4e26f665 | 2014-12-01 14:34:18 | [diff] [blame] | 344 | BooleanPrefMember* force_youtube_safety_mode, |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 345 | PrefService* pref_service) { |
anujk.sharma | 2e02ce16 | 2015-04-29 23:10:02 | [diff] [blame] | 346 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 347 | enable_referrers->Init(prefs::kEnableReferrers, pref_service); |
[email protected] | 6a1c98e0 | 2012-10-24 21:49:43 | [diff] [blame] | 348 | enable_referrers->MoveToThread( |
| 349 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 350 | if (enable_do_not_track) { |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 351 | enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service); |
[email protected] | 6a1c98e0 | 2012-10-24 21:49:43 | [diff] [blame] | 352 | enable_do_not_track->MoveToThread( |
| 353 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 354 | } |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 355 | if (force_google_safe_search) { |
treib | 4e26f665 | 2014-12-01 14:34:18 | [diff] [blame] | 356 | force_google_safe_search->Init(prefs::kForceGoogleSafeSearch, pref_service); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 357 | force_google_safe_search->MoveToThread( |
| 358 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 359 | } |
treib | 4e26f665 | 2014-12-01 14:34:18 | [diff] [blame] | 360 | if (force_youtube_safety_mode) { |
| 361 | force_youtube_safety_mode->Init(prefs::kForceYouTubeSafetyMode, |
| 362 | pref_service); |
| 363 | force_youtube_safety_mode->MoveToThread( |
| 364 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 365 | } |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 368 | // static |
| 369 | void ChromeNetworkDelegate::AllowAccessToAllFiles() { |
| 370 | g_allow_file_access_ = true; |
| 371 | } |
| 372 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 373 | int ChromeNetworkDelegate::OnBeforeURLRequest( |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 374 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 375 | const net::CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 376 | GURL* new_url) { |
mmenke | 376b878 | 2015-05-05 04:41:30 | [diff] [blame] | 377 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed. |
| 378 | tracked_objects::ScopedTracker tracking_profile1( |
| 379 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 380 | "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest")); |
| 381 | |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 382 | // TODO(joaodasilva): This prevents extensions from seeing URLs that are |
| 383 | // blocked. However, an extension might redirect the request to another URL, |
| 384 | // which is not blocked. |
mmenke | f1aa907 | 2015-05-18 16:21:04 | [diff] [blame] | 385 | |
| 386 | const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
[email protected] | cb85f8e5 | 2014-04-08 11:44:40 | [diff] [blame] | 387 | int error = net::ERR_BLOCKED_BY_ADMINISTRATOR; |
mmenke | f1aa907 | 2015-05-18 16:21:04 | [diff] [blame] | 388 | if (info && content::IsResourceTypeFrame(info->GetResourceType()) && |
| 389 | url_blacklist_manager_ && |
| 390 | url_blacklist_manager_->ShouldBlockRequestForFrame( |
| 391 | request->url(), &error)) { |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 392 | // URL access blocked by policy. |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 393 | request->net_log().AddEvent( |
[email protected] | 2fa0891 | 2012-06-14 20:56:26 | [diff] [blame] | 394 | net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, |
| 395 | net::NetLog::StringCallback("url", |
| 396 | &request->url().possibly_invalid_spec())); |
[email protected] | cb85f8e5 | 2014-04-08 11:44:40 | [diff] [blame] | 397 | return error; |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 398 | } |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 399 | |
mmenke | 376b878 | 2015-05-05 04:41:30 | [diff] [blame] | 400 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed. |
| 401 | tracked_objects::ScopedTracker tracking_profile2( |
| 402 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 403 | "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 2")); |
| 404 | |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 405 | extensions_delegate_->ForwardStartRequestStatus(request); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 406 | |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 407 | if (!enable_referrers_->GetValue()) |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 408 | request->SetReferrer(std::string()); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 409 | if (enable_do_not_track_ && enable_do_not_track_->GetValue()) |
| 410 | request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 411 | |
mmenke | 376b878 | 2015-05-05 04:41:30 | [diff] [blame] | 412 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed. |
| 413 | tracked_objects::ScopedTracker tracking_profile3( |
| 414 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 415 | "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 3")); |
| 416 | |
treib | 4e26f665 | 2014-12-01 14:34:18 | [diff] [blame] | 417 | bool force_safe_search = |
treib | 4e26f665 | 2014-12-01 14:34:18 | [diff] [blame] | 418 | (force_google_safe_search_ && force_google_safe_search_->GetValue()); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 419 | |
| 420 | net::CompletionCallback wrapped_callback = callback; |
| 421 | if (force_safe_search) { |
| 422 | wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, |
| 423 | callback, |
| 424 | base::Unretained(request), |
| 425 | base::Unretained(new_url)); |
| 426 | } |
| 427 | |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 428 | int rv = extensions_delegate_->OnBeforeURLRequest( |
| 429 | request, wrapped_callback, new_url); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 430 | |
mmenke | 376b878 | 2015-05-05 04:41:30 | [diff] [blame] | 431 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed. |
| 432 | tracked_objects::ScopedTracker tracking_profile4( |
| 433 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 434 | "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 4")); |
| 435 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 436 | if (force_safe_search && rv == net::OK && new_url->is_empty()) |
[email protected] | 184799df | 2014-07-22 16:03:24 | [diff] [blame] | 437 | safe_search_util::ForceGoogleSafeSearch(request, new_url); |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 438 | |
mmenke | 376b878 | 2015-05-05 04:41:30 | [diff] [blame] | 439 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed. |
| 440 | tracked_objects::ScopedTracker tracking_profile5( |
| 441 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 442 | "456327 URLRequest::ChromeNetworkDelegate::OnBeforeURLRequest 5")); |
| 443 | |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 444 | if (connect_interceptor_) |
| 445 | connect_interceptor_->WitnessURLRequest(request); |
| 446 | |
[email protected] | d1208ba3 | 2012-11-08 11:10:33 | [diff] [blame] | 447 | return rv; |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 450 | int ChromeNetworkDelegate::OnBeforeSendHeaders( |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 451 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 452 | const net::CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 453 | net::HttpRequestHeaders* headers) { |
treib | 2b18805 | 2015-04-17 12:32:50 | [diff] [blame] | 454 | if (force_youtube_safety_mode_ && force_youtube_safety_mode_->GetValue()) |
[email protected] | 184799df | 2014-07-22 16:03:24 | [diff] [blame] | 455 | safe_search_util::ForceYouTubeSafetyMode(request, headers); |
| 456 | |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 457 | return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers); |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 458 | } |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 459 | |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 460 | void ChromeNetworkDelegate::OnSendHeaders( |
| 461 | net::URLRequest* request, |
[email protected] | 783573b | 2011-05-13 11:05:15 | [diff] [blame] | 462 | const net::HttpRequestHeaders& headers) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 463 | extensions_delegate_->OnSendHeaders(request, headers); |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 466 | int ChromeNetworkDelegate::OnHeadersReceived( |
| 467 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 468 | const net::CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 469 | const net::HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 470 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers, |
| 471 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 472 | return extensions_delegate_->OnHeadersReceived( |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 473 | request, |
| 474 | callback, |
| 475 | original_response_headers, |
| 476 | override_response_headers, |
| 477 | allowed_unsafe_redirect_url); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 480 | void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request, |
| 481 | const GURL& new_location) { |
amohammadkhan | 092adb2 | 2015-09-11 21:08:49 | [diff] [blame] | 482 | // Recording data use of request on redirects. |
amohammadkhan | 092adb2 | 2015-09-11 21:08:49 | [diff] [blame] | 483 | data_use_measurement_.ReportDataUseUMA(request); |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 484 | if (domain_reliability_monitor_) |
| 485 | domain_reliability_monitor_->OnBeforeRedirect(request); |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 486 | extensions_delegate_->OnBeforeRedirect(request, new_location); |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 490 | void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 491 | extensions_delegate_->OnResponseStarted(request); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 492 | } |
| 493 | |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 494 | void ChromeNetworkDelegate::OnNetworkBytesReceived(net::URLRequest* request, |
| 495 | int64_t bytes_received) { |
[email protected] | 44879ed | 2012-04-06 01:11:02 | [diff] [blame] | 496 | #if defined(ENABLE_TASK_MANAGER) |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 497 | // Note: Currently, OnNetworkBytesReceived is only implemented for HTTP jobs, |
| 498 | // not FTP or other types, so those kinds of bytes will not be reported here. |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 499 | task_management::TaskManagerInterface::OnRawBytesRead(*request, |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 500 | bytes_received); |
[email protected] | 44879ed | 2012-04-06 01:11:02 | [diff] [blame] | 501 | #endif // defined(ENABLE_TASK_MANAGER) |
sclittle | ae932be | 2015-10-08 20:53:50 | [diff] [blame] | 502 | |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 503 | ReportDataUsageStats(request, 0 /* tx_bytes */, bytes_received); |
sclittle | ae932be | 2015-10-08 20:53:50 | [diff] [blame] | 504 | } |
| 505 | |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 506 | void ChromeNetworkDelegate::OnNetworkBytesSent(net::URLRequest* request, |
sclittle | ae932be | 2015-10-08 20:53:50 | [diff] [blame] | 507 | int64_t bytes_sent) { |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 508 | ReportDataUsageStats(request, bytes_sent, 0 /* rx_bytes */); |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 509 | } |
| 510 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 511 | void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request, |
| 512 | bool started) { |
amohammadkhan | 092adb2 | 2015-09-11 21:08:49 | [diff] [blame] | 513 | // TODO(amohammadkhan): Verify that there is no double recording in data use |
| 514 | // of redirected requests. |
| 515 | data_use_measurement_.ReportDataUseUMA(request); |
sclittle | 3c16159e | 2014-12-15 19:53:17 | [diff] [blame] | 516 | RecordNetworkErrorHistograms(request); |
ellyjones | d84033d | 2015-02-12 19:20:34 | [diff] [blame] | 517 | if (started) { |
| 518 | // Only call in for requests that were started, to obey the precondition |
| 519 | // that RecordCacheStateStats can only be called on requests for which |
| 520 | // OnResponseStarted was called. |
| 521 | RecordCacheStateStats(request); |
| 522 | } |
sclittle | 3c16159e | 2014-12-15 19:53:17 | [diff] [blame] | 523 | |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 524 | if (request->status().status() == net::URLRequestStatus::SUCCESS) { |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 525 | #if BUILDFLAG(ANDROID_JAVA_UI) |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 526 | // For better accuracy, we use the actual bytes read instead of the length |
| 527 | // specified with the Content-Length header, which may be inaccurate, |
| 528 | // or missing, as is the case with chunked encoding. |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 529 | int64_t received_content_length = |
| 530 | request->received_response_content_length(); |
twifkak | 173e751 | 2015-08-11 18:53:30 | [diff] [blame] | 531 | base::TimeDelta latency = base::TimeTicks::Now() - request->creation_time(); |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 532 | |
twifkak | b217543 | 2015-08-07 20:19:07 | [diff] [blame] | 533 | // Record precache metrics when a fetch is completed successfully, if |
| 534 | // precaching is allowed. |
twifkak | c07ac94 | 2015-07-20 18:23:04 | [diff] [blame] | 535 | BrowserThread::PostTask( |
| 536 | BrowserThread::UI, FROM_HERE, |
twifkak | 981c7ea | 2015-08-13 17:56:08 | [diff] [blame] | 537 | base::Bind(&RecordPrecacheStatsOnUIThread, request->url(), |
| 538 | GURL(request->referrer()), latency, base::Time::Now(), |
| 539 | received_content_length, request->was_cached(), profile_)); |
sievers | 2f1e811 | 2015-12-04 18:43:56 | [diff] [blame] | 540 | #endif // BUILDFLAG(ANDROID_JAVA_UI) |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 541 | extensions_delegate_->OnCompleted(request, started); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 542 | } else if (request->status().status() == net::URLRequestStatus::FAILED || |
| 543 | request->status().status() == net::URLRequestStatus::CANCELED) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 544 | extensions_delegate_->OnCompleted(request, started); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 545 | } else { |
| 546 | NOTREACHED(); |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 547 | } |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 548 | if (domain_reliability_monitor_) |
| 549 | domain_reliability_monitor_->OnCompleted(request, started); |
ttuttle | 05ae3f34 | 2015-07-13 17:38:35 | [diff] [blame] | 550 | RecordRequestSourceBandwidth(request, started); |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 551 | extensions_delegate_->ForwardProxyErrors(request); |
| 552 | extensions_delegate_->ForwardDoneRequestStatus(request); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 553 | } |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 554 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 555 | void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 556 | extensions_delegate_->OnURLRequestDestroyed(request); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 557 | } |
| 558 | |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 559 | void ChromeNetworkDelegate::OnPACScriptError(int line_number, |
[email protected] | 439f1e3 | 2013-12-09 20:09:09 | [diff] [blame] | 560 | const base::string16& error) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 561 | extensions_delegate_->OnPACScriptError(line_number, error); |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 562 | } |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 563 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 564 | net::NetworkDelegate::AuthRequiredResponse |
| 565 | ChromeNetworkDelegate::OnAuthRequired( |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 566 | net::URLRequest* request, |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 567 | const net::AuthChallengeInfo& auth_info, |
| 568 | const AuthCallback& callback, |
| 569 | net::AuthCredentials* credentials) { |
[email protected] | 4a2b623 | 2014-06-19 08:44:14 | [diff] [blame] | 570 | return extensions_delegate_->OnAuthRequired( |
| 571 | request, auth_info, callback, credentials); |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 572 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 573 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 574 | bool ChromeNetworkDelegate::OnCanGetCookies( |
| 575 | const net::URLRequest& request, |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 576 | const net::CookieList& cookie_list) { |
| 577 | // NULL during tests, or when we're running in the system context. |
[email protected] | 5173de8b | 2013-06-02 21:16:02 | [diff] [blame] | 578 | if (!cookie_settings_.get()) |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 579 | return true; |
| 580 | |
| 581 | bool allow = cookie_settings_->IsReadingCookieAllowed( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 582 | request.url(), request.first_party_for_cookies()); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 583 | |
| 584 | int render_process_id = -1; |
[email protected] | f3add92 | 2013-12-20 23:17:16 | [diff] [blame] | 585 | int render_frame_id = -1; |
| 586 | if (content::ResourceRequestInfo::GetRenderFrameForRequest( |
| 587 | &request, &render_process_id, &render_frame_id)) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 588 | BrowserThread::PostTask( |
| 589 | BrowserThread::UI, FROM_HERE, |
| 590 | base::Bind(&TabSpecificContentSettings::CookiesRead, |
[email protected] | f3add92 | 2013-12-20 23:17:16 | [diff] [blame] | 591 | render_process_id, render_frame_id, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 592 | request.url(), request.first_party_for_cookies(), |
davidben | f47d191 | 2015-03-14 00:24:24 | [diff] [blame] | 593 | cookie_list, !allow)); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return allow; |
| 597 | } |
| 598 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 599 | bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request, |
| 600 | const std::string& cookie_line, |
| 601 | net::CookieOptions* options) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 602 | // NULL during tests, or when we're running in the system context. |
[email protected] | 5173de8b | 2013-06-02 21:16:02 | [diff] [blame] | 603 | if (!cookie_settings_.get()) |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 604 | return true; |
| 605 | |
| 606 | bool allow = cookie_settings_->IsSettingCookieAllowed( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 607 | request.url(), request.first_party_for_cookies()); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 608 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 609 | int render_process_id = -1; |
[email protected] | f3add92 | 2013-12-20 23:17:16 | [diff] [blame] | 610 | int render_frame_id = -1; |
| 611 | if (content::ResourceRequestInfo::GetRenderFrameForRequest( |
| 612 | &request, &render_process_id, &render_frame_id)) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 613 | BrowserThread::PostTask( |
| 614 | BrowserThread::UI, FROM_HERE, |
| 615 | base::Bind(&TabSpecificContentSettings::CookieChanged, |
[email protected] | f3add92 | 2013-12-20 23:17:16 | [diff] [blame] | 616 | render_process_id, render_frame_id, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 617 | request.url(), request.first_party_for_cookies(), |
[email protected] | fd473d1 | 2012-04-05 11:38:43 | [diff] [blame] | 618 | cookie_line, *options, !allow)); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | return allow; |
| 622 | } |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 623 | |
| 624 | bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 625 | const base::FilePath& path) const { |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 626 | if (g_allow_file_access_) |
| 627 | return true; |
| 628 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 629 | #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
| 630 | return true; |
| 631 | #else |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 632 | #if defined(OS_CHROMEOS) |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 633 | // If we're running Chrome for ChromeOS on Linux, we want to allow file |
| 634 | // access. |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 635 | if (!base::SysInfo::IsRunningOnChromeOS() || |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 636 | base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 637 | return true; |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 638 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 639 | |
| 640 | // Use a whitelist to only allow access to files residing in the list of |
| 641 | // directories below. |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 642 | static const char* const kLocalAccessWhiteList[] = { |
| 643 | "/home/chronos/user/Downloads", |
| 644 | "/home/chronos/user/log", |
[email protected] | 4791af43d | 2014-05-05 13:19:42 | [diff] [blame] | 645 | "/home/chronos/user/WebRTC Logs", |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 646 | "/media", |
| 647 | "/opt/oem", |
| 648 | "/usr/share/chromeos-assets", |
| 649 | "/tmp", |
| 650 | "/var/log", |
| 651 | }; |
[email protected] | ae015e1 | 2013-11-04 19:11:33 | [diff] [blame] | 652 | |
[email protected] | 9e73328 | 2014-06-18 16:56:55 | [diff] [blame] | 653 | // The actual location of "/home/chronos/user/Xyz" is the Xyz directory under |
| 654 | // the profile path ("/home/chronos/user' is a hard link to current primary |
| 655 | // logged in profile.) For the support of multi-profile sessions, we are |
| 656 | // switching to use explicit "$PROFILE_PATH/Xyz" path and here whitelist such |
| 657 | // access. |
[email protected] | ae015e1 | 2013-11-04 19:11:33 | [diff] [blame] | 658 | if (!profile_path_.empty()) { |
| 659 | const base::FilePath downloads = profile_path_.AppendASCII("Downloads"); |
| 660 | if (downloads == path.StripTrailingSeparators() || downloads.IsParent(path)) |
| 661 | return true; |
[email protected] | 9e73328 | 2014-06-18 16:56:55 | [diff] [blame] | 662 | const base::FilePath webrtc_logs = profile_path_.AppendASCII("WebRTC Logs"); |
| 663 | if (webrtc_logs == path.StripTrailingSeparators() || |
| 664 | webrtc_logs.IsParent(path)) { |
| 665 | return true; |
| 666 | } |
[email protected] | ae015e1 | 2013-11-04 19:11:33 | [diff] [blame] | 667 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 668 | #elif defined(OS_ANDROID) |
| 669 | // Access to files in external storage is allowed. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 670 | base::FilePath external_storage_path; |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 671 | PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path); |
| 672 | if (external_storage_path.IsParent(path)) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 673 | return true; |
| 674 | |
fgorski | 708eadb | 2015-11-12 01:00:43 | [diff] [blame] | 675 | // Allow to load offline pages, which are stored in the $PROFILE_PATH/Offline |
| 676 | // Pages/archives. |
| 677 | if (!profile_path_.empty()) { |
| 678 | const base::FilePath offline_page_archives = |
| 679 | profile_path_.Append(chrome::kOfflinePageArchviesDirname); |
| 680 | if (offline_page_archives.IsParent(path)) |
| 681 | return true; |
| 682 | } |
| 683 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 684 | // Whitelist of other allowed directories. |
| 685 | static const char* const kLocalAccessWhiteList[] = { |
| 686 | "/sdcard", |
| 687 | "/mnt/sdcard", |
| 688 | }; |
| 689 | #endif |
| 690 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 691 | for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 692 | const base::FilePath white_listed_path(kLocalAccessWhiteList[i]); |
| 693 | // base::FilePath::operator== should probably handle trailing separators. |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 694 | if (white_listed_path == path.StripTrailingSeparators() || |
| 695 | white_listed_path.IsParent(path)) { |
| 696 | return true; |
| 697 | } |
| 698 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 699 | |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 700 | DVLOG(1) << "File access denied - " << path.value().c_str(); |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 701 | return false; |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 702 | #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 703 | } |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 704 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 705 | bool ChromeNetworkDelegate::OnCanEnablePrivacyMode( |
| 706 | const GURL& url, |
| 707 | const GURL& first_party_for_cookies) const { |
| 708 | // NULL during tests, or when we're running in the system context. |
[email protected] | 5173de8b | 2013-06-02 21:16:02 | [diff] [blame] | 709 | if (!cookie_settings_.get()) |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 710 | return false; |
| 711 | |
| 712 | bool reading_cookie_allowed = cookie_settings_->IsReadingCookieAllowed( |
| 713 | url, first_party_for_cookies); |
| 714 | bool setting_cookie_allowed = cookie_settings_->IsSettingCookieAllowed( |
| 715 | url, first_party_for_cookies); |
| 716 | bool privacy_mode = !(reading_cookie_allowed && setting_cookie_allowed); |
| 717 | return privacy_mode; |
| 718 | } |
| 719 | |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 720 | bool ChromeNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const { |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 721 | return experimental_web_platform_features_enabled_; |
| 722 | } |
| 723 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 724 | bool ChromeNetworkDelegate::OnAreStrictSecureCookiesEnabled() const { |
| 725 | const std::string enforce_strict_secure_group = |
| 726 | base::FieldTrialList::FindFullName("StrictSecureCookies"); |
| 727 | return experimental_web_platform_features_enabled_ || |
| 728 | base::StartsWith(enforce_strict_secure_group, "Enabled", |
| 729 | base::CompareCase::INSENSITIVE_ASCII); |
| 730 | } |
| 731 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 732 | bool ChromeNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader( |
| 733 | const net::URLRequest& request, |
| 734 | const GURL& target_url, |
| 735 | const GURL& referrer_url) const { |
jochen | 65227537 | 2015-01-16 11:35:27 | [diff] [blame] | 736 | ReportInvalidReferrerSend(target_url, referrer_url); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 737 | return true; |
| 738 | } |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 739 | |
| 740 | void ChromeNetworkDelegate::ReportDataUsageStats(net::URLRequest* request, |
| 741 | int64_t tx_bytes, |
| 742 | int64_t rx_bytes) { |
| 743 | if (!data_use_aggregator_) |
| 744 | return; |
| 745 | |
| 746 | if (is_data_usage_off_the_record_) { |
| 747 | data_use_aggregator_->ReportOffTheRecordDataUse(tx_bytes, rx_bytes); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | data_use_aggregator_->ReportDataUse(request, tx_bytes, rx_bytes); |
| 752 | } |