| 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/logging.h" | 10 #include "base/logging.h" |
| 10 #include "content/browser/loader/navigation_url_loader_impl_core.h" | 11 #include "content/browser/loader/navigation_url_loader_impl_core.h" |
| 11 #include "content/browser/loader/netlog_observer.h" | 12 #include "content/browser/loader/netlog_observer.h" |
| 12 #include "content/browser/loader/resource_controller.h" | 13 #include "content/browser/loader/resource_controller.h" |
| 13 #include "content/browser/loader/resource_loader.h" | 14 #include "content/browser/loader/resource_loader.h" |
| 14 #include "content/browser/loader/resource_request_info_impl.h" | 15 #include "content/browser/loader/resource_request_info_impl.h" |
| 15 #include "content/browser/resource_context_impl.h" | 16 #include "content/browser/resource_context_impl.h" |
| 16 #include "content/browser/streams/stream.h" | 17 #include "content/browser/streams/stream.h" |
| 17 #include "content/browser/streams/stream_context.h" | 18 #include "content/browser/streams/stream_context.h" |
| 18 #include "content/public/browser/navigation_data.h" | 19 #include "content/public/browser/navigation_data.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 46 } | 47 } |
| 47 | 48 |
| 48 NavigationResourceHandler::~NavigationResourceHandler() { | 49 NavigationResourceHandler::~NavigationResourceHandler() { |
| 49 if (core_) { | 50 if (core_) { |
| 50 core_->NotifyRequestFailed(false, net::ERR_ABORTED); | 51 core_->NotifyRequestFailed(false, net::ERR_ABORTED); |
| 51 DetachFromCore(); | 52 DetachFromCore(); |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 void NavigationResourceHandler::Cancel() { | 56 void NavigationResourceHandler::Cancel() { |
| 56 controller()->Cancel(); | 57 if (core_) { |
| 57 core_ = nullptr; | 58 core_ = nullptr; |
| 59 OutOfBandCancel(net::ERR_ABORTED, true /* tell_renderer */); |
| 60 } |
| 58 } | 61 } |
| 59 | 62 |
| 60 void NavigationResourceHandler::FollowRedirect() { | 63 void NavigationResourceHandler::FollowRedirect() { |
| 61 controller()->Resume(); | 64 Resume(); |
| 62 } | 65 } |
| 63 | 66 |
| 64 void NavigationResourceHandler::ProceedWithResponse() { | 67 void NavigationResourceHandler::ProceedWithResponse() { |
| 65 // Detach from the loader; at this point, the request is now owned by the | 68 // Detach from the loader; at this point, the request is now owned by the |
| 66 // StreamHandle sent in OnResponseStarted. | 69 // StreamHandle sent in OnResponseStarted. |
| 67 DetachFromCore(); | 70 DetachFromCore(); |
| 68 controller()->Resume(); | 71 Resume(); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void NavigationResourceHandler::SetController(ResourceController* controller) { | 74 void NavigationResourceHandler::OnRequestRedirected( |
| 72 writer_.set_controller(controller); | |
| 73 ResourceHandler::SetController(controller); | |
| 74 } | |
| 75 | |
| 76 bool NavigationResourceHandler::OnRequestRedirected( | |
| 77 const net::RedirectInfo& redirect_info, | 75 const net::RedirectInfo& redirect_info, |
| 78 ResourceResponse* response, | 76 ResourceResponse* response, |
| 79 bool* defer) { | 77 std::unique_ptr<ResourceController> controller) { |
| 80 DCHECK(core_); | 78 DCHECK(core_); |
| 79 DCHECK(!has_controller()); |
| 81 | 80 |
| 82 // TODO(davidben): Perform a CSP check here, and anything else that would have | 81 // TODO(davidben): Perform a CSP check here, and anything else that would have |
| 83 // been done renderer-side. | 82 // been done renderer-side. |
| 84 NetLogObserver::PopulateResponseInfo(request(), response); | 83 NetLogObserver::PopulateResponseInfo(request(), response); |
| 85 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); | 84 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); |
| 86 core_->NotifyRequestRedirected(redirect_info, response); | 85 core_->NotifyRequestRedirected(redirect_info, response); |
| 87 *defer = true; | 86 |
| 88 return true; | 87 HoldController(std::move(controller)); |
| 89 } | 88 } |
| 90 | 89 |
| 91 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, | 90 void NavigationResourceHandler::OnResponseStarted( |
| 92 bool* defer) { | 91 ResourceResponse* response, |
| 92 std::unique_ptr<ResourceController> controller) { |
| 93 DCHECK(core_); | 93 DCHECK(core_); |
| 94 DCHECK(!has_controller()); |
| 94 | 95 |
| 95 ResourceRequestInfoImpl* info = GetRequestInfo(); | 96 ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 96 | 97 |
| 97 StreamContext* stream_context = | 98 StreamContext* stream_context = |
| 98 GetStreamContextForResourceContext(info->GetContext()); | 99 GetStreamContextForResourceContext(info->GetContext()); |
| 99 writer_.InitializeStream(stream_context->registry(), | 100 writer_.InitializeStream( |
| 100 request()->url().GetOrigin()); | 101 stream_context->registry(), request()->url().GetOrigin(), |
| 102 base::Bind(&NavigationResourceHandler::OutOfBandCancel, |
| 103 base::Unretained(this), net::ERR_ABORTED, |
| 104 true /* tell_renderer */)); |
| 101 | 105 |
| 102 NetLogObserver::PopulateResponseInfo(request(), response); | 106 NetLogObserver::PopulateResponseInfo(request(), response); |
| 103 response->head.encoded_data_length = request()->raw_header_size(); | 107 response->head.encoded_data_length = request()->raw_header_size(); |
| 104 | 108 |
| 105 std::unique_ptr<NavigationData> cloned_data; | 109 std::unique_ptr<NavigationData> cloned_data; |
| 106 if (resource_dispatcher_host_delegate_) { | 110 if (resource_dispatcher_host_delegate_) { |
| 107 // Ask the embedder for a NavigationData instance. | 111 // Ask the embedder for a NavigationData instance. |
| 108 NavigationData* navigation_data = | 112 NavigationData* navigation_data = |
| 109 resource_dispatcher_host_delegate_->GetNavigationData(request()); | 113 resource_dispatcher_host_delegate_->GetNavigationData(request()); |
| 110 | 114 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 129 // Make sure that the requests go through the throttle checks. Currently this | 133 // Make sure that the requests go through the throttle checks. Currently this |
| 130 // does not work as the InterceptingResourceHandler is above us and hence it | 134 // does not work as the InterceptingResourceHandler is above us and hence it |
| 131 // does not expect the old handler to defer the request. | 135 // does not expect the old handler to defer the request. |
| 132 // TODO(clamy): We should also make the downloads wait on the | 136 // TODO(clamy): We should also make the downloads wait on the |
| 133 // NavigationThrottle checks be performed. Similarly to streams, it doesn't | 137 // NavigationThrottle checks be performed. Similarly to streams, it doesn't |
| 134 // work because of the InterceptingResourceHandler. | 138 // work because of the InterceptingResourceHandler. |
| 135 // TODO(clamy): This NavigationResourceHandler should be split in two, with | 139 // TODO(clamy): This NavigationResourceHandler should be split in two, with |
| 136 // one part that wait on the NavigationThrottle to execute located between the | 140 // one part that wait on the NavigationThrottle to execute located between the |
| 137 // MIME sniffing and the ResourceThrotlle, and one part that write the | 141 // MIME sniffing and the ResourceThrotlle, and one part that write the |
| 138 // response to the stream being the leaf ResourceHandler. | 142 // response to the stream being the leaf ResourceHandler. |
| 139 if (!info->is_stream() && !info->IsDownload()) | 143 if (info->is_stream() || info->IsDownload()) { |
| 140 *defer = true; | 144 controller->Resume(); |
| 141 | 145 } else { |
| 142 return true; | 146 HoldController(std::move(controller)); |
| 147 } |
| 143 } | 148 } |
| 144 | 149 |
| 145 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 150 void NavigationResourceHandler::OnWillStart( |
| 146 return true; | 151 const GURL& url, |
| 152 std::unique_ptr<ResourceController> controller) { |
| 153 DCHECK(!has_controller()); |
| 154 controller->Resume(); |
| 147 } | 155 } |
| 148 | 156 |
| 149 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 157 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 150 int* buf_size, | 158 int* buf_size, |
| 151 int min_size) { | 159 int min_size) { |
| 160 DCHECK(!has_controller()); |
| 152 writer_.OnWillRead(buf, buf_size, min_size); | 161 writer_.OnWillRead(buf, buf_size, min_size); |
| 153 return true; | 162 return true; |
| 154 } | 163 } |
| 155 | 164 |
| 156 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 165 void NavigationResourceHandler::OnReadCompleted( |
| 157 writer_.OnReadCompleted(bytes_read, defer); | 166 int bytes_read, |
| 158 return true; | 167 std::unique_ptr<ResourceController> controller) { |
| 168 DCHECK(!has_controller()); |
| 169 writer_.OnReadCompleted(bytes_read, |
| 170 base::Bind(&ResourceController::Resume, |
| 171 base::Passed(std::move(controller)))); |
| 159 } | 172 } |
| 160 | 173 |
| 161 void NavigationResourceHandler::OnResponseCompleted( | 174 void NavigationResourceHandler::OnResponseCompleted( |
| 162 const net::URLRequestStatus& status, | 175 const net::URLRequestStatus& status, |
| 163 bool* defer) { | 176 std::unique_ptr<ResourceController> controller) { |
| 164 // If the request has already committed, close the stream and leave it as-is. | 177 // If the request has already committed, close the stream and leave it as-is. |
| 165 if (writer_.stream()) { | 178 if (writer_.stream()) { |
| 166 writer_.Finalize(status.error()); | 179 writer_.Finalize(status.error()); |
| 180 controller->Resume(); |
| 167 return; | 181 return; |
| 168 } | 182 } |
| 169 | 183 |
| 170 if (core_) { | 184 if (core_) { |
| 171 DCHECK_NE(net::OK, status.error()); | 185 DCHECK_NE(net::OK, status.error()); |
| 172 core_->NotifyRequestFailed(request()->response_info().was_cached, | 186 core_->NotifyRequestFailed(request()->response_info().was_cached, |
| 173 status.error()); | 187 status.error()); |
| 174 DetachFromCore(); | 188 DetachFromCore(); |
| 175 } | 189 } |
| 190 controller->Resume(); |
| 176 } | 191 } |
| 177 | 192 |
| 178 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 193 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 179 NOTREACHED(); | 194 NOTREACHED(); |
| 180 } | 195 } |
| 181 | 196 |
| 182 void NavigationResourceHandler::DetachFromCore() { | 197 void NavigationResourceHandler::DetachFromCore() { |
| 183 DCHECK(core_); | 198 DCHECK(core_); |
| 184 core_->set_resource_handler(nullptr); | 199 core_->set_resource_handler(nullptr); |
| 185 core_ = nullptr; | 200 core_ = nullptr; |
| 186 } | 201 } |
| 187 | 202 |
| 188 } // namespace content | 203 } // namespace content |
| OLD | NEW |