WebKit/chromium/src/WebCache.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2009 Google Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 #include "config.h"
       
    32 #include "WebCache.h"
       
    33 
       
    34 // Instead of providing accessors, we make all members of Cache public.
       
    35 // This will make it easier to track WebCore changes to the Cache class.
       
    36 // FIXME: We should introduce public getters on the Cache class.
       
    37 #define private public
       
    38 #include "Cache.h"
       
    39 #undef private
       
    40 
       
    41 using namespace WebCore;
       
    42 
       
    43 namespace WebKit {
       
    44 
       
    45 // A helper method for coverting a Cache::TypeStatistic to a
       
    46 // WebCache::ResourceTypeStat.
       
    47 static void ToResourceTypeStat(const Cache::TypeStatistic& from,
       
    48                                WebCache::ResourceTypeStat& to)
       
    49 {
       
    50     to.count = static_cast<size_t>(from.count);
       
    51     to.size = static_cast<size_t>(from.size);
       
    52     to.liveSize = static_cast<size_t>(from.liveSize);
       
    53     to.decodedSize = static_cast<size_t>(from.decodedSize);
       
    54 }
       
    55 
       
    56 void WebCache::setCapacities(
       
    57     size_t minDeadCapacity, size_t maxDeadCapacity, size_t capacity)
       
    58 {
       
    59     Cache* cache = WebCore::cache();
       
    60     if (cache)
       
    61         cache->setCapacities(static_cast<unsigned int>(minDeadCapacity),
       
    62                              static_cast<unsigned int>(maxDeadCapacity),
       
    63                              static_cast<unsigned int>(capacity));
       
    64 }
       
    65 
       
    66 void WebCache::clear()
       
    67 {
       
    68     Cache* cache = WebCore::cache();
       
    69     if (cache && !cache->disabled()) {
       
    70         // NOTE: I think using setDisabled() instead of setCapacities() will
       
    71         // remove from the cache items that won't actually be freed from memory
       
    72         // (due to other live references to them), so it just results in wasting
       
    73         // time later and not saving memory compared to the below technique.
       
    74         unsigned minDeadCapacity = cache->m_minDeadCapacity;
       
    75         unsigned maxDeadCapacity = cache->m_maxDeadCapacity;
       
    76         unsigned capacity = cache->m_capacity;
       
    77         cache->setCapacities(0, 0, 0);  // Will prune the cache.
       
    78         cache->setCapacities(minDeadCapacity, maxDeadCapacity, capacity);
       
    79     }
       
    80 }
       
    81 
       
    82 void WebCache::getUsageStats(UsageStats* result)
       
    83 {
       
    84     ASSERT(result);
       
    85 
       
    86     Cache* cache = WebCore::cache();
       
    87     if (cache) {
       
    88         result->minDeadCapacity = cache->m_minDeadCapacity;
       
    89         result->maxDeadCapacity = cache->m_maxDeadCapacity;
       
    90         result->capacity = cache->m_capacity;
       
    91         result->liveSize = cache->m_liveSize;
       
    92         result->deadSize = cache->m_deadSize;
       
    93     } else
       
    94         memset(result, 0, sizeof(UsageStats));
       
    95 }
       
    96 
       
    97 void WebCache::getResourceTypeStats(ResourceTypeStats* result)
       
    98 {
       
    99     Cache* cache = WebCore::cache();
       
   100     if (cache) {
       
   101         Cache::Statistics stats = cache->getStatistics();
       
   102         ToResourceTypeStat(stats.images, result->images);
       
   103         ToResourceTypeStat(stats.cssStyleSheets, result->cssStyleSheets);
       
   104         ToResourceTypeStat(stats.scripts, result->scripts);
       
   105 #if ENABLE(XSLT)
       
   106         ToResourceTypeStat(stats.xslStyleSheets, result->xslStyleSheets);
       
   107 #else
       
   108         memset(&result->xslStyleSheets, 0, sizeof(result->xslStyleSheets));
       
   109 #endif
       
   110         ToResourceTypeStat(stats.fonts, result->fonts);
       
   111     } else
       
   112         memset(result, 0, sizeof(WebCache::ResourceTypeStats));
       
   113 }
       
   114 
       
   115 }  // namespace WebKit