Chromium Code Reviews

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

Issue 1706083002: Split ImageResourceClient into ResourceClient and ImageResourceObserver [1/2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reflect comments and Rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 rights reserved. 6 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
7 7
8 This library is free software; you can redistribute it and/or 8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public 9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either 10 License as published by the Free Software Foundation; either
(...skipping 415 matching lines...)
426 } 426 }
427 427
428 bool Resource::unlock() 428 bool Resource::unlock()
429 { 429 {
430 if (!m_data) 430 if (!m_data)
431 return false; 431 return false;
432 432
433 if (!m_data->isLocked()) 433 if (!m_data->isLocked())
434 return true; 434 return true;
435 435
436 if (!memoryCache()->contains(this) || hasClients() || !m_revalidatingRequest .isNull() || !m_loadFinishTime || !isSafeToUnlock()) 436 if (!memoryCache()->contains(this) || hasClientsOrObservers() || !m_revalida tingRequest.isNull() || !m_loadFinishTime || !isSafeToUnlock())
437 return false; 437 return false;
438 438
439 m_data->unlock(); 439 m_data->unlock();
440 return true; 440 return true;
441 } 441 }
442 442
443 void Resource::responseReceived(const ResourceResponse& response, PassOwnPtr<Web DataConsumerHandle>) 443 void Resource::responseReceived(const ResourceResponse& response, PassOwnPtr<Web DataConsumerHandle>)
444 { 444 {
445 m_responseTimestamp = currentTime(); 445 m_responseTimestamp = currentTime();
446 446
(...skipping 59 matching lines...)
506 #if ENABLE(OILPAN) 506 #if ENABLE(OILPAN)
507 return this; 507 return this;
508 #else 508 #else
509 return m_weakPtrFactory.createWeakPtr(); 509 return m_weakPtrFactory.createWeakPtr();
510 #endif 510 #endif
511 } 511 }
512 512
513 String Resource::reasonNotDeletable() const 513 String Resource::reasonNotDeletable() const
514 { 514 {
515 StringBuilder builder; 515 StringBuilder builder;
516 if (hasClients()) { 516 if (hasClientsOrObservers()) {
517 builder.append("hasClients("); 517 builder.append("hasClients(");
518 builder.appendNumber(m_clients.size()); 518 builder.appendNumber(m_clients.size());
519 if (!m_clientsAwaitingCallback.isEmpty()) { 519 if (!m_clientsAwaitingCallback.isEmpty()) {
520 builder.append(", AwaitingCallback="); 520 builder.append(", AwaitingCallback=");
521 builder.appendNumber(m_clientsAwaitingCallback.size()); 521 builder.appendNumber(m_clientsAwaitingCallback.size());
522 } 522 }
523 if (!m_finishedClients.isEmpty()) { 523 if (!m_finishedClients.isEmpty()) {
524 builder.append(", Finished="); 524 builder.append(", Finished=");
525 builder.appendNumber(m_finishedClients.size()); 525 builder.appendNumber(m_finishedClients.size());
526 } 526 }
(...skipping 54 matching lines...)
581 return true; 581 return true;
582 if (type == Resource::CSSStyleSheet) 582 if (type == Resource::CSSStyleSheet)
583 return true; 583 return true;
584 if (type == Resource::Script) 584 if (type == Resource::Script)
585 return true; 585 return true;
586 if (type == Resource::Font) 586 if (type == Resource::Font)
587 return true; 587 return true;
588 return false; 588 return false;
589 } 589 }
590 590
591 void Resource::addClient(ResourceClient* client) 591 void Resource::willAddClientOrObserver()
592 { 592 {
593 ASSERT(!isPurgeable()); 593 ASSERT(!isPurgeable());
594
595 if (m_preloadResult == PreloadNotReferenced) { 594 if (m_preloadResult == PreloadNotReferenced) {
596 if (isLoaded()) 595 if (isLoaded())
597 m_preloadResult = PreloadReferencedWhileComplete; 596 m_preloadResult = PreloadReferencedWhileComplete;
598 else if (m_requestedFromNetworkingLayer) 597 else if (m_requestedFromNetworkingLayer)
599 m_preloadResult = PreloadReferencedWhileLoading; 598 m_preloadResult = PreloadReferencedWhileLoading;
600 else 599 else
601 m_preloadResult = PreloadReferenced; 600 m_preloadResult = PreloadReferenced;
602 } 601 }
603 if (!hasClients()) 602 if (!hasClientsOrObservers())
604 memoryCache()->makeLive(this); 603 memoryCache()->makeLive(this);
604 }
605
606 void Resource::addClient(ResourceClient* client)
607 {
608 willAddClientOrObserver();
605 609
606 if (!m_revalidatingRequest.isNull()) { 610 if (!m_revalidatingRequest.isNull()) {
607 m_clients.add(client); 611 m_clients.add(client);
608 return; 612 return;
609 } 613 }
610 614
611 // If we have existing data to send to the new client and the resource type supprts it, send it asynchronously. 615 // If we have existing data to send to the new client and the resource type supprts it, send it asynchronously.
612 if (!m_response.isNull() && !shouldSendCachedDataSynchronouslyForType(getTyp e()) && !m_needsSynchronousCacheHit) { 616 if (!m_response.isNull() && !shouldSendCachedDataSynchronouslyForType(getTyp e()) && !m_needsSynchronousCacheHit) {
613 m_clientsAwaitingCallback.add(client); 617 m_clientsAwaitingCallback.add(client);
614 ResourceCallback::callbackHandler()->schedule(this); 618 ResourceCallback::callbackHandler()->schedule(this);
615 return; 619 return;
616 } 620 }
617 621
618 m_clients.add(client); 622 m_clients.add(client);
619 didAddClient(client); 623 didAddClient(client);
620 return; 624 return;
621 } 625 }
622 626
623 void Resource::removeClient(ResourceClient* client) 627 void Resource::removeClient(ResourceClient* client)
624 { 628 {
625 ASSERT(hasClient(client)); 629 ASSERT(hasClient(client));
626 if (m_finishedClients.contains(client)) 630 if (m_finishedClients.contains(client))
627 m_finishedClients.remove(client); 631 m_finishedClients.remove(client);
628 else if (m_clientsAwaitingCallback.contains(client)) 632 else if (m_clientsAwaitingCallback.contains(client))
629 m_clientsAwaitingCallback.remove(client); 633 m_clientsAwaitingCallback.remove(client);
630 else 634 else
631 m_clients.remove(client); 635 m_clients.remove(client);
632 636
633 didRemoveClient(client);
634
635 if (m_clientsAwaitingCallback.isEmpty()) 637 if (m_clientsAwaitingCallback.isEmpty())
636 ResourceCallback::callbackHandler()->cancel(this); 638 ResourceCallback::callbackHandler()->cancel(this);
637 639
638 if (!hasClients()) { 640 didRemoveClientOrObserver();
641 // This object may be dead here.
642 }
643
644 void Resource::didRemoveClientOrObserver()
645 {
646 if (!hasClientsOrObservers()) {
639 RefPtrWillBeRawPtr<Resource> protect(this); 647 RefPtrWillBeRawPtr<Resource> protect(this);
640 memoryCache()->makeDead(this); 648 memoryCache()->makeDead(this);
641 allClientsRemoved(); 649 allClientsAndObserversRemoved();
642 650
643 // RFC2616 14.9.2: 651 // RFC2616 14.9.2:
644 // "no-store: ... MUST make a best-effort attempt to remove the informat ion from volatile storage as promptly as possible" 652 // "no-store: ... MUST make a best-effort attempt to remove the informat ion from volatile storage as promptly as possible"
645 // "... History buffers MAY store such responses as part of their normal operation." 653 // "... History buffers MAY store such responses as part of their normal operation."
646 // We allow non-secure content to be reused in history, but we do not al low secure content to be reused. 654 // We allow non-secure content to be reused in history, but we do not al low secure content to be reused.
647 if (hasCacheControlNoStoreHeader() && url().protocolIs("https")) { 655 if (hasCacheControlNoStoreHeader() && url().protocolIs("https")) {
648 memoryCache()->remove(this); 656 memoryCache()->remove(this);
649 memoryCache()->prune(); 657 memoryCache()->prune();
650 } else { 658 } else {
651 memoryCache()->prune(this); 659 memoryCache()->prune(this);
652 } 660 }
653 } 661 }
654 // This object may be dead here. 662 // This object may be dead here.
655 } 663 }
656 664
657 void Resource::allClientsRemoved() 665 void Resource::allClientsAndObserversRemoved()
658 { 666 {
659 if (!m_loader) 667 if (!m_loader)
660 return; 668 return;
661 if (m_type == MainResource || m_type == Raw || !memoryCache()->contains(this )) 669 if (m_type == MainResource || m_type == Raw || !memoryCache()->contains(this ))
662 cancelTimerFired(&m_cancelTimer); 670 cancelTimerFired(&m_cancelTimer);
663 else if (!m_cancelTimer.isActive()) 671 else if (!m_cancelTimer.isActive())
664 m_cancelTimer.startOneShot(0, BLINK_FROM_HERE); 672 m_cancelTimer.startOneShot(0, BLINK_FROM_HERE);
665 673
666 unlock(); 674 unlock();
667 } 675 }
668 676
669 void Resource::cancelTimerFired(Timer<Resource>* timer) 677 void Resource::cancelTimerFired(Timer<Resource>* timer)
670 { 678 {
671 ASSERT_UNUSED(timer, timer == &m_cancelTimer); 679 ASSERT_UNUSED(timer, timer == &m_cancelTimer);
672 if (hasClients() || !m_loader) 680 if (hasClientsOrObservers() || !m_loader)
673 return; 681 return;
674 RefPtrWillBeRawPtr<Resource> protect(this); 682 RefPtrWillBeRawPtr<Resource> protect(this);
675 m_loader->cancelIfNotFinishing(); 683 m_loader->cancelIfNotFinishing();
676 if (m_status != Cached) 684 if (m_status != Cached)
677 memoryCache()->remove(this); 685 memoryCache()->remove(this);
678 } 686 }
679 687
680 void Resource::setDecodedSize(size_t decodedSize) 688 void Resource::setDecodedSize(size_t decodedSize)
681 { 689 {
682 if (decodedSize == m_decodedSize) 690 if (decodedSize == m_decodedSize)
(...skipping 195 matching lines...)
878 return m_data && !m_data->isLocked(); 886 return m_data && !m_data->isLocked();
879 } 887 }
880 888
881 bool Resource::lock() 889 bool Resource::lock()
882 { 890 {
883 if (!m_data) 891 if (!m_data)
884 return true; 892 return true;
885 if (m_data->isLocked()) 893 if (m_data->isLocked())
886 return true; 894 return true;
887 895
888 ASSERT(!hasClients()); 896 ASSERT(!hasClientsOrObservers());
889 897
890 // If locking fails, our buffer has been purged. There's no point 898 // If locking fails, our buffer has been purged. There's no point
891 // in leaving a purged resource in MemoryCache. 899 // in leaving a purged resource in MemoryCache.
892 if (!m_data->lock()) { 900 if (!m_data->lock()) {
893 memoryCache()->remove(this); 901 memoryCache()->remove(this);
894 return false; 902 return false;
895 } 903 }
896 return true; 904 return true;
897 } 905 }
898 906
899 size_t Resource::overheadSize() const 907 size_t Resource::overheadSize() const
900 { 908 {
901 static const int kAverageClientsHashMapSize = 384; 909 static const int kAverageClientsHashMapSize = 384;
902 return sizeof(Resource) + m_response.memoryUsage() + kAverageClientsHashMapS ize + m_resourceRequest.url().getString().length() * 2; 910 return sizeof(Resource) + m_response.memoryUsage() + kAverageClientsHashMapS ize + m_resourceRequest.url().getString().length() * 2;
903 } 911 }
904 912
905 void Resource::didChangePriority(ResourceLoadPriority loadPriority, int intraPri orityValue) 913 void Resource::didChangePriority(ResourceLoadPriority loadPriority, int intraPri orityValue)
906 { 914 {
907 m_resourceRequest.setPriority(loadPriority, intraPriorityValue); 915 m_resourceRequest.setPriority(loadPriority, intraPriorityValue);
908 if (m_loader) 916 if (m_loader)
909 m_loader->didChangePriority(loadPriority, intraPriorityValue); 917 m_loader->didChangePriority(loadPriority, intraPriorityValue);
910 } 918 }
911 919
912 ResourcePriority Resource::priorityFromClients()
913 {
914 ResourcePriority priority;
915 ResourceClientWalker<ResourceClient> walker(m_clients);
916 while (ResourceClient* c = walker.next()) {
917 ResourcePriority nextPriority = c->computeResourcePriority();
918 if (nextPriority.visibility == ResourcePriority::NotVisible)
919 continue;
920 priority.visibility = ResourcePriority::Visible;
921 priority.intraPriorityValue += nextPriority.intraPriorityValue;
922 }
923 return priority;
924 }
925
926 Resource::ResourceCallback* Resource::ResourceCallback::callbackHandler() 920 Resource::ResourceCallback* Resource::ResourceCallback::callbackHandler()
927 { 921 {
928 // Oilpan + LSan: as the callbackHandler() singleton is used by Resource 922 // Oilpan + LSan: as the callbackHandler() singleton is used by Resource
929 // and ResourcePtr finalizers, it cannot be released upon shutdown in 923 // and ResourcePtr finalizers, it cannot be released upon shutdown in
930 // preparation for leak detection. 924 // preparation for leak detection.
931 // 925 //
932 // Keep it out of LSan's reach instead. 926 // Keep it out of LSan's reach instead.
933 LEAK_SANITIZER_DISABLED_SCOPE; 927 LEAK_SANITIZER_DISABLED_SCOPE;
934 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<ResourceCallback>, callbackHandle r, (adoptPtrWillBeNoop(new ResourceCallback))); 928 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<ResourceCallback>, callbackHandle r, (adoptPtrWillBeNoop(new ResourceCallback)));
935 return callbackHandler.get(); 929 return callbackHandler.get();
(...skipping 162 matching lines...)
1098 case Resource::Media: 1092 case Resource::Media:
1099 return "Media"; 1093 return "Media";
1100 case Resource::Manifest: 1094 case Resource::Manifest:
1101 return "Manifest"; 1095 return "Manifest";
1102 } 1096 }
1103 ASSERT_NOT_REACHED(); 1097 ASSERT_NOT_REACHED();
1104 return "Unknown"; 1098 return "Unknown";
1105 } 1099 }
1106 1100
1107 } // namespace blink 1101 } // namespace blink
OLDNEW

Powered by Google App Engine