Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / renderer_host / chrome_resource_dispatcher_host_delegate.cc
blobc3c6480fd5ef0932e9822791d461dec2f5339da4
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/prefetch/prefetch.h"
20 #include "chrome/browser/prerender/prerender_manager.h"
21 #include "chrome/browser/prerender/prerender_manager_factory.h"
22 #include "chrome/browser/prerender/prerender_pending_swap_throttle.h"
23 #include "chrome/browser/prerender/prerender_resource_throttle.h"
24 #include "chrome/browser/prerender/prerender_tracker.h"
25 #include "chrome/browser/prerender/prerender_util.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/browser/profiles/profile_io_data.h"
28 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h"
29 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
30 #include "chrome/browser/signin/signin_header_helper.h"
31 #include "chrome/browser/tab_contents/tab_util.h"
32 #include "chrome/browser/ui/login/login_prompt.h"
33 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
34 #include "chrome/common/extensions/extension_constants.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/user_script.h"
73 #endif
75 #if defined(ENABLE_MANAGED_USERS)
76 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h"
77 #endif
79 #if defined(USE_SYSTEM_PROTOBUF)
80 #include <google/protobuf/repeated_field.h>
81 #else
82 #include "third_party/protobuf/src/google/protobuf/repeated_field.h"
83 #endif
85 #if defined(OS_ANDROID)
86 #include "chrome/browser/android/intercept_download_resource_throttle.h"
87 #include "chrome/browser/ui/android/infobars/auto_login_prompter.h"
88 #include "components/navigation_interception/intercept_navigation_delegate.h"
89 #endif
91 #if defined(OS_CHROMEOS)
92 #include "chrome/browser/chromeos/login/signin/merge_session_throttle.h"
93 // TODO(oshima): Enable this for other platforms.
94 #include "chrome/browser/renderer_host/offline_resource_throttle.h"
95 #endif
97 using content::BrowserThread;
98 using content::RenderViewHost;
99 using content::ResourceDispatcherHostLoginDelegate;
100 using content::ResourceRequestInfo;
101 using content::ResourceType;
103 #if defined(ENABLE_EXTENSIONS)
104 using extensions::Extension;
105 using extensions::StreamsPrivateAPI;
106 #endif
108 #if defined(OS_ANDROID)
109 using navigation_interception::InterceptNavigationDelegate;
110 #endif
112 namespace {
114 ExternalProtocolHandler::Delegate* g_external_protocol_handler_delegate = NULL;
116 void NotifyDownloadInitiatedOnUI(int render_process_id, int render_view_id) {
117 RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
118 render_view_id);
119 if (!rvh)
120 return;
122 content::NotificationService::current()->Notify(
123 chrome::NOTIFICATION_DOWNLOAD_INITIATED,
124 content::Source<RenderViewHost>(rvh),
125 content::NotificationService::NoDetails());
128 prerender::PrerenderManager* GetPrerenderManager(int render_process_id,
129 int render_view_id) {
130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
132 content::WebContents* web_contents =
133 tab_util::GetWebContentsByID(render_process_id, render_view_id);
134 if (!web_contents)
135 return NULL;
137 content::BrowserContext* browser_context = web_contents->GetBrowserContext();
138 if (!browser_context)
139 return NULL;
141 Profile* profile = Profile::FromBrowserContext(browser_context);
142 if (!profile)
143 return NULL;
145 return prerender::PrerenderManagerFactory::GetForProfile(profile);
148 void UpdatePrerenderNetworkBytesCallback(int render_process_id,
149 int render_view_id,
150 int64 bytes) {
151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
153 content::WebContents* web_contents =
154 tab_util::GetWebContentsByID(render_process_id, render_view_id);
155 // PrerenderContents::FromWebContents handles the NULL case.
156 prerender::PrerenderContents* prerender_contents =
157 prerender::PrerenderContents::FromWebContents(web_contents);
159 if (prerender_contents)
160 prerender_contents->AddNetworkBytes(bytes);
162 prerender::PrerenderManager* prerender_manager =
163 GetPrerenderManager(render_process_id, render_view_id);
164 if (prerender_manager)
165 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes);
168 #if defined(ENABLE_EXTENSIONS)
169 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamHandle> stream,
170 int64 expected_content_size,
171 int render_process_id,
172 int render_view_id,
173 const std::string& extension_id,
174 const std::string& view_id) {
175 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
177 content::WebContents* web_contents =
178 tab_util::GetWebContentsByID(render_process_id, render_view_id);
179 if (!web_contents)
180 return;
182 // If the request was for a prerender, abort the prerender and do not
183 // continue.
184 prerender::PrerenderContents* prerender_contents =
185 prerender::PrerenderContents::FromWebContents(web_contents);
186 if (prerender_contents) {
187 prerender_contents->Destroy(prerender::FINAL_STATUS_DOWNLOAD);
188 return;
191 Profile* profile =
192 Profile::FromBrowserContext(web_contents->GetBrowserContext());
194 StreamsPrivateAPI* streams_private = StreamsPrivateAPI::Get(profile);
195 if (!streams_private)
196 return;
197 streams_private->ExecuteMimeTypeHandler(
198 extension_id, web_contents, stream.Pass(), view_id,
199 expected_content_size);
201 #endif // !defined(ENABLE_EXTENSIONS)
203 #if !defined(OS_ANDROID)
204 void LaunchURL(const GURL& url, int render_process_id, int render_view_id) {
205 // If there is no longer a WebContents, the request may have raced with tab
206 // closing. Don't fire the external request. (It may have been a prerender.)
207 content::WebContents* web_contents =
208 tab_util::GetWebContentsByID(render_process_id, render_view_id);
209 if (!web_contents)
210 return;
212 // Do not launch external requests attached to unswapped prerenders.
213 prerender::PrerenderContents* prerender_contents =
214 prerender::PrerenderContents::FromWebContents(web_contents);
215 if (prerender_contents) {
216 prerender_contents->Destroy(prerender::FINAL_STATUS_UNSUPPORTED_SCHEME);
217 prerender::ReportPrerenderExternalURL();
218 return;
221 ExternalProtocolHandler::LaunchUrlWithDelegate(
222 url,
223 render_process_id,
224 render_view_id,
225 g_external_protocol_handler_delegate);
227 #endif // !defined(OS_ANDROID)
229 #if !defined(DISABLE_NACL)
230 void AppendComponentUpdaterThrottles(
231 net::URLRequest* request,
232 content::ResourceContext* resource_context,
233 ResourceType resource_type,
234 ScopedVector<content::ResourceThrottle>* throttles) {
235 const char* crx_id = NULL;
236 component_updater::ComponentUpdateService* cus =
237 g_browser_process->component_updater();
238 if (!cus)
239 return;
240 // Check for PNaCl pexe request.
241 if (resource_type == content::RESOURCE_TYPE_OBJECT) {
242 const net::HttpRequestHeaders& headers = request->extra_request_headers();
243 std::string accept_headers;
244 if (headers.GetHeader("Accept", &accept_headers)) {
245 if (accept_headers.find("application/x-pnacl") != std::string::npos &&
246 pnacl::NeedsOnDemandUpdate())
247 crx_id = "hnimpnehoodheedghdeeijklkeaacbdc";
251 if (crx_id) {
252 // We got a component we need to install, so throttle the resource
253 // until the component is installed.
254 throttles->push_back(
255 component_updater::GetOnDemandResourceThrottle(cus, crx_id));
258 #endif // !defined(DISABLE_NACL)
260 } // namespace
262 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate(
263 prerender::PrerenderTracker* prerender_tracker)
264 : download_request_limiter_(g_browser_process->download_request_limiter()),
265 safe_browsing_(g_browser_process->safe_browsing_service()),
266 #if defined(ENABLE_EXTENSIONS)
267 user_script_listener_(new extensions::UserScriptListener()),
268 #endif
269 prerender_tracker_(prerender_tracker) {
272 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() {
273 #if defined(ENABLE_EXTENSIONS)
274 CHECK(stream_target_info_.empty());
275 #endif
278 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest(
279 const std::string& method,
280 const GURL& url,
281 ResourceType resource_type,
282 content::ResourceContext* resource_context) {
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
285 // Handle a PREFETCH resource type. If prefetch is disabled, squelch the
286 // request. Otherwise, do a normal request to warm the cache.
287 if (resource_type == content::RESOURCE_TYPE_PREFETCH) {
288 // All PREFETCH requests should be GETs, but be defensive about it.
289 if (method != "GET")
290 return false;
292 // If prefetch is disabled, kill the request.
293 if (!prefetch::IsPrefetchEnabled(resource_context))
294 return false;
297 return true;
300 void ChromeResourceDispatcherHostDelegate::RequestBeginning(
301 net::URLRequest* request,
302 content::ResourceContext* resource_context,
303 content::AppCacheService* appcache_service,
304 ResourceType resource_type,
305 ScopedVector<content::ResourceThrottle>* throttles) {
306 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
307 bool is_prerendering =
308 info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender;
309 if (is_prerendering) {
310 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs)
311 // should remain at MAXIMUM_PRIORITY.
312 if (request->load_flags() & net::LOAD_IGNORE_LIMITS) {
313 DCHECK_EQ(request->priority(), net::MAXIMUM_PRIORITY);
314 } else {
315 request->SetPriority(net::IDLE);
319 ProfileIOData* io_data = ProfileIOData::FromResourceContext(
320 resource_context);
322 #if defined(OS_ANDROID)
323 // TODO(davidben): This is insufficient to integrate with prerender properly.
324 // https://crbug.com/370595
325 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME && !is_prerendering) {
326 throttles->push_back(
327 InterceptNavigationDelegate::CreateThrottleFor(request));
329 #else
330 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
331 // Redirect some navigations to apps that have registered matching URL
332 // handlers ('url_handlers' in the manifest).
333 content::ResourceThrottle* url_to_app_throttle =
334 AppUrlRedirector::MaybeCreateThrottleFor(request, io_data);
335 if (url_to_app_throttle)
336 throttles->push_back(url_to_app_throttle);
338 if (!is_prerendering) {
339 // Experimental: Launch ephemeral apps from search results.
340 content::ResourceThrottle* ephemeral_app_throttle =
341 EphemeralAppThrottle::MaybeCreateThrottleForLaunch(
342 request, io_data);
343 if (ephemeral_app_throttle)
344 throttles->push_back(ephemeral_app_throttle);
347 #endif
349 #if defined(OS_CHROMEOS)
350 // Check if we need to add offline throttle. This should be done only
351 // for main frames.
352 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
353 // We check offline first, then check safe browsing so that we still can
354 // block unsafe site after we remove offline page.
355 throttles->push_back(new OfflineResourceThrottle(request,
356 appcache_service));
359 // Check if we need to add merge session throttle. This throttle will postpone
360 // loading of main frames and XHR request.
361 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME ||
362 resource_type == content::RESOURCE_TYPE_XHR) {
363 // Add interstitial page while merge session process (cookie
364 // reconstruction from OAuth2 refresh token in ChromeOS login) is still in
365 // progress while we are attempting to load a google property.
366 if (!MergeSessionThrottle::AreAllSessionMergedAlready() &&
367 request->url().SchemeIsHTTPOrHTTPS()) {
368 throttles->push_back(new MergeSessionThrottle(request, resource_type));
371 #endif
373 // Don't attempt to append headers to requests that have already started.
374 // TODO(stevet): Remove this once the request ordering issues are resolved
375 // in crbug.com/128048.
376 if (!request->is_pending()) {
377 net::HttpRequestHeaders headers;
378 headers.CopyFrom(request->extra_request_headers());
379 bool is_off_the_record = io_data->IsOffTheRecord();
380 variations::VariationsHttpHeaderProvider::GetInstance()->
381 AppendHeaders(request->url(),
382 is_off_the_record,
383 !is_off_the_record &&
384 io_data->GetMetricsEnabledStateOnIOThread(),
385 &headers);
386 request->SetExtraRequestHeaders(headers);
389 #if defined(ENABLE_ONE_CLICK_SIGNIN)
390 AppendChromeSyncGaiaHeader(request, resource_context);
391 #endif
393 #if defined(ENABLE_CONFIGURATION_POLICY)
394 if (io_data->policy_header_helper())
395 io_data->policy_header_helper()->AddPolicyHeaders(request->url(), request);
396 #endif
398 signin::AppendMirrorRequestHeaderIfPossible(
399 request, GURL() /* redirect_url */, io_data);
401 AppendStandardResourceThrottles(request,
402 resource_context,
403 resource_type,
404 throttles);
405 #if !defined(DISABLE_NACL)
406 if (!is_prerendering) {
407 AppendComponentUpdaterThrottles(request,
408 resource_context,
409 resource_type,
410 throttles);
412 #endif
415 void ChromeResourceDispatcherHostDelegate::DownloadStarting(
416 net::URLRequest* request,
417 content::ResourceContext* resource_context,
418 int child_id,
419 int route_id,
420 int request_id,
421 bool is_content_initiated,
422 bool must_download,
423 ScopedVector<content::ResourceThrottle>* throttles) {
424 BrowserThread::PostTask(
425 BrowserThread::UI, FROM_HERE,
426 base::Bind(&NotifyDownloadInitiatedOnUI, child_id, route_id));
428 // If it's from the web, we don't trust it, so we push the throttle on.
429 if (is_content_initiated) {
430 throttles->push_back(
431 new DownloadResourceThrottle(download_request_limiter_.get(),
432 child_id,
433 route_id,
434 request->url(),
435 request->method()));
436 #if defined(OS_ANDROID)
437 throttles->push_back(
438 new chrome::InterceptDownloadResourceThrottle(
439 request, child_id, route_id, request_id));
440 #endif
443 // If this isn't a new request, we've seen this before and added the standard
444 // resource throttles already so no need to add it again.
445 if (!request->is_pending()) {
446 AppendStandardResourceThrottles(request,
447 resource_context,
448 content::RESOURCE_TYPE_MAIN_FRAME,
449 throttles);
453 ResourceDispatcherHostLoginDelegate*
454 ChromeResourceDispatcherHostDelegate::CreateLoginDelegate(
455 net::AuthChallengeInfo* auth_info, net::URLRequest* request) {
456 return CreateLoginPrompt(auth_info, request);
459 bool ChromeResourceDispatcherHostDelegate::HandleExternalProtocol(
460 const GURL& url,
461 int child_id,
462 int route_id) {
463 #if defined(OS_ANDROID)
464 // Android use a resource throttle to handle external as well as internal
465 // protocols.
466 return false;
467 #else
469 #if defined(ENABLE_EXTENSIONS)
470 if (extensions::WebViewRendererState::GetInstance()->IsGuest(child_id))
471 return false;
473 #endif // defined(ENABLE_EXTENSIONS)
475 BrowserThread::PostTask(BrowserThread::UI,
476 FROM_HERE,
477 base::Bind(&LaunchURL, url, child_id, route_id));
478 return true;
479 #endif
482 void ChromeResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
483 net::URLRequest* request,
484 content::ResourceContext* resource_context,
485 ResourceType resource_type,
486 ScopedVector<content::ResourceThrottle>* throttles) {
487 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
488 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
489 // Insert safe browsing at the front of the list, so it gets to decide on
490 // policies first.
491 if (io_data->safe_browsing_enabled()->GetValue()
492 #if defined(OS_ANDROID)
493 || io_data->IsDataReductionProxyEnabled()
494 #endif
496 bool is_subresource_request =
497 resource_type != content::RESOURCE_TYPE_MAIN_FRAME;
498 content::ResourceThrottle* throttle =
499 SafeBrowsingResourceThrottleFactory::Create(request,
500 resource_context,
501 is_subresource_request,
502 safe_browsing_.get());
503 if (throttle)
504 throttles->push_back(throttle);
506 #endif
508 #if defined(ENABLE_MANAGED_USERS)
509 bool is_subresource_request =
510 resource_type != content::RESOURCE_TYPE_MAIN_FRAME;
511 throttles->push_back(new SupervisedUserResourceThrottle(
512 request, !is_subresource_request,
513 io_data->supervised_user_url_filter()));
514 #endif
516 #if defined(ENABLE_EXTENSIONS)
517 content::ResourceThrottle* throttle =
518 user_script_listener_->CreateResourceThrottle(request->url(),
519 resource_type);
520 if (throttle)
521 throttles->push_back(throttle);
522 #endif
524 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
525 if (info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender) {
526 throttles->push_back(new prerender::PrerenderResourceThrottle(request));
528 if (prerender_tracker_->IsPendingSwapRequestOnIOThread(
529 info->GetChildID(), info->GetRenderFrameID(), request->url())) {
530 throttles->push_back(new prerender::PrerenderPendingSwapThrottle(
531 request, prerender_tracker_));
535 #if defined(ENABLE_ONE_CLICK_SIGNIN)
536 void ChromeResourceDispatcherHostDelegate::AppendChromeSyncGaiaHeader(
537 net::URLRequest* request,
538 content::ResourceContext* resource_context) {
539 static const char kAllowChromeSignIn[] = "Allow-Chrome-SignIn";
541 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
542 OneClickSigninHelper::Offer offer =
543 OneClickSigninHelper::CanOfferOnIOThread(request, io_data);
544 switch (offer) {
545 case OneClickSigninHelper::CAN_OFFER:
546 request->SetExtraRequestHeaderByName(kAllowChromeSignIn, "1", false);
547 break;
548 case OneClickSigninHelper::DONT_OFFER:
549 request->RemoveRequestHeaderByName(kAllowChromeSignIn);
550 break;
551 case OneClickSigninHelper::IGNORE_REQUEST:
552 break;
555 #endif
557 bool ChromeResourceDispatcherHostDelegate::ShouldForceDownloadResource(
558 const GURL& url, const std::string& mime_type) {
559 #if defined(ENABLE_EXTENSIONS)
560 // Special-case user scripts to get downloaded instead of viewed.
561 return extensions::UserScript::IsURLUserScript(url, mime_type);
562 #else
563 return false;
564 #endif
567 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
568 net::URLRequest* request,
569 const std::string& mime_type,
570 GURL* origin,
571 std::string* payload) {
572 #if defined(ENABLE_EXTENSIONS)
573 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
574 ProfileIOData* io_data =
575 ProfileIOData::FromResourceContext(info->GetContext());
576 bool profile_is_off_the_record = io_data->IsOffTheRecord();
577 const scoped_refptr<const extensions::InfoMap> extension_info_map(
578 io_data->GetExtensionInfoMap());
579 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist();
580 // Go through the white-listed extensions and try to use them to intercept
581 // the URL request.
582 for (size_t i = 0; i < whitelist.size(); ++i) {
583 const char* extension_id = whitelist[i].c_str();
584 const Extension* extension =
585 extension_info_map->extensions().GetByID(extension_id);
586 // The white-listed extension may not be installed, so we have to NULL check
587 // |extension|.
588 if (!extension ||
589 (profile_is_off_the_record &&
590 !extension_info_map->IsIncognitoEnabled(extension_id))) {
591 continue;
594 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
595 if (handler && handler->CanHandleMIMEType(mime_type)) {
596 StreamTargetInfo target_info;
597 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
598 target_info.extension_id = extension_id;
599 if (!handler->handler_url().empty()) {
600 target_info.view_id = base::GenerateGUID();
601 *payload = origin->spec() + handler->handler_url() +
602 "?id=" + target_info.view_id;
604 stream_target_info_[request] = target_info;
605 return true;
608 #endif
609 return false;
612 void ChromeResourceDispatcherHostDelegate::OnStreamCreated(
613 net::URLRequest* request,
614 scoped_ptr<content::StreamHandle> stream) {
615 #if defined(ENABLE_EXTENSIONS)
616 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
617 std::map<net::URLRequest*, StreamTargetInfo>::iterator ix =
618 stream_target_info_.find(request);
619 CHECK(ix != stream_target_info_.end());
620 content::BrowserThread::PostTask(
621 content::BrowserThread::UI, FROM_HERE,
622 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream),
623 request->GetExpectedContentSize(),
624 info->GetChildID(), info->GetRouteID(),
625 ix->second.extension_id, ix->second.view_id));
626 stream_target_info_.erase(request);
627 #endif
630 void ChromeResourceDispatcherHostDelegate::OnResponseStarted(
631 net::URLRequest* request,
632 content::ResourceContext* resource_context,
633 content::ResourceResponse* response,
634 IPC::Sender* sender) {
635 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
637 #if defined(OS_ANDROID)
638 // See if the response contains the X-Auto-Login header. If so, this was
639 // a request for a login page, and the server is allowing the browser to
640 // suggest auto-login, if available.
641 AutoLoginPrompter::ShowInfoBarIfPossible(request, info->GetChildID(),
642 info->GetRouteID());
643 #endif
645 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
647 #if defined(ENABLE_ONE_CLICK_SIGNIN)
648 // See if the response contains the Google-Accounts-SignIn header. If so,
649 // then the user has just finished signing in, and the server is allowing the
650 // browser to suggest connecting the user's profile to the account.
651 OneClickSigninHelper::ShowInfoBarIfPossible(request, io_data,
652 info->GetChildID(),
653 info->GetRouteID());
654 #endif
656 // See if the response contains the X-Chrome-Manage-Accounts header. If so
657 // show the profile avatar bubble so that user can complete signin/out action
658 // the native UI.
659 signin::ProcessMirrorResponseHeaderIfExists(request, io_data,
660 info->GetChildID(),
661 info->GetRouteID());
663 // Build in additional protection for the chrome web store origin.
664 GURL webstore_url(extension_urls::GetWebstoreLaunchURL());
665 if (request->url().DomainIs(webstore_url.host().c_str())) {
666 net::HttpResponseHeaders* response_headers = request->response_headers();
667 if (!response_headers->HasHeaderValue("x-frame-options", "deny") &&
668 !response_headers->HasHeaderValue("x-frame-options", "sameorigin")) {
669 response_headers->RemoveHeader("x-frame-options");
670 response_headers->AddHeader("x-frame-options: sameorigin");
674 // Ignores x-frame-options for the chrome signin UI.
675 const std::string request_spec(
676 request->first_party_for_cookies().GetOrigin().spec());
677 #if defined(OS_CHROMEOS)
678 if (request_spec == chrome::kChromeUIOobeURL ||
679 request_spec == chrome::kChromeUIChromeSigninURL) {
680 #else
681 if (request_spec == chrome::kChromeUIChromeSigninURL) {
682 #endif
683 net::HttpResponseHeaders* response_headers = request->response_headers();
684 if (response_headers && response_headers->HasHeader("x-frame-options"))
685 response_headers->RemoveHeader("x-frame-options");
688 prerender::URLRequestResponseStarted(request);
691 void ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
692 const GURL& redirect_url,
693 net::URLRequest* request,
694 content::ResourceContext* resource_context,
695 content::ResourceResponse* response) {
696 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
698 #if defined(ENABLE_ONE_CLICK_SIGNIN)
699 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
701 // See if the response contains the Google-Accounts-SignIn header. If so,
702 // then the user has just finished signing in, and the server is allowing the
703 // browser to suggest connecting the user's profile to the account.
704 OneClickSigninHelper::ShowInfoBarIfPossible(request, io_data,
705 info->GetChildID(),
706 info->GetRouteID());
707 AppendChromeSyncGaiaHeader(request, resource_context);
708 #endif
710 // In the Mirror world, Chrome should append a X-Chrome-Connected header to
711 // all Gaia requests from a connected profile so Gaia could return a 204
712 // response and let Chrome handle the action with native UI. The only
713 // exception is requests from gaia webview, since the native profile
714 // management UI is built on top of it.
715 signin::AppendMirrorRequestHeaderIfPossible(request, redirect_url, io_data);
717 #if defined(ENABLE_CONFIGURATION_POLICY)
718 if (io_data->policy_header_helper())
719 io_data->policy_header_helper()->AddPolicyHeaders(redirect_url, request);
720 #endif
723 // Notification that a request has completed.
724 void ChromeResourceDispatcherHostDelegate::RequestComplete(
725 net::URLRequest* url_request) {
726 // Jump on the UI thread and inform the prerender about the bytes.
727 const ResourceRequestInfo* info =
728 ResourceRequestInfo::ForRequest(url_request);
729 if (url_request && !url_request->was_cached()) {
730 BrowserThread::PostTask(BrowserThread::UI,
731 FROM_HERE,
732 base::Bind(&UpdatePrerenderNetworkBytesCallback,
733 info->GetChildID(),
734 info->GetRouteID(),
735 url_request->GetTotalReceivedBytes()));
739 // static
740 void ChromeResourceDispatcherHostDelegate::
741 SetExternalProtocolHandlerDelegateForTesting(
742 ExternalProtocolHandler::Delegate* delegate) {
743 g_external_protocol_handler_delegate = delegate;