1/*
2 * Copyright (C) 2015 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(INDEXED_DATABASE)
29
30#include "UniqueIDBDatabaseTransaction.h"
31#include <wtf/HashMap.h>
32#include <wtf/Identified.h>
33#include <wtf/Ref.h>
34#include <wtf/RefCounted.h>
35#include <wtf/WeakPtr.h>
36
37namespace WebCore {
38
39class IDBError;
40class IDBResultData;
41
42namespace IDBServer {
43
44class IDBConnectionToClient;
45class ServerOpenDBRequest;
46class UniqueIDBDatabase;
47class UniqueIDBDatabaseTransaction;
48
49class UniqueIDBDatabaseConnection : public RefCounted<UniqueIDBDatabaseConnection>, public Identified<UniqueIDBDatabaseConnection> {
50public:
51 static Ref<UniqueIDBDatabaseConnection> create(UniqueIDBDatabase&, ServerOpenDBRequest&);
52
53 ~UniqueIDBDatabaseConnection();
54
55 const IDBResourceIdentifier& openRequestIdentifier() { return m_openRequestIdentifier; }
56 UniqueIDBDatabase* database() { return m_database.get(); }
57 IDBConnectionToClient& connectionToClient() { return m_connectionToClient; }
58
59 void connectionPendingCloseFromClient();
60 void connectionClosedFromClient();
61
62 bool closePending() const { return m_closePending; }
63
64 bool hasNonFinishedTransactions() const;
65
66 void fireVersionChangeEvent(const IDBResourceIdentifier& requestIdentifier, uint64_t requestedVersion);
67 UniqueIDBDatabaseTransaction& createVersionChangeTransaction(uint64_t newVersion);
68
69 void establishTransaction(const IDBTransactionInfo&);
70 void didAbortTransaction(UniqueIDBDatabaseTransaction&, const IDBError&);
71 void didCommitTransaction(UniqueIDBDatabaseTransaction&, const IDBError&);
72 void didCreateObjectStore(const IDBResultData&);
73 void didDeleteObjectStore(const IDBResultData&);
74 void didRenameObjectStore(const IDBResultData&);
75 void didClearObjectStore(const IDBResultData&);
76 void didCreateIndex(const IDBResultData&);
77 void didDeleteIndex(const IDBResultData&);
78 void didRenameIndex(const IDBResultData&);
79 void didFireVersionChangeEvent(const IDBResourceIdentifier& requestIdentifier);
80 void didFinishHandlingVersionChange(const IDBResourceIdentifier& transactionIdentifier);
81 void confirmDidCloseFromServer();
82
83 void abortTransactionWithoutCallback(UniqueIDBDatabaseTransaction&);
84
85 bool connectionIsClosing() const;
86
87 void deleteTransaction(UniqueIDBDatabaseTransaction&);
88
89private:
90 UniqueIDBDatabaseConnection(UniqueIDBDatabase&, ServerOpenDBRequest&);
91
92 WeakPtr<UniqueIDBDatabase> m_database;
93 Ref<IDBConnectionToClient> m_connectionToClient;
94 IDBResourceIdentifier m_openRequestIdentifier;
95
96 bool m_closePending { false };
97
98 HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_transactionMap;
99};
100
101} // namespace IDBServer
102} // namespace WebCore
103
104#endif // ENABLE(INDEXED_DATABASE)
105