1 // Copyright 2014 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 "content/browser/loader/navigation_url_loader_impl_core.h"
8 #include "base/location.h"
9 #include "base/time/time.h"
10 #include "content/browser/frame_host/navigation_request_info.h"
11 #include "content/browser/loader/navigation_resource_handler.h"
12 #include "content/browser/loader/resource_dispatcher_host_impl.h"
13 #include "content/common/navigation_params.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/stream_handle.h"
16 #include "content/public/common/resource_response.h"
17 #include "net/base/net_errors.h"
18 #include "net/url_request/redirect_info.h"
22 NavigationURLLoaderImplCore::NavigationURLLoaderImplCore(
23 const base::WeakPtr
<NavigationURLLoaderImpl
>& loader
)
25 resource_handler_(nullptr) {
26 // This object is created on the UI thread but otherwise is accessed and
27 // deleted on the IO thread.
28 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
31 NavigationURLLoaderImplCore::~NavigationURLLoaderImplCore() {
32 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
34 if (resource_handler_
)
35 resource_handler_
->Cancel();
38 void NavigationURLLoaderImplCore::Start(
39 ResourceContext
* resource_context
,
40 int frame_tree_node_id
,
41 scoped_ptr
<NavigationRequestInfo
> request_info
) {
42 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
44 BrowserThread::PostTask(
45 BrowserThread::UI
, FROM_HERE
,
46 base::Bind(&NavigationURLLoaderImpl::NotifyRequestStarted
, loader_
,
47 base::TimeTicks::Now()));
49 ResourceDispatcherHostImpl::Get()->BeginNavigationRequest(
50 resource_context
, frame_tree_node_id
,
54 void NavigationURLLoaderImplCore::FollowRedirect() {
55 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
57 if (resource_handler_
)
58 resource_handler_
->FollowRedirect();
62 void NavigationURLLoaderImplCore::NotifyRequestRedirected(
63 const net::RedirectInfo
& redirect_info
,
64 ResourceResponse
* response
) {
65 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
67 // Make a copy of the ResourceResponse before it is passed to another thread.
69 // TODO(davidben): This copy could be avoided if ResourceResponse weren't
70 // reference counted and the loader stack passed unique ownership of the
71 // response. https://crbug.com/416050
72 BrowserThread::PostTask(
73 BrowserThread::UI
, FROM_HERE
,
74 base::Bind(&NavigationURLLoaderImpl::NotifyRequestRedirected
, loader_
,
75 redirect_info
, response
->DeepCopy()));
78 void NavigationURLLoaderImplCore::NotifyResponseStarted(
79 ResourceResponse
* response
,
80 scoped_ptr
<StreamHandle
> body
) {
81 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
83 // If, by the time the task reaches the UI thread, |loader_| has already been
84 // destroyed, NotifyResponseStarted will not run. |body| will be destructed
85 // and the request released at that point.
87 // Make a copy of the ResourceResponse before it is passed to another thread.
89 // TODO(davidben): This copy could be avoided if ResourceResponse weren't
90 // reference counted and the loader stack passed unique ownership of the
91 // response. https://crbug.com/416050
92 BrowserThread::PostTask(
93 BrowserThread::UI
, FROM_HERE
,
94 base::Bind(&NavigationURLLoaderImpl::NotifyResponseStarted
, loader_
,
95 response
->DeepCopy(), base::Passed(&body
)));
98 void NavigationURLLoaderImplCore::NotifyRequestFailed(bool in_cache
,
100 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
102 BrowserThread::PostTask(
103 BrowserThread::UI
, FROM_HERE
,
104 base::Bind(&NavigationURLLoaderImpl::NotifyRequestFailed
, loader_
,
105 in_cache
, net_error
));
108 } // namespace content