Home | History | Annotate | Download | only in indexeddb
      1 /*
      2  * Copyright (C) 2012 Google 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  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "modules/indexeddb/IDBOpenDBRequest.h"
     28 
     29 #include "core/dom/ExceptionCode.h"
     30 #include "core/dom/ScriptExecutionContext.h"
     31 #include "modules/indexeddb/IDBDatabase.h"
     32 #include "modules/indexeddb/IDBDatabaseCallbacksImpl.h"
     33 #include "modules/indexeddb/IDBPendingTransactionMonitor.h"
     34 #include "modules/indexeddb/IDBTracing.h"
     35 #include "modules/indexeddb/IDBVersionChangeEvent.h"
     36 
     37 namespace WebCore {
     38 
     39 PassRefPtr<IDBOpenDBRequest> IDBOpenDBRequest::create(ScriptExecutionContext* context, PassRefPtr<IDBDatabaseCallbacksImpl> callbacks, int64_t transactionId, int64_t version)
     40 {
     41     RefPtr<IDBOpenDBRequest> request(adoptRef(new IDBOpenDBRequest(context, callbacks, transactionId, version)));
     42     request->suspendIfNeeded();
     43     return request.release();
     44 }
     45 
     46 IDBOpenDBRequest::IDBOpenDBRequest(ScriptExecutionContext* context, PassRefPtr<IDBDatabaseCallbacksImpl> callbacks, int64_t transactionId, int64_t version)
     47     : IDBRequest(context, IDBAny::createNull(), IDBDatabaseBackendInterface::NormalTask, 0)
     48     , m_databaseCallbacks(callbacks)
     49     , m_transactionId(transactionId)
     50     , m_version(version)
     51 {
     52     ASSERT(!m_result);
     53     ScriptWrappable::init(this);
     54 }
     55 
     56 IDBOpenDBRequest::~IDBOpenDBRequest()
     57 {
     58 }
     59 
     60 const AtomicString& IDBOpenDBRequest::interfaceName() const
     61 {
     62     return eventNames().interfaceForIDBOpenDBRequest;
     63 }
     64 
     65 void IDBOpenDBRequest::onBlocked(int64_t oldVersion)
     66 {
     67     IDB_TRACE("IDBOpenDBRequest::onBlocked()");
     68     if (!shouldEnqueueEvent())
     69         return;
     70     RefPtr<IDBAny> newVersionAny = (m_version == IDBDatabaseMetadata::DefaultIntVersion) ? IDBAny::createNull() : IDBAny::create(m_version);
     71     enqueueEvent(IDBVersionChangeEvent::create(IDBAny::create(oldVersion), newVersionAny.release(), eventNames().blockedEvent));
     72 }
     73 
     74 void IDBOpenDBRequest::onUpgradeNeeded(int64_t oldVersion, PassRefPtr<IDBDatabaseBackendInterface> prpDatabaseBackend, const IDBDatabaseMetadata& metadata, WebKit::WebIDBCallbacks::DataLoss dataLoss)
     75 {
     76     IDB_TRACE("IDBOpenDBRequest::onUpgradeNeeded()");
     77     if (m_contextStopped || !scriptExecutionContext()) {
     78         RefPtr<IDBDatabaseBackendInterface> db = prpDatabaseBackend;
     79         db->abort(m_transactionId);
     80         db->close(m_databaseCallbacks);
     81         return;
     82     }
     83     if (!shouldEnqueueEvent())
     84         return;
     85 
     86     ASSERT(m_databaseCallbacks);
     87 
     88     RefPtr<IDBDatabaseBackendInterface> databaseBackend = prpDatabaseBackend;
     89 
     90     RefPtr<IDBDatabase> idbDatabase = IDBDatabase::create(scriptExecutionContext(), databaseBackend, m_databaseCallbacks);
     91     idbDatabase->setMetadata(metadata);
     92     m_databaseCallbacks->connect(idbDatabase.get());
     93     m_databaseCallbacks = 0;
     94 
     95     if (oldVersion == IDBDatabaseMetadata::NoIntVersion) {
     96         // This database hasn't had an integer version before.
     97         oldVersion = IDBDatabaseMetadata::DefaultIntVersion;
     98     }
     99     IDBDatabaseMetadata oldMetadata(metadata);
    100     oldMetadata.intVersion = oldVersion;
    101 
    102     m_transaction = IDBTransaction::create(scriptExecutionContext(), m_transactionId, idbDatabase.get(), this, oldMetadata);
    103     m_result = IDBAny::create(idbDatabase.release());
    104 
    105     if (m_version == IDBDatabaseMetadata::NoIntVersion)
    106         m_version = 1;
    107     enqueueEvent(IDBVersionChangeEvent::create(IDBAny::create(oldVersion), IDBAny::create(m_version), eventNames().upgradeneededEvent, dataLoss));
    108 }
    109 
    110 void IDBOpenDBRequest::onSuccess(PassRefPtr<IDBDatabaseBackendInterface> prpBackend, const IDBDatabaseMetadata& metadata)
    111 {
    112     IDB_TRACE("IDBOpenDBRequest::onSuccess()");
    113     if (m_contextStopped || !scriptExecutionContext()) {
    114         RefPtr<IDBDatabaseBackendInterface> db = prpBackend;
    115         db->close(m_databaseCallbacks);
    116         return;
    117     }
    118     if (!shouldEnqueueEvent())
    119         return;
    120 
    121     RefPtr<IDBDatabaseBackendInterface> backend = prpBackend;
    122     RefPtr<IDBDatabase> idbDatabase;
    123     if (m_result) {
    124         idbDatabase = m_result->idbDatabase();
    125         ASSERT(idbDatabase);
    126         ASSERT(!m_databaseCallbacks);
    127     } else {
    128         ASSERT(m_databaseCallbacks);
    129         idbDatabase = IDBDatabase::create(scriptExecutionContext(), backend.release(), m_databaseCallbacks);
    130         m_databaseCallbacks->connect(idbDatabase.get());
    131         m_databaseCallbacks = 0;
    132         m_result = IDBAny::create(idbDatabase.get());
    133     }
    134     idbDatabase->setMetadata(metadata);
    135     enqueueEvent(Event::create(eventNames().successEvent, false, false));
    136 }
    137 
    138 bool IDBOpenDBRequest::shouldEnqueueEvent() const
    139 {
    140     if (m_contextStopped || !scriptExecutionContext())
    141         return false;
    142     ASSERT(m_readyState == PENDING || m_readyState == DONE);
    143     if (m_requestAborted)
    144         return false;
    145     return true;
    146 }
    147 
    148 bool IDBOpenDBRequest::dispatchEvent(PassRefPtr<Event> event)
    149 {
    150     // If the connection closed between onUpgradeNeeded and the delivery of the "success" event,
    151     // an "error" event should be fired instead.
    152     if (event->type() == eventNames().successEvent && m_result->type() == IDBAny::IDBDatabaseType && m_result->idbDatabase()->isClosePending()) {
    153         m_result.clear();
    154         onError(DOMError::create(AbortError, "The connection was closed."));
    155         return false;
    156     }
    157 
    158     return IDBRequest::dispatchEvent(event);
    159 }
    160 
    161 } // namespace WebCore
    162