[email protected] | 0a0b254 | 2012-01-03 08:25:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 5 | #include "content/browser/renderer_host/buffered_resource_handler.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 6 | |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 5203e600 | 2009-07-29 03:42:00 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 11 | #include "base/metrics/histogram.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 254ed74 | 2011-08-16 18:45:27 | [diff] [blame] | 13 | #include "content/browser/download/download_resource_handler.h" |
[email protected] | 14365e3 | 2012-01-24 01:12:22 | [diff] [blame] | 14 | #include "content/browser/download/download_stats.h" |
[email protected] | e67385f | 2011-12-21 06:00:56 | [diff] [blame] | 15 | #include "content/browser/plugin_service_impl.h" |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 16 | #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 17 | #include "content/browser/renderer_host/resource_request_info_impl.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 18 | #include "content/browser/renderer_host/x509_user_cert_resource_handler.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 20 | #include "content/public/browser/content_browser_client.h" |
[email protected] | 29a5ffc8 | 2012-03-13 19:35:58 | [diff] [blame] | 21 | #include "content/public/browser/download_save_info.h" |
[email protected] | ce96786 | 2012-02-09 22:47:05 | [diff] [blame] | 22 | #include "content/public/browser/resource_context.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 23 | #include "content/public/browser/resource_dispatcher_host_delegate.h" |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 24 | #include "content/public/common/resource_response.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 25 | #include "net/base/io_buffer.h" |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 26 | #include "net/base/mime_sniffer.h" |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 27 | #include "net/base/mime_util.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 28 | #include "net/base/net_errors.h" |
[email protected] | 28ca9fb | 2012-01-28 14:50:24 | [diff] [blame] | 29 | #include "net/http/http_content_disposition.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 30 | #include "net/http/http_response_headers.h" |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 31 | #include "webkit/plugins/webplugininfo.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 32 | |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 33 | namespace content { |
| 34 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | void RecordSnifferMetrics(bool sniffing_blocked, |
| 38 | bool we_would_like_to_sniff, |
| 39 | const std::string& mime_type) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 40 | static base::Histogram* nosniff_usage(NULL); |
| 41 | if (!nosniff_usage) |
| 42 | nosniff_usage = base::BooleanHistogram::FactoryGet( |
| 43 | "nosniff.usage", base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 44 | nosniff_usage->AddBoolean(sniffing_blocked); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 45 | |
| 46 | if (sniffing_blocked) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 47 | static base::Histogram* nosniff_otherwise(NULL); |
| 48 | if (!nosniff_otherwise) |
| 49 | nosniff_otherwise = base::BooleanHistogram::FactoryGet( |
| 50 | "nosniff.otherwise", base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 51 | nosniff_otherwise->AddBoolean(we_would_like_to_sniff); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 52 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 53 | static base::Histogram* nosniff_empty_mime_type(NULL); |
| 54 | if (!nosniff_empty_mime_type) |
| 55 | nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet( |
| 56 | "nosniff.empty_mime_type", |
| 57 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 58 | nosniff_empty_mime_type->AddBoolean(mime_type.empty()); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 64 | BufferedResourceHandler::BufferedResourceHandler( |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 65 | scoped_ptr<ResourceHandler> next_handler, |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 66 | ResourceDispatcherHostImpl* host, |
| 67 | net::URLRequest* request) |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 68 | : LayeredResourceHandler(next_handler.Pass()), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 69 | host_(host), |
| 70 | request_(request), |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 71 | read_buffer_size_(0), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 72 | bytes_read_(0), |
| 73 | sniff_content_(false), |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 74 | wait_for_plugins_(false), |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 75 | deferred_waiting_for_plugins_(false), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 76 | buffering_(false), |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 77 | next_handler_needs_response_started_(false), |
| 78 | next_handler_needs_will_read_(false), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 79 | finished_(false) { |
| 80 | } |
| 81 | |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 82 | bool BufferedResourceHandler::OnResponseStarted( |
| 83 | int request_id, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 84 | ResourceResponse* response, |
| 85 | bool* defer) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 86 | response_ = response; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 87 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 88 | if (!DelayResponse()) |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 89 | return CompleteResponseStarted(request_id, defer); |
| 90 | |
| 91 | if (wait_for_plugins_) { |
| 92 | deferred_waiting_for_plugins_ = true; |
| 93 | *defer = true; |
| 94 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 98 | // We'll let the original event handler provide a buffer, and reuse it for |
| 99 | // subsequent reads until we're done buffering. |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 100 | bool BufferedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
| 101 | int* buf_size, int min_size) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 102 | if (buffering_) { |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 103 | DCHECK(!my_buffer_.get()); |
[email protected] | 2e7aff6 | 2010-03-16 06:34:56 | [diff] [blame] | 104 | my_buffer_ = new net::IOBuffer(net::kMaxBytesToSniff); |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 105 | *buf = my_buffer_.get(); |
[email protected] | 2e7aff6 | 2010-03-16 06:34:56 | [diff] [blame] | 106 | *buf_size = net::kMaxBytesToSniff; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | if (finished_) |
| 111 | return false; |
| 112 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 113 | if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) |
[email protected] | 6719905 | 2009-06-12 21:15:33 | [diff] [blame] | 114 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 115 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 116 | read_buffer_ = *buf; |
| 117 | read_buffer_size_ = *buf_size; |
[email protected] | 2e7aff6 | 2010-03-16 06:34:56 | [diff] [blame] | 118 | DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 119 | bytes_read_ = 0; |
[email protected] | 6719905 | 2009-06-12 21:15:33 | [diff] [blame] | 120 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 123 | bool BufferedResourceHandler::OnReadCompleted(int request_id, int* bytes_read, |
| 124 | bool* defer) { |
[email protected] | 216c17f4d | 2011-05-17 06:00:55 | [diff] [blame] | 125 | if (sniff_content_) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 126 | if (KeepBuffering(*bytes_read)) { |
| 127 | if (wait_for_plugins_) |
| 128 | *defer = deferred_waiting_for_plugins_ = true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 129 | return true; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 130 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 131 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 132 | *bytes_read = bytes_read_; |
| 133 | |
| 134 | // Done buffering, send the pending ResponseStarted event. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 135 | if (!CompleteResponseStarted(request_id, defer)) |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 136 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 137 | if (*defer) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 138 | return true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 139 | } else if (wait_for_plugins_) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 140 | *defer = deferred_waiting_for_plugins_ = true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 141 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 144 | if (!ForwardPendingEventsToNextHandler(request_id, defer)) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 145 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 146 | if (*defer) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 147 | return true; |
| 148 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 149 | // Release the reference that we acquired at OnWillRead. |
| 150 | read_buffer_ = NULL; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 151 | return next_handler_->OnReadCompleted(request_id, bytes_read, defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 154 | BufferedResourceHandler::~BufferedResourceHandler() {} |
| 155 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 156 | bool BufferedResourceHandler::DelayResponse() { |
| 157 | std::string mime_type; |
| 158 | request_->GetMimeType(&mime_type); |
| 159 | |
| 160 | std::string content_type_options; |
| 161 | request_->GetResponseHeaderByName("x-content-type-options", |
| 162 | &content_type_options); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 163 | |
[email protected] | 423bd5b84 | 2009-01-23 17:30:50 | [diff] [blame] | 164 | const bool sniffing_blocked = |
| 165 | LowerCaseEqualsASCII(content_type_options, "nosniff"); |
[email protected] | e19d713 | 2009-10-16 23:33:57 | [diff] [blame] | 166 | const bool not_modified_status = |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 167 | response_->headers && response_->headers->response_code() == 304; |
[email protected] | e19d713 | 2009-10-16 23:33:57 | [diff] [blame] | 168 | const bool we_would_like_to_sniff = not_modified_status ? |
| 169 | false : net::ShouldSniffMimeType(request_->url(), mime_type); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 170 | |
| 171 | RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type); |
| 172 | |
| 173 | if (!sniffing_blocked && we_would_like_to_sniff) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 174 | // We're going to look at the data before deciding what the content type |
| 175 | // is. That means we need to delay sending the ResponseStarted message |
| 176 | // over the IPC channel. |
| 177 | sniff_content_ = true; |
[email protected] | 23866704 | 2010-10-23 01:40:00 | [diff] [blame] | 178 | VLOG(1) << "To buffer: " << request_->url().spec(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
[email protected] | e19d713 | 2009-10-16 23:33:57 | [diff] [blame] | 182 | if (sniffing_blocked && mime_type.empty() && !not_modified_status) { |
[email protected] | 423bd5b84 | 2009-01-23 17:30:50 | [diff] [blame] | 183 | // Ugg. The server told us not to sniff the content but didn't give us a |
| 184 | // mime type. What's a browser to do? Turns out, we're supposed to treat |
| 185 | // the response as "text/plain". This is the most secure option. |
| 186 | mime_type.assign("text/plain"); |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 187 | response_->mime_type.assign(mime_type); |
[email protected] | 423bd5b84 | 2009-01-23 17:30:50 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | e19d713 | 2009-10-16 23:33:57 | [diff] [blame] | 190 | if (!not_modified_status && ShouldWaitForPlugins()) { |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 191 | wait_for_plugins_ = true; |
| 192 | return true; |
| 193 | } |
| 194 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 198 | bool BufferedResourceHandler::DidBufferEnough(int bytes_read) { |
| 199 | const int kRequiredLength = 256; |
| 200 | |
| 201 | return bytes_read >= kRequiredLength; |
| 202 | } |
| 203 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 204 | bool BufferedResourceHandler::KeepBuffering(int bytes_read) { |
| 205 | DCHECK(read_buffer_); |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 206 | if (my_buffer_) { |
| 207 | // We are using our own buffer to read, update the main buffer. |
[email protected] | c0dac327 | 2010-07-28 08:04:45 | [diff] [blame] | 208 | // TODO(darin): We should handle the case where read_buffer_size_ is small! |
| 209 | // See RedirectToFileResourceHandler::BufIsFull to see how this impairs |
| 210 | // downstream ResourceHandler implementations. |
[email protected] | 2e7aff6 | 2010-03-16 06:34:56 | [diff] [blame] | 211 | CHECK_LT(bytes_read + bytes_read_, read_buffer_size_); |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 212 | memcpy(read_buffer_->data() + bytes_read_, my_buffer_->data(), bytes_read); |
| 213 | my_buffer_ = NULL; |
| 214 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 215 | bytes_read_ += bytes_read; |
| 216 | finished_ = (bytes_read == 0); |
| 217 | |
| 218 | if (sniff_content_) { |
| 219 | std::string type_hint, new_type; |
| 220 | request_->GetMimeType(&type_hint); |
| 221 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 222 | if (!net::SniffMimeType(read_buffer_->data(), bytes_read_, |
| 223 | request_->url(), type_hint, &new_type)) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 224 | // SniffMimeType() returns false if there is not enough data to determine |
| 225 | // the mime type. However, even if it returns false, it returns a new type |
| 226 | // that is probably better than the current one. |
[email protected] | 2e7aff6 | 2010-03-16 06:34:56 | [diff] [blame] | 227 | DCHECK_LT(bytes_read_, net::kMaxBytesToSniff); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 228 | if (!finished_) { |
| 229 | buffering_ = true; |
| 230 | return true; |
| 231 | } |
| 232 | } |
| 233 | sniff_content_ = false; |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 234 | response_->mime_type.assign(new_type); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 235 | |
| 236 | // We just sniffed the mime type, maybe there is a doctype to process. |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 237 | if (ShouldWaitForPlugins()) |
| 238 | wait_for_plugins_ = true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 239 | } |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 240 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 241 | buffering_ = false; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 242 | |
| 243 | if (wait_for_plugins_) |
| 244 | return true; |
| 245 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 249 | bool BufferedResourceHandler::CompleteResponseStarted(int request_id, |
| 250 | bool* defer) { |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 251 | ResourceRequestInfoImpl* info = |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 252 | ResourceRequestInfoImpl::ForRequest(request_); |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 253 | std::string mime_type; |
| 254 | request_->GetMimeType(&mime_type); |
| 255 | |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 256 | if (mime_type == "application/x-x509-user-cert") { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 257 | // Let X.509 certs be handled by the X509UserCertResourceHandler. |
| 258 | |
| 259 | // This is entirely similar to how DownloadResourceHandler works except we |
[email protected] | 60651f7d | 2012-02-06 07:46:18 | [diff] [blame] | 260 | // are doing it for an X.509 client certificates. |
| 261 | // TODO(darin): This does not belong here! |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 262 | |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 263 | if (response_->headers && // Can be NULL if FTP. |
| 264 | response_->headers->response_code() / 100 != 2) { |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 265 | // The response code indicates that this is an error page, but we are |
| 266 | // expecting an X.509 user certificate. We follow Firefox here and show |
| 267 | // our own error page instead of handling the error page as a |
| 268 | // certificate. |
| 269 | // TODO(abarth): We should abstract the response_code test, but this kind |
| 270 | // of check is scattered throughout our codebase. |
[email protected] | 4a5aebb | 2012-03-13 23:57:27 | [diff] [blame] | 271 | request_->CancelWithError(net::ERR_FILE_NOT_FOUND); |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 275 | scoped_ptr<ResourceHandler> handler( |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 276 | new X509UserCertResourceHandler(request_, |
| 277 | info->GetChildID(), |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 278 | info->GetRouteID())); |
[email protected] | 5a9d58fa | 2012-05-29 15:15:04 | [diff] [blame] | 279 | |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 280 | return UseAlternateResourceHandler(request_id, handler.Pass(), defer); |
[email protected] | 5a9d58fa | 2012-05-29 15:15:04 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | if (info->allow_download() && ShouldDownload(NULL)) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 284 | // Forward the data to the download thread. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 285 | |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 286 | if (response_->headers && // Can be NULL if FTP. |
| 287 | response_->headers->response_code() / 100 != 2) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 288 | // The response code indicates that this is an error page, but we don't |
| 289 | // know how to display the content. We follow Firefox here and show our |
| 290 | // own error page instead of triggering a download. |
| 291 | // TODO(abarth): We should abstract the response_code test, but this kind |
| 292 | // of check is scattered throughout our codebase. |
[email protected] | 4a5aebb | 2012-03-13 23:57:27 | [diff] [blame] | 293 | request_->CancelWithError(net::ERR_FILE_NOT_FOUND); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
[email protected] | 347867b7 | 2009-09-02 00:35:58 | [diff] [blame] | 297 | info->set_is_download(true); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 298 | |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 299 | scoped_ptr<ResourceHandler> handler( |
[email protected] | 60651f7d | 2012-02-06 07:46:18 | [diff] [blame] | 300 | host_->CreateResourceHandlerForDownload( |
| 301 | request_, |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 302 | info->GetContext(), |
| 303 | info->GetChildID(), |
| 304 | info->GetRouteID(), |
| 305 | info->GetRequestID(), |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 306 | true, // is_content_initiated |
[email protected] | 60651f7d | 2012-02-06 07:46:18 | [diff] [blame] | 307 | DownloadSaveInfo(), |
| 308 | DownloadResourceHandler::OnStartedCallback())); |
[email protected] | 254ed74 | 2011-08-16 18:45:27 | [diff] [blame] | 309 | |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 310 | return UseAlternateResourceHandler(request_id, handler.Pass(), defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 311 | } |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 312 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 313 | if (*defer) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 314 | return true; |
| 315 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 316 | return next_handler_->OnResponseStarted(request_id, response_, defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 319 | bool BufferedResourceHandler::ShouldWaitForPlugins() { |
| 320 | bool need_plugin_list; |
| 321 | if (!ShouldDownload(&need_plugin_list) || !need_plugin_list) |
| 322 | return false; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 323 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 324 | // Get the plugins asynchronously. |
[email protected] | e67385f | 2011-12-21 06:00:56 | [diff] [blame] | 325 | PluginServiceImpl::GetInstance()->GetPlugins( |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 326 | base::Bind(&BufferedResourceHandler::OnPluginsLoaded, AsWeakPtr())); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
| 330 | // This test mirrors the decision that WebKit makes in |
| 331 | // WebFrameLoaderClient::dispatchDecidePolicyForMIMEType. |
| 332 | bool BufferedResourceHandler::ShouldDownload(bool* need_plugin_list) { |
| 333 | if (need_plugin_list) |
| 334 | *need_plugin_list = false; |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 335 | std::string type = StringToLowerASCII(response_->mime_type); |
[email protected] | 28ca9fb | 2012-01-28 14:50:24 | [diff] [blame] | 336 | |
| 337 | // First, examine Content-Disposition. |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 338 | std::string disposition; |
| 339 | request_->GetResponseHeaderByName("content-disposition", &disposition); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 340 | if (!disposition.empty()) { |
[email protected] | 28ca9fb | 2012-01-28 14:50:24 | [diff] [blame] | 341 | net::HttpContentDisposition parsed_disposition(disposition, std::string()); |
| 342 | if (parsed_disposition.is_attachment()) |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
[email protected] | 9f3fba5 | 2011-06-08 20:37:19 | [diff] [blame] | 346 | if (host_->delegate() && |
| 347 | host_->delegate()->ShouldForceDownloadResource(request_->url(), type)) |
[email protected] | 6657afa6 | 2009-11-04 02:15:20 | [diff] [blame] | 348 | return true; |
| 349 | |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 350 | // MIME type checking. |
| 351 | if (net::IsSupportedMimeType(type)) |
| 352 | return false; |
| 353 | |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 354 | // Finally, check the plugin list. |
| 355 | bool allow_wildcard = false; |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 356 | ResourceRequestInfoImpl* info = |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 357 | ResourceRequestInfoImpl::ForRequest(request_); |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 358 | bool stale = false; |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 359 | webkit::WebPluginInfo plugin; |
[email protected] | e67385f | 2011-12-21 06:00:56 | [diff] [blame] | 360 | bool found = PluginServiceImpl::GetInstance()->GetPluginInfo( |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 361 | info->GetChildID(), info->GetRouteID(), info->GetContext(), |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 362 | request_->url(), GURL(), type, allow_wildcard, |
| 363 | &stale, &plugin, NULL); |
| 364 | |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 365 | if (need_plugin_list) { |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 366 | if (stale) { |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 367 | *need_plugin_list = true; |
| 368 | return true; |
| 369 | } |
| 370 | } else { |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 371 | DCHECK(!stale); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | dfba876 | 2011-09-02 12:49:54 | [diff] [blame] | 374 | return !found; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 375 | } |
| 376 | |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 377 | bool BufferedResourceHandler::UseAlternateResourceHandler( |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 378 | int request_id, |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 379 | scoped_ptr<ResourceHandler> handler, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 380 | bool* defer) { |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 381 | // Inform the original ResourceHandler that this will be handled entirely by |
| 382 | // the new ResourceHandler. |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 383 | // TODO(darin): We should probably check the return values of these. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 384 | bool defer_ignored = false; |
| 385 | next_handler_->OnResponseStarted(request_id, response_, &defer_ignored); |
| 386 | DCHECK(!defer_ignored); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 387 | net::URLRequestStatus status(net::URLRequestStatus::HANDLED_EXTERNALLY, 0); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 388 | next_handler_->OnResponseCompleted(request_id, status, std::string()); |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 389 | |
[email protected] | 11149bd | 2010-06-10 16:31:04 | [diff] [blame] | 390 | // Remove the non-owning pointer to the CrossSiteResourceHandler, if any, |
| 391 | // from the extra request info because the CrossSiteResourceHandler (part of |
| 392 | // the original ResourceHandler chain) will be deleted by the next statement. |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 393 | ResourceRequestInfoImpl* info = |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 394 | ResourceRequestInfoImpl::ForRequest(request_); |
[email protected] | 11149bd | 2010-06-10 16:31:04 | [diff] [blame] | 395 | info->set_cross_site_handler(NULL); |
| 396 | |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 397 | // This is handled entirely within the new ResourceHandler, so just reset the |
| 398 | // original ResourceHandler. |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 399 | next_handler_ = handler.Pass(); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 400 | |
| 401 | next_handler_needs_response_started_ = true; |
| 402 | next_handler_needs_will_read_ = true; |
| 403 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 404 | return ForwardPendingEventsToNextHandler(request_id, defer); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 407 | bool BufferedResourceHandler::ForwardPendingEventsToNextHandler(int request_id, |
| 408 | bool* defer) { |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 409 | if (next_handler_needs_response_started_) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 410 | if (!next_handler_->OnResponseStarted(request_id, response_, defer)) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 411 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 412 | // If the request was deferred during OnResponseStarted, we need to avoid |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 413 | // calling OnResponseStarted again. |
| 414 | next_handler_needs_response_started_ = false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 415 | if (*defer) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 416 | return true; |
| 417 | } |
| 418 | |
| 419 | if (next_handler_needs_will_read_) { |
| 420 | CopyReadBufferToNextHandler(request_id); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 421 | next_handler_needs_will_read_ = false; |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | void BufferedResourceHandler::CopyReadBufferToNextHandler(int request_id) { |
| 427 | if (!bytes_read_) |
| 428 | return; |
| 429 | |
| 430 | net::IOBuffer* buf = NULL; |
| 431 | int buf_len = 0; |
| 432 | if (next_handler_->OnWillRead(request_id, &buf, &buf_len, bytes_read_)) { |
| 433 | CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
| 434 | memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
| 435 | } |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 438 | void BufferedResourceHandler::OnPluginsLoaded( |
| 439 | const std::vector<webkit::WebPluginInfo>& plugins) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 440 | bool needs_resume = deferred_waiting_for_plugins_; |
| 441 | deferred_waiting_for_plugins_ = false; |
| 442 | |
[email protected] | 5658a60 | 2009-11-03 23:12:52 | [diff] [blame] | 443 | wait_for_plugins_ = false; |
[email protected] | 7f2e792e | 2009-11-30 23:18:29 | [diff] [blame] | 444 | if (!request_) |
| 445 | return; |
[email protected] | 5658a60 | 2009-11-03 23:12:52 | [diff] [blame] | 446 | |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 447 | ResourceRequestInfoImpl* info = |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 448 | ResourceRequestInfoImpl::ForRequest(request_); |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 449 | int child_id = info->GetChildID(); |
| 450 | int request_id = info->GetRequestID(); |
| 451 | |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 452 | bool defer = false; |
| 453 | if (!CompleteResponseStarted(request_id, &defer)) { |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 454 | host_->CancelRequest(child_id, request_id, false); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 455 | } else if (!defer && needs_resume) { |
| 456 | host_->ResumeDeferredRequest(child_id, request_id); |
| 457 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 458 | } |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 459 | |
| 460 | } // namespace content |