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