Disable draw_properties benchmark on Android.
[chromium-blink-merge.git] / chrome / browser / renderer_host / chrome_resource_dispatcher_host_delegate.cc
blobf8f35851372015f209d20264123308098f6c205a
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/net/resource_prefetch_predictor_observer.h"
19 #include "chrome/browser/plugins/plugin_prefs.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_resource_throttle.h"
24 #include "chrome/browser/prerender/prerender_util.h"
25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_io_data.h"
27 #include "chrome/browser/renderer_host/safe_browsing_resource_throttle_factory.h"
28 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
29 #include "chrome/browser/signin/signin_header_helper.h"
30 #include "chrome/browser/tab_contents/tab_util.h"
31 #include "chrome/browser/ui/login/login_prompt.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/render_messages.h"
34 #include "chrome/common/url_constants.h"
35 #include "components/content_settings/core/browser/host_content_settings_map.h"
36 #include "components/google/core/browser/google_util.h"
37 #include "components/variations/net/variations_http_header_provider.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/plugin_service.h"
41 #include "content/public/browser/plugin_service_filter.h"
42 #include "content/public/browser/render_process_host.h"
43 #include "content/public/browser/render_view_host.h"
44 #include "content/public/browser/resource_context.h"
45 #include "content/public/browser/resource_dispatcher_host.h"
46 #include "content/public/browser/resource_request_info.h"
47 #include "content/public/browser/service_worker_context.h"
48 #include "content/public/browser/stream_info.h"
49 #include "content/public/browser/web_contents.h"
50 #include "content/public/common/resource_response.h"
51 #include "net/base/load_flags.h"
52 #include "net/base/load_timing_info.h"
53 #include "net/base/request_priority.h"
54 #include "net/http/http_response_headers.h"
55 #include "net/url_request/url_request.h"
57 #if !defined(DISABLE_NACL)
58 #include "chrome/browser/component_updater/pnacl/pnacl_component_installer.h"
59 #endif
61 #if defined(ENABLE_CONFIGURATION_POLICY)
62 #include "components/policy/core/common/cloud/policy_header_io_helper.h"
63 #endif
65 #if defined(ENABLE_EXTENSIONS)
66 #include "chrome/browser/apps/app_url_redirector.h"
67 #include "chrome/browser/apps/ephemeral_app_throttle.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/guest_view/web_view/web_view_renderer_state.h"
71 #include "extensions/browser/info_map.h"
72 #include "extensions/common/constants.h"
73 #include "extensions/common/extension_urls.h"
74 #include "extensions/common/manifest_handlers/mime_types_handler.h"
75 #include "extensions/common/user_script.h"
76 #endif
78 #if defined(ENABLE_SUPERVISED_USERS)
79 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h"
80 #endif
82 #if defined(USE_SYSTEM_PROTOBUF)
83 #include <google/protobuf/repeated_field.h>
84 #else
85 #include "third_party/protobuf/src/google/protobuf/repeated_field.h"
86 #endif
88 #if defined(OS_ANDROID)
89 #include "chrome/browser/android/intercept_download_resource_throttle.h"
90 #include "components/navigation_interception/intercept_navigation_delegate.h"
91 #endif
93 #if defined(ENABLE_DATA_REDUCTION_PROXY_DEBUGGING)
94 #include "components/data_reduction_proxy/content/browser/data_reduction_proxy_debug_resource_throttle.h"
95 #endif
97 #if defined(OS_CHROMEOS)
98 #include "chrome/browser/chromeos/login/signin/merge_session_throttle.h"
99 // TODO(oshima): Enable this for other platforms.
100 #include "chrome/browser/renderer_host/offline_resource_throttle.h"
101 #endif
103 using content::BrowserThread;
104 using content::RenderViewHost;
105 using content::ResourceDispatcherHostLoginDelegate;
106 using content::ResourceRequestInfo;
107 using content::ResourceType;
109 #if defined(ENABLE_EXTENSIONS)
110 using extensions::Extension;
111 using extensions::StreamsPrivateAPI;
112 #endif
114 #if defined(OS_ANDROID)
115 using navigation_interception::InterceptNavigationDelegate;
116 #endif
118 namespace {
120 ExternalProtocolHandler::Delegate* g_external_protocol_handler_delegate = NULL;
122 void NotifyDownloadInitiatedOnUI(int render_process_id, int render_view_id) {
123 RenderViewHost* rvh = RenderViewHost::FromID(render_process_id,
124 render_view_id);
125 if (!rvh)
126 return;
128 content::NotificationService::current()->Notify(
129 chrome::NOTIFICATION_DOWNLOAD_INITIATED,
130 content::Source<RenderViewHost>(rvh),
131 content::NotificationService::NoDetails());
134 prerender::PrerenderManager* GetPrerenderManager(int render_process_id,
135 int render_view_id) {
136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
138 content::WebContents* web_contents =
139 tab_util::GetWebContentsByID(render_process_id, render_view_id);
140 if (!web_contents)
141 return NULL;
143 content::BrowserContext* browser_context = web_contents->GetBrowserContext();
144 if (!browser_context)
145 return NULL;
147 Profile* profile = Profile::FromBrowserContext(browser_context);
148 if (!profile)
149 return NULL;
151 return prerender::PrerenderManagerFactory::GetForProfile(profile);
154 void UpdatePrerenderNetworkBytesCallback(int render_process_id,
155 int render_view_id,
156 int64 bytes) {
157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
159 content::WebContents* web_contents =
160 tab_util::GetWebContentsByID(render_process_id, render_view_id);
161 // PrerenderContents::FromWebContents handles the NULL case.
162 prerender::PrerenderContents* prerender_contents =
163 prerender::PrerenderContents::FromWebContents(web_contents);
165 if (prerender_contents)
166 prerender_contents->AddNetworkBytes(bytes);
168 prerender::PrerenderManager* prerender_manager =
169 GetPrerenderManager(render_process_id, render_view_id);
170 if (prerender_manager)
171 prerender_manager->AddProfileNetworkBytesIfEnabled(bytes);
174 #if defined(ENABLE_EXTENSIONS)
175 void SendExecuteMimeTypeHandlerEvent(scoped_ptr<content::StreamInfo> stream,
176 int64 expected_content_size,
177 int render_process_id,
178 int render_frame_id,
179 const std::string& extension_id,
180 const std::string& view_id,
181 bool embedded) {
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
184 content::WebContents* web_contents =
185 tab_util::GetWebContentsByFrameID(render_process_id, render_frame_id);
186 if (!web_contents)
187 return;
189 // If the request was for a prerender, abort the prerender and do not
190 // continue.
191 prerender::PrerenderContents* prerender_contents =
192 prerender::PrerenderContents::FromWebContents(web_contents);
193 if (prerender_contents) {
194 prerender_contents->Destroy(prerender::FINAL_STATUS_DOWNLOAD);
195 return;
198 Profile* profile =
199 Profile::FromBrowserContext(web_contents->GetBrowserContext());
201 StreamsPrivateAPI* streams_private = StreamsPrivateAPI::Get(profile);
202 if (!streams_private)
203 return;
204 streams_private->ExecuteMimeTypeHandler(
205 extension_id, web_contents, stream.Pass(), view_id, expected_content_size,
206 embedded, render_process_id, render_frame_id);
208 #endif // !defined(ENABLE_EXTENSIONS)
210 #if !defined(OS_ANDROID)
211 void LaunchURL(const GURL& url, int render_process_id, int render_view_id) {
212 // If there is no longer a WebContents, the request may have raced with tab
213 // closing. Don't fire the external request. (It may have been a prerender.)
214 content::WebContents* web_contents =
215 tab_util::GetWebContentsByID(render_process_id, render_view_id);
216 if (!web_contents)
217 return;
219 // Do not launch external requests attached to unswapped prerenders.
220 prerender::PrerenderContents* prerender_contents =
221 prerender::PrerenderContents::FromWebContents(web_contents);
222 if (prerender_contents) {
223 prerender_contents->Destroy(prerender::FINAL_STATUS_UNSUPPORTED_SCHEME);
224 prerender::ReportPrerenderExternalURL();
225 return;
228 ExternalProtocolHandler::LaunchUrlWithDelegate(
229 url,
230 render_process_id,
231 render_view_id,
232 g_external_protocol_handler_delegate);
234 #endif // !defined(OS_ANDROID)
236 #if !defined(DISABLE_NACL)
237 void AppendComponentUpdaterThrottles(
238 net::URLRequest* request,
239 content::ResourceContext* resource_context,
240 ResourceType resource_type,
241 ScopedVector<content::ResourceThrottle>* throttles) {
242 const char* crx_id = NULL;
243 component_updater::ComponentUpdateService* cus =
244 g_browser_process->component_updater();
245 if (!cus)
246 return;
247 // Check for PNaCl pexe request.
248 if (resource_type == content::RESOURCE_TYPE_OBJECT) {
249 const net::HttpRequestHeaders& headers = request->extra_request_headers();
250 std::string accept_headers;
251 if (headers.GetHeader("Accept", &accept_headers)) {
252 if (accept_headers.find("application/x-pnacl") != std::string::npos &&
253 pnacl::NeedsOnDemandUpdate())
254 crx_id = "hnimpnehoodheedghdeeijklkeaacbdc";
258 if (crx_id) {
259 // We got a component we need to install, so throttle the resource
260 // until the component is installed.
261 throttles->push_back(
262 component_updater::GetOnDemandResourceThrottle(cus, crx_id));
265 #endif // !defined(DISABLE_NACL)
267 } // namespace
269 ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate()
270 : download_request_limiter_(g_browser_process->download_request_limiter()),
271 safe_browsing_(g_browser_process->safe_browsing_service())
272 #if defined(ENABLE_EXTENSIONS)
273 , user_script_listener_(new extensions::UserScriptListener())
274 #endif
276 BrowserThread::PostTask(
277 BrowserThread::IO,
278 FROM_HERE,
279 base::Bind(content::ServiceWorkerContext::AddExcludedHeadersForFetchEvent,
280 variations::VariationsHttpHeaderProvider::GetInstance()
281 ->GetVariationHeaderNames()));
284 ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() {
285 #if defined(ENABLE_EXTENSIONS)
286 CHECK(stream_target_info_.empty());
287 #endif
290 bool ChromeResourceDispatcherHostDelegate::ShouldBeginRequest(
291 const std::string& method,
292 const GURL& url,
293 ResourceType resource_type,
294 content::ResourceContext* resource_context) {
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
297 // Handle a PREFETCH resource type. If prefetch is disabled, squelch the
298 // request. Otherwise, do a normal request to warm the cache.
299 if (resource_type == content::RESOURCE_TYPE_PREFETCH) {
300 // All PREFETCH requests should be GETs, but be defensive about it.
301 if (method != "GET")
302 return false;
304 // If prefetch is disabled, kill the request.
305 if (!prefetch::IsPrefetchEnabled(resource_context))
306 return false;
309 return true;
312 void ChromeResourceDispatcherHostDelegate::RequestBeginning(
313 net::URLRequest* request,
314 content::ResourceContext* resource_context,
315 content::AppCacheService* appcache_service,
316 ResourceType resource_type,
317 ScopedVector<content::ResourceThrottle>* throttles) {
318 if (safe_browsing_.get())
319 safe_browsing_->OnResourceRequest(request);
321 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
322 bool is_prerendering =
323 info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender;
324 if (is_prerendering) {
325 // Requests with the IGNORE_LIMITS flag set (i.e., sync XHRs)
326 // should remain at MAXIMUM_PRIORITY.
327 if (request->load_flags() & net::LOAD_IGNORE_LIMITS) {
328 DCHECK_EQ(request->priority(), net::MAXIMUM_PRIORITY);
329 } else {
330 request->SetPriority(net::IDLE);
334 ProfileIOData* io_data = ProfileIOData::FromResourceContext(
335 resource_context);
337 #if defined(OS_ANDROID)
338 // TODO(davidben): This is insufficient to integrate with prerender properly.
339 // https://crbug.com/370595
340 if (!is_prerendering) {
341 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
342 throttles->push_back(
343 InterceptNavigationDelegate::CreateThrottleFor(request));
344 } else if (resource_type == content::RESOURCE_TYPE_XHR) {
345 InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo(request);
348 #else
349 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
350 // Redirect some navigations to apps that have registered matching URL
351 // handlers ('url_handlers' in the manifest).
352 content::ResourceThrottle* url_to_app_throttle =
353 AppUrlRedirector::MaybeCreateThrottleFor(request, io_data);
354 if (url_to_app_throttle)
355 throttles->push_back(url_to_app_throttle);
357 if (!is_prerendering) {
358 // Experimental: Launch ephemeral apps from search results.
359 content::ResourceThrottle* ephemeral_app_throttle =
360 EphemeralAppThrottle::MaybeCreateThrottleForLaunch(
361 request, io_data);
362 if (ephemeral_app_throttle)
363 throttles->push_back(ephemeral_app_throttle);
366 #endif
368 #if defined(OS_CHROMEOS)
369 // Check if we need to add offline throttle. This should be done only
370 // for main frames.
371 // We will fall back to the old ChromeOS offline error page if the
372 // --disable-new-offline-error-page command-line switch is defined.
373 bool new_error_page_enabled = switches::NewOfflineErrorPageEnabled();
374 if (!new_error_page_enabled &&
375 resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
376 // We check offline first, then check safe browsing so that we still can
377 // block unsafe site after we remove offline page.
378 throttles->push_back(new OfflineResourceThrottle(request,
379 appcache_service));
382 // Check if we need to add merge session throttle. This throttle will postpone
383 // loading of main frames and XHR request.
384 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME ||
385 resource_type == content::RESOURCE_TYPE_XHR) {
386 // Add interstitial page while merge session process (cookie
387 // reconstruction from OAuth2 refresh token in ChromeOS login) is still in
388 // progress while we are attempting to load a google property.
389 if (!MergeSessionThrottle::AreAllSessionMergedAlready() &&
390 request->url().SchemeIsHTTPOrHTTPS()) {
391 throttles->push_back(new MergeSessionThrottle(request, resource_type));
394 #endif
396 // Don't attempt to append headers to requests that have already started.
397 // TODO(stevet): Remove this once the request ordering issues are resolved
398 // in crbug.com/128048.
399 if (!request->is_pending()) {
400 net::HttpRequestHeaders headers;
401 headers.CopyFrom(request->extra_request_headers());
402 bool is_off_the_record = io_data->IsOffTheRecord();
403 variations::VariationsHttpHeaderProvider::GetInstance()->
404 AppendHeaders(request->url(),
405 is_off_the_record,
406 !is_off_the_record &&
407 io_data->GetMetricsEnabledStateOnIOThread(),
408 &headers);
409 request->SetExtraRequestHeaders(headers);
412 #if defined(ENABLE_CONFIGURATION_POLICY)
413 if (io_data->policy_header_helper())
414 io_data->policy_header_helper()->AddPolicyHeaders(request->url(), request);
415 #endif
417 signin::AppendMirrorRequestHeaderIfPossible(
418 request, GURL() /* redirect_url */, io_data,
419 info->GetChildID(), info->GetRouteID());
421 AppendStandardResourceThrottles(request,
422 resource_context,
423 resource_type,
424 throttles);
425 #if !defined(DISABLE_NACL)
426 if (!is_prerendering) {
427 AppendComponentUpdaterThrottles(request,
428 resource_context,
429 resource_type,
430 throttles);
432 #endif
434 if (io_data->resource_prefetch_predictor_observer()) {
435 io_data->resource_prefetch_predictor_observer()->OnRequestStarted(
436 request, resource_type, info->GetChildID(), info->GetRenderFrameID());
440 void ChromeResourceDispatcherHostDelegate::DownloadStarting(
441 net::URLRequest* request,
442 content::ResourceContext* resource_context,
443 int child_id,
444 int route_id,
445 int request_id,
446 bool is_content_initiated,
447 bool must_download,
448 ScopedVector<content::ResourceThrottle>* throttles) {
449 BrowserThread::PostTask(
450 BrowserThread::UI, FROM_HERE,
451 base::Bind(&NotifyDownloadInitiatedOnUI, child_id, route_id));
453 // If it's from the web, we don't trust it, so we push the throttle on.
454 if (is_content_initiated) {
455 throttles->push_back(
456 new DownloadResourceThrottle(download_request_limiter_.get(),
457 child_id,
458 route_id,
459 request->url(),
460 request->method()));
461 #if defined(OS_ANDROID)
462 throttles->push_back(
463 new chrome::InterceptDownloadResourceThrottle(
464 request, child_id, route_id, request_id));
465 #endif
468 // If this isn't a new request, we've seen this before and added the standard
469 // resource throttles already so no need to add it again.
470 if (!request->is_pending()) {
471 AppendStandardResourceThrottles(request,
472 resource_context,
473 content::RESOURCE_TYPE_MAIN_FRAME,
474 throttles);
478 ResourceDispatcherHostLoginDelegate*
479 ChromeResourceDispatcherHostDelegate::CreateLoginDelegate(
480 net::AuthChallengeInfo* auth_info, net::URLRequest* request) {
481 return CreateLoginPrompt(auth_info, request);
484 bool ChromeResourceDispatcherHostDelegate::HandleExternalProtocol(
485 const GURL& url,
486 int child_id,
487 int route_id) {
488 #if defined(OS_ANDROID)
489 // Android use a resource throttle to handle external as well as internal
490 // protocols.
491 return false;
492 #else
494 #if defined(ENABLE_EXTENSIONS)
495 if (extensions::WebViewRendererState::GetInstance()->IsGuest(child_id))
496 return false;
498 #endif // defined(ENABLE_EXTENSIONS)
500 BrowserThread::PostTask(BrowserThread::UI,
501 FROM_HERE,
502 base::Bind(&LaunchURL, url, child_id, route_id));
503 return true;
504 #endif
507 void ChromeResourceDispatcherHostDelegate::AppendStandardResourceThrottles(
508 net::URLRequest* request,
509 content::ResourceContext* resource_context,
510 ResourceType resource_type,
511 ScopedVector<content::ResourceThrottle>* throttles) {
512 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
513 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
514 // Insert safe browsing at the front of the list, so it gets to decide on
515 // policies first.
516 if (io_data->safe_browsing_enabled()->GetValue()
517 #if defined(OS_ANDROID)
518 || io_data->IsDataReductionProxyEnabled()
519 #endif
521 content::ResourceThrottle* throttle =
522 SafeBrowsingResourceThrottleFactory::Create(request,
523 resource_context,
524 resource_type,
525 safe_browsing_.get());
526 if (throttle)
527 throttles->push_back(throttle);
529 #endif
531 #if defined(ENABLE_DATA_REDUCTION_PROXY_DEBUGGING)
532 scoped_ptr<content::ResourceThrottle> data_reduction_proxy_throttle =
533 data_reduction_proxy::DataReductionProxyDebugResourceThrottle::
534 MaybeCreate(
535 request, resource_type, io_data->data_reduction_proxy_io_data());
536 if (data_reduction_proxy_throttle)
537 throttles->push_back(data_reduction_proxy_throttle.release());
538 #endif
540 #if defined(ENABLE_SUPERVISED_USERS)
541 bool is_subresource_request =
542 resource_type != content::RESOURCE_TYPE_MAIN_FRAME;
543 throttles->push_back(new SupervisedUserResourceThrottle(
544 request, !is_subresource_request,
545 io_data->supervised_user_url_filter()));
546 #endif
548 #if defined(ENABLE_EXTENSIONS)
549 content::ResourceThrottle* throttle =
550 user_script_listener_->CreateResourceThrottle(request->url(),
551 resource_type);
552 if (throttle)
553 throttles->push_back(throttle);
554 #endif
556 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
557 if (info->GetVisibilityState() == blink::WebPageVisibilityStatePrerender) {
558 throttles->push_back(new prerender::PrerenderResourceThrottle(request));
562 bool ChromeResourceDispatcherHostDelegate::ShouldForceDownloadResource(
563 const GURL& url, const std::string& mime_type) {
564 #if defined(ENABLE_EXTENSIONS)
565 // Special-case user scripts to get downloaded instead of viewed.
566 return extensions::UserScript::IsURLUserScript(url, mime_type);
567 #else
568 return false;
569 #endif
572 bool ChromeResourceDispatcherHostDelegate::ShouldInterceptResourceAsStream(
573 net::URLRequest* request,
574 const base::FilePath& plugin_path,
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 (const std::string& extension_id : whitelist) {
589 const Extension* extension =
590 extension_info_map->extensions().GetByID(extension_id);
591 // The white-listed extension may not be installed, so we have to NULL check
592 // |extension|.
593 if (!extension ||
594 (profile_is_off_the_record &&
595 !extension_info_map->IsIncognitoEnabled(extension_id))) {
596 continue;
598 MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
599 // If the MimeHandlerView plugin to be loaded matches the extension,
600 // intercept the stream for that extension.
601 if (plugin_path ==
602 base::FilePath::FromUTF8Unsafe(extension->url().spec())) {
603 StreamTargetInfo target_info;
604 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
605 target_info.extension_id = extension_id;
606 DCHECK(!handler->handler_url().empty());
607 target_info.view_id = base::GenerateGUID();
608 *payload = target_info.view_id;
609 stream_target_info_[request] = target_info;
610 return true;
611 } else if (plugin_path.empty() && handler &&
612 handler->CanHandleMIMEType(mime_type)) {
613 // If no plugin path is provided, then we are trying to intercept the
614 // stream for the streamsPrivate API.
615 StreamTargetInfo target_info;
616 *origin = Extension::GetBaseURLFromExtensionId(extension_id);
617 target_info.extension_id = extension_id;
618 DCHECK(handler->handler_url().empty());
619 stream_target_info_[request] = target_info;
620 return true;
623 #endif
624 return false;
627 void ChromeResourceDispatcherHostDelegate::OnStreamCreated(
628 net::URLRequest* request,
629 scoped_ptr<content::StreamInfo> stream) {
630 #if defined(ENABLE_EXTENSIONS)
631 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
632 std::map<net::URLRequest*, StreamTargetInfo>::iterator ix =
633 stream_target_info_.find(request);
634 CHECK(ix != stream_target_info_.end());
635 bool embedded = info->GetResourceType() != content::RESOURCE_TYPE_MAIN_FRAME;
636 content::BrowserThread::PostTask(
637 content::BrowserThread::UI, FROM_HERE,
638 base::Bind(&SendExecuteMimeTypeHandlerEvent, base::Passed(&stream),
639 request->GetExpectedContentSize(), info->GetChildID(),
640 info->GetRenderFrameID(), ix->second.extension_id,
641 ix->second.view_id, embedded));
642 stream_target_info_.erase(request);
643 #endif
646 void ChromeResourceDispatcherHostDelegate::OnResponseStarted(
647 net::URLRequest* request,
648 content::ResourceContext* resource_context,
649 content::ResourceResponse* response,
650 IPC::Sender* sender) {
651 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
652 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
654 // See if the response contains the X-Chrome-Manage-Accounts header. If so
655 // show the profile avatar bubble so that user can complete signin/out action
656 // the native UI.
657 signin::ProcessMirrorResponseHeaderIfExists(request, io_data,
658 info->GetChildID(),
659 info->GetRouteID());
661 // Build in additional protection for the chrome web store origin.
662 #if defined(ENABLE_EXTENSIONS)
663 GURL webstore_url(extension_urls::GetWebstoreLaunchURL());
664 if (request->url().DomainIs(webstore_url.host().c_str())) {
665 net::HttpResponseHeaders* response_headers = request->response_headers();
666 if (!response_headers->HasHeaderValue("x-frame-options", "deny") &&
667 !response_headers->HasHeaderValue("x-frame-options", "sameorigin")) {
668 response_headers->RemoveHeader("x-frame-options");
669 response_headers->AddHeader("x-frame-options: sameorigin");
672 #endif
674 if (io_data->resource_prefetch_predictor_observer())
675 io_data->resource_prefetch_predictor_observer()->OnResponseStarted(request);
677 // Ignores x-frame-options for the chrome signin UI.
678 const std::string request_spec(
679 request->first_party_for_cookies().GetOrigin().spec());
680 #if defined(OS_CHROMEOS)
681 if (request_spec == chrome::kChromeUIOobeURL ||
682 request_spec == chrome::kChromeUIChromeSigninURL) {
683 #else
684 if (request_spec == chrome::kChromeUIChromeSigninURL) {
685 #endif
686 net::HttpResponseHeaders* response_headers = request->response_headers();
687 if (response_headers && response_headers->HasHeader("x-frame-options"))
688 response_headers->RemoveHeader("x-frame-options");
691 prerender::URLRequestResponseStarted(request);
694 void ChromeResourceDispatcherHostDelegate::OnRequestRedirected(
695 const GURL& redirect_url,
696 net::URLRequest* request,
697 content::ResourceContext* resource_context,
698 content::ResourceResponse* response) {
699 ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_context);
701 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
703 // In the Mirror world, Chrome should append a X-Chrome-Connected header to
704 // all Gaia requests from a connected profile so Gaia could return a 204
705 // response and let Chrome handle the action with native UI. The only
706 // exception is requests from gaia webview, since the native profile
707 // management UI is built on top of it.
708 signin::AppendMirrorRequestHeaderIfPossible(request, redirect_url, io_data,
709 info->GetChildID(), info->GetRouteID());
711 if (io_data->resource_prefetch_predictor_observer()) {
712 io_data->resource_prefetch_predictor_observer()->OnRequestRedirected(
713 redirect_url, request);
716 #if defined(ENABLE_CONFIGURATION_POLICY)
717 if (io_data->policy_header_helper())
718 io_data->policy_header_helper()->AddPolicyHeaders(redirect_url, request);
719 #endif
722 // Notification that a request has completed.
723 void ChromeResourceDispatcherHostDelegate::RequestComplete(
724 net::URLRequest* url_request) {
725 // Jump on the UI thread and inform the prerender about the bytes.
726 const ResourceRequestInfo* info =
727 ResourceRequestInfo::ForRequest(url_request);
728 if (url_request && !url_request->was_cached()) {
729 BrowserThread::PostTask(BrowserThread::UI,
730 FROM_HERE,
731 base::Bind(&UpdatePrerenderNetworkBytesCallback,
732 info->GetChildID(),
733 info->GetRouteID(),
734 url_request->GetTotalReceivedBytes()));
738 // static
739 void ChromeResourceDispatcherHostDelegate::
740 SetExternalProtocolHandlerDelegateForTesting(
741 ExternalProtocolHandler::Delegate* delegate) {
742 g_external_protocol_handler_delegate = delegate;