Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / DatabaseManager.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 "modules/webdatabase/DatabaseManager.h"
28
29 #include "bindings/core/v8/ExceptionMessages.h"
30 #include "bindings/core/v8/ExceptionState.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/dom/ExecutionContext.h"
33 #include "core/dom/ExecutionContextTask.h"
34 #include "core/inspector/ConsoleMessage.h"
35 #include "platform/Logging.h"
36 #include "modules/webdatabase/AbstractDatabaseServer.h"
37 #include "modules/webdatabase/Database.h"
38 #include "modules/webdatabase/DatabaseBackend.h"
39 #include "modules/webdatabase/DatabaseBackendBase.h"
40 #include "modules/webdatabase/DatabaseBackendSync.h"
41 #include "modules/webdatabase/DatabaseCallback.h"
42 #include "modules/webdatabase/DatabaseClient.h"
43 #include "modules/webdatabase/DatabaseContext.h"
44 #include "modules/webdatabase/DatabaseServer.h"
45 #include "modules/webdatabase/DatabaseSync.h"
46 #include "modules/webdatabase/DatabaseTask.h"
47 #include "platform/weborigin/SecurityOrigin.h"
48
49 namespace blink {
50
51 DatabaseManager& DatabaseManager::manager()
52 {
53     AtomicallyInitializedStatic(DatabaseManager*, dbManager = new DatabaseManager);
54     return *dbManager;
55 }
56
57 DatabaseManager::DatabaseManager()
58 #if ENABLE(ASSERT)
59     : m_databaseContextRegisteredCount(0)
60     , m_databaseContextInstanceCount(0)
61 #endif
62 {
63     m_server = new DatabaseServer;
64     ASSERT(m_server); // We should always have a server to work with.
65 }
66
67 DatabaseManager::~DatabaseManager()
68 {
69 }
70
71 class DatabaseCreationCallbackTask FINAL : public ExecutionContextTask {
72 public:
73     static PassOwnPtr<DatabaseCreationCallbackTask> create(PassRefPtrWillBeRawPtr<Database> database, PassOwnPtr<DatabaseCallback> creationCallback)
74     {
75         return adoptPtr(new DatabaseCreationCallbackTask(database, creationCallback));
76     }
77
78     virtual void performTask(ExecutionContext*) OVERRIDE
79     {
80         m_creationCallback->handleEvent(m_database.get());
81     }
82
83 private:
84     DatabaseCreationCallbackTask(PassRefPtrWillBeRawPtr<Database> database, PassOwnPtr<DatabaseCallback> callback)
85         : m_database(database)
86         , m_creationCallback(callback)
87     {
88     }
89
90     RefPtrWillBePersistent<Database> m_database;
91     OwnPtr<DatabaseCallback> m_creationCallback;
92 };
93
94 DatabaseContext* DatabaseManager::existingDatabaseContextFor(ExecutionContext* context)
95 {
96     MutexLocker locker(m_contextMapLock);
97
98     ASSERT(m_databaseContextRegisteredCount >= 0);
99     ASSERT(m_databaseContextInstanceCount >= 0);
100     ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
101 #if ENABLE(OILPAN)
102     const Persistent<DatabaseContext>* databaseContext = m_contextMap.get(context);
103     return databaseContext ? databaseContext->get() : 0;
104 #else
105     return m_contextMap.get(context);
106 #endif
107 }
108
109 DatabaseContext* DatabaseManager::databaseContextFor(ExecutionContext* context)
110 {
111     if (DatabaseContext* databaseContext = existingDatabaseContextFor(context))
112         return databaseContext;
113     // We don't need to hold a reference returned by DatabaseContext::create
114     // because DatabaseContext::create calls registerDatabaseContext, and the
115     // DatabaseManager holds a reference.
116     return DatabaseContext::create(context).get();
117 }
118
119 void DatabaseManager::registerDatabaseContext(DatabaseContext* databaseContext)
120 {
121     MutexLocker locker(m_contextMapLock);
122     ExecutionContext* context = databaseContext->executionContext();
123 #if ENABLE(OILPAN)
124     m_contextMap.set(context, adoptPtr(new Persistent<DatabaseContext>(databaseContext)));
125 #else
126     m_contextMap.set(context, databaseContext);
127 #endif
128 #if ENABLE(ASSERT)
129     m_databaseContextRegisteredCount++;
130 #endif
131 }
132
133 void DatabaseManager::unregisterDatabaseContext(DatabaseContext* databaseContext)
134 {
135     MutexLocker locker(m_contextMapLock);
136     ExecutionContext* context = databaseContext->executionContext();
137     ASSERT(m_contextMap.get(context));
138 #if ENABLE(ASSERT)
139     m_databaseContextRegisteredCount--;
140 #endif
141     m_contextMap.remove(context);
142 }
143
144 #if ENABLE(ASSERT)
145 void DatabaseManager::didConstructDatabaseContext()
146 {
147     MutexLocker lock(m_contextMapLock);
148     m_databaseContextInstanceCount++;
149 }
150
151 void DatabaseManager::didDestructDatabaseContext()
152 {
153     MutexLocker lock(m_contextMapLock);
154     m_databaseContextInstanceCount--;
155     ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
156 }
157 #endif
158
159 void DatabaseManager::throwExceptionForDatabaseError(DatabaseError error, const String& errorMessage, ExceptionState& exceptionState)
160 {
161     switch (error) {
162     case DatabaseError::None:
163         return;
164     case DatabaseError::GenericSecurityError:
165         exceptionState.throwSecurityError(errorMessage);
166         return;
167     case DatabaseError::InvalidDatabaseState:
168         exceptionState.throwDOMException(InvalidStateError, errorMessage);
169         return;
170     default:
171         ASSERT_NOT_REACHED();
172     }
173 }
174
175 static void logOpenDatabaseError(ExecutionContext* context, const String& name)
176 {
177     WTF_LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(),
178         context->securityOrigin()->toString().ascii().data());
179 }
180
181 PassRefPtrWillBeRawPtr<DatabaseBackendBase> DatabaseManager::openDatabaseBackend(ExecutionContext* context,
182     DatabaseType type, const String& name, const String& expectedVersion, const String& displayName,
183     unsigned long estimatedSize, bool setVersionInNewDatabase, DatabaseError& error, String& errorMessage)
184 {
185     ASSERT(error == DatabaseError::None);
186
187     RefPtrWillBeRawPtr<DatabaseBackendBase> backend = m_server->openDatabase(
188         databaseContextFor(context)->backend(), type, name, expectedVersion,
189         displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
190
191     if (!backend) {
192         ASSERT(error != DatabaseError::None);
193
194         switch (error) {
195         case DatabaseError::GenericSecurityError:
196             logOpenDatabaseError(context, name);
197             return nullptr;
198
199         case DatabaseError::InvalidDatabaseState:
200             logErrorMessage(context, errorMessage);
201             return nullptr;
202
203         default:
204             ASSERT_NOT_REACHED();
205         }
206     }
207
208     return backend.release();
209 }
210
211 PassRefPtrWillBeRawPtr<Database> DatabaseManager::openDatabase(ExecutionContext* context,
212     const String& name, const String& expectedVersion, const String& displayName,
213     unsigned long estimatedSize, PassOwnPtr<DatabaseCallback> creationCallback,
214     DatabaseError& error, String& errorMessage)
215 {
216     ASSERT(error == DatabaseError::None);
217
218     bool setVersionInNewDatabase = !creationCallback;
219     RefPtrWillBeRawPtr<DatabaseBackendBase> backend = openDatabaseBackend(context, DatabaseType::Async, name,
220         expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
221     if (!backend)
222         return nullptr;
223
224     RefPtrWillBeRawPtr<Database> database = Database::create(context, backend);
225
226     databaseContextFor(context)->setHasOpenDatabases();
227     DatabaseClient::from(context)->didOpenDatabase(database, context->securityOrigin()->host(), name, expectedVersion);
228
229     if (backend->isNew() && creationCallback.get()) {
230         WTF_LOG(StorageAPI, "Scheduling DatabaseCreationCallbackTask for database %p\n", database.get());
231         database->executionContext()->postTask(DatabaseCreationCallbackTask::create(database, creationCallback));
232     }
233
234     ASSERT(database);
235     return database.release();
236 }
237
238 PassRefPtrWillBeRawPtr<DatabaseSync> DatabaseManager::openDatabaseSync(ExecutionContext* context,
239     const String& name, const String& expectedVersion, const String& displayName,
240     unsigned long estimatedSize, PassOwnPtr<DatabaseCallback> creationCallback,
241     DatabaseError& error, String& errorMessage)
242 {
243     ASSERT(context->isContextThread());
244     ASSERT(error == DatabaseError::None);
245
246     bool setVersionInNewDatabase = !creationCallback;
247     RefPtrWillBeRawPtr<DatabaseBackendBase> backend = openDatabaseBackend(context, DatabaseType::Sync, name,
248         expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
249     if (!backend)
250         return nullptr;
251
252     RefPtrWillBeRawPtr<DatabaseSync> database = DatabaseSync::create(context, backend);
253
254     if (backend->isNew() && creationCallback.get()) {
255         WTF_LOG(StorageAPI, "Invoking the creation callback for database %p\n", database.get());
256         creationCallback->handleEvent(database.get());
257     }
258
259     ASSERT(database);
260     return database.release();
261 }
262
263 String DatabaseManager::fullPathForDatabase(SecurityOrigin* origin, const String& name, bool createIfDoesNotExist)
264 {
265     return m_server->fullPathForDatabase(origin, name, createIfDoesNotExist);
266 }
267
268 void DatabaseManager::closeDatabasesImmediately(const String& originIdentifier, const String& name)
269 {
270     m_server->closeDatabasesImmediately(originIdentifier, name);
271 }
272
273 void DatabaseManager::interruptAllDatabasesForContext(DatabaseContext* databaseContext)
274 {
275     m_server->interruptAllDatabasesForContext(databaseContext->backend());
276 }
277
278 void DatabaseManager::logErrorMessage(ExecutionContext* context, const String& message)
279 {
280     context->addConsoleMessage(ConsoleMessage::create(StorageMessageSource, ErrorMessageLevel, message));
281 }
282
283 } // namespace blink