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 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, true)); |
101 | 104 |
102 NetLogObserver::PopulateResponseInfo(request(), response); | 105 NetLogObserver::PopulateResponseInfo(request(), response); |
103 | 106 |
104 std::unique_ptr<NavigationData> cloned_data; | 107 std::unique_ptr<NavigationData> cloned_data; |
105 if (resource_dispatcher_host_delegate_) { | 108 if (resource_dispatcher_host_delegate_) { |
106 // Ask the embedder for a NavigationData instance. | 109 // Ask the embedder for a NavigationData instance. |
107 NavigationData* navigation_data = | 110 NavigationData* navigation_data = |
108 resource_dispatcher_host_delegate_->GetNavigationData(request()); | 111 resource_dispatcher_host_delegate_->GetNavigationData(request()); |
109 | 112 |
110 // Clone the embedder's NavigationData before moving it to the UI thread. | 113 // Clone the embedder's NavigationData before moving it to the UI thread. |
(...skipping 17 matching lines...) Expand all Loading... |
128 // Make sure that the requests go through the throttle checks. Currently this | 131 // Make sure that the requests go through the throttle checks. Currently this |
129 // does not work as the InterceptingResourceHandler is above us and hence it | 132 // does not work as the InterceptingResourceHandler is above us and hence it |
130 // does not expect the old handler to defer the request. | 133 // does not expect the old handler to defer the request. |
131 // TODO(clamy): We should also make the downloads wait on the | 134 // TODO(clamy): We should also make the downloads wait on the |
132 // NavigationThrottle checks be performed. Similarly to streams, it doesn't | 135 // NavigationThrottle checks be performed. Similarly to streams, it doesn't |
133 // work because of the InterceptingResourceHandler. | 136 // work because of the InterceptingResourceHandler. |
134 // TODO(clamy): This NavigationResourceHandler should be split in two, with | 137 // TODO(clamy): This NavigationResourceHandler should be split in two, with |
135 // one part that wait on the NavigationThrottle to execute located between the | 138 // one part that wait on the NavigationThrottle to execute located between the |
136 // MIME sniffing and the ResourceThrotlle, and one part that write the | 139 // MIME sniffing and the ResourceThrotlle, and one part that write the |
137 // response to the stream being the leaf ResourceHandler. | 140 // response to the stream being the leaf ResourceHandler. |
138 if (!info->is_stream() && !info->IsDownload()) | 141 if (!info->is_stream() && !info->IsDownload()) { |
139 *defer = true; | 142 HoldController(std::move(controller)); |
140 | 143 } else { |
141 return true; | 144 controller->Resume(); |
| 145 } |
142 } | 146 } |
143 | 147 |
144 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 148 void NavigationResourceHandler::OnWillStart( |
145 return true; | 149 const GURL& url, |
| 150 std::unique_ptr<ResourceController> controller) { |
| 151 DCHECK(!has_controller()); |
| 152 controller->Resume(); |
146 } | 153 } |
147 | 154 |
148 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 155 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
149 int* buf_size, | 156 int* buf_size, |
150 int min_size) { | 157 int min_size) { |
| 158 DCHECK(!has_controller()); |
151 writer_.OnWillRead(buf, buf_size, min_size); | 159 writer_.OnWillRead(buf, buf_size, min_size); |
152 return true; | 160 return true; |
153 } | 161 } |
154 | 162 |
155 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 163 void NavigationResourceHandler::OnReadCompleted( |
156 writer_.OnReadCompleted(bytes_read, defer); | 164 int bytes_read, |
157 return true; | 165 std::unique_ptr<ResourceController> controller) { |
| 166 DCHECK(!has_controller()); |
| 167 writer_.OnReadCompleted(bytes_read, |
| 168 base::Bind(&ResourceController::Resume, |
| 169 base::Passed(std::move(controller)))); |
158 } | 170 } |
159 | 171 |
160 void NavigationResourceHandler::OnResponseCompleted( | 172 void NavigationResourceHandler::OnResponseCompleted( |
161 const net::URLRequestStatus& status, | 173 const net::URLRequestStatus& status, |
162 bool* defer) { | 174 std::unique_ptr<ResourceController> controller) { |
163 // If the request has already committed, close the stream and leave it as-is. | 175 // If the request has already committed, close the stream and leave it as-is. |
164 if (writer_.stream()) { | 176 if (writer_.stream()) { |
165 writer_.Finalize(status.error()); | 177 writer_.Finalize(status.error()); |
| 178 controller->Resume(); |
166 return; | 179 return; |
167 } | 180 } |
168 | 181 |
169 if (core_) { | 182 if (core_) { |
170 DCHECK_NE(net::OK, status.error()); | 183 DCHECK_NE(net::OK, status.error()); |
171 core_->NotifyRequestFailed(request()->response_info().was_cached, | 184 core_->NotifyRequestFailed(request()->response_info().was_cached, |
172 status.error()); | 185 status.error()); |
173 DetachFromCore(); | 186 DetachFromCore(); |
174 } | 187 } |
| 188 controller->Resume(); |
175 } | 189 } |
176 | 190 |
177 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 191 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
178 NOTREACHED(); | 192 NOTREACHED(); |
179 } | 193 } |
180 | 194 |
181 void NavigationResourceHandler::DetachFromCore() { | 195 void NavigationResourceHandler::DetachFromCore() { |
182 DCHECK(core_); | 196 DCHECK(core_); |
183 core_->set_resource_handler(nullptr); | 197 core_->set_resource_handler(nullptr); |
184 core_ = nullptr; | 198 core_ = nullptr; |
185 } | 199 } |
186 | 200 |
187 } // namespace content | 201 } // namespace content |
OLD | NEW |