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