IDBOpenDBRequest.cpp 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "IDBOpenDBRequest.h"

#if ENABLE(INDEXED_DATABASE)

#include "IDBDatabase.h"
32
#include "IDBDatabaseCallbacksImpl.h"
33
#include "IDBPendingTransactionMonitor.h"
34
#include "IDBVersionChangeEvent.h"
beidson@apple.com's avatar
beidson@apple.com committed
35
#include "Logging.h"
36 37 38 39
#include "ScriptExecutionContext.h"

namespace WebCore {

beidson@apple.com's avatar
beidson@apple.com committed
40
PassRefPtr<IDBOpenDBRequest> IDBOpenDBRequest::create(ScriptExecutionContext* context, PassRefPtr<IDBDatabaseCallbacks> callbacks, int64_t transactionId, uint64_t version, IndexedDB::VersionNullness versionNullness)
41
{
beidson@apple.com's avatar
beidson@apple.com committed
42
    RefPtr<IDBOpenDBRequest> request(adoptRef(new IDBOpenDBRequest(context, callbacks, transactionId, version, versionNullness)));
43 44 45 46
    request->suspendIfNeeded();
    return request.release();
}

beidson@apple.com's avatar
beidson@apple.com committed
47
IDBOpenDBRequest::IDBOpenDBRequest(ScriptExecutionContext* context, PassRefPtr<IDBDatabaseCallbacks> callbacks, int64_t transactionId, uint64_t version, IndexedDB::VersionNullness versionNullness)
48
    : IDBRequest(context, IDBAny::createNull(), IDBDatabaseBackendInterface::NormalTask, 0)
49
    , m_databaseCallbacks(callbacks)
50
    , m_transactionId(transactionId)
51
    , m_version(version)
beidson@apple.com's avatar
beidson@apple.com committed
52
    , m_versionNullness(versionNullness)
53 54 55 56 57 58 59 60
{
    ASSERT(!m_result);
}

IDBOpenDBRequest::~IDBOpenDBRequest()
{
}

61
EventTargetInterface IDBOpenDBRequest::eventTargetInterface() const
62
{
63
    return IDBOpenDBRequestEventTargetInterfaceType;
64 65
}

beidson@apple.com's avatar
beidson@apple.com committed
66
void IDBOpenDBRequest::onBlocked(uint64_t oldVersion)
67
{
beidson@apple.com's avatar
beidson@apple.com committed
68
    LOG(StorageAPI, "IDBOpenDBRequest::onBlocked()");
69 70
    if (!shouldEnqueueEvent())
        return;
beidson@apple.com's avatar
beidson@apple.com committed
71 72
    
    enqueueEvent(IDBVersionChangeEvent::create(oldVersion, m_version, m_versionNullness, eventNames().blockedEvent));
73 74
}

beidson@apple.com's avatar
beidson@apple.com committed
75
void IDBOpenDBRequest::onUpgradeNeeded(uint64_t oldVersion, PassRefPtr<IDBDatabaseBackendInterface> prpDatabaseBackend, const IDBDatabaseMetadata& metadata)
76
{
beidson@apple.com's avatar
beidson@apple.com committed
77
    LOG(StorageAPI, "IDBOpenDBRequest::onUpgradeNeeded()");
78 79
    if (m_contextStopped || !scriptExecutionContext()) {
        RefPtr<IDBDatabaseBackendInterface> db = prpDatabaseBackend;
80
        db->abort(m_transactionId);
81 82 83
        db->close(m_databaseCallbacks);
        return;
    }
84 85 86
    if (!shouldEnqueueEvent())
        return;

87 88
    ASSERT(m_databaseCallbacks);

89
    RefPtr<IDBDatabaseBackendInterface> databaseBackend = prpDatabaseBackend;
90

91
    RefPtr<IDBDatabase> idbDatabase = IDBDatabase::create(scriptExecutionContext(), databaseBackend, m_databaseCallbacks);
92
    idbDatabase->setMetadata(metadata);
93 94
    m_databaseCallbacks->connect(idbDatabase.get());
    m_databaseCallbacks = 0;
95

96
    IDBDatabaseMetadata oldMetadata(metadata);
beidson@apple.com's avatar
beidson@apple.com committed
97
    oldMetadata.version = oldVersion;
98

99
    m_transaction = IDBTransaction::create(scriptExecutionContext(), m_transactionId, idbDatabase.get(), this, oldMetadata);
100 101
    m_result = IDBAny::create(idbDatabase.release());

beidson@apple.com's avatar
beidson@apple.com committed
102
    if (m_versionNullness == IndexedDB::NullVersion)
103
        m_version = 1;
beidson@apple.com's avatar
beidson@apple.com committed
104
    enqueueEvent(IDBVersionChangeEvent::create(oldVersion, m_version, m_versionNullness, eventNames().upgradeneededEvent));
105 106
}

107
void IDBOpenDBRequest::onSuccess(PassRefPtr<IDBDatabaseBackendInterface> prpBackend, const IDBDatabaseMetadata& metadata)
108
{
beidson@apple.com's avatar
beidson@apple.com committed
109
    LOG(StorageAPI, "IDBOpenDBRequest::onSuccess()");
110 111 112
    if (!shouldEnqueueEvent())
        return;

113
    RefPtr<IDBDatabaseBackendInterface> backend = prpBackend;
114 115 116 117
    RefPtr<IDBDatabase> idbDatabase;
    if (m_result) {
        idbDatabase = m_result->idbDatabase();
        ASSERT(idbDatabase);
118
        ASSERT(!m_databaseCallbacks);
119
    } else {
120
        ASSERT(m_databaseCallbacks);
121
        idbDatabase = IDBDatabase::create(scriptExecutionContext(), backend.release(), m_databaseCallbacks);
122 123
        m_databaseCallbacks->connect(idbDatabase.get());
        m_databaseCallbacks = 0;
124 125
        m_result = IDBAny::create(idbDatabase.get());
    }
126
    idbDatabase->setMetadata(metadata);
127 128 129
    enqueueEvent(Event::create(eventNames().successEvent, false, false));
}

130 131 132 133 134 135 136 137 138 139
bool IDBOpenDBRequest::shouldEnqueueEvent() const
{
    if (m_contextStopped || !scriptExecutionContext())
        return false;
    ASSERT(m_readyState == PENDING || m_readyState == DONE);
    if (m_requestAborted)
        return false;
    return true;
}

140 141 142 143
bool IDBOpenDBRequest::dispatchEvent(PassRefPtr<Event> event)
{
    // If the connection closed between onUpgradeNeeded and the delivery of the "success" event,
    // an "error" event should be fired instead.
144
    if (event->type() == eventNames().successEvent && m_result->type() == IDBAny::IDBDatabaseType && m_result->idbDatabase()->isClosePending()) {
145
        m_result.clear();
146
        onError(IDBDatabaseError::create(IDBDatabaseException::AbortError, "The connection was closed."));
147 148 149 150 151
        return false;
    }

    return IDBRequest::dispatchEvent(event);
}
152 153 154 155

} // namespace WebCore

#endif