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 "WebNSObjectExtras.h"
29#import "WebPreferences.h"
30#import "WebSystemInterface.h"
31#import "WebView.h"
32#import "WebViewInternal.h"
33#import <WebCore/ApplicationCacheStorage.h>
34#import <WebCore/MemoryCache.h>
35#import <WebCore/CrossOriginPreflightResultCache.h>
36
37@implementation WebCache
38
39+ (void)initialize
40{
41    InitWebCoreSystemInterface();
42}
43
44+ (NSArray *)statistics
45{
46    WebCore::MemoryCache::Statistics s = WebCore::memoryCache()->getStatistics();
47
48    return [NSArray arrayWithObjects:
49        [NSDictionary dictionaryWithObjectsAndKeys:
50            [NSNumber numberWithInt:s.images.count], @"Images",
51            [NSNumber numberWithInt:s.cssStyleSheets.count], @"CSS",
52#if ENABLE(XSLT)
53            [NSNumber numberWithInt:s.xslStyleSheets.count], @"XSL",
54#else
55            [NSNumber numberWithInt:0], @"XSL",
56#endif
57            [NSNumber numberWithInt:s.scripts.count], @"JavaScript",
58            nil],
59        [NSDictionary dictionaryWithObjectsAndKeys:
60            [NSNumber numberWithInt:s.images.size], @"Images",
61            [NSNumber numberWithInt:s.cssStyleSheets.size] ,@"CSS",
62#if ENABLE(XSLT)
63            [NSNumber numberWithInt:s.xslStyleSheets.size], @"XSL",
64#else
65            [NSNumber numberWithInt:0], @"XSL",
66#endif
67            [NSNumber numberWithInt:s.scripts.size], @"JavaScript",
68            nil],
69        [NSDictionary dictionaryWithObjectsAndKeys:
70            [NSNumber numberWithInt:s.images.liveSize], @"Images",
71            [NSNumber numberWithInt:s.cssStyleSheets.liveSize] ,@"CSS",
72#if ENABLE(XSLT)
73            [NSNumber numberWithInt:s.xslStyleSheets.liveSize], @"XSL",
74#else
75            [NSNumber numberWithInt:0], @"XSL",
76#endif
77            [NSNumber numberWithInt:s.scripts.liveSize], @"JavaScript",
78            nil],
79        [NSDictionary dictionaryWithObjectsAndKeys:
80            [NSNumber numberWithInt:s.images.decodedSize], @"Images",
81            [NSNumber numberWithInt:s.cssStyleSheets.decodedSize] ,@"CSS",
82#if ENABLE(XSLT)
83            [NSNumber numberWithInt:s.xslStyleSheets.decodedSize], @"XSL",
84#else
85            [NSNumber numberWithInt:0], @"XSL",
86#endif
87            [NSNumber numberWithInt:s.scripts.decodedSize], @"JavaScript",
88            nil],
89        [NSDictionary dictionaryWithObjectsAndKeys:
90            [NSNumber numberWithInt:s.images.purgeableSize], @"Images",
91            [NSNumber numberWithInt:s.cssStyleSheets.purgeableSize] ,@"CSS",
92#if ENABLE(XSLT)
93            [NSNumber numberWithInt:s.xslStyleSheets.purgeableSize], @"XSL",
94#else
95            [NSNumber numberWithInt:0], @"XSL",
96#endif
97            [NSNumber numberWithInt:s.scripts.purgeableSize], @"JavaScript",
98            nil],
99        [NSDictionary dictionaryWithObjectsAndKeys:
100            [NSNumber numberWithInt:s.images.purgedSize], @"Images",
101            [NSNumber numberWithInt:s.cssStyleSheets.purgedSize] ,@"CSS",
102#if ENABLE(XSLT)
103            [NSNumber numberWithInt:s.xslStyleSheets.purgedSize], @"XSL",
104#else
105            [NSNumber numberWithInt:0], @"XSL",
106#endif
107            [NSNumber numberWithInt:s.scripts.purgedSize], @"JavaScript",
108            nil],
109        nil];
110}
111
112+ (void)empty
113{
114    // Toggling the cache model like this forces the cache to evict all its in-memory resources.
115    WebCacheModel cacheModel = [WebView _cacheModel];
116    [WebView _setCacheModel:WebCacheModelDocumentViewer];
117    [WebView _setCacheModel:cacheModel];
118
119#if ENABLE(OFFLINE_WEB_APPLICATIONS)
120    // Empty the application cache.
121    WebCore::cacheStorage().empty();
122#endif
123
124    // Empty the Cross-Origin Preflight cache
125    WebCore::CrossOriginPreflightResultCache::shared().empty();
126}
127
128+ (void)setDisabled:(BOOL)disabled
129{
130    if (!pthread_main_np())
131        return [[self _webkit_invokeOnMainThread] setDisabled:disabled];
132
133    WebCore::memoryCache()->setDisabled(disabled);
134}
135
136+ (BOOL)isDisabled
137{
138    return WebCore::memoryCache()->disabled();
139}
140
141@end
142