• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "StringHash.h"
36 #include <wtf/HashMap.h>
37 #include <wtf/HashSet.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 Database;
48 class ScriptExecutionContext;
49 class SecurityOrigin;
50 
51 struct SecurityOriginHash;
52 
53 #if !PLATFORM(CHROMIUM)
54 class DatabaseTrackerClient;
55 class OriginQuotaManager;
56 
57 struct SecurityOriginTraits;
58 #endif // !PLATFORM(CHROMIUM)
59 
60 class DatabaseTracker : public Noncopyable {
61 public:
62     static DatabaseTracker& tracker();
63     // FIXME: Due to workers having multiple threads in a single process sharing
64     // a DatabaseTracker, this singleton will have to be synchronized or moved
65     // to TLS.
66 
67     bool canEstablishDatabase(ScriptExecutionContext*, const String& name, const String& displayName, unsigned long estimatedSize);
68     void setDatabaseDetails(SecurityOrigin*, const String& name, const String& displayName, unsigned long estimatedSize);
69     String fullPathForDatabase(SecurityOrigin*, const String& name, bool createIfDoesNotExist = true);
70 
71     void addOpenDatabase(Database*);
72     void removeOpenDatabase(Database*);
73     void getOpenDatabases(SecurityOrigin* origin, const String& name, HashSet<RefPtr<Database> >* databases);
74 
75     unsigned long long getMaxSizeForDatabase(const Database*);
76 
77 private:
78     DatabaseTracker();
79 
80     typedef HashSet<Database*> DatabaseSet;
81     typedef HashMap<String, DatabaseSet*> DatabaseNameMap;
82     typedef HashMap<RefPtr<SecurityOrigin>, DatabaseNameMap*, SecurityOriginHash> DatabaseOriginMap;
83 
84     Mutex m_openDatabaseMapGuard;
85     mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap;
86 
87 #if !PLATFORM(CHROMIUM)
88 public:
89     void setDatabaseDirectoryPath(const String&);
90     const 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     void deleteOrigin(SecurityOrigin*);
104     void 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     OriginQuotaManager& originQuotaManager();
112 
113 
114     bool hasEntryForOrigin(SecurityOrigin*);
115 
116 private:
117     String trackerDatabasePath() const;
118     void openTrackerDatabase(bool createIfDoesNotExist);
119 
120     String originPath(SecurityOrigin*) const;
121 
122     bool hasEntryForDatabase(SecurityOrigin*, const String& databaseIdentifier);
123 
124     bool addDatabase(SecurityOrigin*, const String& name, const String& path);
125     void populateOrigins();
126 
127     bool deleteDatabaseFile(SecurityOrigin*, const String& name);
128 
129     SQLiteDatabase m_database;
130 
131     typedef HashMap<RefPtr<SecurityOrigin>, unsigned long long, SecurityOriginHash> QuotaMap;
132     Mutex m_quotaMapGuard;
133     mutable OwnPtr<QuotaMap> m_quotaMap;
134 
135     OwnPtr<OriginQuotaManager> m_quotaManager;
136 
137     String m_databaseDirectoryPath;
138 
139     DatabaseTrackerClient* m_client;
140 
141     std::pair<SecurityOrigin*, DatabaseDetails>* m_proposedDatabase;
142 
143 #ifndef NDEBUG
144     ThreadIdentifier m_thread;
145 #endif
146 
147     static void scheduleForNotification();
148     static void notifyDatabasesChanged(void*);
149 #endif // !PLATFORM(CHROMIUM)
150 };
151 
152 } // namespace WebCore
153 
154 #endif // ENABLE(DATABASE)
155 #endif // DatabaseTracker_h
156