| 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 "net/base/mime_sniffer.h" | 8 #include "net/base/mime_sniffer.h" |
| 8 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" | 9 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" |
| 9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 10 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 10 | 11 |
| 12 namespace { |
| 13 |
| 14 void RecordSnifferMetrics(bool sniffing_blocked, |
| 15 bool we_would_like_to_sniff, |
| 16 const std::string& mime_type) { |
| 17 static BooleanHistogram nosniff_usage(L"nosniff.usage"); |
| 18 nosniff_usage.SetFlags(kUmaTargetedHistogramFlag); |
| 19 nosniff_usage.AddBoolean(sniffing_blocked); |
| 20 |
| 21 if (sniffing_blocked) { |
| 22 static BooleanHistogram nosniff_otherwise(L"nosniff.otherwise"); |
| 23 nosniff_otherwise.SetFlags(kUmaTargetedHistogramFlag); |
| 24 nosniff_otherwise.AddBoolean(we_would_like_to_sniff); |
| 25 |
| 26 static BooleanHistogram nosniff_empty_mime_type(L"nosniff.empty_mime_type"); |
| 27 nosniff_empty_mime_type.SetFlags(kUmaTargetedHistogramFlag); |
| 28 nosniff_empty_mime_type.AddBoolean(mime_type.empty()); |
| 29 } |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
| 11 BufferedResourceHandler::BufferedResourceHandler(ResourceHandler* handler, | 34 BufferedResourceHandler::BufferedResourceHandler(ResourceHandler* handler, |
| 12 ResourceDispatcherHost* host, | 35 ResourceDispatcherHost* host, |
| 13 URLRequest* request) | 36 URLRequest* request) |
| 14 : real_handler_(handler), | 37 : real_handler_(handler), |
| 15 host_(host), | 38 host_(host), |
| 16 request_(request), | 39 request_(request), |
| 17 bytes_read_(0), | 40 bytes_read_(0), |
| 18 sniff_content_(false), | 41 sniff_content_(false), |
| 19 should_buffer_(false), | 42 should_buffer_(false), |
| 20 buffering_(false), | 43 buffering_(false), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 return real_handler_->OnReadCompleted(request_id, bytes_read); | 108 return real_handler_->OnReadCompleted(request_id, bytes_read); |
| 86 } | 109 } |
| 87 | 110 |
| 88 bool BufferedResourceHandler::DelayResponse() { | 111 bool BufferedResourceHandler::DelayResponse() { |
| 89 std::string mime_type; | 112 std::string mime_type; |
| 90 request_->GetMimeType(&mime_type); | 113 request_->GetMimeType(&mime_type); |
| 91 | 114 |
| 92 std::string content_type_options; | 115 std::string content_type_options; |
| 93 request_->GetResponseHeaderByName("x-content-type-options", | 116 request_->GetResponseHeaderByName("x-content-type-options", |
| 94 &content_type_options); | 117 &content_type_options); |
| 95 if (content_type_options != "nosniff" && | 118 |
| 96 net::ShouldSniffMimeType(request_->url(), mime_type)) { | 119 const bool sniffing_blocked = (content_type_options == "nosniff"); |
| 120 const bool we_would_like_to_sniff = |
| 121 net::ShouldSniffMimeType(request_->url(), mime_type); |
| 122 |
| 123 RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type); |
| 124 |
| 125 if (!sniffing_blocked && we_would_like_to_sniff) { |
| 97 // We're going to look at the data before deciding what the content type | 126 // We're going to look at the data before deciding what the content type |
| 98 // is. That means we need to delay sending the ResponseStarted message | 127 // is. That means we need to delay sending the ResponseStarted message |
| 99 // over the IPC channel. | 128 // over the IPC channel. |
| 100 sniff_content_ = true; | 129 sniff_content_ = true; |
| 101 LOG(INFO) << "To buffer: " << request_->url().spec(); | 130 LOG(INFO) << "To buffer: " << request_->url().spec(); |
| 102 return true; | 131 return true; |
| 103 } | 132 } |
| 104 | 133 |
| 105 if (ShouldBuffer(request_->url(), mime_type)) { | 134 if (ShouldBuffer(request_->url(), mime_type)) { |
| 106 // This is a temporary fix for the fact that webkit expects to have | 135 // This is a temporary fix for the fact that webkit expects to have |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 real_handler_ = download_handler; | 248 real_handler_ = download_handler; |
| 220 } | 249 } |
| 221 return real_handler_->OnResponseStarted(request_id, response_); | 250 return real_handler_->OnResponseStarted(request_id, response_); |
| 222 } | 251 } |
| 223 | 252 |
| 224 bool BufferedResourceHandler::DidBufferEnough(int bytes_read) { | 253 bool BufferedResourceHandler::DidBufferEnough(int bytes_read) { |
| 225 const int kRequiredLength = 256; | 254 const int kRequiredLength = 256; |
| 226 | 255 |
| 227 return bytes_read >= kRequiredLength; | 256 return bytes_read >= kRequiredLength; |
| 228 } | 257 } |
| OLD | NEW |