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