OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/buffered_resource_handler.h" | 5 #include "content/browser/loader/buffered_resource_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 type_hint, &new_type); | 290 type_hint, &new_type); |
291 | 291 |
292 // SniffMimeType() returns false if there is not enough data to determine | 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 | 293 // the mime type. However, even if it returns false, it returns a new type |
294 // that is probably better than the current one. | 294 // that is probably better than the current one. |
295 response_->head.mime_type.assign(new_type); | 295 response_->head.mime_type.assign(new_type); |
296 | 296 |
297 return made_final_decision; | 297 return made_final_decision; |
298 } | 298 } |
299 | 299 |
300 // Returns true if a plugin will handle the current request. |defer| is set | |
301 // to true if plugin data is stale and needs to be refreshed before the | |
302 // request can be handled. |request_handled| is false if the request should be | |
303 // cancelled and true otherwise. | |
mmenke
2015/03/25 14:11:44
Method-level docs should go before the declaration
Deepak
2015/03/26 04:55:11
Done.
| |
304 bool BufferedResourceHandler::IsHandledByPlugin(bool* defer, | |
305 bool* request_handled) { | |
306 #if defined(ENABLE_PLUGINS) | |
307 bool stale; | |
308 WebPluginInfo plugin; | |
309 bool allow_wildcard = false; | |
310 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
311 bool has_plugin = plugin_service_->GetPluginInfo( | |
312 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(), | |
313 request()->url(), GURL(), response_->head.mime_type, allow_wildcard, | |
314 &stale, &plugin, NULL); | |
315 | |
316 if (stale) { | |
317 // Refresh the plugins asynchronously. | |
318 plugin_service_->GetPlugins( | |
319 base::Bind(&BufferedResourceHandler::OnPluginsLoaded, | |
320 weak_ptr_factory_.GetWeakPtr())); | |
321 request()->LogBlockedBy("BufferedResourceHandler"); | |
322 *defer = true; | |
323 return true; | |
324 } | |
325 | |
326 if (has_plugin) { | |
327 if (plugin.type == WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN) { | |
328 // If it is a MimeHandlerView plugin, intercept the stream. | |
329 std::string payload; | |
330 scoped_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream( | |
331 plugin.path, request(), response_.get(), &payload)); | |
332 if (handler) { | |
333 *request_handled = UseAlternateNextHandler(handler.Pass(), payload); | |
334 return true; | |
335 } | |
336 return false; | |
337 } else { | |
338 return true; | |
339 } | |
340 } | |
341 | |
342 // If execution reaches here then we should try intercepting the stream for | |
343 // the old streamsPrivate extensions API. This API is deprecated and should | |
344 // go away. | |
345 std::string payload; | |
346 scoped_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream( | |
347 base::FilePath(), request(), response_.get(), &payload)); | |
348 if (handler) { | |
349 *request_handled = UseAlternateNextHandler(handler.Pass(), payload); | |
350 return true; | |
351 } | |
352 #endif | |
353 return false; | |
354 } | |
355 | |
300 bool BufferedResourceHandler::SelectNextHandler(bool* defer) { | 356 bool BufferedResourceHandler::SelectNextHandler(bool* defer) { |
301 DCHECK(!response_->head.mime_type.empty()); | 357 DCHECK(!response_->head.mime_type.empty()); |
302 | 358 |
303 ResourceRequestInfoImpl* info = GetRequestInfo(); | 359 ResourceRequestInfoImpl* info = GetRequestInfo(); |
304 const std::string& mime_type = response_->head.mime_type; | 360 const std::string& mime_type = response_->head.mime_type; |
305 | 361 |
306 if (net::IsSupportedCertificateMimeType(mime_type)) { | 362 if (net::IsSupportedCertificateMimeType(mime_type)) { |
307 // Install certificate file. | 363 // Install certificate file. |
308 info->set_is_download(true); | 364 info->set_is_download(true); |
309 scoped_ptr<ResourceHandler> handler( | 365 scoped_ptr<ResourceHandler> handler( |
310 new CertificateResourceHandler(request())); | 366 new CertificateResourceHandler(request())); |
311 return UseAlternateNextHandler(handler.Pass(), std::string()); | 367 return UseAlternateNextHandler(handler.Pass(), std::string()); |
312 } | 368 } |
313 | 369 |
314 // Allow requests for object/embed tags to be intercepted as streams. | 370 // Allow requests for object/embed tags to be intercepted as streams. |
315 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { | 371 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { |
316 DCHECK(!info->allow_download()); | 372 DCHECK(!info->allow_download()); |
317 std::string payload; | 373 |
318 scoped_ptr<ResourceHandler> handler( | 374 bool request_handled = true; |
319 host_->MaybeInterceptAsStream(request(), response_.get(), &payload)); | 375 bool handled_by_plugin = IsHandledByPlugin(defer, &request_handled); |
320 if (handler) { | 376 if (!request_handled) |
321 DCHECK(!net::IsSupportedMimeType(mime_type)); | 377 return false; |
322 return UseAlternateNextHandler(handler.Pass(), payload); | 378 if (handled_by_plugin) |
323 } | 379 return true; |
324 } | 380 } |
325 | 381 |
326 if (!info->allow_download()) | 382 if (!info->allow_download()) |
327 return true; | 383 return true; |
328 | 384 |
329 // info->allow_download() == true implies | 385 // info->allow_download() == true implies |
330 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or | 386 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or |
331 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. | 387 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. |
332 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || | 388 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || |
333 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); | 389 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); |
334 | 390 |
335 bool must_download = MustDownload(); | 391 bool must_download = MustDownload(); |
336 if (!must_download) { | 392 if (!must_download) { |
337 if (net::IsSupportedMimeType(mime_type)) | 393 if (net::IsSupportedMimeType(mime_type)) |
338 return true; | 394 return true; |
339 | 395 |
340 std::string payload; | 396 bool request_handled = true; |
341 scoped_ptr<ResourceHandler> handler( | 397 bool handled_by_plugin = IsHandledByPlugin(defer, &request_handled); |
342 host_->MaybeInterceptAsStream(request(), response_.get(), &payload)); | 398 if (!request_handled) |
343 if (handler) { | 399 return false; |
344 return UseAlternateNextHandler(handler.Pass(), payload); | 400 if (handled_by_plugin) |
345 } | |
346 | |
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; | 401 return true; |
358 } | |
359 if (has_plugin) | |
360 return true; | |
361 #endif | |
362 } | 402 } |
363 | 403 |
364 // Install download handler | 404 // Install download handler |
365 info->set_is_download(true); | 405 info->set_is_download(true); |
366 scoped_ptr<ResourceHandler> handler( | 406 scoped_ptr<ResourceHandler> handler( |
367 host_->CreateResourceHandlerForDownload( | 407 host_->CreateResourceHandlerForDownload( |
368 request(), | 408 request(), |
369 true, // is_content_initiated | 409 true, // is_content_initiated |
370 must_download, | 410 must_download, |
371 DownloadItem::kInvalidId, | 411 DownloadItem::kInvalidId, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 host_->delegate()->ShouldForceDownloadResource( | 508 host_->delegate()->ShouldForceDownloadResource( |
469 request()->url(), response_->head.mime_type)) { | 509 request()->url(), response_->head.mime_type)) { |
470 must_download_ = true; | 510 must_download_ = true; |
471 } else { | 511 } else { |
472 must_download_ = false; | 512 must_download_ = false; |
473 } | 513 } |
474 | 514 |
475 return must_download_; | 515 return must_download_; |
476 } | 516 } |
477 | 517 |
478 bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) { | |
479 #if defined(ENABLE_PLUGINS) | |
480 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
481 | |
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 | |
493 } | |
494 | |
495 bool BufferedResourceHandler::CopyReadBufferToNextHandler() { | 518 bool BufferedResourceHandler::CopyReadBufferToNextHandler() { |
496 if (!read_buffer_.get()) | 519 if (!read_buffer_.get()) |
497 return true; | 520 return true; |
498 | 521 |
499 scoped_refptr<net::IOBuffer> buf; | 522 scoped_refptr<net::IOBuffer> buf; |
500 int buf_len = 0; | 523 int buf_len = 0; |
501 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) | 524 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) |
502 return false; | 525 return false; |
503 | 526 |
504 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); | 527 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
505 memcpy(buf->data(), read_buffer_->data(), bytes_read_); | 528 memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
506 return true; | 529 return true; |
507 } | 530 } |
508 | 531 |
509 void BufferedResourceHandler::OnPluginsLoaded( | 532 void BufferedResourceHandler::OnPluginsLoaded( |
510 const std::vector<WebPluginInfo>& plugins) { | 533 const std::vector<WebPluginInfo>& plugins) { |
511 request()->LogUnblocked(); | 534 request()->LogUnblocked(); |
512 bool defer = false; | 535 bool defer = false; |
513 if (!ProcessResponse(&defer)) { | 536 if (!ProcessResponse(&defer)) { |
514 controller()->Cancel(); | 537 controller()->Cancel(); |
515 } else if (!defer) { | 538 } else if (!defer) { |
516 controller()->Resume(); | 539 controller()->Resume(); |
517 } | 540 } |
518 } | 541 } |
519 | 542 |
520 } // namespace content | 543 } // namespace content |
OLD | NEW |