[email protected] | 0a0b254 | 2012-01-03 08:25:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 5 | #include "content/browser/renderer_host/buffered_resource_handler.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 6 | |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 5203e600 | 2009-07-29 03:42:00 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 11 | #include "base/metrics/histogram.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 254ed74 | 2011-08-16 18:45:27 | [diff] [blame] | 13 | #include "content/browser/download/download_resource_handler.h" |
[email protected] | 14365e3 | 2012-01-24 01:12:22 | [diff] [blame] | 14 | #include "content/browser/download/download_stats.h" |
[email protected] | e67385f | 2011-12-21 06:00:56 | [diff] [blame] | 15 | #include "content/browser/plugin_service_impl.h" |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 16 | #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 17 | #include "content/browser/renderer_host/resource_request_info_impl.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 18 | #include "content/browser/renderer_host/x509_user_cert_resource_handler.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 20 | #include "content/public/browser/content_browser_client.h" |
[email protected] | 29a5ffc8 | 2012-03-13 19:35:58 | [diff] [blame] | 21 | #include "content/public/browser/download_save_info.h" |
[email protected] | ce96786 | 2012-02-09 22:47:05 | [diff] [blame] | 22 | #include "content/public/browser/resource_context.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 23 | #include "content/public/browser/resource_dispatcher_host_delegate.h" |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 24 | #include "content/public/common/resource_response.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 25 | #include "net/base/io_buffer.h" |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 26 | #include "net/base/mime_sniffer.h" |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 27 | #include "net/base/mime_util.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 28 | #include "net/base/net_errors.h" |
[email protected] | 28ca9fb | 2012-01-28 14:50:24 | [diff] [blame] | 29 | #include "net/http/http_content_disposition.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 30 | #include "net/http/http_response_headers.h" |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 31 | #include "webkit/plugins/webplugininfo.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 32 | |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 33 | namespace content { |
| 34 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | void RecordSnifferMetrics(bool sniffing_blocked, |
| 38 | bool we_would_like_to_sniff, |
| 39 | const std::string& mime_type) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 40 | static base::Histogram* nosniff_usage(NULL); |
| 41 | if (!nosniff_usage) |
| 42 | nosniff_usage = base::BooleanHistogram::FactoryGet( |
| 43 | "nosniff.usage", base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 44 | nosniff_usage->AddBoolean(sniffing_blocked); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 45 | |
| 46 | if (sniffing_blocked) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 47 | static base::Histogram* nosniff_otherwise(NULL); |
| 48 | if (!nosniff_otherwise) |
| 49 | nosniff_otherwise = base::BooleanHistogram::FactoryGet( |
| 50 | "nosniff.otherwise", base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 51 | nosniff_otherwise->AddBoolean(we_would_like_to_sniff); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 52 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 53 | static base::Histogram* nosniff_empty_mime_type(NULL); |
| 54 | if (!nosniff_empty_mime_type) |
| 55 | nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet( |
| 56 | "nosniff.empty_mime_type", |
| 57 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 58 | nosniff_empty_mime_type->AddBoolean(mime_type.empty()); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
[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: |
| 71 | ~DependentIOBuffer() {} |
| 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), |
| 89 | must_download_is_set_(false) { |
| 90 | } |
| 91 | |
| 92 | BufferedResourceHandler::~BufferedResourceHandler() { |
| 93 | } |
| 94 | |
| 95 | void BufferedResourceHandler::SetController(ResourceController* controller) { |
| 96 | ResourceHandler::SetController(controller); |
| 97 | |
| 98 | // Downstream handlers see us as their ResourceController, which allows us to |
| 99 | // consume part or all of the resource response, and then later replay it to |
| 100 | // downstream handler. |
| 101 | DCHECK(next_handler_.get()); |
| 102 | next_handler_->SetController(this); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 103 | } |
| 104 | |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 105 | bool BufferedResourceHandler::OnResponseStarted( |
| 106 | int request_id, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 107 | ResourceResponse* response, |
| 108 | bool* defer) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 109 | response_ = response; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 110 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 111 | // TODO(darin): It is very odd to special-case 304 responses at this level. |
| 112 | // We do so only because the code always has, see r24977 and r29355. The |
| 113 | // fact that 204 is no longer special-cased this way suggests that 304 need |
| 114 | // not be special-cased either. |
| 115 | // |
| 116 | // The network stack only forwards 304 responses that were not received in |
| 117 | // response to a conditional request (i.e., If-Modified-Since). Other 304 |
| 118 | // responses end up being translated to 200 or whatever the cached response |
| 119 | // 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] | 120 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 121 | if (!(response_->head.headers && |
| 122 | response_->head.headers->response_code() == 304)) { |
| 123 | if (ShouldSniffContent()) { |
| 124 | state_ = STATE_BUFFERING; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | if (response_->head.mime_type.empty()) { |
| 129 | // Ugg. The server told us not to sniff the content but didn't give us |
| 130 | // a mime type. What's a browser to do? Turns out, we're supposed to |
| 131 | // treat the response as "text/plain". This is the most secure option. |
| 132 | response_->head.mime_type.assign("text/plain"); |
| 133 | } |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 134 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 135 | |
| 136 | state_ = STATE_PROCESSING; |
| 137 | return ProcessResponse(defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 140 | // We'll let the original event handler provide a buffer, and reuse it for |
| 141 | // subsequent reads until we're done buffering. |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 142 | bool BufferedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
| 143 | int* buf_size, int min_size) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 144 | if (state_ == STATE_STREAMING) |
| 145 | return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); |
| 146 | |
| 147 | DCHECK_EQ(-1, min_size); |
| 148 | |
| 149 | if (read_buffer_) { |
| 150 | CHECK_LT(bytes_read_, read_buffer_size_); |
| 151 | *buf = new DependentIOBuffer(read_buffer_, bytes_read_); |
| 152 | *buf_size = read_buffer_size_ - bytes_read_; |
| 153 | } else { |
| 154 | if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) |
| 155 | return false; |
| 156 | |
| 157 | read_buffer_ = *buf; |
| 158 | read_buffer_size_ = *buf_size; |
| 159 | DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 160 | } |
[email protected] | 6719905 | 2009-06-12 21:15:33 | [diff] [blame] | 161 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 164 | bool BufferedResourceHandler::OnReadCompleted(int request_id, int bytes_read, |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 165 | bool* defer) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 166 | if (state_ == STATE_STREAMING) |
| 167 | return next_handler_->OnReadCompleted(request_id, bytes_read, defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 168 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 169 | DCHECK_EQ(state_, STATE_BUFFERING); |
| 170 | bytes_read_ += bytes_read; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 171 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 172 | if (!DetermineMimeType() && (bytes_read > 0)) |
| 173 | return true; // Needs more data, so keep buffering. |
| 174 | |
| 175 | state_ = STATE_PROCESSING; |
| 176 | return ProcessResponse(defer); |
| 177 | } |
| 178 | |
| 179 | bool BufferedResourceHandler::OnResponseCompleted( |
| 180 | int request_id, |
| 181 | const net::URLRequestStatus& status, |
| 182 | const std::string& security_info) { |
| 183 | // Upon completion, act like a pass-through handler in case the downstream |
| 184 | // handler defers OnResponseCompleted. |
| 185 | state_ = STATE_STREAMING; |
| 186 | |
| 187 | return next_handler_->OnResponseCompleted(request_id, status, security_info); |
| 188 | } |
| 189 | |
| 190 | void BufferedResourceHandler::Resume() { |
| 191 | switch (state_) { |
| 192 | case STATE_BUFFERING: |
| 193 | case STATE_PROCESSING: |
| 194 | NOTREACHED(); |
| 195 | break; |
| 196 | case STATE_REPLAYING: |
| 197 | MessageLoop::current()->PostTask( |
| 198 | FROM_HERE, |
| 199 | base::Bind(&BufferedResourceHandler::CallReplayReadCompleted, |
| 200 | AsWeakPtr())); |
| 201 | break; |
| 202 | case STATE_STARTING: |
| 203 | case STATE_STREAMING: |
| 204 | controller()->Resume(); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void BufferedResourceHandler::Cancel() { |
| 210 | controller()->Cancel(); |
| 211 | } |
| 212 | |
| 213 | bool BufferedResourceHandler::ProcessResponse(bool* defer) { |
| 214 | DCHECK_EQ(STATE_PROCESSING, state_); |
| 215 | |
| 216 | // TODO(darin): Stop special-casing 304 responses. |
| 217 | if (!(response_->head.headers && |
| 218 | response_->head.headers->response_code() == 304)) { |
| 219 | if (!SelectNextHandler(defer)) |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 220 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 221 | if (*defer) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 222 | return true; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 223 | } |
| 224 | |
| 225 | state_ = STATE_REPLAYING; |
| 226 | |
| 227 | int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
| 228 | if (!next_handler_->OnResponseStarted(request_id, response_, defer)) |
| 229 | return false; |
| 230 | |
| 231 | if (!read_buffer_) { |
| 232 | state_ = STATE_STREAMING; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 233 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 236 | if (!*defer) |
| 237 | return ReplayReadCompleted(defer); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 238 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 239 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 242 | bool BufferedResourceHandler::ShouldSniffContent() { |
| 243 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 244 | |
| 245 | std::string content_type_options; |
| 246 | request_->GetResponseHeaderByName("x-content-type-options", |
| 247 | &content_type_options); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 248 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 249 | bool sniffing_blocked = |
[email protected] | 423bd5b84 | 2009-01-23 17:30:50 | [diff] [blame] | 250 | LowerCaseEqualsASCII(content_type_options, "nosniff"); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 251 | bool we_would_like_to_sniff = |
| 252 | net::ShouldSniffMimeType(request_->url(), mime_type); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 253 | |
| 254 | RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type); |
| 255 | |
| 256 | if (!sniffing_blocked && we_would_like_to_sniff) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 257 | // We're going to look at the data before deciding what the content type |
| 258 | // is. That means we need to delay sending the ResponseStarted message |
| 259 | // over the IPC channel. |
[email protected] | 23866704 | 2010-10-23 01:40:00 | [diff] [blame] | 260 | VLOG(1) << "To buffer: " << request_->url().spec(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 267 | bool BufferedResourceHandler::DetermineMimeType() { |
| 268 | DCHECK_EQ(STATE_BUFFERING, state_); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 269 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 270 | const std::string& type_hint = response_->head.mime_type; |
| 271 | |
| 272 | std::string new_type; |
| 273 | bool made_final_decision = |
| 274 | net::SniffMimeType(read_buffer_->data(), bytes_read_, request_->url(), |
| 275 | type_hint, &new_type); |
| 276 | |
| 277 | // SniffMimeType() returns false if there is not enough data to determine |
| 278 | // the mime type. However, even if it returns false, it returns a new type |
| 279 | // that is probably better than the current one. |
| 280 | response_->head.mime_type.assign(new_type); |
| 281 | |
| 282 | return made_final_decision; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 285 | bool BufferedResourceHandler::SelectNextHandler(bool* defer) { |
| 286 | DCHECK(!response_->head.mime_type.empty()); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 287 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 288 | ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); |
| 289 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 290 | |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 291 | if (mime_type == "application/x-x509-user-cert") { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 292 | // Install X509 handler. |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 293 | scoped_ptr<ResourceHandler> handler( |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 294 | new X509UserCertResourceHandler(request_, |
| 295 | info->GetChildID(), |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 296 | info->GetRouteID())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 297 | return UseAlternateNextHandler(handler.Pass()); |
[email protected] | 5a9d58fa | 2012-05-29 15:15:04 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 300 | if (!info->allow_download()) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 301 | return true; |
| 302 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 303 | if (!MustDownload()) { |
| 304 | if (net::IsSupportedMimeType(mime_type)) |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 305 | return true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 306 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 307 | bool stale; |
| 308 | bool has_plugin = HasSupportingPlugin(&stale); |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 309 | if (stale) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 310 | // Refresh the plugins asynchronously. |
| 311 | PluginServiceImpl::GetInstance()->GetPlugins( |
| 312 | base::Bind(&BufferedResourceHandler::OnPluginsLoaded, AsWeakPtr())); |
| 313 | *defer = true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 314 | return true; |
| 315 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 316 | if (has_plugin) |
| 317 | return true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 320 | // Install download handler |
| 321 | info->set_is_download(true); |
| 322 | scoped_ptr<ResourceHandler> handler( |
| 323 | host_->CreateResourceHandlerForDownload( |
| 324 | request_, |
| 325 | true, // is_content_initiated |
| 326 | DownloadSaveInfo(), |
| 327 | DownloadResourceHandler::OnStartedCallback())); |
| 328 | return UseAlternateNextHandler(handler.Pass()); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 331 | bool BufferedResourceHandler::UseAlternateNextHandler( |
| 332 | scoped_ptr<ResourceHandler> new_handler) { |
| 333 | if (response_->head.headers && // Can be NULL if FTP. |
| 334 | response_->head.headers->response_code() / 100 != 2) { |
| 335 | // The response code indicates that this is an error page, but we don't |
| 336 | // know how to display the content. We follow Firefox here and show our |
| 337 | // own error page instead of triggering a download. |
| 338 | // TODO(abarth): We should abstract the response_code test, but this kind |
| 339 | // of check is scattered throughout our codebase. |
| 340 | request_->CancelWithError(net::ERR_FILE_NOT_FOUND); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
| 345 | |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 346 | // Inform the original ResourceHandler that this will be handled entirely by |
| 347 | // the new ResourceHandler. |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 348 | // TODO(darin): We should probably check the return values of these. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 349 | bool defer_ignored = false; |
| 350 | next_handler_->OnResponseStarted(request_id, response_, &defer_ignored); |
| 351 | DCHECK(!defer_ignored); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 352 | net::URLRequestStatus status(net::URLRequestStatus::HANDLED_EXTERNALLY, 0); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 353 | next_handler_->OnResponseCompleted(request_id, status, std::string()); |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 354 | |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 355 | // This is handled entirely within the new ResourceHandler, so just reset the |
| 356 | // original ResourceHandler. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 357 | next_handler_ = new_handler.Pass(); |
| 358 | next_handler_->SetController(this); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 359 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 360 | return CopyReadBufferToNextHandler(request_id); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 363 | bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) { |
| 364 | DCHECK(read_buffer_); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 365 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 366 | int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID(); |
| 367 | bool result = next_handler_->OnReadCompleted(request_id, bytes_read_, defer); |
| 368 | |
| 369 | read_buffer_ = NULL; |
| 370 | read_buffer_size_ = 0; |
| 371 | bytes_read_ = 0; |
| 372 | |
| 373 | state_ = STATE_STREAMING; |
| 374 | |
| 375 | return result; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 378 | void BufferedResourceHandler::CallReplayReadCompleted() { |
| 379 | bool defer = false; |
| 380 | if (!ReplayReadCompleted(&defer)) { |
| 381 | controller()->Cancel(); |
| 382 | } else if (!defer) { |
| 383 | state_ = STATE_STREAMING; |
| 384 | controller()->Resume(); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | bool BufferedResourceHandler::MustDownload() { |
| 389 | if (must_download_is_set_) |
| 390 | return must_download_; |
| 391 | |
| 392 | must_download_is_set_ = true; |
| 393 | |
| 394 | std::string disposition; |
| 395 | request_->GetResponseHeaderByName("content-disposition", &disposition); |
| 396 | if (!disposition.empty() && |
| 397 | net::HttpContentDisposition(disposition, std::string()).is_attachment()) { |
| 398 | must_download_ = true; |
| 399 | } else if (host_->delegate() && |
| 400 | host_->delegate()->ShouldForceDownloadResource( |
| 401 | request_->url(), response_->head.mime_type)) { |
| 402 | must_download_ = true; |
| 403 | } else { |
| 404 | must_download_ = false; |
| 405 | } |
| 406 | |
| 407 | return must_download_; |
| 408 | } |
| 409 | |
| 410 | bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) { |
| 411 | ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_); |
| 412 | |
| 413 | bool allow_wildcard = false; |
| 414 | webkit::WebPluginInfo plugin; |
| 415 | return PluginServiceImpl::GetInstance()->GetPluginInfo( |
| 416 | info->GetChildID(), info->GetRouteID(), info->GetContext(), |
| 417 | request_->url(), GURL(), response_->head.mime_type, allow_wildcard, |
| 418 | stale, &plugin, NULL); |
| 419 | } |
| 420 | |
| 421 | bool BufferedResourceHandler::CopyReadBufferToNextHandler(int request_id) { |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 422 | if (!bytes_read_) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 423 | return true; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 424 | |
| 425 | net::IOBuffer* buf = NULL; |
| 426 | int buf_len = 0; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 427 | if (!next_handler_->OnWillRead(request_id, &buf, &buf_len, bytes_read_)) |
| 428 | return false; |
| 429 | |
| 430 | CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
| 431 | memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
| 432 | return true; |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 433 | } |
| 434 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 435 | void BufferedResourceHandler::OnPluginsLoaded( |
| 436 | const std::vector<webkit::WebPluginInfo>& plugins) { |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 437 | bool defer = false; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 438 | if (!ProcessResponse(&defer)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 439 | controller()->Cancel(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame^] | 440 | } else if (!defer) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 441 | controller()->Resume(); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 442 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 443 | } |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 444 | |
| 445 | } // namespace content |