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 #include "DatabaseDetails.h" 33 #include "PlatformString.h" 34 #include "SQLiteDatabase.h" 35 #include "StringHash.h" 36 #include <wtf/HashSet.h> 37 #include <wtf/OwnPtr.h> 38 39 namespace WebCore { 40 41 class Database; 42 class DatabaseTrackerClient; 43 class Document; 44 class OriginQuotaManager; 45 class SecurityOrigin; 46 47 struct SecurityOriginHash; 48 struct SecurityOriginTraits; 49 50 class DatabaseTracker { 51 public: 52 void setDatabaseDirectoryPath(const String&); 53 const String& databaseDirectoryPath() const; 54 55 bool canEstablishDatabase(Document*, const String& name, const String& displayName, unsigned long estimatedSize); 56 void setDatabaseDetails(SecurityOrigin*, const String& name, const String& displayName, unsigned long estimatedSize); 57 String fullPathForDatabase(SecurityOrigin*, const String& name, bool createIfDoesNotExist = true); 58 59 void origins(Vector<RefPtr<SecurityOrigin> >& result); 60 bool databaseNamesForOrigin(SecurityOrigin*, Vector<String>& result); 61 62 DatabaseDetails detailsForNameAndOrigin(const String&, SecurityOrigin*); 63 64 void addOpenDatabase(Database*); 65 void removeOpenDatabase(Database*); 66 67 unsigned long long usageForDatabase(const String&, SecurityOrigin*); 68 unsigned long long usageForOrigin(SecurityOrigin*); 69 unsigned long long quotaForOrigin(SecurityOrigin*); 70 void setQuota(SecurityOrigin*, unsigned long long); 71 72 void deleteAllDatabases(); 73 void deleteOrigin(SecurityOrigin*); 74 void deleteDatabase(SecurityOrigin*, const String& name); 75 76 void setClient(DatabaseTrackerClient*); 77 78 // From a secondary thread, must be thread safe with its data 79 void scheduleNotifyDatabaseChanged(SecurityOrigin*, const String& name); 80 81 OriginQuotaManager& originQuotaManager(); 82 83 static DatabaseTracker& tracker(); 84 85 bool hasEntryForOrigin(SecurityOrigin*); 86 87 private: 88 DatabaseTracker(); 89 90 String trackerDatabasePath() const; 91 void openTrackerDatabase(bool createIfDoesNotExist); 92 93 String originPath(SecurityOrigin*) const; 94 95 bool hasEntryForDatabase(SecurityOrigin*, const String& databaseIdentifier); 96 97 bool addDatabase(SecurityOrigin*, const String& name, const String& path); 98 void populateOrigins(); 99 100 bool deleteDatabaseFile(SecurityOrigin*, const String& name); 101 102 SQLiteDatabase m_database; 103 104 typedef HashMap<RefPtr<SecurityOrigin>, unsigned long long, SecurityOriginHash> QuotaMap; 105 Mutex m_quotaMapGuard; 106 mutable OwnPtr<QuotaMap> m_quotaMap; 107 108 typedef HashSet<Database*> DatabaseSet; 109 typedef HashMap<String, DatabaseSet*> DatabaseNameMap; 110 typedef HashMap<RefPtr<SecurityOrigin>, DatabaseNameMap*, SecurityOriginHash> DatabaseOriginMap; 111 112 Mutex m_openDatabaseMapGuard; 113 mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap; 114 115 OwnPtr<OriginQuotaManager> m_quotaManager; 116 117 String m_databaseDirectoryPath; 118 119 DatabaseTrackerClient* m_client; 120 121 std::pair<SecurityOrigin*, DatabaseDetails>* m_proposedDatabase; 122 123 #ifndef NDEBUG 124 ThreadIdentifier m_thread; 125 #endif 126 127 static void scheduleForNotification(); 128 static void notifyDatabasesChanged(void*); 129 }; 130 131 } // namespace WebCore 132 133 #endif // DatabaseTracker_h 134