Replace uses of ArgumentEncoder/ArgumentDecoder with MessageEncoder/MessageDecoder
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebCoreSupport / WebDatabaseManager.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 "WebDatabaseManager.h"
28
29 #if ENABLE(SQL_DATABASE)
30
31 #include "Connection.h"
32 #include "MessageID.h"
33 #include "OriginAndDatabases.h"
34 #include "WebCoreArgumentCoders.h"
35 #include "WebDatabaseManagerProxyMessages.h"
36 #include "WebProcess.h"
37 #include <WebCore/DatabaseDetails.h>
38 #include <WebCore/DatabaseTracker.h>
39 #include <WebCore/SecurityOrigin.h>
40
41 using namespace WebCore;
42
43 namespace WebKit {
44
45 WebDatabaseManager& WebDatabaseManager::shared()
46 {
47     static WebDatabaseManager& shared = *new WebDatabaseManager;
48     return shared;
49 }
50
51 void WebDatabaseManager::initialize(const String& databaseDirectory)
52 {
53     DatabaseTracker::initializeTracker(databaseDirectory);
54 }
55
56 WebDatabaseManager::WebDatabaseManager()
57 {
58     DatabaseTracker::tracker().setClient(this);
59 }
60
61 WebDatabaseManager::~WebDatabaseManager()
62 {
63 }
64
65 void WebDatabaseManager::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::MessageDecoder& decoder)
66 {
67     didReceiveWebDatabaseManagerMessage(connection, messageID, decoder);
68 }
69
70 void WebDatabaseManager::getDatabasesByOrigin(uint64_t callbackID) const
71 {
72     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
73
74     // FIXME: This could be made more efficient by adding a function to DatabaseTracker
75     // to get both the origins and the Vector of DatabaseDetails for each origin in one
76     // shot.  That would avoid taking the numerous locks this requires.
77
78     Vector<RefPtr<SecurityOrigin> > origins;
79     DatabaseTracker::tracker().origins(origins);
80
81     Vector<OriginAndDatabases> originAndDatabasesVector;
82     originAndDatabasesVector.reserveInitialCapacity(origins.size());
83
84     for (size_t i = 0; i < origins.size(); ++i) {
85         OriginAndDatabases originAndDatabases;
86
87         Vector<String> nameVector;
88         if (!DatabaseTracker::tracker().databaseNamesForOrigin(origins[i].get(), nameVector))
89             continue;
90
91         Vector<DatabaseDetails> detailsVector;
92         detailsVector.reserveInitialCapacity(nameVector.size());
93         for (size_t j = 0; j < nameVector.size(); j++) {
94             DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(nameVector[j], origins[i].get());
95             if (details.name().isNull())
96                 continue;
97
98             detailsVector.append(details);
99         }
100
101         if (detailsVector.isEmpty())
102             continue;
103
104         originAndDatabases.originIdentifier = origins[i]->databaseIdentifier();
105         originAndDatabases.originQuota = DatabaseTracker::tracker().quotaForOrigin(origins[i].get());
106         originAndDatabases.originUsage = DatabaseTracker::tracker().usageForOrigin(origins[i].get());
107         originAndDatabases.databases.swap(detailsVector); 
108         originAndDatabasesVector.append(originAndDatabases);
109     }
110
111     WebProcess::shared().connection()->send(Messages::WebDatabaseManagerProxy::DidGetDatabasesByOrigin(originAndDatabasesVector, callbackID), 0);
112 }
113
114 void WebDatabaseManager::getDatabaseOrigins(uint64_t callbackID) const
115 {
116     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
117
118     Vector<RefPtr<SecurityOrigin> > origins;
119     DatabaseTracker::tracker().origins(origins);
120
121     size_t numOrigins = origins.size();
122
123     Vector<String> identifiers(numOrigins);
124     for (size_t i = 0; i < numOrigins; ++i)
125         identifiers[i] = origins[i]->databaseIdentifier();
126     WebProcess::shared().connection()->send(Messages::WebDatabaseManagerProxy::DidGetDatabaseOrigins(identifiers, callbackID), 0);
127 }
128
129 void WebDatabaseManager::deleteDatabaseWithNameForOrigin(const String& databaseIdentifier, const String& originIdentifier) const
130 {
131     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
132
133     RefPtr<SecurityOrigin> origin = SecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
134     if (!origin)
135         return;
136
137     DatabaseTracker::tracker().deleteDatabase(origin.get(), databaseIdentifier);
138 }
139
140 void WebDatabaseManager::deleteDatabasesForOrigin(const String& originIdentifier) const
141 {
142     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
143
144     RefPtr<SecurityOrigin> origin = SecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
145     if (!origin)
146         return;
147
148     DatabaseTracker::tracker().deleteOrigin(origin.get());
149 }
150
151 void WebDatabaseManager::deleteAllDatabases() const
152 {
153     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
154
155     DatabaseTracker::tracker().deleteAllDatabases();
156 }
157
158 void WebDatabaseManager::setQuotaForOrigin(const String& originIdentifier, unsigned long long quota) const
159 {
160     WebProcess::LocalTerminationDisabler terminationDisabler(WebProcess::shared());
161
162     // If the quota is set to a value lower than the current usage, that quota will
163     // "stick" but no data will be purged to meet the new quota. This will simply
164     // prevent new data from being added to databases in that origin.
165
166     RefPtr<SecurityOrigin> origin = SecurityOrigin::createFromDatabaseIdentifier(originIdentifier);
167     if (!origin)
168         return;
169
170     DatabaseTracker::tracker().setQuota(origin.get(), quota);
171 }
172
173 void WebDatabaseManager::dispatchDidModifyOrigin(SecurityOrigin* origin)
174 {
175     // NOTE: This may be called on a non-main thread.
176     WebProcess::shared().connection()->send(Messages::WebDatabaseManagerProxy::DidModifyOrigin(origin->databaseIdentifier()), 0);
177 }
178
179 void WebDatabaseManager::dispatchDidModifyDatabase(WebCore::SecurityOrigin* origin, const String& databaseIdentifier)
180 {
181     // NOTE: This may be called on a non-main thread.
182     WebProcess::shared().connection()->send(Messages::WebDatabaseManagerProxy::DidModifyDatabase(origin->databaseIdentifier(), databaseIdentifier), 0);
183 }
184
185 } // namespace WebKit
186
187 #endif // ENABLE(SQL_DATABASE)