1 /* 2 * Copyright (C) 2006 Apple Computer, 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 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #import "WebCache.h" 27 28 #import "WebPreferences.h" 29 #import "WebSystemInterface.h" 30 #import "WebView.h" 31 #import "WebViewInternal.h" 32 #import <WebCore/ApplicationCacheStorage.h> 33 #import <WebCore/Cache.h> 34 #import <WebCore/CrossOriginPreflightResultCache.h> 35 36 @implementation WebCache 37 38 + (void)initialize 39 { 40 InitWebCoreSystemInterface(); 41 } 42 43 + (NSArray *)statistics 44 { 45 WebCore::Cache::Statistics s = WebCore::cache()->getStatistics(); 46 47 return [NSArray arrayWithObjects: 48 [NSDictionary dictionaryWithObjectsAndKeys: 49 [NSNumber numberWithInt:s.images.count], @"Images", 50 [NSNumber numberWithInt:s.cssStyleSheets.count], @"CSS", 51 #if ENABLE(XSLT) 52 [NSNumber numberWithInt:s.xslStyleSheets.count], @"XSL", 53 #else 54 [NSNumber numberWithInt:0], @"XSL", 55 #endif 56 [NSNumber numberWithInt:s.scripts.count], @"JavaScript", 57 nil], 58 [NSDictionary dictionaryWithObjectsAndKeys: 59 [NSNumber numberWithInt:s.images.size], @"Images", 60 [NSNumber numberWithInt:s.cssStyleSheets.size] ,@"CSS", 61 #if ENABLE(XSLT) 62 [NSNumber numberWithInt:s.xslStyleSheets.size], @"XSL", 63 #else 64 [NSNumber numberWithInt:0], @"XSL", 65 #endif 66 [NSNumber numberWithInt:s.scripts.size], @"JavaScript", 67 nil], 68 [NSDictionary dictionaryWithObjectsAndKeys: 69 [NSNumber numberWithInt:s.images.liveSize], @"Images", 70 [NSNumber numberWithInt:s.cssStyleSheets.liveSize] ,@"CSS", 71 #if ENABLE(XSLT) 72 [NSNumber numberWithInt:s.xslStyleSheets.liveSize], @"XSL", 73 #else 74 [NSNumber numberWithInt:0], @"XSL", 75 #endif 76 [NSNumber numberWithInt:s.scripts.liveSize], @"JavaScript", 77 nil], 78 [NSDictionary dictionaryWithObjectsAndKeys: 79 [NSNumber numberWithInt:s.images.decodedSize], @"Images", 80 [NSNumber numberWithInt:s.cssStyleSheets.decodedSize] ,@"CSS", 81 #if ENABLE(XSLT) 82 [NSNumber numberWithInt:s.xslStyleSheets.decodedSize], @"XSL", 83 #else 84 [NSNumber numberWithInt:0], @"XSL", 85 #endif 86 [NSNumber numberWithInt:s.scripts.decodedSize], @"JavaScript", 87 nil], 88 [NSDictionary dictionaryWithObjectsAndKeys: 89 [NSNumber numberWithInt:s.images.purgeableSize], @"Images", 90 [NSNumber numberWithInt:s.cssStyleSheets.purgeableSize] ,@"CSS", 91 #if ENABLE(XSLT) 92 [NSNumber numberWithInt:s.xslStyleSheets.purgeableSize], @"XSL", 93 #else 94 [NSNumber numberWithInt:0], @"XSL", 95 #endif 96 [NSNumber numberWithInt:s.scripts.purgeableSize], @"JavaScript", 97 nil], 98 [NSDictionary dictionaryWithObjectsAndKeys: 99 [NSNumber numberWithInt:s.images.purgedSize], @"Images", 100 [NSNumber numberWithInt:s.cssStyleSheets.purgedSize] ,@"CSS", 101 #if ENABLE(XSLT) 102 [NSNumber numberWithInt:s.xslStyleSheets.purgedSize], @"XSL", 103 #else 104 [NSNumber numberWithInt:0], @"XSL", 105 #endif 106 [NSNumber numberWithInt:s.scripts.purgedSize], @"JavaScript", 107 nil], 108 nil]; 109 } 110 111 + (void)empty 112 { 113 // Toggling the cache model like this forces the cache to evict all its in-memory resources. 114 WebCacheModel cacheModel = [WebView _cacheModel]; 115 [WebView _setCacheModel:WebCacheModelDocumentViewer]; 116 [WebView _setCacheModel:cacheModel]; 117 118 #if ENABLE(OFFLINE_WEB_APPLICATIONS) 119 // Empty the application cache. 120 WebCore::cacheStorage().empty(); 121 #endif 122 123 // Empty the Cross-Origin Preflight cache 124 WebCore::CrossOriginPreflightResultCache::shared().empty(); 125 } 126 127 + (void)setDisabled:(BOOL)disabled 128 { 129 WebCore::cache()->setDisabled(disabled); 130 } 131 132 + (BOOL)isDisabled 133 { 134 return WebCore::cache()->disabled(); 135 } 136 137 @end 138