| 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); |
| 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 set_controller(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 // If the MimeTypeResourceHandler intercepted this request and converted it | 98 // If the MimeTypeResourceHandler intercepted this request and converted it |
| 98 // into a download, it will still call OnResponseStarted and immediately | 99 // into a download, it will still call OnResponseStarted and immediately |
| 99 // cancel. Ignore the call; OnReadCompleted will happen shortly. | 100 // cancel. Ignore the call; OnReadCompleted will happen shortly. |
| 100 // | 101 // |
| 101 // TODO(davidben): Move the dispatch out of MimeTypeResourceHandler. Perhaps | 102 // TODO(davidben): Move the dispatch out of MimeTypeResourceHandler. Perhaps |
| 102 // all the way to the UI thread. Downloads, user certificates, etc., should be | 103 // all the way to the UI thread. Downloads, user certificates, etc., should be |
| 103 // dispatched at the navigation layer. | 104 // dispatched at the navigation layer. |
| 104 if (info->IsDownload()) | 105 if (info->IsDownload()) { |
| 105 return true; | 106 controller->Resume(); |
| 107 return; |
| 108 } |
| 106 | 109 |
| 107 StreamContext* stream_context = | 110 StreamContext* stream_context = |
| 108 GetStreamContextForResourceContext(info->GetContext()); | 111 GetStreamContextForResourceContext(info->GetContext()); |
| 109 writer_.InitializeStream(stream_context->registry(), | 112 writer_.InitializeStream( |
| 110 request()->url().GetOrigin()); | 113 stream_context->registry(), request()->url().GetOrigin(), |
| 114 base::Bind(&NavigationResourceHandler::OutOfBandCancel, |
| 115 base::Unretained(this), net::ERR_ABORTED, true)); |
| 111 | 116 |
| 112 NetLogObserver::PopulateResponseInfo(request(), response); | 117 NetLogObserver::PopulateResponseInfo(request(), response); |
| 113 | 118 |
| 114 std::unique_ptr<NavigationData> cloned_data; | 119 std::unique_ptr<NavigationData> cloned_data; |
| 115 if (resource_dispatcher_host_delegate_) { | 120 if (resource_dispatcher_host_delegate_) { |
| 116 // Ask the embedder for a NavigationData instance. | 121 // Ask the embedder for a NavigationData instance. |
| 117 NavigationData* navigation_data = | 122 NavigationData* navigation_data = |
| 118 resource_dispatcher_host_delegate_->GetNavigationData(request()); | 123 resource_dispatcher_host_delegate_->GetNavigationData(request()); |
| 119 | 124 |
| 120 // Clone the embedder's NavigationData before moving it to the UI thread. | 125 // Clone the embedder's NavigationData before moving it to the UI thread. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 131 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle(), | 136 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle(), |
| 132 ssl_status, std::move(cloned_data), | 137 ssl_status, std::move(cloned_data), |
| 133 info->GetGlobalRequestID(), info->IsDownload(), | 138 info->GetGlobalRequestID(), info->IsDownload(), |
| 134 info->is_stream()); | 139 info->is_stream()); |
| 135 // Don't defer stream based requests. This includes requests initiated via | 140 // Don't defer stream based requests. This includes requests initiated via |
| 136 // mime type sniffing, etc. | 141 // mime type sniffing, etc. |
| 137 // TODO(ananta) | 142 // TODO(ananta) |
| 138 // Make sure that the requests go through the throttle checks. Currently this | 143 // Make sure that the requests go through the throttle checks. Currently this |
| 139 // does not work as the InterceptingResourceHandler is above us and hence it | 144 // does not work as the InterceptingResourceHandler is above us and hence it |
| 140 // does not expect the old handler to defer the request. | 145 // does not expect the old handler to defer the request. |
| 141 if (!info->is_stream()) | 146 if (!info->is_stream()) { |
| 142 *defer = true; | 147 set_controller(std::move(controller)); |
| 143 return true; | 148 } else { |
| 149 controller->Resume(); |
| 150 } |
| 144 } | 151 } |
| 145 | 152 |
| 146 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 153 void NavigationResourceHandler::OnWillStart( |
| 147 return true; | 154 const GURL& url, |
| 155 std::unique_ptr<ResourceController> controller) { |
| 156 DCHECK(!has_controller()); |
| 157 controller->Resume(); |
| 148 } | 158 } |
| 149 | 159 |
| 150 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 160 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 151 int* buf_size, | 161 int* buf_size, |
| 152 int min_size) { | 162 int min_size) { |
| 163 DCHECK(!has_controller()); |
| 153 writer_.OnWillRead(buf, buf_size, min_size); | 164 writer_.OnWillRead(buf, buf_size, min_size); |
| 154 return true; | 165 return true; |
| 155 } | 166 } |
| 156 | 167 |
| 157 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 168 void NavigationResourceHandler::OnReadCompleted( |
| 158 writer_.OnReadCompleted(bytes_read, defer); | 169 int bytes_read, |
| 159 return true; | 170 std::unique_ptr<ResourceController> controller) { |
| 171 DCHECK(!has_controller()); |
| 172 writer_.OnReadCompleted(bytes_read, |
| 173 base::Bind(&ResourceController::Resume, |
| 174 base::Passed(std::move(controller)))); |
| 160 } | 175 } |
| 161 | 176 |
| 162 void NavigationResourceHandler::OnResponseCompleted( | 177 void NavigationResourceHandler::OnResponseCompleted( |
| 163 const net::URLRequestStatus& status, | 178 const net::URLRequestStatus& status, |
| 164 bool* defer) { | 179 std::unique_ptr<ResourceController> controller) { |
| 165 // If the request has already committed, close the stream and leave it as-is. | 180 // If the request has already committed, close the stream and leave it as-is. |
| 166 if (writer_.stream()) { | 181 if (writer_.stream()) { |
| 167 writer_.Finalize(status.error()); | 182 writer_.Finalize(status.error()); |
| 183 controller->Resume(); |
| 168 return; | 184 return; |
| 169 } | 185 } |
| 170 | 186 |
| 171 if (core_) { | 187 if (core_) { |
| 172 DCHECK_NE(net::OK, status.error()); | 188 DCHECK_NE(net::OK, status.error()); |
| 173 core_->NotifyRequestFailed(request()->response_info().was_cached, | 189 core_->NotifyRequestFailed(request()->response_info().was_cached, |
| 174 status.error()); | 190 status.error()); |
| 175 DetachFromCore(); | 191 DetachFromCore(); |
| 176 } | 192 } |
| 193 controller->Resume(); |
| 177 } | 194 } |
| 178 | 195 |
| 179 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 196 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 180 NOTREACHED(); | 197 NOTREACHED(); |
| 181 } | 198 } |
| 182 | 199 |
| 183 void NavigationResourceHandler::DetachFromCore() { | 200 void NavigationResourceHandler::DetachFromCore() { |
| 184 DCHECK(core_); | 201 DCHECK(core_); |
| 185 core_->set_resource_handler(nullptr); | 202 core_->set_resource_handler(nullptr); |
| 186 core_ = nullptr; | 203 core_ = nullptr; |
| 187 } | 204 } |
| 188 | 205 |
| 189 } // namespace content | 206 } // namespace content |
| OLD | NEW |