Changeset 163318 in webkit


Ignore:
Timestamp:
Feb 3, 2014 11:34:30 AM (10 years ago)
Author:
ddkilzer@apple.com
Message:

Remove CachedImageManual class
<http://webkit.org/b/128043>

Reviewed by Darin Adler.

Get rid of the CachedImageManual class by inlining its
functionality into CachedImage. This makes it possible to
de-virtual-ize isManual() (renamed to isManuallyCached()) and to
make CachedImage final. The size of CachedImage does not
increase because we turn an existing bool into a bitfield to add
an m_isManuallyCached bit, and create a static CachedImageClient
in MemoryCache.cpp as the "fake" client to keep the manually
cached image alive in the cache.

  • loader/cache/CachedImage.cpp:

(WebCore::CachedImage::CachedImage): Set m_isManuallyCached
bitfield. For one overloaded constructor, move the
CachedImageManual code into the CachedImage constructor.
(WebCore::CachedImageManual::CachedImageManual): Remove.
(WebCore::CachedImage::mustRevalidateDueToCacheHeaders): Move
method from CachedImageManual to CachedImage, and put
ManuallyCached behavior behind a check.

  • loader/cache/CachedImage.h: Update includes. Make CachedImage

final. Add CachedImage::CacheBehaviorType enum when manually
cached images are created. Move CachedImageManual methods into
CachedImage, remove addFakeClient() and removeFakeClient()
methods (MemoryCache methods use addClient() and removeClient()
with a static CachedImageClient), and remove the
CachedImageManual class definition. Change
m_shouldPaintBrokenImage to a bitfield and add
m_isManuallyCached bitfield.

  • loader/cache/MemoryCache.cpp:

(WebCore::MemoryCache::addImageToCache): Use std::unique_ptr and
remove useless NULL check after calling CachedImage constructor.
(WebCore::MemoryCache::removeImageFromCache):

  • Update to use CachedImage class instead of CachedImageManual.
