Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 7 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 8 #include "base/logging.h" | 11 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 10 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/string_split.h" | |
| 11 #include "chrome/browser/api/prefs/pref_member.h" | 15 #include "chrome/browser/api/prefs/pref_member.h" |
| 12 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/content_settings/cookie_settings.h" | 17 #include "chrome/browser/content_settings/cookie_settings.h" |
| 14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 18 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 15 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 19 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
| 16 #include "chrome/browser/extensions/api/proxy/proxy_api.h" | 20 #include "chrome/browser/extensions/api/proxy/proxy_api.h" |
| 17 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | 21 #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
| 18 #include "chrome/browser/extensions/event_router_forwarder.h" | 22 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 19 #include "chrome/browser/extensions/extension_info_map.h" | 23 #include "chrome/browser/extensions/extension_info_map.h" |
| 20 #include "chrome/browser/extensions/extension_process_manager.h" | 24 #include "chrome/browser/extensions/extension_process_manager.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 25 #include "chrome/browser/extensions/extension_system.h" |
| 26 #include "chrome/browser/google/google_util.h" | |
| 22 #include "chrome/browser/net/load_time_stats.h" | 27 #include "chrome/browser/net/load_time_stats.h" |
| 23 #include "chrome/browser/performance_monitor/performance_monitor.h" | 28 #include "chrome/browser/performance_monitor/performance_monitor.h" |
| 24 #include "chrome/browser/prefs/pref_service.h" | 29 #include "chrome/browser/prefs/pref_service.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 30 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "chrome/browser/task_manager/task_manager.h" | 31 #include "chrome/browser/task_manager/task_manager.h" |
| 27 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
| 29 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/render_view_host.h" | 35 #include "content/public/browser/render_view_host.h" |
| 31 #include "content/public/browser/resource_request_info.h" | 36 #include "content/public/browser/resource_request_info.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 switch (request->status().error()) { | 87 switch (request->status().error()) { |
| 83 case net::ERR_PROXY_AUTH_UNSUPPORTED: | 88 case net::ERR_PROXY_AUTH_UNSUPPORTED: |
| 84 case net::ERR_PROXY_CONNECTION_FAILED: | 89 case net::ERR_PROXY_CONNECTION_FAILED: |
| 85 case net::ERR_TUNNEL_CONNECTION_FAILED: | 90 case net::ERR_TUNNEL_CONNECTION_FAILED: |
| 86 extensions::ProxyEventRouter::GetInstance()->OnProxyError( | 91 extensions::ProxyEventRouter::GetInstance()->OnProxyError( |
| 87 event_router, profile, request->status().error()); | 92 event_router, profile, request->status().error()); |
| 88 } | 93 } |
| 89 } | 94 } |
| 90 } | 95 } |
| 91 | 96 |
| 97 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the | |
| 98 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters | |
| 99 // must be in key=value form. | |
| 100 bool HasSameParameterKey(const std::string& first_parameter, | |
| 101 const std::string& second_parameter) { | |
| 102 DCHECK(second_parameter.find("=") != std::string::npos); | |
| 103 // Prefix for "foo=bar" is "foo=". | |
| 104 std::string parameter_prefix = second_parameter.substr( | |
| 105 0, second_parameter.find("=") + 1); | |
| 106 return StartsWithASCII(first_parameter, parameter_prefix, false); | |
| 107 } | |
| 108 | |
| 109 // Examines the query string containing parameters and adds the necessary ones | |
| 110 // so that SafeSearch is active. |query| is the string to examine and the | |
| 111 // return value is the |query| string modified such that SafeSearch is active. | |
| 112 std::string AddSafeSearchParameters(const std::string& query) { | |
| 113 std::vector<std::string> new_parameters; | |
| 114 std::string safe_parameter = chrome::kSafeSearchSafeParameter; | |
| 115 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; | |
| 116 | |
| 117 std::vector<std::string> parameters; | |
| 118 base::SplitString(query, '&', ¶meters); | |
| 119 | |
| 120 std::vector<std::string>::iterator it; | |
| 121 for (it = parameters.begin(); it < parameters.end(); ++it) { | |
| 122 if (!HasSameParameterKey(*it, safe_parameter) && | |
| 123 !HasSameParameterKey(*it, ssui_parameter)) { | |
| 124 new_parameters.push_back(*it); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 new_parameters.push_back(safe_parameter); | |
| 129 new_parameters.push_back(ssui_parameter); | |
| 130 return JoinString(new_parameters, '&'); | |
| 131 } | |
| 132 | |
| 133 // If |request| is a request to Google Web Search the function | |
| 134 // enforces that the SafeSearch query parameters are set to active. | |
| 135 // Sets the query part of |new_url| with the new value of the parameters. | |
| 136 void ForceGoogleSafeSearch(net::URLRequest* request, | |
| 137 GURL* new_url) { | |
| 138 if (google_util::SupportsSafeSearch(request->url().query())) { | |
|
Bernhard Bauer
2012/11/02 13:54:34
Early-return if the URL doesn't support SafeSearch
Sergiu
2012/11/02 20:38:43
Do you mean
if (!SupportsSafeSearch(..))
return;
| |
| 139 std::string query = request->url().query(); | |
| 140 std::string new_query = AddSafeSearchParameters(query); | |
| 141 if (query == new_query) | |
| 142 return; | |
| 143 | |
| 144 GURL::Replacements replacements; | |
| 145 replacements.SetQueryStr(new_query); | |
| 146 *new_url = request->url().ReplaceComponents(replacements); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 // Gets called when the extensions finish work on the URL. If the extensions | |
| 151 // did not do a redirect (so |new_url| is empty) then we enforce the | |
| 152 // SafeSearch parameters. Otherwise we will get called again after the | |
| 153 // redirect and we enforce SafeSearch then. | |
| 154 void ForceGoogleSafeSearchCallbackWrapper( | |
| 155 const net::CompletionCallback& callback, | |
| 156 net::URLRequest* request, | |
| 157 GURL* new_url, | |
| 158 int rv) { | |
| 159 if (rv == net::OK && new_url->is_empty()) | |
| 160 ForceGoogleSafeSearch(request, new_url); | |
| 161 callback.Run(rv); | |
| 162 } | |
| 163 | |
| 92 enum RequestStatus { REQUEST_STARTED, REQUEST_DONE }; | 164 enum RequestStatus { REQUEST_STARTED, REQUEST_DONE }; |
| 93 | 165 |
| 94 // Notifies the ExtensionProcessManager that a request has started or stopped | 166 // Notifies the ExtensionProcessManager that a request has started or stopped |
| 95 // for a particular RenderView. | 167 // for a particular RenderView. |
| 96 void NotifyEPMRequestStatus(RequestStatus status, | 168 void NotifyEPMRequestStatus(RequestStatus status, |
| 97 void* profile_id, | 169 void* profile_id, |
| 98 int process_id, | 170 int process_id, |
| 99 int render_view_id) { | 171 int render_view_id) { |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 101 Profile* profile = reinterpret_cast<Profile*>(profile_id); | 173 Profile* profile = reinterpret_cast<Profile*>(profile_id); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 | 261 |
| 190 ChromeNetworkDelegate::ChromeNetworkDelegate( | 262 ChromeNetworkDelegate::ChromeNetworkDelegate( |
| 191 extensions::EventRouterForwarder* event_router, | 263 extensions::EventRouterForwarder* event_router, |
| 192 ExtensionInfoMap* extension_info_map, | 264 ExtensionInfoMap* extension_info_map, |
| 193 const policy::URLBlacklistManager* url_blacklist_manager, | 265 const policy::URLBlacklistManager* url_blacklist_manager, |
| 194 const ManagedModeURLFilter* managed_mode_url_filter, | 266 const ManagedModeURLFilter* managed_mode_url_filter, |
| 195 void* profile, | 267 void* profile, |
| 196 CookieSettings* cookie_settings, | 268 CookieSettings* cookie_settings, |
| 197 BooleanPrefMember* enable_referrers, | 269 BooleanPrefMember* enable_referrers, |
| 198 BooleanPrefMember* enable_do_not_track, | 270 BooleanPrefMember* enable_do_not_track, |
| 271 BooleanPrefMember* force_google_safe_search, | |
| 199 chrome_browser_net::LoadTimeStats* load_time_stats) | 272 chrome_browser_net::LoadTimeStats* load_time_stats) |
| 200 : event_router_(event_router), | 273 : event_router_(event_router), |
| 201 profile_(profile), | 274 profile_(profile), |
| 202 cookie_settings_(cookie_settings), | 275 cookie_settings_(cookie_settings), |
| 203 extension_info_map_(extension_info_map), | 276 extension_info_map_(extension_info_map), |
| 204 enable_referrers_(enable_referrers), | 277 enable_referrers_(enable_referrers), |
| 205 enable_do_not_track_(enable_do_not_track), | 278 enable_do_not_track_(enable_do_not_track), |
| 279 force_google_safe_search_(force_google_safe_search), | |
| 206 url_blacklist_manager_(url_blacklist_manager), | 280 url_blacklist_manager_(url_blacklist_manager), |
| 207 managed_mode_url_filter_(managed_mode_url_filter), | 281 managed_mode_url_filter_(managed_mode_url_filter), |
| 208 load_time_stats_(load_time_stats), | 282 load_time_stats_(load_time_stats), |
| 209 received_content_length_(0), | 283 received_content_length_(0), |
| 210 original_content_length_(0) { | 284 original_content_length_(0) { |
| 211 DCHECK(event_router); | 285 DCHECK(event_router); |
| 212 DCHECK(enable_referrers); | 286 DCHECK(enable_referrers); |
| 213 DCHECK(!profile || cookie_settings); | 287 DCHECK(!profile || cookie_settings); |
| 214 } | 288 } |
| 215 | 289 |
| 216 ChromeNetworkDelegate::~ChromeNetworkDelegate() {} | 290 ChromeNetworkDelegate::~ChromeNetworkDelegate() {} |
| 217 | 291 |
| 218 // static | 292 // static |
| 219 void ChromeNetworkDelegate::NeverThrottleRequests() { | 293 void ChromeNetworkDelegate::NeverThrottleRequests() { |
| 220 g_never_throttle_requests_ = true; | 294 g_never_throttle_requests_ = true; |
| 221 } | 295 } |
| 222 | 296 |
| 223 // static | 297 // static |
| 224 void ChromeNetworkDelegate::InitializePrefsOnUIThread( | 298 void ChromeNetworkDelegate::InitializePrefsOnUIThread( |
| 225 BooleanPrefMember* enable_referrers, | 299 BooleanPrefMember* enable_referrers, |
| 226 BooleanPrefMember* enable_do_not_track, | 300 BooleanPrefMember* enable_do_not_track, |
| 301 BooleanPrefMember* force_google_safe_search, | |
| 227 PrefService* pref_service) { | 302 PrefService* pref_service) { |
| 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 229 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL); | 304 enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL); |
| 230 enable_referrers->MoveToThread( | 305 enable_referrers->MoveToThread( |
| 231 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 306 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 232 if (enable_do_not_track) { | 307 if (enable_do_not_track) { |
| 233 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service, NULL); | 308 enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service, NULL); |
| 234 enable_do_not_track->MoveToThread( | 309 enable_do_not_track->MoveToThread( |
| 235 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 310 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
| 236 } | 311 } |
| 312 if (force_google_safe_search) { | |
| 313 force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service, NULL); | |
| 314 force_google_safe_search->MoveToThread( | |
| 315 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
| 316 } | |
| 237 } | 317 } |
| 238 | 318 |
| 239 // static | 319 // static |
| 240 void ChromeNetworkDelegate::AllowAccessToAllFiles() { | 320 void ChromeNetworkDelegate::AllowAccessToAllFiles() { |
| 241 g_allow_file_access_ = true; | 321 g_allow_file_access_ = true; |
| 242 } | 322 } |
| 243 | 323 |
| 244 // static | 324 // static |
| 245 Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() { | 325 Value* ChromeNetworkDelegate::HistoricNetworkStatsInfoToValue() { |
| 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 247 PrefService* prefs = g_browser_process->local_state(); | 327 PrefService* prefs = g_browser_process->local_state(); |
| 248 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength); | 328 int64 total_received = prefs->GetInt64(prefs::kHttpReceivedContentLength); |
| 249 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength); | 329 int64 total_original = prefs->GetInt64(prefs::kHttpOriginalContentLength); |
| 250 | 330 |
| 251 DictionaryValue* dict = new DictionaryValue(); | 331 DictionaryValue* dict = new DictionaryValue(); |
| 252 dict->SetInteger("historic_received_content_length", total_received); | 332 dict->SetInteger("historic_received_content_length", total_received); |
| 253 dict->SetInteger("historic_original_content_length", total_original); | 333 dict->SetInteger("historic_original_content_length", total_original); |
| 254 return dict; | 334 return dict; |
| 255 } | 335 } |
| 256 | 336 |
| 257 Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const { | 337 Value* ChromeNetworkDelegate::SessionNetworkStatsInfoToValue() const { |
| 258 DictionaryValue* dict = new DictionaryValue(); | 338 DictionaryValue* dict = new DictionaryValue(); |
| 259 dict->SetInteger("session_received_content_length", received_content_length_); | 339 dict->SetInteger("session_received_content_length", received_content_length_); |
| 260 dict->SetInteger("session_original_content_length", original_content_length_); | 340 dict->SetInteger("session_original_content_length", original_content_length_); |
| 261 return dict; | 341 return dict; |
| 262 } | 342 } |
| 263 | 343 |
| 344 // static | |
| 345 void ChromeNetworkDelegate::ForceGoogleSafeSearch(net::URLRequest* request, | |
|
Bernhard Bauer
2012/11/02 13:54:34
Wait, we already have this method above ;-)
Sergiu
2012/11/02 20:38:43
/facepalm, rebasing fail
| |
| 346 GURL* new_url) { | |
| 347 // Force SafeSearch. | |
| 348 const bool is_search_url = google_util::IsGoogleSearchUrl( | |
| 349 request->url().spec()); | |
| 350 const bool is_homepage_url = google_util::IsGoogleHomePageUrl( | |
| 351 request->url().spec()); | |
| 352 if (is_search_url || is_homepage_url) { | |
| 353 std::string query = request->url().query(); | |
| 354 std::string new_query = AddSafeSearchParameters(query); | |
| 355 if (query == new_query) | |
| 356 return; | |
| 357 | |
| 358 GURL::Replacements replacements; | |
| 359 replacements.SetQueryStr(new_query); | |
| 360 *new_url = request->url().ReplaceComponents(replacements); | |
| 361 } | |
| 362 } | |
| 363 | |
| 264 int ChromeNetworkDelegate::OnBeforeURLRequest( | 364 int ChromeNetworkDelegate::OnBeforeURLRequest( |
| 265 net::URLRequest* request, | 365 net::URLRequest* request, |
| 266 const net::CompletionCallback& callback, | 366 const net::CompletionCallback& callback, |
| 267 GURL* new_url) { | 367 GURL* new_url) { |
| 268 #if defined(ENABLE_CONFIGURATION_POLICY) | 368 #if defined(ENABLE_CONFIGURATION_POLICY) |
| 269 // TODO(joaodasilva): This prevents extensions from seeing URLs that are | 369 // TODO(joaodasilva): This prevents extensions from seeing URLs that are |
| 270 // blocked. However, an extension might redirect the request to another URL, | 370 // blocked. However, an extension might redirect the request to another URL, |
| 271 // which is not blocked. | 371 // which is not blocked. |
| 272 if (url_blacklist_manager_ && | 372 if (url_blacklist_manager_ && |
| 273 url_blacklist_manager_->IsURLBlocked(request->url())) { | 373 url_blacklist_manager_->IsURLBlocked(request->url())) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 287 return net::ERR_NETWORK_ACCESS_DENIED; | 387 return net::ERR_NETWORK_ACCESS_DENIED; |
| 288 } | 388 } |
| 289 #endif | 389 #endif |
| 290 | 390 |
| 291 ForwardRequestStatus(REQUEST_STARTED, request, profile_); | 391 ForwardRequestStatus(REQUEST_STARTED, request, profile_); |
| 292 | 392 |
| 293 if (!enable_referrers_->GetValue()) | 393 if (!enable_referrers_->GetValue()) |
| 294 request->set_referrer(std::string()); | 394 request->set_referrer(std::string()); |
| 295 if (enable_do_not_track_ && enable_do_not_track_->GetValue()) | 395 if (enable_do_not_track_ && enable_do_not_track_->GetValue()) |
| 296 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */); | 396 request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */); |
| 297 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest( | 397 |
| 298 profile_, extension_info_map_.get(), request, callback, new_url); | 398 bool force_safe_search = force_google_safe_search_ && |
| 399 force_google_safe_search_->GetValue(); | |
| 400 | |
| 401 net::CompletionCallback wrapped_callback = callback; | |
| 402 if (force_safe_search) { | |
| 403 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, | |
| 404 callback, | |
| 405 base::Unretained(request), | |
| 406 base::Unretained(new_url)); | |
| 407 } | |
| 408 | |
| 409 int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest( | |
| 410 profile_, extension_info_map_.get(), request, wrapped_callback, | |
| 411 new_url); | |
| 412 | |
| 413 if (force_safe_search && rv == net::OK && new_url->is_empty()) | |
| 414 ForceGoogleSafeSearch(request, new_url); | |
| 415 | |
| 416 return rv; | |
| 299 } | 417 } |
| 300 | 418 |
| 301 int ChromeNetworkDelegate::OnBeforeSendHeaders( | 419 int ChromeNetworkDelegate::OnBeforeSendHeaders( |
| 302 net::URLRequest* request, | 420 net::URLRequest* request, |
| 303 const net::CompletionCallback& callback, | 421 const net::CompletionCallback& callback, |
| 304 net::HttpRequestHeaders* headers) { | 422 net::HttpRequestHeaders* headers) { |
| 305 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders( | 423 return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders( |
| 306 profile_, extension_info_map_.get(), request, callback, headers); | 424 profile_, extension_info_map_.get(), request, callback, headers); |
| 307 } | 425 } |
| 308 | 426 |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 | 681 |
| 564 void ChromeNetworkDelegate::AccumulateContentLength( | 682 void ChromeNetworkDelegate::AccumulateContentLength( |
| 565 int64 received_content_length, int64 original_content_length) { | 683 int64 received_content_length, int64 original_content_length) { |
| 566 DCHECK_GE(received_content_length, 0); | 684 DCHECK_GE(received_content_length, 0); |
| 567 DCHECK_GE(original_content_length, 0); | 685 DCHECK_GE(original_content_length, 0); |
| 568 StoreAccumulatedContentLength(received_content_length, | 686 StoreAccumulatedContentLength(received_content_length, |
| 569 original_content_length); | 687 original_content_length); |
| 570 received_content_length_ += received_content_length; | 688 received_content_length_ += received_content_length; |
| 571 original_content_length_ += original_content_length; | 689 original_content_length_ += original_content_length; |
| 572 } | 690 } |
| OLD | NEW |