Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: third_party/WebKit/Source/platform/loader/fetch/ResourceFetcher.cpp

Issue 2827993002: ResourceFetcher: Refactor ResourceForStaticData().
Patch Set: ResourceFetcher: Refactor ResourceForStaticData(). Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/loader/fetch/ResourceFetcher.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All 5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
6 rights reserved. 6 rights reserved.
7 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ 7 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
8 8
9 This library is free software; you can redistribute it and/or 9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public 10 modify it under the terms of the GNU Library General Public
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 Context().DispatchDidFinishLoading( 359 Context().DispatchDidFinishLoading(
360 identifier, 0, 0, resource->GetResponse().DecodedBodyLength()); 360 identifier, 0, 0, resource->GetResponse().DecodedBodyLength());
361 } 361 }
362 362
363 static std::unique_ptr<TracedValue> UrlForTraceEvent(const KURL& url) { 363 static std::unique_ptr<TracedValue> UrlForTraceEvent(const KURL& url) {
364 std::unique_ptr<TracedValue> value = TracedValue::Create(); 364 std::unique_ptr<TracedValue> value = TracedValue::Create();
365 value->SetString("url", url.GetString()); 365 value->SetString("url", url.GetString());
366 return value; 366 return value;
367 } 367 }
368 368
369 Resource* ResourceFetcher::ResourceForStaticData( 369 Resource* ResourceFetcher::ResourceForStaticDataFromCache(
370 const FetchParameters& params, 370 const FetchParameters& request) {
371 if (Resource* old_resource = GetMemoryCache()->ResourceForURL(
372 request.GetResourceRequest().Url(), GetCacheIdentifier())) {
373 // There's no reason to re-parse if we saved the data from the previous
374 // parse.
375 if (request.Options().data_buffering_policy != kDoNotBufferData)
376 return old_resource;
377 GetMemoryCache()->Remove(old_resource);
378 }
379 return nullptr;
380 }
381
382 Resource* ResourceFetcher::ResourceForSubstituteData(
383 const FetchParameters& request,
371 const ResourceFactory& factory, 384 const ResourceFactory& factory,
372 const SubstituteData& substitute_data) { 385 const SubstituteData& substitute_data) {
373 const KURL& url = params.GetResourceRequest().Url(); 386 DCHECK(substitute_data.IsValid());
374 DCHECK(url.ProtocolIsData() || substitute_data.IsValid() || archive_); 387 // TODO(arthursonzogni): should we use the cache? Even with substitute data?
388 if (Resource* resource = ResourceForStaticDataFromCache(request)) {
389 return resource;
390 }
391
392 ResourceResponse response;
393 RefPtr<SharedBuffer> data;
394 data = substitute_data.Content();
395 response.SetURL(request.GetResourceRequest().Url());
396 response.SetMimeType(substitute_data.MimeType());
397 response.SetExpectedContentLength(data->size());
398 response.SetTextEncodingName(substitute_data.TextEncoding());
399
400 return CreateResourceFromStaticData(request, factory, response, data,
401 substitute_data);
402 }
403
404 Resource* ResourceFetcher::ResourceForDataUrl(
405 const FetchParameters& request,
406 const ResourceFactory& factory,
407 const SubstituteData& substitute_data) {
408 const KURL& url = request.GetResourceRequest().Url();
409 DCHECK(url.ProtocolIsData());
410 if (Resource* resource = ResourceForStaticDataFromCache(request)) {
411 return resource;
412 }
375 413
376 // TODO(japhet): We only send main resource data: urls through WebURLLoader 414 // TODO(japhet): We only send main resource data: urls through WebURLLoader
377 // for the benefit of a service worker test 415 // for the benefit of a service worker test
378 // (RenderViewImplTest.ServiceWorkerNetworkProviderSetup), which is at a layer 416 // (RenderViewImplTest.ServiceWorkerNetworkProviderSetup), which is at a layer
379 // where it isn't easy to mock out a network load. It uses data: urls to 417 // where it isn't easy to mock out a network load. It uses data: urls to
380 // emulate the behavior it wants to test, which would otherwise be reserved 418 // emulate the behavior it wants to test, which would otherwise be reserved
381 // for network loads. 419 // for network loads.
382 if (!archive_ && !substitute_data.IsValid() && 420 if (factory.GetType() == Resource::kMainResource ||
383 (factory.GetType() == Resource::kMainResource || 421 factory.GetType() == Resource::kRaw) {
384 factory.GetType() == Resource::kRaw))
385 return nullptr; 422 return nullptr;
386
387 const String cache_identifier = GetCacheIdentifier();
388 if (Resource* old_resource =
389 GetMemoryCache()->ResourceForURL(url, cache_identifier)) {
390 // There's no reason to re-parse if we saved the data from the previous
391 // parse.
392 if (params.Options().data_buffering_policy != kDoNotBufferData)
393 return old_resource;
394 GetMemoryCache()->Remove(old_resource);
395 } 423 }
396 424
397 ResourceResponse response; 425 ResourceResponse response;
398 RefPtr<SharedBuffer> data; 426 if (RefPtr<SharedBuffer> data =
399 if (substitute_data.IsValid()) { 427 NetworkUtils::ParseDataURLAndPopulateResponse(url, response)) {
400 data = substitute_data.Content();
401 response.SetURL(url);
402 response.SetMimeType(substitute_data.MimeType());
403 response.SetExpectedContentLength(data->size());
404 response.SetTextEncodingName(substitute_data.TextEncoding());
405 } else if (url.ProtocolIsData()) {
406 data = NetworkUtils::ParseDataURLAndPopulateResponse(url, response);
407 if (!data)
408 return nullptr;
409 // |response| is modified by parseDataURLAndPopulateResponse() and is 428 // |response| is modified by parseDataURLAndPopulateResponse() and is
410 // ready to be used. 429 // ready to be used.
411 } else { 430 Resource* resource = CreateResourceFromStaticData(
412 ArchiveResource* archive_resource = 431 request, factory, response, data, substitute_data);
413 archive_->SubresourceForURL(params.Url()); 432
414 // Fall back to the network if the archive doesn't contain the resource. 433 GetMemoryCache()->Add(resource);
415 if (!archive_resource) 434 return resource;
416 return nullptr; 435 }
417 data = archive_resource->Data(); 436
437 return nullptr;
438 }
439
440 Resource* ResourceFetcher::ResourceForArchive(
441 const FetchParameters& request,
442 const ResourceFactory& factory,
443 const SubstituteData& substitute_data) {
444 const KURL& url = request.GetResourceRequest().Url();
445 DCHECK(archive_);
446 if (Resource* resource = ResourceForStaticDataFromCache(request)) {
447 return resource;
448 }
449
450 if (ArchiveResource* archive_resource =
451 archive_->SubresourceForURL(request.Url())) {
452 RefPtr<SharedBuffer> data = archive_resource->Data();
453 ResourceResponse response;
418 response.SetURL(url); 454 response.SetURL(url);
419 response.SetMimeType(archive_resource->MimeType()); 455 response.SetMimeType(archive_resource->MimeType());
420 response.SetExpectedContentLength(data->size()); 456 response.SetExpectedContentLength(data->size());
421 response.SetTextEncodingName(archive_resource->TextEncoding()); 457 response.SetTextEncodingName(archive_resource->TextEncoding());
458
459 Resource* resource = CreateResourceFromStaticData(
460 request, factory, response, data, substitute_data);
461
462 GetMemoryCache()->Add(resource);
463 return resource;
422 } 464 }
423 465
424 Resource* resource = factory.Create(params.GetResourceRequest(), 466 return nullptr;
425 params.Options(), params.Charset()); 467 }
468
469 Resource* ResourceFetcher::CreateResourceFromStaticData(
470 const FetchParameters& request,
471 const ResourceFactory& factory,
472 const ResourceResponse& response,
473 const RefPtr<SharedBuffer>& data,
474 const SubstituteData& substitute_data) {
475 Resource* resource = factory.Create(request.GetResourceRequest(),
476 request.Options(), request.Charset());
426 resource->SetNeedsSynchronousCacheHit(substitute_data.ForceSynchronousLoad()); 477 resource->SetNeedsSynchronousCacheHit(substitute_data.ForceSynchronousLoad());
427 // FIXME: We should provide a body stream here. 478 // FIXME: We should provide a body stream here.
428 resource->ResponseReceived(response, nullptr); 479 resource->ResponseReceived(response, nullptr);
429 resource->SetDataBufferingPolicy(kBufferData); 480 resource->SetDataBufferingPolicy(kBufferData);
430 if (data->size()) 481 if (data->size())
431 resource->SetResourceBuffer(data); 482 resource->SetResourceBuffer(data);
432 resource->SetIdentifier(CreateUniqueIdentifier()); 483 resource->SetIdentifier(CreateUniqueIdentifier());
433 resource->SetCacheIdentifier(cache_identifier); 484 resource->SetCacheIdentifier(GetCacheIdentifier());
434 resource->Finish(); 485 resource->Finish();
435 486
436 if (!substitute_data.IsValid())
437 GetMemoryCache()->Add(resource);
438
439 return resource; 487 return resource;
440 } 488 }
441 489
442 Resource* ResourceFetcher::ResourceForBlockedRequest( 490 Resource* ResourceFetcher::ResourceForBlockedRequest(
443 const FetchParameters& params, 491 const FetchParameters& params,
444 const ResourceFactory& factory, 492 const ResourceFactory& factory,
445 ResourceRequestBlockedReason blocked_reason) { 493 ResourceRequestBlockedReason blocked_reason) {
446 Resource* resource = factory.Create(params.GetResourceRequest(), 494 Resource* resource = factory.Create(params.GetResourceRequest(),
447 params.Options(), params.Charset()); 495 params.Options(), params.Charset());
448 resource->GetError(ResourceError::CancelledDueToAccessCheckError( 496 resource->GetError(ResourceError::CancelledDueToAccessCheckError(
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 const SubstituteData& substitute_data) { 597 const SubstituteData& substitute_data) {
550 unsigned long identifier = CreateUniqueIdentifier(); 598 unsigned long identifier = CreateUniqueIdentifier();
551 ResourceRequest& resource_request = params.MutableResourceRequest(); 599 ResourceRequest& resource_request = params.MutableResourceRequest();
552 network_instrumentation::ScopedResourceLoadTracker 600 network_instrumentation::ScopedResourceLoadTracker
553 scoped_resource_load_tracker(identifier, resource_request); 601 scoped_resource_load_tracker(identifier, resource_request);
554 SCOPED_BLINK_UMA_HISTOGRAM_TIMER("Blink.Fetch.RequestResourceTime"); 602 SCOPED_BLINK_UMA_HISTOGRAM_TIMER("Blink.Fetch.RequestResourceTime");
555 // TODO(dproy): Remove this. http://crbug.com/659666 603 // TODO(dproy): Remove this. http://crbug.com/659666
556 TRACE_EVENT1("blink", "ResourceFetcher::requestResource", "url", 604 TRACE_EVENT1("blink", "ResourceFetcher::requestResource", "url",
557 UrlForTraceEvent(params.Url())); 605 UrlForTraceEvent(params.Url()));
558 606
559 Resource* resource = nullptr;
560 ResourceRequestBlockedReason blocked_reason = 607 ResourceRequestBlockedReason blocked_reason =
561 ResourceRequestBlockedReason::kNone; 608 ResourceRequestBlockedReason::kNone;
562 609
563 PrepareRequestResult result = PrepareRequest(params, factory, substitute_data, 610 PrepareRequestResult result = PrepareRequest(params, factory, substitute_data,
564 identifier, blocked_reason); 611 identifier, blocked_reason);
565 if (result == kAbort) 612 if (result == kAbort)
566 return nullptr; 613 return nullptr;
567 if (result == kBlock) 614 if (result == kBlock)
568 return ResourceForBlockedRequest(params, factory, blocked_reason); 615 return ResourceForBlockedRequest(params, factory, blocked_reason);
569 616
570 if (!params.IsSpeculativePreload()) { 617 if (!params.IsSpeculativePreload()) {
571 // Only log if it's not for speculative preload. 618 // Only log if it's not for speculative preload.
572 Context().RecordLoadingActivity(identifier, resource_request, 619 Context().RecordLoadingActivity(identifier, resource_request,
573 factory.GetType(), 620 factory.GetType(),
574 params.Options().initiator_info.name); 621 params.Options().initiator_info.name);
575 } 622 }
576 623
624 Resource* resource = nullptr;
577 bool is_data_url = resource_request.Url().ProtocolIsData(); 625 bool is_data_url = resource_request.Url().ProtocolIsData();
578 bool is_static_data = is_data_url || substitute_data.IsValid() || archive_; 626 bool is_static_data = is_data_url || substitute_data.IsValid() || archive_;
579 if (is_static_data) { 627
580 resource = ResourceForStaticData(params, factory, substitute_data); 628 if (substitute_data.IsValid()) {
581 // Abort the request if the archive doesn't contain the resource, except in 629 resource = ResourceForSubstituteData(params, factory, substitute_data);
582 // the case of data URLs which might have resources such as fonts that need 630 } else if (is_data_url) {
583 // to be decoded only on demand. These data URLs are allowed to be 631 resource = ResourceForDataUrl(params, factory, substitute_data);
632 // In the some cases, the data URLs might have resources such as fonts that
633 // need to be decoded only on demand. These data URLs are allowed to be
584 // processed using the normal ResourceFetcher machinery. 634 // processed using the normal ResourceFetcher machinery.
585 if (!resource && !is_data_url && archive_) 635 } else if (archive_) {
636 resource = ResourceForArchive(params, factory, substitute_data);
637 // TODO(arthursonzogni): In past version of ResourceForStaticData, when
638 // there is an |archive_| but the resource is not found. It is stated:
639 // "Fall back to the network if the archive doesn't contain the resource.",
640 // but then the caller abort the request whatever happens:
641 // "Abort the request if the archive doesn't contain the resource".
642 // It is contradictory.
643 if (!resource)
586 return nullptr; 644 return nullptr;
587 } 645 }
646
588 if (!resource) { 647 if (!resource) {
589 resource = 648 resource =
590 GetMemoryCache()->ResourceForURL(params.Url(), GetCacheIdentifier()); 649 GetMemoryCache()->ResourceForURL(params.Url(), GetCacheIdentifier());
591 } 650 }
592 651
593 // If we got a preloaded resource from the cache for a non-preload request, 652 // If we got a preloaded resource from the cache for a non-preload request,
594 // we may need to make it block the onload event. 653 // we may need to make it block the onload event.
595 MakePreloadedResourceBlockOnloadIfNeeded(resource, params); 654 MakePreloadedResourceBlockOnloadIfNeeded(resource, params);
596 655
597 const RevalidationPolicy policy = DetermineRevalidationPolicy( 656 const RevalidationPolicy policy = DetermineRevalidationPolicy(
(...skipping 979 matching lines...) Expand 10 before | Expand all | Expand 10 after
1577 visitor->Trace(context_); 1636 visitor->Trace(context_);
1578 visitor->Trace(archive_); 1637 visitor->Trace(archive_);
1579 visitor->Trace(loaders_); 1638 visitor->Trace(loaders_);
1580 visitor->Trace(non_blocking_loaders_); 1639 visitor->Trace(non_blocking_loaders_);
1581 visitor->Trace(document_resources_); 1640 visitor->Trace(document_resources_);
1582 visitor->Trace(preloads_); 1641 visitor->Trace(preloads_);
1583 visitor->Trace(resource_timing_info_map_); 1642 visitor->Trace(resource_timing_info_map_);
1584 } 1643 }
1585 1644
1586 } // namespace blink 1645 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/loader/fetch/ResourceFetcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698