Re-add resource speculative prefetching code
[chromium-blink-merge.git] / chrome / browser / renderer_host / chrome_resource_dispatcher_host_delegate.cc
blob1f60a834e0e6fd156ad516fa8503f60780e8627f
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/guid.h"
12 #include "base/logging.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/component_updater/component_updater_resource_throttle.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/net/resource_prefetch_predictor_observer.h"
20 #include "chrome/browser/prefetch/prefetch.h"
21 #include "chrome/browser/prerender/prerender_manager.h"
22 #include "chrome/browser/prerender/prerender_manager_factory.h"
23 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h"
24 #include "chrome/browser/prerender/prerender_resource_throttle.h"
25 #include "chrome/browser/prerender/prerender_tracker.h"
26 #include "chrome/browser/prerender/prerender_util.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/profiles/profile_io_data.h"
29 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h"
30 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
31 #include "chrome/browser/signin/signin_header_helper.h"
32 #include "chrome/browser/tab_contents/tab_util.h"
33 #include "chrome/browser/ui/login/login_prompt.h"
34 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
35 #include "chrome/common/render_messages.h"
36 #include "chrome/common/url_constants.h"
37 #include "components/google/core/browser/google_util.h"
38 #include "components/variations/variations_http_header_provider.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/render_process_host.h"
42 #include "content/public/browser/render_view_host.h"
43 #include "content/public/browser/resource_context.h"
44 #include "content/public/browser/resource_dispatcher_host.h"
45 #include "content/public/browser/resource_request_info.h"
46 #include "content/public/browser/stream_handle.h"
47 #include "content/public/browser/web_contents.h"
48 #include "content/public/common/resource_response.h"
49 #include "net/base/load_flags.h"
50 #include "net/base/load_timing_info.h"
51 #include "net/base/request_priority.h"
52 #include "net/http/http_response_headers.h"
53 #include "net/url_request/url_request.h"
55 #if !defined(DISABLE_NACL)
56 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
57 #endif
59 #if defined(ENABLE_CONFIGURATION_POLICY)
60 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
61 #endif
63 #if defined(ENABLE_EXTENSIONS)
64 #include "chrome/browser/apps/app_url_redirector.h"
65 #include "chrome/browser/apps/ephemeral_app_throttle.h"
66 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
67 #include "chrome/browser/extensions/user_script_listener.h"
68 #include "chrome/common/extensions/manifest_handlers/mime_types_handler.h"
69 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
70 #include "extensions/browser/info_map.h"
71 #include "extensions/common/constants.h"
72 #include "extensions/common/extension_urls.h"
73 #include "extensions/common/user_script.h"
74 #endif
76 #if defined(ENABLE_MANAGED_USERS)
77 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h"
78 #endif
80 #if defined(USE_SYSTEM_PROTOBUF)
81 #include <google/protobuf/repeated_field.h>
82 #else
83 #include "third_party/protobuf/src/google/protobuf/repeated_field.h"
84 #endif
86 #if defined(OS_ANDROID)
87 #include "chrome/browser/android/intercept_download_resource_throttle.h"
88 #include "chrome/browser/ui/android/infobars/auto_login_prompter.h"
89 #include "components/navigation_interception/intercept_navigation_delegate.h"
90 #endif
92 #if defined(OS_CHROMEOS)
93 #include "chrome/browser/chromeos/login/signin/merge_session_throttle.h"
94 // TODO(oshima): Enable this for other platforms.
95 #include "chrome/browser/renderer_host/offline_resource_throttle.h"
96 #endif
98 using content::BrowserThread;
99 using content::RenderViewHost;
100 using content::ResourceDispatcherHostLoginDelegate;
101 using content::ResourceRequestInfo;
102 using content::ResourceType;
104 #if defined(ENABLE_EXTENSIONS)
105 using extensions::Extension;
106 using extensions::StreamsPrivateAPI;
107 #endif
109 #if defined(OS_ANDROID)
110 using navigation_interception::InterceptNavigationDelegate;
111 #endif
113 namespace {
115 ExternalProtocolHandler::Delegate* g_external_protocol_handler_delegate = NULL;
117 void NotifyDownloadInitiatedOnUI(int render_process_id, int render_view_id) {
118 RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
119 render_view_id);
120 if (!rvh)
121 return;
123 content::NotificationService::current()->Notify(
124 chrome::NOTIFICATION_DOWNLOAD_INITIATED,
125 content::Source<RenderViewHost>(rvh),
126 content::NotificationService::NoDetails());
129 prerender::PrerenderManager* GetPrerenderManager(int render_process_id,
130 int render_view_id) {
131 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
133 content::WebContents* web_contents =
134 tab_util::GetWebContentsByID(render_process_id, render_view_id);
135 if (!web_contents)
136 return NULL;
138 content::BrowserContext* browser_context = web_contents->GetBrowserContext();
139 if (!browser_context)
140 return NULL;
142 Profile* profile = Profile::FromBrowserContext(browser_context);
143 if (!profile)
144 return NULL;
146 return prerender::PrerenderManagerFactory::GetForProfile(profile);
149 void UpdatePrerenderNetworkBytesCallback(int render_process_id,
150 int render_view_id,
151 int64 bytes) {
152 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
154 content::WebContents* web_contents =
155 tab_util::GetWebContentsByID(render_process_id, render_view_id);
156 // PrerenderContents::FromWebContents handles the NULL case.
157 prerender::PrerenderContents* prerender_contents =
158 prerender::PrerenderContents::FromWebContents(web_contents);
160 if (prerender_contents)
161 prerender_contents->AddNetworkBytes(bytes);
163 prerender::PrerenderManager* prerender_manager =
164 GetPrerenderManager(render_process_id, render_view_id);
165 if (prerender_manager)
166 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes);
169 #if defined(ENABLE_EXTENSIONS)
170 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamHandle> stream,
171 int64 expected_content_size,
172 int render_process_id,
173 int render_view_id,
174 const std::string& extension_id,
175 const std::string& view_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(), view_id,
200 expected_content_size);
202 #endif // !defined(ENABLE_EXTENSIONS)
204 #if !defined(OS_ANDROID)
205 void LaunchURL(const GURL& url, int render_process_id, int render_view_id) {
206 // If there is no longer a WebContents, the request may have raced with tab
207 // closing. Don't fire the external request. (It may have been a prerender.)
208 content::WebContents* web_contents =
209 tab_util::GetWebContentsByID(render_process_id, render_view_id);
210 if (!web_contents)
211 return;
213 // Do not launch external requests attached to unswapped prerenders.
214 prerender::PrerenderContents* prerender_contents =
215 prerender::PrerenderContents::FromWebContents(web_contents);
216 if (prerender_contents) {
217 prerender_contents->Destroy(prerender::FINAL_STATUS_UNSUPPORTED_SCHEME);
218 prerender::ReportPrerenderExternalURL();
219 return;
222 ExternalProtocolHandler::LaunchUrlWithDelegate(
223 url,
224 render_process_id,
225 render_view_id,
226 g_external_protocol_handler_delegate);
228 #endif // !defined(OS_ANDROID)
230 #if !defined(DISABLE_NACL)
231 void AppendComponentUpdaterThrottles(
232 net::URLRequest* request,
233 content::ResourceContext* resource_context,
234 ResourceType resource_type,
235 ScopedVector<content::ResourceThrottle>* throttles) {
236 const char* crx_id = NULL;
237 component_updater::ComponentUpdateService* cus =
238 g_browser_process->component_updater();
239 if (!cus)
240 return;
241 // Check for PNaCl pexe request.
242 if (resource_type == content::RESOURCE_TYPE_OBJECT) {
243 const net::HttpRequestHeaders& headers = request->extra_request_headers();
244 std::string accept_headers;
245 if (headers.GetHeader("Accept", &accept_headers)) {
246 if (accept_headers.find("application/x-pnacl") != std::string::npos &&
247 pnacl::NeedsOnDemandUpdate())
248 crx_id = "hnimpnehoodheedghdeeijklkeaacbdc";
252 if (crx_id) {
253 // We got a component we need to install, so throttle the resource
254 // until the component is installed.
255 throttles->push_back(
256 component_updater::GetOnDemandResourceThrottle(cus, crx_id));
259 #endif // !defined(DISABLE_NACL)
261 } // namespace
263 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate(
264 prerender::PrerenderTracker* prerender_tracker)
265 : download_request_limiter_(g_browser_process->download_request_limiter()),
266 safe_browsing_(g_browser_process->safe_browsing_service()),
267 #if defined(ENABLE_EXTENSIONS)
268 user_script_listener_(new extensions::UserScriptListener()),
269 #endif
270 prerender_tracker_(prerender_tracker) {
273 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() {
274 #if defined(ENABLE_EXTENSIONS)
275 CHECK(stream_target_info_.empty());
276 #endif
279 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest(
280 const std::string& method,
281 const GURL& url,
282 ResourceType resource_type,
283 content::ResourceContext* resource_context) {
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
286 // Handle a PREFETCH resource type. If prefetch is disabled, squelch the
287 // request. Otherwise, do a normal request to warm the cache.
288 if (resource_type == content::RESOURCE_TYPE_PREFETCH) {
289 // All PREFETCH requests should be GETs, but be defensive about it.
290 if (method != "GET")
291 return false;
293 // If prefetch is disabled, kill the request.
294 if (!prefetch::IsPrefetchEnabled(resource_context))
295 return false;
298 return true;
301 void ChromeResourceDispatcherHostDelegate::RequestBeginning(
302 net::URLRequest* request,
303 content::ResourceContext* resource_context,
304 content::AppCacheService* appcache_service,
305 ResourceType resource_type,
306 ScopedVector<content::ResourceThrottle>* throttles) {
307 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
308 bool is_prerendering =
309 info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender;
310 if (is_prerendering) {
311 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs)
312 // should remain at MAXIMUM_PRIORITY.
313 if (request->load_flags() & net::LOAD_IGNORE_LIMITS) {
314 DCHECK_EQ(request->priority(), net::MAXIMUM_PRIORITY);
315 } else {
316 request->SetPriority(net::IDLE);
320 ProfileIOData* io_data = ProfileIOData::FromResourceContext(
321 resource_context);
323 #if defined(OS_ANDROID)
324 // TODO(davidben): This is insufficient to integrate with prerender properly.
325 // https://crbug.com/370595
326 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME && !is_prerendering) {
327 throttles->push_back(
328 InterceptNavigationDelegate::CreateThrottleFor(request));
330 #else
331 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
332 // Redirect some navigations to apps that have registered matching URL
333 // handlers ('url_handlers' in the manifest).
334 content::ResourceThrottle* url_to_app_throttle =
335 AppUrlRedirector::MaybeCreateThrottleFor(request, io_data);
336 if (url_to_app_throttle)
337 throttles->push_back(url_to_app_throttle);
339 if (!is_prerendering) {
340 // Experimental: Launch ephemeral apps from search results.
341 content::ResourceThrottle* ephemeral_app_throttle =
342 EphemeralAppThrottle::MaybeCreateThrottleForLaunch(
343 request, io_data);
344 if (ephemeral_app_throttle)
345 throttles->push_back(ephemeral_app_throttle);
348 #endif
350 #if defined(OS_CHROMEOS)
351 // Check if we need to add offline throttle. This should be done only
352 // for main frames.
353 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
354 // We check offline first, then check safe browsing so that we still can
355 // block unsafe site after we remove offline page.
356 throttles->push_back(new OfflineResourceThrottle(request,
357 appcache_service));
360 // Check if we need to add merge session throttle. This throttle will postpone
361 // loading of main frames and XHR request.
362 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME ||
363 resource_type == content::RESOURCE_TYPE_XHR) {
364 // Add interstitial page while merge session process (cookie
365 // reconstruction from OAuth2 refresh token in ChromeOS login) is still in
366 // progress while we are attempting to load a google property.
367 if (!MergeSessionThrottle::AreAllSessionMergedAlready() &&
368 request->url().SchemeIsHTTPOrHTTPS()) {
369 throttles->push_back(new MergeSessionThrottle(request, resource_type));
372 #endif
374 // Don't attempt to append headers to requests that have already started.
375 // TODO(stevet): Remove this once the request ordering issues are resolved
376 // in crbug.com/128048.
377 if (!request->is_pending()) {
378 net::HttpRequestHeaders headers;
379 headers.CopyFrom(request->extra_request_headers());
380 bool is_off_the_record = io_data->IsOffTheRecord();
381 variations::VariationsHttpHeaderProvider::GetInstance()->
382 AppendHeaders(request->url(),
383 is_off_the_record,
384 !is_off_the_record &&
385 io_data->GetMetricsEnabledStateOnIOThread(),
386 &headers);
387 request->SetExtraRequestHeaders(headers);
390 #if defined(ENABLE_ONE_CLICK_SIGNIN)
391 AppendChromeSyncGaiaHeader(request, resource_context);
392 #endif
394 #if defined(ENABLE_CONFIGURATION_POLICY)
395 if (io_data->policy_header_helper())
396 io_data->policy_header_helper()->AddPolicyHeaders(request->url(), request);
397 #endif
399 signin::AppendMirrorRequestHeaderIfPossible(
400 request, GURL() /* redirect_url */, io_data);
402 AppendStandardResourceThrottles(request,
403 resource_context,
404 resource_type,
405 throttles);
406 #if !defined(DISABLE_NACL)
407 if (!is_prerendering) {
408 AppendComponentUpdaterThrottles(request,
409 resource_context,
410 resource_type,
411 throttles);
413 #endif
415 if (io_data->resource_prefetch_predictor_observer()) {
416 io_data->resource_prefetch_predictor_observer()->OnRequestStarted(
417 request, resource_type, info->GetChildID(), info->GetRenderFrameID());
421 void ChromeResourceDispatcherHostDelegate::DownloadStarting(
422 net::URLRequest* request,
423 content::ResourceContext* resource_context,
424 int child_id,
425 int route_id,
426 int request_id,
427 bool is_content_initiated,
428 bool must_download,
429 ScopedVector<content::ResourceThrottle>* throttles) {
430 BrowserThread::PostTask(
431 BrowserThread::UI, FROM_HERE,
432 base::Bind(&NotifyDownloadInitiatedOnUI, child_id, route_id));
434 // If it's from the web, we don't trust it, so we push the throttle on.
435 if (is_content_initiated) {
436 throttles->push_back(
437 new DownloadResourceThrottle(download_request_limiter_.get(),
438 child_id,
439 route_id,
440 request->url(),
441 request->method()));
442 #if defined(OS_ANDROID)
443 throttles->push_back(
444 new chrome::InterceptDownloadResourceThrottle(
445 request, child_id, route_id, request_id));
446 #endif
449 // If this isn't a new request, we've seen this before and added the standard
450 // resource throttles already so no need to add it again.
451 if (!request->is_pending()) {
452 AppendStandardResourceThrottles(request,
453 resource_context,
454 content::RESOURCE_TYPE_MAIN_FRAME,
455 throttles);
459 ResourceDispatcherHostLoginDelegate*
460 ChromeResourceDispatcherHostDelegate::CreateLoginDelegate(
461 net::AuthChallengeInfo* auth_info, net::URLRequest* request) {
462 return CreateLoginPrompt(auth_info, request);
465 bool ChromeResourceDispatcherHostDelegate::HandleExternalProtocol(
466 const GURL& url,
467 int child_id,
468 int route_id) {
469 #if defined(OS_ANDROID)
470 // Android use a resource throttle to handle external as well as internal
471 // protocols.
472 return false;
473 #else
475 #if defined(ENABLE_EXTENSIONS)
476 if (extensions::WebViewRendererState::GetInstance()->IsGuest(child_id))
477 return false;
479 #endif // defined(ENABLE_EXTENSIONS)
481 BrowserThread::PostTask(BrowserThread::UI,
482 FROM_HERE,
483 base::Bind(&LaunchURL, url, child_id, route_id));
484 return true;
485 #endif
488 void ChromeResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
489 net::URLRequest* request,
490 content::ResourceContext* resource_context,
491 ResourceType resource_type,
492 ScopedVector<content::ResourceThrottle>* throttles) {
493 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
494 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
495 // Insert safe browsing at the front of the list, so it gets to decide on
496 // policies first.
497 if (io_data->safe_browsing_enabled()->GetValue()
498 #if defined(OS_ANDROID)
499 || io_data->IsDataReductionProxyEnabled()
500 #endif
502 bool is_subresource_request =
503 resource_type != content::RESOURCE_TYPE_MAIN_FRAME;
504 content::ResourceThrottle* throttle =
505 SafeBrowsingResourceThrottleFactory::Create(request,
506 resource_context,
507 is_subresource_request,
508 safe_browsing_.get());
509 if (throttle)
510 throttles->push_back(throttle);
512 #endif
514 #if defined(ENABLE_MANAGED_USERS)
515 bool is_subresource_request =
516 resource_type != content::RESOURCE_TYPE_MAIN_FRAME;
517 throttles->push_back(new SupervisedUserResourceThrottle(
518 request, !is_subresource_request,
519 io_data->supervised_user_url_filter()));
520 #endif
522 #if defined(ENABLE_EXTENSIONS)
523 content::ResourceThrottle* throttle =
524 user_script_listener_->CreateResourceThrottle(request->url(),
525 resource_type);
526 if (throttle)
527 throttles->push_back(throttle);
528 #endif
530 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
531 if (info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender) {
532 throttles->push_back(new prerender::PrerenderResourceThrottle(request));
534 if (prerender_tracker_->IsPendingSwapRequestOnIOThread(
535 info->GetChildID(), info->GetRenderFrameID(), request->url())) {
536 throttles->push_back(new prerender::PrerenderPendingSwapThrottle(
537 request, prerender_tracker_));
541 #if defined(ENABLE_ONE_CLICK_SIGNIN)
542 void ChromeResourceDispatcherHostDelegate::AppendChromeSyncGaiaHeader(
543 net::URLRequest* request,
544 content::ResourceContext* resource_context) {
545 static const char kAllowChromeSignIn[] = "Allow-Chrome-SignIn";
547 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
548 OneClickSigninHelper::Offer offer =
549 OneClickSigninHelper::CanOfferOnIOThread(request, io_data);
550 switch (offer) {
551 case OneClickSigninHelper::CAN_OFFER:
552 request->SetExtraRequestHeaderByName(kAllowChromeSignIn, "1", false);
553 break;
554 case OneClickSigninHelper::DONT_OFFER:
555 request->RemoveRequestHeaderByName(kAllowChromeSignIn);
556 break;
557 case OneClickSigninHelper::IGNORE_REQUEST:
558 break;
561 #endif
563 bool ChromeResourceDispatcherHostDelegate::ShouldForceDownloadResource(
564 const GURL& url, const std::string& mime_type) {
565 #if defined(ENABLE_EXTENSIONS)
566 // Special-case user scripts to get downloaded instead of viewed.
567 return extensions::UserScript::IsURLUserScript(url, mime_type);
568 #else
569 return false;
570 #endif
573 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
574 net::URLRequest* request,
575 const std::string& mime_type,
576 GURL* origin,
577 std::string* payload) {
578 #if defined(ENABLE_EXTENSIONS)
579 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
580 ProfileIOData* io_data =
581 ProfileIOData::FromResourceContext(info->GetContext());
582 bool profile_is_off_the_record = io_data->IsOffTheRecord();
583 const scoped_refptr<const extensions::InfoMap> extension_info_map(
584 io_data->GetExtensionInfoMap());
585 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist();
586 // Go through the white-listed extensions and try to use them to intercept
587 // the URL request.
588 for (size_t i = 0; i < whitelist.size(); ++i) {
589 const char* extension_id = whitelist[i].c_str();
590 const Extension* extension =
591 extension_info_map->extensions().GetByID(extension_id);
592 // The white-listed extension may not be installed, so we have to NULL check
593 // |extension|.
594 if (!extension ||
595 (profile_is_off_the_record &&
596 !extension_info_map->IsIncognitoEnabled(extension_id))) {
597 continue;
600 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
601 if (handler && handler->CanHandleMIMEType(mime_type)) {
602 StreamTargetInfo target_info;
603 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
604 target_info.extension_id = extension_id;
605 if (!handler->handler_url().empty()) {
606 target_info.view_id = base::GenerateGUID();
607 *payload = origin->spec() + handler->handler_url() +
608 "?id=" + target_info.view_id;
610 stream_target_info_[request] = target_info;
611 return true;
614 #endif
615 return false;
618 void ChromeResourceDispatcherHostDelegate::OnStreamCreated(
619 net::URLRequest* request,
620 scoped_ptr<content::StreamHandle> stream) {
621 #if defined(ENABLE_EXTENSIONS)
622 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
623 std::map<net::URLRequest*, StreamTargetInfo>::iterator ix =
624 stream_target_info_.find(request);
625 CHECK(ix != stream_target_info_.end());
626 content::BrowserThread::PostTask(
627 content::BrowserThread::UI, FROM_HERE,
628 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream),
629 request->GetExpectedContentSize(),
630 info->GetChildID(), info->GetRouteID(),
631 ix->second.extension_id, ix->second.view_id));
632 stream_target_info_.erase(request);
633 #endif
636 void ChromeResourceDispatcherHostDelegate::OnResponseStarted(
637 net::URLRequest* request,
638 content::ResourceContext* resource_context,
639 content::ResourceResponse* response,
640 IPC::Sender* sender) {
641 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
643 #if defined(OS_ANDROID)
644 // See if the response contains the X-Auto-Login header. If so, this was
645 // a request for a login page, and the server is allowing the browser to
646 // suggest auto-login, if available.
647 AutoLoginPrompter::ShowInfoBarIfPossible(request, info->GetChildID(),
648 info->GetRouteID());
649 #endif
651 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
653 #if defined(ENABLE_ONE_CLICK_SIGNIN)
654 // See if the response contains the Google-Accounts-SignIn header. If so,
655 // then the user has just finished signing in, and the server is allowing the
656 // browser to suggest connecting the user's profile to the account.
657 OneClickSigninHelper::ShowInfoBarIfPossible(request, io_data,
658 info->GetChildID(),
659 info->GetRouteID());
660 #endif
662 // See if the response contains the X-Chrome-Manage-Accounts header. If so
663 // show the profile avatar bubble so that user can complete signin/out action
664 // the native UI.
665 signin::ProcessMirrorResponseHeaderIfExists(request, io_data,
666 info->GetChildID(),
667 info->GetRouteID());
669 // Build in additional protection for the chrome web store origin.
670 #if defined(ENABLE_EXTENSIONS)
671 GURL webstore_url(extension_urls::GetWebstoreLaunchURL());
672 if (request->url().DomainIs(webstore_url.host().c_str())) {
673 net::HttpResponseHeaders* response_headers = request->response_headers();
674 if (!response_headers->HasHeaderValue("x-frame-options", "deny") &&
675 !response_headers->HasHeaderValue("x-frame-options", "sameorigin")) {
676 response_headers->RemoveHeader("x-frame-options");
677 response_headers->AddHeader("x-frame-options: sameorigin");
680 #endif
682 if (io_data->resource_prefetch_predictor_observer())
683 io_data->resource_prefetch_predictor_observer()->OnResponseStarted(request);
685 // Ignores x-frame-options for the chrome signin UI.
686 const std::string request_spec(
687 request->first_party_for_cookies().GetOrigin().spec());
688 #if defined(OS_CHROMEOS)
689 if (request_spec == chrome::kChromeUIOobeURL ||
690 request_spec == chrome::kChromeUIChromeSigninURL) {
691 #else
692 if (request_spec == chrome::kChromeUIChromeSigninURL) {
693 #endif
694 net::HttpResponseHeaders* response_headers = request->response_headers();
695 if (response_headers && response_headers->HasHeader("x-frame-options"))
696 response_headers->RemoveHeader("x-frame-options");
699 prerender::URLRequestResponseStarted(request);
702 void ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
703 const GURL& redirect_url,
704 net::URLRequest* request,
705 content::ResourceContext* resource_context,
706 content::ResourceResponse* response) {
707 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
709 #if defined(ENABLE_ONE_CLICK_SIGNIN)
710 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
712 // See if the response contains the Google-Accounts-SignIn header. If so,
713 // then the user has just finished signing in, and the server is allowing the
714 // browser to suggest connecting the user's profile to the account.
715 OneClickSigninHelper::ShowInfoBarIfPossible(request, io_data,
716 info->GetChildID(),
717 info->GetRouteID());
718 AppendChromeSyncGaiaHeader(request, resource_context);
719 #endif
721 // In the Mirror world, Chrome should append a X-Chrome-Connected header to
722 // all Gaia requests from a connected profile so Gaia could return a 204
723 // response and let Chrome handle the action with native UI. The only
724 // exception is requests from gaia webview, since the native profile
725 // management UI is built on top of it.
726 signin::AppendMirrorRequestHeaderIfPossible(request, redirect_url, io_data);
728 if (io_data->resource_prefetch_predictor_observer()) {
729 io_data->resource_prefetch_predictor_observer()->OnRequestRedirected(
730 redirect_url, request);
733 #if defined(ENABLE_CONFIGURATION_POLICY)
734 if (io_data->policy_header_helper())
735 io_data->policy_header_helper()->AddPolicyHeaders(redirect_url, request);
736 #endif
739 // Notification that a request has completed.
740 void ChromeResourceDispatcherHostDelegate::RequestComplete(
741 net::URLRequest* url_request) {
742 // Jump on the UI thread and inform the prerender about the bytes.
743 const ResourceRequestInfo* info =
744 ResourceRequestInfo::ForRequest(url_request);
745 if (url_request && !url_request->was_cached()) {
746 BrowserThread::PostTask(BrowserThread::UI,
747 FROM_HERE,
748 base::Bind(&UpdatePrerenderNetworkBytesCallback,
749 info->GetChildID(),
750 info->GetRouteID(),
751 url_request->GetTotalReceivedBytes()));
755 // static
756 void ChromeResourceDispatcherHostDelegate::
757 SetExternalProtocolHandlerDelegateForTesting(
758 ExternalProtocolHandler::Delegate* delegate) {
759 g_external_protocol_handler_delegate = delegate;