[iOS][WK2] Pause/resume database thread when UIProcess enters/leaves the background
[WebKit-https.git] / Source / WebCore / Modules / webdatabase / DatabaseServer.cpp
1 /*
2  * Copyright (C) 2012 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "DatabaseServer.h"
28
29 #include "Database.h"
30 #include "DatabaseBackend.h"
31 #include "DatabaseContext.h"
32 #include "DatabaseTracker.h"
33
34 namespace WebCore {
35
36 void DatabaseServer::initialize(const String& databasePath)
37 {
38     DatabaseTracker::initializeTracker(databasePath);
39 }
40
41 void DatabaseServer::setClient(DatabaseManagerClient* client)
42 {
43     DatabaseTracker::tracker().setClient(client);
44 }
45
46 String DatabaseServer::databaseDirectoryPath() const
47 {
48     return DatabaseTracker::tracker().databaseDirectoryPath();
49 }
50
51 void DatabaseServer::setDatabaseDirectoryPath(const String& path)
52 {
53     DatabaseTracker::tracker().setDatabaseDirectoryPath(path);
54 }
55
56 String DatabaseServer::fullPathForDatabase(SecurityOrigin* origin, const String& name, bool createIfDoesNotExist)
57 {
58     return DatabaseTracker::tracker().fullPathForDatabase(origin, name, createIfDoesNotExist);
59 }
60
61 bool DatabaseServer::hasEntryForOrigin(SecurityOrigin* origin)
62 {
63     return DatabaseTracker::tracker().hasEntryForOrigin(origin);
64 }
65
66 void DatabaseServer::origins(Vector<RefPtr<SecurityOrigin>>& result)
67 {
68     DatabaseTracker::tracker().origins(result);
69 }
70
71 bool DatabaseServer::databaseNamesForOrigin(SecurityOrigin* origin, Vector<String>& result)
72 {
73     return DatabaseTracker::tracker().databaseNamesForOrigin(origin, result);
74 }
75
76 DatabaseDetails DatabaseServer::detailsForNameAndOrigin(const String& name, SecurityOrigin* origin)
77 {
78     return DatabaseTracker::tracker().detailsForNameAndOrigin(name, origin);
79 }
80
81 unsigned long long DatabaseServer::usageForOrigin(SecurityOrigin* origin)
82 {
83     return DatabaseTracker::tracker().usageForOrigin(origin);
84 }
85
86 unsigned long long DatabaseServer::quotaForOrigin(SecurityOrigin* origin)
87 {
88     return DatabaseTracker::tracker().quotaForOrigin(origin);
89 }
90
91 void DatabaseServer::setQuota(SecurityOrigin* origin, unsigned long long quotaSize)
92 {
93     DatabaseTracker::tracker().setQuota(origin, quotaSize);
94 }
95
96 void DatabaseServer::deleteAllDatabases()
97 {
98     DatabaseTracker::tracker().deleteAllDatabases();
99 }
100
101 bool DatabaseServer::deleteOrigin(SecurityOrigin* origin)
102 {
103     return DatabaseTracker::tracker().deleteOrigin(origin);
104 }
105
106 bool DatabaseServer::deleteDatabase(SecurityOrigin* origin, const String& name)
107 {
108     return DatabaseTracker::tracker().deleteDatabase(origin, name);
109 }
110
111 void DatabaseServer::setPauseAllDatabases(bool pauseAllDatabases)
112 {
113 #if PLATFORM(IOS)
114     DatabaseTracker::tracker().setDatabasesPaused(pauseAllDatabases);
115 #else
116     UNUSED_PARAM(pauseAllDatabases);
117 #endif
118 }
119
120 void DatabaseServer::interruptAllDatabasesForContext(const DatabaseContext* context)
121 {
122     DatabaseTracker::tracker().interruptAllDatabasesForContext(context);
123 }
124
125 PassRefPtr<DatabaseBackendBase> DatabaseServer::openDatabase(RefPtr<DatabaseContext>& backendContext, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, bool setVersionInNewDatabase, DatabaseError &error, String& errorMessage,
126     OpenAttempt attempt)
127 {
128     RefPtr<DatabaseBackendBase> database;
129     bool success = false; // Make some older compilers happy.
130     
131     switch (attempt) {
132     case FirstTryToOpenDatabase:
133         success = DatabaseTracker::tracker().canEstablishDatabase(backendContext.get(), name, estimatedSize, error);
134         break;
135     case RetryOpenDatabase:
136         success = DatabaseTracker::tracker().retryCanEstablishDatabase(backendContext.get(), name, estimatedSize, error);
137     }
138
139     if (success)
140         database = createDatabase(backendContext, name, expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
141     return database.release();
142 }
143
144 PassRefPtr<DatabaseBackendBase> DatabaseServer::createDatabase(RefPtr<DatabaseContext>& backendContext, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, bool setVersionInNewDatabase, DatabaseError& error, String& errorMessage)
145 {
146     RefPtr<Database> database = adoptRef(new Database(backendContext, name, expectedVersion, displayName, estimatedSize));
147
148     if (!database->openAndVerifyVersion(setVersionInNewDatabase, error, errorMessage))
149         return 0;
150
151     DatabaseTracker::tracker().setDatabaseDetails(backendContext->securityOrigin(), name, displayName, estimatedSize);
152     return database.release();
153 }
154
155 } // namespace WebCore