Home | History | Annotate | Download | only in webdatabase
      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 "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"
     44 
     45 namespace blink {
     46 
     47 DatabaseManager& DatabaseManager::manager()
     48 {
     49     ASSERT(isMainThread());
     50     DEFINE_STATIC_LOCAL(DatabaseManager, dbManager, ());
     51     return dbManager;
     52 }
     53 
     54 DatabaseManager::DatabaseManager()
     55 #if ENABLE(ASSERT)
     56     : m_databaseContextRegisteredCount(0)
     57     , m_databaseContextInstanceCount(0)
     58 #endif
     59 {
     60 }
     61 
     62 DatabaseManager::~DatabaseManager()
     63 {
     64 }
     65 
     66 class DatabaseCreationCallbackTask FINAL : public ExecutionContextTask {
     67 public:
     68     static PassOwnPtr<DatabaseCreationCallbackTask> create(PassRefPtrWillBeRawPtr<Database> database, DatabaseCallback* creationCallback)
     69     {
     70         return adoptPtr(new DatabaseCreationCallbackTask(database, creationCallback));
     71     }
     72 
     73     virtual void performTask(ExecutionContext*) OVERRIDE
     74     {
     75         m_creationCallback->handleEvent(m_database.get());
     76     }
     77 
     78 private:
     79     DatabaseCreationCallbackTask(PassRefPtrWillBeRawPtr<Database> database, DatabaseCallback* callback)
     80         : m_database(database)
     81         , m_creationCallback(callback)
     82     {
     83     }
     84 
     85     RefPtrWillBePersistent<Database> m_database;
     86     Persistent<DatabaseCallback> m_creationCallback;
     87 };
     88 
     89 DatabaseContext* DatabaseManager::existingDatabaseContextFor(ExecutionContext* context)
     90 {
     91     ASSERT(m_databaseContextRegisteredCount >= 0);
     92     ASSERT(m_databaseContextInstanceCount >= 0);
     93     ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
     94     return m_contextMap.get(context);
     95 }
     96 
     97 DatabaseContext* DatabaseManager::databaseContextFor(ExecutionContext* context)
     98 {
     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();
    105 }
    106 
    107 void DatabaseManager::registerDatabaseContext(DatabaseContext* databaseContext)
    108 {
    109     ExecutionContext* context = databaseContext->executionContext();
    110     m_contextMap.set(context, databaseContext);
    111 #if ENABLE(ASSERT)
    112     m_databaseContextRegisteredCount++;
    113 #endif
    114 }
    115 
    116 void DatabaseManager::unregisterDatabaseContext(DatabaseContext* databaseContext)
    117 {
    118     ExecutionContext* context = databaseContext->executionContext();
    119     ASSERT(m_contextMap.get(context));
    120 #if ENABLE(ASSERT)
    121     m_databaseContextRegisteredCount--;
    122 #endif
    123     m_contextMap.remove(context);
    124 }
    125 
    126 #if ENABLE(ASSERT)
    127 void DatabaseManager::didConstructDatabaseContext()
    128 {
    129     m_databaseContextInstanceCount++;
    130 }
    131 
    132 void DatabaseManager::didDestructDatabaseContext()
    133 {
    134     m_databaseContextInstanceCount--;
    135     ASSERT(m_databaseContextRegisteredCount <= m_databaseContextInstanceCount);
    136 }
    137 #endif
    138 
    139 void DatabaseManager::throwExceptionForDatabaseError(DatabaseError error, const String& errorMessage, ExceptionState& exceptionState)
    140 {
    141     switch (error) {
    142     case DatabaseError::None:
    143         return;
    144     case DatabaseError::GenericSecurityError:
    145         exceptionState.throwSecurityError(errorMessage);
    146         return;
    147     case DatabaseError::InvalidDatabaseState:
    148         exceptionState.throwDOMException(InvalidStateError, errorMessage);
    149         return;
    150     default:
    151         ASSERT_NOT_REACHED();
    152     }
    153 }
    154 
    155 static void logOpenDatabaseError(ExecutionContext* context, const String& name)
    156 {
    157     WTF_LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(),
    158         context->securityOrigin()->toString().ascii().data());
    159 }
    160 
    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)
    164 {
    165     ASSERT(error == DatabaseError::None);
    166 
    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();
    172     }
    173 
    174     ASSERT(error != DatabaseError::None);
    175     switch (error) {
    176     case DatabaseError::GenericSecurityError:
    177         logOpenDatabaseError(context, name);
    178         return nullptr;
    179 
    180     case DatabaseError::InvalidDatabaseState:
    181         logErrorMessage(context, errorMessage);
    182         return nullptr;
    183 
    184     default:
    185         ASSERT_NOT_REACHED();
    186     }
    187     return nullptr;
    188 }
    189 
    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)
    194 {
    195     ASSERT(error == DatabaseError::None);
    196 
    197     bool setVersionInNewDatabase = !creationCallback;
    198     RefPtrWillBeRawPtr<Database> database = openDatabaseInternal(context, name,
    199         expectedVersion, displayName, estimatedSize, setVersionInNewDatabase, error, errorMessage);
    200     if (!database)
    201         return nullptr;
    202 
    203     databaseContextFor(context)->setHasOpenDatabases();
    204     DatabaseClient::from(context)->didOpenDatabase(database, context->securityOrigin()->host(), name, expectedVersion);
    205 
    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));
    209     }
    210 
    211     ASSERT(database);
    212     return database.release();
    213 }
    214 
    215 String DatabaseManager::fullPathForDatabase(SecurityOrigin* origin, const String& name, bool createIfDoesNotExist)
    216 {
    217     return DatabaseTracker::tracker().fullPathForDatabase(origin, name, createIfDoesNotExist);
    218 }
    219 
    220 void DatabaseManager::logErrorMessage(ExecutionContext* context, const String& message)
    221 {
    222     context->addConsoleMessage(ConsoleMessage::create(StorageMessageSource, ErrorMessageLevel, message));
    223 }
    224 
    225 } // namespace blink
    226