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/renderer_host/chrome_resource_dispatcher_host_delegate.
h" | 5 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.
h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_metrics_helper.h" |
12 #include "chrome/browser/content_settings/host_content_settings_map.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map.h" |
13 #include "chrome/browser/download/download_request_limiter.h" | 14 #include "chrome/browser/download/download_request_limiter.h" |
14 #include "chrome/browser/download/download_resource_throttle.h" | 15 #include "chrome/browser/download/download_resource_throttle.h" |
15 #include "chrome/browser/download/download_util.h" | 16 #include "chrome/browser/download/download_util.h" |
16 #include "chrome/browser/extensions/user_script_listener.h" | 17 #include "chrome/browser/extensions/user_script_listener.h" |
17 #include "chrome/browser/external_protocol/external_protocol_handler.h" | 18 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
18 #include "chrome/browser/google/google_util.h" | 19 #include "chrome/browser/google/google_util.h" |
19 #include "chrome/browser/net/load_timing_observer.h" | 20 #include "chrome/browser/net/load_timing_observer.h" |
20 #include "chrome/browser/net/resource_prefetch_predictor_observer.h" | 21 #include "chrome/browser/net/resource_prefetch_predictor_observer.h" |
21 #include "chrome/browser/prerender/prerender_manager.h" | 22 #include "chrome/browser/prerender/prerender_manager.h" |
22 #include "chrome/browser/prerender/prerender_tracker.h" | 23 #include "chrome/browser/prerender/prerender_tracker.h" |
23 #include "chrome/browser/profiles/profile_io_data.h" | 24 #include "chrome/browser/profiles/profile_io_data.h" |
24 #include "chrome/browser/renderer_host/chrome_url_request_user_data.h" | 25 #include "chrome/browser/renderer_host/chrome_url_request_user_data.h" |
25 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h
" | 26 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h
" |
26 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 27 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
27 #include "chrome/browser/ui/auto_login_prompter.h" | 28 #include "chrome/browser/ui/auto_login_prompter.h" |
28 #include "chrome/browser/ui/login/login_prompt.h" | 29 #include "chrome/browser/ui/login/login_prompt.h" |
29 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | 30 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
30 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
31 #include "chrome/common/extensions/extension_constants.h" | 32 #include "chrome/common/extensions/extension_constants.h" |
32 #include "chrome/common/extensions/user_script.h" | 33 #include "chrome/common/extensions/user_script.h" |
33 #include "chrome/common/metrics/variations/variations_util.h" | |
34 #include "chrome/common/metrics/proto/chrome_experiments.pb.h" | |
35 #include "chrome/common/render_messages.h" | 34 #include "chrome/common/render_messages.h" |
36 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
37 #include "content/public/browser/notification_service.h" | 36 #include "content/public/browser/notification_service.h" |
38 #include "content/public/browser/render_view_host.h" | 37 #include "content/public/browser/render_view_host.h" |
39 #include "content/public/browser/resource_context.h" | 38 #include "content/public/browser/resource_context.h" |
40 #include "content/public/browser/resource_dispatcher_host.h" | 39 #include "content/public/browser/resource_dispatcher_host.h" |
41 #include "content/public/browser/resource_request_info.h" | 40 #include "content/public/browser/resource_request_info.h" |
42 #include "net/base/load_flags.h" | 41 #include "net/base/load_flags.h" |
43 #include "net/base/ssl_config_service.h" | 42 #include "net/base/ssl_config_service.h" |
44 #include "net/http/http_response_headers.h" | 43 #include "net/http/http_response_headers.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 content::NotificationService::NoDetails()); | 79 content::NotificationService::NoDetails()); |
81 } | 80 } |
82 | 81 |
83 } // end namespace | 82 } // end namespace |
84 | 83 |
85 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate( | 84 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate( |
86 prerender::PrerenderTracker* prerender_tracker) | 85 prerender::PrerenderTracker* prerender_tracker) |
87 : download_request_limiter_(g_browser_process->download_request_limiter()), | 86 : download_request_limiter_(g_browser_process->download_request_limiter()), |
88 safe_browsing_(g_browser_process->safe_browsing_service()), | 87 safe_browsing_(g_browser_process->safe_browsing_service()), |
89 user_script_listener_(new extensions::UserScriptListener()), | 88 user_script_listener_(new extensions::UserScriptListener()), |
90 prerender_tracker_(prerender_tracker), | 89 prerender_tracker_(prerender_tracker) { |
91 variation_ids_cache_initialized_(false) { | |
92 } | 90 } |
93 | 91 |
94 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() { | 92 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() { |
95 } | 93 } |
96 | 94 |
97 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest( | 95 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest( |
98 int child_id, | 96 int child_id, |
99 int route_id, | 97 int route_id, |
100 const std::string& method, | 98 const std::string& method, |
101 const GURL& url, | 99 const GURL& url, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 #endif | 155 #endif |
158 #if defined(OS_CHROMEOS) | 156 #if defined(OS_CHROMEOS) |
159 if (resource_type == ResourceType::MAIN_FRAME) { | 157 if (resource_type == ResourceType::MAIN_FRAME) { |
160 // We check offline first, then check safe browsing so that we still can | 158 // We check offline first, then check safe browsing so that we still can |
161 // block unsafe site after we remove offline page. | 159 // block unsafe site after we remove offline page. |
162 throttles->push_back(new OfflineResourceThrottle( | 160 throttles->push_back(new OfflineResourceThrottle( |
163 child_id, route_id, request, appcache_service)); | 161 child_id, route_id, request, appcache_service)); |
164 } | 162 } |
165 #endif | 163 #endif |
166 | 164 |
167 AppendChromeMetricsHeaders(request, resource_context, resource_type); | 165 // Don't attempt to append headers to requests that have already started. |
| 166 // TODO(stevet): Remove this once the request ordering issues are resolved |
| 167 // in crbug.com/128048. |
| 168 if (!request->is_pending()) { |
| 169 net::HttpRequestHeaders headers; |
| 170 headers.CopyFrom(request->extra_request_headers()); |
| 171 ProfileIOData* io_data = ProfileIOData::FromResourceContext( |
| 172 resource_context); |
| 173 ChromeMetricsHelper::GetInstance()->AppendHeaders( |
| 174 request->url(), io_data->is_incognito(), |
| 175 io_data->GetMetricsEnabledStateOnIOThread(), &headers); |
| 176 request->SetExtraRequestHeaders(headers); |
| 177 } |
168 | 178 |
169 #if defined(ENABLE_ONE_CLICK_SIGNIN) | 179 #if defined(ENABLE_ONE_CLICK_SIGNIN) |
170 AppendChromeSyncGaiaHeader(request, resource_context); | 180 AppendChromeSyncGaiaHeader(request, resource_context); |
171 #endif | 181 #endif |
172 | 182 |
173 AppendStandardResourceThrottles(request, | 183 AppendStandardResourceThrottles(request, |
174 resource_context, | 184 resource_context, |
175 child_id, | 185 child_id, |
176 route_id, | 186 route_id, |
177 resource_type, | 187 resource_type, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 request, child_id, route_id, !is_subresource_request)); | 315 request, child_id, route_id, !is_subresource_request)); |
306 #endif | 316 #endif |
307 | 317 |
308 content::ResourceThrottle* throttle = | 318 content::ResourceThrottle* throttle = |
309 user_script_listener_->CreateResourceThrottle(request->url(), | 319 user_script_listener_->CreateResourceThrottle(request->url(), |
310 resource_type); | 320 resource_type); |
311 if (throttle) | 321 if (throttle) |
312 throttles->push_back(throttle); | 322 throttles->push_back(throttle); |
313 } | 323 } |
314 | 324 |
315 void ChromeResourceDispatcherHostDelegate::AppendChromeMetricsHeaders( | |
316 net::URLRequest* request, | |
317 content::ResourceContext* resource_context, | |
318 ResourceType::Type resource_type) { | |
319 // Don't attempt to append headers to requests that have already started. | |
320 // TODO(stevet): Remove this once the request ordering issues are resolved | |
321 // in crbug.com/128048. | |
322 if (request->is_pending()) | |
323 return; | |
324 | |
325 // Note the criteria for attaching Chrome experiment headers: | |
326 // 1. We only transmit to *.google.<TLD> domains. NOTE that this use of | |
327 // google_util helpers to check this does not guarantee that the URL is | |
328 // Google-owned, only that it is of the form *.google.<TLD>. In the future | |
329 // we may choose to reinforce this check. | |
330 // 2. Only transmit for non-Incognito profiles. | |
331 // 3. For the X-Chrome-UMA-Enabled bit, only set it if UMA is in fact enabled | |
332 // for this install of Chrome. | |
333 // 4. For the X-Chrome-Variations, only include non-empty variation IDs. | |
334 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); | |
335 if (io_data->is_incognito() || | |
336 !google_util::IsGoogleDomainUrl(request->url().spec(), | |
337 google_util::ALLOW_SUBDOMAIN, | |
338 google_util::ALLOW_NON_STANDARD_PORTS)) | |
339 return; | |
340 | |
341 if (io_data->GetMetricsEnabledStateOnIOThread()) | |
342 request->SetExtraRequestHeaderByName("X-Chrome-UMA-Enabled", "1", false); | |
343 | |
344 // Lazily initialize the header, if not already done, before attempting to | |
345 // transmit it. | |
346 InitVariationIDsCacheIfNeeded(); | |
347 if (!variation_ids_header_.empty()) { | |
348 request->SetExtraRequestHeaderByName("X-Chrome-Variations", | |
349 variation_ids_header_, | |
350 false); | |
351 } | |
352 } | |
353 | |
354 #if defined(ENABLE_ONE_CLICK_SIGNIN) | 325 #if defined(ENABLE_ONE_CLICK_SIGNIN) |
355 void ChromeResourceDispatcherHostDelegate::AppendChromeSyncGaiaHeader( | 326 void ChromeResourceDispatcherHostDelegate::AppendChromeSyncGaiaHeader( |
356 net::URLRequest* request, | 327 net::URLRequest* request, |
357 content::ResourceContext* resource_context) { | 328 content::ResourceContext* resource_context) { |
358 static const char kAllowChromeSignIn[] = "Allow-Chrome-SignIn"; | 329 static const char kAllowChromeSignIn[] = "Allow-Chrome-SignIn"; |
359 | 330 |
360 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); | 331 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); |
361 OneClickSigninHelper::Offer offer = | 332 OneClickSigninHelper::Offer offer = |
362 OneClickSigninHelper::CanOfferOnIOThread(request, io_data); | 333 OneClickSigninHelper::CanOfferOnIOThread(request, io_data); |
363 switch (offer) { | 334 switch (offer) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 OneClickSigninHelper::ShowInfoBarIfPossible(request, info->GetChildID(), | 422 OneClickSigninHelper::ShowInfoBarIfPossible(request, info->GetChildID(), |
452 info->GetRouteID()); | 423 info->GetRouteID()); |
453 #endif | 424 #endif |
454 | 425 |
455 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); | 426 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context); |
456 if (io_data->resource_prefetch_predictor_observer()) { | 427 if (io_data->resource_prefetch_predictor_observer()) { |
457 io_data->resource_prefetch_predictor_observer()->OnRequestRedirected( | 428 io_data->resource_prefetch_predictor_observer()->OnRequestRedirected( |
458 redirect_url, request); | 429 redirect_url, request); |
459 } | 430 } |
460 } | 431 } |
461 | |
462 void ChromeResourceDispatcherHostDelegate::OnFieldTrialGroupFinalized( | |
463 const std::string& trial_name, | |
464 const std::string& group_name) { | |
465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
466 chrome_variations::VariationID new_id = | |
467 chrome_variations::GetGoogleVariationID( | |
468 chrome_variations::GOOGLE_WEB_PROPERTIES, trial_name, group_name); | |
469 if (new_id == chrome_variations::kEmptyID) | |
470 return; | |
471 variation_ids_set_.insert(new_id); | |
472 UpdateVariationIDsHeaderValue(); | |
473 } | |
474 | |
475 void ChromeResourceDispatcherHostDelegate::InitVariationIDsCacheIfNeeded() { | |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
477 if (variation_ids_cache_initialized_) | |
478 return; | |
479 | |
480 // Register for additional cache updates. This is done first to avoid a race | |
481 // that could cause registered FieldTrials to be missed. | |
482 base::FieldTrialList::AddObserver(this); | |
483 | |
484 base::FieldTrial::ActiveGroups initial_groups; | |
485 base::FieldTrialList::GetActiveFieldTrialGroups(&initial_groups); | |
486 for (base::FieldTrial::ActiveGroups::const_iterator it = | |
487 initial_groups.begin(); it != initial_groups.end(); ++it) { | |
488 const chrome_variations::VariationID id = | |
489 chrome_variations::GetGoogleVariationID( | |
490 chrome_variations::GOOGLE_WEB_PROPERTIES, it->trial_name, | |
491 it->group_name); | |
492 if (id != chrome_variations::kEmptyID) | |
493 variation_ids_set_.insert(id); | |
494 } | |
495 UpdateVariationIDsHeaderValue(); | |
496 | |
497 variation_ids_cache_initialized_ = true; | |
498 } | |
499 | |
500 void ChromeResourceDispatcherHostDelegate::UpdateVariationIDsHeaderValue() { | |
501 // The header value is a serialized protobuffer of Variation IDs which is | |
502 // base64 encoded before transmitting as a string. | |
503 if (variation_ids_set_.empty()) | |
504 return; | |
505 | |
506 // This is the bottleneck for the creation of the header, so validate the size | |
507 // here. Force a hard maximum on the ID count in case the Variations server | |
508 // returns too many IDs and DOSs receiving servers with large requests. | |
509 DCHECK_LE(variation_ids_set_.size(), 10U); | |
510 if (variation_ids_set_.size() > 20) { | |
511 variation_ids_header_.clear(); | |
512 return; | |
513 } | |
514 | |
515 metrics::ChromeVariations proto; | |
516 for (std::set<chrome_variations::VariationID>::const_iterator it = | |
517 variation_ids_set_.begin(); it != variation_ids_set_.end(); ++it) | |
518 proto.add_variation_id(*it); | |
519 | |
520 std::string serialized; | |
521 proto.SerializeToString(&serialized); | |
522 | |
523 std::string hashed; | |
524 if (base::Base64Encode(serialized, &hashed)) { | |
525 // If successful, swap the header value with the new one. | |
526 // Note that the list of IDs and the header could be temporarily out of sync | |
527 // if IDs are added as the header is recreated. The receiving servers are OK | |
528 // with such descrepancies. | |
529 variation_ids_header_ = hashed; | |
530 } else { | |
531 DVLOG(1) << "Failed to base64 encode Variation IDs value: " << serialized; | |
532 } | |
533 } | |
OLD | NEW |