Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: content/browser/loader/navigation_resource_handler.cc

Issue 2526983002: Refactor ResourceHandler API. (Closed)
Patch Set: Fix stuff Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_loader.h" 13 #include "content/browser/loader/resource_loader.h"
13 #include "content/browser/loader/resource_request_info_impl.h" 14 #include "content/browser/loader/resource_request_info_impl.h"
14 #include "content/browser/resource_context_impl.h" 15 #include "content/browser/resource_context_impl.h"
15 #include "content/browser/streams/stream.h" 16 #include "content/browser/streams/stream.h"
16 #include "content/browser/streams/stream_context.h" 17 #include "content/browser/streams/stream_context.h"
17 #include "content/public/browser/navigation_data.h" 18 #include "content/public/browser/navigation_data.h"
18 #include "content/public/browser/resource_controller.h" 19 #include "content/public/browser/resource_controller.h"
(...skipping 27 matching lines...) Expand all
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 // TODO(mmenke): This is ugle and error prone. Can we do better?
58 ResourceHandler::Cancel();
57 core_ = nullptr; 59 core_ = nullptr;
58 } 60 }
59 61
60 void NavigationResourceHandler::FollowRedirect() { 62 void NavigationResourceHandler::FollowRedirect() {
61 controller()->Resume(); 63 Resume();
62 } 64 }
63 65
64 void NavigationResourceHandler::ProceedWithResponse() { 66 void NavigationResourceHandler::ProceedWithResponse() {
65 // Detach from the loader; at this point, the request is now owned by the 67 // Detach from the loader; at this point, the request is now owned by the
66 // StreamHandle sent in OnResponseStarted. 68 // StreamHandle sent in OnResponseStarted.
67 DetachFromCore(); 69 DetachFromCore();
68 controller()->Resume(); 70 Resume();
69 } 71 }
70 72
71 void NavigationResourceHandler::SetController(ResourceController* controller) { 73 void NavigationResourceHandler::OnRequestRedirected(
72 writer_.set_controller(controller);
73 ResourceHandler::SetController(controller);
74 }
75
76 bool NavigationResourceHandler::OnRequestRedirected(
77 const net::RedirectInfo& redirect_info, 74 const net::RedirectInfo& redirect_info,
78 ResourceResponse* response, 75 ResourceResponse* response,
79 bool* defer) { 76 std::unique_ptr<ResourceController> controller) {
80 DCHECK(core_); 77 DCHECK(core_);
78 DCHECK(!has_controller());
81 79
82 // TODO(davidben): Perform a CSP check here, and anything else that would have 80 // TODO(davidben): Perform a CSP check here, and anything else that would have
83 // been done renderer-side. 81 // been done renderer-side.
84 NetLogObserver::PopulateResponseInfo(request(), response); 82 NetLogObserver::PopulateResponseInfo(request(), response);
85 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); 83 response->head.encoded_data_length = request()->GetTotalReceivedBytes();
86 core_->NotifyRequestRedirected(redirect_info, response); 84 core_->NotifyRequestRedirected(redirect_info, response);
87 *defer = true; 85
88 return true; 86 set_controller(std::move(controller));
89 } 87 }
90 88
91 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, 89 void NavigationResourceHandler::OnResponseStarted(
92 bool* defer) { 90 ResourceResponse* response,
91 std::unique_ptr<ResourceController> controller) {
93 DCHECK(core_); 92 DCHECK(core_);
93 DCHECK(!has_controller());
94 94
95 ResourceRequestInfoImpl* info = GetRequestInfo(); 95 ResourceRequestInfoImpl* info = GetRequestInfo();
96 96
97 // If the MimeTypeResourceHandler intercepted this request and converted it 97 // If the MimeTypeResourceHandler intercepted this request and converted it
98 // into a download, it will still call OnResponseStarted and immediately 98 // into a download, it will still call OnResponseStarted and immediately
99 // cancel. Ignore the call; OnReadCompleted will happen shortly. 99 // cancel. Ignore the call; OnReadCompleted will happen shortly.
100 // 100 //
101 // TODO(davidben): Move the dispatch out of MimeTypeResourceHandler. Perhaps 101 // TODO(davidben): Move the dispatch out of MimeTypeResourceHandler. Perhaps
102 // all the way to the UI thread. Downloads, user certificates, etc., should be 102 // all the way to the UI thread. Downloads, user certificates, etc., should be
103 // dispatched at the navigation layer. 103 // dispatched at the navigation layer.
104 if (info->IsDownload()) 104 if (info->IsDownload()) {
105 return true; 105 controller->Resume();
106 return;
107 }
106 108
107 StreamContext* stream_context = 109 StreamContext* stream_context =
108 GetStreamContextForResourceContext(info->GetContext()); 110 GetStreamContextForResourceContext(info->GetContext());
109 writer_.InitializeStream(stream_context->registry(), 111 writer_.InitializeStream(
110 request()->url().GetOrigin()); 112 stream_context->registry(), request()->url().GetOrigin(),
113 base::Bind(&NavigationResourceHandler::OutOfBandCancel,
114 base::Unretained(this)));
111 115
112 NetLogObserver::PopulateResponseInfo(request(), response); 116 NetLogObserver::PopulateResponseInfo(request(), response);
113 117
114 std::unique_ptr<NavigationData> cloned_data; 118 std::unique_ptr<NavigationData> cloned_data;
115 if (resource_dispatcher_host_delegate_) { 119 if (resource_dispatcher_host_delegate_) {
116 // Ask the embedder for a NavigationData instance. 120 // Ask the embedder for a NavigationData instance.
117 NavigationData* navigation_data = 121 NavigationData* navigation_data =
118 resource_dispatcher_host_delegate_->GetNavigationData(request()); 122 resource_dispatcher_host_delegate_->GetNavigationData(request());
119 123
120 // Clone the embedder's NavigationData before moving it to the UI thread. 124 // Clone the embedder's NavigationData before moving it to the UI thread.
121 if (navigation_data) 125 if (navigation_data)
122 cloned_data = navigation_data->Clone(); 126 cloned_data = navigation_data->Clone();
123 } 127 }
124 128
125 SSLStatus ssl_status; 129 SSLStatus ssl_status;
126 if (request()->ssl_info().cert.get()) { 130 if (request()->ssl_info().cert.get()) {
127 GetSSLStatusForRequest(request()->url(), request()->ssl_info(), 131 GetSSLStatusForRequest(request()->url(), request()->ssl_info(),
128 info->GetChildID(), &ssl_status); 132 info->GetChildID(), &ssl_status);
129 } 133 }
130 134
131 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle(), 135 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle(),
132 ssl_status, std::move(cloned_data)); 136 ssl_status, std::move(cloned_data));
133 // Don't defer stream based requests. This includes requests initiated via 137 // Don't defer stream based requests. This includes requests initiated via
134 // mime type sniffing, etc. 138 // mime type sniffing, etc.
135 // TODO(ananta) 139 // TODO(ananta)
136 // Make sure that the requests go through the throttle checks. Currently this 140 // Make sure that the requests go through the throttle checks. Currently this
137 // does not work as the InterceptingResourceHandler is above us and hence it 141 // does not work as the InterceptingResourceHandler is above us and hence it
138 // does not expect the old handler to defer the request. 142 // does not expect the old handler to defer the request.
139 if (!info->is_stream()) 143 if (!info->is_stream()) {
140 *defer = true; 144 set_controller(std::move(controller));
141 return true; 145 } else {
146 controller->Resume();
147 }
142 } 148 }
143 149
144 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { 150 void NavigationResourceHandler::OnWillStart(
145 return true; 151 const GURL& url,
152 std::unique_ptr<ResourceController> controller) {
153 DCHECK(!has_controller());
154 controller->Resume();
146 } 155 }
147 156
148 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 157 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
149 int* buf_size, 158 int* buf_size,
150 int min_size) { 159 int min_size) {
160 DCHECK(!has_controller());
151 writer_.OnWillRead(buf, buf_size, min_size); 161 writer_.OnWillRead(buf, buf_size, min_size);
152 return true; 162 return true;
153 } 163 }
154 164
155 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { 165 void NavigationResourceHandler::OnReadCompleted(
156 writer_.OnReadCompleted(bytes_read, defer); 166 int bytes_read,
157 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))));
158 } 172 }
159 173
160 void NavigationResourceHandler::OnResponseCompleted( 174 void NavigationResourceHandler::OnResponseCompleted(
161 const net::URLRequestStatus& status, 175 const net::URLRequestStatus& status,
162 bool* defer) { 176 std::unique_ptr<ResourceController> controller) {
163 // 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.
164 if (writer_.stream()) { 178 if (writer_.stream()) {
165 writer_.Finalize(status.error()); 179 writer_.Finalize(status.error());
180 controller->Resume();
166 return; 181 return;
167 } 182 }
168 183
169 if (core_) { 184 if (core_) {
170 DCHECK_NE(net::OK, status.error()); 185 DCHECK_NE(net::OK, status.error());
171 core_->NotifyRequestFailed(request()->response_info().was_cached, 186 core_->NotifyRequestFailed(request()->response_info().was_cached,
172 status.error()); 187 status.error());
173 DetachFromCore(); 188 DetachFromCore();
174 } 189 }
190 controller->Resume();
175 } 191 }
176 192
177 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { 193 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) {
178 NOTREACHED(); 194 NOTREACHED();
179 } 195 }
180 196
181 void NavigationResourceHandler::DetachFromCore() { 197 void NavigationResourceHandler::DetachFromCore() {
182 DCHECK(core_); 198 DCHECK(core_);
183 core_->set_resource_handler(nullptr); 199 core_->set_resource_handler(nullptr);
184 core_ = nullptr; 200 core_ = nullptr;
185 } 201 }
186 202
187 } // namespace content 203 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698