[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] | 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] | 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] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 20 | #include "content/public/browser/download_id.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] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 40 | static base::HistogramBase* nosniff_usage(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 41 | if (!nosniff_usage) |
| 42 | nosniff_usage = base::BooleanHistogram::FactoryGet( |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 43 | "nosniff.usage", base::HistogramBase::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] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 47 | static base::HistogramBase* nosniff_otherwise(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 48 | if (!nosniff_otherwise) |
| 49 | nosniff_otherwise = base::BooleanHistogram::FactoryGet( |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 50 | "nosniff.otherwise", base::HistogramBase::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] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 53 | static base::HistogramBase* nosniff_empty_mime_type(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 54 | if (!nosniff_empty_mime_type) |
| 55 | nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet( |
| 56 | "nosniff.empty_mime_type", |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 57 | base::HistogramBase::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 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 62 | // Used to write into an existing IOBuffer at a given offset. |
| 63 | class DependentIOBuffer : public net::WrappedIOBuffer { |
| 64 | public: |
| 65 | DependentIOBuffer(net::IOBuffer* buf, int offset) |
| 66 | : net::WrappedIOBuffer(buf->data() + offset), |
| 67 | buf_(buf) { |
| 68 | } |
| 69 | |
| 70 | private: |
[email protected] | c3e3589 | 2013-02-12 02:08:01 | [diff] [blame] | 71 | virtual ~DependentIOBuffer() {} |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 72 | |
| 73 | scoped_refptr<net::IOBuffer> buf_; |
| 74 | }; |
| 75 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 76 | } // namespace |
| 77 | |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 78 | BufferedResourceHandler::BufferedResourceHandler( |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 79 | scoped_ptr<ResourceHandler> next_handler, |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 80 | ResourceDispatcherHostImpl* host, |
| 81 | net::URLRequest* request) |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 82 | : LayeredResourceHandler(next_handler.Pass()), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 83 | state_(STATE_STARTING), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 84 | host_(host), |
| 85 | request_(request), |
[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] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 149 | bool BufferedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
| 150 | int* buf_size, int min_size) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 151 | if (state_ == STATE_STREAMING) |
| 152 | return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); |
| 153 | |
| 154 | DCHECK_EQ(-1, min_size); |
| 155 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 156 | if (read_buffer_.get()) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 157 | CHECK_LT(bytes_read_, read_buffer_size_); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 158 | *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 159 | *buf_size = read_buffer_size_ - bytes_read_; |
| 160 | } else { |
| 161 | if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) |
| 162 | return false; |
| 163 | |
| 164 | read_buffer_ = *buf; |
| 165 | read_buffer_size_ = *buf_size; |
| 166 | DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 167 | } |
[email protected] | 6719905 | 2009-06-12 21:15:33 | [diff] [blame] | 168 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 171 | bool BufferedResourceHandler::OnReadCompleted(int request_id, int bytes_read, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 172 | bool* defer) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 173 | if (state_ == STATE_STREAMING) |
| 174 | return next_handler_->OnReadCompleted(request_id, bytes_read, defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 175 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 176 | DCHECK_EQ(state_, STATE_BUFFERING); |
| 177 | bytes_read_ += bytes_read; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 178 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 179 | if (!DetermineMimeType() && (bytes_read > 0)) |
| 180 | return true; // Needs more data, so keep buffering. |
| 181 | |
| 182 | state_ = STATE_PROCESSING; |
| 183 | return ProcessResponse(defer); |
| 184 | } |
| 185 | |
| 186 | bool BufferedResourceHandler::OnResponseCompleted( |
| 187 | int request_id, |
| 188 | const net::URLRequestStatus& status, |
| 189 | const std::string& security_info) { |
| 190 | // Upon completion, act like a pass-through handler in case the downstream |
| 191 | // handler defers OnResponseCompleted. |
| 192 | state_ = STATE_STREAMING; |
| 193 | |
| 194 | return next_handler_->OnResponseCompleted(request_id, status, security_info); |
| 195 | } |
| 196 | |
| 197 | void BufferedResourceHandler::Resume() { |
| 198 | switch (state_) { |
| 199 | case STATE_BUFFERING: |
| 200 | case STATE_PROCESSING: |
| 201 | NOTREACHED(); |
| 202 | break; |
| 203 | case STATE_REPLAYING: |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 204 | base::MessageLoop::current()->PostTask( |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 205 | FROM_HERE, |
| 206 | base::Bind(&BufferedResourceHandler::CallReplayReadCompleted, |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 207 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 208 | break; |
| 209 | case STATE_STARTING: |
| 210 | case STATE_STREAMING: |
| 211 | controller()->Resume(); |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void BufferedResourceHandler::Cancel() { |
| 217 | controller()->Cancel(); |
| 218 | } |
| 219 | |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 220 | void BufferedResourceHandler::CancelAndIgnore() { |
| 221 | controller()->CancelAndIgnore(); |
| 222 | } |
| 223 | |
[email protected] | af3d496 | 2012-10-19 10:57:26 | [diff] [blame] | 224 | void BufferedResourceHandler::CancelWithError(int error_code) { |
| 225 | controller()->CancelWithError(error_code); |
| 226 | } |
| 227 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 228 | bool BufferedResourceHandler::ProcessResponse(bool* defer) { |
| 229 | DCHECK_EQ(STATE_PROCESSING, state_); |
| 230 | |
| 231 | // TODO(darin): Stop special-casing 304 responses. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 232 | if (!(response_->head.headers.get() && |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 233 | response_->head.headers->response_code() == 304)) { |
| 234 | if (!SelectNextHandler(defer)) |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 235 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 236 | if (*defer) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 237 | return true; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | state_ = STATE_REPLAYING; |
| 241 | |
| 242 | int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 243 | if (!next_handler_->OnResponseStarted(request_id, response_.get(), defer)) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 244 | return false; |
| 245 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 246 | if (!read_buffer_.get()) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 247 | state_ = STATE_STREAMING; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 248 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 251 | if (!*defer) |
| 252 | return ReplayReadCompleted(defer); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 253 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 254 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 257 | bool BufferedResourceHandler::ShouldSniffContent() { |
| 258 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 259 | |
| 260 | std::string content_type_options; |
| 261 | request_->GetResponseHeaderByName("x-content-type-options", |
| 262 | &content_type_options); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 263 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 264 | bool sniffing_blocked = |
[email protected] | 423bd5b84 | 2009-01-23 17:30:50 | [diff] [blame] | 265 | LowerCaseEqualsASCII(content_type_options, "nosniff"); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 266 | bool we_would_like_to_sniff = |
| 267 | net::ShouldSniffMimeType(request_->url(), mime_type); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 268 | |
| 269 | RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type); |
| 270 | |
| 271 | if (!sniffing_blocked && we_would_like_to_sniff) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 272 | // We're going to look at the data before deciding what the content type |
| 273 | // is. That means we need to delay sending the ResponseStarted message |
| 274 | // over the IPC channel. |
[email protected] | 23866704 | 2010-10-23 01:40:00 | [diff] [blame] | 275 | VLOG(1) << "To buffer: " << request_->url().spec(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 282 | bool BufferedResourceHandler::DetermineMimeType() { |
| 283 | DCHECK_EQ(STATE_BUFFERING, state_); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 284 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 285 | const std::string& type_hint = response_->head.mime_type; |
| 286 | |
| 287 | std::string new_type; |
| 288 | bool made_final_decision = |
| 289 | net::SniffMimeType(read_buffer_->data(), bytes_read_, request_->url(), |
| 290 | type_hint, &new_type); |
| 291 | |
| 292 | // SniffMimeType() returns false if there is not enough data to determine |
| 293 | // the mime type. However, even if it returns false, it returns a new type |
| 294 | // that is probably better than the current one. |
| 295 | response_->head.mime_type.assign(new_type); |
| 296 | |
| 297 | return made_final_decision; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 300 | bool BufferedResourceHandler::SelectNextHandler(bool* defer) { |
| 301 | DCHECK(!response_->head.mime_type.empty()); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 302 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 303 | ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); |
| 304 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 305 | |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 306 | if (net::IsSupportedCertificateMimeType(mime_type)) { |
| 307 | // Install certificate file. |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 308 | scoped_ptr<ResourceHandler> handler( |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 309 | new CertificateResourceHandler(request_, |
| 310 | info->GetChildID(), |
| 311 | info->GetRouteID())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 312 | return UseAlternateNextHandler(handler.Pass()); |
[email protected] | 5a9d58fa | 2012-05-29 15:15:04 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 315 | if (!info->allow_download()) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 316 | return true; |
| 317 | |
[email protected] | 3ca004f7 | 2012-12-18 10:35:21 | [diff] [blame] | 318 | bool must_download = MustDownload(); |
| 319 | if (!must_download) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 320 | if (net::IsSupportedMimeType(mime_type)) |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 321 | return true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 322 | |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 323 | scoped_ptr<ResourceHandler> handler( |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 324 | host_->MaybeInterceptAsStream(request_, response_.get())); |
[email protected] | 64688982 | 2013-03-20 00:26:45 | [diff] [blame] | 325 | if (handler) |
| 326 | return UseAlternateNextHandler(handler.Pass()); |
| 327 | |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 328 | #if defined(ENABLE_PLUGINS) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 329 | bool stale; |
| 330 | bool has_plugin = HasSupportingPlugin(&stale); |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 331 | if (stale) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 332 | // Refresh the plugins asynchronously. |
| 333 | PluginServiceImpl::GetInstance()->GetPlugins( |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 334 | base::Bind(&BufferedResourceHandler::OnPluginsLoaded, |
| 335 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 336 | *defer = true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 337 | return true; |
| 338 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 339 | if (has_plugin) |
| 340 | return true; |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 341 | #endif |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 344 | // Install download handler |
| 345 | info->set_is_download(true); |
| 346 | scoped_ptr<ResourceHandler> handler( |
| 347 | host_->CreateResourceHandlerForDownload( |
| 348 | request_, |
| 349 | true, // is_content_initiated |
[email protected] | 3ca004f7 | 2012-12-18 10:35:21 | [diff] [blame] | 350 | must_download, |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 351 | DownloadId(), |
[email protected] | c873c86 | 2012-10-17 15:32:12 | [diff] [blame] | 352 | scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 353 | DownloadResourceHandler::OnStartedCallback())); |
| 354 | return UseAlternateNextHandler(handler.Pass()); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 355 | } |
| 356 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 357 | bool BufferedResourceHandler::UseAlternateNextHandler( |
| 358 | scoped_ptr<ResourceHandler> new_handler) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 359 | if (response_->head.headers.get() && // Can be NULL if FTP. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 360 | response_->head.headers->response_code() / 100 != 2) { |
| 361 | // The response code indicates that this is an error page, but we don't |
| 362 | // know how to display the content. We follow Firefox here and show our |
| 363 | // own error page instead of triggering a download. |
| 364 | // TODO(abarth): We should abstract the response_code test, but this kind |
| 365 | // of check is scattered throughout our codebase. |
| 366 | request_->CancelWithError(net::ERR_FILE_NOT_FOUND); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
| 371 | |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 372 | // Inform the original ResourceHandler that this will be handled entirely by |
| 373 | // the new ResourceHandler. |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 374 | // TODO(darin): We should probably check the return values of these. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 375 | bool defer_ignored = false; |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 376 | next_handler_->OnResponseStarted(request_id, response_.get(), &defer_ignored); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 377 | DCHECK(!defer_ignored); |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 378 | net::URLRequestStatus status(net::URLRequestStatus::CANCELED, |
| 379 | net::ERR_ABORTED); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 380 | next_handler_->OnResponseCompleted(request_id, status, std::string()); |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 381 | |
| 382 | // This is handled entirely within the new ResourceHandler, so just reset the |
| 383 | // original ResourceHandler. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 384 | next_handler_ = new_handler.Pass(); |
| 385 | next_handler_->SetController(this); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 386 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 387 | return CopyReadBufferToNextHandler(request_id); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 390 | bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame^] | 391 | DCHECK(read_buffer_.get()); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 392 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 393 | int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
| 394 | bool result = next_handler_->OnReadCompleted(request_id, bytes_read_, defer); |
| 395 | |
| 396 | read_buffer_ = NULL; |
| 397 | read_buffer_size_ = 0; |
| 398 | bytes_read_ = 0; |
| 399 | |
| 400 | state_ = STATE_STREAMING; |
| 401 | |
| 402 | return result; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 403 | } |
| 404 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 405 | void BufferedResourceHandler::CallReplayReadCompleted() { |
| 406 | bool defer = false; |
| 407 | if (!ReplayReadCompleted(&defer)) { |
| 408 | controller()->Cancel(); |
| 409 | } else if (!defer) { |
| 410 | state_ = STATE_STREAMING; |
| 411 | controller()->Resume(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | bool BufferedResourceHandler::MustDownload() { |
| 416 | if (must_download_is_set_) |
| 417 | return must_download_; |
| 418 | |
| 419 | must_download_is_set_ = true; |
| 420 | |
| 421 | std::string disposition; |
| 422 | request_->GetResponseHeaderByName("content-disposition", &disposition); |
| 423 | if (!disposition.empty() && |
| 424 | net::HttpContentDisposition(disposition, std::string()).is_attachment()) { |
| 425 | must_download_ = true; |
| 426 | } else if (host_->delegate() && |
| 427 | host_->delegate()->ShouldForceDownloadResource( |
| 428 | request_->url(), response_->head.mime_type)) { |
| 429 | must_download_ = true; |
| 430 | } else { |
| 431 | must_download_ = false; |
| 432 | } |
| 433 | |
| 434 | return must_download_; |
| 435 | } |
| 436 | |
| 437 | bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) { |
| 438 | ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); |
| 439 | |
| 440 | bool allow_wildcard = false; |
| 441 | webkit::WebPluginInfo plugin; |
| 442 | return PluginServiceImpl::GetInstance()->GetPluginInfo( |
| 443 | info->GetChildID(), info->GetRouteID(), info->GetContext(), |
| 444 | request_->url(), GURL(), response_->head.mime_type, allow_wildcard, |
| 445 | stale, &plugin, NULL); |
| 446 | } |
| 447 | |
| 448 | bool BufferedResourceHandler::CopyReadBufferToNextHandler(int request_id) { |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 449 | if (!bytes_read_) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 450 | return true; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 451 | |
| 452 | net::IOBuffer* buf = NULL; |
| 453 | int buf_len = 0; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 454 | if (!next_handler_->OnWillRead(request_id, &buf, &buf_len, bytes_read_)) |
| 455 | return false; |
| 456 | |
| 457 | CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
| 458 | memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
| 459 | return true; |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 462 | void BufferedResourceHandler::OnPluginsLoaded( |
| 463 | const std::vector<webkit::WebPluginInfo>& plugins) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 464 | bool defer = false; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 465 | if (!ProcessResponse(&defer)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 466 | controller()->Cancel(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 467 | } else if (!defer) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 468 | controller()->Resume(); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 469 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 470 | } |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 471 | |
| 472 | } // namespace content |