OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/navigation_resource_handler.h" | 5 #include "content/browser/loader/navigation_resource_handler.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 17 matching lines...) Loading... | |
28 | 28 |
29 void NavigationResourceHandler::GetSSLStatusForRequest( | 29 void NavigationResourceHandler::GetSSLStatusForRequest( |
30 const net::SSLInfo& ssl_info, | 30 const net::SSLInfo& ssl_info, |
31 SSLStatus* ssl_status) { | 31 SSLStatus* ssl_status) { |
32 DCHECK(ssl_info.cert); | 32 DCHECK(ssl_info.cert); |
33 *ssl_status = SSLStatus(ssl_info); | 33 *ssl_status = SSLStatus(ssl_info); |
34 } | 34 } |
35 | 35 |
36 NavigationResourceHandler::NavigationResourceHandler( | 36 NavigationResourceHandler::NavigationResourceHandler( |
37 net::URLRequest* request, | 37 net::URLRequest* request, |
38 std::unique_ptr<ResourceHandler> next_handler, | |
38 NavigationURLLoaderImplCore* core, | 39 NavigationURLLoaderImplCore* core, |
39 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate) | 40 ResourceDispatcherHostDelegate* resource_dispatcher_host_delegate, |
40 : ResourceHandler(request), | 41 std::unique_ptr<StreamHandle> stream_handle) |
42 : LayeredResourceHandler(request, std::move(next_handler)), | |
41 core_(core), | 43 core_(core), |
44 stream_handle_(std::move(stream_handle)), | |
42 resource_dispatcher_host_delegate_(resource_dispatcher_host_delegate) { | 45 resource_dispatcher_host_delegate_(resource_dispatcher_host_delegate) { |
43 core_->set_resource_handler(this); | 46 core_->set_resource_handler(this); |
44 writer_.set_immediate_mode(true); | |
45 } | 47 } |
46 | 48 |
47 NavigationResourceHandler::~NavigationResourceHandler() { | 49 NavigationResourceHandler::~NavigationResourceHandler() { |
48 if (core_) { | 50 if (core_) { |
49 core_->NotifyRequestFailed(false, net::ERR_ABORTED); | 51 core_->NotifyRequestFailed(false, net::ERR_ABORTED); |
50 DetachFromCore(); | 52 DetachFromCore(); |
51 } | 53 } |
52 } | 54 } |
53 | 55 |
54 void NavigationResourceHandler::Cancel() { | 56 void NavigationResourceHandler::Cancel() { |
55 if (core_) { | 57 if (core_) { |
56 core_ = nullptr; | 58 core_ = nullptr; |
mmenke
2017/04/28 16:02:20
Why does this just set core_ to nullptr instead of
clamy
2017/05/02 15:05:44
No idea. I have changed it in the latest patchset.
| |
57 OutOfBandCancel(net::ERR_ABORTED, true /* tell_renderer */); | 59 if (has_controller()) { |
60 CancelAndIgnore(); | |
61 } else { | |
62 OutOfBandCancel(net::ERR_ABORTED, true /* tell_renderer */); | |
63 } | |
58 } | 64 } |
59 } | 65 } |
60 | 66 |
61 void NavigationResourceHandler::FollowRedirect() { | 67 void NavigationResourceHandler::FollowRedirect() { |
62 Resume(); | 68 DCHECK(response_); |
69 DCHECK(redirect_info_); | |
70 DCHECK(has_controller()); | |
71 next_handler_->OnRequestRedirected(*redirect_info_, response_.get(), | |
72 ReleaseController()); | |
73 response_ = nullptr; | |
74 redirect_info_ = nullptr; | |
63 } | 75 } |
64 | 76 |
65 void NavigationResourceHandler::ProceedWithResponse() { | 77 void NavigationResourceHandler::ProceedWithResponse() { |
78 DCHECK(response_); | |
79 DCHECK(has_controller()); | |
66 // Detach from the loader; at this point, the request is now owned by the | 80 // Detach from the loader; at this point, the request is now owned by the |
67 // StreamHandle sent in OnResponseStarted. | 81 // StreamHandle sent in OnResponseStarted. |
68 DetachFromCore(); | 82 DetachFromCore(); |
69 Resume(); | 83 next_handler_->OnResponseStarted(response_.get(), ReleaseController()); |
84 response_ = nullptr; | |
70 } | 85 } |
71 | 86 |
72 void NavigationResourceHandler::OnRequestRedirected( | 87 void NavigationResourceHandler::OnRequestRedirected( |
73 const net::RedirectInfo& redirect_info, | 88 const net::RedirectInfo& redirect_info, |
74 ResourceResponse* response, | 89 ResourceResponse* response, |
75 std::unique_ptr<ResourceController> controller) { | 90 std::unique_ptr<ResourceController> controller) { |
76 DCHECK(!has_controller()); | 91 DCHECK(!has_controller()); |
77 | 92 |
78 // The UI thread already cancelled the navigation. Do not proceed. | 93 // The UI thread already cancelled the navigation. Do not proceed. |
79 if (!core_) { | 94 if (!core_) { |
80 controller->CancelAndIgnore(); | 95 controller->CancelAndIgnore(); |
81 return; | 96 return; |
82 } | 97 } |
83 | 98 |
84 NetLogObserver::PopulateResponseInfo(request(), response); | 99 NetLogObserver::PopulateResponseInfo(request(), response); |
85 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); | 100 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); |
86 core_->NotifyRequestRedirected(redirect_info, response); | 101 core_->NotifyRequestRedirected(redirect_info, response); |
87 | 102 |
88 HoldController(std::move(controller)); | 103 HoldController(std::move(controller)); |
104 response_ = response; | |
105 redirect_info_ = | |
106 std::unique_ptr<net::RedirectInfo>(new net::RedirectInfo(redirect_info)); | |
mmenke
2017/04/28 16:02:20
base::MakeUnique<net::RedirectInfo>(redirect_info)
clamy
2017/05/02 15:05:44
Done. I made it a unique_ptr in order not to initi
mmenke
2017/05/02 15:55:39
I'd tend to just make it a plain member variable,
| |
89 } | 107 } |
90 | 108 |
91 void NavigationResourceHandler::OnResponseStarted( | 109 void NavigationResourceHandler::OnResponseStarted( |
92 ResourceResponse* response, | 110 ResourceResponse* response, |
93 std::unique_ptr<ResourceController> controller) { | 111 std::unique_ptr<ResourceController> controller) { |
94 DCHECK(!has_controller()); | 112 DCHECK(!has_controller()); |
95 | 113 |
96 // The UI thread already cancelled the navigation. Do not proceed. | 114 // The UI thread already cancelled the navigation. Do not proceed. |
97 if (!core_) { | 115 if (!core_) { |
98 controller->CancelAndIgnore(); | 116 controller->CancelAndIgnore(); |
99 return; | 117 return; |
100 } | 118 } |
101 | 119 |
102 ResourceRequestInfoImpl* info = GetRequestInfo(); | 120 ResourceRequestInfoImpl* info = GetRequestInfo(); |
103 | 121 |
104 StreamContext* stream_context = | |
105 GetStreamContextForResourceContext(info->GetContext()); | |
106 writer_.InitializeStream( | |
107 stream_context->registry(), request()->url().GetOrigin(), | |
108 base::Bind(&NavigationResourceHandler::OutOfBandCancel, | |
109 base::Unretained(this), net::ERR_ABORTED, | |
110 true /* tell_renderer */)); | |
111 | |
112 NetLogObserver::PopulateResponseInfo(request(), response); | 122 NetLogObserver::PopulateResponseInfo(request(), response); |
113 response->head.encoded_data_length = request()->raw_header_size(); | 123 response->head.encoded_data_length = request()->raw_header_size(); |
114 | 124 |
115 std::unique_ptr<NavigationData> cloned_data; | 125 std::unique_ptr<NavigationData> cloned_data; |
116 if (resource_dispatcher_host_delegate_) { | 126 if (resource_dispatcher_host_delegate_) { |
117 // Ask the embedder for a NavigationData instance. | 127 // Ask the embedder for a NavigationData instance. |
118 NavigationData* navigation_data = | 128 NavigationData* navigation_data = |
119 resource_dispatcher_host_delegate_->GetNavigationData(request()); | 129 resource_dispatcher_host_delegate_->GetNavigationData(request()); |
120 | 130 |
121 // Clone the embedder's NavigationData before moving it to the UI thread. | 131 // Clone the embedder's NavigationData before moving it to the UI thread. |
122 if (navigation_data) | 132 if (navigation_data) |
123 cloned_data = navigation_data->Clone(); | 133 cloned_data = navigation_data->Clone(); |
124 } | 134 } |
125 | 135 |
126 SSLStatus ssl_status; | 136 SSLStatus ssl_status; |
127 if (request()->ssl_info().cert.get()) | 137 if (request()->ssl_info().cert.get()) |
128 GetSSLStatusForRequest(request()->ssl_info(), &ssl_status); | 138 GetSSLStatusForRequest(request()->ssl_info(), &ssl_status); |
129 | 139 |
130 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle(), | 140 core_->NotifyResponseStarted( |
131 ssl_status, std::move(cloned_data), | 141 response, std::move(stream_handle_), ssl_status, std::move(cloned_data), |
132 info->GetGlobalRequestID(), info->IsDownload(), | 142 info->GetGlobalRequestID(), info->IsDownload(), info->is_stream()); |
133 info->is_stream()); | 143 HoldController(std::move(controller)); |
134 // Don't defer stream based requests. This includes requests initiated via | 144 response_ = response; |
135 // mime type sniffing, etc. | |
136 // TODO(ananta) | |
137 // Make sure that the requests go through the throttle checks. Currently this | |
138 // does not work as the InterceptingResourceHandler is above us and hence it | |
139 // does not expect the old handler to defer the request. | |
140 // TODO(clamy): We should also make the downloads wait on the | |
141 // NavigationThrottle checks be performed. Similarly to streams, it doesn't | |
142 // work because of the InterceptingResourceHandler. | |
143 // TODO(clamy): This NavigationResourceHandler should be split in two, with | |
144 // one part that wait on the NavigationThrottle to execute located between the | |
145 // MIME sniffing and the ResourceThrotlle, and one part that write the | |
146 // response to the stream being the leaf ResourceHandler. | |
147 if (info->is_stream() || info->IsDownload()) { | |
148 controller->Resume(); | |
149 } else { | |
150 HoldController(std::move(controller)); | |
151 } | |
152 } | |
153 | |
154 void NavigationResourceHandler::OnWillStart( | |
155 const GURL& url, | |
156 std::unique_ptr<ResourceController> controller) { | |
157 DCHECK(!has_controller()); | |
158 controller->Resume(); | |
159 } | |
160 | |
161 void NavigationResourceHandler::OnWillRead( | |
162 scoped_refptr<net::IOBuffer>* buf, | |
163 int* buf_size, | |
164 std::unique_ptr<ResourceController> controller) { | |
165 DCHECK(!has_controller()); | |
166 writer_.OnWillRead(buf, buf_size, -1); | |
167 controller->Resume(); | |
168 } | |
169 | |
170 void NavigationResourceHandler::OnReadCompleted( | |
171 int bytes_read, | |
172 std::unique_ptr<ResourceController> controller) { | |
173 DCHECK(!has_controller()); | |
174 writer_.OnReadCompleted(bytes_read, | |
175 base::Bind(&ResourceController::Resume, | |
176 base::Passed(std::move(controller)))); | |
177 } | 145 } |
178 | 146 |
179 void NavigationResourceHandler::OnResponseCompleted( | 147 void NavigationResourceHandler::OnResponseCompleted( |
180 const net::URLRequestStatus& status, | 148 const net::URLRequestStatus& status, |
181 std::unique_ptr<ResourceController> controller) { | 149 std::unique_ptr<ResourceController> controller) { |
182 // If the request has already committed, close the stream and leave it as-is. | |
183 if (writer_.stream()) { | |
184 writer_.Finalize(status.error()); | |
185 controller->Resume(); | |
186 return; | |
187 } | |
188 | |
189 if (core_) { | 150 if (core_) { |
190 DCHECK_NE(net::OK, status.error()); | 151 DCHECK_NE(net::OK, status.error()); |
191 core_->NotifyRequestFailed(request()->response_info().was_cached, | 152 core_->NotifyRequestFailed(request()->response_info().was_cached, |
192 status.error()); | 153 status.error()); |
193 DetachFromCore(); | 154 DetachFromCore(); |
194 } | 155 } |
195 controller->Resume(); | 156 next_handler_->OnResponseCompleted(status, std::move(controller)); |
mmenke
2017/04/28 16:02:20
So I guess we now unconditionally create writer_.s
clamy
2017/05/02 15:05:44
Based on the tests, it seems to work. When we have
| |
196 } | |
197 | |
198 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
199 NOTREACHED(); | |
200 } | 157 } |
201 | 158 |
202 void NavigationResourceHandler::DetachFromCore() { | 159 void NavigationResourceHandler::DetachFromCore() { |
203 DCHECK(core_); | 160 DCHECK(core_); |
204 core_->set_resource_handler(nullptr); | 161 core_->set_resource_handler(nullptr); |
205 core_ = nullptr; | 162 core_ = nullptr; |
206 } | 163 } |
207 | 164 |
208 } // namespace content | 165 } // namespace content |
OLD | NEW |