2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "modules/webdatabase/DatabaseManager.h"
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 "modules/webdatabase/Database.h"
36 #include "modules/webdatabase/DatabaseCallback.h"
37 #include "modules/webdatabase/DatabaseClient.h"
38 #include "modules/webdatabase/DatabaseContext.h"
39 #include "modules/webdatabase/DatabaseTask.h"
40 #include "modules/webdatabase/DatabaseTracker.h"
41 #include "platform/Logging.h"
42 #include "platform/weborigin/SecurityOrigin.h"
43 #include "wtf/MainThread.h"
47 DatabaseManager& DatabaseManager::manager()
49 ASSERT(isMainThread());
50 DEFINE_STATIC_LOCAL(DatabaseManager, dbManager, ());
54 DatabaseManager::DatabaseManager()
56 : m_databaseContextRegisteredCount(0)
57 , m_databaseContextInstanceCount(0)
62 DatabaseManager::~DatabaseManager()
66 class DatabaseCreationCallbackTask FINAL : public ExecutionContextTask {
68 static PassOwnPtr<DatabaseCreationCallbackTask> create(PassRefPtrWillBeRawPtr<Database> database, DatabaseCallback* creationCallback)
70 return adoptPtr(new DatabaseCreationCallbackTask(database, creationCallback));
73 virtual void performTask(ExecutionContext*) OVERRIDE
75 m_creationCallback->handleEvent(m_database.get());
79 DatabaseCreationCallbackTask(PassRefPtrWillBeRawPtr<Database> database, DatabaseCallback* callback)
80 : m_database(database)
81 , m_creationCallback(callback)
85 RefPtrWillBePersistent<Database> m_database;
86 Persistent<DatabaseCallback> m_creationCallback;
89 DatabaseContext* DatabaseManager::existingDatabaseContextFor(ExecutionContext* context)
91 ASSERT(m_databaseContextRegisteredCount >= 0);
92 ASSERT(m_databaseContextInstanceCount >= 0);
93 ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
94 return m_contextMap.get(context);
97 DatabaseContext* DatabaseManager::databaseContextFor(ExecutionContext* context)
99 if (DatabaseContext* databaseContext = existingDatabaseContextFor(context))
100 return databaseContext;
101 // We don't need to hold a reference returned by DatabaseContext::create
102 // because DatabaseContext::create calls registerDatabaseContext, and the
103 // DatabaseManager holds a reference.
104 return DatabaseContext::create(context).get();
107 void DatabaseManager::registerDatabaseContext(DatabaseContext* databaseContext)
109 ExecutionContext* context = databaseContext->executionContext();
110 m_contextMap.set(context, databaseContext);
112 m_databaseContextRegisteredCount++;
116 void DatabaseManager::unregisterDatabaseContext(DatabaseContext* databaseContext)
118 ExecutionContext* context = databaseContext->executionContext();
119 ASSERT(m_contextMap.get(context));
121 m_databaseContextRegisteredCount--;
123 m_contextMap.remove(context);
127 void DatabaseManager::didConstructDatabaseContext()
129 m_databaseContextInstanceCount++;
132 void DatabaseManager::didDestructDatabaseContext()
134 m_databaseContextInstanceCount--;
135 ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
139 void DatabaseManager::throwExceptionForDatabaseError(DatabaseError error, const String& errorMessage, ExceptionState& exceptionState)
142 case DatabaseError::None:
144 case DatabaseError::GenericSecurityError:
145 exceptionState.throwSecurityError(errorMessage);
147 case DatabaseError::InvalidDatabaseState:
148 exceptionState.throwDOMException(InvalidStateError, errorMessage);
151 ASSERT_NOT_REACHED();
155 static void logOpenDatabaseError(ExecutionContext* context, const String& name)
157 WTF_LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(),
158 context->securityOrigin()->toString().ascii().data());
161 PassRefPtrWillBeRawPtr<Database> DatabaseManager::openDatabaseInternal(ExecutionContext* context,
162 const String& name, const String& expectedVersion, const String& displayName,
163 unsigned long estimatedSize, bool setVersionInNewDatabase, DatabaseError& error, String& errorMessage)
165 ASSERT(error == DatabaseError::None);
167 DatabaseContext* backendContext = databaseContextFor(context)->backend();
168 if (DatabaseTracker::tracker().canEstablishDatabase(backendContext, name, displayName, estimatedSize, error)) {
169 RefPtrWillBeRawPtr<Database> backend = adoptRefWillBeNoop(new Database(backendContext, name, expectedVersion, displayName, estimatedSize));
170 if (backend->openAndVerifyVersion(setVersionInNewDatabase, error, errorMessage))
171 return backend.release();
174 ASSERT(error != DatabaseError::None);
176 case DatabaseError::GenericSecurityError:
177 logOpenDatabaseError(context, name);
180 case DatabaseError::InvalidDatabaseState:
181 logErrorMessage(context, errorMessage);
185 ASSERT_NOT_REACHED();
190 PassRefPtrWillBeRawPtr<Database> DatabaseManager::openDatabase(ExecutionContext* context,
191 const String& name, const String& expectedVersion, const String& displayName,
192 unsigned long estimatedSize, DatabaseCallback* creationCallback,
193 DatabaseError& error, String& errorMessage)
195 ASSERT(error == DatabaseError::None);
197 bool setVersionInNewDatabase = !creationCallback;
198 RefPtrWillBeRawPtr<Database> database = openDatabaseInternal(context, name,
199 expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
203 databaseContextFor(context)->setHasOpenDatabases();
204 DatabaseClient::from(context)->didOpenDatabase(database, context->securityOrigin()->host(), name, expectedVersion);
206 if (database->isNew() && creationCallback) {
207 WTF_LOG(StorageAPI, "Scheduling DatabaseCreationCallbackTask for database %p\n", database.get());
208 database->executionContext()->postTask(DatabaseCreationCallbackTask::create(database, creationCallback));
212 return database.release();
215 String DatabaseManager::fullPathForDatabase(SecurityOrigin* origin, const String& name, bool createIfDoesNotExist)
217 return DatabaseTracker::tracker().fullPathForDatabase(origin, name, createIfDoesNotExist);
220 void DatabaseManager::logErrorMessage(ExecutionContext* context, const String& message)
222 context->addConsoleMessage(ConsoleMessage::create(StorageMessageSource, ErrorMessageLevel, message));