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