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,
111        CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
112
113    RetainPtr<CFNumberRef> value(AdoptCF, CFNumberCreate(0, kCFNumberIntType, &stat.images.count));
114    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
115
116    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.count));
117    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
118
119#if ENABLE(XSLT)
120    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.count));
121#else
122    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
123#endif
124    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
125
126    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.count));
127    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
128
129    COMPtr<CFDictionaryPropertyBag> propBag = CFDictionaryPropertyBag::createInstance();
130    propBag->setDictionary(dictionary.get());
131    s[0] = propBag.releaseRef();
132
133    dictionary.adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
134
135    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.size));
136    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
137
138    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.size));
139    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
140
141#if ENABLE(XSLT)
142    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.size));
143#else
144    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
145#endif
146    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
147
148    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.size));
149    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
150
151    propBag = CFDictionaryPropertyBag::createInstance();
152    propBag->setDictionary(dictionary.get());
153    s[1] = propBag.releaseRef();
154
155    dictionary.adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
156
157    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.liveSize));
158    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
159
160    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.liveSize));
161    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
162
163#if ENABLE(XSLT)
164    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.liveSize));
165#else
166    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
167#endif
168    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
169
170    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.liveSize));
171    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
172
173    propBag = CFDictionaryPropertyBag::createInstance();
174    propBag->setDictionary(dictionary.get());
175    s[2] = propBag.releaseRef();
176
177    dictionary.adoptCF(CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
178
179    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.images.decodedSize));
180    CFDictionaryAddValue(dictionary.get(), imagesKey, value.get());
181
182    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.cssStyleSheets.decodedSize));
183    CFDictionaryAddValue(dictionary.get(), stylesheetsKey, value.get());
184
185#if ENABLE(XSLT)
186    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.xslStyleSheets.decodedSize));
187#else
188    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &zero));
189#endif
190    CFDictionaryAddValue(dictionary.get(), xslKey, value.get());
191
192    value.adoptCF(CFNumberCreate(0, kCFNumberIntType, &stat.scripts.decodedSize));
193    CFDictionaryAddValue(dictionary.get(), scriptsKey, value.get());
194
195    propBag = CFDictionaryPropertyBag::createInstance();
196    propBag->setDictionary(dictionary.get());
197    s[3] = propBag.releaseRef();
198
199    return S_OK;
200}
201
202HRESULT STDMETHODCALLTYPE WebCache::empty( void)
203{
204    if (WebCore::memoryCache()->disabled())
205        return S_OK;
206    WebCore::memoryCache()->setDisabled(true);
207    WebCore::memoryCache()->setDisabled(false);
208
209    // Empty the application cache.
210    WebCore::cacheStorage().empty();
211
212    // Empty the Cross-Origin Preflight cache
213    WebCore::CrossOriginPreflightResultCache::shared().empty();
214
215    return S_OK;
216}
217
218HRESULT STDMETHODCALLTYPE WebCache::setDisabled(
219    /* [in] */ BOOL disabled)
220{
221    WebCore::memoryCache()->setDisabled(!!disabled);
222    return S_OK;
223}
224
225HRESULT STDMETHODCALLTYPE WebCache::disabled(
226    /* [out][retval] */ BOOL* disabled)
227{
228    if (!disabled)
229        return E_POINTER;
230    *disabled = WebCore::memoryCache()->disabled();
231    return S_OK;
232}
233