Merge "Use EwkView's variables instead of drawingScaleFactor and drawingScrollPositio...
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / WebDatabaseManagerProxy.cpp
1 /*
2  * Copyright (C) 2010 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebDatabaseManagerProxy.h"
28
29 #if ENABLE(SQL_DATABASE)
30
31 #include "ImmutableArray.h"
32 #include "ImmutableDictionary.h"
33 #include "WebDatabaseManagerMessages.h"
34 #include "WebContext.h"
35 #include "WebSecurityOrigin.h"
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 String WebDatabaseManagerProxy::originKey()
42 {
43     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerOriginKey"));
44     return key;
45 }
46
47 String WebDatabaseManagerProxy::originQuotaKey()
48 {
49     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerOriginQuotaKey"));
50     return key;
51 }
52
53 String WebDatabaseManagerProxy::originUsageKey()
54 {
55     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerOriginUsageKey"));
56     return key;
57 }
58
59 String WebDatabaseManagerProxy::databaseDetailsKey()
60 {
61     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerDatabaseDetailsKey"));
62     return key;
63 }
64
65 String WebDatabaseManagerProxy::databaseDetailsNameKey()
66 {
67     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerDatabaseDetailsNameKey"));
68     return key;
69 }
70
71 String WebDatabaseManagerProxy::databaseDetailsDisplayNameKey()
72 {
73     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerDatabaseDetailsDisplayNameKey"));
74     return key;
75 }
76
77 String WebDatabaseManagerProxy::databaseDetailsExpectedUsageKey()
78 {
79     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerDatabaseDetailsExpectedUsageKey"));
80     return key;
81 }
82
83 String WebDatabaseManagerProxy::databaseDetailsCurrentUsageKey()
84 {
85     DEFINE_STATIC_LOCAL(String, key, ("WebDatabaseManagerDatabaseDetailsCurrentUsageKey"));
86     return key;
87 }
88
89 PassRefPtr<WebDatabaseManagerProxy> WebDatabaseManagerProxy::create(WebContext* webContext)
90 {
91     return adoptRef(new WebDatabaseManagerProxy(webContext));
92 }
93
94 WebDatabaseManagerProxy::WebDatabaseManagerProxy(WebContext* webContext)
95     : m_webContext(webContext)
96 {
97 }
98
99 WebDatabaseManagerProxy::~WebDatabaseManagerProxy()
100 {
101 }
102
103 void WebDatabaseManagerProxy::invalidate()
104 {
105     invalidateCallbackMap(m_arrayCallbacks);
106 }
107
108 bool WebDatabaseManagerProxy::shouldTerminate(WebProcessProxy*) const
109 {
110     return m_arrayCallbacks.isEmpty();
111 }
112
113 void WebDatabaseManagerProxy::initializeClient(const WKDatabaseManagerClient* client)
114 {
115     m_client.initialize(client);
116 }
117
118 void WebDatabaseManagerProxy::getDatabasesByOrigin(PassRefPtr<ArrayCallback> prpCallback)
119 {
120     RefPtr<ArrayCallback> callback = prpCallback;
121     uint64_t callbackID = callback->callbackID();
122     m_arrayCallbacks.set(callbackID, callback.release());
123
124     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
125     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::GetDatabasesByOrigin(callbackID));
126 }
127
128 void WebDatabaseManagerProxy::didGetDatabasesByOrigin(const Vector<OriginAndDatabases>& originAndDatabasesVector, uint64_t callbackID)
129 {
130     RefPtr<ArrayCallback> callback = m_arrayCallbacks.take(callbackID);
131     if (!callback) {
132         // FIXME: Log error or assert.
133         return;
134     }
135
136     size_t originAndDatabasesCount = originAndDatabasesVector.size();
137     Vector<RefPtr<APIObject> > result(originAndDatabasesCount);
138
139     for (size_t i = 0; i < originAndDatabasesCount; ++i) {
140         const OriginAndDatabases& originAndDatabases = originAndDatabasesVector[i];
141     
142         RefPtr<APIObject> origin = WebSecurityOrigin::createFromDatabaseIdentifier(originAndDatabases.originIdentifier);
143     
144         size_t databasesCount = originAndDatabases.databases.size();
145         Vector<RefPtr<APIObject> > databases(databasesCount);
146     
147         for (size_t j = 0; j < databasesCount; ++j) {
148             const DatabaseDetails& details = originAndDatabases.databases[i];
149             HashMap<String, RefPtr<APIObject> > detailsMap;
150
151             detailsMap.set(databaseDetailsNameKey(), WebString::create(details.name()));
152             detailsMap.set(databaseDetailsDisplayNameKey(), WebString::create(details.displayName()));
153             detailsMap.set(databaseDetailsExpectedUsageKey(), WebUInt64::create(details.expectedUsage()));
154             detailsMap.set(databaseDetailsCurrentUsageKey(), WebUInt64::create(details.currentUsage()));
155             databases.append(ImmutableDictionary::adopt(detailsMap));
156         }
157
158         HashMap<String, RefPtr<APIObject> > originAndDatabasesMap;
159         originAndDatabasesMap.set(originKey(), origin);
160         originAndDatabasesMap.set(originQuotaKey(), WebUInt64::create(originAndDatabases.originQuota));
161         originAndDatabasesMap.set(originUsageKey(), WebUInt64::create(originAndDatabases.originUsage));
162         originAndDatabasesMap.set(databaseDetailsKey(), ImmutableArray::adopt(databases));
163
164         result.append(ImmutableDictionary::adopt(originAndDatabasesMap));
165     }
166
167     RefPtr<ImmutableArray> resultArray = ImmutableArray::adopt(result);
168     callback->performCallbackWithReturnValue(resultArray.get());
169 }
170
171 void WebDatabaseManagerProxy::getDatabaseOrigins(PassRefPtr<ArrayCallback> prpCallback)
172 {
173     RefPtr<ArrayCallback> callback = prpCallback;
174     uint64_t callbackID = callback->callbackID();
175     m_arrayCallbacks.set(callbackID, callback.release());
176
177     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
178     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::GetDatabaseOrigins(callbackID));
179 }
180
181 void WebDatabaseManagerProxy::didGetDatabaseOrigins(const Vector<String>& originIdentifiers, uint64_t callbackID)
182 {
183     RefPtr<ArrayCallback> callback = m_arrayCallbacks.take(callbackID);
184     if (!callback) {
185         // FIXME: Log error or assert.
186         return;
187     }
188
189     size_t originIdentifiersCount = originIdentifiers.size();
190     Vector<RefPtr<APIObject> > securityOrigins(originIdentifiersCount);
191
192     for (size_t i = 0; i < originIdentifiersCount; ++i)
193         securityOrigins[i] = WebSecurityOrigin::createFromDatabaseIdentifier(originIdentifiers[i]);
194
195     callback->performCallbackWithReturnValue(ImmutableArray::adopt(securityOrigins).get());
196 }
197
198 void WebDatabaseManagerProxy::deleteDatabaseWithNameForOrigin(const String& databaseIdentifier, WebSecurityOrigin* origin)
199 {
200     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
201     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::DeleteDatabaseWithNameForOrigin(databaseIdentifier, origin->databaseIdentifier()));
202 }
203
204 void WebDatabaseManagerProxy::deleteDatabasesForOrigin(WebSecurityOrigin* origin)
205 {
206     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
207     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::DeleteDatabasesForOrigin(origin->databaseIdentifier()));
208 }
209
210 void WebDatabaseManagerProxy::deleteAllDatabases()
211 {
212     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
213     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::DeleteAllDatabases());
214 }
215
216 #if ENABLE(TIZEN_SQL_DATABASE)
217 void WebDatabaseManagerProxy::getDatabasePath(PassRefPtr<DatabaseStringCallback> prpCallback)
218 {
219     RefPtr<DatabaseStringCallback> callback = prpCallback;
220     uint64_t callbackID = callback->callbackID();
221     m_databaseStringCallbacks.set(callbackID, callback.release());
222
223     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
224     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::GetDatabasePath(callbackID));
225 }
226
227 void WebDatabaseManagerProxy::didGetDatabasePath(const String& path, uint64_t callbackID)
228 {
229     RefPtr<DatabaseStringCallback> callback = m_databaseStringCallbacks.take(callbackID);
230     if (!callback) {
231         // FIXME: Log error or assert.
232         return;
233     }
234
235     m_databaseStringCallbacks.remove(callbackID);
236     callback->performCallbackWithReturnValue(path.impl());
237 }
238
239 void WebDatabaseManagerProxy::getQuotaForOrigin(PassRefPtr<DatabaseUInt64Callback> prpCallback, WebSecurityOrigin* origin)
240 {
241     RefPtr<DatabaseUInt64Callback> callback = prpCallback;
242     uint64_t callbackID = callback->callbackID();
243     m_databaseUInt64Callbacks.set(callbackID, callback.release());
244
245     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
246     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::GetQuotaForOrigin(callbackID, origin->databaseIdentifier()));
247 }
248
249 void WebDatabaseManagerProxy::didGetQuotaForOrigin(const uint64_t quota, uint64_t callbackID)
250 {
251     RefPtr<DatabaseUInt64Callback> callback = m_databaseUInt64Callbacks.take(callbackID);
252     if (!callback) {
253         // FIXME: Log error or assert.
254         return;
255     }
256
257     m_databaseUInt64Callbacks.remove(callbackID);
258     callback->performCallbackWithReturnValue(WebUInt64::create(quota).leakRef());
259 }
260
261 void WebDatabaseManagerProxy::getUsageForOrigin(PassRefPtr<DatabaseUInt64Callback> prpCallback, WebSecurityOrigin* origin)
262 {
263     RefPtr<DatabaseUInt64Callback> callback = prpCallback;
264     uint64_t callbackID = callback->callbackID();
265     m_databaseUInt64Callbacks.set(callbackID, callback.release());
266
267     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
268     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::GetUsageForOrigin(callbackID, origin->databaseIdentifier()));
269 }
270
271 void WebDatabaseManagerProxy::didGetUsageForOrigin(const uint64_t usage, uint64_t callbackID)
272 {
273     RefPtr<DatabaseUInt64Callback> callback = m_databaseUInt64Callbacks.take(callbackID);
274     if (!callback) {
275         // FIXME: Log error or assert.
276         return;
277     }
278
279     callback->performCallbackWithReturnValue(WebUInt64::create(usage).leakRef());
280 }
281 #endif
282
283 void WebDatabaseManagerProxy::setQuotaForOrigin(WebSecurityOrigin* origin, uint64_t quota)
284 {
285     // FIXME (Multi-WebProcess): Databases shouldn't be stored in the web process.
286     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::SetQuotaForOrigin(origin->databaseIdentifier(), quota));
287 }
288
289 void WebDatabaseManagerProxy::didModifyOrigin(const String& originIdentifier)
290 {
291     RefPtr<WebSecurityOrigin> origin = WebSecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
292     m_client.didModifyOrigin(this, origin.get());
293 }
294
295 void WebDatabaseManagerProxy::didModifyDatabase(const String& originIdentifier, const String& databaseIdentifier)
296 {
297     RefPtr<WebSecurityOrigin> origin = WebSecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
298     m_client.didModifyDatabase(this, origin.get(), databaseIdentifier);
299 }
300
301 } // namespace WebKit
302
303 #endif // ENABLE(SQL_DATABASE)