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 MemoryCache public. 35 // This will make it easier to track WebCore changes to the MemoryCache class. 36 // FIXME: We should introduce public getters on the MemoryCache class. 37 #define private public 38 #include "MemoryCache.h" 39 #undef private 40 41 using namespace WebCore; 42 43 namespace WebKit { 44 45 // A helper method for coverting a MemoryCache::TypeStatistic to a 46 // WebCache::ResourceTypeStat. 47 static void ToResourceTypeStat(const MemoryCache::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 MemoryCache* cache = WebCore::memoryCache(); 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 MemoryCache* cache = WebCore::memoryCache(); 69 if (cache && !cache->disabled()) { 70 cache->setDisabled(true); 71 cache->setDisabled(false); 72 } 73 } 74 75 void WebCache::getUsageStats(UsageStats* result) 76 { 77 ASSERT(result); 78 79 MemoryCache* cache = WebCore::memoryCache(); 80 if (cache) { 81 result->minDeadCapacity = cache->m_minDeadCapacity; 82 result->maxDeadCapacity = cache->m_maxDeadCapacity; 83 result->capacity = cache->m_capacity; 84 result->liveSize = cache->m_liveSize; 85 result->deadSize = cache->m_deadSize; 86 } else 87 memset(result, 0, sizeof(UsageStats)); 88 } 89 90 void WebCache::getResourceTypeStats(ResourceTypeStats* result) 91 { 92 MemoryCache* cache = WebCore::memoryCache(); 93 if (cache) { 94 MemoryCache::Statistics stats = cache->getStatistics(); 95 ToResourceTypeStat(stats.images, result->images); 96 ToResourceTypeStat(stats.cssStyleSheets, result->cssStyleSheets); 97 ToResourceTypeStat(stats.scripts, result->scripts); 98 #if ENABLE(XSLT) 99 ToResourceTypeStat(stats.xslStyleSheets, result->xslStyleSheets); 100 #else 101 memset(&result->xslStyleSheets, 0, sizeof(result->xslStyleSheets)); 102 #endif 103 ToResourceTypeStat(stats.fonts, result->fonts); 104 } else 105 memset(result, 0, sizeof(WebCache::ResourceTypeStats)); 106 } 107 108 } // namespace WebKit 109