[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 | |
| 7 | #include "base/logging.h" |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 8 | #include "base/base_paths.h" |
| 9 | #include "base/path_service.h" |
[email protected] | c5aa826 | 2012-08-21 11:19:54 | [diff] [blame] | 10 | #include "chrome/browser/api/prefs/pref_member.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 12 | #include "chrome/browser/content_settings/cookie_settings.h" |
| 13 | #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 14 | #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 15 | #include "chrome/browser/extensions/api/proxy/proxy_api.h" |
[email protected] | 2b33dcd0 | 2012-03-18 01:34:16 | [diff] [blame] | 16 | #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 17 | #include "chrome/browser/extensions/event_router_forwarder.h" |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 18 | #include "chrome/browser/extensions/extension_info_map.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 20 | #include "chrome/browser/net/load_time_stats.h" |
[email protected] | 6f4b4b4 | 2012-08-27 21:59:07 | [diff] [blame] | 21 | #include "chrome/browser/performance_monitor/performance_monitor.h" |
[email protected] | fb8fdf1 | 2012-08-21 16:28:20 | [diff] [blame] | 22 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 23 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 24 | #include "chrome/browser/task_manager/task_manager.h" |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 25 | #include "chrome/common/pref_names.h" |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 26 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 28 | #include "content/public/browser/render_view_host.h" |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 29 | #include "content/public/browser/resource_request_info.h" |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 30 | #include "net/base/host_port_pair.h" |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 31 | #include "net/base/net_errors.h" |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 32 | #include "net/base/net_log.h" |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 33 | #include "net/cookies/canonical_cookie.h" |
| 34 | #include "net/cookies/cookie_options.h" |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 35 | #include "net/http/http_request_headers.h" |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 36 | #include "net/http/http_response_headers.h" |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 37 | #include "net/socket_stream/socket_stream.h" |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 38 | #include "net/url_request/url_request.h" |
| 39 | |
[email protected] | e9c41d2 | 2012-08-17 00:08:15 | [diff] [blame] | 40 | #if !defined(OS_ANDROID) |
| 41 | #include "chrome/browser/managed_mode_url_filter.h" |
| 42 | #endif |
| 43 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 44 | #if defined(OS_CHROMEOS) |
| 45 | #include "base/chromeos/chromeos_version.h" |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 46 | #include "base/command_line.h" |
| 47 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 48 | #endif |
| 49 | |
[email protected] | 3e598ff1 | 2011-09-06 11:22:34 | [diff] [blame] | 50 | #if defined(ENABLE_CONFIGURATION_POLICY) |
| 51 | #include "chrome/browser/policy/url_blacklist_manager.h" |
| 52 | #endif |
| 53 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 54 | using content::BrowserThread; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 55 | using content::RenderViewHost; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 56 | using content::ResourceRequestInfo; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 57 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 58 | // By default we don't allow access to all file:// urls on ChromeOS and |
| 59 | // Android. |
| 60 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 61 | bool ChromeNetworkDelegate::g_allow_file_access_ = false; |
| 62 | #else |
| 63 | bool ChromeNetworkDelegate::g_allow_file_access_ = true; |
| 64 | #endif |
| 65 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 66 | // This remains false unless the --disable-extensions-http-throttling |
| 67 | // flag is passed to the browser. |
| 68 | bool ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
| 69 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 70 | namespace { |
| 71 | |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 72 | const char kDNTHeader[] = "DNT"; |
| 73 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 74 | // If the |request| failed due to problems with a proxy, forward the error to |
| 75 | // the proxy extension API. |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 76 | void ForwardProxyErrors(net::URLRequest* request, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 77 | extensions::EventRouterForwarder* event_router, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 78 | void* profile) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 79 | if (request->status().status() == net::URLRequestStatus::FAILED) { |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 80 | switch (request->status().error()) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 81 | case net::ERR_PROXY_AUTH_UNSUPPORTED: |
| 82 | case net::ERR_PROXY_CONNECTION_FAILED: |
| 83 | case net::ERR_TUNNEL_CONNECTION_FAILED: |
[email protected] | c454fe67 | 2012-03-12 21:18:01 | [diff] [blame] | 84 | extensions::ProxyEventRouter::GetInstance()->OnProxyError( |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 85 | event_router, profile, request->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 90 | enum RequestStatus { REQUEST_STARTED, REQUEST_DONE }; |
| 91 | |
| 92 | // Notifies the ExtensionProcessManager that a request has started or stopped |
| 93 | // for a particular RenderView. |
| 94 | void NotifyEPMRequestStatus(RequestStatus status, |
| 95 | void* profile_id, |
| 96 | int process_id, |
| 97 | int render_view_id) { |
| 98 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 | Profile* profile = reinterpret_cast<Profile*>(profile_id); |
| 100 | if (!g_browser_process->profile_manager()->IsValidProfile(profile)) |
| 101 | return; |
| 102 | |
[email protected] | e5775a5 | 2012-03-17 04:59:57 | [diff] [blame] | 103 | ExtensionProcessManager* extension_process_manager = |
| 104 | profile->GetExtensionProcessManager(); |
| 105 | // This may be NULL in unit tests. |
| 106 | if (!extension_process_manager) |
| 107 | return; |
| 108 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 109 | // Will be NULL if the request was not issued on behalf of a renderer (e.g. a |
| 110 | // system-level request). |
[email protected] | d3e898e | 2012-03-14 03:45:08 | [diff] [blame] | 111 | RenderViewHost* render_view_host = |
| 112 | RenderViewHost::FromID(process_id, render_view_id); |
[email protected] | e5775a5 | 2012-03-17 04:59:57 | [diff] [blame] | 113 | if (render_view_host) { |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 114 | if (status == REQUEST_STARTED) { |
[email protected] | d3e898e | 2012-03-14 03:45:08 | [diff] [blame] | 115 | extension_process_manager->OnNetworkRequestStarted(render_view_host); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 116 | } else if (status == REQUEST_DONE) { |
[email protected] | d3e898e | 2012-03-14 03:45:08 | [diff] [blame] | 117 | extension_process_manager->OnNetworkRequestDone(render_view_host); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 118 | } else { |
| 119 | NOTREACHED(); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void ForwardRequestStatus( |
| 125 | RequestStatus status, net::URLRequest* request, void* profile_id) { |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 126 | const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 127 | if (!info) |
| 128 | return; |
| 129 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 130 | int process_id, render_view_id; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 131 | if (info->GetAssociatedRenderView(&process_id, &render_view_id)) { |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 132 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 133 | base::Bind(&NotifyEPMRequestStatus, |
| 134 | status, profile_id, process_id, render_view_id)); |
| 135 | } |
| 136 | } |
| 137 | |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 138 | } // namespace |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 139 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 140 | ChromeNetworkDelegate::ChromeNetworkDelegate( |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 141 | extensions::EventRouterForwarder* event_router, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 142 | ExtensionInfoMap* extension_info_map, |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 143 | const policy::URLBlacklistManager* url_blacklist_manager, |
[email protected] | e9c41d2 | 2012-08-17 00:08:15 | [diff] [blame] | 144 | const ManagedModeURLFilter* managed_mode_url_filter, |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 145 | void* profile, |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 146 | CookieSettings* cookie_settings, |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 147 | BooleanPrefMember* enable_referrers, |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 148 | BooleanPrefMember* enable_do_not_track, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 149 | chrome_browser_net::LoadTimeStats* load_time_stats) |
[email protected] | 3ce0241 | 2011-03-01 12:01:15 | [diff] [blame] | 150 | : event_router_(event_router), |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 151 | profile_(profile), |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 152 | cookie_settings_(cookie_settings), |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 153 | extension_info_map_(extension_info_map), |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 154 | enable_referrers_(enable_referrers), |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 155 | enable_do_not_track_(enable_do_not_track), |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 156 | url_blacklist_manager_(url_blacklist_manager), |
[email protected] | e9c41d2 | 2012-08-17 00:08:15 | [diff] [blame] | 157 | managed_mode_url_filter_(managed_mode_url_filter), |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 158 | load_time_stats_(load_time_stats) { |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 159 | DCHECK(event_router); |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 160 | DCHECK(enable_referrers); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 161 | DCHECK(!profile || cookie_settings); |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 164 | ChromeNetworkDelegate::~ChromeNetworkDelegate() {} |
| 165 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 166 | // static |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 167 | void ChromeNetworkDelegate::NeverThrottleRequests() { |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 168 | g_never_throttle_requests_ = true; |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 171 | // static |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 172 | void ChromeNetworkDelegate::InitializePrefsOnUIThread( |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 173 | BooleanPrefMember* enable_referrers, |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 174 | BooleanPrefMember* enable_do_not_track, |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 175 | PrefService* pref_service) { |
| 176 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 177 | enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL); |
| 178 | enable_referrers->MoveToThread(BrowserThread::IO); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 179 | if (enable_do_not_track) { |
| 180 | enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service, NULL); |
| 181 | enable_do_not_track->MoveToThread(BrowserThread::IO); |
| 182 | } |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 185 | // static |
| 186 | void ChromeNetworkDelegate::AllowAccessToAllFiles() { |
| 187 | g_allow_file_access_ = true; |
| 188 | } |
| 189 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 190 | int ChromeNetworkDelegate::OnBeforeURLRequest( |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 191 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 192 | const net::CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 193 | GURL* new_url) { |
[email protected] | 3e598ff1 | 2011-09-06 11:22:34 | [diff] [blame] | 194 | #if defined(ENABLE_CONFIGURATION_POLICY) |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 195 | // TODO(joaodasilva): This prevents extensions from seeing URLs that are |
| 196 | // blocked. However, an extension might redirect the request to another URL, |
| 197 | // which is not blocked. |
| 198 | if (url_blacklist_manager_ && |
| 199 | url_blacklist_manager_->IsURLBlocked(request->url())) { |
| 200 | // URL access blocked by policy. |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 201 | request->net_log().AddEvent( |
[email protected] | 2fa0891 | 2012-06-14 20:56:26 | [diff] [blame] | 202 | net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, |
| 203 | net::NetLog::StringCallback("url", |
| 204 | &request->url().possibly_invalid_spec())); |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 205 | return net::ERR_NETWORK_ACCESS_DENIED; |
| 206 | } |
[email protected] | 3e598ff1 | 2011-09-06 11:22:34 | [diff] [blame] | 207 | #endif |
[email protected] | 6a5f77c3 | 2011-09-04 19:19:59 | [diff] [blame] | 208 | |
[email protected] | e9c41d2 | 2012-08-17 00:08:15 | [diff] [blame] | 209 | #if !defined(OS_ANDROID) |
| 210 | if (managed_mode_url_filter_ && |
| 211 | !managed_mode_url_filter_->IsURLWhitelisted(request->url())) { |
| 212 | // Block for now. |
| 213 | return net::ERR_NETWORK_ACCESS_DENIED; |
| 214 | } |
| 215 | #endif |
| 216 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 217 | ForwardRequestStatus(REQUEST_STARTED, request, profile_); |
| 218 | |
[email protected] | 0a8db0d | 2011-04-13 15:15:40 | [diff] [blame] | 219 | if (!enable_referrers_->GetValue()) |
| 220 | request->set_referrer(std::string()); |
[email protected] | 9d8cfb68 | 2012-09-13 16:48:04 | [diff] [blame] | 221 | if (enable_do_not_track_ && enable_do_not_track_->GetValue()) |
| 222 | request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */); |
[email protected] | 05cc4e7 | 2011-03-08 21:29:48 | [diff] [blame] | 223 | return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 224 | profile_, extension_info_map_.get(), request, callback, new_url); |
[email protected] | d05ef99c | 2011-02-01 21:38:16 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 227 | int ChromeNetworkDelegate::OnBeforeSendHeaders( |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 228 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 229 | const net::CompletionCallback& callback, |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 230 | net::HttpRequestHeaders* headers) { |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 231 | return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 232 | profile_, extension_info_map_.get(), request, callback, headers); |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 233 | } |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 234 | |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 235 | void ChromeNetworkDelegate::OnSendHeaders( |
| 236 | net::URLRequest* request, |
[email protected] | 783573b | 2011-05-13 11:05:15 | [diff] [blame] | 237 | const net::HttpRequestHeaders& headers) { |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 238 | ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders( |
| 239 | profile_, extension_info_map_.get(), request, headers); |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 242 | int ChromeNetworkDelegate::OnHeadersReceived( |
| 243 | net::URLRequest* request, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 244 | const net::CompletionCallback& callback, |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 245 | net::HttpResponseHeaders* original_response_headers, |
| 246 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { |
| 247 | return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived( |
| 248 | profile_, extension_info_map_.get(), request, callback, |
| 249 | original_response_headers, override_response_headers); |
| 250 | } |
| 251 | |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 252 | void ChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest* request, |
| 253 | const GURL& new_location) { |
| 254 | ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 255 | profile_, extension_info_map_.get(), request, new_location); |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 259 | void ChromeNetworkDelegate::OnResponseStarted(net::URLRequest* request) { |
[email protected] | 62fecae | 2011-04-21 11:08:24 | [diff] [blame] | 260 | ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 261 | profile_, extension_info_map_.get(), request); |
| 262 | ForwardProxyErrors(request, event_router_.get(), profile_); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 265 | void ChromeNetworkDelegate::OnRawBytesRead(const net::URLRequest& request, |
| 266 | int bytes_read) { |
[email protected] | 6f4b4b4 | 2012-08-27 21:59:07 | [diff] [blame] | 267 | performance_monitor::PerformanceMonitor::GetInstance()->BytesReadOnIOThread( |
| 268 | request, bytes_read); |
| 269 | |
[email protected] | 44879ed | 2012-04-06 01:11:02 | [diff] [blame] | 270 | #if defined(ENABLE_TASK_MANAGER) |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 271 | TaskManager::GetInstance()->model()->NotifyBytesRead(request, bytes_read); |
[email protected] | 44879ed | 2012-04-06 01:11:02 | [diff] [blame] | 272 | #endif // defined(ENABLE_TASK_MANAGER) |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 275 | void ChromeNetworkDelegate::OnCompleted(net::URLRequest* request, |
| 276 | bool started) { |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 277 | if (request->status().status() == net::URLRequestStatus::SUCCESS) { |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 278 | bool is_redirect = request->response_headers() && |
| 279 | net::HttpResponseHeaders::IsRedirectResponseCode( |
| 280 | request->response_headers()->response_code()); |
| 281 | if (!is_redirect) { |
| 282 | ExtensionWebRequestEventRouter::GetInstance()->OnCompleted( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 283 | profile_, extension_info_map_.get(), request); |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 284 | } |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 285 | } else if (request->status().status() == net::URLRequestStatus::FAILED || |
| 286 | request->status().status() == net::URLRequestStatus::CANCELED) { |
[email protected] | 05b6ab4 | 2011-04-23 13:46:04 | [diff] [blame] | 287 | ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 288 | profile_, extension_info_map_.get(), request, started); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 289 | } else { |
| 290 | NOTREACHED(); |
[email protected] | 4894438 | 2011-04-23 13:28:16 | [diff] [blame] | 291 | } |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 292 | ForwardProxyErrors(request, event_router_.get(), profile_); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 293 | |
| 294 | ForwardRequestStatus(REQUEST_DONE, request, profile_); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 295 | } |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 296 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 297 | void ChromeNetworkDelegate::OnURLRequestDestroyed(net::URLRequest* request) { |
| 298 | ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 299 | profile_, request); |
[email protected] | 16d5c72b | 2012-09-14 20:42:37 | [diff] [blame] | 300 | if (load_time_stats_) |
| 301 | load_time_stats_->OnURLRequestDestroyed(*request); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 304 | void ChromeNetworkDelegate::OnPACScriptError(int line_number, |
| 305 | const string16& error) { |
[email protected] | c454fe67 | 2012-03-12 21:18:01 | [diff] [blame] | 306 | extensions::ProxyEventRouter::GetInstance()->OnPACScriptError( |
[email protected] | 67351452 | 2011-07-13 18:17:18 | [diff] [blame] | 307 | event_router_.get(), profile_, line_number, error); |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 308 | } |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 309 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 310 | net::NetworkDelegate::AuthRequiredResponse |
| 311 | ChromeNetworkDelegate::OnAuthRequired( |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 312 | net::URLRequest* request, |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 313 | const net::AuthChallengeInfo& auth_info, |
| 314 | const AuthCallback& callback, |
| 315 | net::AuthCredentials* credentials) { |
[email protected] | 90449ab | 2011-10-11 15:36:45 | [diff] [blame] | 316 | return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired( |
| 317 | profile_, extension_info_map_.get(), request, auth_info, |
| 318 | callback, credentials); |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 319 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 320 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 321 | bool ChromeNetworkDelegate::OnCanGetCookies( |
| 322 | const net::URLRequest& request, |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 323 | const net::CookieList& cookie_list) { |
| 324 | // NULL during tests, or when we're running in the system context. |
| 325 | if (!cookie_settings_) |
| 326 | return true; |
| 327 | |
| 328 | bool allow = cookie_settings_->IsReadingCookieAllowed( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 329 | request.url(), request.first_party_for_cookies()); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 330 | |
| 331 | int render_process_id = -1; |
| 332 | int render_view_id = -1; |
| 333 | if (content::ResourceRequestInfo::GetRenderViewForRequest( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 334 | &request, &render_process_id, &render_view_id)) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 335 | BrowserThread::PostTask( |
| 336 | BrowserThread::UI, FROM_HERE, |
| 337 | base::Bind(&TabSpecificContentSettings::CookiesRead, |
| 338 | render_process_id, render_view_id, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 339 | request.url(), request.first_party_for_cookies(), |
[email protected] | fd473d1 | 2012-04-05 11:38:43 | [diff] [blame] | 340 | cookie_list, !allow)); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return allow; |
| 344 | } |
| 345 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 346 | bool ChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest& request, |
| 347 | const std::string& cookie_line, |
| 348 | net::CookieOptions* options) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 349 | // NULL during tests, or when we're running in the system context. |
| 350 | if (!cookie_settings_) |
| 351 | return true; |
| 352 | |
| 353 | bool allow = cookie_settings_->IsSettingCookieAllowed( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 354 | request.url(), request.first_party_for_cookies()); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 355 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 356 | int render_process_id = -1; |
| 357 | int render_view_id = -1; |
| 358 | if (content::ResourceRequestInfo::GetRenderViewForRequest( |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 359 | &request, &render_process_id, &render_view_id)) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 360 | BrowserThread::PostTask( |
| 361 | BrowserThread::UI, FROM_HERE, |
| 362 | base::Bind(&TabSpecificContentSettings::CookieChanged, |
| 363 | render_process_id, render_view_id, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 364 | request.url(), request.first_party_for_cookies(), |
[email protected] | fd473d1 | 2012-04-05 11:38:43 | [diff] [blame] | 365 | cookie_line, *options, !allow)); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return allow; |
| 369 | } |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 370 | |
| 371 | bool ChromeNetworkDelegate::OnCanAccessFile(const net::URLRequest& request, |
| 372 | const FilePath& path) const { |
| 373 | if (g_allow_file_access_) |
| 374 | return true; |
| 375 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 376 | #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
| 377 | return true; |
| 378 | #else |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 379 | #if defined(OS_CHROMEOS) |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 380 | // If we're running Chrome for ChromeOS on Linux, we want to allow file |
| 381 | // access. |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 382 | if (!base::chromeos::IsRunningOnChromeOS() || |
| 383 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) { |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 384 | return true; |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 385 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 386 | |
| 387 | // Use a whitelist to only allow access to files residing in the list of |
| 388 | // directories below. |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 389 | static const char* const kLocalAccessWhiteList[] = { |
| 390 | "/home/chronos/user/Downloads", |
| 391 | "/home/chronos/user/log", |
| 392 | "/media", |
| 393 | "/opt/oem", |
| 394 | "/usr/share/chromeos-assets", |
| 395 | "/tmp", |
| 396 | "/var/log", |
| 397 | }; |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 398 | #elif defined(OS_ANDROID) |
| 399 | // Access to files in external storage is allowed. |
| 400 | FilePath external_storage_path; |
| 401 | PathService::Get(base::DIR_ANDROID_EXTERNAL_STORAGE, &external_storage_path); |
| 402 | if (external_storage_path.IsParent(path)) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 403 | return true; |
| 404 | |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 405 | // Whitelist of other allowed directories. |
| 406 | static const char* const kLocalAccessWhiteList[] = { |
| 407 | "/sdcard", |
| 408 | "/mnt/sdcard", |
| 409 | }; |
| 410 | #endif |
| 411 | |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 412 | for (size_t i = 0; i < arraysize(kLocalAccessWhiteList); ++i) { |
| 413 | const FilePath white_listed_path(kLocalAccessWhiteList[i]); |
| 414 | // FilePath::operator== should probably handle trailing separators. |
| 415 | if (white_listed_path == path.StripTrailingSeparators() || |
| 416 | white_listed_path.IsParent(path)) { |
| 417 | return true; |
| 418 | } |
| 419 | } |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 420 | |
[email protected] | 28853848 | 2012-09-06 21:09:35 | [diff] [blame] | 421 | DVLOG(1) << "File access denied - " << path.value().c_str(); |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 422 | return false; |
[email protected] | d8e4f13 | 2012-09-06 04:28:05 | [diff] [blame] | 423 | #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 424 | } |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 425 | |
| 426 | bool ChromeNetworkDelegate::OnCanThrottleRequest( |
| 427 | const net::URLRequest& request) const { |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 428 | if (g_never_throttle_requests_) { |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | |
[email protected] | c4a7df8 | 2012-08-09 22:48:46 | [diff] [blame] | 432 | return request.first_party_for_cookies().scheme() == |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 433 | chrome::kExtensionScheme; |
| 434 | } |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 435 | |
| 436 | int ChromeNetworkDelegate::OnBeforeSocketStreamConnect( |
| 437 | net::SocketStream* socket, |
| 438 | const net::CompletionCallback& callback) { |
| 439 | #if defined(ENABLE_CONFIGURATION_POLICY) |
| 440 | if (url_blacklist_manager_ && |
| 441 | url_blacklist_manager_->IsURLBlocked(socket->url())) { |
| 442 | // URL access blocked by policy. |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 443 | socket->net_log()->AddEvent( |
[email protected] | 2fa0891 | 2012-06-14 20:56:26 | [diff] [blame] | 444 | net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST, |
| 445 | net::NetLog::StringCallback("url", |
| 446 | &socket->url().possibly_invalid_spec())); |
[email protected] | aa28181e | 2012-06-13 00:53:58 | [diff] [blame] | 447 | return net::ERR_NETWORK_ACCESS_DENIED; |
| 448 | } |
| 449 | #endif |
| 450 | return net::OK; |
| 451 | } |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 452 | |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 453 | void ChromeNetworkDelegate::OnRequestWaitStateChange( |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 454 | const net::URLRequest& request, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 455 | RequestWaitState state) { |
| 456 | if (load_time_stats_) |
| 457 | load_time_stats_->OnRequestWaitStateChange(request, state); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 458 | } |