Reviewed by Darin.
[WebKit-https.git] / WebKit / win / WebDatabaseManager.cpp
1 /*\r
2  * Copyright (C) 2007 Apple Inc. All rights reserved.\r
3  *\r
4  * Redistribution and use in source and binary forms, with or without\r
5  * modification, are permitted provided that the following conditions\r
6  * are met:\r
7  *\r
8  * 1.  Redistributions of source code must retain the above copyright\r
9  *     notice, this list of conditions and the following disclaimer.\r
10  * 2.  Redistributions in binary form must reproduce the above copyright\r
11  *     notice, this list of conditions and the following disclaimer in the\r
12  *     documentation and/or other materials provided with the distribution.\r
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of\r
14  *     its contributors may be used to endorse or promote products derived\r
15  *     from this software without specific prior written permission.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY\r
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY\r
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  */\r
28 #include "config.h"\r
29 #include "WebDatabaseManager.h"\r
30 #include "WebKitDLL.h"\r
31 \r
32 #include "COMEnumVariant.h"\r
33 #include "WebSecurityOrigin.h"\r
34 \r
35 #include <WebCore/BString.h>\r
36 #include <WebCore/COMPtr.h>\r
37 #include <WebCore/DatabaseTracker.h>\r
38 #include <WebCore/FileSystem.h>\r
39 #include <WebCore/SecurityOriginData.h>\r
40 \r
41 using namespace WebCore;\r
42 \r
43 static COMPtr<WebDatabaseManager> s_sharedWebDatabaseManager;\r
44 \r
45 // WebDatabaseManager --------------------------------------------------------------\r
46 WebDatabaseManager* WebDatabaseManager::createInstance()\r
47 {\r
48     WebDatabaseManager* manager = new WebDatabaseManager();\r
49     manager->AddRef();\r
50     return manager;    \r
51 }\r
52 \r
53 WebDatabaseManager::WebDatabaseManager()\r
54     : m_refCount(0)\r
55 {\r
56     gClassCount++;\r
57 }\r
58 \r
59 WebDatabaseManager::~WebDatabaseManager()\r
60 {\r
61     gClassCount--;\r
62 }\r
63 \r
64 // IUnknown ------------------------------------------------------------------------\r
65 HRESULT STDMETHODCALLTYPE WebDatabaseManager::QueryInterface(REFIID riid, void** ppvObject)\r
66 {\r
67     *ppvObject = 0;\r
68     if (IsEqualGUID(riid, IID_IUnknown))\r
69         *ppvObject = static_cast<WebDatabaseManager*>(this);\r
70     else if (IsEqualGUID(riid, IID_IWebDatabaseManager))\r
71         *ppvObject = static_cast<WebDatabaseManager*>(this);\r
72     else\r
73         return E_NOINTERFACE;\r
74 \r
75     AddRef();\r
76     return S_OK;\r
77 }\r
78 \r
79 ULONG STDMETHODCALLTYPE WebDatabaseManager::AddRef()\r
80 {\r
81     return ++m_refCount;\r
82 }\r
83 \r
84 ULONG STDMETHODCALLTYPE WebDatabaseManager::Release()\r
85 {\r
86     ULONG newRef = --m_refCount;\r
87     if (!newRef)\r
88         delete this;\r
89 \r
90     return newRef;\r
91 }\r
92 \r
93 template<> struct COMVariantSetter<SecurityOriginData> : COMIUnknownVariantSetter<WebSecurityOrigin, SecurityOriginData> {};\r
94 \r
95 // IWebDatabaseManager -------------------------------------------------------------\r
96 HRESULT STDMETHODCALLTYPE WebDatabaseManager::sharedWebDatabaseManager( \r
97     /* [retval][out] */ IWebDatabaseManager** result)\r
98 {\r
99     if (!s_sharedWebDatabaseManager)\r
100         s_sharedWebDatabaseManager.adoptRef(WebDatabaseManager::createInstance());\r
101 \r
102     return s_sharedWebDatabaseManager.copyRefTo(result);\r
103 }\r
104 \r
105 HRESULT STDMETHODCALLTYPE WebDatabaseManager::origins( \r
106     /* [retval][out] */ IEnumVARIANT** result)\r
107 {\r
108     if (!result)\r
109         return E_POINTER;\r
110 \r
111     *result = 0;\r
112 \r
113     if (this != s_sharedWebDatabaseManager)\r
114         return E_FAIL;\r
115 \r
116     Vector<SecurityOriginData> origins;\r
117     DatabaseTracker::tracker().origins(origins);\r
118     COMPtr<COMEnumVariant<Vector<SecurityOriginData> > > enumVariant(AdoptCOM, COMEnumVariant<Vector<SecurityOriginData> >::adopt(origins));\r
119 \r
120     *result = enumVariant.releaseRef();\r
121     return S_OK;\r
122 }\r
123     \r
124 HRESULT STDMETHODCALLTYPE WebDatabaseManager::databasesWithOrigin( \r
125     /* [in] */ IWebSecurityOrigin* origin,\r
126     /* [retval][out] */ IEnumVARIANT** result)\r
127 {\r
128     if (!origin || !result)\r
129         return E_POINTER;\r
130 \r
131     *result = 0;\r
132 \r
133     if (this != s_sharedWebDatabaseManager)\r
134         return E_FAIL;\r
135 \r
136     COMPtr<WebSecurityOrigin> webSecurityOrigin(Query, origin);\r
137     if (!webSecurityOrigin)\r
138         return E_FAIL;\r
139 \r
140     Vector<String> databaseNames;\r
141     DatabaseTracker::tracker().databaseNamesForOrigin(webSecurityOrigin->securityOriginData(), databaseNames);\r
142 \r
143     COMPtr<COMEnumVariant<Vector<String> > > enumVariant(AdoptCOM, COMEnumVariant<Vector<String> >::adopt(databaseNames));\r
144 \r
145     *result = enumVariant.releaseRef();\r
146     return S_OK;\r
147 }\r
148 \r
149 HRESULT STDMETHODCALLTYPE WebDatabaseManager::detailsForDatabaseWithOrigin( \r
150     /* [in] */ BSTR* database,\r
151     /* [in] */ IWebSecurityOrigin* origin,\r
152     /* [retval][out] */ IPropertyBag** result)\r
153 {\r
154     if (!database || !origin || !result)\r
155         return E_POINTER;\r
156 \r
157     *result = 0;\r
158 \r
159     if (this != s_sharedWebDatabaseManager)\r
160         return E_FAIL;\r
161 \r
162     return E_NOTIMPL;\r
163 }\r
164     \r
165 HRESULT STDMETHODCALLTYPE WebDatabaseManager::deleteAllDatabases()\r
166 {\r
167     if (this != s_sharedWebDatabaseManager)\r
168         return E_FAIL;\r
169 \r
170     return E_NOTIMPL;\r
171 }\r
172    \r
173 HRESULT STDMETHODCALLTYPE WebDatabaseManager::deleteDatabasesWithOrigin( \r
174     /* [in] */ IWebSecurityOrigin* origin)\r
175 {\r
176     if (!origin)\r
177         return E_POINTER;\r
178 \r
179     if (this != s_sharedWebDatabaseManager)\r
180         return E_FAIL;\r
181 \r
182     return E_NOTIMPL;\r
183 }\r
184     \r
185 HRESULT STDMETHODCALLTYPE WebDatabaseManager::deleteDatabaseWithOrigin( \r
186     /* [in] */ BSTR* databaseName,\r
187     /* [in] */ IWebSecurityOrigin* origin)\r
188 {\r
189     if (!databaseName || !origin)\r
190         return E_POINTER;\r
191 \r
192     if (this != s_sharedWebDatabaseManager)\r
193         return E_FAIL;\r
194 \r
195     return E_NOTIMPL;\r
196 }\r
197 \r
198 void WebKitSetWebDatabasesPathIfNecessary()\r
199 {\r
200     static bool pathSet = false;\r
201     if (pathSet)\r
202         return;\r
203 \r
204     WebCore::String databasesDirectory = WebCore::pathByAppendingComponent(WebCore::localUserSpecificStorageDirectory(), "Databases");\r
205     WebCore::DatabaseTracker::tracker().setDatabasePath(databasesDirectory);\r
206 \r
207     pathSet = true;\r
208 }\r
209 \r