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