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