1/*
2 * Copyright (C) 2006, 2007 Apple 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#include "config.h"
27#include "WebKitDLL.h"
28#include "WebCache.h"
29
30#include "CFDictionaryPropertyBag.h"
31#include <WebCore/ApplicationCacheStorage.h>
32#include <WebCore/MemoryCache.h>
33#include <WebCore/CrossOriginPreflightResultCache.h>
34
35// WebCache ---------------------------------------------------------------------------
36
37WebCache::WebCache()
38: m_refCount(0)
39{
40    gClassCount++;
41    gClassNameCount.add("WebCache");
42}
43
44WebCache::~WebCache()
45{
46    gClassCount--;
47    gClassNameCount.remove("WebCache");
48}
49
50WebCache* WebCache::createInstance()
51{
52    WebCache* instance = new WebCache();
53    instance->AddRef();
54    return instance;
55}
56
57// IUnknown -------------------------------------------------------------------
58
59HRESULT STDMETHODCALLTYPE WebCache::QueryInterface(REFIID riid, void** ppvObject)
60{
61    *ppvObject = 0;
62    if (IsEqualGUID(riid, IID_IUnknown))
63        *ppvObject = static_cast<WebCache*>(this);
64    else if (IsEqualGUID(riid, IID_IWebCache))
65        *ppvObject = static_cast<WebCache*>(this);
66    else
67        return E_NOINTERFACE;
68
69    AddRef();
70    return S_OK;
71}
72
73ULONG STDMETHODCALLTYPE WebCache::AddRef(void)
74{
75    return ++m_refCount;
76}
77
78ULONG STDMETHODCALLTYPE WebCache::Release(void)
79{
80    ULONG newRef = --m_refCount;
81    if (!newRef)
82        delete(this);
83
84    return newRef;
85}
86
87// IWebCache ------------------------------------------------------------------------------
88
89HRESULT STDMETHODCALLTYPE WebCache::statistics(
90    /* [in][out] */ int* count,
91    /* [retval][out] */ IPropertyBag ** s)
92{
93    if (!count || (s && *count < 4))
94        return E_FAIL;
95
96    *count = 4;
97    if (!s)
98        return S_OK;
99
100    WebCore::MemoryCache::Statistics stat = WebCore::memoryCache()->getStatistics();
101
102    static CFStringRef imagesKey = CFSTR("images");
103    static CFStringRef stylesheetsKey = CFSTR("style sheets");
104    static CFStringRef xslKey = CFSTR("xsl");
105    static CFStringRef scriptsKey = CFSTR("scripts");
106#if !ENABLE(XSLT)
107    const int zero = 0;
108#endif
109
110    RetainPtr<CFMutableDictionaryRef> dictionary = adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
111
112    RetainPtr<CFNumberRef> value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.count));
113    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
114
115    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.count));
116    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
117
118#if ENABLE(XSLT)
119    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.count));
120#else
121    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
122#endif
123    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
124
125    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.count));
126    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
127
128    COMPtr<CFDictionaryPropertyBag> propBag = CFDictionaryPropertyBag::createInstance();
129    propBag->setDictionary(dictionary.get());
130    s[0] = propBag.leakRef();
131
132    dictionary = adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
133
134    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.size));
135    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
136
137    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.size));
138    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
139
140#if ENABLE(XSLT)
141    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.size));
142#else
143    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
144#endif
145    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
146
147    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.size));
148    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
149
150    propBag = CFDictionaryPropertyBag::createInstance();
151    propBag->setDictionary(dictionary.get());
152    s[1] = propBag.leakRef();
153
154    dictionary = adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
155
156    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.liveSize));
157    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
158
159    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.liveSize));
160    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
161
162#if ENABLE(XSLT)
163    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.liveSize));
164#else
165    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
166#endif
167    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
168
169    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.liveSize));
170    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
171
172    propBag = CFDictionaryPropertyBag::createInstance();
173    propBag->setDictionary(dictionary.get());
174    s[2] = propBag.leakRef();
175
176    dictionary = adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
177
178    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.decodedSize));
179    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
180
181    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.decodedSize));
182    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
183
184#if ENABLE(XSLT)
185    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.decodedSize));
186#else
187    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
188#endif
189    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
190
191    value = adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.decodedSize));
192    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
193
194    propBag = CFDictionaryPropertyBag::createInstance();
195    propBag->setDictionary(dictionary.get());
196    s[3] = propBag.leakRef();
197
198    return S_OK;
199}
200
201HRESULT STDMETHODCALLTYPE WebCache::empty( void)
202{
203    if (WebCore::memoryCache()->disabled())
204        return S_OK;
205    WebCore::memoryCache()->setDisabled(true);
206    WebCore::memoryCache()->setDisabled(false);
207
208    // Empty the application cache.
209    WebCore::cacheStorage().empty();
210
211    // Empty the Cross-Origin Preflight cache
212    WebCore::CrossOriginPreflightResultCache::shared().empty();
213
214    return S_OK;
215}
216
217HRESULT STDMETHODCALLTYPE WebCache::setDisabled(
218    /* [in] */ BOOL disabled)
219{
220    WebCore::memoryCache()->setDisabled(!!disabled);
221    return S_OK;
222}
223
224HRESULT STDMETHODCALLTYPE WebCache::disabled(
225    /* [out][retval] */ BOOL* disabled)
226{
227    if (!disabled)
228        return E_POINTER;
229    *disabled = WebCore::memoryCache()->disabled();
230    return S_OK;
231}
232