Location:
trunk/Source/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r163317 r163318  
     12014-02-03  David Kilzer  <ddkilzer@apple.com>
     2
     3        Remove CachedImageManual class
     4        <http://webkit.org/b/128043>
     5
     6        Reviewed by Darin Adler.
     7
     8        Get rid of the CachedImageManual class by inlining its
     9        functionality into CachedImage.  This makes it possible to
     10        de-virtual-ize isManual() (renamed to isManuallyCached()) and to
     11        make CachedImage final.  The size of CachedImage does not
     12        increase because we turn an existing bool into a bitfield to add
     13        an m_isManuallyCached bit, and create a static CachedImageClient
     14        in MemoryCache.cpp as the "fake" client to keep the manually
     15        cached image alive in the cache.
     16
     17        * loader/cache/CachedImage.cpp:
     18        (WebCore::CachedImage::CachedImage): Set m_isManuallyCached
     19        bitfield.  For one overloaded constructor, move the
     20        CachedImageManual code into the CachedImage constructor.
     21        (WebCore::CachedImageManual::CachedImageManual): Remove.
     22        (WebCore::CachedImage::mustRevalidateDueToCacheHeaders): Move
     23        method from CachedImageManual to CachedImage, and put
     24        ManuallyCached behavior behind a check.
     25        * loader/cache/CachedImage.h: Update includes.  Make CachedImage
     26        final.  Add CachedImage::CacheBehaviorType enum when manually
     27        cached images are created.  Move CachedImageManual methods into
     28        CachedImage, remove addFakeClient() and removeFakeClient()
     29        methods (MemoryCache methods use addClient() and removeClient()
     30        with a static CachedImageClient), and remove the
     31        CachedImageManual class definition.  Change
     32        m_shouldPaintBrokenImage to a bitfield and add
     33        m_isManuallyCached bitfield.
     34
     35        * loader/cache/MemoryCache.cpp:
     36        (WebCore::MemoryCache::addImageToCache): Use std::unique_ptr and
     37        remove useless NULL check after calling CachedImage constructor.
     38        (WebCore::MemoryCache::removeImageFromCache):
     39        - Update to use CachedImage class instead of CachedImageManual.
     40
    1412014-02-03  Zan Dobersek  <zdobersek@igalia.com>
    242
  • trunk/Source/WebCore/loader/cache/CachedImage.cpp

    r163148 r163318  
    6767    : CachedResource(resourceRequest, ImageResource)
    6868    , m_image(0)
     69    , m_isManuallyCached(false)
    6970    , m_shouldPaintBrokenImage(true)
    7071{
     
    7576    : CachedResource(ResourceRequest(), ImageResource)
    7677    , m_image(image)
     78    , m_isManuallyCached(false)
    7779    , m_shouldPaintBrokenImage(true)
    7880{
     
    8183}
    8284
    83 CachedImage::CachedImage(const URL& url, Image* image)
     85CachedImage::CachedImage(const URL& url, Image* image, CachedImage::CacheBehaviorType type)
    8486    : CachedResource(ResourceRequest(url), ImageResource)
    8587    , m_image(image)
     88    , m_isManuallyCached(type == CachedImage::ManuallyCached)
    8689    , m_shouldPaintBrokenImage(true)
    8790{
    8891    setStatus(Cached);
    8992    setLoading(false);
     93    if (UNLIKELY(isManuallyCached())) {
     94        // Use the incoming URL in the response field. This ensures that code
     95        // using the response directly, such as origin checks for security,
     96        // actually see something.
     97        m_response.setURL(url);
     98    }
    9099}
    91100
     
    613622}
    614623
    615 #if USE(CF)
    616 // FIXME: We should look to incorporate the functionality of CachedImageManual
    617 // into CachedImage or find a better place for this class.
    618 // FIXME: Remove the USE(CF) once we make MemoryCache::addImageToCache() platform-independent.
    619 CachedImageManual::CachedImageManual(const URL& url, Image* image)
    620     : CachedImage(url, image)
    621     , m_fakeClient(std::make_unique<CachedImageClient>())
    622 {
    623     // Use the incoming URL in the response field. This ensures that code
    624     // using the response directly, such as origin checks for security,
    625     // actually see something.
    626     m_response.setURL(url);
    627 }
    628 
    629 bool CachedImageManual::mustRevalidateDueToCacheHeaders(CachePolicy) const
    630 {
    631     // Do not revalidate manually cached images. This mechanism is used as a
    632     // way to efficiently share an image from the client to content and
    633     // the URL for that image may not represent a resource that can be
    634     // retrieved by standard means. If the manual caching SPI is used, it is
    635     // incumbent on the client to only use valid resources.
    636     return false;
    637 }
    638 #endif
     624bool CachedImage::mustRevalidateDueToCacheHeaders(CachePolicy policy) const
     625{
     626    if (UNLIKELY(isManuallyCached())) {
     627        // Do not revalidate manually cached images. This mechanism is used as a
     628        // way to efficiently share an image from the client to content and
     629        // the URL for that image may not represent a resource that can be
     630        // retrieved by standard means. If the manual caching SPI is used, it is
     631        // incumbent on the client to only use valid resources.
     632        return false;
     633    }
     634    return CachedResource::mustRevalidateDueToCacheHeaders(policy);
     635}
    639636
    640637} // namespace WebCore
  • trunk/Source/WebCore/loader/cache/CachedImage.h

    r163242 r163318  
    4545struct Length;
    4646
    47 class CachedImage : public CachedResource, public ImageObserver {
     47class CachedImage final : public CachedResource, public ImageObserver {
    4848    friend class MemoryCache;
    4949
    5050public:
     51    enum CacheBehaviorType { AutomaticallyCached, ManuallyCached };
     52
    5153    CachedImage(const ResourceRequest&);
    5254    CachedImage(Image*);
    53     CachedImage(const URL&, Image*);
     55    CachedImage(const URL&, Image*, CacheBehaviorType = AutomaticallyCached);
    5456    virtual ~CachedImage();
    5557
     
    8082    void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
    8183
    82 #if USE(CF)
    83     // FIXME: Remove the USE(CF) once we make MemoryCache::addImageToCache() platform-independent.
    84     virtual bool isManual() const { return false; }
    85 #endif
     84    bool isManuallyCached() const { return m_isManuallyCached; }
     85    virtual bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
    8686
    8787    static void resumeAnimatingImagesForLoader(CachedResourceLoader*);
     
    143143    std::unique_ptr<SVGImageCache> m_svgImageCache;
    144144#endif
    145     bool m_shouldPaintBrokenImage;
     145    unsigned char m_isManuallyCached : 1;
     146    unsigned char m_shouldPaintBrokenImage : 1;
    146147};
    147148
    148 #if USE(CF)
    149 // FIXME: We should look to incorporate the functionality of CachedImageManual
    150 // into CachedImage or find a better place for this class.
    151 // FIXME: Remove the USE(CF) once we make MemoryCache::addImageToCache() platform-independent.
    152 class CachedImageManual final : public CachedImage {
    153 public:
    154     CachedImageManual(const URL&, Image*);
    155     void addFakeClient() { addClient(m_fakeClient.get()); }
    156     void removeFakeClient() { removeClient(m_fakeClient.get()); }
    157     virtual bool isManual() const override { return true; }
    158     virtual bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
    159 private:
    160     std::unique_ptr<CachedResourceClient> m_fakeClient;
    161 };
    162 #endif
    163 
    164149CACHED_RESOURCE_TYPE_CASTS(CachedImage, CachedResource, CachedResource::ImageResource)
    165 #if USE(CF)
    166 TYPE_CASTS_BASE(CachedImageManual, CachedImage, resource, resource->isManual(), resource.isManual())
    167 #endif
    168150
    169151}
  • trunk/Source/WebCore/loader/cache/MemoryCache.cpp

    r163148 r163318  
    2626#include "BitmapImage.h"
    2727#include "CachedImage.h"
     28#include "CachedImageClient.h"
    2829#include "CachedResource.h"
    2930#include "CachedResourceHandle.h"
     
    216217// FIXME: Remove the USE(CG) once we either make NativeImagePtr a smart pointer on all platforms or
    217218// remove the usage of CFRetain() in MemoryCache::addImageToCache() so as to make the code platform-independent.
     219static CachedImageClient& dummyCachedImageClient()
     220{
     221    DEFINE_STATIC_LOCAL(CachedImageClient, client, ());
     222    return client;
     223}
     224
    218225bool MemoryCache::addImageToCache(NativeImagePtr image, const URL& url, const String& cachePartition)
    219226{
     
    225232        return false;
    226233
    227     CachedImageManual* cachedImage = new CachedImageManual(url, bitmapImage.get());
    228     if (!cachedImage)
    229         return false;
     234    std::unique_ptr<CachedImage> cachedImage = std::make_unique<CachedImage>(url, bitmapImage.get(), CachedImage::ManuallyCached);
    230235
    231236    // Actual release of the CGImageRef is done in BitmapImage.
    232237    CFRetain(image);
    233     cachedImage->addFakeClient();
     238    cachedImage->addClient(&dummyCachedImageClient());
    234239    cachedImage->setDecodedSize(bitmapImage->decodedSize());
    235240#if ENABLE(CACHE_PARTITIONING)
    236241    cachedImage->resourceRequest().setCachePartition(cachePartition);
    237242#endif
    238     add(cachedImage);
     243    add(cachedImage.release());
    239244    return true;
    240245}
     
    256261
    257262    // A resource exists and is not a manually cached image, so just remove it.
    258     if (!resource->isImage() || !toCachedImage(resource)->isManual()) {
     263    if (!resource->isImage() || !toCachedImage(resource)->isManuallyCached()) {
    259264        evict(resource);
    260265        return;
     
    266271    // removing the last client triggers a MemoryCache::prune, so the
    267272    // resource may be deleted after this call.
    268     toCachedImageManual(toCachedImage(resource))->removeFakeClient();
     273    toCachedImage(resource)->removeClient(&dummyCachedImageClient());
    269274}
    270275#endif
Note: See TracChangeset for help on using the changeset viewer.