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

Side by Side Diff: third_party/WebKit/Source/core/fetch/Resource.cpp

Issue 2656443003: Make the uses of Resource::m_resourceRequest/m_response const if applicable (Closed)
Patch Set: Rebase Created 3 years, 11 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/core/fetch/Resource.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) 2006 Samuel Weinig (sam.weinig@gmail.com) 5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All 6 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
7 rights reserved. 7 rights reserved.
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 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 m_integrityDisposition(ResourceIntegrityDisposition::NotChecked), 331 m_integrityDisposition(ResourceIntegrityDisposition::NotChecked),
332 m_options(options), 332 m_options(options),
333 m_responseTimestamp(currentTime()), 333 m_responseTimestamp(currentTime()),
334 m_cancelTimer(Platform::current()->mainThread()->getWebTaskRunner(), 334 m_cancelTimer(Platform::current()->mainThread()->getWebTaskRunner(),
335 this, 335 this,
336 &Resource::cancelTimerFired), 336 &Resource::cancelTimerFired),
337 m_resourceRequest(request) { 337 m_resourceRequest(request) {
338 InstanceCounters::incrementCounter(InstanceCounters::ResourceCounter); 338 InstanceCounters::incrementCounter(InstanceCounters::ResourceCounter);
339 339
340 // Currently we support the metadata caching only for HTTP family. 340 // Currently we support the metadata caching only for HTTP family.
341 if (m_resourceRequest.url().protocolIsInHTTPFamily()) 341 if (resourceRequest().url().protocolIsInHTTPFamily())
342 m_cacheHandler = CachedMetadataHandlerImpl::create(this); 342 m_cacheHandler = CachedMetadataHandlerImpl::create(this);
343 MemoryCoordinator::instance().registerClient(this); 343 MemoryCoordinator::instance().registerClient(this);
344 } 344 }
345 345
346 Resource::~Resource() { 346 Resource::~Resource() {
347 InstanceCounters::decrementCounter(InstanceCounters::ResourceCounter); 347 InstanceCounters::decrementCounter(InstanceCounters::ResourceCounter);
348 } 348 }
349 349
350 DEFINE_TRACE(Resource) { 350 DEFINE_TRACE(Resource) {
351 visitor->trace(m_loader); 351 visitor->trace(m_loader);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 DCHECK(!m_isRevalidating); 433 DCHECK(!m_isRevalidating);
434 m_loadFinishTime = loadFinishTime; 434 m_loadFinishTime = loadFinishTime;
435 if (!errorOccurred()) 435 if (!errorOccurred())
436 m_status = Cached; 436 m_status = Cached;
437 m_loader = nullptr; 437 m_loader = nullptr;
438 checkNotify(); 438 checkNotify();
439 } 439 }
440 440
441 AtomicString Resource::httpContentType() const { 441 AtomicString Resource::httpContentType() const {
442 return extractMIMETypeFromMediaType( 442 return extractMIMETypeFromMediaType(
443 m_response.httpHeaderField(HTTPNames::Content_Type).lower()); 443 response().httpHeaderField(HTTPNames::Content_Type).lower());
444 } 444 }
445 445
446 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin) const { 446 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin) const {
447 StoredCredentials storedCredentials = 447 StoredCredentials storedCredentials =
448 lastResourceRequest().allowStoredCredentials() 448 lastResourceRequest().allowStoredCredentials()
449 ? AllowStoredCredentials 449 ? AllowStoredCredentials
450 : DoNotAllowStoredCredentials; 450 : DoNotAllowStoredCredentials;
451 CrossOriginAccessControl::AccessStatus status = 451 CrossOriginAccessControl::AccessStatus status =
452 CrossOriginAccessControl::checkAccess(m_response, storedCredentials, 452 CrossOriginAccessControl::checkAccess(response(), storedCredentials,
453 securityOrigin); 453 securityOrigin);
454 454
455 return status == CrossOriginAccessControl::kAccessAllowed; 455 return status == CrossOriginAccessControl::kAccessAllowed;
456 } 456 }
457 457
458 bool Resource::isEligibleForIntegrityCheck( 458 bool Resource::isEligibleForIntegrityCheck(
459 SecurityOrigin* securityOrigin) const { 459 SecurityOrigin* securityOrigin) const {
460 return securityOrigin->canRequest(resourceRequest().url()) || 460 return securityOrigin->canRequest(resourceRequest().url()) ||
461 passesAccessControlCheck(securityOrigin); 461 passesAccessControlCheck(securityOrigin);
462 } 462 }
(...skipping 23 matching lines...) Expand all
486 ? std::max(0., responseTimestamp - dateValue) 486 ? std::max(0., responseTimestamp - dateValue)
487 : 0; 487 : 0;
488 double ageValue = response.age(); 488 double ageValue = response.age();
489 double correctedReceivedAge = 489 double correctedReceivedAge =
490 std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge; 490 std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
491 double residentTime = currentTime() - responseTimestamp; 491 double residentTime = currentTime() - responseTimestamp;
492 return correctedReceivedAge + residentTime; 492 return correctedReceivedAge + residentTime;
493 } 493 }
494 494
495 double Resource::currentAge() const { 495 double Resource::currentAge() const {
496 return blink::currentAge(m_response, m_responseTimestamp); 496 return blink::currentAge(response(), m_responseTimestamp);
497 } 497 }
498 498
499 static double freshnessLifetime(ResourceResponse& response, 499 static double freshnessLifetime(const ResourceResponse& response,
500 double responseTimestamp) { 500 double responseTimestamp) {
501 #if !OS(ANDROID) 501 #if !OS(ANDROID)
502 // On desktop, local files should be reloaded in case they change. 502 // On desktop, local files should be reloaded in case they change.
503 if (response.url().isLocalFile()) 503 if (response.url().isLocalFile())
504 return 0; 504 return 0;
505 #endif 505 #endif
506 506
507 // Cache other non-http / non-filesystem resources liberally. 507 // Cache other non-http / non-filesystem resources liberally.
508 if (!response.url().protocolIsInHTTPFamily() && 508 if (!response.url().protocolIsInHTTPFamily() &&
509 !response.url().protocolIs("filesystem")) 509 !response.url().protocolIs("filesystem"))
(...skipping 10 matching lines...) Expand all
520 if (std::isfinite(expiresValue)) 520 if (std::isfinite(expiresValue))
521 return expiresValue - creationTime; 521 return expiresValue - creationTime;
522 double lastModifiedValue = response.lastModified(); 522 double lastModifiedValue = response.lastModified();
523 if (std::isfinite(lastModifiedValue)) 523 if (std::isfinite(lastModifiedValue))
524 return (creationTime - lastModifiedValue) * 0.1; 524 return (creationTime - lastModifiedValue) * 0.1;
525 // If no cache headers are present, the specification leaves the decision to 525 // If no cache headers are present, the specification leaves the decision to
526 // the UA. Other browsers seem to opt for 0. 526 // the UA. Other browsers seem to opt for 0.
527 return 0; 527 return 0;
528 } 528 }
529 529
530 double Resource::freshnessLifetime() { 530 double Resource::freshnessLifetime() const {
531 return blink::freshnessLifetime(m_response, m_responseTimestamp); 531 return blink::freshnessLifetime(response(), m_responseTimestamp);
532 } 532 }
533 533
534 double Resource::stalenessLifetime() { 534 double Resource::stalenessLifetime() const {
535 return m_response.cacheControlStaleWhileRevalidate(); 535 return response().cacheControlStaleWhileRevalidate();
536 } 536 }
537 537
538 static bool canUseResponse(ResourceResponse& response, 538 static bool canUseResponse(const ResourceResponse& response,
539 double responseTimestamp) { 539 double responseTimestamp) {
540 if (response.isNull()) 540 if (response.isNull())
541 return false; 541 return false;
542 542
543 // FIXME: Why isn't must-revalidate considered a reason we can't use the 543 // FIXME: Why isn't must-revalidate considered a reason we can't use the
544 // response? 544 // response?
545 if (response.cacheControlContainsNoCache() || 545 if (response.cacheControlContainsNoCache() ||
546 response.cacheControlContainsNoStore()) 546 response.cacheControlContainsNoStore())
547 return false; 547 return false;
548 548
(...skipping 10 matching lines...) Expand all
559 if (!hasMaxAge && !hasExpires) 559 if (!hasMaxAge && !hasExpires)
560 return false; 560 return false;
561 } 561 }
562 562
563 return currentAge(response, responseTimestamp) <= 563 return currentAge(response, responseTimestamp) <=
564 freshnessLifetime(response, responseTimestamp); 564 freshnessLifetime(response, responseTimestamp);
565 } 565 }
566 566
567 const ResourceRequest& Resource::lastResourceRequest() const { 567 const ResourceRequest& Resource::lastResourceRequest() const {
568 if (!m_redirectChain.size()) 568 if (!m_redirectChain.size())
569 return m_resourceRequest; 569 return resourceRequest();
570 return m_redirectChain.back().m_request; 570 return m_redirectChain.back().m_request;
571 } 571 }
572 572
573 void Resource::setRevalidatingRequest(const ResourceRequest& request) { 573 void Resource::setRevalidatingRequest(const ResourceRequest& request) {
574 SECURITY_CHECK(m_redirectChain.isEmpty()); 574 SECURITY_CHECK(m_redirectChain.isEmpty());
575 DCHECK(!request.isNull()); 575 DCHECK(!request.isNull());
576 CHECK(!m_isRevalidationStartForbidden); 576 CHECK(!m_isRevalidationStartForbidden);
577 m_isRevalidating = true; 577 m_isRevalidating = true;
578 m_resourceRequest = request; 578 m_resourceRequest = request;
579 m_status = NotStarted; 579 m_status = NotStarted;
580 } 580 }
581 581
582 bool Resource::willFollowRedirect(const ResourceRequest& newRequest, 582 bool Resource::willFollowRedirect(const ResourceRequest& newRequest,
583 const ResourceResponse& redirectResponse) { 583 const ResourceResponse& redirectResponse) {
584 if (m_isRevalidating) 584 if (m_isRevalidating)
585 revalidationFailed(); 585 revalidationFailed();
586 m_redirectChain.push_back(RedirectPair(newRequest, redirectResponse)); 586 m_redirectChain.push_back(RedirectPair(newRequest, redirectResponse));
587 return true; 587 return true;
588 } 588 }
589 589
590 void Resource::setResponse(const ResourceResponse& response) { 590 void Resource::setResponse(const ResourceResponse& response) {
591 m_response = response; 591 m_response = response;
592 if (m_response.wasFetchedViaServiceWorker()) { 592 if (this->response().wasFetchedViaServiceWorker()) {
593 m_cacheHandler = ServiceWorkerResponseCachedMetadataHandler::create( 593 m_cacheHandler = ServiceWorkerResponseCachedMetadataHandler::create(
594 this, m_fetcherSecurityOrigin.get()); 594 this, m_fetcherSecurityOrigin.get());
595 } 595 }
596 } 596 }
597 597
598 void Resource::responseReceived(const ResourceResponse& response, 598 void Resource::responseReceived(const ResourceResponse& response,
599 std::unique_ptr<WebDataConsumerHandle>) { 599 std::unique_ptr<WebDataConsumerHandle>) {
600 m_responseTimestamp = currentTime(); 600 m_responseTimestamp = currentTime();
601 if (m_preloadDiscoveryTime) { 601 if (m_preloadDiscoveryTime) {
602 int timeSinceDiscovery = static_cast<int>( 602 int timeSinceDiscovery = static_cast<int>(
(...skipping 12 matching lines...) Expand all
615 revalidationFailed(); 615 revalidationFailed();
616 } 616 }
617 setResponse(response); 617 setResponse(response);
618 String encoding = response.textEncodingName(); 618 String encoding = response.textEncodingName();
619 if (!encoding.isNull()) 619 if (!encoding.isNull())
620 setEncoding(encoding); 620 setEncoding(encoding);
621 } 621 }
622 622
623 void Resource::setSerializedCachedMetadata(const char* data, size_t size) { 623 void Resource::setSerializedCachedMetadata(const char* data, size_t size) {
624 DCHECK(!m_isRevalidating); 624 DCHECK(!m_isRevalidating);
625 DCHECK(!m_response.isNull()); 625 DCHECK(!response().isNull());
626 if (m_cacheHandler) 626 if (m_cacheHandler)
627 m_cacheHandler->setSerializedCachedMetadata(data, size); 627 m_cacheHandler->setSerializedCachedMetadata(data, size);
628 } 628 }
629 629
630 CachedMetadataHandler* Resource::cacheHandler() { 630 CachedMetadataHandler* Resource::cacheHandler() {
631 return m_cacheHandler.get(); 631 return m_cacheHandler.get();
632 } 632 }
633 633
634 String Resource::reasonNotDeletable() const { 634 String Resource::reasonNotDeletable() const {
635 StringBuilder builder; 635 StringBuilder builder;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 722
723 willAddClientOrObserver(policy); 723 willAddClientOrObserver(policy);
724 724
725 if (m_isRevalidating) { 725 if (m_isRevalidating) {
726 m_clients.add(client); 726 m_clients.add(client);
727 return; 727 return;
728 } 728 }
729 729
730 // If an error has occurred or we have existing data to send to the new client 730 // If an error has occurred or we have existing data to send to the new client
731 // and the resource type supprts it, send it asynchronously. 731 // and the resource type supprts it, send it asynchronously.
732 if ((errorOccurred() || !m_response.isNull()) && 732 if ((errorOccurred() || !response().isNull()) &&
733 !typeNeedsSynchronousCacheHit(getType()) && !m_needsSynchronousCacheHit) { 733 !typeNeedsSynchronousCacheHit(getType()) && !m_needsSynchronousCacheHit) {
734 m_clientsAwaitingCallback.add(client); 734 m_clientsAwaitingCallback.add(client);
735 ResourceCallback::callbackHandler().schedule(this); 735 ResourceCallback::callbackHandler().schedule(this);
736 return; 736 return;
737 } 737 }
738 738
739 m_clients.add(client); 739 m_clients.add(client);
740 didAddClient(client); 740 didAddClient(client);
741 return; 741 return;
742 } 742 }
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 } 938 }
939 939
940 void Resource::clearRangeRequestHeader() { 940 void Resource::clearRangeRequestHeader() {
941 m_resourceRequest.clearHTTPHeaderField("range"); 941 m_resourceRequest.clearHTTPHeaderField("range");
942 } 942 }
943 943
944 void Resource::revalidationSucceeded( 944 void Resource::revalidationSucceeded(
945 const ResourceResponse& validatingResponse) { 945 const ResourceResponse& validatingResponse) {
946 SECURITY_CHECK(m_redirectChain.isEmpty()); 946 SECURITY_CHECK(m_redirectChain.isEmpty());
947 SECURITY_CHECK(equalIgnoringFragmentIdentifier(validatingResponse.url(), 947 SECURITY_CHECK(equalIgnoringFragmentIdentifier(validatingResponse.url(),
948 m_response.url())); 948 response().url()));
949 m_response.setResourceLoadTiming(validatingResponse.resourceLoadTiming()); 949 m_response.setResourceLoadTiming(validatingResponse.resourceLoadTiming());
950 950
951 // RFC2616 10.3.5 951 // RFC2616 10.3.5
952 // Update cached headers from the 304 response 952 // Update cached headers from the 304 response
953 const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields(); 953 const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields();
954 for (const auto& header : newHeaders) { 954 for (const auto& header : newHeaders) {
955 // Entity headers should not be sent by servers when generating a 304 955 // Entity headers should not be sent by servers when generating a 304
956 // response; misconfigured servers send them anyway. We shouldn't allow such 956 // response; misconfigured servers send them anyway. We shouldn't allow such
957 // headers to update the original request. We'll base this on the list 957 // headers to update the original request. We'll base this on the list
958 // defined by RFC2616 7.1, with a few additions for extension headers we 958 // defined by RFC2616 7.1, with a few additions for extension headers we
959 // care about. 959 // care about.
960 if (!shouldUpdateHeaderAfterRevalidation(header.key)) 960 if (!shouldUpdateHeaderAfterRevalidation(header.key))
961 continue; 961 continue;
962 m_response.setHTTPHeaderField(header.key, header.value); 962 m_response.setHTTPHeaderField(header.key, header.value);
963 } 963 }
964 964
965 m_isRevalidating = false; 965 m_isRevalidating = false;
966 } 966 }
967 967
968 void Resource::revalidationFailed() { 968 void Resource::revalidationFailed() {
969 SECURITY_CHECK(m_redirectChain.isEmpty()); 969 SECURITY_CHECK(m_redirectChain.isEmpty());
970 clearData(); 970 clearData();
971 m_cacheHandler.clear(); 971 m_cacheHandler.clear();
972 destroyDecodedDataForFailedRevalidation(); 972 destroyDecodedDataForFailedRevalidation();
973 m_isRevalidating = false; 973 m_isRevalidating = false;
974 } 974 }
975 975
976 bool Resource::canReuseRedirectChain() { 976 bool Resource::canReuseRedirectChain() const {
977 for (auto& redirect : m_redirectChain) { 977 for (auto& redirect : m_redirectChain) {
978 if (!canUseResponse(redirect.m_redirectResponse, m_responseTimestamp)) 978 if (!canUseResponse(redirect.m_redirectResponse, m_responseTimestamp))
979 return false; 979 return false;
980 if (redirect.m_request.cacheControlContainsNoCache() || 980 if (redirect.m_request.cacheControlContainsNoCache() ||
981 redirect.m_request.cacheControlContainsNoStore()) 981 redirect.m_request.cacheControlContainsNoStore())
982 return false; 982 return false;
983 } 983 }
984 return true; 984 return true;
985 } 985 }
986 986
987 bool Resource::hasCacheControlNoStoreHeader() const { 987 bool Resource::hasCacheControlNoStoreHeader() const {
988 return m_response.cacheControlContainsNoStore() || 988 return response().cacheControlContainsNoStore() ||
989 m_resourceRequest.cacheControlContainsNoStore(); 989 resourceRequest().cacheControlContainsNoStore();
990 } 990 }
991 991
992 bool Resource::hasVaryHeader() const { 992 bool Resource::hasVaryHeader() const {
993 return !m_response.httpHeaderField(HTTPNames::Vary).isNull(); 993 return !response().httpHeaderField(HTTPNames::Vary).isNull();
994 } 994 }
995 995
996 bool Resource::mustRevalidateDueToCacheHeaders() { 996 bool Resource::mustRevalidateDueToCacheHeaders() const {
997 return !canUseResponse(m_response, m_responseTimestamp) || 997 return !canUseResponse(response(), m_responseTimestamp) ||
998 m_resourceRequest.cacheControlContainsNoCache() || 998 resourceRequest().cacheControlContainsNoCache() ||
999 m_resourceRequest.cacheControlContainsNoStore(); 999 resourceRequest().cacheControlContainsNoStore();
1000 } 1000 }
1001 1001
1002 bool Resource::canUseCacheValidator() { 1002 bool Resource::canUseCacheValidator() const {
1003 if (isLoading() || errorOccurred()) 1003 if (isLoading() || errorOccurred())
1004 return false; 1004 return false;
1005 1005
1006 if (hasCacheControlNoStoreHeader()) 1006 if (hasCacheControlNoStoreHeader())
1007 return false; 1007 return false;
1008 1008
1009 // Do not revalidate Resource with redirects. https://crbug.com/613971 1009 // Do not revalidate Resource with redirects. https://crbug.com/613971
1010 if (!redirectChain().isEmpty()) 1010 if (!redirectChain().isEmpty())
1011 return false; 1011 return false;
1012 1012
1013 return m_response.hasCacheValidatorFields() || 1013 return response().hasCacheValidatorFields() ||
1014 m_resourceRequest.hasCacheValidatorFields(); 1014 resourceRequest().hasCacheValidatorFields();
1015 } 1015 }
1016 1016
1017 size_t Resource::calculateOverheadSize() const { 1017 size_t Resource::calculateOverheadSize() const {
1018 static const int kAverageClientsHashMapSize = 384; 1018 static const int kAverageClientsHashMapSize = 384;
1019 return sizeof(Resource) + m_response.memoryUsage() + 1019 return sizeof(Resource) + response().memoryUsage() +
1020 kAverageClientsHashMapSize + 1020 kAverageClientsHashMapSize +
1021 m_resourceRequest.url().getString().length() * 2; 1021 resourceRequest().url().getString().length() * 2;
1022 } 1022 }
1023 1023
1024 void Resource::didChangePriority(ResourceLoadPriority loadPriority, 1024 void Resource::didChangePriority(ResourceLoadPriority loadPriority,
1025 int intraPriorityValue) { 1025 int intraPriorityValue) {
1026 m_resourceRequest.setPriority(loadPriority, intraPriorityValue); 1026 m_resourceRequest.setPriority(loadPriority, intraPriorityValue);
1027 if (m_loader) 1027 if (m_loader)
1028 m_loader->didChangePriority(loadPriority, intraPriorityValue); 1028 m_loader->didChangePriority(loadPriority, intraPriorityValue);
1029 } 1029 }
1030 1030
1031 // TODO(toyoshim): Consider to generate automatically. https://crbug.com/675515. 1031 // TODO(toyoshim): Consider to generate automatically. https://crbug.com/675515.
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 case Resource::Media: 1115 case Resource::Media:
1116 case Resource::Manifest: 1116 case Resource::Manifest:
1117 case Resource::Mock: 1117 case Resource::Mock:
1118 return false; 1118 return false;
1119 } 1119 }
1120 NOTREACHED(); 1120 NOTREACHED();
1121 return false; 1121 return false;
1122 } 1122 }
1123 1123
1124 } // namespace blink 1124 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/Resource.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698