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