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 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading
7 #include "content/browser/loader/resource_dispatcher_host_impl.h"
13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
15 #include "base/command_line.h"
16 #include "base/compiler_specific.h"
17 #include "base/debug/alias.h"
18 #include "base/logging.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/shared_memory.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/metrics/histogram.h"
23 #include "base/metrics/sparse_histogram.h"
24 #include "base/stl_util.h"
25 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
26 #include "content/browser/appcache/chrome_appcache_service.h"
27 #include "content/browser/cert_store_impl.h"
28 #include "content/browser/child_process_security_policy_impl.h"
29 #include "content/browser/cross_site_request_manager.h"
30 #include "content/browser/download/download_resource_handler.h"
31 #include "content/browser/download/save_file_manager.h"
32 #include "content/browser/download/save_file_resource_handler.h"
33 #include "content/browser/fileapi/chrome_blob_storage_context.h"
34 #include "content/browser/loader/async_resource_handler.h"
35 #include "content/browser/loader/buffered_resource_handler.h"
36 #include "content/browser/loader/cross_site_resource_handler.h"
37 #include "content/browser/loader/detachable_resource_handler.h"
38 #include "content/browser/loader/power_save_block_resource_throttle.h"
39 #include "content/browser/loader/redirect_to_file_resource_handler.h"
40 #include "content/browser/loader/resource_message_filter.h"
41 #include "content/browser/loader/resource_request_info_impl.h"
42 #include "content/browser/loader/stream_resource_handler.h"
43 #include "content/browser/loader/sync_resource_handler.h"
44 #include "content/browser/loader/throttling_resource_handler.h"
45 #include "content/browser/loader/upload_data_stream_builder.h"
46 #include "content/browser/plugin_service_impl.h"
47 #include "content/browser/renderer_host/render_view_host_delegate.h"
48 #include "content/browser/renderer_host/render_view_host_impl.h"
49 #include "content/browser/resource_context_impl.h"
50 #include "content/browser/streams/stream.h"
51 #include "content/browser/streams/stream_context.h"
52 #include "content/browser/streams/stream_registry.h"
53 #include "content/browser/worker_host/worker_service_impl.h"
54 #include "content/browser/web_contents/web_contents_impl.h"
55 #include "content/common/resource_messages.h"
56 #include "content/common/ssl_status_serialization.h"
57 #include "content/common/view_messages.h"
58 #include "content/public/browser/browser_thread.h"
59 #include "content/public/browser/content_browser_client.h"
60 #include "content/public/browser/download_manager.h"
61 #include "content/public/browser/download_url_parameters.h"
62 #include "content/public/browser/global_request_id.h"
63 #include "content/public/browser/resource_dispatcher_host_delegate.h"
64 #include "content/public/browser/resource_request_details.h"
65 #include "content/public/browser/resource_throttle.h"
66 #include "content/public/browser/stream_handle.h"
67 #include "content/public/browser/user_metrics.h"
68 #include "content/public/common/content_switches.h"
69 #include "content/public/common/process_type.h"
70 #include "content/public/common/url_constants.h"
71 #include "ipc/ipc_message_macros.h"
72 #include "ipc/ipc_message_start.h"
73 #include "net/base/auth.h"
74 #include "net/base/load_flags.h"
75 #include "net/base/mime_util.h"
76 #include "net/base/net_errors.h"
77 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
78 #include "net/base/request_priority.h"
79 #include "net/base/upload_data_stream.h"
80 #include "net/cert/cert_status_flags.h"
81 #include "net/cookies/cookie_monster.h"
82 #include "net/http/http_response_headers.h"
83 #include "net/http/http_response_info.h"
84 #include "net/ssl/ssl_cert_request_info.h"
85 #include "net/url_request/url_request.h"
86 #include "net/url_request/url_request_context.h"
87 #include "net/url_request/url_request_job_factory.h"
88 #include "webkit/browser/appcache/appcache_interceptor.h"
89 #include "webkit/common/blob/blob_data.h"
90 #include "webkit/browser/blob/blob_data_handle.h"
91 #include "webkit/browser/blob/blob_storage_context.h"
92 #include "webkit/browser/blob/blob_url_request_job_factory.h"
93 #include "webkit/browser/fileapi/file_permission_policy.h"
94 #include "webkit/browser/fileapi/file_system_context.h"
95 #include "webkit/common/appcache/appcache_interfaces.h"
96 #include "webkit/common/blob/shareable_file_reference.h"
97 #include "webkit/common/resource_request_body.h"
100 using base::TimeDelta
;
101 using base::TimeTicks
;
102 using webkit_blob::ShareableFileReference
;
103 using webkit_glue::ResourceRequestBody
;
105 // ----------------------------------------------------------------------------
111 static ResourceDispatcherHostImpl
* g_resource_dispatcher_host
;
113 // The interval for calls to ResourceDispatcherHostImpl::UpdateLoadStates
114 const int kUpdateLoadStatesIntervalMsec
= 100;
116 // Maximum byte "cost" of all the outstanding requests for a renderer.
117 // See delcaration of |max_outstanding_requests_cost_per_process_| for details.
118 // This bound is 25MB, which allows for around 6000 outstanding requests.
119 const int kMaxOutstandingRequestsCostPerProcess
= 26214400;
121 // The number of milliseconds after noting a user gesture that we will
122 // tag newly-created URLRequest objects with the
123 // net::LOAD_MAYBE_USER_GESTURE load flag. This is a fairly arbitrary
124 // guess at how long to expect direct impact from a user gesture, but
125 // this should be OK as the load flag is a best-effort thing only,
126 // rather than being intended as fully accurate.
127 const int kUserGestureWindowMs
= 3500;
129 // Ratio of |max_num_in_flight_requests_| that any one renderer is allowed to
130 // use. Arbitrarily chosen.
131 const double kMaxRequestsPerProcessRatio
= 0.45;
133 // TODO(jkarlin): The value is high to reduce the chance of the detachable
134 // request timing out, forcing a blocked second request to open a new connection
135 // and start over. Reduce this value once we have a better idea of what it
136 // should be and once we stop blocking multiple simultaneous requests for the
137 // same resource (see bugs 46104 and 31014).
138 const int kDefaultDetachableCancelDelayMs
= 30000;
140 bool IsDetachableResourceType(ResourceType::Type type
) {
142 case ResourceType::PREFETCH
:
143 case ResourceType::PING
:
150 // Aborts a request before an URLRequest has actually been created.
151 void AbortRequestBeforeItStarts(ResourceMessageFilter
* filter
,
152 IPC::Message
* sync_result
,
155 SyncLoadResult result
;
156 result
.error_code
= net::ERR_ABORTED
;
157 ResourceHostMsg_SyncLoad::WriteReplyParams(sync_result
, result
);
158 filter
->Send(sync_result
);
160 // Tell the renderer that this request was disallowed.
161 ResourceMsg_RequestCompleteData request_complete_data
;
162 request_complete_data
.error_code
= net::ERR_ABORTED
;
163 request_complete_data
.was_ignored_by_handler
= false;
164 request_complete_data
.exists_in_cache
= false;
165 // No security info needed, connection not established.
166 request_complete_data
.completion_time
= base::TimeTicks();
167 request_complete_data
.encoded_data_length
= 0;
168 filter
->Send(new ResourceMsg_RequestComplete(
169 request_id
, request_complete_data
));
173 void SetReferrerForRequest(net::URLRequest
* request
, const Referrer
& referrer
) {
174 if (!referrer
.url
.is_valid() ||
175 CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoReferrers
)) {
176 request
->SetReferrer(std::string());
178 request
->SetReferrer(referrer
.url
.spec());
181 net::URLRequest::ReferrerPolicy net_referrer_policy
=
182 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
;
183 switch (referrer
.policy
) {
184 case blink::WebReferrerPolicyDefault
:
185 net_referrer_policy
=
186 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
;
188 case blink::WebReferrerPolicyAlways
:
189 case blink::WebReferrerPolicyNever
:
190 case blink::WebReferrerPolicyOrigin
:
191 net_referrer_policy
= net::URLRequest::NEVER_CLEAR_REFERRER
;
194 request
->set_referrer_policy(net_referrer_policy
);
197 // Consults the RendererSecurity policy to determine whether the
198 // ResourceDispatcherHostImpl should service this request. A request might be
199 // disallowed if the renderer is not authorized to retrieve the request URL or
200 // if the renderer is attempting to upload an unauthorized file.
201 bool ShouldServiceRequest(int process_type
,
203 const ResourceHostMsg_Request
& request_data
,
204 fileapi::FileSystemContext
* file_system_context
) {
205 if (process_type
== PROCESS_TYPE_PLUGIN
)
208 ChildProcessSecurityPolicyImpl
* policy
=
209 ChildProcessSecurityPolicyImpl::GetInstance();
211 // Check if the renderer is permitted to request the requested URL.
212 if (!policy
->CanRequestURL(child_id
, request_data
.url
)) {
213 VLOG(1) << "Denied unauthorized request for "
214 << request_data
.url
.possibly_invalid_spec();
218 // Check if the renderer is permitted to upload the requested files.
219 if (request_data
.request_body
.get()) {
220 const std::vector
<ResourceRequestBody::Element
>* uploads
=
221 request_data
.request_body
->elements();
222 std::vector
<ResourceRequestBody::Element
>::const_iterator iter
;
223 for (iter
= uploads
->begin(); iter
!= uploads
->end(); ++iter
) {
224 if (iter
->type() == ResourceRequestBody::Element::TYPE_FILE
&&
225 !policy
->CanReadFile(child_id
, iter
->path())) {
226 NOTREACHED() << "Denied unauthorized upload of "
227 << iter
->path().value();
230 if (iter
->type() == ResourceRequestBody::Element::TYPE_FILE_FILESYSTEM
) {
231 fileapi::FileSystemURL url
=
232 file_system_context
->CrackURL(iter
->filesystem_url());
233 if (!policy
->CanReadFileSystemFile(child_id
, url
)) {
234 NOTREACHED() << "Denied unauthorized upload of "
235 << iter
->filesystem_url().spec();
245 void RemoveDownloadFileFromChildSecurityPolicy(int child_id
,
246 const base::FilePath
& path
) {
247 ChildProcessSecurityPolicyImpl::GetInstance()->RevokeAllPermissionsForFile(
252 #pragma warning(disable: 4748)
253 #pragma optimize("", off)
257 #pragma optimize("", on)
258 #pragma warning(default: 4748)
261 DownloadInterruptReason
CallbackAndReturn(
262 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
263 DownloadInterruptReason interrupt_reason
) {
264 if (started_cb
.is_null())
265 return interrupt_reason
;
266 BrowserThread::PostTask(
270 started_cb
, static_cast<DownloadItem
*>(NULL
), interrupt_reason
));
272 return interrupt_reason
;
275 int GetCertID(net::URLRequest
* request
, int child_id
) {
276 if (request
->ssl_info().cert
.get()) {
277 return CertStore::GetInstance()->StoreCert(request
->ssl_info().cert
.get(),
283 void NotifyRedirectOnUI(int render_process_id
,
284 int render_frame_host
,
285 scoped_ptr
<ResourceRedirectDetails
> details
) {
286 RenderFrameHostImpl
* host
=
287 RenderFrameHostImpl::FromID(render_process_id
, render_frame_host
);
288 WebContentsImpl
* web_contents
=
289 static_cast<WebContentsImpl
*>(WebContents::FromRenderFrameHost(host
));
292 web_contents
->DidGetRedirectForResourceRequest(
293 host
->render_view_host(), *details
.get());
296 void NotifyResponseOnUI(int render_process_id
,
297 int render_frame_host
,
298 scoped_ptr
<ResourceRequestDetails
> details
) {
299 RenderFrameHostImpl
* host
=
300 RenderFrameHostImpl::FromID(render_process_id
, render_frame_host
);
301 WebContentsImpl
* web_contents
=
302 static_cast<WebContentsImpl
*>(WebContents::FromRenderFrameHost(host
));
305 web_contents
->DidGetResourceResponseStart(*details
.get());
309 const net::SignedCertificateTimestampAndStatus
& sct_status
) {
310 return sct_status
.status
== net::ct::SCT_STATUS_OK
;
316 ResourceDispatcherHost
* ResourceDispatcherHost::Get() {
317 return g_resource_dispatcher_host
;
320 ResourceDispatcherHostImpl::ResourceDispatcherHostImpl()
321 : save_file_manager_(new SaveFileManager()),
324 num_in_flight_requests_(0),
325 max_num_in_flight_requests_(base::SharedMemory::GetHandleLimit()),
326 max_num_in_flight_requests_per_process_(
328 max_num_in_flight_requests_
* kMaxRequestsPerProcessRatio
)),
329 max_outstanding_requests_cost_per_process_(
330 kMaxOutstandingRequestsCostPerProcess
),
333 allow_cross_origin_auth_prompt_(false) {
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
335 DCHECK(!g_resource_dispatcher_host
);
336 g_resource_dispatcher_host
= this;
338 GetContentClient()->browser()->ResourceDispatcherHostCreated();
340 ANNOTATE_BENIGN_RACE(
341 &last_user_gesture_time_
,
342 "We don't care about the precise value, see http://crbug.com/92889");
344 BrowserThread::PostTask(BrowserThread::IO
,
346 base::Bind(&ResourceDispatcherHostImpl::OnInit
,
347 base::Unretained(this)));
349 update_load_states_timer_
.reset(
350 new base::RepeatingTimer
<ResourceDispatcherHostImpl
>());
353 ResourceDispatcherHostImpl::~ResourceDispatcherHostImpl() {
354 DCHECK(outstanding_requests_stats_map_
.empty());
355 DCHECK(g_resource_dispatcher_host
);
356 g_resource_dispatcher_host
= NULL
;
360 ResourceDispatcherHostImpl
* ResourceDispatcherHostImpl::Get() {
361 return g_resource_dispatcher_host
;
364 void ResourceDispatcherHostImpl::SetDelegate(
365 ResourceDispatcherHostDelegate
* delegate
) {
366 delegate_
= delegate
;
369 void ResourceDispatcherHostImpl::SetAllowCrossOriginAuthPrompt(bool value
) {
370 allow_cross_origin_auth_prompt_
= value
;
373 void ResourceDispatcherHostImpl::AddResourceContext(ResourceContext
* context
) {
374 active_resource_contexts_
.insert(context
);
377 void ResourceDispatcherHostImpl::RemoveResourceContext(
378 ResourceContext
* context
) {
379 CHECK(ContainsKey(active_resource_contexts_
, context
));
380 active_resource_contexts_
.erase(context
);
383 void ResourceDispatcherHostImpl::CancelRequestsForContext(
384 ResourceContext
* context
) {
385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
388 CHECK(ContainsKey(active_resource_contexts_
, context
));
390 // Note that request cancellation has side effects. Therefore, we gather all
391 // the requests to cancel first, and then we start cancelling. We assert at
392 // the end that there are no more to cancel since the context is about to go
394 typedef std::vector
<linked_ptr
<ResourceLoader
> > LoaderList
;
395 LoaderList loaders_to_cancel
;
397 for (LoaderMap::iterator i
= pending_loaders_
.begin();
398 i
!= pending_loaders_
.end();) {
399 if (i
->second
->GetRequestInfo()->GetContext() == context
) {
400 loaders_to_cancel
.push_back(i
->second
);
401 IncrementOutstandingRequestsMemory(-1, *i
->second
->GetRequestInfo());
402 pending_loaders_
.erase(i
++);
408 for (BlockedLoadersMap::iterator i
= blocked_loaders_map_
.begin();
409 i
!= blocked_loaders_map_
.end();) {
410 BlockedLoadersList
* loaders
= i
->second
;
411 if (loaders
->empty()) {
412 // This can happen if BlockRequestsForRoute() has been called for a route,
413 // but we haven't blocked any matching requests yet.
417 ResourceRequestInfoImpl
* info
= loaders
->front()->GetRequestInfo();
418 if (info
->GetContext() == context
) {
419 blocked_loaders_map_
.erase(i
++);
420 for (BlockedLoadersList::const_iterator it
= loaders
->begin();
421 it
!= loaders
->end(); ++it
) {
422 linked_ptr
<ResourceLoader
> loader
= *it
;
423 info
= loader
->GetRequestInfo();
424 // We make the assumption that all requests on the list have the same
426 DCHECK_EQ(context
, info
->GetContext());
427 IncrementOutstandingRequestsMemory(-1, *info
);
428 loaders_to_cancel
.push_back(loader
);
437 for (LoaderList::iterator i
= loaders_to_cancel
.begin();
438 i
!= loaders_to_cancel
.end(); ++i
) {
439 // There is no strict requirement that this be the case, but currently
440 // downloads, streams, detachable requests, and transferred requests are the
441 // only requests that aren't cancelled when the associated processes go
442 // away. It may be OK for this invariant to change in the future, but if
443 // this assertion fires without the invariant changing, then it's indicative
445 DCHECK((*i
)->GetRequestInfo()->IsDownload() ||
446 (*i
)->GetRequestInfo()->is_stream() ||
447 ((*i
)->GetRequestInfo()->detachable_handler() &&
448 (*i
)->GetRequestInfo()->detachable_handler()->is_detached()) ||
449 (*i
)->is_transferring());
453 loaders_to_cancel
.clear();
455 // Validate that no more requests for this context were added.
456 for (LoaderMap::const_iterator i
= pending_loaders_
.begin();
457 i
!= pending_loaders_
.end(); ++i
) {
458 // http://crbug.com/90971
459 CHECK_NE(i
->second
->GetRequestInfo()->GetContext(), context
);
462 for (BlockedLoadersMap::const_iterator i
= blocked_loaders_map_
.begin();
463 i
!= blocked_loaders_map_
.end(); ++i
) {
464 BlockedLoadersList
* loaders
= i
->second
;
465 if (!loaders
->empty()) {
466 ResourceRequestInfoImpl
* info
= loaders
->front()->GetRequestInfo();
467 // http://crbug.com/90971
468 CHECK_NE(info
->GetContext(), context
);
473 DownloadInterruptReason
ResourceDispatcherHostImpl::BeginDownload(
474 scoped_ptr
<net::URLRequest
> request
,
475 const Referrer
& referrer
,
476 bool is_content_initiated
,
477 ResourceContext
* context
,
481 scoped_ptr
<DownloadSaveInfo
> save_info
,
483 const DownloadStartedCallback
& started_callback
) {
485 return CallbackAndReturn(started_callback
,
486 DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN
);
488 const GURL
& url
= request
->original_url();
490 // http://crbug.com/90971
492 base::strlcpy(url_buf
, url
.spec().c_str(), arraysize(url_buf
));
493 base::debug::Alias(url_buf
);
494 CHECK(ContainsKey(active_resource_contexts_
, context
));
496 SetReferrerForRequest(request
.get(), referrer
);
498 int extra_load_flags
= net::LOAD_IS_DOWNLOAD
;
500 // If there is upload data attached, only retrieve from cache because there
501 // is no current mechanism to prompt the user for their consent for a
502 // re-post. For GETs, try to retrieve data from the cache and skip
503 // validating the entry if present.
504 if (request
->get_upload() != NULL
)
505 extra_load_flags
|= net::LOAD_ONLY_FROM_CACHE
;
507 extra_load_flags
|= net::LOAD_PREFERRING_CACHE
;
509 extra_load_flags
|= net::LOAD_DISABLE_CACHE
;
511 request
->SetLoadFlags(request
->load_flags() | extra_load_flags
);
513 // No need to get offline load flags for downloads, but make sure
514 // we have an OfflinePolicy to receive request completions.
515 GlobalRoutingID
id(child_id
, route_id
);
516 if (!offline_policy_map_
[id
])
517 offline_policy_map_
[id
] = new OfflinePolicy();
519 // Check if the renderer is permitted to request the requested URL.
520 if (!ChildProcessSecurityPolicyImpl::GetInstance()->
521 CanRequestURL(child_id
, url
)) {
522 VLOG(1) << "Denied unauthorized download request for "
523 << url
.possibly_invalid_spec();
524 return CallbackAndReturn(started_callback
,
525 DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST
);
530 const net::URLRequestContext
* request_context
= context
->GetRequestContext();
531 if (!request_context
->job_factory()->IsHandledURL(url
)) {
532 VLOG(1) << "Download request for unsupported protocol: "
533 << url
.possibly_invalid_spec();
534 return CallbackAndReturn(started_callback
,
535 DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST
);
538 ResourceRequestInfoImpl
* extra_info
=
539 CreateRequestInfo(child_id
, route_id
, true, context
);
540 extra_info
->AssociateWithRequest(request
.get()); // Request takes ownership.
542 if (request
->url().SchemeIs(chrome::kBlobScheme
)) {
543 ChromeBlobStorageContext
* blob_context
=
544 GetChromeBlobStorageContextForResourceContext(context
);
545 webkit_blob::BlobProtocolHandler::SetRequestedBlobDataHandle(
547 blob_context
->context()->GetBlobDataFromPublicURL(request
->url()));
550 // From this point forward, the |DownloadResourceHandler| is responsible for
551 // |started_callback|.
552 scoped_ptr
<ResourceHandler
> handler(
553 CreateResourceHandlerForDownload(request
.get(), is_content_initiated
,
554 true, download_id
, save_info
.Pass(),
557 BeginRequestInternal(request
.Pass(), handler
.Pass());
559 return DOWNLOAD_INTERRUPT_REASON_NONE
;
562 void ResourceDispatcherHostImpl::ClearLoginDelegateForRequest(
563 net::URLRequest
* request
) {
564 ResourceRequestInfoImpl
* info
= ResourceRequestInfoImpl::ForRequest(request
);
566 ResourceLoader
* loader
= GetLoader(info
->GetGlobalRequestID());
568 loader
->ClearLoginDelegate();
572 void ResourceDispatcherHostImpl::Shutdown() {
573 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
574 BrowserThread::PostTask(BrowserThread::IO
,
576 base::Bind(&ResourceDispatcherHostImpl::OnShutdown
,
577 base::Unretained(this)));
580 scoped_ptr
<ResourceHandler
>
581 ResourceDispatcherHostImpl::CreateResourceHandlerForDownload(
582 net::URLRequest
* request
,
583 bool is_content_initiated
,
586 scoped_ptr
<DownloadSaveInfo
> save_info
,
587 const DownloadUrlParameters::OnStartedCallback
& started_cb
) {
588 scoped_ptr
<ResourceHandler
> handler(
589 new DownloadResourceHandler(id
, request
, started_cb
, save_info
.Pass()));
591 const ResourceRequestInfo
* request_info(
592 ResourceRequestInfo::ForRequest(request
));
594 ScopedVector
<ResourceThrottle
> throttles
;
595 delegate_
->DownloadStarting(
596 request
, request_info
->GetContext(), request_info
->GetChildID(),
597 request_info
->GetRouteID(), request_info
->GetRequestID(),
598 is_content_initiated
, must_download
, &throttles
);
599 if (!throttles
.empty()) {
601 new ThrottlingResourceHandler(
602 handler
.Pass(), request
, throttles
.Pass()));
605 return handler
.Pass();
608 scoped_ptr
<ResourceHandler
>
609 ResourceDispatcherHostImpl::MaybeInterceptAsStream(net::URLRequest
* request
,
610 ResourceResponse
* response
) {
611 ResourceRequestInfoImpl
* info
= ResourceRequestInfoImpl::ForRequest(request
);
612 const std::string
& mime_type
= response
->head
.mime_type
;
615 std::string target_id
;
617 !delegate_
->ShouldInterceptResourceAsStream(info
->GetContext(),
622 return scoped_ptr
<ResourceHandler
>();
625 StreamContext
* stream_context
=
626 GetStreamContextForResourceContext(info
->GetContext());
628 scoped_ptr
<StreamResourceHandler
> handler(
629 new StreamResourceHandler(request
,
630 stream_context
->registry(),
633 info
->set_is_stream(true);
634 delegate_
->OnStreamCreated(
639 handler
->stream()->CreateHandle(request
->url(), mime_type
),
640 request
->GetExpectedContentSize());
641 return handler
.PassAs
<ResourceHandler
>();
644 void ResourceDispatcherHostImpl::ClearSSLClientAuthHandlerForRequest(
645 net::URLRequest
* request
) {
646 ResourceRequestInfoImpl
* info
= ResourceRequestInfoImpl::ForRequest(request
);
648 ResourceLoader
* loader
= GetLoader(info
->GetGlobalRequestID());
650 loader
->ClearSSLClientAuthHandler();
654 ResourceDispatcherHostLoginDelegate
*
655 ResourceDispatcherHostImpl::CreateLoginDelegate(
656 ResourceLoader
* loader
,
657 net::AuthChallengeInfo
* auth_info
) {
661 return delegate_
->CreateLoginDelegate(auth_info
, loader
->request());
664 bool ResourceDispatcherHostImpl::HandleExternalProtocol(ResourceLoader
* loader
,
669 ResourceRequestInfoImpl
* info
= loader
->GetRequestInfo();
671 if (!ResourceType::IsFrame(info
->GetResourceType()))
674 const net::URLRequestJobFactory
* job_factory
=
675 info
->GetContext()->GetRequestContext()->job_factory();
676 if (job_factory
->IsHandledURL(url
))
679 return delegate_
->HandleExternalProtocol(url
, info
->GetChildID(),
683 void ResourceDispatcherHostImpl::DidStartRequest(ResourceLoader
* loader
) {
684 // Make sure we have the load state monitor running
685 if (!update_load_states_timer_
->IsRunning()) {
686 update_load_states_timer_
->Start(FROM_HERE
,
687 TimeDelta::FromMilliseconds(kUpdateLoadStatesIntervalMsec
),
688 this, &ResourceDispatcherHostImpl::UpdateLoadStates
);
692 void ResourceDispatcherHostImpl::DidReceiveRedirect(ResourceLoader
* loader
,
693 const GURL
& new_url
) {
694 ResourceRequestInfoImpl
* info
= loader
->GetRequestInfo();
696 int render_process_id
, render_frame_host
;
697 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_host
))
700 // Notify the observers on the UI thread.
701 scoped_ptr
<ResourceRedirectDetails
> detail(new ResourceRedirectDetails(
703 GetCertID(loader
->request(), info
->GetChildID()),
705 BrowserThread::PostTask(
706 BrowserThread::UI
, FROM_HERE
,
709 render_process_id
, render_frame_host
, base::Passed(&detail
)));
712 void ResourceDispatcherHostImpl::DidReceiveResponse(ResourceLoader
* loader
) {
713 ResourceRequestInfoImpl
* info
= loader
->GetRequestInfo();
715 if (loader
->request()->was_fetched_via_proxy() &&
716 loader
->request()->was_fetched_via_spdy() &&
717 loader
->request()->url().SchemeIs("http")) {
718 scheduler_
->OnReceivedSpdyProxiedHttpResponse(
719 info
->GetChildID(), info
->GetRouteID());
722 // There should be an entry in the map created when we dispatched the
723 // request unless it's been detached and the renderer has died.
724 OfflineMap::iterator
policy_it(
725 offline_policy_map_
.find(info
->GetGlobalRoutingID()));
726 if (offline_policy_map_
.end() != policy_it
) {
727 policy_it
->second
->UpdateStateForSuccessfullyStartedRequest(
728 loader
->request()->response_info());
730 // Unless detached, we should have an entry in offline_policy_map_ from
731 // when this request traversed Begin{Download,SaveFile,Request}.
732 // TODO(rdsmith): This isn't currently true; see http://crbug.com/241176.
733 DCHECK(info
->detachable_handler() &&
734 info
->detachable_handler()->is_detached());
737 int render_process_id
, render_frame_host
;
738 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_host
))
741 // Notify the observers on the UI thread.
742 scoped_ptr
<ResourceRequestDetails
> detail(new ResourceRequestDetails(
744 GetCertID(loader
->request(), info
->GetChildID())));
745 BrowserThread::PostTask(
746 BrowserThread::UI
, FROM_HERE
,
749 render_process_id
, render_frame_host
, base::Passed(&detail
)));
752 void ResourceDispatcherHostImpl::DidFinishLoading(ResourceLoader
* loader
) {
753 ResourceRequestInfo
* info
= loader
->GetRequestInfo();
755 // Record final result of all resource loads.
756 if (info
->GetResourceType() == ResourceType::MAIN_FRAME
) {
757 // This enumeration has "3" appended to its name to distinguish it from
759 UMA_HISTOGRAM_SPARSE_SLOWLY(
760 "Net.ErrorCodesForMainFrame3",
761 -loader
->request()->status().error());
763 if (loader
->request()->url().SchemeIsSecure()) {
764 if (loader
->request()->url().host() == "www.google.com") {
765 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.ErrorCodesForHTTPSGoogleMainFrame2",
766 -loader
->request()->status().error());
769 int num_valid_scts
= std::count_if(
770 loader
->request()->ssl_info().signed_certificate_timestamps
.begin(),
771 loader
->request()->ssl_info().signed_certificate_timestamps
.end(),
773 UMA_HISTOGRAM_COUNTS_100(
774 "Net.CertificateTransparency.MainFrameValidSCTCount", num_valid_scts
);
777 if (info
->GetResourceType() == ResourceType::IMAGE
) {
778 UMA_HISTOGRAM_SPARSE_SLOWLY(
779 "Net.ErrorCodesForImages",
780 -loader
->request()->status().error());
782 // This enumeration has "2" appended to distinguish it from older versions.
783 UMA_HISTOGRAM_SPARSE_SLOWLY(
784 "Net.ErrorCodesForSubresources2",
785 -loader
->request()->status().error());
789 delegate_
->RequestComplete(loader
->request());
791 // Destroy the ResourceLoader.
792 RemovePendingRequest(info
->GetChildID(), info
->GetRequestID());
795 void ResourceDispatcherHostImpl::OnInit() {
796 scheduler_
.reset(new ResourceScheduler
);
797 appcache::AppCacheInterceptor::EnsureRegistered();
800 void ResourceDispatcherHostImpl::OnShutdown() {
801 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
804 pending_loaders_
.clear();
806 // Make sure we shutdown the timer now, otherwise by the time our destructor
807 // runs if the timer is still running the Task is deleted twice (once by
808 // the MessageLoop and the second time by RepeatingTimer).
809 update_load_states_timer_
.reset();
811 // Clear blocked requests if any left.
812 // Note that we have to do this in 2 passes as we cannot call
813 // CancelBlockedRequestsForRoute while iterating over
814 // blocked_loaders_map_, as it modifies it.
815 std::set
<GlobalRoutingID
> ids
;
816 for (BlockedLoadersMap::const_iterator iter
= blocked_loaders_map_
.begin();
817 iter
!= blocked_loaders_map_
.end(); ++iter
) {
818 std::pair
<std::set
<GlobalRoutingID
>::iterator
, bool> result
=
819 ids
.insert(iter
->first
);
820 // We should not have duplicates.
821 DCHECK(result
.second
);
823 for (std::set
<GlobalRoutingID
>::const_iterator iter
= ids
.begin();
824 iter
!= ids
.end(); ++iter
) {
825 CancelBlockedRequestsForRoute(iter
->child_id
, iter
->route_id
);
831 bool ResourceDispatcherHostImpl::OnMessageReceived(
832 const IPC::Message
& message
,
833 ResourceMessageFilter
* filter
,
834 bool* message_was_ok
) {
837 IPC_BEGIN_MESSAGE_MAP_EX(ResourceDispatcherHostImpl
, message
, *message_was_ok
)
838 IPC_MESSAGE_HANDLER(ResourceHostMsg_RequestResource
, OnRequestResource
)
839 IPC_MESSAGE_HANDLER_DELAY_REPLY(ResourceHostMsg_SyncLoad
, OnSyncLoad
)
840 IPC_MESSAGE_HANDLER(ResourceHostMsg_ReleaseDownloadedFile
,
841 OnReleaseDownloadedFile
)
842 IPC_MESSAGE_HANDLER(ResourceHostMsg_DataDownloaded_ACK
, OnDataDownloadedACK
)
843 IPC_MESSAGE_HANDLER(ResourceHostMsg_UploadProgress_ACK
, OnUploadProgressACK
)
844 IPC_MESSAGE_HANDLER(ResourceHostMsg_CancelRequest
, OnCancelRequest
)
845 IPC_MESSAGE_UNHANDLED(handled
= false)
846 IPC_END_MESSAGE_MAP_EX()
848 if (!handled
&& IPC_MESSAGE_ID_CLASS(message
.type()) == ResourceMsgStart
) {
849 PickleIterator
iter(message
);
851 bool ok
= iter
.ReadInt(&request_id
);
853 GlobalRequestID
id(filter_
->child_id(), request_id
);
854 DelegateMap::iterator it
= delegate_map_
.find(id
);
855 if (it
!= delegate_map_
.end()) {
856 ObserverList
<ResourceMessageDelegate
>::Iterator
del_it(*it
->second
);
857 ResourceMessageDelegate
* delegate
;
858 while (!handled
&& (delegate
= del_it
.GetNext()) != NULL
) {
859 handled
= delegate
->OnMessageReceived(message
, message_was_ok
);
863 // As the unhandled resource message effectively has no consumer, mark it as
864 // handled to prevent needless propagation through the filter pipeline.
872 void ResourceDispatcherHostImpl::OnRequestResource(
873 const IPC::Message
& message
,
875 const ResourceHostMsg_Request
& request_data
) {
876 BeginRequest(request_id
, request_data
, NULL
, message
.routing_id());
879 // Begins a resource request with the given params on behalf of the specified
880 // child process. Responses will be dispatched through the given receiver. The
881 // process ID is used to lookup WebContentsImpl from routing_id's in the case of
882 // a request from a renderer. request_context is the cookie/cache context to be
883 // used for this request.
885 // If sync_result is non-null, then a SyncLoad reply will be generated, else
886 // a normal asynchronous set of response messages will be generated.
887 void ResourceDispatcherHostImpl::OnSyncLoad(
889 const ResourceHostMsg_Request
& request_data
,
890 IPC::Message
* sync_result
) {
891 BeginRequest(request_id
, request_data
, sync_result
,
892 sync_result
->routing_id());
895 void ResourceDispatcherHostImpl::UpdateRequestForTransfer(
899 const ResourceHostMsg_Request
& request_data
,
900 const linked_ptr
<ResourceLoader
>& loader
) {
901 ResourceRequestInfoImpl
* info
= loader
->GetRequestInfo();
902 GlobalRoutingID
old_routing_id(
903 request_data
.transferred_request_child_id
, info
->GetRouteID());
904 GlobalRequestID
old_request_id(request_data
.transferred_request_child_id
,
905 request_data
.transferred_request_request_id
);
906 GlobalRoutingID
new_routing_id(child_id
, route_id
);
907 GlobalRequestID
new_request_id(child_id
, request_id
);
909 // Clear out data that depends on |info| before updating it.
910 IncrementOutstandingRequestsMemory(-1, *info
);
911 OustandingRequestsStats empty_stats
= { 0, 0 };
912 OustandingRequestsStats old_stats
= GetOutstandingRequestsStats(*info
);
913 UpdateOutstandingRequestsStats(*info
, empty_stats
);
914 pending_loaders_
.erase(old_request_id
);
916 // ResourceHandlers should always get state related to the request from the
917 // ResourceRequestInfo rather than caching it locally. This lets us update
918 // the info object when a transfer occurs.
919 info
->UpdateForTransfer(child_id
, route_id
, request_data
.origin_pid
,
920 request_id
, request_data
.frame_id
,
921 request_data
.parent_frame_id
, filter_
->GetWeakPtr());
923 // Update maps that used the old IDs, if necessary. Some transfers in tests
924 // do not actually use a different ID, so not all maps need to be updated.
925 pending_loaders_
[new_request_id
] = loader
;
926 UpdateOutstandingRequestsStats(*info
, old_stats
);
927 IncrementOutstandingRequestsMemory(1, *info
);
928 if (old_routing_id
!= new_routing_id
) {
929 if (offline_policy_map_
.find(old_routing_id
) != offline_policy_map_
.end()) {
930 if (offline_policy_map_
.find(new_routing_id
) !=
931 offline_policy_map_
.end())
932 delete offline_policy_map_
[new_routing_id
];
933 offline_policy_map_
[new_routing_id
] = offline_policy_map_
[old_routing_id
];
934 offline_policy_map_
.erase(old_routing_id
);
936 if (blocked_loaders_map_
.find(old_routing_id
) !=
937 blocked_loaders_map_
.end()) {
938 blocked_loaders_map_
[new_routing_id
] =
939 blocked_loaders_map_
[old_routing_id
];
940 blocked_loaders_map_
.erase(old_routing_id
);
943 if (old_request_id
!= new_request_id
) {
944 DelegateMap::iterator it
= delegate_map_
.find(old_request_id
);
945 if (it
!= delegate_map_
.end()) {
946 // Tell each delegate that the request ID has changed.
947 ObserverList
<ResourceMessageDelegate
>::Iterator
del_it(*it
->second
);
948 ResourceMessageDelegate
* delegate
;
949 while ((delegate
= del_it
.GetNext()) != NULL
) {
950 delegate
->set_request_id(new_request_id
);
952 // Now store the observer list under the new request ID.
953 delegate_map_
[new_request_id
] = delegate_map_
[old_request_id
];
954 delegate_map_
.erase(old_request_id
);
958 appcache::AppCacheInterceptor::CompleteCrossSiteTransfer(
961 request_data
.appcache_host_id
);
963 // We should have a CrossSiteResourceHandler to finish the transfer.
964 DCHECK(info
->cross_site_handler());
967 void ResourceDispatcherHostImpl::BeginRequest(
969 const ResourceHostMsg_Request
& request_data
,
970 IPC::Message
* sync_result
, // only valid for sync
972 int process_type
= filter_
->process_type();
973 int child_id
= filter_
->child_id();
975 // Reject invalid priority.
976 if (request_data
.priority
< net::MINIMUM_PRIORITY
||
977 request_data
.priority
> net::MAXIMUM_PRIORITY
) {
978 RecordAction(base::UserMetricsAction("BadMessageTerminate_RDH"));
979 filter_
->BadMessageReceived();
983 // If we crash here, figure out what URL the renderer was requesting.
984 // http://crbug.com/91398
986 base::strlcpy(url_buf
, request_data
.url
.spec().c_str(), arraysize(url_buf
));
987 base::debug::Alias(url_buf
);
989 // If the request that's coming in is being transferred from another process,
990 // we want to reuse and resume the old loader rather than start a new one.
991 linked_ptr
<ResourceLoader
> deferred_loader
;
993 LoaderMap::iterator it
= pending_loaders_
.find(
994 GlobalRequestID(request_data
.transferred_request_child_id
,
995 request_data
.transferred_request_request_id
));
996 if (it
!= pending_loaders_
.end()) {
997 // If the request is transferring to a new process, we can update our
998 // state and let it resume with its existing ResourceHandlers.
999 if (it
->second
->is_transferring()) {
1000 deferred_loader
= it
->second
;
1001 UpdateRequestForTransfer(child_id
, route_id
, request_id
,
1002 request_data
, deferred_loader
);
1004 deferred_loader
->CompleteTransfer();
1006 RecordAction(base::UserMetricsAction("BadMessageTerminate_RDH"));
1007 filter_
->BadMessageReceived();
1013 ResourceContext
* resource_context
= NULL
;
1014 net::URLRequestContext
* request_context
= NULL
;
1015 filter_
->GetContexts(request_data
, &resource_context
, &request_context
);
1016 // http://crbug.com/90971
1017 CHECK(ContainsKey(active_resource_contexts_
, resource_context
));
1020 !ShouldServiceRequest(process_type
, child_id
, request_data
,
1021 filter_
->file_system_context())) {
1022 AbortRequestBeforeItStarts(filter_
, sync_result
, request_id
);
1026 const Referrer
referrer(request_data
.referrer
, request_data
.referrer_policy
);
1028 // Allow the observer to block/handle the request.
1029 if (delegate_
&& !delegate_
->ShouldBeginRequest(child_id
,
1031 request_data
.method
,
1033 request_data
.resource_type
,
1034 resource_context
)) {
1035 AbortRequestBeforeItStarts(filter_
, sync_result
, request_id
);
1039 bool is_sync_load
= sync_result
!= NULL
;
1041 BuildLoadFlagsForRequest(request_data
, child_id
, is_sync_load
);
1043 GlobalRoutingID
id(child_id
, route_id
);
1044 if (!offline_policy_map_
[id
])
1045 offline_policy_map_
[id
] = new OfflinePolicy();
1046 load_flags
|= offline_policy_map_
[id
]->GetAdditionalLoadFlags(
1047 load_flags
, request_data
.resource_type
== ResourceType::MAIN_FRAME
);
1049 // Sync loads should have maximum priority and should be the only
1050 // requets that have the ignore limits flag set.
1052 DCHECK_EQ(request_data
.priority
, net::MAXIMUM_PRIORITY
);
1053 DCHECK_NE(load_flags
& net::LOAD_IGNORE_LIMITS
, 0);
1055 DCHECK_EQ(load_flags
& net::LOAD_IGNORE_LIMITS
, 0);
1058 // Construct the request.
1059 scoped_ptr
<net::URLRequest
> new_request
;
1060 net::URLRequest
* request
;
1061 new_request
= request_context
->CreateRequest(
1062 request_data
.url
, request_data
.priority
, NULL
);
1063 request
= new_request
.get();
1065 request
->set_method(request_data
.method
);
1066 request
->set_first_party_for_cookies(request_data
.first_party_for_cookies
);
1067 SetReferrerForRequest(request
, referrer
);
1069 net::HttpRequestHeaders headers
;
1070 headers
.AddHeadersFromString(request_data
.headers
);
1071 request
->SetExtraRequestHeaders(headers
);
1073 request
->SetLoadFlags(load_flags
);
1075 // Resolve elements from request_body and prepare upload data.
1076 if (request_data
.request_body
.get()) {
1077 webkit_blob::BlobStorageContext
* blob_context
= NULL
;
1078 if (filter_
->blob_storage_context())
1079 blob_context
= filter_
->blob_storage_context()->context();
1080 request
->set_upload(UploadDataStreamBuilder::Build(
1081 request_data
.request_body
.get(),
1083 filter_
->file_system_context(),
1084 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)
1088 bool allow_download
= request_data
.allow_download
&&
1089 ResourceType::IsFrame(request_data
.resource_type
);
1091 // Make extra info and read footer (contains request ID).
1092 ResourceRequestInfoImpl
* extra_info
=
1093 new ResourceRequestInfoImpl(
1097 request_data
.origin_pid
,
1099 request_data
.render_frame_id
,
1100 request_data
.is_main_frame
,
1101 request_data
.frame_id
,
1102 request_data
.parent_is_main_frame
,
1103 request_data
.parent_frame_id
,
1104 request_data
.resource_type
,
1105 request_data
.transition_type
,
1106 request_data
.should_replace_current_entry
,
1107 false, // is download
1110 request_data
.has_user_gesture
,
1111 request_data
.referrer_policy
,
1112 request_data
.visiblity_state
,
1114 filter_
->GetWeakPtr(),
1116 extra_info
->AssociateWithRequest(request
); // Request takes ownership.
1118 if (request
->url().SchemeIs(chrome::kBlobScheme
)) {
1119 // Hang on to a reference to ensure the blob is not released prior
1120 // to the job being started.
1121 webkit_blob::BlobProtocolHandler::SetRequestedBlobDataHandle(
1123 filter_
->blob_storage_context()->context()->
1124 GetBlobDataFromPublicURL(request
->url()));
1127 // Have the appcache associate its extra info with the request.
1128 appcache::AppCacheInterceptor::SetExtraRequestInfo(
1129 request
, filter_
->appcache_service(), child_id
,
1130 request_data
.appcache_host_id
, request_data
.resource_type
);
1132 scoped_ptr
<ResourceHandler
> handler(
1133 CreateResourceHandler(
1135 request_data
, sync_result
, route_id
, process_type
, child_id
,
1138 BeginRequestInternal(new_request
.Pass(), handler
.Pass());
1141 scoped_ptr
<ResourceHandler
> ResourceDispatcherHostImpl::CreateResourceHandler(
1142 net::URLRequest
* request
,
1143 const ResourceHostMsg_Request
& request_data
,
1144 IPC::Message
* sync_result
,
1148 ResourceContext
* resource_context
) {
1149 // Construct the IPC resource handler.
1150 scoped_ptr
<ResourceHandler
> handler
;
1152 handler
.reset(new SyncResourceHandler(request
, sync_result
, this));
1154 handler
.reset(new AsyncResourceHandler(request
, this));
1155 if (IsDetachableResourceType(request_data
.resource_type
)) {
1156 handler
.reset(new DetachableResourceHandler(
1158 base::TimeDelta::FromMilliseconds(kDefaultDetachableCancelDelayMs
),
1163 // The RedirectToFileResourceHandler depends on being next in the chain.
1164 if (request_data
.download_to_file
) {
1166 new RedirectToFileResourceHandler(handler
.Pass(), request
, this));
1169 // Install a CrossSiteResourceHandler for all main frame requests. This will
1170 // let us check whether a transfer is required and pause for the unload
1171 // handler either if so or if a cross-process navigation is already under way.
1172 bool is_swappable_navigation
=
1173 request_data
.resource_type
== ResourceType::MAIN_FRAME
;
1174 // If we are using --site-per-process, install it for subframes as well.
1175 if (!is_swappable_navigation
&&
1176 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSitePerProcess
)) {
1177 is_swappable_navigation
=
1178 request_data
.resource_type
== ResourceType::SUB_FRAME
;
1180 if (is_swappable_navigation
&& process_type
== PROCESS_TYPE_RENDERER
)
1181 handler
.reset(new CrossSiteResourceHandler(handler
.Pass(), request
));
1183 // Insert a buffered event handler before the actual one.
1185 new BufferedResourceHandler(handler
.Pass(), this, request
));
1187 ScopedVector
<ResourceThrottle
> throttles
;
1189 delegate_
->RequestBeginning(request
,
1191 filter_
->appcache_service(),
1192 request_data
.resource_type
,
1198 if (request
->has_upload()) {
1199 // Block power save while uploading data.
1200 throttles
.push_back(new PowerSaveBlockResourceThrottle());
1203 throttles
.push_back(
1204 scheduler_
->ScheduleRequest(child_id
, route_id
, request
).release());
1207 new ThrottlingResourceHandler(handler
.Pass(), request
, throttles
.Pass()));
1209 return handler
.Pass();
1212 void ResourceDispatcherHostImpl::OnReleaseDownloadedFile(int request_id
) {
1213 UnregisterDownloadedTempFile(filter_
->child_id(), request_id
);
1216 void ResourceDispatcherHostImpl::OnDataDownloadedACK(int request_id
) {
1217 // TODO(michaeln): maybe throttle DataDownloaded messages
1220 void ResourceDispatcherHostImpl::RegisterDownloadedTempFile(
1221 int child_id
, int request_id
, ShareableFileReference
* reference
) {
1222 registered_temp_files_
[child_id
][request_id
] = reference
;
1223 ChildProcessSecurityPolicyImpl::GetInstance()->GrantReadFile(
1224 child_id
, reference
->path());
1226 // When the temp file is deleted, revoke permissions that the renderer has
1227 // to that file. This covers an edge case where the file is deleted and then
1228 // the same name is re-used for some other purpose, we don't want the old
1229 // renderer to still have access to it.
1231 // We do this when the file is deleted because the renderer can take a blob
1232 // reference to the temp file that outlives the url loaded that it was
1233 // loaded with to keep the file (and permissions) alive.
1234 reference
->AddFinalReleaseCallback(
1235 base::Bind(&RemoveDownloadFileFromChildSecurityPolicy
,
1239 void ResourceDispatcherHostImpl::UnregisterDownloadedTempFile(
1240 int child_id
, int request_id
) {
1241 DeletableFilesMap
& map
= registered_temp_files_
[child_id
];
1242 DeletableFilesMap::iterator found
= map
.find(request_id
);
1243 if (found
== map
.end())
1248 // Note that we don't remove the security bits here. This will be done
1249 // when all file refs are deleted (see RegisterDownloadedTempFile).
1252 bool ResourceDispatcherHostImpl::Send(IPC::Message
* message
) {
1257 void ResourceDispatcherHostImpl::OnUploadProgressACK(int request_id
) {
1258 ResourceLoader
* loader
= GetLoader(filter_
->child_id(), request_id
);
1260 loader
->OnUploadProgressACK();
1263 // Note that this cancel is subtly different from the other
1264 // CancelRequest methods in this file, which also tear down the loader.
1265 void ResourceDispatcherHostImpl::OnCancelRequest(int request_id
) {
1266 int child_id
= filter_
->child_id();
1268 // When the old renderer dies, it sends a message to us to cancel its
1270 if (IsTransferredNavigation(GlobalRequestID(child_id
, request_id
)))
1273 ResourceLoader
* loader
= GetLoader(child_id
, request_id
);
1275 // We probably want to remove this warning eventually, but I wanted to be
1276 // able to notice when this happens during initial development since it
1277 // should be rare and may indicate a bug.
1278 DVLOG(1) << "Canceling a request that wasn't found";
1282 loader
->CancelRequest(true);
1285 ResourceRequestInfoImpl
* ResourceDispatcherHostImpl::CreateRequestInfo(
1289 ResourceContext
* context
) {
1290 return new ResourceRequestInfoImpl(
1291 PROCESS_TYPE_RENDERER
,
1296 MSG_ROUTING_NONE
, // render_frame_id
1297 false, // is_main_frame
1299 false, // parent_is_main_frame
1300 -1, // parent_frame_id
1301 ResourceType::SUB_RESOURCE
,
1302 PAGE_TRANSITION_LINK
,
1303 false, // should_replace_current_entry
1304 download
, // is_download
1306 download
, // allow_download
1307 false, // has_user_gesture
1308 blink::WebReferrerPolicyDefault
,
1309 blink::WebPageVisibilityStateVisible
,
1311 base::WeakPtr
<ResourceMessageFilter
>(), // filter
1315 void ResourceDispatcherHostImpl::OnRenderViewHostCreated(
1318 scheduler_
->OnClientCreated(child_id
, route_id
);
1321 void ResourceDispatcherHostImpl::OnRenderViewHostDeleted(
1324 scheduler_
->OnClientDeleted(child_id
, route_id
);
1325 CancelRequestsForRoute(child_id
, route_id
);
1328 // This function is only used for saving feature.
1329 void ResourceDispatcherHostImpl::BeginSaveFile(
1331 const Referrer
& referrer
,
1334 ResourceContext
* context
) {
1338 // http://crbug.com/90971
1340 base::strlcpy(url_buf
, url
.spec().c_str(), arraysize(url_buf
));
1341 base::debug::Alias(url_buf
);
1342 CHECK(ContainsKey(active_resource_contexts_
, context
));
1344 scoped_ptr
<ResourceHandler
> handler(
1345 new SaveFileResourceHandler(child_id
,
1348 save_file_manager_
.get()));
1351 const net::URLRequestContext
* request_context
= context
->GetRequestContext();
1353 request_context
->job_factory()->IsHandledURL(url
);
1355 // Since any URLs which have non-standard scheme have been filtered
1356 // by save manager(see GURL::SchemeIsStandard). This situation
1357 // should not happen.
1362 scoped_ptr
<net::URLRequest
> request(
1363 request_context
->CreateRequest(url
, net::DEFAULT_PRIORITY
, NULL
));
1364 request
->set_method("GET");
1365 SetReferrerForRequest(request
.get(), referrer
);
1367 // So far, for saving page, we need fetch content from cache, in the
1368 // future, maybe we can use a configuration to configure this behavior.
1369 request
->SetLoadFlags(net::LOAD_PREFERRING_CACHE
);
1371 // No need to get offline load flags for save files, but make sure
1372 // we have an OfflinePolicy to receive request completions.
1373 GlobalRoutingID
id(child_id
, route_id
);
1374 if (!offline_policy_map_
[id
])
1375 offline_policy_map_
[id
] = new OfflinePolicy();
1377 // Since we're just saving some resources we need, disallow downloading.
1378 ResourceRequestInfoImpl
* extra_info
=
1379 CreateRequestInfo(child_id
, route_id
, false, context
);
1380 extra_info
->AssociateWithRequest(request
.get()); // Request takes ownership.
1382 BeginRequestInternal(request
.Pass(), handler
.Pass());
1385 void ResourceDispatcherHostImpl::MarkAsTransferredNavigation(
1386 const GlobalRequestID
& id
) {
1387 GetLoader(id
)->MarkAsTransferring();
1390 void ResourceDispatcherHostImpl::CancelTransferringNavigation(
1391 const GlobalRequestID
& id
) {
1392 // Request should still exist and be in the middle of a transfer.
1393 DCHECK(IsTransferredNavigation(id
));
1394 RemovePendingRequest(id
.child_id
, id
.request_id
);
1397 void ResourceDispatcherHostImpl::ResumeDeferredNavigation(
1398 const GlobalRequestID
& id
) {
1399 ResourceLoader
* loader
= GetLoader(id
);
1401 // The response we were meant to resume could have already been canceled.
1402 ResourceRequestInfoImpl
* info
= loader
->GetRequestInfo();
1403 if (info
->cross_site_handler())
1404 info
->cross_site_handler()->ResumeResponse();
1408 // The object died, so cancel and detach all requests associated with it except
1409 // for downloads and detachable resources, which belong to the browser process
1410 // even if initiated via a renderer.
1411 void ResourceDispatcherHostImpl::CancelRequestsForProcess(int child_id
) {
1412 CancelRequestsForRoute(child_id
, -1 /* cancel all */);
1413 registered_temp_files_
.erase(child_id
);
1416 void ResourceDispatcherHostImpl::CancelRequestsForRoute(int child_id
,
1418 // Since pending_requests_ is a map, we first build up a list of all of the
1419 // matching requests to be cancelled, and then we cancel them. Since there
1420 // may be more than one request to cancel, we cannot simply hold onto the map
1421 // iterators found in the first loop.
1423 // Find the global ID of all matching elements.
1424 bool any_requests_transferring
= false;
1425 std::vector
<GlobalRequestID
> matching_requests
;
1426 for (LoaderMap::const_iterator i
= pending_loaders_
.begin();
1427 i
!= pending_loaders_
.end(); ++i
) {
1428 if (i
->first
.child_id
!= child_id
)
1431 ResourceRequestInfoImpl
* info
= i
->second
->GetRequestInfo();
1433 GlobalRequestID
id(child_id
, i
->first
.request_id
);
1434 DCHECK(id
== i
->first
);
1435 // Don't cancel navigations that are expected to live beyond this process.
1436 if (IsTransferredNavigation(id
))
1437 any_requests_transferring
= true;
1438 if (info
->detachable_handler()) {
1439 info
->detachable_handler()->Detach();
1440 } else if (!info
->IsDownload() && !info
->is_stream() &&
1441 !IsTransferredNavigation(id
) &&
1442 (route_id
== -1 || route_id
== info
->GetRouteID())) {
1443 matching_requests
.push_back(id
);
1448 for (size_t i
= 0; i
< matching_requests
.size(); ++i
) {
1449 LoaderMap::iterator iter
= pending_loaders_
.find(matching_requests
[i
]);
1450 // Although every matching request was in pending_requests_ when we built
1451 // matching_requests, it is normal for a matching request to be not found
1452 // in pending_requests_ after we have removed some matching requests from
1453 // pending_requests_. For example, deleting a net::URLRequest that has
1454 // exclusive (write) access to an HTTP cache entry may unblock another
1455 // net::URLRequest that needs exclusive access to the same cache entry, and
1456 // that net::URLRequest may complete and remove itself from
1457 // pending_requests_. So we need to check that iter is not equal to
1458 // pending_requests_.end().
1459 if (iter
!= pending_loaders_
.end())
1460 RemovePendingLoader(iter
);
1463 // Don't clear the blocked loaders or offline policy maps if any of the
1464 // requests in route_id are being transferred to a new process, since those
1465 // maps will be updated with the new route_id after the transfer. Otherwise
1466 // we will lose track of this info when the old route goes away, before the
1467 // new one is created.
1468 if (any_requests_transferring
)
1471 // Now deal with blocked requests if any.
1472 if (route_id
!= -1) {
1473 if (blocked_loaders_map_
.find(GlobalRoutingID(child_id
, route_id
)) !=
1474 blocked_loaders_map_
.end()) {
1475 CancelBlockedRequestsForRoute(child_id
, route_id
);
1478 // We have to do all render views for the process |child_id|.
1479 // Note that we have to do this in 2 passes as we cannot call
1480 // CancelBlockedRequestsForRoute while iterating over
1481 // blocked_loaders_map_, as it modifies it.
1482 std::set
<int> route_ids
;
1483 for (BlockedLoadersMap::const_iterator iter
= blocked_loaders_map_
.begin();
1484 iter
!= blocked_loaders_map_
.end(); ++iter
) {
1485 if (iter
->first
.child_id
== child_id
)
1486 route_ids
.insert(iter
->first
.route_id
);
1488 for (std::set
<int>::const_iterator iter
= route_ids
.begin();
1489 iter
!= route_ids
.end(); ++iter
) {
1490 CancelBlockedRequestsForRoute(child_id
, *iter
);
1494 // Cleanup the offline state for the route.
1495 if (-1 != route_id
) {
1496 OfflineMap::iterator it
= offline_policy_map_
.find(
1497 GlobalRoutingID(child_id
, route_id
));
1498 if (offline_policy_map_
.end() != it
) {
1500 offline_policy_map_
.erase(it
);
1503 for (OfflineMap::iterator it
= offline_policy_map_
.begin();
1504 offline_policy_map_
.end() != it
;) {
1505 // Increment iterator so deletion doesn't invalidate it.
1506 OfflineMap::iterator current_it
= it
++;
1508 if (child_id
== current_it
->first
.child_id
) {
1509 delete current_it
->second
;
1510 offline_policy_map_
.erase(current_it
);
1516 // Cancels the request and removes it from the list.
1517 void ResourceDispatcherHostImpl::RemovePendingRequest(int child_id
,
1519 LoaderMap::iterator i
= pending_loaders_
.find(
1520 GlobalRequestID(child_id
, request_id
));
1521 if (i
== pending_loaders_
.end()) {
1522 NOTREACHED() << "Trying to remove a request that's not here";
1525 RemovePendingLoader(i
);
1528 void ResourceDispatcherHostImpl::RemovePendingLoader(
1529 const LoaderMap::iterator
& iter
) {
1530 ResourceRequestInfoImpl
* info
= iter
->second
->GetRequestInfo();
1532 // Remove the memory credit that we added when pushing the request onto
1533 // the pending list.
1534 IncrementOutstandingRequestsMemory(-1, *info
);
1536 pending_loaders_
.erase(iter
);
1538 // If we have no more pending requests, then stop the load state monitor
1539 if (pending_loaders_
.empty() && update_load_states_timer_
)
1540 update_load_states_timer_
->Stop();
1543 void ResourceDispatcherHostImpl::CancelRequest(int child_id
,
1545 ResourceLoader
* loader
= GetLoader(child_id
, request_id
);
1547 // We probably want to remove this warning eventually, but I wanted to be
1548 // able to notice when this happens during initial development since it
1549 // should be rare and may indicate a bug.
1550 DVLOG(1) << "Canceling a request that wasn't found";
1554 RemovePendingRequest(child_id
, request_id
);
1557 ResourceDispatcherHostImpl::OustandingRequestsStats
1558 ResourceDispatcherHostImpl::GetOutstandingRequestsStats(
1559 const ResourceRequestInfoImpl
& info
) {
1560 OutstandingRequestsStatsMap::iterator entry
=
1561 outstanding_requests_stats_map_
.find(info
.GetChildID());
1562 OustandingRequestsStats stats
= { 0, 0 };
1563 if (entry
!= outstanding_requests_stats_map_
.end())
1564 stats
= entry
->second
;
1568 void ResourceDispatcherHostImpl::UpdateOutstandingRequestsStats(
1569 const ResourceRequestInfoImpl
& info
,
1570 const OustandingRequestsStats
& stats
) {
1571 if (stats
.memory_cost
== 0 && stats
.num_requests
== 0)
1572 outstanding_requests_stats_map_
.erase(info
.GetChildID());
1574 outstanding_requests_stats_map_
[info
.GetChildID()] = stats
;
1577 ResourceDispatcherHostImpl::OustandingRequestsStats
1578 ResourceDispatcherHostImpl::IncrementOutstandingRequestsMemory(
1580 const ResourceRequestInfoImpl
& info
) {
1581 DCHECK_EQ(1, abs(count
));
1583 // Retrieve the previous value (defaulting to 0 if not found).
1584 OustandingRequestsStats stats
= GetOutstandingRequestsStats(info
);
1586 // Insert/update the total; delete entries when their count reaches 0.
1587 stats
.memory_cost
+= count
* info
.memory_cost();
1588 DCHECK_GE(stats
.memory_cost
, 0);
1589 UpdateOutstandingRequestsStats(info
, stats
);
1594 ResourceDispatcherHostImpl::OustandingRequestsStats
1595 ResourceDispatcherHostImpl::IncrementOutstandingRequestsCount(
1597 const ResourceRequestInfoImpl
& info
) {
1598 DCHECK_EQ(1, abs(count
));
1599 num_in_flight_requests_
+= count
;
1601 OustandingRequestsStats stats
= GetOutstandingRequestsStats(info
);
1602 stats
.num_requests
+= count
;
1603 DCHECK_GE(stats
.num_requests
, 0);
1604 UpdateOutstandingRequestsStats(info
, stats
);
1609 bool ResourceDispatcherHostImpl::HasSufficientResourcesForRequest(
1610 const net::URLRequest
* request_
) {
1611 const ResourceRequestInfoImpl
* info
=
1612 ResourceRequestInfoImpl::ForRequest(request_
);
1613 OustandingRequestsStats stats
= IncrementOutstandingRequestsCount(1, *info
);
1615 if (stats
.num_requests
> max_num_in_flight_requests_per_process_
)
1617 if (num_in_flight_requests_
> max_num_in_flight_requests_
)
1623 void ResourceDispatcherHostImpl::FinishedWithResourcesForRequest(
1624 const net::URLRequest
* request_
) {
1625 const ResourceRequestInfoImpl
* info
=
1626 ResourceRequestInfoImpl::ForRequest(request_
);
1627 IncrementOutstandingRequestsCount(-1, *info
);
1631 int ResourceDispatcherHostImpl::CalculateApproximateMemoryCost(
1632 net::URLRequest
* request
) {
1633 // The following fields should be a minor size contribution (experimentally
1634 // on the order of 100). However since they are variable length, it could
1635 // in theory be a sizeable contribution.
1636 int strings_cost
= request
->extra_request_headers().ToString().size() +
1637 request
->original_url().spec().size() +
1638 request
->referrer().size() +
1639 request
->method().size();
1641 // Note that this expression will typically be dominated by:
1642 // |kAvgBytesPerOutstandingRequest|.
1643 return kAvgBytesPerOutstandingRequest
+ strings_cost
;
1646 void ResourceDispatcherHostImpl::BeginRequestInternal(
1647 scoped_ptr
<net::URLRequest
> request
,
1648 scoped_ptr
<ResourceHandler
> handler
) {
1649 DCHECK(!request
->is_pending());
1650 ResourceRequestInfoImpl
* info
=
1651 ResourceRequestInfoImpl::ForRequest(request
.get());
1653 if ((TimeTicks::Now() - last_user_gesture_time_
) <
1654 TimeDelta::FromMilliseconds(kUserGestureWindowMs
)) {
1655 request
->SetLoadFlags(
1656 request
->load_flags() | net::LOAD_MAYBE_USER_GESTURE
);
1659 // Add the memory estimate that starting this request will consume.
1660 info
->set_memory_cost(CalculateApproximateMemoryCost(request
.get()));
1662 // If enqueing/starting this request will exceed our per-process memory
1663 // bound, abort it right away.
1664 OustandingRequestsStats stats
= IncrementOutstandingRequestsMemory(1, *info
);
1665 if (stats
.memory_cost
> max_outstanding_requests_cost_per_process_
) {
1666 // We call "CancelWithError()" as a way of setting the net::URLRequest's
1667 // status -- it has no effect beyond this, since the request hasn't started.
1668 request
->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES
);
1671 handler
->OnResponseCompleted(info
->GetRequestID(), request
->status(),
1672 std::string(), &defer
);
1674 // TODO(darin): The handler is not ready for us to kill the request. Oops!
1678 IncrementOutstandingRequestsMemory(-1, *info
);
1680 // A ResourceHandler must not outlive its associated URLRequest.
1685 linked_ptr
<ResourceLoader
> loader(
1686 new ResourceLoader(request
.Pass(), handler
.Pass(), this));
1688 GlobalRoutingID
id(info
->GetGlobalRoutingID());
1689 BlockedLoadersMap::const_iterator iter
= blocked_loaders_map_
.find(id
);
1690 if (iter
!= blocked_loaders_map_
.end()) {
1691 // The request should be blocked.
1692 iter
->second
->push_back(loader
);
1696 StartLoading(info
, loader
);
1699 void ResourceDispatcherHostImpl::StartLoading(
1700 ResourceRequestInfoImpl
* info
,
1701 const linked_ptr
<ResourceLoader
>& loader
) {
1702 pending_loaders_
[info
->GetGlobalRequestID()] = loader
;
1704 loader
->StartRequest();
1707 void ResourceDispatcherHostImpl::OnUserGesture(WebContentsImpl
* contents
) {
1708 last_user_gesture_time_
= TimeTicks::Now();
1711 net::URLRequest
* ResourceDispatcherHostImpl::GetURLRequest(
1712 const GlobalRequestID
& id
) {
1713 ResourceLoader
* loader
= GetLoader(id
);
1717 return loader
->request();
1722 // This function attempts to return the "more interesting" load state of |a|
1723 // and |b|. We don't have temporal information about these load states
1724 // (meaning we don't know when we transitioned into these states), so we just
1725 // rank them according to how "interesting" the states are.
1727 // We take advantage of the fact that the load states are an enumeration listed
1728 // in the order in which they occur during the lifetime of a request, so we can
1729 // regard states with larger numeric values as being further along toward
1730 // completion. We regard those states as more interesting to report since they
1731 // represent progress.
1733 // For example, by this measure "tranferring data" is a more interesting state
1734 // than "resolving host" because when we are transferring data we are actually
1735 // doing something that corresponds to changes that the user might observe,
1736 // whereas waiting for a host name to resolve implies being stuck.
1738 const net::LoadStateWithParam
& MoreInterestingLoadState(
1739 const net::LoadStateWithParam
& a
, const net::LoadStateWithParam
& b
) {
1740 return (a
.state
< b
.state
) ? b
: a
;
1743 // Carries information about a load state change.
1746 net::LoadStateWithParam load_state
;
1747 uint64 upload_position
;
1751 // Map from ProcessID+RouteID pair to LoadState
1752 typedef std::map
<GlobalRoutingID
, LoadInfo
> LoadInfoMap
;
1754 // Used to marshal calls to LoadStateChanged from the IO to UI threads. We do
1755 // them all as a single callback to avoid spamming the UI thread.
1756 void LoadInfoUpdateCallback(const LoadInfoMap
& info_map
) {
1757 LoadInfoMap::const_iterator i
;
1758 for (i
= info_map
.begin(); i
!= info_map
.end(); ++i
) {
1759 RenderViewHostImpl
* view
=
1760 RenderViewHostImpl::FromID(i
->first
.child_id
, i
->first
.route_id
);
1761 if (view
) // The view could be gone at this point.
1762 view
->LoadStateChanged(i
->second
.url
, i
->second
.load_state
,
1763 i
->second
.upload_position
,
1764 i
->second
.upload_size
);
1770 void ResourceDispatcherHostImpl::UpdateLoadStates() {
1771 // Populate this map with load state changes, and then send them on to the UI
1772 // thread where they can be passed along to the respective RVHs.
1773 LoadInfoMap info_map
;
1775 LoaderMap::const_iterator i
;
1777 // Determine the largest upload size of all requests
1778 // in each View (good chance it's zero).
1779 std::map
<GlobalRoutingID
, uint64
> largest_upload_size
;
1780 for (i
= pending_loaders_
.begin(); i
!= pending_loaders_
.end(); ++i
) {
1781 net::URLRequest
* request
= i
->second
->request();
1782 ResourceRequestInfoImpl
* info
= i
->second
->GetRequestInfo();
1783 uint64 upload_size
= request
->GetUploadProgress().size();
1784 if (request
->GetLoadState().state
!= net::LOAD_STATE_SENDING_REQUEST
)
1786 GlobalRoutingID
id(info
->GetGlobalRoutingID());
1787 if (upload_size
&& largest_upload_size
[id
] < upload_size
)
1788 largest_upload_size
[id
] = upload_size
;
1791 for (i
= pending_loaders_
.begin(); i
!= pending_loaders_
.end(); ++i
) {
1792 net::URLRequest
* request
= i
->second
->request();
1793 ResourceRequestInfoImpl
* info
= i
->second
->GetRequestInfo();
1794 net::LoadStateWithParam load_state
= request
->GetLoadState();
1795 net::UploadProgress progress
= request
->GetUploadProgress();
1797 // We also poll for upload progress on this timer and send upload
1798 // progress ipc messages to the plugin process.
1799 i
->second
->ReportUploadProgress();
1801 GlobalRoutingID
id(info
->GetGlobalRoutingID());
1803 // If a request is uploading data, ignore all other requests so that the
1804 // upload progress takes priority for being shown in the status bar.
1805 if (largest_upload_size
.find(id
) != largest_upload_size
.end() &&
1806 progress
.size() < largest_upload_size
[id
])
1809 net::LoadStateWithParam to_insert
= load_state
;
1810 LoadInfoMap::iterator existing
= info_map
.find(id
);
1811 if (existing
!= info_map
.end()) {
1813 MoreInterestingLoadState(existing
->second
.load_state
, load_state
);
1814 if (to_insert
.state
== existing
->second
.load_state
.state
)
1817 LoadInfo
& load_info
= info_map
[id
];
1818 load_info
.url
= request
->url();
1819 load_info
.load_state
= to_insert
;
1820 load_info
.upload_size
= progress
.size();
1821 load_info
.upload_position
= progress
.position();
1824 if (info_map
.empty())
1827 BrowserThread::PostTask(
1828 BrowserThread::UI
, FROM_HERE
,
1829 base::Bind(&LoadInfoUpdateCallback
, info_map
));
1832 void ResourceDispatcherHostImpl::BlockRequestsForRoute(int child_id
,
1834 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
1835 GlobalRoutingID
key(child_id
, route_id
);
1836 DCHECK(blocked_loaders_map_
.find(key
) == blocked_loaders_map_
.end()) <<
1837 "BlockRequestsForRoute called multiple time for the same RVH";
1838 blocked_loaders_map_
[key
] = new BlockedLoadersList();
1841 void ResourceDispatcherHostImpl::ResumeBlockedRequestsForRoute(int child_id
,
1843 ProcessBlockedRequestsForRoute(child_id
, route_id
, false);
1846 void ResourceDispatcherHostImpl::CancelBlockedRequestsForRoute(int child_id
,
1848 ProcessBlockedRequestsForRoute(child_id
, route_id
, true);
1851 void ResourceDispatcherHostImpl::ProcessBlockedRequestsForRoute(
1854 bool cancel_requests
) {
1855 BlockedLoadersMap::iterator iter
= blocked_loaders_map_
.find(
1856 GlobalRoutingID(child_id
, route_id
));
1857 if (iter
== blocked_loaders_map_
.end()) {
1858 // It's possible to reach here if the renderer crashed while an interstitial
1859 // page was showing.
1863 BlockedLoadersList
* loaders
= iter
->second
;
1865 // Removing the vector from the map unblocks any subsequent requests.
1866 blocked_loaders_map_
.erase(iter
);
1868 for (BlockedLoadersList::iterator loaders_iter
= loaders
->begin();
1869 loaders_iter
!= loaders
->end(); ++loaders_iter
) {
1870 linked_ptr
<ResourceLoader
> loader
= *loaders_iter
;
1871 ResourceRequestInfoImpl
* info
= loader
->GetRequestInfo();
1872 if (cancel_requests
) {
1873 IncrementOutstandingRequestsMemory(-1, *info
);
1875 StartLoading(info
, loader
);
1882 ResourceDispatcherHostImpl::HttpAuthRelationType
1883 ResourceDispatcherHostImpl::HttpAuthRelationTypeOf(
1884 const GURL
& request_url
,
1885 const GURL
& first_party
) {
1886 if (!first_party
.is_valid())
1887 return HTTP_AUTH_RELATION_TOP
;
1889 if (net::registry_controlled_domains::SameDomainOrHost(
1890 first_party
, request_url
,
1891 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
))
1892 return HTTP_AUTH_RELATION_SAME_DOMAIN
;
1894 if (allow_cross_origin_auth_prompt())
1895 return HTTP_AUTH_RELATION_ALLOWED_CROSS
;
1897 return HTTP_AUTH_RELATION_BLOCKED_CROSS
;
1900 bool ResourceDispatcherHostImpl::allow_cross_origin_auth_prompt() {
1901 return allow_cross_origin_auth_prompt_
;
1904 bool ResourceDispatcherHostImpl::IsTransferredNavigation(
1905 const GlobalRequestID
& id
) const {
1906 ResourceLoader
* loader
= GetLoader(id
);
1907 return loader
? loader
->is_transferring() : false;
1910 ResourceLoader
* ResourceDispatcherHostImpl::GetLoader(
1911 const GlobalRequestID
& id
) const {
1912 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
1914 LoaderMap::const_iterator i
= pending_loaders_
.find(id
);
1915 if (i
== pending_loaders_
.end())
1918 return i
->second
.get();
1921 ResourceLoader
* ResourceDispatcherHostImpl::GetLoader(int child_id
,
1922 int request_id
) const {
1923 return GetLoader(GlobalRequestID(child_id
, request_id
));
1926 void ResourceDispatcherHostImpl::RegisterResourceMessageDelegate(
1927 const GlobalRequestID
& id
, ResourceMessageDelegate
* delegate
) {
1928 DelegateMap::iterator it
= delegate_map_
.find(id
);
1929 if (it
== delegate_map_
.end()) {
1930 it
= delegate_map_
.insert(
1931 std::make_pair(id
, new ObserverList
<ResourceMessageDelegate
>)).first
;
1933 it
->second
->AddObserver(delegate
);
1936 void ResourceDispatcherHostImpl::UnregisterResourceMessageDelegate(
1937 const GlobalRequestID
& id
, ResourceMessageDelegate
* delegate
) {
1938 DCHECK(ContainsKey(delegate_map_
, id
));
1939 DelegateMap::iterator it
= delegate_map_
.find(id
);
1940 DCHECK(it
->second
->HasObserver(delegate
));
1941 it
->second
->RemoveObserver(delegate
);
1942 if (!it
->second
->might_have_observers()) {
1944 delegate_map_
.erase(it
);
1948 int ResourceDispatcherHostImpl::BuildLoadFlagsForRequest(
1949 const ResourceHostMsg_Request
& request_data
,
1951 bool is_sync_load
) {
1952 int load_flags
= request_data
.load_flags
;
1954 // Although EV status is irrelevant to sub-frames and sub-resources, we have
1955 // to perform EV certificate verification on all resources because an HTTP
1956 // keep-alive connection created to load a sub-frame or a sub-resource could
1957 // be reused to load a main frame.
1958 load_flags
|= net::LOAD_VERIFY_EV_CERT
;
1959 if (request_data
.resource_type
== ResourceType::MAIN_FRAME
) {
1960 load_flags
|= net::LOAD_MAIN_FRAME
;
1961 } else if (request_data
.resource_type
== ResourceType::SUB_FRAME
) {
1962 load_flags
|= net::LOAD_SUB_FRAME
;
1963 } else if (request_data
.resource_type
== ResourceType::PREFETCH
) {
1964 load_flags
|= (net::LOAD_PREFETCH
| net::LOAD_DO_NOT_PROMPT_FOR_LOGIN
);
1965 } else if (request_data
.resource_type
== ResourceType::FAVICON
) {
1966 load_flags
|= net::LOAD_DO_NOT_PROMPT_FOR_LOGIN
;
1967 } else if (request_data
.resource_type
== ResourceType::IMAGE
) {
1968 // Prevent third-party image content from prompting for login, as this
1969 // is often a scam to extract credentials for another domain from the user.
1970 // Only block image loads, as the attack applies largely to the "src"
1971 // property of the <img> tag. It is common for web properties to allow
1972 // untrusted values for <img src>; this is considered a fair thing for an
1973 // HTML sanitizer to do. Conversely, any HTML sanitizer that didn't
1974 // filter sources for <script>, <link>, <embed>, <object>, <iframe> tags
1975 // would be considered vulnerable in and of itself.
1976 HttpAuthRelationType relation_type
= HttpAuthRelationTypeOf(
1977 request_data
.url
, request_data
.first_party_for_cookies
);
1978 if (relation_type
== HTTP_AUTH_RELATION_BLOCKED_CROSS
) {
1979 load_flags
|= (net::LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
|
1980 net::LOAD_DO_NOT_PROMPT_FOR_LOGIN
);
1985 load_flags
|= net::LOAD_IGNORE_LIMITS
;
1987 ChildProcessSecurityPolicyImpl
* policy
=
1988 ChildProcessSecurityPolicyImpl::GetInstance();
1989 if (!policy
->CanSendCookiesForOrigin(child_id
, request_data
.url
)) {
1990 load_flags
|= (net::LOAD_DO_NOT_SEND_COOKIES
|
1991 net::LOAD_DO_NOT_SEND_AUTH_DATA
|
1992 net::LOAD_DO_NOT_SAVE_COOKIES
);
1995 // Raw headers are sensitive, as they include Cookie/Set-Cookie, so only
1996 // allow requesting them if requester has ReadRawCookies permission.
1997 if ((load_flags
& net::LOAD_REPORT_RAW_HEADERS
)
1998 && !policy
->CanReadRawCookies(child_id
)) {
1999 VLOG(1) << "Denied unauthorized request for raw headers";
2000 load_flags
&= ~net::LOAD_REPORT_RAW_HEADERS
;
2006 } // namespace content