Temporarily re-enabling SizeAfterPrefChange test with traces (this time for Linux...
[chromium-blink-merge.git] / chrome / browser / renderer_host / chrome_resource_dispatcher_host_delegate.cc
blob65e68968ad4bbc4e38fb795a3d78b2fcd5b9b3ba
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
3 // found in the LICENSE file.
5 #include "chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.h"
7 #include <string>
8 #include <vector>
10 #include "base/base64.h"
11 #include "base/logging.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/component_updater/component_updater_service.h"
15 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
16 #include "chrome/browser/content_settings/host_content_settings_map.h"
17 #include "chrome/browser/download/download_request_limiter.h"
18 #include "chrome/browser/download/download_resource_throttle.h"
19 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
20 #include "chrome/browser/extensions/extension_renderer_state.h"
21 #include "chrome/browser/extensions/user_script_listener.h"
22 #include "chrome/browser/google/google_util.h"
23 #include "chrome/browser/metrics/variations/variations_http_header_provider.h"
24 #include "chrome/browser/prefetch/prefetch.h"
25 #include "chrome/browser/prerender/prerender_manager.h"
26 #include "chrome/browser/prerender/prerender_manager_factory.h"
27 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h"
28 #include "chrome/browser/prerender/prerender_resource_throttle.h"
29 #include "chrome/browser/prerender/prerender_tracker.h"
30 #include "chrome/browser/prerender/prerender_util.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_io_data.h"
33 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h"
34 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
35 #include "chrome/browser/signin/signin_header_helper.h"
36 #include "chrome/browser/tab_contents/tab_util.h"
37 #include "chrome/browser/ui/login/login_prompt.h"
38 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
39 #include "chrome/common/extensions/extension_constants.h"
40 #include "chrome/common/extensions/manifest_handlers/mime_types_handler.h"
41 #include "chrome/common/render_messages.h"
42 #include "chrome/common/url_constants.h"
43 #include "content/public/browser/browser_thread.h"
44 #include "content/public/browser/notification_service.h"
45 #include "content/public/browser/render_process_host.h"
46 #include "content/public/browser/render_view_host.h"
47 #include "content/public/browser/resource_context.h"
48 #include "content/public/browser/resource_dispatcher_host.h"
49 #include "content/public/browser/resource_request_info.h"
50 #include "content/public/browser/stream_handle.h"
51 #include "content/public/browser/web_contents.h"
52 #include "content/public/common/resource_response.h"
53 #include "extensions/browser/info_map.h"
54 #include "extensions/common/constants.h"
55 #include "extensions/common/user_script.h"
56 #include "net/base/load_flags.h"
57 #include "net/base/load_timing_info.h"
58 #include "net/base/request_priority.h"
59 #include "net/http/http_response_headers.h"
60 #include "net/url_request/url_request.h"
62 #if defined(ENABLE_CONFIGURATION_POLICY)
63 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
64 #endif
66 #if defined(ENABLE_MANAGED_USERS)
67 #include "chrome/browser/managed_mode/managed_mode_resource_throttle.h"
68 #endif
70 #if defined(USE_SYSTEM_PROTOBUF)
71 #include <google/protobuf/repeated_field.h>
72 #else
73 #include "third_party/protobuf/src/google/protobuf/repeated_field.h"
74 #endif
76 #if defined(OS_ANDROID)
77 #include "chrome/browser/android/intercept_download_resource_throttle.h"
78 #include "chrome/browser/ui/android/infobars/auto_login_prompter.h"
79 #include "components/navigation_interception/intercept_navigation_delegate.h"
80 #else
81 #include "chrome/browser/apps/app_url_redirector.h"
82 #include "chrome/browser/apps/ephemeral_app_throttle.h"
83 #endif
85 #if defined(OS_CHROMEOS)
86 #include "chrome/browser/chromeos/login/signin/merge_session_throttle.h"
87 // TODO(oshima): Enable this for other platforms.
88 #include "chrome/browser/renderer_host/offline_resource_throttle.h"
89 #endif
91 using content::BrowserThread;
92 using content::RenderViewHost;
93 using content::ResourceDispatcherHostLoginDelegate;
94 using content::ResourceRequestInfo;
95 using extensions::Extension;
96 using extensions::StreamsPrivateAPI;
98 #if defined(OS_ANDROID)
99 using navigation_interception::InterceptNavigationDelegate;
100 #endif
102 namespace {
104 ExternalProtocolHandler::Delegate* g_external_protocol_handler_delegate = NULL;
106 void NotifyDownloadInitiatedOnUI(int render_process_id, int render_view_id) {
107 RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
108 render_view_id);
109 if (!rvh)
110 return;
112 content::NotificationService::current()->Notify(
113 chrome::NOTIFICATION_DOWNLOAD_INITIATED,
114 content::Source<RenderViewHost>(rvh),
115 content::NotificationService::NoDetails());
118 prerender::PrerenderManager* GetPrerenderManager(int render_process_id,
119 int render_view_id) {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
122 content::WebContents* web_contents =
123 tab_util::GetWebContentsByID(render_process_id, render_view_id);
124 if (!web_contents)
125 return NULL;
127 content::BrowserContext* browser_context = web_contents->GetBrowserContext();
128 if (!browser_context)
129 return NULL;
131 Profile* profile = Profile::FromBrowserContext(browser_context);
132 if (!profile)
133 return NULL;
135 return prerender::PrerenderManagerFactory::GetForProfile(profile);
138 void UpdatePrerenderNetworkBytesCallback(int render_process_id,
139 int render_view_id,
140 int64 bytes) {
141 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
143 content::WebContents* web_contents =
144 tab_util::GetWebContentsByID(render_process_id, render_view_id);
145 // PrerenderContents::FromWebContents handles the NULL case.
146 prerender::PrerenderContents* prerender_contents =
147 prerender::PrerenderContents::FromWebContents(web_contents);
149 if (prerender_contents)
150 prerender_contents->AddNetworkBytes(bytes);
152 prerender::PrerenderManager* prerender_manager =
153 GetPrerenderManager(render_process_id, render_view_id);
154 if (prerender_manager)
155 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes);
158 #if !defined(OS_ANDROID)
159 // Goes through the extension's file browser handlers and checks if there is one
160 // that can handle the |mime_type|.
161 // |extension| must not be NULL.
162 bool ExtensionCanHandleMimeType(const Extension* extension,
163 const std::string& mime_type) {
164 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
165 if (!handler)
166 return false;
168 return handler->CanHandleMIMEType(mime_type);
171 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamHandle> stream,
172 int64 expected_content_size,
173 int render_process_id,
174 int render_view_id,
175 const std::string& extension_id) {
176 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
178 content::WebContents* web_contents =
179 tab_util::GetWebContentsByID(render_process_id, render_view_id);
180 if (!web_contents)
181 return;
183 // If the request was for a prerender, abort the prerender and do not
184 // continue.
185 prerender::PrerenderContents* prerender_contents =
186 prerender::PrerenderContents::FromWebContents(web_contents);
187 if (prerender_contents) {
188 prerender_contents->Destroy(prerender::FINAL_STATUS_DOWNLOAD);
189 return;
192 Profile* profile =
193 Profile::FromBrowserContext(web_contents->GetBrowserContext());
195 StreamsPrivateAPI* streams_private = StreamsPrivateAPI::Get(profile);
196 if (!streams_private)
197 return;
198 streams_private->ExecuteMimeTypeHandler(
199 extension_id, web_contents, stream.Pass(), expected_content_size);
202 void LaunchURL(const GURL& url, int render_process_id, int render_view_id,
203 bool user_gesture) {
204 // If there is no longer a WebContents, the request may have raced with tab
205 // closing. Don't fire the external request. (It may have been a prerender.)
206 content::WebContents* web_contents =
207 tab_util::GetWebContentsByID(render_process_id, render_view_id);
208 if (!web_contents)
209 return;
211 // Do not launch external requests attached to unswapped prerenders.
212 prerender::PrerenderContents* prerender_contents =
213 prerender::PrerenderContents::FromWebContents(web_contents);
214 if (prerender_contents) {
215 prerender_contents->Destroy(prerender::FINAL_STATUS_UNSUPPORTED_SCHEME);
216 prerender::ReportPrerenderExternalURL();
217 return;
220 ExternalProtocolHandler::LaunchUrlWithDelegate(
221 url, render_process_id, render_view_id,
222 g_external_protocol_handler_delegate,
223 user_gesture);
225 #endif // !defined(OS_ANDROID)
227 void AppendComponentUpdaterThrottles(
228 net::URLRequest* request,
229 content::ResourceContext* resource_context,
230 ResourceType::Type resource_type,
231 ScopedVector<content::ResourceThrottle>* throttles) {
232 const char* crx_id = NULL;
233 component_updater::ComponentUpdateService* cus =
234 g_browser_process->component_updater();
235 if (!cus)
236 return;
237 // Check for PNaCl pexe request.
238 if (resource_type == ResourceType::OBJECT) {
239 const net::HttpRequestHeaders& headers = request->extra_request_headers();
240 std::string accept_headers;
241 if (headers.GetHeader("Accept", &accept_headers)) {
242 if (accept_headers.find("application/x-pnacl") != std::string::npos &&
243 pnacl::NeedsOnDemandUpdate())
244 crx_id = "hnimpnehoodheedghdeeijklkeaacbdc";
248 if (crx_id) {
249 // We got a component we need to install, so throttle the resource
250 // until the component is installed.
251 throttles->push_back(
252 cus->GetOnDemandUpdater().GetOnDemandResourceThrottle(request, crx_id));
256 } // end namespace
258 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate(
259 prerender::PrerenderTracker* prerender_tracker)
260 : download_request_limiter_(g_browser_process->download_request_limiter()),
261 safe_browsing_(g_browser_process->safe_browsing_service()),
262 user_script_listener_(new extensions::UserScriptListener()),
263 prerender_tracker_(prerender_tracker) {
266 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() {
269 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest(
270 int child_id,
271 int route_id,
272 const std::string& method,
273 const GURL& url,
274 ResourceType::Type resource_type,
275 content::ResourceContext* resource_context) {
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
278 // Handle a PREFETCH resource type. If prefetch is disabled, squelch the
279 // request. Otherwise, do a normal request to warm the cache.
280 if (resource_type == ResourceType::PREFETCH) {
281 // All PREFETCH requests should be GETs, but be defensive about it.
282 if (method != "GET")
283 return false;
285 // If prefetch is disabled, kill the request.
286 if (!prefetch::IsPrefetchEnabled(resource_context))
287 return false;
290 return true;
293 void ChromeResourceDispatcherHostDelegate::RequestBeginning(
294 net::URLRequest* request,
295 content::ResourceContext* resource_context,
296 appcache::AppCacheService* appcache_service,
297 ResourceType::Type resource_type,
298 int child_id,
299 int route_id,
300 ScopedVector<content::ResourceThrottle>* throttles) {
301 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
302 bool is_prerendering =
303 info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender;
304 if (is_prerendering) {
305 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs)
306 // should remain at MAXIMUM_PRIORITY.
307 if (request->load_flags() & net::LOAD_IGNORE_LIMITS) {
308 DCHECK_EQ(request->priority(), net::MAXIMUM_PRIORITY);
309 } else {
310 request->SetPriority(net::IDLE);
314 ProfileIOData* io_data = ProfileIOData::FromResourceContext(
315 resource_context);
317 #if defined(OS_ANDROID)
318 // TODO(davidben): This is insufficient to integrate with prerender properly.
319 // https://crbug.com/370595
320 if (resource_type == ResourceType::MAIN_FRAME && !is_prerendering) {
321 throttles->push_back(
322 InterceptNavigationDelegate::CreateThrottleFor(request));
324 #else
325 if (resource_type == ResourceType::MAIN_FRAME) {
326 // Redirect some navigations to apps that have registered matching URL
327 // handlers ('url_handlers' in the manifest).
328 content::ResourceThrottle* url_to_app_throttle =
329 AppUrlRedirector::MaybeCreateThrottleFor(request, io_data);
330 if (url_to_app_throttle)
331 throttles->push_back(url_to_app_throttle);
333 if (!is_prerendering) {
334 // Experimental: Launch ephemeral apps from search results.
335 content::ResourceThrottle* ephemeral_app_throttle =
336 EphemeralAppThrottle::MaybeCreateThrottleForLaunch(
337 request, io_data);
338 if (ephemeral_app_throttle)
339 throttles->push_back(ephemeral_app_throttle);
342 #endif
344 #if defined(OS_CHROMEOS)
345 // Check if we need to add offline throttle. This should be done only
346 // for main frames.
347 if (resource_type == ResourceType::MAIN_FRAME) {
348 // We check offline first, then check safe browsing so that we still can
349 // block unsafe site after we remove offline page.
350 throttles->push_back(new OfflineResourceThrottle(request,
351 appcache_service));
354 // Check if we need to add merge session throttle. This throttle will postpone
355 // loading of main frames and XHR request.
356 if (resource_type == ResourceType::MAIN_FRAME ||
357 resource_type == ResourceType::XHR) {
358 // Add interstitial page while merge session process (cookie
359 // reconstruction from OAuth2 refresh token in ChromeOS login) is still in
360 // progress while we are attempting to load a google property.
361 if (!MergeSessionThrottle::AreAllSessionMergedAlready() &&
362 request->url().SchemeIsHTTPOrHTTPS()) {
363 throttles->push_back(new MergeSessionThrottle(request, resource_type));
366 #endif
368 // Don't attempt to append headers to requests that have already started.
369 // TODO(stevet): Remove this once the request ordering issues are resolved
370 // in crbug.com/128048.
371 if (!request->is_pending()) {
372 net::HttpRequestHeaders headers;
373 headers.CopyFrom(request->extra_request_headers());
374 bool is_off_the_record = io_data->IsOffTheRecord();
375 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->
376 AppendHeaders(request->url(),
377 is_off_the_record,
378 !is_off_the_record &&
379 io_data->GetMetricsEnabledStateOnIOThread(),
380 &headers);
381 request->SetExtraRequestHeaders(headers);
384 #if defined(ENABLE_ONE_CLICK_SIGNIN)
385 AppendChromeSyncGaiaHeader(request, resource_context);
386 #endif
388 #if defined(ENABLE_CONFIGURATION_POLICY)
389 if (io_data->policy_header_helper())
390 io_data->policy_header_helper()->AddPolicyHeaders(request);
391 #endif
393 signin::AppendMirrorRequestHeaderIfPossible(
394 request, GURL() /* redirect_url */,
395 io_data, info->GetChildID(), info->GetRouteID());
397 AppendStandardResourceThrottles(request,
398 resource_context,
399 resource_type,
400 throttles);
401 if (!is_prerendering) {
402 AppendComponentUpdaterThrottles(request,
403 resource_context,
404 resource_type,
405 throttles);
409 void ChromeResourceDispatcherHostDelegate::DownloadStarting(
410 net::URLRequest* request,
411 content::ResourceContext* resource_context,
412 int child_id,
413 int route_id,
414 int request_id,
415 bool is_content_initiated,
416 bool must_download,
417 ScopedVector<content::ResourceThrottle>* throttles) {
418 BrowserThread::PostTask(
419 BrowserThread::UI, FROM_HERE,
420 base::Bind(&NotifyDownloadInitiatedOnUI, child_id, route_id));
422 // If it's from the web, we don't trust it, so we push the throttle on.
423 if (is_content_initiated) {
424 throttles->push_back(
425 new DownloadResourceThrottle(download_request_limiter_.get(),
426 child_id,
427 route_id,
428 request->url(),
429 request->method()));
430 #if defined(OS_ANDROID)
431 throttles->push_back(
432 new chrome::InterceptDownloadResourceThrottle(
433 request, child_id, route_id, request_id));
434 #endif
437 // If this isn't a new request, we've seen this before and added the standard
438 // resource throttles already so no need to add it again.
439 if (!request->is_pending()) {
440 AppendStandardResourceThrottles(request,
441 resource_context,
442 ResourceType::MAIN_FRAME,
443 throttles);
447 ResourceDispatcherHostLoginDelegate*
448 ChromeResourceDispatcherHostDelegate::CreateLoginDelegate(
449 net::AuthChallengeInfo* auth_info, net::URLRequest* request) {
450 return CreateLoginPrompt(auth_info, request);
453 bool ChromeResourceDispatcherHostDelegate::HandleExternalProtocol(
454 const GURL& url,
455 int child_id,
456 int route_id,
457 bool initiated_by_user_gesture) {
458 #if defined(OS_ANDROID)
459 // Android use a resource throttle to handle external as well as internal
460 // protocols.
461 return false;
462 #else
464 ExtensionRendererState::WebViewInfo info;
465 if (ExtensionRendererState::GetInstance()->GetWebViewInfo(child_id,
466 route_id,
467 &info)) {
468 return false;
471 BrowserThread::PostTask(
472 BrowserThread::UI, FROM_HERE,
473 base::Bind(&LaunchURL, url, child_id, route_id,
474 initiated_by_user_gesture));
475 return true;
476 #endif
479 void ChromeResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
480 net::URLRequest* request,
481 content::ResourceContext* resource_context,
482 ResourceType::Type resource_type,
483 ScopedVector<content::ResourceThrottle>* throttles) {
484 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
485 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
486 // Insert safe browsing at the front of the list, so it gets to decide on
487 // policies first.
488 if (io_data->safe_browsing_enabled()->GetValue()) {
489 bool is_subresource_request = resource_type != ResourceType::MAIN_FRAME;
490 content::ResourceThrottle* throttle =
491 SafeBrowsingResourceThrottleFactory::Create(request,
492 is_subresource_request,
493 safe_browsing_.get());
494 if (throttle)
495 throttles->push_back(throttle);
497 #endif
499 #if defined(ENABLE_MANAGED_USERS)
500 bool is_subresource_request = resource_type != ResourceType::MAIN_FRAME;
501 throttles->push_back(new ManagedModeResourceThrottle(
502 request, !is_subresource_request,
503 io_data->managed_mode_url_filter()));
504 #endif
506 content::ResourceThrottle* throttle =
507 user_script_listener_->CreateResourceThrottle(request->url(),
508 resource_type);
509 if (throttle)
510 throttles->push_back(throttle);
512 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
513 if (info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender) {
514 throttles->push_back(new prerender::PrerenderResourceThrottle(request));
516 if (prerender_tracker_->IsPendingSwapRequestOnIOThread(
517 info->GetChildID(), info->GetRenderFrameID(), request->url())) {
518 throttles->push_back(new prerender::PrerenderPendingSwapThrottle(
519 request, prerender_tracker_));
523 #if defined(ENABLE_ONE_CLICK_SIGNIN)
524 void ChromeResourceDispatcherHostDelegate::AppendChromeSyncGaiaHeader(
525 net::URLRequest* request,
526 content::ResourceContext* resource_context) {
527 static const char kAllowChromeSignIn[] = "Allow-Chrome-SignIn";
529 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
530 OneClickSigninHelper::Offer offer =
531 OneClickSigninHelper::CanOfferOnIOThread(request, io_data);
532 switch (offer) {
533 case OneClickSigninHelper::CAN_OFFER:
534 request->SetExtraRequestHeaderByName(kAllowChromeSignIn, "1", false);
535 break;
536 case OneClickSigninHelper::DONT_OFFER:
537 request->RemoveRequestHeaderByName(kAllowChromeSignIn);
538 break;
539 case OneClickSigninHelper::IGNORE_REQUEST:
540 break;
543 #endif
545 bool ChromeResourceDispatcherHostDelegate::ShouldForceDownloadResource(
546 const GURL& url, const std::string& mime_type) {
547 // Special-case user scripts to get downloaded instead of viewed.
548 return extensions::UserScript::IsURLUserScript(url, mime_type);
551 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
552 content::ResourceContext* resource_context,
553 const GURL& url,
554 const std::string& mime_type,
555 GURL* origin,
556 std::string* target_id) {
557 #if !defined(OS_ANDROID)
558 ProfileIOData* io_data =
559 ProfileIOData::FromResourceContext(resource_context);
560 bool profile_is_off_the_record = io_data->IsOffTheRecord();
561 const scoped_refptr<const extensions::InfoMap> extension_info_map(
562 io_data->GetExtensionInfoMap());
563 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist();
564 // Go through the white-listed extensions and try to use them to intercept
565 // the URL request.
566 for (size_t i = 0; i < whitelist.size(); ++i) {
567 const char* extension_id = whitelist[i].c_str();
568 const Extension* extension =
569 extension_info_map->extensions().GetByID(extension_id);
570 // The white-listed extension may not be installed, so we have to NULL check
571 // |extension|.
572 if (!extension ||
573 (profile_is_off_the_record &&
574 !extension_info_map->IsIncognitoEnabled(extension_id))) {
575 continue;
578 if (ExtensionCanHandleMimeType(extension, mime_type)) {
579 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
580 *target_id = extension_id;
581 return true;
584 #endif
585 return false;
588 void ChromeResourceDispatcherHostDelegate::OnStreamCreated(
589 content::ResourceContext* resource_context,
590 int render_process_id,
591 int render_view_id,
592 const std::string& target_id,
593 scoped_ptr<content::StreamHandle> stream,
594 int64 expected_content_size) {
595 #if !defined(OS_ANDROID)
596 content::BrowserThread::PostTask(
597 content::BrowserThread::UI, FROM_HERE,
598 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream),
599 expected_content_size, render_process_id, render_view_id,
600 target_id));
601 #endif
604 void ChromeResourceDispatcherHostDelegate::OnResponseStarted(
605 net::URLRequest* request,
606 content::ResourceContext* resource_context,
607 content::ResourceResponse* response,
608 IPC::Sender* sender) {
609 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
611 #if defined(OS_ANDROID)
612 // See if the response contains the X-Auto-Login header. If so, this was
613 // a request for a login page, and the server is allowing the browser to
614 // suggest auto-login, if available.
615 AutoLoginPrompter::ShowInfoBarIfPossible(request, info->GetChildID(),
616 info->GetRouteID());
617 #endif
619 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
621 #if defined(ENABLE_ONE_CLICK_SIGNIN)
622 // See if the response contains the Google-Accounts-SignIn header. If so,
623 // then the user has just finished signing in, and the server is allowing the
624 // browser to suggest connecting the user's profile to the account.
625 OneClickSigninHelper::ShowInfoBarIfPossible(request, io_data,
626 info->GetChildID(),
627 info->GetRouteID());
628 #endif
630 // See if the response contains the X-Chrome-Manage-Accounts header. If so
631 // show the profile avatar bubble so that user can complete signin/out action
632 // the native UI.
633 signin::ProcessMirrorResponseHeaderIfExists(request, io_data,
634 info->GetChildID(),
635 info->GetRouteID());
637 // Build in additional protection for the chrome web store origin.
638 GURL webstore_url(extension_urls::GetWebstoreLaunchURL());
639 if (request->url().DomainIs(webstore_url.host().c_str())) {
640 net::HttpResponseHeaders* response_headers = request->response_headers();
641 if (!response_headers->HasHeaderValue("x-frame-options", "deny") &&
642 !response_headers->HasHeaderValue("x-frame-options", "sameorigin")) {
643 response_headers->RemoveHeader("x-frame-options");
644 response_headers->AddHeader("x-frame-options: sameorigin");
648 // Ignores x-frame-options for the chrome signin UI.
649 const std::string request_spec(
650 request->first_party_for_cookies().GetOrigin().spec());
651 #if defined(OS_CHROMEOS)
652 if (request_spec == chrome::kChromeUIOobeURL ||
653 request_spec == chrome::kChromeUIChromeSigninURL) {
654 #else
655 if (request_spec == chrome::kChromeUIChromeSigninURL) {
656 #endif
657 net::HttpResponseHeaders* response_headers = request->response_headers();
658 if (response_headers->HasHeader("x-frame-options"))
659 response_headers->RemoveHeader("x-frame-options");
662 prerender::URLRequestResponseStarted(request);
665 void ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
666 const GURL& redirect_url,
667 net::URLRequest* request,
668 content::ResourceContext* resource_context,
669 content::ResourceResponse* response) {
670 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
671 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
673 #if defined(ENABLE_ONE_CLICK_SIGNIN)
674 // See if the response contains the Google-Accounts-SignIn header. If so,
675 // then the user has just finished signing in, and the server is allowing the
676 // browser to suggest connecting the user's profile to the account.
677 OneClickSigninHelper::ShowInfoBarIfPossible(request, io_data,
678 info->GetChildID(),
679 info->GetRouteID());
680 AppendChromeSyncGaiaHeader(request, resource_context);
681 #endif
683 // In the Mirror world, Chrome should append a X-Chrome-Connected header to
684 // all Gaia requests from a connected profile so Gaia could return a 204
685 // response and let Chrome handle the action with native UI. The only
686 // exception is requests from gaia webview, since the native profile
687 // management UI is built on top of it.
688 signin::AppendMirrorRequestHeaderIfPossible(request, redirect_url, io_data,
689 info->GetChildID(), info->GetRouteID());
691 #if defined(ENABLE_CONFIGURATION_POLICY)
692 if (io_data->policy_header_helper())
693 io_data->policy_header_helper()->AddPolicyHeaders(request);
694 #endif
697 // Notification that a request has completed.
698 void ChromeResourceDispatcherHostDelegate::RequestComplete(
699 net::URLRequest* url_request) {
700 // Jump on the UI thread and inform the prerender about the bytes.
701 const ResourceRequestInfo* info =
702 ResourceRequestInfo::ForRequest(url_request);
703 if (url_request && !url_request->was_cached()) {
704 BrowserThread::PostTask(BrowserThread::UI,
705 FROM_HERE,
706 base::Bind(&UpdatePrerenderNetworkBytesCallback,
707 info->GetChildID(),
708 info->GetRouteID(),
709 url_request->GetTotalReceivedBytes()));
713 // static
714 void ChromeResourceDispatcherHostDelegate::
715 SetExternalProtocolHandlerDelegateForTesting(
716 ExternalProtocolHandler::Delegate* delegate) {
717 g_external_protocol_handler_delegate = delegate;