b4331d45c7dea303d9acfa9ffec6d5da4e1d5bed
[WebKit-https.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, (ASCIILiteral("WebDatabaseManagerOriginKey")));
44     return key;
45 }
46
47 String WebDatabaseManagerProxy::originQuotaKey()
48 {
49     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("WebDatabaseManagerOriginQuotaKey")));
50     return key;
51 }
52
53 String WebDatabaseManagerProxy::originUsageKey()
54 {
55     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("WebDatabaseManagerOriginUsageKey")));
56     return key;
57 }
58
59 String WebDatabaseManagerProxy::databaseDetailsKey()
60 {
61     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("WebDatabaseManagerDatabaseDetailsKey")));
62     return key;
63 }
64
65 String WebDatabaseManagerProxy::databaseDetailsNameKey()
66 {
67     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("WebDatabaseManagerDatabaseDetailsNameKey")));
68     return key;
69 }
70
71 String WebDatabaseManagerProxy::databaseDetailsDisplayNameKey()
72 {
73     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("WebDatabaseManagerDatabaseDetailsDisplayNameKey")));
74     return key;
75 }
76
77 String WebDatabaseManagerProxy::databaseDetailsExpectedUsageKey()
78 {
79     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("WebDatabaseManagerDatabaseDetailsExpectedUsageKey")));
80     return key;
81 }
82
83 String WebDatabaseManagerProxy::databaseDetailsCurrentUsageKey()
84 {
85     DEFINE_STATIC_LOCAL(String, key, (ASCIILiteral("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): <rdar://problem/12239765> 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): <rdar://problem/12239765> 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): <rdar://problem/7855696> 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): <rdar://problem/7855696> 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): <rdar://problem/7855696> Databases shouldn't be stored in the web process.
213     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::DeleteAllDatabases());
214 }
215
216 void WebDatabaseManagerProxy::setQuotaForOrigin(WebSecurityOrigin* origin, uint64_t quota)
217 {
218     // FIXME (Multi-WebProcess): <rdar://problem/7855696> Databases shouldn't be stored in the web process.
219     m_webContext->sendToAllProcessesRelaunchingThemIfNecessary(Messages::WebDatabaseManager::SetQuotaForOrigin(origin->databaseIdentifier(), quota));
220 }
221
222 void WebDatabaseManagerProxy::didModifyOrigin(const String& originIdentifier)
223 {
224     RefPtr<WebSecurityOrigin> origin = WebSecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
225     m_client.didModifyOrigin(this, origin.get());
226 }
227
228 void WebDatabaseManagerProxy::didModifyDatabase(const String& originIdentifier, const String& databaseIdentifier)
229 {
230     RefPtr<WebSecurityOrigin> origin = WebSecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
231     m_client.didModifyDatabase(this, origin.get(), databaseIdentifier);
232 }
233
234 } // namespace WebKit
235
236 #endif // ENABLE(SQL_DATABASE)