| 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 bool BufferedResourceHandler::IsHandledByPlugin(bool* defer, | |
| 301 bool* request_handled) { | |
| 302 #if defined(ENABLE_PLUGINS) | |
| 303 bool stale; | |
| 304 WebPluginInfo plugin; | |
| 305 bool allow_wildcard = false; | |
| 306 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 307 bool has_plugin = plugin_service_->GetPluginInfo( | |
| 308 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(), | |
| 309 request()->url(), GURL(), response_->head.mime_type, allow_wildcard, | |
| 310 &stale, &plugin, NULL); | |
| 311 | |
| 312 if (stale) { | |
| 313 // Refresh the plugins asynchronously. | |
| 314 plugin_service_->GetPlugins( | |
| 315 base::Bind(&BufferedResourceHandler::OnPluginsLoaded, | |
| 316 weak_ptr_factory_.GetWeakPtr())); | |
| 317 request()->LogBlockedBy("BufferedResourceHandler"); | |
| 318 *defer = true; | |
| 319 return true; | |
| 320 } | |
| 321 | |
| 322 if (has_plugin) { | |
| 323 if (plugin.type == WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN) { | |
| 324 // If it is a MimeHandlerView plugin, intercept the stream. | |
| 325 std::string payload; | |
| 326 scoped_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream( | |
| 327 plugin.path, request(), response_.get(), &payload)); | |
| 328 if (handler) { | |
| 329 *request_handled = UseAlternateNextHandler(handler.Pass(), payload); | |
| 330 return true; | |
| 331 } | |
| 332 return false; | |
| 333 } else { | |
| 334 return true; | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 // If execution reaches here then we should try intercepting the stream for | |
| 339 // the old streamsPrivate extensions API. This API is deprecated and should | |
| 340 // go away. | |
| 341 std::string payload; | |
| 342 scoped_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream( | |
| 343 base::FilePath(), request(), response_.get(), &payload)); | |
| 344 if (handler) { | |
| 345 *request_handled = UseAlternateNextHandler(handler.Pass(), payload); | |
| 346 return true; | |
| 347 } | |
| 348 #endif | |
| 349 return false; | |
| 350 } | |
| 351 | |
| 352 bool BufferedResourceHandler::SelectNextHandler(bool* defer) { | 300 bool BufferedResourceHandler::SelectNextHandler(bool* defer) { |
| 353 DCHECK(!response_->head.mime_type.empty()); | 301 DCHECK(!response_->head.mime_type.empty()); |
| 354 | 302 |
| 355 ResourceRequestInfoImpl* info = GetRequestInfo(); | 303 ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 356 const std::string& mime_type = response_->head.mime_type; | 304 const std::string& mime_type = response_->head.mime_type; |
| 357 | 305 |
| 358 if (net::IsSupportedCertificateMimeType(mime_type)) { | 306 if (net::IsSupportedCertificateMimeType(mime_type)) { |
| 359 // Install certificate file. | 307 // Install certificate file. |
| 360 info->set_is_download(true); | 308 info->set_is_download(true); |
| 361 scoped_ptr<ResourceHandler> handler( | 309 scoped_ptr<ResourceHandler> handler( |
| 362 new CertificateResourceHandler(request())); | 310 new CertificateResourceHandler(request())); |
| 363 return UseAlternateNextHandler(handler.Pass(), std::string()); | 311 return UseAlternateNextHandler(handler.Pass(), std::string()); |
| 364 } | 312 } |
| 365 | 313 |
| 366 // Allow requests for object/embed tags to be intercepted as streams. | 314 // Allow requests for object/embed tags to be intercepted as streams. |
| 367 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { | 315 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { |
| 368 DCHECK(!info->allow_download()); | 316 DCHECK(!info->allow_download()); |
| 369 | 317 std::string payload; |
| 370 bool request_handled = true; | 318 scoped_ptr<ResourceHandler> handler( |
| 371 bool handled_by_plugin = IsHandledByPlugin(defer, &request_handled); | 319 host_->MaybeInterceptAsStream(request(), response_.get(), &payload)); |
| 372 if (!request_handled) | 320 if (handler) { |
| 373 return false; | 321 DCHECK(!net::IsSupportedMimeType(mime_type)); |
| 374 if (handled_by_plugin) | 322 return UseAlternateNextHandler(handler.Pass(), payload); |
| 375 return true; | 323 } |
| 376 } | 324 } |
| 377 | 325 |
| 378 if (!info->allow_download()) | 326 if (!info->allow_download()) |
| 379 return true; | 327 return true; |
| 380 | 328 |
| 381 // info->allow_download() == true implies | 329 // info->allow_download() == true implies |
| 382 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or | 330 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or |
| 383 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. | 331 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. |
| 384 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || | 332 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || |
| 385 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); | 333 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); |
| 386 | 334 |
| 387 bool must_download = MustDownload(); | 335 bool must_download = MustDownload(); |
| 388 if (!must_download) { | 336 if (!must_download) { |
| 389 if (net::IsSupportedMimeType(mime_type)) | 337 if (net::IsSupportedMimeType(mime_type)) |
| 390 return true; | 338 return true; |
| 391 | 339 |
| 392 bool request_handled = true; | 340 std::string payload; |
| 393 bool handled_by_plugin = IsHandledByPlugin(defer, &request_handled); | 341 scoped_ptr<ResourceHandler> handler( |
| 394 if (!request_handled) | 342 host_->MaybeInterceptAsStream(request(), response_.get(), &payload)); |
| 395 return false; | 343 if (handler) { |
| 396 if (handled_by_plugin) | 344 return UseAlternateNextHandler(handler.Pass(), payload); |
| 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; |
| 397 return true; | 357 return true; |
| 358 } |
| 359 if (has_plugin) |
| 360 return true; |
| 361 #endif |
| 398 } | 362 } |
| 399 | 363 |
| 400 // Install download handler | 364 // Install download handler |
| 401 info->set_is_download(true); | 365 info->set_is_download(true); |
| 402 scoped_ptr<ResourceHandler> handler( | 366 scoped_ptr<ResourceHandler> handler( |
| 403 host_->CreateResourceHandlerForDownload( | 367 host_->CreateResourceHandlerForDownload( |
| 404 request(), | 368 request(), |
| 405 true, // is_content_initiated | 369 true, // is_content_initiated |
| 406 must_download, | 370 must_download, |
| 407 DownloadItem::kInvalidId, | 371 DownloadItem::kInvalidId, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 host_->delegate()->ShouldForceDownloadResource( | 468 host_->delegate()->ShouldForceDownloadResource( |
| 505 request()->url(), response_->head.mime_type)) { | 469 request()->url(), response_->head.mime_type)) { |
| 506 must_download_ = true; | 470 must_download_ = true; |
| 507 } else { | 471 } else { |
| 508 must_download_ = false; | 472 must_download_ = false; |
| 509 } | 473 } |
| 510 | 474 |
| 511 return must_download_; | 475 return must_download_; |
| 512 } | 476 } |
| 513 | 477 |
| 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 |
| 514 bool BufferedResourceHandler::CopyReadBufferToNextHandler() { | 495 bool BufferedResourceHandler::CopyReadBufferToNextHandler() { |
| 515 if (!read_buffer_.get()) | 496 if (!read_buffer_.get()) |
| 516 return true; | 497 return true; |
| 517 | 498 |
| 518 scoped_refptr<net::IOBuffer> buf; | 499 scoped_refptr<net::IOBuffer> buf; |
| 519 int buf_len = 0; | 500 int buf_len = 0; |
| 520 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) | 501 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) |
| 521 return false; | 502 return false; |
| 522 | 503 |
| 523 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); | 504 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
| 524 memcpy(buf->data(), read_buffer_->data(), bytes_read_); | 505 memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
| 525 return true; | 506 return true; |
| 526 } | 507 } |
| 527 | 508 |
| 528 void BufferedResourceHandler::OnPluginsLoaded( | 509 void BufferedResourceHandler::OnPluginsLoaded( |
| 529 const std::vector<WebPluginInfo>& plugins) { | 510 const std::vector<WebPluginInfo>& plugins) { |
| 530 request()->LogUnblocked(); | 511 request()->LogUnblocked(); |
| 531 bool defer = false; | 512 bool defer = false; |
| 532 if (!ProcessResponse(&defer)) { | 513 if (!ProcessResponse(&defer)) { |
| 533 controller()->Cancel(); | 514 controller()->Cancel(); |
| 534 } else if (!defer) { | 515 } else if (!defer) { |
| 535 controller()->Resume(); | 516 controller()->Resume(); |
| 536 } | 517 } |
| 537 } | 518 } |
| 538 | 519 |
| 539 } // namespace content | 520 } // namespace content |
| OLD | NEW |