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