Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / renderer_host / chrome_resource_dispatcher_host_delegate.cc
blobb28332ea3809ee058b9b810f1b6d44be397b5319
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/download/download_request_limiter.h"
17 #include "chrome/browser/download/download_resource_throttle.h"
18 #include "chrome/browser/mod_pagespeed/mod_pagespeed_metrics.h"
19 #include "chrome/browser/net/resource_prefetch_predictor_observer.h"
20 #include "chrome/browser/plugins/plugin_prefs.h"
21 #include "chrome/browser/prefetch/prefetch.h"
22 #include "chrome/browser/prerender/prerender_manager.h"
23 #include "chrome/browser/prerender/prerender_manager_factory.h"
24 #include "chrome/browser/prerender/prerender_resource_throttle.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/data_reduction_proxy_resource_throttle_android.h"
29 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle.h"
30 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
31 #include "chrome/browser/signin/chrome_signin_helper.h"
32 #include "chrome/browser/tab_contents/tab_util.h"
33 #include "chrome/browser/ui/login/login_prompt.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/url_constants.h"
36 #include "components/content_settings/core/browser/host_content_settings_map.h"
37 #include "components/google/core/browser/google_util.h"
38 #include "components/variations/net/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/plugin_service.h"
42 #include "content/public/browser/plugin_service_filter.h"
43 #include "content/public/browser/render_process_host.h"
44 #include "content/public/browser/render_view_host.h"
45 #include "content/public/browser/resource_context.h"
46 #include "content/public/browser/resource_dispatcher_host.h"
47 #include "content/public/browser/resource_request_info.h"
48 #include "content/public/browser/service_worker_context.h"
49 #include "content/public/browser/stream_info.h"
50 #include "content/public/browser/web_contents.h"
51 #include "content/public/common/resource_response.h"
52 #include "net/base/load_flags.h"
53 #include "net/base/load_timing_info.h"
54 #include "net/base/request_priority.h"
55 #include "net/http/http_response_headers.h"
56 #include "net/url_request/url_request.h"
58 #if !defined(DISABLE_NACL)
59 #include "chrome/browser/component_updater/pnacl_component_installer.h"
60 #endif
62 #if defined(ENABLE_CONFIGURATION_POLICY)
63 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
64 #endif
66 #if defined(ENABLE_EXTENSIONS)
67 #include "chrome/browser/apps/app_url_redirector.h"
68 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
69 #include "chrome/browser/extensions/user_script_listener.h"
70 #include "extensions/browser/extension_throttle_manager.h"
71 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
72 #include "extensions/browser/info_map.h"
73 #include "extensions/common/constants.h"
74 #include "extensions/common/extension_urls.h"
75 #include "extensions/common/manifest_handlers/mime_types_handler.h"
76 #include "extensions/common/user_script.h"
77 #endif
79 #if defined(ENABLE_SUPERVISED_USERS)
80 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h"
81 #endif
83 #if defined(USE_SYSTEM_PROTOBUF)
84 #include <google/protobuf/repeated_field.h>
85 #else
86 #include "third_party/protobuf/src/google/protobuf/repeated_field.h"
87 #endif
89 #if defined(OS_ANDROID)
90 #include "chrome/browser/android/intercept_download_resource_throttle.h"
91 #include "components/navigation_interception/intercept_navigation_delegate.h"
92 #endif
94 #if defined(ENABLE_DATA_REDUCTION_PROXY_DEBUGGING)
95 #include "components/data_reduction_proxy/content/browser/data_reduction_proxy_debug_resource_throttle.h"
96 #endif
98 #if defined(OS_CHROMEOS)
99 #include "chrome/browser/chromeos/login/signin/merge_session_throttle.h"
100 #endif
102 using content::BrowserThread;
103 using content::RenderViewHost;
104 using content::ResourceDispatcherHostLoginDelegate;
105 using content::ResourceRequestInfo;
106 using content::ResourceType;
108 #if defined(ENABLE_EXTENSIONS)
109 using extensions::Extension;
110 using extensions::StreamsPrivateAPI;
111 #endif
113 #if defined(OS_ANDROID)
114 using navigation_interception::InterceptNavigationDelegate;
115 #endif
117 namespace {
119 ExternalProtocolHandler::Delegate* g_external_protocol_handler_delegate = NULL;
121 void NotifyDownloadInitiatedOnUI(int render_process_id, int render_view_id) {
122 RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
123 render_view_id);
124 if (!rvh)
125 return;
127 content::NotificationService::current()->Notify(
128 chrome::NOTIFICATION_DOWNLOAD_INITIATED,
129 content::Source<RenderViewHost>(rvh),
130 content::NotificationService::NoDetails());
133 prerender::PrerenderManager* GetPrerenderManager(int render_process_id,
134 int render_view_id) {
135 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
137 content::WebContents* web_contents =
138 tab_util::GetWebContentsByID(render_process_id, render_view_id);
139 if (!web_contents)
140 return NULL;
142 content::BrowserContext* browser_context = web_contents->GetBrowserContext();
143 if (!browser_context)
144 return NULL;
146 Profile* profile = Profile::FromBrowserContext(browser_context);
147 if (!profile)
148 return NULL;
150 return prerender::PrerenderManagerFactory::GetForProfile(profile);
153 void UpdatePrerenderNetworkBytesCallback(int render_process_id,
154 int render_view_id,
155 int64 bytes) {
156 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
158 content::WebContents* web_contents =
159 tab_util::GetWebContentsByID(render_process_id, render_view_id);
160 // PrerenderContents::FromWebContents handles the NULL case.
161 prerender::PrerenderContents* prerender_contents =
162 prerender::PrerenderContents::FromWebContents(web_contents);
164 if (prerender_contents)
165 prerender_contents->AddNetworkBytes(bytes);
167 prerender::PrerenderManager* prerender_manager =
168 GetPrerenderManager(render_process_id, render_view_id);
169 if (prerender_manager)
170 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes);
173 #if defined(ENABLE_EXTENSIONS)
174 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamInfo> stream,
175 int64 expected_content_size,
176 int render_process_id,
177 int render_frame_id,
178 const std::string& extension_id,
179 const std::string& view_id,
180 bool embedded) {
181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
183 content::WebContents* web_contents =
184 tab_util::GetWebContentsByFrameID(render_process_id, render_frame_id);
185 if (!web_contents)
186 return;
188 // If the request was for a prerender, abort the prerender and do not
189 // continue.
190 prerender::PrerenderContents* prerender_contents =
191 prerender::PrerenderContents::FromWebContents(web_contents);
192 if (prerender_contents) {
193 prerender_contents->Destroy(prerender::FINAL_STATUS_DOWNLOAD);
194 return;
197 Profile* profile =
198 Profile::FromBrowserContext(web_contents->GetBrowserContext());
200 StreamsPrivateAPI* streams_private = StreamsPrivateAPI::Get(profile);
201 if (!streams_private)
202 return;
203 streams_private->ExecuteMimeTypeHandler(
204 extension_id, web_contents, stream.Pass(), view_id, expected_content_size,
205 embedded, render_process_id, render_frame_id);
207 #endif // !defined(ENABLE_EXTENSIONS)
209 void LaunchURL(
210 const GURL& url,
211 int render_process_id,
212 int render_view_id,
213 ui::PageTransition page_transition,
214 bool has_user_gesture) {
215 // If there is no longer a WebContents, the request may have raced with tab
216 // closing. Don't fire the external request. (It may have been a prerender.)
217 content::WebContents* web_contents =
218 tab_util::GetWebContentsByID(render_process_id, render_view_id);
219 if (!web_contents)
220 return;
222 // Do not launch external requests attached to unswapped prerenders.
223 prerender::PrerenderContents* prerender_contents =
224 prerender::PrerenderContents::FromWebContents(web_contents);
225 if (prerender_contents) {
226 prerender_contents->Destroy(prerender::FINAL_STATUS_UNSUPPORTED_SCHEME);
227 prerender::ReportPrerenderExternalURL();
228 return;
231 ExternalProtocolHandler::LaunchUrlWithDelegate(
232 url,
233 render_process_id,
234 render_view_id,
235 page_transition,
236 has_user_gesture,
237 g_external_protocol_handler_delegate);
240 #if !defined(DISABLE_NACL)
241 void AppendComponentUpdaterThrottles(
242 net::URLRequest* request,
243 content::ResourceContext* resource_context,
244 ResourceType resource_type,
245 ScopedVector<content::ResourceThrottle>* throttles) {
246 const char* crx_id = NULL;
247 component_updater::ComponentUpdateService* cus =
248 g_browser_process->component_updater();
249 if (!cus)
250 return;
251 // Check for PNaCl pexe request.
252 if (resource_type == content::RESOURCE_TYPE_OBJECT) {
253 const net::HttpRequestHeaders& headers = request->extra_request_headers();
254 std::string accept_headers;
255 if (headers.GetHeader("Accept", &accept_headers)) {
256 if (accept_headers.find("application/x-pnacl") != std::string::npos &&
257 pnacl::NeedsOnDemandUpdate())
258 crx_id = "hnimpnehoodheedghdeeijklkeaacbdc";
262 if (crx_id) {
263 // We got a component we need to install, so throttle the resource
264 // until the component is installed.
265 throttles->push_back(
266 component_updater::GetOnDemandResourceThrottle(cus, crx_id));
269 #endif // !defined(DISABLE_NACL)
271 } // namespace
273 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate()
274 : download_request_limiter_(g_browser_process->download_request_limiter()),
275 safe_browsing_(g_browser_process->safe_browsing_service())
276 #if defined(ENABLE_EXTENSIONS)
277 , user_script_listener_(new extensions::UserScriptListener())
278 #endif
280 BrowserThread::PostTask(
281 BrowserThread::IO,
282 FROM_HERE,
283 base::Bind(content::ServiceWorkerContext::AddExcludedHeadersForFetchEvent,
284 variations::VariationsHttpHeaderProvider::GetInstance()
285 ->GetVariationHeaderNames()));
288 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() {
289 #if defined(ENABLE_EXTENSIONS)
290 CHECK(stream_target_info_.empty());
291 #endif
294 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest(
295 const std::string& method,
296 const GURL& url,
297 ResourceType resource_type,
298 content::ResourceContext* resource_context) {
299 DCHECK_CURRENTLY_ON(BrowserThread::IO);
301 // Handle a PREFETCH resource type. If prefetch is disabled, squelch the
302 // request. Otherwise, do a normal request to warm the cache.
303 if (resource_type == content::RESOURCE_TYPE_PREFETCH) {
304 // All PREFETCH requests should be GETs, but be defensive about it.
305 if (method != "GET")
306 return false;
308 // If prefetch is disabled, kill the request.
309 if (!prefetch::IsPrefetchEnabled(resource_context))
310 return false;
313 return true;
316 void ChromeResourceDispatcherHostDelegate::RequestBeginning(
317 net::URLRequest* request,
318 content::ResourceContext* resource_context,
319 content::AppCacheService* appcache_service,
320 ResourceType resource_type,
321 ScopedVector<content::ResourceThrottle>* throttles) {
322 if (safe_browsing_.get())
323 safe_browsing_->OnResourceRequest(request);
325 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
326 bool is_prerendering =
327 info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender;
328 if (is_prerendering) {
329 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs)
330 // should remain at MAXIMUM_PRIORITY.
331 if (request->load_flags() & net::LOAD_IGNORE_LIMITS) {
332 DCHECK_EQ(request->priority(), net::MAXIMUM_PRIORITY);
333 } else {
334 request->SetPriority(net::IDLE);
338 ProfileIOData* io_data = ProfileIOData::FromResourceContext(
339 resource_context);
341 #if defined(OS_ANDROID)
342 if (resource_type != content::RESOURCE_TYPE_MAIN_FRAME)
343 InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo(request);
344 #endif
346 #if defined(OS_CHROMEOS)
347 // Check if we need to add merge session throttle. This throttle will postpone
348 // loading of main frames and XHR request.
349 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME ||
350 resource_type == content::RESOURCE_TYPE_XHR) {
351 // Add interstitial page while merge session process (cookie
352 // reconstruction from OAuth2 refresh token in ChromeOS login) is still in
353 // progress while we are attempting to load a google property.
354 if (!MergeSessionThrottle::AreAllSessionMergedAlready() &&
355 request->url().SchemeIsHTTPOrHTTPS()) {
356 throttles->push_back(new MergeSessionThrottle(request, resource_type));
359 #endif
361 // Don't attempt to append headers to requests that have already started.
362 // TODO(stevet): Remove this once the request ordering issues are resolved
363 // in crbug.com/128048.
364 if (!request->is_pending()) {
365 net::HttpRequestHeaders headers;
366 headers.CopyFrom(request->extra_request_headers());
367 bool is_off_the_record = io_data->IsOffTheRecord();
368 variations::VariationsHttpHeaderProvider::GetInstance()->
369 AppendHeaders(request->url(),
370 is_off_the_record,
371 !is_off_the_record &&
372 io_data->GetMetricsEnabledStateOnIOThread(),
373 &headers);
374 request->SetExtraRequestHeaders(headers);
377 #if defined(ENABLE_CONFIGURATION_POLICY)
378 if (io_data->policy_header_helper())
379 io_data->policy_header_helper()->AddPolicyHeaders(request->url(), request);
380 #endif
382 signin::AppendMirrorRequestHeaderHelper(request, GURL() /* redirect_url */,
383 io_data, info->GetChildID(),
384 info->GetRouteID());
386 AppendStandardResourceThrottles(request,
387 resource_context,
388 resource_type,
389 throttles);
390 #if !defined(DISABLE_NACL)
391 if (!is_prerendering) {
392 AppendComponentUpdaterThrottles(request,
393 resource_context,
394 resource_type,
395 throttles);
397 #endif
399 if (io_data->resource_prefetch_predictor_observer()) {
400 io_data->resource_prefetch_predictor_observer()->OnRequestStarted(
401 request, resource_type, info->GetChildID(), info->GetRenderFrameID());
405 void ChromeResourceDispatcherHostDelegate::DownloadStarting(
406 net::URLRequest* request,
407 content::ResourceContext* resource_context,
408 int child_id,
409 int route_id,
410 int request_id,
411 bool is_content_initiated,
412 bool must_download,
413 ScopedVector<content::ResourceThrottle>* throttles) {
414 BrowserThread::PostTask(
415 BrowserThread::UI, FROM_HERE,
416 base::Bind(&NotifyDownloadInitiatedOnUI, child_id, route_id));
418 // If it's from the web, we don't trust it, so we push the throttle on.
419 if (is_content_initiated) {
420 throttles->push_back(new DownloadResourceThrottle(
421 download_request_limiter_, child_id, route_id, request->url(),
422 request->method()));
423 #if defined(OS_ANDROID)
424 throttles->push_back(
425 new chrome::InterceptDownloadResourceThrottle(
426 request, child_id, route_id, request_id));
427 #endif
430 // If this isn't a new request, we've seen this before and added the standard
431 // resource throttles already so no need to add it again.
432 if (!request->is_pending()) {
433 AppendStandardResourceThrottles(request,
434 resource_context,
435 content::RESOURCE_TYPE_MAIN_FRAME,
436 throttles);
440 ResourceDispatcherHostLoginDelegate*
441 ChromeResourceDispatcherHostDelegate::CreateLoginDelegate(
442 net::AuthChallengeInfo* auth_info, net::URLRequest* request) {
443 return CreateLoginPrompt(auth_info, request);
446 bool ChromeResourceDispatcherHostDelegate::HandleExternalProtocol(
447 const GURL& url,
448 int child_id,
449 int route_id,
450 bool is_main_frame,
451 ui::PageTransition page_transition,
452 bool has_user_gesture) {
453 #if defined(ENABLE_EXTENSIONS)
454 // External protocols are disabled for guests. An exception is made for the
455 // "mailto" protocol, so that pages that utilize it work properly in a
456 // WebView.
457 if (extensions::WebViewRendererState::GetInstance()->IsGuest(child_id) &&
458 !url.SchemeIs(url::kMailToScheme)) {
459 return false;
461 #endif // defined(ENABLE_EXTENSIONS)
463 #if defined(OS_ANDROID)
464 // Main frame external protocols are handled by
465 // InterceptNavigationResourceThrottle.
466 if (is_main_frame)
467 return false;
468 #endif // defined(ANDROID)
470 BrowserThread::PostTask(
471 BrowserThread::UI,
472 FROM_HERE,
473 base::Bind(&LaunchURL, url, child_id, route_id, page_transition,
474 has_user_gesture));
475 return true;
478 void ChromeResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
479 net::URLRequest* request,
480 content::ResourceContext* resource_context,
481 ResourceType resource_type,
482 ScopedVector<content::ResourceThrottle>* throttles) {
483 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
485 // Insert either safe browsing or data reduction proxy throttle at the front
486 // of the list, so one of them gets to decide if the resource is safe.
487 content::ResourceThrottle* first_throttle = NULL;
488 #if defined(OS_ANDROID) && defined(SAFE_BROWSING_SERVICE)
489 first_throttle = DataReductionProxyResourceThrottle::MaybeCreate(
490 request, resource_context, resource_type, safe_browsing_.get());
491 #endif // defined(OS_ANDROID) && defined(SAFE_BROWSING_SERVICE)
493 #if defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE)
494 if (!first_throttle && io_data->safe_browsing_enabled()->GetValue()) {
495 first_throttle = SafeBrowsingResourceThrottle::MaybeCreate(
496 request, resource_type, safe_browsing_.get());
498 #endif // defined(SAFE_BROWSING_DB_LOCAL) || defined(SAFE_BROWSING_DB_REMOTE)
500 if (first_throttle)
501 throttles->push_back(first_throttle);
503 #if defined(ENABLE_DATA_REDUCTION_PROXY_DEBUGGING)
504 scoped_ptr<content::ResourceThrottle> data_reduction_proxy_throttle =
505 data_reduction_proxy::DataReductionProxyDebugResourceThrottle::
506 MaybeCreate(
507 request, resource_type, io_data->data_reduction_proxy_io_data());
508 if (data_reduction_proxy_throttle)
509 throttles->push_back(data_reduction_proxy_throttle.Pass());
510 #endif
512 #if defined(ENABLE_SUPERVISED_USERS)
513 bool is_subresource_request =
514 resource_type != content::RESOURCE_TYPE_MAIN_FRAME;
515 throttles->push_back(new SupervisedUserResourceThrottle(
516 request, !is_subresource_request,
517 io_data->supervised_user_url_filter()));
518 #endif
520 #if defined(ENABLE_EXTENSIONS)
521 content::ResourceThrottle* wait_for_extensions_init_throttle =
522 user_script_listener_->CreateResourceThrottle(request->url(),
523 resource_type);
524 if (wait_for_extensions_init_throttle)
525 throttles->push_back(wait_for_extensions_init_throttle);
527 extensions::ExtensionThrottleManager* extension_throttle_manager =
528 io_data->GetExtensionThrottleManager();
529 if (extension_throttle_manager) {
530 scoped_ptr<content::ResourceThrottle> extension_throttle =
531 extension_throttle_manager->MaybeCreateThrottle(request);
532 if (extension_throttle)
533 throttles->push_back(extension_throttle.release());
535 #endif
537 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
538 if (info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender) {
539 throttles->push_back(new prerender::PrerenderResourceThrottle(request));
543 bool ChromeResourceDispatcherHostDelegate::ShouldForceDownloadResource(
544 const GURL& url, const std::string& mime_type) {
545 #if defined(ENABLE_EXTENSIONS)
546 // Special-case user scripts to get downloaded instead of viewed.
547 return extensions::UserScript::IsURLUserScript(url, mime_type);
548 #else
549 return false;
550 #endif
553 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
554 net::URLRequest* request,
555 const base::FilePath& plugin_path,
556 const std::string& mime_type,
557 GURL* origin,
558 std::string* payload) {
559 #if defined(ENABLE_EXTENSIONS)
560 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
561 ProfileIOData* io_data =
562 ProfileIOData::FromResourceContext(info->GetContext());
563 bool profile_is_off_the_record = io_data->IsOffTheRecord();
564 const scoped_refptr<const extensions::InfoMap> extension_info_map(
565 io_data->GetExtensionInfoMap());
566 std::vector<std::string> whitelist = MimeTypesHandler::GetMIMETypeWhitelist();
567 // Go through the white-listed extensions and try to use them to intercept
568 // the URL request.
569 for (const std::string& extension_id : whitelist) {
570 const Extension* extension =
571 extension_info_map->extensions().GetByID(extension_id);
572 // The white-listed extension may not be installed, so we have to NULL check
573 // |extension|.
574 if (!extension ||
575 (profile_is_off_the_record &&
576 !extension_info_map->IsIncognitoEnabled(extension_id))) {
577 continue;
579 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
580 if (!handler)
581 continue;
583 // If a plugin path is provided then a stream is being intercepted for the
584 // mimeHandlerPrivate API. Otherwise a stream is being intercepted for the
585 // streamsPrivate API.
586 if (!plugin_path.empty()) {
587 if (handler->HasPlugin() && plugin_path == handler->GetPluginPath()) {
588 StreamTargetInfo target_info;
589 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
590 target_info.extension_id = extension_id;
591 target_info.view_id = base::GenerateGUID();
592 *payload = target_info.view_id;
593 stream_target_info_[request] = target_info;
594 return true;
596 } else {
597 if (!handler->HasPlugin() && handler->CanHandleMIMEType(mime_type)) {
598 StreamTargetInfo target_info;
599 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
600 target_info.extension_id = extension_id;
601 stream_target_info_[request] = target_info;
602 return true;
606 #endif
607 return false;
610 void ChromeResourceDispatcherHostDelegate::OnStreamCreated(
611 net::URLRequest* request,
612 scoped_ptr<content::StreamInfo> stream) {
613 #if defined(ENABLE_EXTENSIONS)
614 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
615 std::map<net::URLRequest*, StreamTargetInfo>::iterator ix =
616 stream_target_info_.find(request);
617 CHECK(ix != stream_target_info_.end());
618 bool embedded = info->GetResourceType() != content::RESOURCE_TYPE_MAIN_FRAME;
619 content::BrowserThread::PostTask(
620 content::BrowserThread::UI, FROM_HERE,
621 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream),
622 request->GetExpectedContentSize(), info->GetChildID(),
623 info->GetRenderFrameID(), ix->second.extension_id,
624 ix->second.view_id, embedded));
625 stream_target_info_.erase(request);
626 #endif
629 void ChromeResourceDispatcherHostDelegate::OnResponseStarted(
630 net::URLRequest* request,
631 content::ResourceContext* resource_context,
632 content::ResourceResponse* response,
633 IPC::Sender* sender) {
634 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
635 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
637 // See if the response contains the X-Chrome-Manage-Accounts header. If so
638 // show the profile avatar bubble so that user can complete signin/out action
639 // the native UI.
640 signin::ProcessMirrorResponseHeaderIfExists(request, io_data,
641 info->GetChildID(),
642 info->GetRouteID());
644 // Built-in additional protection for the chrome web store origin.
645 #if defined(ENABLE_EXTENSIONS)
646 GURL webstore_url(extension_urls::GetWebstoreLaunchURL());
647 if (request->url().SchemeIsHTTPOrHTTPS() &&
648 request->url().DomainIs(webstore_url.host().c_str())) {
649 net::HttpResponseHeaders* response_headers = request->response_headers();
650 if (response_headers &&
651 !response_headers->HasHeaderValue("x-frame-options", "deny") &&
652 !response_headers->HasHeaderValue("x-frame-options", "sameorigin")) {
653 response_headers->RemoveHeader("x-frame-options");
654 response_headers->AddHeader("x-frame-options: sameorigin");
657 #endif
659 if (io_data->resource_prefetch_predictor_observer())
660 io_data->resource_prefetch_predictor_observer()->OnResponseStarted(request);
662 // Ignores x-frame-options for the chrome signin UI.
663 const std::string request_spec(
664 request->first_party_for_cookies().GetOrigin().spec());
665 #if defined(OS_CHROMEOS)
666 if (request_spec == chrome::kChromeUIOobeURL ||
667 request_spec == chrome::kChromeUIChromeSigninURL) {
668 #else
669 if (request_spec == chrome::kChromeUIChromeSigninURL) {
670 #endif
671 net::HttpResponseHeaders* response_headers = request->response_headers();
672 if (response_headers && response_headers->HasHeader("x-frame-options"))
673 response_headers->RemoveHeader("x-frame-options");
676 mod_pagespeed::RecordMetrics(info->GetResourceType(), request->url(),
677 request->response_headers());
680 void ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
681 const GURL& redirect_url,
682 net::URLRequest* request,
683 content::ResourceContext* resource_context,
684 content::ResourceResponse* response) {
685 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
687 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
689 // In the Mirror world, Chrome should append a X-Chrome-Connected header to
690 // all Gaia requests from a connected profile so Gaia could return a 204
691 // response and let Chrome handle the action with native UI. The only
692 // exception is requests from gaia webview, since the native profile
693 // management UI is built on top of it.
694 signin::AppendMirrorRequestHeaderHelper(
695 request, redirect_url, io_data, info->GetChildID(), info->GetRouteID());
697 if (io_data->resource_prefetch_predictor_observer()) {
698 io_data->resource_prefetch_predictor_observer()->OnRequestRedirected(
699 redirect_url, request);
702 #if defined(ENABLE_CONFIGURATION_POLICY)
703 if (io_data->policy_header_helper())
704 io_data->policy_header_helper()->AddPolicyHeaders(redirect_url, request);
705 #endif
708 // Notification that a request has completed.
709 void ChromeResourceDispatcherHostDelegate::RequestComplete(
710 net::URLRequest* url_request) {
711 // Jump on the UI thread and inform the prerender about the bytes.
712 const ResourceRequestInfo* info =
713 ResourceRequestInfo::ForRequest(url_request);
714 if (url_request && !url_request->was_cached()) {
715 BrowserThread::PostTask(BrowserThread::UI,
716 FROM_HERE,
717 base::Bind(&UpdatePrerenderNetworkBytesCallback,
718 info->GetChildID(),
719 info->GetRouteID(),
720 url_request->GetTotalReceivedBytes()));
724 // static
725 void ChromeResourceDispatcherHostDelegate::
726 SetExternalProtocolHandlerDelegateForTesting(
727 ExternalProtocolHandler::Delegate* delegate) {
728 g_external_protocol_handler_delegate = delegate;