| 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...) Expand all 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 DetachFromCore(); |
| 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_ = base::MakeUnique<net::RedirectInfo>(redirect_info); |
| 89 } | 106 } |
| 90 | 107 |
| 91 void NavigationResourceHandler::OnResponseStarted( | 108 void NavigationResourceHandler::OnResponseStarted( |
| 92 ResourceResponse* response, | 109 ResourceResponse* response, |
| 93 std::unique_ptr<ResourceController> controller) { | 110 std::unique_ptr<ResourceController> controller) { |
| 94 DCHECK(!has_controller()); | 111 DCHECK(!has_controller()); |
| 95 | 112 |
| 96 // The UI thread already cancelled the navigation. Do not proceed. | 113 // The UI thread already cancelled the navigation. Do not proceed. |
| 97 if (!core_) { | 114 if (!core_) { |
| 98 controller->CancelAndIgnore(); | 115 controller->CancelAndIgnore(); |
| 99 return; | 116 return; |
| 100 } | 117 } |
| 101 | 118 |
| 102 ResourceRequestInfoImpl* info = GetRequestInfo(); | 119 ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 103 | 120 |
| 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); | 121 NetLogObserver::PopulateResponseInfo(request(), response); |
| 113 response->head.encoded_data_length = request()->raw_header_size(); | 122 response->head.encoded_data_length = request()->raw_header_size(); |
| 114 | 123 |
| 115 std::unique_ptr<NavigationData> cloned_data; | 124 std::unique_ptr<NavigationData> cloned_data; |
| 116 if (resource_dispatcher_host_delegate_) { | 125 if (resource_dispatcher_host_delegate_) { |
| 117 // Ask the embedder for a NavigationData instance. | 126 // Ask the embedder for a NavigationData instance. |
| 118 NavigationData* navigation_data = | 127 NavigationData* navigation_data = |
| 119 resource_dispatcher_host_delegate_->GetNavigationData(request()); | 128 resource_dispatcher_host_delegate_->GetNavigationData(request()); |
| 120 | 129 |
| 121 // Clone the embedder's NavigationData before moving it to the UI thread. | 130 // Clone the embedder's NavigationData before moving it to the UI thread. |
| 122 if (navigation_data) | 131 if (navigation_data) |
| 123 cloned_data = navigation_data->Clone(); | 132 cloned_data = navigation_data->Clone(); |
| 124 } | 133 } |
| 125 | 134 |
| 126 SSLStatus ssl_status; | 135 SSLStatus ssl_status; |
| 127 if (request()->ssl_info().cert.get()) | 136 if (request()->ssl_info().cert.get()) |
| 128 GetSSLStatusForRequest(request()->ssl_info(), &ssl_status); | 137 GetSSLStatusForRequest(request()->ssl_info(), &ssl_status); |
| 129 | 138 |
| 130 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle(), | 139 core_->NotifyResponseStarted( |
| 131 ssl_status, std::move(cloned_data), | 140 response, std::move(stream_handle_), ssl_status, std::move(cloned_data), |
| 132 info->GetGlobalRequestID(), info->IsDownload(), | 141 info->GetGlobalRequestID(), info->IsDownload(), info->is_stream()); |
| 133 info->is_stream()); | 142 HoldController(std::move(controller)); |
| 134 // Don't defer stream based requests. This includes requests initiated via | 143 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 } | 144 } |
| 178 | 145 |
| 179 void NavigationResourceHandler::OnResponseCompleted( | 146 void NavigationResourceHandler::OnResponseCompleted( |
| 180 const net::URLRequestStatus& status, | 147 const net::URLRequestStatus& status, |
| 181 std::unique_ptr<ResourceController> controller) { | 148 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_) { | 149 if (core_) { |
| 190 DCHECK_NE(net::OK, status.error()); | 150 DCHECK_NE(net::OK, status.error()); |
| 191 core_->NotifyRequestFailed(request()->response_info().was_cached, | 151 core_->NotifyRequestFailed(request()->response_info().was_cached, |
| 192 status.error()); | 152 status.error()); |
| 193 DetachFromCore(); | 153 DetachFromCore(); |
| 194 } | 154 } |
| 195 controller->Resume(); | 155 next_handler_->OnResponseCompleted(status, std::move(controller)); |
| 196 } | |
| 197 | |
| 198 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
| 199 NOTREACHED(); | |
| 200 } | 156 } |
| 201 | 157 |
| 202 void NavigationResourceHandler::DetachFromCore() { | 158 void NavigationResourceHandler::DetachFromCore() { |
| 203 DCHECK(core_); | 159 DCHECK(core_); |
| 204 core_->set_resource_handler(nullptr); | 160 core_->set_resource_handler(nullptr); |
| 205 core_ = nullptr; | 161 core_ = nullptr; |
| 206 } | 162 } |
| 207 | 163 |
| 208 } // namespace content | 164 } // namespace content |
| OLD | NEW |