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

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

Issue 25536005: Clean up ResourceHandler API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 2 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/async_resource_handler.h" 5 #include "content/browser/loader/async_resource_handler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 backing_(backing) { 72 backing_(backing) {
73 } 73 }
74 private: 74 private:
75 virtual ~DependentIOBuffer() {} 75 virtual ~DependentIOBuffer() {}
76 scoped_refptr<ResourceBuffer> backing_; 76 scoped_refptr<ResourceBuffer> backing_;
77 }; 77 };
78 78
79 AsyncResourceHandler::AsyncResourceHandler( 79 AsyncResourceHandler::AsyncResourceHandler(
80 net::URLRequest* request, 80 net::URLRequest* request,
81 ResourceDispatcherHostImpl* rdh) 81 ResourceDispatcherHostImpl* rdh)
82 : ResourceMessageDelegate(request), 82 : ResourceHandler(request),
83 request_(request), 83 ResourceMessageDelegate(request),
84 rdh_(rdh), 84 rdh_(rdh),
85 pending_data_count_(0), 85 pending_data_count_(0),
86 allocation_size_(0), 86 allocation_size_(0),
87 did_defer_(false), 87 did_defer_(false),
88 has_checked_for_sufficient_resources_(false), 88 has_checked_for_sufficient_resources_(false),
89 sent_received_response_msg_(false), 89 sent_received_response_msg_(false),
90 sent_first_data_msg_(false) { 90 sent_first_data_msg_(false) {
91 InitializeResourceBufferConstants(); 91 InitializeResourceBufferConstants();
92 } 92 }
93 93
94 AsyncResourceHandler::~AsyncResourceHandler() { 94 AsyncResourceHandler::~AsyncResourceHandler() {
95 if (has_checked_for_sufficient_resources_) 95 if (has_checked_for_sufficient_resources_)
96 rdh_->FinishedWithResourcesForRequest(request_); 96 rdh_->FinishedWithResourcesForRequest(request());
97 } 97 }
98 98
99 bool AsyncResourceHandler::OnMessageReceived(const IPC::Message& message, 99 bool AsyncResourceHandler::OnMessageReceived(const IPC::Message& message,
100 bool* message_was_ok) { 100 bool* message_was_ok) {
101 bool handled = true; 101 bool handled = true;
102 IPC_BEGIN_MESSAGE_MAP_EX(AsyncResourceHandler, message, *message_was_ok) 102 IPC_BEGIN_MESSAGE_MAP_EX(AsyncResourceHandler, message, *message_was_ok)
103 IPC_MESSAGE_HANDLER(ResourceHostMsg_FollowRedirect, OnFollowRedirect) 103 IPC_MESSAGE_HANDLER(ResourceHostMsg_FollowRedirect, OnFollowRedirect)
104 IPC_MESSAGE_HANDLER(ResourceHostMsg_DataReceived_ACK, OnDataReceivedACK) 104 IPC_MESSAGE_HANDLER(ResourceHostMsg_DataReceived_ACK, OnDataReceivedACK)
105 IPC_MESSAGE_UNHANDLED(handled = false) 105 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP_EX() 106 IPC_END_MESSAGE_MAP_EX()
107 return handled; 107 return handled;
108 } 108 }
109 109
110 void AsyncResourceHandler::OnFollowRedirect( 110 void AsyncResourceHandler::OnFollowRedirect(
111 int request_id, 111 int request_id,
112 bool has_new_first_party_for_cookies, 112 bool has_new_first_party_for_cookies,
113 const GURL& new_first_party_for_cookies) { 113 const GURL& new_first_party_for_cookies) {
114 if (!request_->status().is_success()) { 114 if (!request()->status().is_success()) {
115 DVLOG(1) << "OnFollowRedirect for invalid request"; 115 DVLOG(1) << "OnFollowRedirect for invalid request";
116 return; 116 return;
117 } 117 }
118 118
119 if (has_new_first_party_for_cookies) 119 if (has_new_first_party_for_cookies)
120 request_->set_first_party_for_cookies(new_first_party_for_cookies); 120 request()->set_first_party_for_cookies(new_first_party_for_cookies);
121 121
122 ResumeIfDeferred(); 122 ResumeIfDeferred();
123 } 123 }
124 124
125 void AsyncResourceHandler::OnDataReceivedACK(int request_id) { 125 void AsyncResourceHandler::OnDataReceivedACK(int request_id) {
126 if (pending_data_count_) { 126 if (pending_data_count_) {
127 --pending_data_count_; 127 --pending_data_count_;
128 128
129 buffer_->RecycleLeastRecentlyAllocated(); 129 buffer_->RecycleLeastRecentlyAllocated();
130 if (buffer_->CanAllocate()) 130 if (buffer_->CanAllocate())
131 ResumeIfDeferred(); 131 ResumeIfDeferred();
132 } 132 }
133 } 133 }
134 134
135 bool AsyncResourceHandler::OnUploadProgress(int request_id, 135 bool AsyncResourceHandler::OnUploadProgress(int request_id,
136 uint64 position, 136 uint64 position,
137 uint64 size) { 137 uint64 size) {
138 const ResourceRequestInfoImpl* info = 138 ResourceMessageFilter* filter = GetFilter();
139 ResourceRequestInfoImpl::ForRequest(request_); 139 if (!filter)
140 if (!info->filter())
141 return false; 140 return false;
142 return info->filter()->Send( 141 return filter->Send(
143 new ResourceMsg_UploadProgress(request_id, position, size)); 142 new ResourceMsg_UploadProgress(request_id, position, size));
144 } 143 }
145 144
146 bool AsyncResourceHandler::OnRequestRedirected(int request_id, 145 bool AsyncResourceHandler::OnRequestRedirected(int request_id,
147 const GURL& new_url, 146 const GURL& new_url,
148 ResourceResponse* response, 147 ResourceResponse* response,
149 bool* defer) { 148 bool* defer) {
150 const ResourceRequestInfoImpl* info = 149 const ResourceRequestInfoImpl* info = GetRequestInfo();
151 ResourceRequestInfoImpl::ForRequest(request_);
152 if (!info->filter()) 150 if (!info->filter())
153 return false; 151 return false;
154 152
155 *defer = did_defer_ = true; 153 *defer = did_defer_ = true;
156 154
157 if (rdh_->delegate()) { 155 if (rdh_->delegate()) {
158 rdh_->delegate()->OnRequestRedirected( 156 rdh_->delegate()->OnRequestRedirected(
159 new_url, request_, info->GetContext(), response); 157 new_url, request(), info->GetContext(), response);
160 } 158 }
161 159
162 DevToolsNetLogObserver::PopulateResponseInfo(request_, response); 160 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
163 response->head.request_start = request_->creation_time(); 161 response->head.request_start = request()->creation_time();
164 response->head.response_start = TimeTicks::Now(); 162 response->head.response_start = TimeTicks::Now();
165 return info->filter()->Send(new ResourceMsg_ReceivedRedirect( 163 return info->filter()->Send(new ResourceMsg_ReceivedRedirect(
166 request_id, new_url, response->head)); 164 request_id, new_url, response->head));
167 } 165 }
168 166
169 bool AsyncResourceHandler::OnResponseStarted(int request_id, 167 bool AsyncResourceHandler::OnResponseStarted(int request_id,
170 ResourceResponse* response, 168 ResourceResponse* response,
171 bool* defer) { 169 bool* defer) {
172 // For changes to the main frame, inform the renderer of the new URL's 170 // For changes to the main frame, inform the renderer of the new URL's
173 // per-host settings before the request actually commits. This way the 171 // per-host settings before the request actually commits. This way the
174 // renderer will be able to set these precisely at the time the 172 // renderer will be able to set these precisely at the time the
175 // request commits, avoiding the possibility of e.g. zooming the old content 173 // request commits, avoiding the possibility of e.g. zooming the old content
176 // or of having to layout the new content twice. 174 // or of having to layout the new content twice.
177 175
178 const ResourceRequestInfoImpl* info = 176 const ResourceRequestInfoImpl* info = GetRequestInfo();
179 ResourceRequestInfoImpl::ForRequest(request_);
180 if (!info->filter()) 177 if (!info->filter())
181 return false; 178 return false;
182 179
183 if (rdh_->delegate()) { 180 if (rdh_->delegate()) {
184 rdh_->delegate()->OnResponseStarted( 181 rdh_->delegate()->OnResponseStarted(
185 request_, info->GetContext(), response, info->filter()); 182 request(), info->GetContext(), response, info->filter());
186 } 183 }
187 184
188 DevToolsNetLogObserver::PopulateResponseInfo(request_, response); 185 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
189 186
190 HostZoomMap* host_zoom_map = 187 HostZoomMap* host_zoom_map =
191 GetHostZoomMapForResourceContext(info->GetContext()); 188 GetHostZoomMapForResourceContext(info->GetContext());
192 189
193 if (info->GetResourceType() == ResourceType::MAIN_FRAME && host_zoom_map) { 190 if (info->GetResourceType() == ResourceType::MAIN_FRAME && host_zoom_map) {
194 const GURL& request_url = request_->url(); 191 const GURL& request_url = request()->url();
195 info->filter()->Send(new ViewMsg_SetZoomLevelForLoadingURL( 192 info->filter()->Send(new ViewMsg_SetZoomLevelForLoadingURL(
196 info->GetRouteID(), 193 info->GetRouteID(),
197 request_url, host_zoom_map->GetZoomLevelForHostAndScheme( 194 request_url, host_zoom_map->GetZoomLevelForHostAndScheme(
198 request_url.scheme(), 195 request_url.scheme(),
199 net::GetHostOrSpecFromURL(request_url)))); 196 net::GetHostOrSpecFromURL(request_url))));
200 } 197 }
201 198
202 response->head.request_start = request_->creation_time(); 199 response->head.request_start = request()->creation_time();
203 response->head.response_start = TimeTicks::Now(); 200 response->head.response_start = TimeTicks::Now();
204 info->filter()->Send(new ResourceMsg_ReceivedResponse(request_id, 201 info->filter()->Send(new ResourceMsg_ReceivedResponse(request_id,
205 response->head)); 202 response->head));
206 sent_received_response_msg_ = true; 203 sent_received_response_msg_ = true;
207 204
208 if (request_->response_info().metadata.get()) { 205 if (request()->response_info().metadata.get()) {
209 std::vector<char> copy(request_->response_info().metadata->data(), 206 std::vector<char> copy(request()->response_info().metadata->data(),
210 request_->response_info().metadata->data() + 207 request()->response_info().metadata->data() +
211 request_->response_info().metadata->size()); 208 request()->response_info().metadata->size());
212 info->filter()->Send(new ResourceMsg_ReceivedCachedMetadata(request_id, 209 info->filter()->Send(new ResourceMsg_ReceivedCachedMetadata(request_id,
213 copy)); 210 copy));
214 } 211 }
215 212
216 return true; 213 return true;
217 } 214 }
218 215
219 bool AsyncResourceHandler::OnWillStart(int request_id, 216 bool AsyncResourceHandler::OnWillStart(int request_id,
220 const GURL& url, 217 const GURL& url,
221 bool* defer) { 218 bool* defer) {
222 return true; 219 return true;
223 } 220 }
224 221
225 bool AsyncResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, 222 bool AsyncResourceHandler::OnWillRead(int request_id,
226 int* buf_size, int min_size) { 223 scoped_refptr<net::IOBuffer>* buf,
224 int* buf_size,
225 int min_size) {
227 DCHECK_EQ(-1, min_size); 226 DCHECK_EQ(-1, min_size);
228 227
229 if (!EnsureResourceBufferIsInitialized()) 228 if (!EnsureResourceBufferIsInitialized())
230 return false; 229 return false;
231 230
232 DCHECK(buffer_->CanAllocate()); 231 DCHECK(buffer_->CanAllocate());
233 char* memory = buffer_->Allocate(&allocation_size_); 232 char* memory = buffer_->Allocate(&allocation_size_);
234 CHECK(memory); 233 CHECK(memory);
235 234
236 *buf = new DependentIOBuffer(buffer_.get(), memory); 235 *buf = new DependentIOBuffer(buffer_.get(), memory);
237 *buf_size = allocation_size_; 236 *buf_size = allocation_size_;
238 237
239 UMA_HISTOGRAM_CUSTOM_COUNTS( 238 UMA_HISTOGRAM_CUSTOM_COUNTS(
240 "Net.AsyncResourceHandler_SharedIOBuffer_Alloc", 239 "Net.AsyncResourceHandler_SharedIOBuffer_Alloc",
241 *buf_size, 0, kMaxAllocationSize, 100); 240 *buf_size, 0, kMaxAllocationSize, 100);
242 return true; 241 return true;
243 } 242 }
244 243
245 bool AsyncResourceHandler::OnReadCompleted(int request_id, int bytes_read, 244 bool AsyncResourceHandler::OnReadCompleted(int request_id, int bytes_read,
246 bool* defer) { 245 bool* defer) {
247 if (!bytes_read) 246 if (!bytes_read)
248 return true; 247 return true;
249 248
250 const ResourceRequestInfoImpl* info = 249 ResourceMessageFilter* filter = GetFilter();
251 ResourceRequestInfoImpl::ForRequest(request_); 250 if (!filter)
252 if (!info->filter())
253 return false; 251 return false;
254 252
255 buffer_->ShrinkLastAllocation(bytes_read); 253 buffer_->ShrinkLastAllocation(bytes_read);
256 254
257 UMA_HISTOGRAM_CUSTOM_COUNTS( 255 UMA_HISTOGRAM_CUSTOM_COUNTS(
258 "Net.AsyncResourceHandler_SharedIOBuffer_Used", 256 "Net.AsyncResourceHandler_SharedIOBuffer_Used",
259 bytes_read, 0, kMaxAllocationSize, 100); 257 bytes_read, 0, kMaxAllocationSize, 100);
260 UMA_HISTOGRAM_PERCENTAGE( 258 UMA_HISTOGRAM_PERCENTAGE(
261 "Net.AsyncResourceHandler_SharedIOBuffer_UsedPercentage", 259 "Net.AsyncResourceHandler_SharedIOBuffer_UsedPercentage",
262 CalcUsedPercentage(bytes_read, allocation_size_)); 260 CalcUsedPercentage(bytes_read, allocation_size_));
263 261
264 if (!sent_first_data_msg_) { 262 if (!sent_first_data_msg_) {
265 base::SharedMemoryHandle handle; 263 base::SharedMemoryHandle handle;
266 int size; 264 int size;
267 if (!buffer_->ShareToProcess(info->filter()->PeerHandle(), &handle, &size)) 265 if (!buffer_->ShareToProcess(filter->PeerHandle(), &handle, &size))
268 return false; 266 return false;
269 info->filter()->Send(new ResourceMsg_SetDataBuffer( 267 filter->Send(new ResourceMsg_SetDataBuffer(
270 request_id, handle, size, info->filter()->peer_pid())); 268 request_id, handle, size, filter->peer_pid()));
271 sent_first_data_msg_ = true; 269 sent_first_data_msg_ = true;
272 } 270 }
273 271
274 int data_offset = buffer_->GetLastAllocationOffset(); 272 int data_offset = buffer_->GetLastAllocationOffset();
275 int encoded_data_length = 273 int encoded_data_length =
276 DevToolsNetLogObserver::GetAndResetEncodedDataLength(request_); 274 DevToolsNetLogObserver::GetAndResetEncodedDataLength(request());
277 275
278 info->filter()->Send(new ResourceMsg_DataReceived( 276 filter->Send(new ResourceMsg_DataReceived(
279 request_id, data_offset, bytes_read, encoded_data_length)); 277 request_id, data_offset, bytes_read, encoded_data_length));
280 ++pending_data_count_; 278 ++pending_data_count_;
281 UMA_HISTOGRAM_CUSTOM_COUNTS( 279 UMA_HISTOGRAM_CUSTOM_COUNTS(
282 "Net.AsyncResourceHandler_PendingDataCount", 280 "Net.AsyncResourceHandler_PendingDataCount",
283 pending_data_count_, 0, 100, 100); 281 pending_data_count_, 0, 100, 100);
284 282
285 if (!buffer_->CanAllocate()) { 283 if (!buffer_->CanAllocate()) {
286 UMA_HISTOGRAM_CUSTOM_COUNTS( 284 UMA_HISTOGRAM_CUSTOM_COUNTS(
287 "Net.AsyncResourceHandler_PendingDataCount_WhenFull", 285 "Net.AsyncResourceHandler_PendingDataCount_WhenFull",
288 pending_data_count_, 0, 100, 100); 286 pending_data_count_, 0, 100, 100);
289 *defer = did_defer_ = true; 287 *defer = did_defer_ = true;
290 } 288 }
291 289
292 return true; 290 return true;
293 } 291 }
294 292
295 void AsyncResourceHandler::OnDataDownloaded( 293 void AsyncResourceHandler::OnDataDownloaded(
296 int request_id, int bytes_downloaded) { 294 int request_id, int bytes_downloaded) {
297 int encoded_data_length = 295 int encoded_data_length =
298 DevToolsNetLogObserver::GetAndResetEncodedDataLength(request_); 296 DevToolsNetLogObserver::GetAndResetEncodedDataLength(request());
299 297
300 const ResourceRequestInfoImpl* info = 298 ResourceMessageFilter* filter = GetFilter();
301 ResourceRequestInfoImpl::ForRequest(request_); 299 if (filter) {
302 if (info->filter()) { 300 filter->Send(new ResourceMsg_DataDownloaded(
303 info->filter()->Send(new ResourceMsg_DataDownloaded(
304 request_id, bytes_downloaded, encoded_data_length)); 301 request_id, bytes_downloaded, encoded_data_length));
305 } 302 }
306 } 303 }
307 304
308 bool AsyncResourceHandler::OnResponseCompleted( 305 bool AsyncResourceHandler::OnResponseCompleted(
309 int request_id, 306 int request_id,
310 const net::URLRequestStatus& status, 307 const net::URLRequestStatus& status,
311 const std::string& security_info) { 308 const std::string& security_info) {
312 const ResourceRequestInfoImpl* info = 309 const ResourceRequestInfoImpl* info = GetRequestInfo();
313 ResourceRequestInfoImpl::ForRequest(request_);
314 if (!info->filter()) 310 if (!info->filter())
315 return false; 311 return false;
316 312
317 // If we crash here, figure out what URL the renderer was requesting. 313 // If we crash here, figure out what URL the renderer was requesting.
318 // http://crbug.com/107692 314 // http://crbug.com/107692
319 char url_buf[128]; 315 char url_buf[128];
320 base::strlcpy(url_buf, request_->url().spec().c_str(), arraysize(url_buf)); 316 base::strlcpy(url_buf, request()->url().spec().c_str(), arraysize(url_buf));
321 base::debug::Alias(url_buf); 317 base::debug::Alias(url_buf);
322 318
323 // TODO(gavinp): Remove this CHECK when we figure out the cause of 319 // TODO(gavinp): Remove this CHECK when we figure out the cause of
324 // http://crbug.com/124680 . This check mirrors closely check in 320 // http://crbug.com/124680 . This check mirrors closely check in
325 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore 321 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore
326 // ResourceHandleInternal which asserts on its state and crashes. By crashing 322 // ResourceHandleInternal which asserts on its state and crashes. By crashing
327 // when the message is sent, we should get better crash reports. 323 // when the message is sent, we should get better crash reports.
328 CHECK(status.status() != net::URLRequestStatus::SUCCESS || 324 CHECK(status.status() != net::URLRequestStatus::SUCCESS ||
329 sent_received_response_msg_); 325 sent_received_response_msg_);
330 326
331 TimeTicks completion_time = TimeTicks::Now(); 327 TimeTicks completion_time = TimeTicks::Now();
332 328
333 int error_code = status.error(); 329 int error_code = status.error();
334 bool was_ignored_by_handler = 330 bool was_ignored_by_handler = info->WasIgnoredByHandler();
335 ResourceRequestInfoImpl::ForRequest(request_)->WasIgnoredByHandler();
336 331
337 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING); 332 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING);
338 // If this check fails, then we're in an inconsistent state because all 333 // If this check fails, then we're in an inconsistent state because all
339 // requests ignored by the handler should be canceled (which should result in 334 // requests ignored by the handler should be canceled (which should result in
340 // the ERR_ABORTED error code). 335 // the ERR_ABORTED error code).
341 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); 336 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED);
342 337
343 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus 338 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus
344 // with a status() != SUCCESS and an error_code() == net::OK. 339 // with a status() != SUCCESS and an error_code() == net::OK.
345 if (status.status() == net::URLRequestStatus::CANCELED && 340 if (status.status() == net::URLRequestStatus::CANCELED &&
(...skipping 12 matching lines...) Expand all
358 completion_time)); 353 completion_time));
359 return true; 354 return true;
360 } 355 }
361 356
362 bool AsyncResourceHandler::EnsureResourceBufferIsInitialized() { 357 bool AsyncResourceHandler::EnsureResourceBufferIsInitialized() {
363 if (buffer_.get() && buffer_->IsInitialized()) 358 if (buffer_.get() && buffer_->IsInitialized())
364 return true; 359 return true;
365 360
366 if (!has_checked_for_sufficient_resources_) { 361 if (!has_checked_for_sufficient_resources_) {
367 has_checked_for_sufficient_resources_ = true; 362 has_checked_for_sufficient_resources_ = true;
368 if (!rdh_->HasSufficientResourcesForRequest(request_)) { 363 if (!rdh_->HasSufficientResourcesForRequest(request())) {
369 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES); 364 controller()->CancelWithError(net::ERR_INSUFFICIENT_RESOURCES);
370 return false; 365 return false;
371 } 366 }
372 } 367 }
373 368
374 buffer_ = new ResourceBuffer(); 369 buffer_ = new ResourceBuffer();
375 return buffer_->Initialize(kBufferSize, 370 return buffer_->Initialize(kBufferSize,
376 kMinAllocationSize, 371 kMinAllocationSize,
377 kMaxAllocationSize); 372 kMaxAllocationSize);
378 } 373 }
379 374
380 void AsyncResourceHandler::ResumeIfDeferred() { 375 void AsyncResourceHandler::ResumeIfDeferred() {
381 if (did_defer_) { 376 if (did_defer_) {
382 did_defer_ = false; 377 did_defer_ = false;
383 controller()->Resume(); 378 controller()->Resume();
384 } 379 }
385 } 380 }
386 381
387 } // namespace content 382 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/async_resource_handler.h ('k') | content/browser/loader/buffered_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698