Add remoting and PPAPI tests to GN build
[chromium-blink-merge.git] / content / browser / loader / buffered_resource_handler.cc
blob67e74a586f5231d3290ee813674357ce1d43bc7a
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/loader/buffered_resource_handler.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_util.h"
13 #include "content/browser/download/download_resource_handler.h"
14 #include "content/browser/download/download_stats.h"
15 #include "content/browser/loader/certificate_resource_handler.h"
16 #include "content/browser/loader/resource_dispatcher_host_impl.h"
17 #include "content/browser/loader/resource_request_info_impl.h"
18 #include "content/browser/loader/stream_resource_handler.h"
19 #include "content/public/browser/content_browser_client.h"
20 #include "content/public/browser/download_item.h"
21 #include "content/public/browser/download_save_info.h"
22 #include "content/public/browser/download_url_parameters.h"
23 #include "content/public/browser/plugin_service.h"
24 #include "content/public/browser/resource_context.h"
25 #include "content/public/browser/resource_dispatcher_host_delegate.h"
26 #include "content/public/common/resource_response.h"
27 #include "content/public/common/webplugininfo.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/mime_sniffer.h"
30 #include "net/base/mime_util.h"
31 #include "net/base/net_errors.h"
32 #include "net/http/http_content_disposition.h"
33 #include "net/http/http_response_headers.h"
35 namespace content {
37 namespace {
39 void RecordSnifferMetrics(bool sniffing_blocked,
40 bool we_would_like_to_sniff,
41 const std::string& mime_type) {
42 static base::HistogramBase* nosniff_usage(NULL);
43 if (!nosniff_usage)
44 nosniff_usage = base::BooleanHistogram::FactoryGet(
45 "nosniff.usage", base::HistogramBase::kUmaTargetedHistogramFlag);
46 nosniff_usage->AddBoolean(sniffing_blocked);
48 if (sniffing_blocked) {
49 static base::HistogramBase* nosniff_otherwise(NULL);
50 if (!nosniff_otherwise)
51 nosniff_otherwise = base::BooleanHistogram::FactoryGet(
52 "nosniff.otherwise", base::HistogramBase::kUmaTargetedHistogramFlag);
53 nosniff_otherwise->AddBoolean(we_would_like_to_sniff);
55 static base::HistogramBase* nosniff_empty_mime_type(NULL);
56 if (!nosniff_empty_mime_type)
57 nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet(
58 "nosniff.empty_mime_type",
59 base::HistogramBase::kUmaTargetedHistogramFlag);
60 nosniff_empty_mime_type->AddBoolean(mime_type.empty());
64 // Used to write into an existing IOBuffer at a given offset.
65 class DependentIOBuffer : public net::WrappedIOBuffer {
66 public:
67 DependentIOBuffer(net::IOBuffer* buf, int offset)
68 : net::WrappedIOBuffer(buf->data() + offset),
69 buf_(buf) {
72 private:
73 ~DependentIOBuffer() override {}
75 scoped_refptr<net::IOBuffer> buf_;
78 } // namespace
80 BufferedResourceHandler::BufferedResourceHandler(
81 scoped_ptr<ResourceHandler> next_handler,
82 ResourceDispatcherHostImpl* host,
83 PluginService* plugin_service,
84 net::URLRequest* request)
85 : LayeredResourceHandler(request, next_handler.Pass()),
86 state_(STATE_STARTING),
87 host_(host),
88 plugin_service_(plugin_service),
89 read_buffer_size_(0),
90 bytes_read_(0),
91 must_download_(false),
92 must_download_is_set_(false),
93 weak_ptr_factory_(this) {
96 BufferedResourceHandler::~BufferedResourceHandler() {
99 void BufferedResourceHandler::SetController(ResourceController* controller) {
100 ResourceHandler::SetController(controller);
102 // Downstream handlers see us as their ResourceController, which allows us to
103 // consume part or all of the resource response, and then later replay it to
104 // downstream handler.
105 DCHECK(next_handler_.get());
106 next_handler_->SetController(this);
109 bool BufferedResourceHandler::OnResponseStarted(ResourceResponse* response,
110 bool* defer) {
111 response_ = response;
113 // TODO(darin): It is very odd to special-case 304 responses at this level.
114 // We do so only because the code always has, see r24977 and r29355. The
115 // fact that 204 is no longer special-cased this way suggests that 304 need
116 // not be special-cased either.
118 // The network stack only forwards 304 responses that were not received in
119 // response to a conditional request (i.e., If-Modified-Since). Other 304
120 // responses end up being translated to 200 or whatever the cached response
121 // code happens to be. It should be very rare to see a 304 at this level.
123 if (!(response_->head.headers.get() &&
124 response_->head.headers->response_code() == 304)) {
125 if (ShouldSniffContent()) {
126 state_ = STATE_BUFFERING;
127 return true;
130 if (response_->head.mime_type.empty()) {
131 // Ugg. The server told us not to sniff the content but didn't give us
132 // a mime type. What's a browser to do? Turns out, we're supposed to
133 // treat the response as "text/plain". This is the most secure option.
134 response_->head.mime_type.assign("text/plain");
137 // Treat feed types as text/plain.
138 if (response_->head.mime_type == "application/rss+xml" ||
139 response_->head.mime_type == "application/atom+xml") {
140 response_->head.mime_type.assign("text/plain");
144 state_ = STATE_PROCESSING;
145 return ProcessResponse(defer);
148 // We'll let the original event handler provide a buffer, and reuse it for
149 // subsequent reads until we're done buffering.
150 bool BufferedResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
151 int* buf_size,
152 int min_size) {
153 if (state_ == STATE_STREAMING)
154 return next_handler_->OnWillRead(buf, buf_size, min_size);
156 DCHECK_EQ(-1, min_size);
158 if (read_buffer_.get()) {
159 CHECK_LT(bytes_read_, read_buffer_size_);
160 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_);
161 *buf_size = read_buffer_size_ - bytes_read_;
162 } else {
163 if (!next_handler_->OnWillRead(buf, buf_size, min_size))
164 return false;
166 read_buffer_ = *buf;
167 read_buffer_size_ = *buf_size;
168 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2);
170 return true;
173 bool BufferedResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
174 if (state_ == STATE_STREAMING)
175 return next_handler_->OnReadCompleted(bytes_read, defer);
177 DCHECK_EQ(state_, STATE_BUFFERING);
178 bytes_read_ += bytes_read;
180 if (!DetermineMimeType() && (bytes_read > 0))
181 return true; // Needs more data, so keep buffering.
183 state_ = STATE_PROCESSING;
184 return ProcessResponse(defer);
187 void BufferedResourceHandler::OnResponseCompleted(
188 const net::URLRequestStatus& status,
189 const std::string& security_info,
190 bool* defer) {
191 // Upon completion, act like a pass-through handler in case the downstream
192 // handler defers OnResponseCompleted.
193 state_ = STATE_STREAMING;
195 next_handler_->OnResponseCompleted(status, security_info, defer);
198 void BufferedResourceHandler::Resume() {
199 switch (state_) {
200 case STATE_BUFFERING:
201 case STATE_PROCESSING:
202 NOTREACHED();
203 break;
204 case STATE_REPLAYING:
205 base::MessageLoop::current()->PostTask(
206 FROM_HERE,
207 base::Bind(&BufferedResourceHandler::CallReplayReadCompleted,
208 weak_ptr_factory_.GetWeakPtr()));
209 break;
210 case STATE_STARTING:
211 case STATE_STREAMING:
212 controller()->Resume();
213 break;
217 void BufferedResourceHandler::Cancel() {
218 controller()->Cancel();
221 void BufferedResourceHandler::CancelAndIgnore() {
222 controller()->CancelAndIgnore();
225 void BufferedResourceHandler::CancelWithError(int error_code) {
226 controller()->CancelWithError(error_code);
229 bool BufferedResourceHandler::ProcessResponse(bool* defer) {
230 DCHECK_EQ(STATE_PROCESSING, state_);
232 // TODO(darin): Stop special-casing 304 responses.
233 if (!(response_->head.headers.get() &&
234 response_->head.headers->response_code() == 304)) {
235 if (!SelectNextHandler(defer))
236 return false;
237 if (*defer)
238 return true;
241 state_ = STATE_REPLAYING;
243 if (!next_handler_->OnResponseStarted(response_.get(), defer))
244 return false;
246 if (!read_buffer_.get()) {
247 state_ = STATE_STREAMING;
248 return true;
251 if (!*defer)
252 return ReplayReadCompleted(defer);
254 return true;
257 bool BufferedResourceHandler::ShouldSniffContent() {
258 const std::string& mime_type = response_->head.mime_type;
260 std::string content_type_options;
261 request()->GetResponseHeaderByName("x-content-type-options",
262 &content_type_options);
264 bool sniffing_blocked =
265 LowerCaseEqualsASCII(content_type_options, "nosniff");
266 bool we_would_like_to_sniff =
267 net::ShouldSniffMimeType(request()->url(), mime_type);
269 RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type);
271 if (!sniffing_blocked && we_would_like_to_sniff) {
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.
275 VLOG(1) << "To buffer: " << request()->url().spec();
276 return true;
279 return false;
282 bool BufferedResourceHandler::DetermineMimeType() {
283 DCHECK_EQ(STATE_BUFFERING, state_);
285 const std::string& type_hint = response_->head.mime_type;
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);
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);
297 return made_final_decision;
300 bool BufferedResourceHandler::SelectNextHandler(bool* defer) {
301 DCHECK(!response_->head.mime_type.empty());
303 ResourceRequestInfoImpl* info = GetRequestInfo();
304 const std::string& mime_type = response_->head.mime_type;
306 if (net::IsSupportedCertificateMimeType(mime_type)) {
307 // Install certificate file.
308 info->set_is_download(true);
309 scoped_ptr<ResourceHandler> handler(
310 new CertificateResourceHandler(request()));
311 return UseAlternateNextHandler(handler.Pass(), std::string());
314 // Allow requests for object/embed tags to be intercepted as streams.
315 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) {
316 DCHECK(!info->allow_download());
317 std::string payload;
318 scoped_ptr<ResourceHandler> handler(
319 host_->MaybeInterceptAsStream(request(), response_.get(), &payload));
320 if (handler) {
321 DCHECK(!net::IsSupportedMimeType(mime_type));
322 return UseAlternateNextHandler(handler.Pass(), payload);
326 if (!info->allow_download())
327 return true;
329 // info->allow_download() == true implies
330 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or
331 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME.
332 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME ||
333 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME);
335 bool must_download = MustDownload();
336 if (!must_download) {
337 if (net::IsSupportedMimeType(mime_type))
338 return true;
340 std::string payload;
341 scoped_ptr<ResourceHandler> handler(
342 host_->MaybeInterceptAsStream(request(), response_.get(), &payload));
343 if (handler) {
344 return UseAlternateNextHandler(handler.Pass(), payload);
347 #if defined(ENABLE_PLUGINS)
348 bool stale;
349 bool has_plugin = HasSupportingPlugin(&stale);
350 if (stale) {
351 // Refresh the plugins asynchronously.
352 plugin_service_->GetPlugins(
353 base::Bind(&BufferedResourceHandler::OnPluginsLoaded,
354 weak_ptr_factory_.GetWeakPtr()));
355 request()->LogBlockedBy("BufferedResourceHandler");
356 *defer = true;
357 return true;
359 if (has_plugin)
360 return true;
361 #endif
364 // Install download handler
365 info->set_is_download(true);
366 scoped_ptr<ResourceHandler> handler(
367 host_->CreateResourceHandlerForDownload(
368 request(),
369 true, // is_content_initiated
370 must_download,
371 DownloadItem::kInvalidId,
372 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()),
373 DownloadUrlParameters::OnStartedCallback()));
374 return UseAlternateNextHandler(handler.Pass(), std::string());
377 bool BufferedResourceHandler::UseAlternateNextHandler(
378 scoped_ptr<ResourceHandler> new_handler,
379 const std::string& payload_for_old_handler) {
380 if (response_->head.headers.get() && // Can be NULL if FTP.
381 response_->head.headers->response_code() / 100 != 2) {
382 // The response code indicates that this is an error page, but we don't
383 // know how to display the content. We follow Firefox here and show our
384 // own error page instead of triggering a download.
385 // TODO(abarth): We should abstract the response_code test, but this kind
386 // of check is scattered throughout our codebase.
387 request()->CancelWithError(net::ERR_INVALID_RESPONSE);
388 return false;
391 // Inform the original ResourceHandler that this will be handled entirely by
392 // the new ResourceHandler.
393 // TODO(darin): We should probably check the return values of these.
394 bool defer_ignored = false;
395 next_handler_->OnResponseStarted(response_.get(), &defer_ignored);
396 // Although deferring OnResponseStarted is legal, the only downstream handler
397 // which does so is CrossSiteResourceHandler. Cross-site transitions should
398 // not trigger when switching handlers.
399 DCHECK(!defer_ignored);
400 if (payload_for_old_handler.empty()) {
401 net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
402 net::ERR_ABORTED);
403 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
404 DCHECK(!defer_ignored);
405 } else {
406 scoped_refptr<net::IOBuffer> buf;
407 int size = 0;
409 next_handler_->OnWillRead(&buf, &size, -1);
410 CHECK_GE(size, static_cast<int>(payload_for_old_handler.length()));
412 memcpy(buf->data(), payload_for_old_handler.c_str(),
413 payload_for_old_handler.length());
415 next_handler_->OnReadCompleted(payload_for_old_handler.length(),
416 &defer_ignored);
417 DCHECK(!defer_ignored);
419 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
420 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
421 DCHECK(!defer_ignored);
424 // This is handled entirely within the new ResourceHandler, so just reset the
425 // original ResourceHandler.
426 next_handler_ = new_handler.Pass();
427 next_handler_->SetController(this);
429 return CopyReadBufferToNextHandler();
432 bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) {
433 DCHECK(read_buffer_.get());
435 bool result = next_handler_->OnReadCompleted(bytes_read_, defer);
437 read_buffer_ = NULL;
438 read_buffer_size_ = 0;
439 bytes_read_ = 0;
441 state_ = STATE_STREAMING;
443 return result;
446 void BufferedResourceHandler::CallReplayReadCompleted() {
447 bool defer = false;
448 if (!ReplayReadCompleted(&defer)) {
449 controller()->Cancel();
450 } else if (!defer) {
451 state_ = STATE_STREAMING;
452 controller()->Resume();
456 bool BufferedResourceHandler::MustDownload() {
457 if (must_download_is_set_)
458 return must_download_;
460 must_download_is_set_ = true;
462 std::string disposition;
463 request()->GetResponseHeaderByName("content-disposition", &disposition);
464 if (!disposition.empty() &&
465 net::HttpContentDisposition(disposition, std::string()).is_attachment()) {
466 must_download_ = true;
467 } else if (host_->delegate() &&
468 host_->delegate()->ShouldForceDownloadResource(
469 request()->url(), response_->head.mime_type)) {
470 must_download_ = true;
471 } else {
472 must_download_ = false;
475 return must_download_;
478 bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) {
479 #if defined(ENABLE_PLUGINS)
480 ResourceRequestInfoImpl* info = GetRequestInfo();
482 bool allow_wildcard = false;
483 WebPluginInfo plugin;
484 return plugin_service_->GetPluginInfo(
485 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(),
486 request()->url(), GURL(), response_->head.mime_type, allow_wildcard,
487 stale, &plugin, NULL);
488 #else
489 if (stale)
490 *stale = false;
491 return false;
492 #endif
495 bool BufferedResourceHandler::CopyReadBufferToNextHandler() {
496 if (!read_buffer_.get())
497 return true;
499 scoped_refptr<net::IOBuffer> buf;
500 int buf_len = 0;
501 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_))
502 return false;
504 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0));
505 memcpy(buf->data(), read_buffer_->data(), bytes_read_);
506 return true;
509 void BufferedResourceHandler::OnPluginsLoaded(
510 const std::vector<WebPluginInfo>& plugins) {
511 request()->LogUnblocked();
512 bool defer = false;
513 if (!ProcessResponse(&defer)) {
514 controller()->Cancel();
515 } else if (!defer) {
516 controller()->Resume();
520 } // namespace content