| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/renderer_host/buffered_resource_handler.h" | 5 #include "chrome/browser/renderer_host/buffered_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
| 8 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/task.h" |
| 9 #include "net/base/mime_sniffer.h" | 11 #include "net/base/mime_sniffer.h" |
| 10 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" | 12 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" |
| 11 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 13 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/tab_contents/tab_util.h" |
| 12 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 13 #include "net/base/mime_sniffer.h" | 17 #include "net/base/mime_sniffer.h" |
| 14 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 15 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 16 | 20 |
| 17 namespace { | 21 namespace { |
| 18 | 22 |
| 19 const int kMaxBytesToSniff = 512; | 23 const int kMaxBytesToSniff = 512; |
| 20 | 24 |
| 21 void RecordSnifferMetrics(bool sniffing_blocked, | 25 void RecordSnifferMetrics(bool sniffing_blocked, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 33 static BooleanHistogram nosniff_empty_mime_type("nosniff.empty_mime_type"); | 37 static BooleanHistogram nosniff_empty_mime_type("nosniff.empty_mime_type"); |
| 34 nosniff_empty_mime_type.SetFlags(kUmaTargetedHistogramFlag); | 38 nosniff_empty_mime_type.SetFlags(kUmaTargetedHistogramFlag); |
| 35 nosniff_empty_mime_type.AddBoolean(mime_type.empty()); | 39 nosniff_empty_mime_type.AddBoolean(mime_type.empty()); |
| 36 } | 40 } |
| 37 } | 41 } |
| 38 | 42 |
| 39 } // namespace | 43 } // namespace |
| 40 | 44 |
| 41 BufferedResourceHandler::BufferedResourceHandler(ResourceHandler* handler, | 45 BufferedResourceHandler::BufferedResourceHandler(ResourceHandler* handler, |
| 42 ResourceDispatcherHost* host, | 46 ResourceDispatcherHost* host, |
| 43 URLRequest* request) | 47 URLRequest* request, |
| 48 int render_process_host_id, |
| 49 int render_view_id) |
| 44 : real_handler_(handler), | 50 : real_handler_(handler), |
| 45 host_(host), | 51 host_(host), |
| 46 request_(request), | 52 request_(request), |
| 53 render_process_host_id_(render_process_host_id), |
| 54 render_view_id_(render_view_id), |
| 47 bytes_read_(0), | 55 bytes_read_(0), |
| 48 sniff_content_(false), | 56 sniff_content_(false), |
| 49 should_buffer_(false), | 57 should_buffer_(false), |
| 50 buffering_(false), | 58 buffering_(false), |
| 51 finished_(false) { | 59 finished_(false) { |
| 52 } | 60 } |
| 53 | 61 |
| 54 bool BufferedResourceHandler::OnUploadProgress(int request_id, | 62 bool BufferedResourceHandler::OnUploadProgress(int request_id, |
| 55 uint64 position, | 63 uint64 position, |
| 56 uint64 size) { | 64 uint64 size) { |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 // know how to display the content. We follow Firefox here and show our | 251 // know how to display the content. We follow Firefox here and show our |
| 244 // own error page instead of triggering a download. | 252 // own error page instead of triggering a download. |
| 245 // TODO(abarth): We should abstract the response_code test, but this kind | 253 // TODO(abarth): We should abstract the response_code test, but this kind |
| 246 // of check is scattered throughout our codebase. | 254 // of check is scattered throughout our codebase. |
| 247 request_->SimulateError(net::ERR_FILE_NOT_FOUND); | 255 request_->SimulateError(net::ERR_FILE_NOT_FOUND); |
| 248 return false; | 256 return false; |
| 249 } | 257 } |
| 250 | 258 |
| 251 info->is_download = true; | 259 info->is_download = true; |
| 252 | 260 |
| 261 host_->ui_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 262 this, &BufferedResourceHandler::NotifyDownload)); |
| 263 |
| 253 scoped_refptr<DownloadThrottlingResourceHandler> download_handler = | 264 scoped_refptr<DownloadThrottlingResourceHandler> download_handler = |
| 254 new DownloadThrottlingResourceHandler(host_, | 265 new DownloadThrottlingResourceHandler(host_, |
| 255 request_, | 266 request_, |
| 256 request_->url(), | 267 request_->url(), |
| 257 info->process_id, | 268 info->process_id, |
| 258 info->route_id, | 269 info->route_id, |
| 259 request_id, | 270 request_id, |
| 260 in_complete); | 271 in_complete); |
| 261 if (bytes_read_) { | 272 if (bytes_read_) { |
| 262 // a Read has already occurred and we need to copy the data into the | 273 // a Read has already occurred and we need to copy the data into the |
| (...skipping 12 matching lines...) Expand all Loading... |
| 275 real_handler_->OnResponseCompleted(info->request_id, status, | 286 real_handler_->OnResponseCompleted(info->request_id, status, |
| 276 std::string()); | 287 std::string()); |
| 277 | 288 |
| 278 // Ditch the old async handler that talks to the renderer for the new | 289 // Ditch the old async handler that talks to the renderer for the new |
| 279 // download handler that talks to the DownloadManager. | 290 // download handler that talks to the DownloadManager. |
| 280 real_handler_ = download_handler; | 291 real_handler_ = download_handler; |
| 281 } | 292 } |
| 282 return real_handler_->OnResponseStarted(request_id, response_); | 293 return real_handler_->OnResponseStarted(request_id, response_); |
| 283 } | 294 } |
| 284 | 295 |
| 296 void BufferedResourceHandler::NotifyDownload() { |
| 297 TabContents* tab_contents = |
| 298 tab_util::GetTabContentsByID(render_process_host_id_, render_view_id_); |
| 299 tab_contents->MarkAsDownload(); |
| 300 } |
| 301 |
| 285 bool BufferedResourceHandler::DidBufferEnough(int bytes_read) { | 302 bool BufferedResourceHandler::DidBufferEnough(int bytes_read) { |
| 286 const int kRequiredLength = 256; | 303 const int kRequiredLength = 256; |
| 287 | 304 |
| 288 return bytes_read >= kRequiredLength; | 305 return bytes_read >= kRequiredLength; |
| 289 } | 306 } |
| OLD | NEW |