IDBFactory.cpp 6.79 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
/*
 * Copyright (C) 2010 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.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * 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.
 */
28

29
#include "config.h"
30
#include "IDBFactory.h"
31

32 33
#if ENABLE(INDEXED_DATABASE)

34
#include "Document.h"
35
#include "ExceptionCode.h"
36
#include "Frame.h"
37
#include "GroupSettings.h"
38
#include "HistogramSupport.h"
39
#include "IDBBindingUtilities.h"
40
#include "IDBDatabase.h"
41
#include "IDBDatabaseCallbacksImpl.h"
42
#include "IDBDatabaseException.h"
43
#include "IDBFactoryBackendInterface.h"
44
#include "IDBHistograms.h"
45
#include "IDBKey.h"
46
#include "IDBKeyRange.h"
47
#include "IDBOpenDBRequest.h"
beidson@apple.com's avatar
beidson@apple.com committed
48
#include "Logging.h"
49 50
#include "Page.h"
#include "PageGroup.h"
51
#include "SecurityOrigin.h"
52
#include "WorkerGlobalScope.h"
53 54
#include "WorkerLoaderProxy.h"
#include "WorkerThread.h"
55 56 57

namespace WebCore {

58
IDBFactory::IDBFactory(IDBFactoryBackendInterface* factory)
59
    : m_backend(factory)
60
{
61 62
    // We pass a reference to this object before it can be adopted.
    relaxAdoptionRequirement();
63 64
}

65
IDBFactory::~IDBFactory()
66 67 68
{
}

69 70
namespace {
static bool isContextValid(ScriptExecutionContext* context)
71
{
72
    ASSERT(context->isDocument() || context->isWorkerGlobalScope());
73
    if (context->isDocument()) {
74
        Document* document = toDocument(context);
75 76 77 78 79 80 81 82 83
        return document->frame() && document->page();
    }
    return true;
}

static String getIndexedDBDatabasePath(ScriptExecutionContext* context)
{
    ASSERT(isContextValid(context));
    if (context->isDocument()) {
84
        Document* document = toDocument(context);
85
        return document->page()->group().groupSettings().indexedDBDatabasePath();
86
    }
87 88
    WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
    const GroupSettings* groupSettings = workerGlobalScope->groupSettings();
89 90 91 92 93
    if (groupSettings)
        return groupSettings->indexedDBDatabasePath();
    return String();
}
}
94

95
PassRefPtr<IDBRequest> IDBFactory::getDatabaseNames(ScriptExecutionContext* context, ExceptionCode& ec)
96
{
beidson@apple.com's avatar
beidson@apple.com committed
97
    LOG(StorageAPI, "IDBFactory::getDatabaseNames");
98
    if (!isContextValid(context))
99
        return 0;
100 101 102 103
    if (!context->securityOrigin()->canAccessDatabase(context->topOrigin())) {
        ec = SECURITY_ERR;
        return 0;
    }
104

105 106
    RefPtr<IDBRequest> request = IDBRequest::create(context, IDBAny::create(this), 0);
    m_backend->getDatabaseNames(request, context->securityOrigin(), context, getIndexedDBDatabasePath(context));
107 108 109
    return request;
}

beidson@apple.com's avatar
beidson@apple.com committed
110 111
PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ScriptExecutionContext* context, const String& name, ExceptionCode& ec)
{
beidson@apple.com's avatar
beidson@apple.com committed
112
    LOG(StorageAPI, "IDBFactory::open");
beidson@apple.com's avatar
beidson@apple.com committed
113 114 115
    return openInternal(context, name, 0, IndexedDB::NullVersion, ec);
}

116
PassRefPtr<IDBOpenDBRequest> IDBFactory::open(ScriptExecutionContext* context, const String& name, unsigned long long version, ExceptionCode& ec)
117
{
beidson@apple.com's avatar
beidson@apple.com committed
118
    LOG(StorageAPI, "IDBFactory::open");
119
    if (!version) {
120
        ec = TypeError;
121 122
        return 0;
    }
beidson@apple.com's avatar
beidson@apple.com committed
123
    return openInternal(context, name, version, IndexedDB::NonNullVersion, ec);
124 125
}

beidson@apple.com's avatar
beidson@apple.com committed
126
PassRefPtr<IDBOpenDBRequest> IDBFactory::openInternal(ScriptExecutionContext* context, const String& name, uint64_t version, IndexedDB::VersionNullness versionNullness, ExceptionCode& ec)
127
{
128
    HistogramSupport::histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBOpenCall, IDBMethodsMax);
beidson@apple.com's avatar
beidson@apple.com committed
129
    ASSERT(version >= 1 || versionNullness == IndexedDB::NullVersion);
130
    if (name.isNull()) {
131
        ec = TypeError;
132 133
        return 0;
    }
134 135
    if (!isContextValid(context))
        return 0;
136 137 138 139
    if (!context->securityOrigin()->canAccessDatabase(context->topOrigin())) {
        ec = SECURITY_ERR;
        return 0;
    }
140

beidson@apple.com's avatar
beidson@apple.com committed
141
    RefPtr<IDBDatabaseCallbacks> databaseCallbacks = IDBDatabaseCallbacksImpl::create();
142
    int64_t transactionId = IDBDatabase::nextTransactionId();
beidson@apple.com's avatar
beidson@apple.com committed
143
    RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, databaseCallbacks, transactionId, version, versionNullness);
144
    m_backend->open(name, version, transactionId, request, databaseCallbacks, *(context->securityOrigin()), *(context->topOrigin()));
145
    return request;
146 147
}

148
PassRefPtr<IDBOpenDBRequest> IDBFactory::deleteDatabase(ScriptExecutionContext* context, const String& name, ExceptionCode& ec)
149
{
beidson@apple.com's avatar
beidson@apple.com committed
150
    LOG(StorageAPI, "IDBFactory::deleteDatabase");
151
    HistogramSupport::histogramEnumeration("WebCore.IndexedDB.FrontEndAPICalls", IDBDeleteDatabaseCall, IDBMethodsMax);
152
    if (name.isNull()) {
153
        ec = TypeError;
154 155
        return 0;
    }
156 157
    if (!isContextValid(context))
        return 0;
158 159 160 161
    if (!context->securityOrigin()->canAccessDatabase(context->topOrigin())) {
        ec = SECURITY_ERR;
        return 0;
    }
162

beidson@apple.com's avatar
beidson@apple.com committed
163
    RefPtr<IDBOpenDBRequest> request = IDBOpenDBRequest::create(context, 0, 0, 0, IndexedDB::NullVersion);
164
    m_backend->deleteDatabase(name, request, context->securityOrigin(), context, getIndexedDBDatabasePath(context));
165 166 167
    return request;
}

168
short IDBFactory::cmp(ScriptExecutionContext* context, const ScriptValue& firstValue, const ScriptValue& secondValue, ExceptionCode& ec)
169
{
170 171 172 173
    DOMRequestState requestState(context);
    RefPtr<IDBKey> first = scriptValueToIDBKey(&requestState, firstValue);
    RefPtr<IDBKey> second = scriptValueToIDBKey(&requestState, secondValue);

174 175 176
    ASSERT(first);
    ASSERT(second);

177
    if (!first->isValid() || !second->isValid()) {
178
        ec = IDBDatabaseException::DataError;
179
        return 0;
180 181
    }

182 183 184
    return static_cast<short>(first->compare(second.get()));
}

185 186 187
} // namespace WebCore

#endif // ENABLE(INDEXED_DATABASE)