OLD | NEW |
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/buffered_resource_handler.h" | 5 #include "content/browser/loader/buffered_resource_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // TODO(darin): It is very odd to special-case 304 responses at this level. | 112 // TODO(darin): It is very odd to special-case 304 responses at this level. |
113 // We do so only because the code always has, see r24977 and r29355. The | 113 // We do so only because the code always has, see r24977 and r29355. The |
114 // fact that 204 is no longer special-cased this way suggests that 304 need | 114 // fact that 204 is no longer special-cased this way suggests that 304 need |
115 // not be special-cased either. | 115 // not be special-cased either. |
116 // | 116 // |
117 // The network stack only forwards 304 responses that were not received in | 117 // The network stack only forwards 304 responses that were not received in |
118 // response to a conditional request (i.e., If-Modified-Since). Other 304 | 118 // response to a conditional request (i.e., If-Modified-Since). Other 304 |
119 // responses end up being translated to 200 or whatever the cached response | 119 // responses end up being translated to 200 or whatever the cached response |
120 // code happens to be. It should be very rare to see a 304 at this level. | 120 // code happens to be. It should be very rare to see a 304 at this level. |
121 | 121 |
122 if (!(response_->head.headers && | 122 if (!(response_->head.headers.get() && |
123 response_->head.headers->response_code() == 304)) { | 123 response_->head.headers->response_code() == 304)) { |
124 if (ShouldSniffContent()) { | 124 if (ShouldSniffContent()) { |
125 state_ = STATE_BUFFERING; | 125 state_ = STATE_BUFFERING; |
126 return true; | 126 return true; |
127 } | 127 } |
128 | 128 |
129 if (response_->head.mime_type.empty()) { | 129 if (response_->head.mime_type.empty()) { |
130 // Ugg. The server told us not to sniff the content but didn't give us | 130 // Ugg. The server told us not to sniff the content but didn't give us |
131 // a mime type. What's a browser to do? Turns out, we're supposed to | 131 // a mime type. What's a browser to do? Turns out, we're supposed to |
132 // treat the response as "text/plain". This is the most secure option. | 132 // treat the response as "text/plain". This is the most secure option. |
(...skipping 13 matching lines...) Expand all Loading... |
146 | 146 |
147 // We'll let the original event handler provide a buffer, and reuse it for | 147 // We'll let the original event handler provide a buffer, and reuse it for |
148 // subsequent reads until we're done buffering. | 148 // subsequent reads until we're done buffering. |
149 bool BufferedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | 149 bool BufferedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
150 int* buf_size, int min_size) { | 150 int* buf_size, int min_size) { |
151 if (state_ == STATE_STREAMING) | 151 if (state_ == STATE_STREAMING) |
152 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); | 152 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); |
153 | 153 |
154 DCHECK_EQ(-1, min_size); | 154 DCHECK_EQ(-1, min_size); |
155 | 155 |
156 if (read_buffer_) { | 156 if (read_buffer_.get()) { |
157 CHECK_LT(bytes_read_, read_buffer_size_); | 157 CHECK_LT(bytes_read_, read_buffer_size_); |
158 *buf = new DependentIOBuffer(read_buffer_, bytes_read_); | 158 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); |
159 *buf_size = read_buffer_size_ - bytes_read_; | 159 *buf_size = read_buffer_size_ - bytes_read_; |
160 } else { | 160 } else { |
161 if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) | 161 if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) |
162 return false; | 162 return false; |
163 | 163 |
164 read_buffer_ = *buf; | 164 read_buffer_ = *buf; |
165 read_buffer_size_ = *buf_size; | 165 read_buffer_size_ = *buf_size; |
166 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); | 166 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
167 } | 167 } |
168 return true; | 168 return true; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 } | 222 } |
223 | 223 |
224 void BufferedResourceHandler::CancelWithError(int error_code) { | 224 void BufferedResourceHandler::CancelWithError(int error_code) { |
225 controller()->CancelWithError(error_code); | 225 controller()->CancelWithError(error_code); |
226 } | 226 } |
227 | 227 |
228 bool BufferedResourceHandler::ProcessResponse(bool* defer) { | 228 bool BufferedResourceHandler::ProcessResponse(bool* defer) { |
229 DCHECK_EQ(STATE_PROCESSING, state_); | 229 DCHECK_EQ(STATE_PROCESSING, state_); |
230 | 230 |
231 // TODO(darin): Stop special-casing 304 responses. | 231 // TODO(darin): Stop special-casing 304 responses. |
232 if (!(response_->head.headers && | 232 if (!(response_->head.headers.get() && |
233 response_->head.headers->response_code() == 304)) { | 233 response_->head.headers->response_code() == 304)) { |
234 if (!SelectNextHandler(defer)) | 234 if (!SelectNextHandler(defer)) |
235 return false; | 235 return false; |
236 if (*defer) | 236 if (*defer) |
237 return true; | 237 return true; |
238 } | 238 } |
239 | 239 |
240 state_ = STATE_REPLAYING; | 240 state_ = STATE_REPLAYING; |
241 | 241 |
242 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); | 242 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
243 if (!next_handler_->OnResponseStarted(request_id, response_, defer)) | 243 if (!next_handler_->OnResponseStarted(request_id, response_.get(), defer)) |
244 return false; | 244 return false; |
245 | 245 |
246 if (!read_buffer_) { | 246 if (!read_buffer_.get()) { |
247 state_ = STATE_STREAMING; | 247 state_ = STATE_STREAMING; |
248 return true; | 248 return true; |
249 } | 249 } |
250 | 250 |
251 if (!*defer) | 251 if (!*defer) |
252 return ReplayReadCompleted(defer); | 252 return ReplayReadCompleted(defer); |
253 | 253 |
254 return true; | 254 return true; |
255 } | 255 } |
256 | 256 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 | 314 |
315 if (!info->allow_download()) | 315 if (!info->allow_download()) |
316 return true; | 316 return true; |
317 | 317 |
318 bool must_download = MustDownload(); | 318 bool must_download = MustDownload(); |
319 if (!must_download) { | 319 if (!must_download) { |
320 if (net::IsSupportedMimeType(mime_type)) | 320 if (net::IsSupportedMimeType(mime_type)) |
321 return true; | 321 return true; |
322 | 322 |
323 scoped_ptr<ResourceHandler> handler( | 323 scoped_ptr<ResourceHandler> handler( |
324 host_->MaybeInterceptAsStream(request_, response_)); | 324 host_->MaybeInterceptAsStream(request_, response_.get())); |
325 if (handler) | 325 if (handler) |
326 return UseAlternateNextHandler(handler.Pass()); | 326 return UseAlternateNextHandler(handler.Pass()); |
327 | 327 |
328 #if defined(ENABLE_PLUGINS) | 328 #if defined(ENABLE_PLUGINS) |
329 bool stale; | 329 bool stale; |
330 bool has_plugin = HasSupportingPlugin(&stale); | 330 bool has_plugin = HasSupportingPlugin(&stale); |
331 if (stale) { | 331 if (stale) { |
332 // Refresh the plugins asynchronously. | 332 // Refresh the plugins asynchronously. |
333 PluginServiceImpl::GetInstance()->GetPlugins( | 333 PluginServiceImpl::GetInstance()->GetPlugins( |
334 base::Bind(&BufferedResourceHandler::OnPluginsLoaded, | 334 base::Bind(&BufferedResourceHandler::OnPluginsLoaded, |
(...skipping 14 matching lines...) Expand all Loading... |
349 true, // is_content_initiated | 349 true, // is_content_initiated |
350 must_download, | 350 must_download, |
351 DownloadId(), | 351 DownloadId(), |
352 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()), | 352 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()), |
353 DownloadResourceHandler::OnStartedCallback())); | 353 DownloadResourceHandler::OnStartedCallback())); |
354 return UseAlternateNextHandler(handler.Pass()); | 354 return UseAlternateNextHandler(handler.Pass()); |
355 } | 355 } |
356 | 356 |
357 bool BufferedResourceHandler::UseAlternateNextHandler( | 357 bool BufferedResourceHandler::UseAlternateNextHandler( |
358 scoped_ptr<ResourceHandler> new_handler) { | 358 scoped_ptr<ResourceHandler> new_handler) { |
359 if (response_->head.headers && // Can be NULL if FTP. | 359 if (response_->head.headers.get() && // Can be NULL if FTP. |
360 response_->head.headers->response_code() / 100 != 2) { | 360 response_->head.headers->response_code() / 100 != 2) { |
361 // The response code indicates that this is an error page, but we don't | 361 // The response code indicates that this is an error page, but we don't |
362 // know how to display the content. We follow Firefox here and show our | 362 // know how to display the content. We follow Firefox here and show our |
363 // own error page instead of triggering a download. | 363 // own error page instead of triggering a download. |
364 // TODO(abarth): We should abstract the response_code test, but this kind | 364 // TODO(abarth): We should abstract the response_code test, but this kind |
365 // of check is scattered throughout our codebase. | 365 // of check is scattered throughout our codebase. |
366 request_->CancelWithError(net::ERR_FILE_NOT_FOUND); | 366 request_->CancelWithError(net::ERR_FILE_NOT_FOUND); |
367 return false; | 367 return false; |
368 } | 368 } |
369 | 369 |
370 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); | 370 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
371 | 371 |
372 // Inform the original ResourceHandler that this will be handled entirely by | 372 // Inform the original ResourceHandler that this will be handled entirely by |
373 // the new ResourceHandler. | 373 // the new ResourceHandler. |
374 // TODO(darin): We should probably check the return values of these. | 374 // TODO(darin): We should probably check the return values of these. |
375 bool defer_ignored = false; | 375 bool defer_ignored = false; |
376 next_handler_->OnResponseStarted(request_id, response_, &defer_ignored); | 376 next_handler_->OnResponseStarted(request_id, response_.get(), &defer_ignored); |
377 DCHECK(!defer_ignored); | 377 DCHECK(!defer_ignored); |
378 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, | 378 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, |
379 net::ERR_ABORTED); | 379 net::ERR_ABORTED); |
380 next_handler_->OnResponseCompleted(request_id, status, std::string()); | 380 next_handler_->OnResponseCompleted(request_id, status, std::string()); |
381 | 381 |
382 // This is handled entirely within the new ResourceHandler, so just reset the | 382 // This is handled entirely within the new ResourceHandler, so just reset the |
383 // original ResourceHandler. | 383 // original ResourceHandler. |
384 next_handler_ = new_handler.Pass(); | 384 next_handler_ = new_handler.Pass(); |
385 next_handler_->SetController(this); | 385 next_handler_->SetController(this); |
386 | 386 |
387 return CopyReadBufferToNextHandler(request_id); | 387 return CopyReadBufferToNextHandler(request_id); |
388 } | 388 } |
389 | 389 |
390 bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) { | 390 bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) { |
391 DCHECK(read_buffer_); | 391 DCHECK(read_buffer_.get()); |
392 | 392 |
393 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); | 393 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
394 bool result = next_handler_->OnReadCompleted(request_id, bytes_read_, defer); | 394 bool result = next_handler_->OnReadCompleted(request_id, bytes_read_, defer); |
395 | 395 |
396 read_buffer_ = NULL; | 396 read_buffer_ = NULL; |
397 read_buffer_size_ = 0; | 397 read_buffer_size_ = 0; |
398 bytes_read_ = 0; | 398 bytes_read_ = 0; |
399 | 399 |
400 state_ = STATE_STREAMING; | 400 state_ = STATE_STREAMING; |
401 | 401 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 const std::vector<webkit::WebPluginInfo>& plugins) { | 463 const std::vector<webkit::WebPluginInfo>& plugins) { |
464 bool defer = false; | 464 bool defer = false; |
465 if (!ProcessResponse(&defer)) { | 465 if (!ProcessResponse(&defer)) { |
466 controller()->Cancel(); | 466 controller()->Cancel(); |
467 } else if (!defer) { | 467 } else if (!defer) { |
468 controller()->Resume(); | 468 controller()->Resume(); |
469 } | 469 } |
470 } | 470 } |
471 | 471 |
472 } // namespace content | 472 } // namespace content |
OLD | NEW |