[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] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 5 | #include "content/browser/loader/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] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 12 | #include "base/strings/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] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 15 | #include "content/browser/loader/certificate_resource_handler.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 16 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 17 | #include "content/browser/loader/resource_request_info_impl.h" |
[email protected] | e67385f | 2011-12-21 06:00:56 | [diff] [blame] | 18 | #include "content/browser/plugin_service_impl.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 19 | #include "content/public/browser/content_browser_client.h" |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 20 | #include "content/public/browser/download_item.h" |
[email protected] | 29a5ffc8 | 2012-03-13 19:35:58 | [diff] [blame] | 21 | #include "content/public/browser/download_save_info.h" |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 22 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ce96786 | 2012-02-09 22:47:05 | [diff] [blame] | 23 | #include "content/public/browser/resource_context.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 24 | #include "content/public/browser/resource_dispatcher_host_delegate.h" |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 25 | #include "content/public/common/resource_response.h" |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 26 | #include "content/public/common/webplugininfo.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 27 | #include "net/base/io_buffer.h" |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 28 | #include "net/base/mime_sniffer.h" |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 29 | #include "net/base/mime_util.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 30 | #include "net/base/net_errors.h" |
[email protected] | 28ca9fb | 2012-01-28 14:50:24 | [diff] [blame] | 31 | #include "net/http/http_content_disposition.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 33 | |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 34 | namespace content { |
| 35 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | void RecordSnifferMetrics(bool sniffing_blocked, |
| 39 | bool we_would_like_to_sniff, |
| 40 | const std::string& mime_type) { |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 41 | static base::HistogramBase* nosniff_usage(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 42 | if (!nosniff_usage) |
| 43 | nosniff_usage = base::BooleanHistogram::FactoryGet( |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 44 | "nosniff.usage", base::HistogramBase::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 45 | nosniff_usage->AddBoolean(sniffing_blocked); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 46 | |
| 47 | if (sniffing_blocked) { |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 48 | static base::HistogramBase* nosniff_otherwise(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 49 | if (!nosniff_otherwise) |
| 50 | nosniff_otherwise = base::BooleanHistogram::FactoryGet( |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 51 | "nosniff.otherwise", base::HistogramBase::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 52 | nosniff_otherwise->AddBoolean(we_would_like_to_sniff); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 53 | |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 54 | static base::HistogramBase* nosniff_empty_mime_type(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 55 | if (!nosniff_empty_mime_type) |
| 56 | nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet( |
| 57 | "nosniff.empty_mime_type", |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 58 | base::HistogramBase::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 59 | nosniff_empty_mime_type->AddBoolean(mime_type.empty()); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 63 | // Used to write into an existing IOBuffer at a given offset. |
| 64 | class DependentIOBuffer : public net::WrappedIOBuffer { |
| 65 | public: |
| 66 | DependentIOBuffer(net::IOBuffer* buf, int offset) |
| 67 | : net::WrappedIOBuffer(buf->data() + offset), |
| 68 | buf_(buf) { |
| 69 | } |
| 70 | |
| 71 | private: |
[email protected] | c3e3589 | 2013-02-12 02:08:01 | [diff] [blame] | 72 | virtual ~DependentIOBuffer() {} |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 73 | |
| 74 | scoped_refptr<net::IOBuffer> buf_; |
| 75 | }; |
| 76 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 77 | } // namespace |
| 78 | |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 79 | BufferedResourceHandler::BufferedResourceHandler( |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 80 | scoped_ptr<ResourceHandler> next_handler, |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 81 | ResourceDispatcherHostImpl* host, |
| 82 | net::URLRequest* request) |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 83 | : LayeredResourceHandler(request, next_handler.Pass()), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 84 | state_(STATE_STARTING), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 85 | host_(host), |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 86 | read_buffer_size_(0), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 87 | bytes_read_(0), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 88 | must_download_(false), |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 89 | must_download_is_set_(false), |
| 90 | weak_ptr_factory_(this) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | BufferedResourceHandler::~BufferedResourceHandler() { |
| 94 | } |
| 95 | |
| 96 | void BufferedResourceHandler::SetController(ResourceController* controller) { |
| 97 | ResourceHandler::SetController(controller); |
| 98 | |
| 99 | // Downstream handlers see us as their ResourceController, which allows us to |
| 100 | // consume part or all of the resource response, and then later replay it to |
| 101 | // downstream handler. |
| 102 | DCHECK(next_handler_.get()); |
| 103 | next_handler_->SetController(this); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 106 | bool BufferedResourceHandler::OnResponseStarted( |
| 107 | int request_id, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 108 | ResourceResponse* response, |
| 109 | bool* defer) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 110 | response_ = response; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 111 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 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 |
| 114 | // fact that 204 is no longer special-cased this way suggests that 304 need |
| 115 | // not be special-cased either. |
| 116 | // |
| 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 |
| 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. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 121 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 122 | if (!(response_->head.headers.get() && |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 123 | response_->head.headers->response_code() == 304)) { |
| 124 | if (ShouldSniffContent()) { |
| 125 | state_ = STATE_BUFFERING; |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | if (response_->head.mime_type.empty()) { |
| 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 |
| 132 | // treat the response as "text/plain". This is the most secure option. |
| 133 | response_->head.mime_type.assign("text/plain"); |
| 134 | } |
[email protected] | 3ff67926 | 2012-10-12 23:40:05 | [diff] [blame] | 135 | |
| 136 | // Treat feed types as text/plain. |
| 137 | if (response_->head.mime_type == "application/rss+xml" || |
| 138 | response_->head.mime_type == "application/atom+xml") { |
| 139 | response_->head.mime_type.assign("text/plain"); |
| 140 | } |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 141 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 142 | |
| 143 | state_ = STATE_PROCESSING; |
| 144 | return ProcessResponse(defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 147 | // We'll let the original event handler provide a buffer, and reuse it for |
| 148 | // subsequent reads until we're done buffering. |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 149 | bool BufferedResourceHandler::OnWillRead(int request_id, |
| 150 | scoped_refptr<net::IOBuffer>* buf, |
| 151 | int* buf_size, |
| 152 | int min_size) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 153 | if (state_ == STATE_STREAMING) |
| 154 | return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); |
| 155 | |
| 156 | DCHECK_EQ(-1, min_size); |
| 157 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 158 | if (read_buffer_.get()) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 159 | CHECK_LT(bytes_read_, read_buffer_size_); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 160 | *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 161 | *buf_size = read_buffer_size_ - bytes_read_; |
| 162 | } else { |
| 163 | if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) |
| 164 | return false; |
| 165 | |
| 166 | read_buffer_ = *buf; |
| 167 | read_buffer_size_ = *buf_size; |
| 168 | DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 169 | } |
[email protected] | 6719905 | 2009-06-12 21:15:33 | [diff] [blame] | 170 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 173 | bool BufferedResourceHandler::OnReadCompleted(int request_id, int bytes_read, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 174 | bool* defer) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 175 | if (state_ == STATE_STREAMING) |
| 176 | return next_handler_->OnReadCompleted(request_id, bytes_read, defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 177 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 178 | DCHECK_EQ(state_, STATE_BUFFERING); |
| 179 | bytes_read_ += bytes_read; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 180 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 181 | if (!DetermineMimeType() && (bytes_read > 0)) |
| 182 | return true; // Needs more data, so keep buffering. |
| 183 | |
| 184 | state_ = STATE_PROCESSING; |
| 185 | return ProcessResponse(defer); |
| 186 | } |
| 187 | |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 188 | void BufferedResourceHandler::OnResponseCompleted( |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 189 | int request_id, |
| 190 | const net::URLRequestStatus& status, |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 191 | const std::string& security_info, |
| 192 | bool* defer) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 193 | // Upon completion, act like a pass-through handler in case the downstream |
| 194 | // handler defers OnResponseCompleted. |
| 195 | state_ = STATE_STREAMING; |
| 196 | |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 197 | next_handler_->OnResponseCompleted(request_id, status, security_info, defer); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void BufferedResourceHandler::Resume() { |
| 201 | switch (state_) { |
| 202 | case STATE_BUFFERING: |
| 203 | case STATE_PROCESSING: |
| 204 | NOTREACHED(); |
| 205 | break; |
| 206 | case STATE_REPLAYING: |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 207 | base::MessageLoop::current()->PostTask( |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 208 | FROM_HERE, |
| 209 | base::Bind(&BufferedResourceHandler::CallReplayReadCompleted, |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 210 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 211 | break; |
| 212 | case STATE_STARTING: |
| 213 | case STATE_STREAMING: |
| 214 | controller()->Resume(); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void BufferedResourceHandler::Cancel() { |
| 220 | controller()->Cancel(); |
| 221 | } |
| 222 | |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 223 | void BufferedResourceHandler::CancelAndIgnore() { |
| 224 | controller()->CancelAndIgnore(); |
| 225 | } |
| 226 | |
[email protected] | af3d496 | 2012-10-19 10:57:26 | [diff] [blame] | 227 | void BufferedResourceHandler::CancelWithError(int error_code) { |
| 228 | controller()->CancelWithError(error_code); |
| 229 | } |
| 230 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 231 | bool BufferedResourceHandler::ProcessResponse(bool* defer) { |
| 232 | DCHECK_EQ(STATE_PROCESSING, state_); |
| 233 | |
| 234 | // TODO(darin): Stop special-casing 304 responses. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 235 | if (!(response_->head.headers.get() && |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 236 | response_->head.headers->response_code() == 304)) { |
| 237 | if (!SelectNextHandler(defer)) |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 238 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 239 | if (*defer) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 240 | return true; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | state_ = STATE_REPLAYING; |
| 244 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 245 | if (!next_handler_->OnResponseStarted(GetRequestID(), response_.get(), defer)) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 246 | return false; |
| 247 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 248 | if (!read_buffer_.get()) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 249 | state_ = STATE_STREAMING; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 250 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 253 | if (!*defer) |
| 254 | return ReplayReadCompleted(defer); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 255 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 256 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 259 | bool BufferedResourceHandler::ShouldSniffContent() { |
| 260 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 261 | |
| 262 | std::string content_type_options; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 263 | request()->GetResponseHeaderByName("x-content-type-options", |
| 264 | &content_type_options); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 265 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 266 | bool sniffing_blocked = |
[email protected] | 423bd5b84 | 2009-01-23 17:30:50 | [diff] [blame] | 267 | LowerCaseEqualsASCII(content_type_options, "nosniff"); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 268 | bool we_would_like_to_sniff = |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 269 | net::ShouldSniffMimeType(request()->url(), mime_type); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 270 | |
| 271 | RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type); |
| 272 | |
| 273 | if (!sniffing_blocked && we_would_like_to_sniff) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 274 | // We're going to look at the data before deciding what the content type |
| 275 | // is. That means we need to delay sending the ResponseStarted message |
| 276 | // over the IPC channel. |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 277 | VLOG(1) << "To buffer: " << request()->url().spec(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 284 | bool BufferedResourceHandler::DetermineMimeType() { |
| 285 | DCHECK_EQ(STATE_BUFFERING, state_); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 286 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 287 | const std::string& type_hint = response_->head.mime_type; |
| 288 | |
| 289 | std::string new_type; |
| 290 | bool made_final_decision = |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 291 | net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 292 | type_hint, &new_type); |
| 293 | |
| 294 | // SniffMimeType() returns false if there is not enough data to determine |
| 295 | // the mime type. However, even if it returns false, it returns a new type |
| 296 | // that is probably better than the current one. |
| 297 | response_->head.mime_type.assign(new_type); |
| 298 | |
| 299 | return made_final_decision; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 300 | } |
| 301 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 302 | bool BufferedResourceHandler::SelectNextHandler(bool* defer) { |
| 303 | DCHECK(!response_->head.mime_type.empty()); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 304 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 305 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 306 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 307 | |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 308 | if (net::IsSupportedCertificateMimeType(mime_type)) { |
| 309 | // Install certificate file. |
[email protected] | 6f3ac60 | 2014-02-13 21:38:05 | [diff] [blame] | 310 | info->set_is_download(true); |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 311 | scoped_ptr<ResourceHandler> handler( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 312 | new CertificateResourceHandler(request())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 313 | return UseAlternateNextHandler(handler.Pass()); |
[email protected] | 5a9d58fa | 2012-05-29 15:15:04 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 316 | if (!info->allow_download()) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 317 | return true; |
| 318 | |
[email protected] | 3ca004f7 | 2012-12-18 10:35:21 | [diff] [blame] | 319 | bool must_download = MustDownload(); |
| 320 | if (!must_download) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 321 | if (net::IsSupportedMimeType(mime_type)) |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 322 | return true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 323 | |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 324 | scoped_ptr<ResourceHandler> handler( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 325 | host_->MaybeInterceptAsStream(request(), response_.get())); |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 326 | if (handler) |
| 327 | return UseAlternateNextHandler(handler.Pass()); |
| 328 | |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 329 | #if defined(ENABLE_PLUGINS) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 330 | bool stale; |
| 331 | bool has_plugin = HasSupportingPlugin(&stale); |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 332 | if (stale) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 333 | // Refresh the plugins asynchronously. |
| 334 | PluginServiceImpl::GetInstance()->GetPlugins( |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 335 | base::Bind(&BufferedResourceHandler::OnPluginsLoaded, |
| 336 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | ae9c092 | 2014-02-13 22:40:23 | [diff] [blame^] | 337 | request()->LogBlockedBy("BufferedResourceHandler"); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 338 | *defer = true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 339 | return true; |
| 340 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 341 | if (has_plugin) |
| 342 | return true; |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 343 | #endif |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 346 | // Install download handler |
| 347 | info->set_is_download(true); |
| 348 | scoped_ptr<ResourceHandler> handler( |
| 349 | host_->CreateResourceHandlerForDownload( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 350 | request(), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 351 | true, // is_content_initiated |
[email protected] | 3ca004f7 | 2012-12-18 10:35:21 | [diff] [blame] | 352 | must_download, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 353 | content::DownloadItem::kInvalidId, |
[email protected] | c873c86 | 2012-10-17 15:32:12 | [diff] [blame] | 354 | scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()), |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 355 | DownloadUrlParameters::OnStartedCallback())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 356 | return UseAlternateNextHandler(handler.Pass()); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 359 | bool BufferedResourceHandler::UseAlternateNextHandler( |
| 360 | scoped_ptr<ResourceHandler> new_handler) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 361 | if (response_->head.headers.get() && // Can be NULL if FTP. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 362 | response_->head.headers->response_code() / 100 != 2) { |
| 363 | // The response code indicates that this is an error page, but we don't |
| 364 | // know how to display the content. We follow Firefox here and show our |
| 365 | // own error page instead of triggering a download. |
| 366 | // TODO(abarth): We should abstract the response_code test, but this kind |
| 367 | // of check is scattered throughout our codebase. |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 368 | request()->CancelWithError(net::ERR_FILE_NOT_FOUND); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 372 | int request_id = GetRequestID(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 373 | |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 374 | // Inform the original ResourceHandler that this will be handled entirely by |
| 375 | // the new ResourceHandler. |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 376 | // TODO(darin): We should probably check the return values of these. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 377 | bool defer_ignored = false; |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 378 | next_handler_->OnResponseStarted(request_id, response_.get(), &defer_ignored); |
[email protected] | 6f3ac60 | 2014-02-13 21:38:05 | [diff] [blame] | 379 | // Although deferring OnResponseStarted is legal, the only downstream handler |
| 380 | // which does so is CrossSiteResourceHandler. Cross-site transitions should |
| 381 | // not trigger when switching handlers. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 382 | DCHECK(!defer_ignored); |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 383 | net::URLRequestStatus status(net::URLRequestStatus::CANCELED, |
| 384 | net::ERR_ABORTED); |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 385 | next_handler_->OnResponseCompleted(request_id, status, std::string(), |
| 386 | &defer_ignored); |
| 387 | DCHECK(!defer_ignored); |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 388 | |
| 389 | // This is handled entirely within the new ResourceHandler, so just reset the |
| 390 | // original ResourceHandler. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 391 | next_handler_ = new_handler.Pass(); |
| 392 | next_handler_->SetController(this); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 393 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 394 | return CopyReadBufferToNextHandler(request_id); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 397 | bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 398 | DCHECK(read_buffer_.get()); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 399 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 400 | bool result = next_handler_->OnReadCompleted(GetRequestID(), bytes_read_, |
| 401 | defer); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 402 | |
| 403 | read_buffer_ = NULL; |
| 404 | read_buffer_size_ = 0; |
| 405 | bytes_read_ = 0; |
| 406 | |
| 407 | state_ = STATE_STREAMING; |
| 408 | |
| 409 | return result; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 410 | } |
| 411 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 412 | void BufferedResourceHandler::CallReplayReadCompleted() { |
| 413 | bool defer = false; |
| 414 | if (!ReplayReadCompleted(&defer)) { |
| 415 | controller()->Cancel(); |
| 416 | } else if (!defer) { |
| 417 | state_ = STATE_STREAMING; |
| 418 | controller()->Resume(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | bool BufferedResourceHandler::MustDownload() { |
| 423 | if (must_download_is_set_) |
| 424 | return must_download_; |
| 425 | |
| 426 | must_download_is_set_ = true; |
| 427 | |
| 428 | std::string disposition; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 429 | request()->GetResponseHeaderByName("content-disposition", &disposition); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 430 | if (!disposition.empty() && |
| 431 | net::HttpContentDisposition(disposition, std::string()).is_attachment()) { |
| 432 | must_download_ = true; |
| 433 | } else if (host_->delegate() && |
| 434 | host_->delegate()->ShouldForceDownloadResource( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 435 | request()->url(), response_->head.mime_type)) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 436 | must_download_ = true; |
| 437 | } else { |
| 438 | must_download_ = false; |
| 439 | } |
| 440 | |
| 441 | return must_download_; |
| 442 | } |
| 443 | |
| 444 | bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) { |
[email protected] | 012a171 | 2013-11-18 05:29:41 | [diff] [blame] | 445 | #if defined(ENABLE_PLUGINS) |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 446 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 447 | |
| 448 | bool allow_wildcard = false; |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 449 | WebPluginInfo plugin; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 450 | return PluginServiceImpl::GetInstance()->GetPluginInfo( |
[email protected] | 60eca4eb | 2013-12-06 00:02:16 | [diff] [blame] | 451 | info->GetChildID(), info->GetRenderFrameID(), info->GetContext(), |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 452 | request()->url(), GURL(), response_->head.mime_type, allow_wildcard, |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 453 | stale, &plugin, NULL); |
[email protected] | 012a171 | 2013-11-18 05:29:41 | [diff] [blame] | 454 | #else |
| 455 | if (stale) |
| 456 | *stale = false; |
| 457 | return false; |
| 458 | #endif |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | bool BufferedResourceHandler::CopyReadBufferToNextHandler(int request_id) { |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 462 | if (!bytes_read_) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 463 | return true; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 464 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 465 | scoped_refptr<net::IOBuffer> buf; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 466 | int buf_len = 0; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 467 | if (!next_handler_->OnWillRead(request_id, &buf, &buf_len, bytes_read_)) |
| 468 | return false; |
| 469 | |
| 470 | CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
| 471 | memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
| 472 | return true; |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 475 | void BufferedResourceHandler::OnPluginsLoaded( |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 476 | const std::vector<WebPluginInfo>& plugins) { |
[email protected] | ae9c092 | 2014-02-13 22:40:23 | [diff] [blame^] | 477 | request()->LogUnblocked(); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 478 | bool defer = false; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 479 | if (!ProcessResponse(&defer)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 480 | controller()->Cancel(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 481 | } else if (!defer) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 482 | controller()->Resume(); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 483 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 484 | } |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 485 | |
| 486 | } // namespace content |