initial import
[vuplus_webkit] / Source / WebCore / storage / DatabaseTracker.h
1 /*
2  * Copyright (C) 2007, 2008 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  *
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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef DatabaseTracker_h
30 #define DatabaseTracker_h
31
32 #if ENABLE(DATABASE)
33
34 #include "PlatformString.h"
35 #include <wtf/HashMap.h>
36 #include <wtf/HashSet.h>
37 #include <wtf/text/StringHash.h>
38
39 #if !PLATFORM(CHROMIUM)
40 #include "DatabaseDetails.h"
41 #include "SQLiteDatabase.h"
42 #include <wtf/OwnPtr.h>
43 #endif // !PLATFORM(CHROMIUM)
44
45 namespace WebCore {
46
47 class AbstractDatabase;
48 class ScriptExecutionContext;
49 class SecurityOrigin;
50
51 struct SecurityOriginHash;
52
53 #if !PLATFORM(CHROMIUM)
54 class DatabaseTrackerClient;
55
56 struct SecurityOriginTraits;
57 #endif // !PLATFORM(CHROMIUM)
58
59 class DatabaseTracker {
60     WTF_MAKE_NONCOPYABLE(DatabaseTracker); WTF_MAKE_FAST_ALLOCATED;
61 public:
62     static void initializeTracker(const String& databasePath);
63     static DatabaseTracker& tracker();
64     // This singleton will potentially be used from multiple worker threads and the page's context thread simultaneously.  To keep this safe, it's
65     // currently using 4 locks.  In order to avoid deadlock when taking multiple locks, you must take them in the correct order:
66     // m_databaseGuard before quotaManager if both locks are needed.
67     // m_openDatabaseMapGuard before quotaManager if both locks are needed.
68     // m_databaseGuard and m_openDatabaseMapGuard currently don't overlap.
69     // notificationMutex() is currently independent of the other locks.
70
71     bool canEstablishDatabase(ScriptExecutionContext*, const String& name, const String& displayName, unsigned long estimatedSize);
72     void setDatabaseDetails(SecurityOrigin*, const String& name, const String& displayName, unsigned long estimatedSize);
73     String fullPathForDatabase(SecurityOrigin*, const String& name, bool createIfDoesNotExist = true);
74
75     void addOpenDatabase(AbstractDatabase*);
76     void removeOpenDatabase(AbstractDatabase*);
77     void getOpenDatabases(SecurityOrigin* origin, const String& name, HashSet<RefPtr<AbstractDatabase> >* databases);
78
79     unsigned long long getMaxSizeForDatabase(const AbstractDatabase*);
80     void databaseChanged(AbstractDatabase*);
81
82     void interruptAllDatabasesForContext(const ScriptExecutionContext*);
83
84 private:
85     DatabaseTracker(const String& databasePath);
86
87 #if !PLATFORM(CHROMIUM)
88 public:
89     void setDatabaseDirectoryPath(const String&);
90     String databaseDirectoryPath() const;
91
92     void origins(Vector<RefPtr<SecurityOrigin> >& result);
93     bool databaseNamesForOrigin(SecurityOrigin*, Vector<String>& result);
94
95     DatabaseDetails detailsForNameAndOrigin(const String&, SecurityOrigin*);
96
97     unsigned long long usageForDatabase(const String&, SecurityOrigin*);
98     unsigned long long usageForOrigin(SecurityOrigin*);
99     unsigned long long quotaForOrigin(SecurityOrigin*);
100     void setQuota(SecurityOrigin*, unsigned long long);
101
102     void deleteAllDatabases();
103     bool deleteOrigin(SecurityOrigin*);
104     bool deleteDatabase(SecurityOrigin*, const String& name);
105
106     void setClient(DatabaseTrackerClient*);
107
108     // From a secondary thread, must be thread safe with its data
109     void scheduleNotifyDatabaseChanged(SecurityOrigin*, const String& name);
110
111     bool hasEntryForOrigin(SecurityOrigin*);
112
113 private:
114     bool hasEntryForOriginNoLock(SecurityOrigin* origin);
115     String fullPathForDatabaseNoLock(SecurityOrigin*, const String& name, bool createIfDoesNotExist);
116     bool databaseNamesForOriginNoLock(SecurityOrigin* origin, Vector<String>& resultVector);
117     unsigned long long usageForOriginNoLock(SecurityOrigin* origin);
118     unsigned long long quotaForOriginNoLock(SecurityOrigin* origin);
119
120     String trackerDatabasePath() const;
121     void openTrackerDatabase(bool createIfDoesNotExist);
122
123     String originPath(SecurityOrigin*) const;
124
125     bool hasEntryForDatabase(SecurityOrigin*, const String& databaseIdentifier);
126
127     bool addDatabase(SecurityOrigin*, const String& name, const String& path);
128     void populateOrigins();
129
130     bool deleteDatabaseFile(SecurityOrigin*, const String& name);
131
132     typedef HashSet<AbstractDatabase*> DatabaseSet;
133     typedef HashMap<String, DatabaseSet*> DatabaseNameMap;
134     typedef HashMap<RefPtr<SecurityOrigin>, DatabaseNameMap*, SecurityOriginHash> DatabaseOriginMap;
135
136     Mutex m_openDatabaseMapGuard;
137     mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap;
138
139     // This lock protects m_database, m_quotaMap, m_proposedDatabases, m_databaseDirectoryPath, m_originsBeingDeleted, m_beingCreated, and m_beingDeleted.
140     Mutex m_databaseGuard;
141     SQLiteDatabase m_database;
142
143     typedef HashMap<RefPtr<SecurityOrigin>, unsigned long long, SecurityOriginHash> QuotaMap;
144     mutable OwnPtr<QuotaMap> m_quotaMap;
145
146     String m_databaseDirectoryPath;
147
148     DatabaseTrackerClient* m_client;
149
150     typedef std::pair<RefPtr<SecurityOrigin>, DatabaseDetails> ProposedDatabase;
151     HashSet<ProposedDatabase*> m_proposedDatabases;
152
153     typedef HashMap<String, long> NameCountMap;
154     typedef HashMap<RefPtr<SecurityOrigin>, NameCountMap*, SecurityOriginHash> CreateSet;
155     CreateSet m_beingCreated;
156     typedef HashSet<String> NameSet;
157     HashMap<RefPtr<SecurityOrigin>, NameSet*, SecurityOriginHash> m_beingDeleted;
158     HashSet<RefPtr<SecurityOrigin>, SecurityOriginHash> m_originsBeingDeleted;
159     bool canCreateDatabase(SecurityOrigin *origin, const String& name);
160     void recordCreatingDatabase(SecurityOrigin *origin, const String& name);
161     void doneCreatingDatabase(SecurityOrigin *origin, const String& name);
162     bool creatingDatabase(SecurityOrigin *origin, const String& name);
163     bool canDeleteDatabase(SecurityOrigin *origin, const String& name);
164     void recordDeletingDatabase(SecurityOrigin *origin, const String& name);
165     void doneDeletingDatabase(SecurityOrigin *origin, const String& name);
166     bool deletingDatabase(SecurityOrigin *origin, const String& name);
167     bool canDeleteOrigin(SecurityOrigin *origin);
168     bool deletingOrigin(SecurityOrigin *origin);
169     void recordDeletingOrigin(SecurityOrigin *origin);
170     void doneDeletingOrigin(SecurityOrigin *origin);
171
172     static void scheduleForNotification();
173     static void notifyDatabasesChanged(void*);
174 #else
175 public:
176     void getOpenDatabases(const String& originIdentifier, const String& name, HashSet<RefPtr<AbstractDatabase> >* databases);
177
178 private:
179     typedef HashSet<AbstractDatabase*> DatabaseSet;
180     typedef HashMap<String, DatabaseSet*> DatabaseNameMap;
181     typedef HashMap<String, DatabaseNameMap*> DatabaseOriginMap;
182
183     Mutex m_openDatabaseMapGuard;
184     mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap;
185 #endif // !PLATFORM(CHROMIUM)
186 };
187
188 } // namespace WebCore
189
190 #endif // ENABLE(DATABASE)
191 #endif // DatabaseTracker_h