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#ifndef DatabaseManager_h
27#define DatabaseManager_h
28
29#if ENABLE(SQL_DATABASE)
30
31#include "DatabaseBasicTypes.h"
32#include "DatabaseDetails.h"
33#include "DatabaseError.h"
34#include <mutex>
35#include <wtf/Assertions.h>
36#include <wtf/HashMap.h>
37#include <wtf/HashSet.h>
38#include <wtf/PassRefPtr.h>
39#include <wtf/Threading.h>
40
41namespace WebCore {
42
43class AbstractDatabaseServer;
44class Database;
45class DatabaseBackendBase;
46class DatabaseCallback;
47class DatabaseContext;
48class DatabaseManagerClient;
49class DatabaseSync;
50class DatabaseTaskSynchronizer;
51class SecurityOrigin;
52class ScriptExecutionContext;
53
54class DatabaseManager {
55    WTF_MAKE_NONCOPYABLE(DatabaseManager); WTF_MAKE_FAST_ALLOCATED;
56public:
57    static DatabaseManager& manager();
58
59    void initialize(const String& databasePath);
60    void setClient(DatabaseManagerClient*);
61    String databaseDirectoryPath() const;
62    void setDatabaseDirectoryPath(const String&);
63
64    bool isAvailable();
65    void setIsAvailable(bool);
66
67    // This gets a DatabaseContext for the specified ScriptExecutionContext.
68    // If one doesn't already exist, it will create a new one.
69    PassRefPtr<DatabaseContext> databaseContextFor(ScriptExecutionContext*);
70
71    // These 2 methods are for DatabaseContext (un)registration, and should only
72    // be called by the DatabaseContext constructor and destructor.
73    void registerDatabaseContext(DatabaseContext*);
74    void unregisterDatabaseContext(DatabaseContext*);
75
76#if !ASSERT_DISABLED
77    void didConstructDatabaseContext();
78    void didDestructDatabaseContext();
79#else
80    void didConstructDatabaseContext() { }
81    void didDestructDatabaseContext() { }
82#endif
83
84    static ExceptionCode exceptionCodeForDatabaseError(DatabaseError);
85
86    PassRefPtr<Database> openDatabase(ScriptExecutionContext*, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, PassRefPtr<DatabaseCallback>, DatabaseError&);
87    PassRefPtr<DatabaseSync> openDatabaseSync(ScriptExecutionContext*, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, PassRefPtr<DatabaseCallback>, DatabaseError&);
88
89    bool hasOpenDatabases(ScriptExecutionContext*);
90    void stopDatabases(ScriptExecutionContext*, DatabaseTaskSynchronizer*);
91
92    String fullPathForDatabase(SecurityOrigin*, const String& name, bool createIfDoesNotExist = true);
93
94    bool hasEntryForOrigin(SecurityOrigin*);
95    void origins(Vector<RefPtr<SecurityOrigin>>& result);
96    bool databaseNamesForOrigin(SecurityOrigin*, Vector<String>& result);
97    DatabaseDetails detailsForNameAndOrigin(const String&, SecurityOrigin*);
98
99    unsigned long long usageForOrigin(SecurityOrigin*);
100    unsigned long long quotaForOrigin(SecurityOrigin*);
101
102    void setQuota(SecurityOrigin*, unsigned long long);
103
104    void deleteAllDatabases();
105    bool deleteOrigin(SecurityOrigin*);
106    bool deleteDatabase(SecurityOrigin*, const String& name);
107
108    void interruptAllDatabasesForContext(ScriptExecutionContext*);
109
110private:
111    class ProposedDatabase {
112    public:
113        ProposedDatabase(DatabaseManager&, SecurityOrigin*,
114            const String& name, const String& displayName, unsigned long estimatedSize);
115        ~ProposedDatabase();
116
117        SecurityOrigin* origin() { return m_origin.get(); }
118        DatabaseDetails& details() { return m_details; }
119
120    private:
121        DatabaseManager& m_manager;
122        RefPtr<SecurityOrigin> m_origin;
123        DatabaseDetails m_details;
124    };
125
126    DatabaseManager();
127    ~DatabaseManager() { }
128
129    // This gets a DatabaseContext for the specified ScriptExecutionContext if
130    // it already exist previously. Otherwise, it returns 0.
131    PassRefPtr<DatabaseContext> existingDatabaseContextFor(ScriptExecutionContext*);
132
133    PassRefPtr<DatabaseBackendBase> openDatabaseBackend(ScriptExecutionContext*,
134        DatabaseType, const String& name, const String& expectedVersion, const String& displayName,
135        unsigned long estimatedSize, bool setVersionInNewDatabase, DatabaseError&, String& errorMessage);
136
137    void addProposedDatabase(ProposedDatabase*);
138    void removeProposedDatabase(ProposedDatabase*);
139
140    static void logErrorMessage(ScriptExecutionContext*, const String& message);
141
142    AbstractDatabaseServer* m_server;
143    DatabaseManagerClient* m_client;
144    bool m_databaseIsAvailable;
145
146    // Access to the following fields require locking m_lock below:
147    typedef HashMap<ScriptExecutionContext*, DatabaseContext*> ContextMap;
148    ContextMap m_contextMap;
149#if !ASSERT_DISABLED
150    int m_databaseContextRegisteredCount;
151    int m_databaseContextInstanceCount;
152#endif
153    HashSet<ProposedDatabase*> m_proposedDatabases;
154
155    // This mutex protects m_contextMap, and m_proposedDatabases.
156    std::mutex m_mutex;
157};
158
159} // namespace WebCore
160
161#endif // ENABLE(SQL_DATABASE)
162
163#endif // DatabaseManager_h
164