OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/browser/loader/navigation_url_loader_core.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "content/browser/frame_host/frame_tree_node.h" | |
9 #include "content/browser/frame_host/navigation_request_info.h" | |
10 #include "content/browser/loader/navigation_resource_handler.h" | |
11 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
12 #include "content/public/browser/browser_context.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/stream_handle.h" | |
15 #include "content/public/common/resource_response.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/url_request/redirect_info.h" | |
18 | |
19 namespace content { | |
20 | |
21 NavigationURLLoaderCore::NavigationURLLoaderCore( | |
22 NavigationURLLoader::Delegate* delegate) | |
23 : delegate_(delegate), | |
24 resource_handler_(NULL) { | |
25 } | |
26 | |
27 void NavigationURLLoaderCore::StartRequest( | |
28 BrowserContext* browser_context, | |
29 int64 frame_tree_node_id, | |
30 const NavigationRequestInfo& request_info, | |
31 ResourceRequestBody* request_body) { | |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
33 | |
34 BrowserThread::PostTask( | |
35 BrowserThread::IO, FROM_HERE, | |
36 base::Bind(&NavigationURLLoaderCore::StartRequestOnIOThread, this, | |
37 browser_context->GetResourceContext(), | |
38 frame_tree_node_id, request_info, | |
39 make_scoped_refptr(request_body))); | |
40 } | |
41 | |
42 void NavigationURLLoaderCore::Resume() { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
44 | |
45 BrowserThread::PostTask( | |
46 BrowserThread::IO, FROM_HERE, | |
47 base::Bind(&NavigationURLLoaderCore::ResumeOnIOThread, this)); | |
48 } | |
49 | |
50 void NavigationURLLoaderCore::Cancel() { | |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
52 | |
53 delegate_ = NULL; | |
54 BrowserThread::PostTask( | |
55 BrowserThread::IO, FROM_HERE, | |
56 base::Bind(&NavigationURLLoaderCore::CancelOnIOThread, this)); | |
57 } | |
58 | |
59 void NavigationURLLoaderCore::RequestRedirectedOnIOThread( | |
60 const net::RedirectInfo& redirect_info, | |
61 ResourceResponse* response) { | |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
63 | |
64 BrowserThread::PostTask( | |
65 BrowserThread::UI, FROM_HERE, | |
66 base::Bind(&NavigationURLLoaderCore::CallOnRequestRedirected, | |
67 this, redirect_info, make_scoped_refptr(response))); | |
68 } | |
69 | |
70 void NavigationURLLoaderCore::ResponseStartedOnIOThread( | |
71 ResourceResponse* response, | |
72 scoped_ptr<StreamHandle> body) { | |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
74 | |
75 resource_handler_ = NULL; | |
76 BrowserThread::PostTask( | |
clamy
2014/09/12 20:51:25
Are we sure that after this point ResourceResponse
davidben
2014/09/19 18:30:50
I believe it is safe. I tried to ensure this by ma
| |
77 BrowserThread::UI, FROM_HERE, | |
78 base::Bind(&NavigationURLLoaderCore::CallOnResponseStarted, | |
79 this, make_scoped_refptr(response), base::Passed(&body))); | |
80 } | |
81 | |
82 void NavigationURLLoaderCore::RequestFailedOnIOThread(int net_error) { | |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
84 | |
85 resource_handler_ = NULL; | |
86 BrowserThread::PostTask( | |
87 BrowserThread::UI, FROM_HERE, | |
88 base::Bind(&NavigationURLLoaderCore::CallOnRequestFailed, | |
89 this, net_error)); | |
90 } | |
91 | |
92 NavigationURLLoaderCore::~NavigationURLLoaderCore() { | |
93 DCHECK(!delegate_); | |
94 DCHECK(!resource_handler_); | |
clamy
2014/09/12 20:51:25
When is resource_handler_ set to null? In fact, I
davidben
2014/09/19 18:30:49
I've switched all the NULL setting to explicit cal
| |
95 } | |
96 | |
97 void NavigationURLLoaderCore::StartRequestOnIOThread( | |
98 ResourceContext* resource_context, | |
99 int64 frame_tree_node_id, | |
100 const NavigationRequestInfo& request_info, | |
101 ResourceRequestBody* request_body) { | |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
103 | |
104 if (!ResourceDispatcherHostImpl::Get()->BeginNavigationRequest( | |
105 resource_context, frame_tree_node_id, request_info, request_body, | |
106 this)) { | |
107 DCHECK(!resource_handler_); | |
108 RequestFailedOnIOThread(net::ERR_ABORTED); | |
clamy
2014/09/12 20:51:25
Shouldn't we return here?
davidben
2014/09/19 18:30:50
Oof. Done. Also added a test that exercises this p
| |
109 } | |
110 | |
111 DCHECK(resource_handler_); | |
112 } | |
113 | |
114 void NavigationURLLoaderCore::ResumeOnIOThread() { | |
115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
116 | |
117 if (resource_handler_) | |
118 resource_handler_->Resume(); | |
119 } | |
120 | |
121 void NavigationURLLoaderCore::CancelOnIOThread() { | |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
123 | |
124 if (resource_handler_) | |
125 resource_handler_->Cancel(); | |
126 } | |
127 | |
128 void NavigationURLLoaderCore::CallOnRequestRedirected( | |
129 const net::RedirectInfo& redirect_info, | |
130 ResourceResponse* response) { | |
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
132 | |
133 if (delegate_) | |
134 delegate_->OnRequestRedirected(redirect_info, response); | |
135 } | |
136 | |
137 void NavigationURLLoaderCore::CallOnResponseStarted( | |
138 ResourceResponse* response, | |
139 scoped_ptr<StreamHandle> body) { | |
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
141 | |
142 // If |delegate_| is NULL, OnResponseStarted on the IO thread raced with | |
143 // Cancel on the UI thread. |body| will be destructed and the request released | |
144 // at that point. | |
145 if (delegate_) | |
146 delegate_->OnResponseStarted(response, body.Pass()); | |
147 } | |
148 | |
149 void NavigationURLLoaderCore::CallOnRequestFailed(int net_error) { | |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
151 | |
152 if (delegate_) | |
153 delegate_->OnRequestFailed(net_error); | |
154 } | |
155 | |
156 } // namespace content | |
OLD | NEW |