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