2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "WebDatabaseManager.h"
31 #include "WebKitDLL.h"
33 #if ENABLE(SQL_DATABASE)
35 #include "CFDictionaryPropertyBag.h"
36 #include "COMEnumVariant.h"
37 #include "MarshallingHelpers.h"
38 #include "WebNotificationCenter.h"
39 #include "WebSecurityOrigin.h"
41 #include <WebCore/BString.h>
42 #include <WebCore/COMPtr.h>
43 #include <WebCore/DatabaseTracker.h>
44 #include <WebCore/FileSystem.h>
45 #include <WebCore/SecurityOrigin.h>
46 #include <wtf/MainThread.h>
48 using namespace WebCore;
50 static inline bool isEqual(LPCWSTR s1, LPCWSTR s2)
52 return !wcscmp(s1, s2);
55 class DatabaseDetailsPropertyBag : public IPropertyBag {
56 WTF_MAKE_NONCOPYABLE(DatabaseDetailsPropertyBag);
58 static DatabaseDetailsPropertyBag* createInstance(const DatabaseDetails&);
61 virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
62 virtual ULONG STDMETHODCALLTYPE AddRef();
63 virtual ULONG STDMETHODCALLTYPE Release();
66 virtual HRESULT STDMETHODCALLTYPE Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog);
67 virtual HRESULT STDMETHODCALLTYPE Write(LPCOLESTR pszPropName, VARIANT* pVar);
69 DatabaseDetailsPropertyBag(const DatabaseDetails& details)
71 , m_details(details) { }
72 ~DatabaseDetailsPropertyBag() { }
75 DatabaseDetails m_details;
78 // DatabaseDetailsPropertyBag ------------------------------------------------------
79 DatabaseDetailsPropertyBag* DatabaseDetailsPropertyBag::createInstance(const DatabaseDetails& details)
81 DatabaseDetailsPropertyBag* instance = new DatabaseDetailsPropertyBag(details);
86 // IUnknown ------------------------------------------------------------------------
87 ULONG STDMETHODCALLTYPE DatabaseDetailsPropertyBag::AddRef()
92 ULONG STDMETHODCALLTYPE DatabaseDetailsPropertyBag::Release()
94 ULONG newRef = --m_refCount;
101 HRESULT STDMETHODCALLTYPE DatabaseDetailsPropertyBag::QueryInterface(REFIID riid, void** ppvObject)
104 if (IsEqualGUID(riid, IID_IUnknown))
105 *ppvObject = static_cast<DatabaseDetailsPropertyBag*>(this);
106 else if (IsEqualGUID(riid, IID_IPropertyBag))
107 *ppvObject = static_cast<DatabaseDetailsPropertyBag*>(this);
109 return E_NOINTERFACE;
115 // IPropertyBag --------------------------------------------------------------------
116 HRESULT STDMETHODCALLTYPE DatabaseDetailsPropertyBag::Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog*)
118 if (!pszPropName || !pVar)
123 if (isEqual(pszPropName, WebDatabaseDisplayNameKey)) {
124 COMVariantSetter<String>::setVariant(pVar, m_details.displayName());
126 } else if (isEqual(pszPropName, WebDatabaseExpectedSizeKey)) {
127 COMVariantSetter<unsigned long long>::setVariant(pVar, m_details.expectedUsage());
129 } else if (isEqual(pszPropName, WebDatabaseUsageKey)) {
130 COMVariantSetter<unsigned long long>::setVariant(pVar, m_details.currentUsage());
137 HRESULT STDMETHODCALLTYPE DatabaseDetailsPropertyBag::Write(LPCOLESTR pszPropName, VARIANT* pVar)
139 if (!pszPropName || !pVar)
145 static COMPtr<WebDatabaseManager> s_sharedWebDatabaseManager;
147 // WebDatabaseManager --------------------------------------------------------------
148 WebDatabaseManager* WebDatabaseManager::createInstance()
150 WebDatabaseManager* manager = new WebDatabaseManager();
155 WebDatabaseManager::WebDatabaseManager()
159 gClassNameCount.add("WebDatabaseManager");
162 WebDatabaseManager::~WebDatabaseManager()
165 gClassNameCount.remove("WebDatabaseManager");
168 // IUnknown ------------------------------------------------------------------------
169 HRESULT STDMETHODCALLTYPE WebDatabaseManager::QueryInterface(REFIID riid, void** ppvObject)
172 if (IsEqualGUID(riid, IID_IUnknown))
173 *ppvObject = static_cast<WebDatabaseManager*>(this);
174 else if (IsEqualGUID(riid, IID_IWebDatabaseManager))
175 *ppvObject = static_cast<WebDatabaseManager*>(this);
177 return E_NOINTERFACE;
183 ULONG STDMETHODCALLTYPE WebDatabaseManager::AddRef()
188 ULONG STDMETHODCALLTYPE WebDatabaseManager::Release()
190 ULONG newRef = --m_refCount;
197 template<> struct COMVariantSetter<RefPtr<SecurityOrigin> > : COMIUnknownVariantSetter<WebSecurityOrigin, RefPtr<SecurityOrigin> > {};
199 // IWebDatabaseManager -------------------------------------------------------------
200 HRESULT STDMETHODCALLTYPE WebDatabaseManager::sharedWebDatabaseManager(
201 /* [retval][out] */ IWebDatabaseManager** result)
203 if (!s_sharedWebDatabaseManager) {
204 s_sharedWebDatabaseManager.adoptRef(WebDatabaseManager::createInstance());
205 DatabaseTracker::tracker().setClient(s_sharedWebDatabaseManager.get());
208 return s_sharedWebDatabaseManager.copyRefTo(result);
211 HRESULT STDMETHODCALLTYPE WebDatabaseManager::origins(
212 /* [retval][out] */ IEnumVARIANT** result)
219 if (this != s_sharedWebDatabaseManager)
222 Vector<RefPtr<SecurityOrigin> > origins;
223 DatabaseTracker::tracker().origins(origins);
224 COMPtr<COMEnumVariant<Vector<RefPtr<SecurityOrigin> > > > enumVariant(AdoptCOM, COMEnumVariant<Vector<RefPtr<SecurityOrigin> > >::adopt(origins));
226 *result = enumVariant.leakRef();
230 HRESULT STDMETHODCALLTYPE WebDatabaseManager::databasesWithOrigin(
231 /* [in] */ IWebSecurityOrigin* origin,
232 /* [retval][out] */ IEnumVARIANT** result)
234 if (!origin || !result)
239 if (this != s_sharedWebDatabaseManager)
242 COMPtr<WebSecurityOrigin> webSecurityOrigin(Query, origin);
243 if (!webSecurityOrigin)
246 Vector<String> databaseNames;
247 DatabaseTracker::tracker().databaseNamesForOrigin(webSecurityOrigin->securityOrigin(), databaseNames);
249 COMPtr<COMEnumVariant<Vector<String> > > enumVariant(AdoptCOM, COMEnumVariant<Vector<String> >::adopt(databaseNames));
251 *result = enumVariant.leakRef();
255 HRESULT STDMETHODCALLTYPE WebDatabaseManager::detailsForDatabase(
256 /* [in] */ BSTR databaseName,
257 /* [in] */ IWebSecurityOrigin* origin,
258 /* [retval][out] */ IPropertyBag** result)
260 if (!origin || !result)
265 if (this != s_sharedWebDatabaseManager)
268 COMPtr<WebSecurityOrigin> webSecurityOrigin(Query, origin);
269 if (!webSecurityOrigin)
272 DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(String(databaseName, SysStringLen(databaseName)),
273 webSecurityOrigin->securityOrigin());
275 if (details.name().isNull())
278 *result = DatabaseDetailsPropertyBag::createInstance(details);
282 HRESULT STDMETHODCALLTYPE WebDatabaseManager::deleteAllDatabases()
284 if (this != s_sharedWebDatabaseManager)
287 DatabaseTracker::tracker().deleteAllDatabases();
292 HRESULT STDMETHODCALLTYPE WebDatabaseManager::deleteOrigin(
293 /* [in] */ IWebSecurityOrigin* origin)
298 if (this != s_sharedWebDatabaseManager)
301 COMPtr<WebSecurityOrigin> webSecurityOrigin(Query, origin);
302 if (!webSecurityOrigin)
305 DatabaseTracker::tracker().deleteOrigin(webSecurityOrigin->securityOrigin());
310 HRESULT STDMETHODCALLTYPE WebDatabaseManager::deleteDatabase(
311 /* [in] */ BSTR databaseName,
312 /* [in] */ IWebSecurityOrigin* origin)
320 if (this != s_sharedWebDatabaseManager)
323 COMPtr<WebSecurityOrigin> webSecurityOrigin(Query, origin);
324 if (!webSecurityOrigin)
327 DatabaseTracker::tracker().deleteDatabase(webSecurityOrigin->securityOrigin(), String(databaseName, SysStringLen(databaseName)));
332 class DidModifyOriginData {
333 WTF_MAKE_NONCOPYABLE(DidModifyOriginData);
335 static void dispatchToMainThread(WebDatabaseManager* databaseManager, SecurityOrigin* origin)
337 DidModifyOriginData* context = new DidModifyOriginData(databaseManager, origin->isolatedCopy());
338 callOnMainThread(&DidModifyOriginData::dispatchDidModifyOriginOnMainThread, context);
342 DidModifyOriginData(WebDatabaseManager* databaseManager, PassRefPtr<SecurityOrigin> origin)
343 : databaseManager(databaseManager)
348 static void dispatchDidModifyOriginOnMainThread(void* context)
350 ASSERT(isMainThread());
351 DidModifyOriginData* info = static_cast<DidModifyOriginData*>(context);
352 info->databaseManager->dispatchDidModifyOrigin(info->origin.get());
356 WebDatabaseManager* databaseManager;
357 RefPtr<SecurityOrigin> origin;
360 void WebDatabaseManager::dispatchDidModifyOrigin(SecurityOrigin* origin)
362 if (!isMainThread()) {
363 DidModifyOriginData::dispatchToMainThread(this, origin);
367 static BSTR databaseDidModifyOriginName = SysAllocString(WebDatabaseDidModifyOriginNotification);
368 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
370 COMPtr<WebSecurityOrigin> securityOrigin(AdoptCOM, WebSecurityOrigin::createInstance(origin));
371 notifyCenter->postNotificationName(databaseDidModifyOriginName, securityOrigin.get(), 0);
374 HRESULT STDMETHODCALLTYPE WebDatabaseManager::setQuota(
375 /* [in] */ BSTR origin,
376 /* [in] */ unsigned long long quota)
381 if (this != s_sharedWebDatabaseManager)
384 DatabaseTracker::tracker().setQuota(SecurityOrigin::createFromString(origin).get(), quota);
389 void WebDatabaseManager::dispatchDidModifyDatabase(SecurityOrigin* origin, const String& databaseName)
391 if (!isMainThread()) {
392 DidModifyOriginData::dispatchToMainThread(this, origin);
396 static BSTR databaseDidModifyOriginName = SysAllocString(WebDatabaseDidModifyDatabaseNotification);
397 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
399 COMPtr<WebSecurityOrigin> securityOrigin(AdoptCOM, WebSecurityOrigin::createInstance(origin));
401 RetainPtr<CFMutableDictionaryRef> userInfo(AdoptCF, CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
403 static CFStringRef databaseNameKey = MarshallingHelpers::LPCOLESTRToCFStringRef(WebDatabaseNameKey);
404 RetainPtr<CFStringRef> str(AdoptCF, databaseName.createCFString());
405 CFDictionarySetValue(userInfo.get(), databaseNameKey, str.get());
407 COMPtr<CFDictionaryPropertyBag> userInfoBag = CFDictionaryPropertyBag::createInstance();
408 userInfoBag->setDictionary(userInfo.get());
410 notifyCenter->postNotificationName(databaseDidModifyOriginName, securityOrigin.get(), userInfoBag.get());
413 void WebKitInitializeWebDatabasesIfNecessary()
415 static bool initialized = false;
419 WTF::String databasesDirectory = WebCore::pathByAppendingComponent(WebCore::localUserSpecificStorageDirectory(), "Databases");
420 WebCore::DatabaseTracker::initializeTracker(databasesDirectory);