blob: 8003154c1ec1b932df36c87a53dfb302f70fceb2 [file] [log] [blame]
[email protected]0a0b2542012-01-03 08:25:221// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e3c404b2008-12-23 01:07:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]678c0362012-12-05 08:02:445#include "content/browser/loader/buffered_resource_handler.h"
[email protected]e3c404b2008-12-23 01:07:326
[email protected]dd9b5462009-09-25 17:22:237#include <vector>
8
[email protected]d33e7cc2011-09-23 01:43:569#include "base/bind.h"
[email protected]5203e6002009-07-29 03:42:0010#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3811#include "base/metrics/histogram.h"
[email protected]348fbaac2013-06-11 06:31:5112#include "base/strings/string_util.h"
[email protected]254ed742011-08-16 18:45:2713#include "content/browser/download/download_resource_handler.h"
[email protected]14365e32012-01-24 01:12:2214#include "content/browser/download/download_stats.h"
[email protected]3b455502012-12-11 18:22:5815#include "content/browser/loader/certificate_resource_handler.h"
[email protected]678c0362012-12-05 08:02:4416#include "content/browser/loader/resource_dispatcher_host_impl.h"
17#include "content/browser/loader/resource_request_info_impl.h"
[email protected]e67385f2011-12-21 06:00:5618#include "content/browser/plugin_service_impl.h"
[email protected]87f3c082011-10-19 18:07:4419#include "content/public/browser/content_browser_client.h"
[email protected]530047e2013-07-12 17:02:2520#include "content/public/browser/download_item.h"
[email protected]29a5ffc82012-03-13 19:35:5821#include "content/public/browser/download_save_info.h"
[email protected]530047e2013-07-12 17:02:2522#include "content/public/browser/download_url_parameters.h"
[email protected]ce967862012-02-09 22:47:0523#include "content/public/browser/resource_context.h"
[email protected]1bd0ef12011-10-20 05:24:1724#include "content/public/browser/resource_dispatcher_host_delegate.h"
[email protected]2336ffe2011-11-24 01:23:3425#include "content/public/common/resource_response.h"
[email protected]d7bd3e52013-07-21 04:29:2026#include "content/public/common/webplugininfo.h"
[email protected]dd9b5462009-09-25 17:22:2327#include "net/base/io_buffer.h"
[email protected]9dea9e1f2009-01-29 00:30:4728#include "net/base/mime_sniffer.h"
[email protected]35fa6a22009-08-15 00:04:0129#include "net/base/mime_util.h"
[email protected]dd9b5462009-09-25 17:22:2330#include "net/base/net_errors.h"
[email protected]28ca9fb2012-01-28 14:50:2431#include "net/http/http_content_disposition.h"
[email protected]319d9e6f2009-02-18 19:47:2132#include "net/http/http_response_headers.h"
[email protected]e3c404b2008-12-23 01:07:3233
[email protected]d18720a2012-01-06 09:53:5534namespace content {
35
[email protected]306291392009-01-17 19:15:3636namespace {
37
38void RecordSnifferMetrics(bool sniffing_blocked,
39 bool we_would_like_to_sniff,
40 const std::string& mime_type) {
[email protected]de415552013-01-23 04:12:1741 static base::HistogramBase* nosniff_usage(NULL);
[email protected]81ce9f3b2011-04-05 04:48:5342 if (!nosniff_usage)
43 nosniff_usage = base::BooleanHistogram::FactoryGet(
[email protected]de415552013-01-23 04:12:1744 "nosniff.usage", base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:3745 nosniff_usage->AddBoolean(sniffing_blocked);
[email protected]306291392009-01-17 19:15:3646
47 if (sniffing_blocked) {
[email protected]de415552013-01-23 04:12:1748 static base::HistogramBase* nosniff_otherwise(NULL);
[email protected]81ce9f3b2011-04-05 04:48:5349 if (!nosniff_otherwise)
50 nosniff_otherwise = base::BooleanHistogram::FactoryGet(
[email protected]de415552013-01-23 04:12:1751 "nosniff.otherwise", base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:3752 nosniff_otherwise->AddBoolean(we_would_like_to_sniff);
[email protected]306291392009-01-17 19:15:3653
[email protected]de415552013-01-23 04:12:1754 static base::HistogramBase* nosniff_empty_mime_type(NULL);
[email protected]81ce9f3b2011-04-05 04:48:5355 if (!nosniff_empty_mime_type)
56 nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet(
57 "nosniff.empty_mime_type",
[email protected]de415552013-01-23 04:12:1758 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:3759 nosniff_empty_mime_type->AddBoolean(mime_type.empty());
[email protected]306291392009-01-17 19:15:3660 }
61}
62
[email protected]926360f2012-06-29 04:45:0263// Used to write into an existing IOBuffer at a given offset.
64class 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]c3e35892013-02-12 02:08:0172 virtual ~DependentIOBuffer() {}
[email protected]926360f2012-06-29 04:45:0273
74 scoped_refptr<net::IOBuffer> buf_;
75};
76
[email protected]306291392009-01-17 19:15:3677} // namespace
78
[email protected]ea114722012-03-12 01:11:2579BufferedResourceHandler::BufferedResourceHandler(
[email protected]d651cbc2012-05-31 21:33:0580 scoped_ptr<ResourceHandler> next_handler,
[email protected]ea114722012-03-12 01:11:2581 ResourceDispatcherHostImpl* host,
82 net::URLRequest* request)
[email protected]ef5306e2013-10-15 19:38:1883 : LayeredResourceHandler(request, next_handler.Pass()),
[email protected]926360f2012-06-29 04:45:0284 state_(STATE_STARTING),
[email protected]e3c404b2008-12-23 01:07:3285 host_(host),
[email protected]dd9b5462009-09-25 17:22:2386 read_buffer_size_(0),
[email protected]e3c404b2008-12-23 01:07:3287 bytes_read_(0),
[email protected]926360f2012-06-29 04:45:0288 must_download_(false),
[email protected]5c1d3e52012-08-01 05:14:2789 must_download_is_set_(false),
90 weak_ptr_factory_(this) {
[email protected]926360f2012-06-29 04:45:0291}
92
93BufferedResourceHandler::~BufferedResourceHandler() {
94}
95
96void 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]e3c404b2008-12-23 01:07:32104}
105
[email protected]2336ffe2011-11-24 01:23:34106bool BufferedResourceHandler::OnResponseStarted(
107 int request_id,
[email protected]22b305442012-05-21 18:02:59108 ResourceResponse* response,
109 bool* defer) {
[email protected]e3c404b2008-12-23 01:07:32110 response_ = response;
[email protected]22b305442012-05-21 18:02:59111
[email protected]926360f2012-06-29 04:45:02112 // 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]22b305442012-05-21 18:02:59121
[email protected]fc72bb12013-06-02 21:13:46122 if (!(response_->head.headers.get() &&
[email protected]926360f2012-06-29 04:45:02123 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]3ff679262012-10-12 23:40:05135
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]22b305442012-05-21 18:02:59141 }
[email protected]926360f2012-06-29 04:45:02142
143 state_ = STATE_PROCESSING;
144 return ProcessResponse(defer);
[email protected]e3c404b2008-12-23 01:07:32145}
146
[email protected]e3c404b2008-12-23 01:07:32147// We'll let the original event handler provide a buffer, and reuse it for
148// subsequent reads until we're done buffering.
[email protected]ef5306e2013-10-15 19:38:18149bool BufferedResourceHandler::OnWillRead(int request_id,
150 scoped_refptr<net::IOBuffer>* buf,
151 int* buf_size,
152 int min_size) {
[email protected]926360f2012-06-29 04:45:02153 if (state_ == STATE_STREAMING)
154 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size);
155
156 DCHECK_EQ(-1, min_size);
157
[email protected]fc72bb12013-06-02 21:13:46158 if (read_buffer_.get()) {
[email protected]926360f2012-06-29 04:45:02159 CHECK_LT(bytes_read_, read_buffer_size_);
[email protected]fc72bb12013-06-02 21:13:46160 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_);
[email protected]926360f2012-06-29 04:45:02161 *buf_size = read_buffer_size_ - bytes_read_;
162 } else {
163 if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size))
164 return false;
165
166 read_buffer_ = *buf;
167 read_buffer_size_ = *buf_size;
168 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2);
[email protected]e3c404b2008-12-23 01:07:32169 }
[email protected]67199052009-06-12 21:15:33170 return true;
[email protected]e3c404b2008-12-23 01:07:32171}
172
[email protected]926360f2012-06-29 04:45:02173bool BufferedResourceHandler::OnReadCompleted(int request_id, int bytes_read,
[email protected]22b305442012-05-21 18:02:59174 bool* defer) {
[email protected]926360f2012-06-29 04:45:02175 if (state_ == STATE_STREAMING)
176 return next_handler_->OnReadCompleted(request_id, bytes_read, defer);
[email protected]e3c404b2008-12-23 01:07:32177
[email protected]926360f2012-06-29 04:45:02178 DCHECK_EQ(state_, STATE_BUFFERING);
179 bytes_read_ += bytes_read;
[email protected]e3c404b2008-12-23 01:07:32180
[email protected]926360f2012-06-29 04:45:02181 if (!DetermineMimeType() && (bytes_read > 0))
182 return true; // Needs more data, so keep buffering.
183
184 state_ = STATE_PROCESSING;
185 return ProcessResponse(defer);
186}
187
[email protected]3780874a2013-11-18 05:49:03188void BufferedResourceHandler::OnResponseCompleted(
[email protected]926360f2012-06-29 04:45:02189 int request_id,
190 const net::URLRequestStatus& status,
[email protected]3780874a2013-11-18 05:49:03191 const std::string& security_info,
192 bool* defer) {
[email protected]926360f2012-06-29 04:45:02193 // Upon completion, act like a pass-through handler in case the downstream
194 // handler defers OnResponseCompleted.
195 state_ = STATE_STREAMING;
196
[email protected]3780874a2013-11-18 05:49:03197 next_handler_->OnResponseCompleted(request_id, status, security_info, defer);
[email protected]926360f2012-06-29 04:45:02198}
199
200void BufferedResourceHandler::Resume() {
201 switch (state_) {
202 case STATE_BUFFERING:
203 case STATE_PROCESSING:
204 NOTREACHED();
205 break;
206 case STATE_REPLAYING:
[email protected]dd32b1272013-05-04 14:17:11207 base::MessageLoop::current()->PostTask(
[email protected]926360f2012-06-29 04:45:02208 FROM_HERE,
209 base::Bind(&BufferedResourceHandler::CallReplayReadCompleted,
[email protected]5c1d3e52012-08-01 05:14:27210 weak_ptr_factory_.GetWeakPtr()));
[email protected]926360f2012-06-29 04:45:02211 break;
212 case STATE_STARTING:
213 case STATE_STREAMING:
214 controller()->Resume();
215 break;
216 }
217}
218
219void BufferedResourceHandler::Cancel() {
220 controller()->Cancel();
221}
222
[email protected]2756a8e2012-09-07 18:24:29223void BufferedResourceHandler::CancelAndIgnore() {
224 controller()->CancelAndIgnore();
225}
226
[email protected]af3d4962012-10-19 10:57:26227void BufferedResourceHandler::CancelWithError(int error_code) {
228 controller()->CancelWithError(error_code);
229}
230
[email protected]926360f2012-06-29 04:45:02231bool BufferedResourceHandler::ProcessResponse(bool* defer) {
232 DCHECK_EQ(STATE_PROCESSING, state_);
233
234 // TODO(darin): Stop special-casing 304 responses.
[email protected]fc72bb12013-06-02 21:13:46235 if (!(response_->head.headers.get() &&
[email protected]926360f2012-06-29 04:45:02236 response_->head.headers->response_code() == 304)) {
237 if (!SelectNextHandler(defer))
[email protected]e3c404b2008-12-23 01:07:32238 return false;
[email protected]22b305442012-05-21 18:02:59239 if (*defer)
[email protected]98d6f152011-09-29 19:35:51240 return true;
[email protected]926360f2012-06-29 04:45:02241 }
242
243 state_ = STATE_REPLAYING;
244
[email protected]ef5306e2013-10-15 19:38:18245 if (!next_handler_->OnResponseStarted(GetRequestID(), response_.get(), defer))
[email protected]926360f2012-06-29 04:45:02246 return false;
247
[email protected]fc72bb12013-06-02 21:13:46248 if (!read_buffer_.get()) {
[email protected]926360f2012-06-29 04:45:02249 state_ = STATE_STREAMING;
[email protected]35fa6a22009-08-15 00:04:01250 return true;
[email protected]e3c404b2008-12-23 01:07:32251 }
252
[email protected]926360f2012-06-29 04:45:02253 if (!*defer)
254 return ReplayReadCompleted(defer);
[email protected]5e5e0c82012-01-25 09:12:29255
[email protected]926360f2012-06-29 04:45:02256 return true;
[email protected]e3c404b2008-12-23 01:07:32257}
258
[email protected]926360f2012-06-29 04:45:02259bool BufferedResourceHandler::ShouldSniffContent() {
260 const std::string& mime_type = response_->head.mime_type;
[email protected]e3c404b2008-12-23 01:07:32261
262 std::string content_type_options;
[email protected]ef5306e2013-10-15 19:38:18263 request()->GetResponseHeaderByName("x-content-type-options",
264 &content_type_options);
[email protected]306291392009-01-17 19:15:36265
[email protected]926360f2012-06-29 04:45:02266 bool sniffing_blocked =
[email protected]423bd5b842009-01-23 17:30:50267 LowerCaseEqualsASCII(content_type_options, "nosniff");
[email protected]926360f2012-06-29 04:45:02268 bool we_would_like_to_sniff =
[email protected]ef5306e2013-10-15 19:38:18269 net::ShouldSniffMimeType(request()->url(), mime_type);
[email protected]306291392009-01-17 19:15:36270
271 RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type);
272
273 if (!sniffing_blocked && we_would_like_to_sniff) {
[email protected]e3c404b2008-12-23 01:07:32274 // We're going to look at the data before deciding what the content type
275 // is. That means we need to delay sending the ResponseStarted message
276 // over the IPC channel.
[email protected]ef5306e2013-10-15 19:38:18277 VLOG(1) << "To buffer: " << request()->url().spec();
[email protected]e3c404b2008-12-23 01:07:32278 return true;
279 }
280
[email protected]e3c404b2008-12-23 01:07:32281 return false;
282}
283
[email protected]926360f2012-06-29 04:45:02284bool BufferedResourceHandler::DetermineMimeType() {
285 DCHECK_EQ(STATE_BUFFERING, state_);
[email protected]35fa6a22009-08-15 00:04:01286
[email protected]926360f2012-06-29 04:45:02287 const std::string& type_hint = response_->head.mime_type;
288
289 std::string new_type;
290 bool made_final_decision =
[email protected]ef5306e2013-10-15 19:38:18291 net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(),
[email protected]926360f2012-06-29 04:45:02292 type_hint, &new_type);
293
294 // SniffMimeType() returns false if there is not enough data to determine
295 // the mime type. However, even if it returns false, it returns a new type
296 // that is probably better than the current one.
297 response_->head.mime_type.assign(new_type);
298
299 return made_final_decision;
[email protected]35fa6a22009-08-15 00:04:01300}
301
[email protected]926360f2012-06-29 04:45:02302bool BufferedResourceHandler::SelectNextHandler(bool* defer) {
303 DCHECK(!response_->head.mime_type.empty());
[email protected]e3c404b2008-12-23 01:07:32304
[email protected]ef5306e2013-10-15 19:38:18305 ResourceRequestInfoImpl* info = GetRequestInfo();
[email protected]926360f2012-06-29 04:45:02306 const std::string& mime_type = response_->head.mime_type;
[email protected]a755e1072009-10-23 16:58:37307
[email protected]3b455502012-12-11 18:22:58308 if (net::IsSupportedCertificateMimeType(mime_type)) {
309 // Install certificate file.
[email protected]6f3ac602014-02-13 21:38:05310 info->set_is_download(true);
[email protected]d651cbc2012-05-31 21:33:05311 scoped_ptr<ResourceHandler> handler(
[email protected]ef5306e2013-10-15 19:38:18312 new CertificateResourceHandler(request()));
[email protected]926360f2012-06-29 04:45:02313 return UseAlternateNextHandler(handler.Pass());
[email protected]5a9d58fa2012-05-29 15:15:04314 }
315
[email protected]926360f2012-06-29 04:45:02316 if (!info->allow_download())
[email protected]5e5e0c82012-01-25 09:12:29317 return true;
318
[email protected]3ca004f72012-12-18 10:35:21319 bool must_download = MustDownload();
320 if (!must_download) {
[email protected]926360f2012-06-29 04:45:02321 if (net::IsSupportedMimeType(mime_type))
[email protected]35fa6a22009-08-15 00:04:01322 return true;
[email protected]35fa6a22009-08-15 00:04:01323
[email protected]646889822013-03-20 00:26:45324 scoped_ptr<ResourceHandler> handler(
[email protected]ef5306e2013-10-15 19:38:18325 host_->MaybeInterceptAsStream(request(), response_.get()));
[email protected]646889822013-03-20 00:26:45326 if (handler)
327 return UseAlternateNextHandler(handler.Pass());
328
[email protected]ebd71962012-12-20 02:56:55329#if defined(ENABLE_PLUGINS)
[email protected]926360f2012-06-29 04:45:02330 bool stale;
331 bool has_plugin = HasSupportingPlugin(&stale);
[email protected]68598072011-07-29 08:21:28332 if (stale) {
[email protected]926360f2012-06-29 04:45:02333 // Refresh the plugins asynchronously.
334 PluginServiceImpl::GetInstance()->GetPlugins(
[email protected]5c1d3e52012-08-01 05:14:27335 base::Bind(&BufferedResourceHandler::OnPluginsLoaded,
336 weak_ptr_factory_.GetWeakPtr()));
[email protected]ae9c0922014-02-13 22:40:23337 request()->LogBlockedBy("BufferedResourceHandler");
[email protected]926360f2012-06-29 04:45:02338 *defer = true;
[email protected]35fa6a22009-08-15 00:04:01339 return true;
340 }
[email protected]926360f2012-06-29 04:45:02341 if (has_plugin)
342 return true;
[email protected]ebd71962012-12-20 02:56:55343#endif
[email protected]35fa6a22009-08-15 00:04:01344 }
345
[email protected]926360f2012-06-29 04:45:02346 // Install download handler
347 info->set_is_download(true);
348 scoped_ptr<ResourceHandler> handler(
349 host_->CreateResourceHandlerForDownload(
[email protected]ef5306e2013-10-15 19:38:18350 request(),
[email protected]926360f2012-06-29 04:45:02351 true, // is_content_initiated
[email protected]3ca004f72012-12-18 10:35:21352 must_download,
[email protected]530047e2013-07-12 17:02:25353 content::DownloadItem::kInvalidId,
[email protected]c873c862012-10-17 15:32:12354 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()),
[email protected]530047e2013-07-12 17:02:25355 DownloadUrlParameters::OnStartedCallback()));
[email protected]926360f2012-06-29 04:45:02356 return UseAlternateNextHandler(handler.Pass());
[email protected]35fa6a22009-08-15 00:04:01357}
358
[email protected]926360f2012-06-29 04:45:02359bool BufferedResourceHandler::UseAlternateNextHandler(
360 scoped_ptr<ResourceHandler> new_handler) {
[email protected]fc72bb12013-06-02 21:13:46361 if (response_->head.headers.get() && // Can be NULL if FTP.
[email protected]926360f2012-06-29 04:45:02362 response_->head.headers->response_code() / 100 != 2) {
363 // The response code indicates that this is an error page, but we don't
364 // know how to display the content. We follow Firefox here and show our
365 // own error page instead of triggering a download.
366 // TODO(abarth): We should abstract the response_code test, but this kind
367 // of check is scattered throughout our codebase.
[email protected]ef5306e2013-10-15 19:38:18368 request()->CancelWithError(net::ERR_FILE_NOT_FOUND);
[email protected]926360f2012-06-29 04:45:02369 return false;
370 }
371
[email protected]ef5306e2013-10-15 19:38:18372 int request_id = GetRequestID();
[email protected]926360f2012-06-29 04:45:02373
[email protected]0dc72bbe2010-04-08 15:29:17374 // Inform the original ResourceHandler that this will be handled entirely by
375 // the new ResourceHandler.
[email protected]5e5e0c82012-01-25 09:12:29376 // TODO(darin): We should probably check the return values of these.
[email protected]22b305442012-05-21 18:02:59377 bool defer_ignored = false;
[email protected]fc72bb12013-06-02 21:13:46378 next_handler_->OnResponseStarted(request_id, response_.get(), &defer_ignored);
[email protected]6f3ac602014-02-13 21:38:05379 // Although deferring OnResponseStarted is legal, the only downstream handler
380 // which does so is CrossSiteResourceHandler. Cross-site transitions should
381 // not trigger when switching handlers.
[email protected]22b305442012-05-21 18:02:59382 DCHECK(!defer_ignored);
[email protected]2756a8e2012-09-07 18:24:29383 net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
384 net::ERR_ABORTED);
[email protected]3780874a2013-11-18 05:49:03385 next_handler_->OnResponseCompleted(request_id, status, std::string(),
386 &defer_ignored);
387 DCHECK(!defer_ignored);
[email protected]0dc72bbe2010-04-08 15:29:17388
389 // This is handled entirely within the new ResourceHandler, so just reset the
390 // original ResourceHandler.
[email protected]926360f2012-06-29 04:45:02391 next_handler_ = new_handler.Pass();
392 next_handler_->SetController(this);
[email protected]5e5e0c82012-01-25 09:12:29393
[email protected]926360f2012-06-29 04:45:02394 return CopyReadBufferToNextHandler(request_id);
[email protected]5e5e0c82012-01-25 09:12:29395}
396
[email protected]926360f2012-06-29 04:45:02397bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) {
[email protected]fc72bb12013-06-02 21:13:46398 DCHECK(read_buffer_.get());
[email protected]5e5e0c82012-01-25 09:12:29399
[email protected]ef5306e2013-10-15 19:38:18400 bool result = next_handler_->OnReadCompleted(GetRequestID(), bytes_read_,
401 defer);
[email protected]926360f2012-06-29 04:45:02402
403 read_buffer_ = NULL;
404 read_buffer_size_ = 0;
405 bytes_read_ = 0;
406
407 state_ = STATE_STREAMING;
408
409 return result;
[email protected]5e5e0c82012-01-25 09:12:29410}
411
[email protected]926360f2012-06-29 04:45:02412void BufferedResourceHandler::CallReplayReadCompleted() {
413 bool defer = false;
414 if (!ReplayReadCompleted(&defer)) {
415 controller()->Cancel();
416 } else if (!defer) {
417 state_ = STATE_STREAMING;
418 controller()->Resume();
419 }
420}
421
422bool BufferedResourceHandler::MustDownload() {
423 if (must_download_is_set_)
424 return must_download_;
425
426 must_download_is_set_ = true;
427
428 std::string disposition;
[email protected]ef5306e2013-10-15 19:38:18429 request()->GetResponseHeaderByName("content-disposition", &disposition);
[email protected]926360f2012-06-29 04:45:02430 if (!disposition.empty() &&
431 net::HttpContentDisposition(disposition, std::string()).is_attachment()) {
432 must_download_ = true;
433 } else if (host_->delegate() &&
434 host_->delegate()->ShouldForceDownloadResource(
[email protected]ef5306e2013-10-15 19:38:18435 request()->url(), response_->head.mime_type)) {
[email protected]926360f2012-06-29 04:45:02436 must_download_ = true;
437 } else {
438 must_download_ = false;
439 }
440
441 return must_download_;
442}
443
444bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) {
[email protected]012a1712013-11-18 05:29:41445#if defined(ENABLE_PLUGINS)
[email protected]ef5306e2013-10-15 19:38:18446 ResourceRequestInfoImpl* info = GetRequestInfo();
[email protected]926360f2012-06-29 04:45:02447
448 bool allow_wildcard = false;
[email protected]d7bd3e52013-07-21 04:29:20449 WebPluginInfo plugin;
[email protected]926360f2012-06-29 04:45:02450 return PluginServiceImpl::GetInstance()->GetPluginInfo(
[email protected]60eca4eb2013-12-06 00:02:16451 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(),
[email protected]ef5306e2013-10-15 19:38:18452 request()->url(), GURL(), response_->head.mime_type, allow_wildcard,
[email protected]926360f2012-06-29 04:45:02453 stale, &plugin, NULL);
[email protected]012a1712013-11-18 05:29:41454#else
455 if (stale)
456 *stale = false;
457 return false;
458#endif
[email protected]926360f2012-06-29 04:45:02459}
460
461bool BufferedResourceHandler::CopyReadBufferToNextHandler(int request_id) {
[email protected]5e5e0c82012-01-25 09:12:29462 if (!bytes_read_)
[email protected]926360f2012-06-29 04:45:02463 return true;
[email protected]5e5e0c82012-01-25 09:12:29464
[email protected]ef5306e2013-10-15 19:38:18465 scoped_refptr<net::IOBuffer> buf;
[email protected]5e5e0c82012-01-25 09:12:29466 int buf_len = 0;
[email protected]926360f2012-06-29 04:45:02467 if (!next_handler_->OnWillRead(request_id, &buf, &buf_len, bytes_read_))
468 return false;
469
470 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0));
471 memcpy(buf->data(), read_buffer_->data(), bytes_read_);
472 return true;
[email protected]0dc72bbe2010-04-08 15:29:17473}
474
[email protected]d33e7cc2011-09-23 01:43:56475void BufferedResourceHandler::OnPluginsLoaded(
[email protected]d7bd3e52013-07-21 04:29:20476 const std::vector<WebPluginInfo>& plugins) {
[email protected]ae9c0922014-02-13 22:40:23477 request()->LogUnblocked();
[email protected]22b305442012-05-21 18:02:59478 bool defer = false;
[email protected]926360f2012-06-29 04:45:02479 if (!ProcessResponse(&defer)) {
[email protected]56f0b082012-06-14 07:12:32480 controller()->Cancel();
[email protected]926360f2012-06-29 04:45:02481 } else if (!defer) {
[email protected]56f0b082012-06-14 07:12:32482 controller()->Resume();
[email protected]22b305442012-05-21 18:02:59483 }
[email protected]e3c404b2008-12-23 01:07:32484}
[email protected]d18720a2012-01-06 09:53:55485
486} // namespace content