WebCore/storage/DatabaseTracker.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 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 : public Noncopyable {
       
    60 public:
       
    61     static void initializeTracker(const String& databasePath);
       
    62     static DatabaseTracker& tracker();
       
    63     // This singleton will potentially be used from multiple worker threads and the page's context thread simultaneously.  To keep this safe, it's
       
    64     // currently using 4 locks.  In order to avoid deadlock when taking multiple locks, you must take them in the correct order:
       
    65     // m_databaseGuard before quotaManager if both locks are needed.
       
    66     // m_openDatabaseMapGuard before quotaManager if both locks are needed.
       
    67     // m_databaseGuard and m_openDatabaseMapGuard currently don't overlap.
       
    68     // notificationMutex() is currently independent of the other locks.
       
    69 
       
    70     bool canEstablishDatabase(ScriptExecutionContext*, const String& name, const String& displayName, unsigned long estimatedSize);
       
    71     void setDatabaseDetails(SecurityOrigin*, const String& name, const String& displayName, unsigned long estimatedSize);
       
    72     String fullPathForDatabase(SecurityOrigin*, const String& name, bool createIfDoesNotExist = true);
       
    73 
       
    74     void addOpenDatabase(AbstractDatabase*);
       
    75     void removeOpenDatabase(AbstractDatabase*);
       
    76     void getOpenDatabases(SecurityOrigin* origin, const String& name, HashSet<RefPtr<AbstractDatabase> >* databases);
       
    77 
       
    78     unsigned long long getMaxSizeForDatabase(const AbstractDatabase*);
       
    79     void databaseChanged(AbstractDatabase*);
       
    80 
       
    81 private:
       
    82     DatabaseTracker(const String& databasePath);
       
    83 
       
    84     typedef HashSet<AbstractDatabase*> DatabaseSet;
       
    85     typedef HashMap<String, DatabaseSet*> DatabaseNameMap;
       
    86     typedef HashMap<RefPtr<SecurityOrigin>, DatabaseNameMap*, SecurityOriginHash> DatabaseOriginMap;
       
    87 
       
    88     Mutex m_openDatabaseMapGuard;
       
    89     mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap;
       
    90 
       
    91 #if !PLATFORM(CHROMIUM)
       
    92 public:
       
    93     void setDatabaseDirectoryPath(const String&);
       
    94     String databaseDirectoryPath() const;
       
    95 
       
    96     void origins(Vector<RefPtr<SecurityOrigin> >& result);
       
    97     bool databaseNamesForOrigin(SecurityOrigin*, Vector<String>& result);
       
    98 
       
    99     DatabaseDetails detailsForNameAndOrigin(const String&, SecurityOrigin*);
       
   100 
       
   101     unsigned long long usageForDatabase(const String&, SecurityOrigin*);
       
   102     unsigned long long usageForOrigin(SecurityOrigin*);
       
   103     unsigned long long quotaForOrigin(SecurityOrigin*);
       
   104     void setQuota(SecurityOrigin*, unsigned long long);
       
   105 
       
   106     void deleteAllDatabases();
       
   107     bool deleteOrigin(SecurityOrigin*);
       
   108     bool deleteDatabase(SecurityOrigin*, const String& name);
       
   109 
       
   110     void setClient(DatabaseTrackerClient*);
       
   111 
       
   112     // From a secondary thread, must be thread safe with its data
       
   113     void scheduleNotifyDatabaseChanged(SecurityOrigin*, const String& name);
       
   114 
       
   115     bool hasEntryForOrigin(SecurityOrigin*);
       
   116 
       
   117 private:
       
   118     bool hasEntryForOriginNoLock(SecurityOrigin* origin);
       
   119     String fullPathForDatabaseNoLock(SecurityOrigin*, const String& name, bool createIfDoesNotExist);
       
   120     bool databaseNamesForOriginNoLock(SecurityOrigin* origin, Vector<String>& resultVector);
       
   121     unsigned long long usageForOriginNoLock(SecurityOrigin* origin);
       
   122     unsigned long long quotaForOriginNoLock(SecurityOrigin* origin);
       
   123 
       
   124     String trackerDatabasePath() const;
       
   125     void openTrackerDatabase(bool createIfDoesNotExist);
       
   126 
       
   127     String originPath(SecurityOrigin*) const;
       
   128 
       
   129     bool hasEntryForDatabase(SecurityOrigin*, const String& databaseIdentifier);
       
   130 
       
   131     bool addDatabase(SecurityOrigin*, const String& name, const String& path);
       
   132     void populateOrigins();
       
   133 
       
   134     bool deleteDatabaseFile(SecurityOrigin*, const String& name);
       
   135 
       
   136     // This lock protects m_database, m_quotaMap, m_proposedDatabases, m_databaseDirectoryPath, m_originsBeingDeleted, m_beingCreated, and m_beingDeleted.
       
   137     Mutex m_databaseGuard;
       
   138     SQLiteDatabase m_database;
       
   139 
       
   140     typedef HashMap<RefPtr<SecurityOrigin>, unsigned long long, SecurityOriginHash> QuotaMap;
       
   141     mutable OwnPtr<QuotaMap> m_quotaMap;
       
   142 
       
   143     String m_databaseDirectoryPath;
       
   144 
       
   145     DatabaseTrackerClient* m_client;
       
   146 
       
   147     typedef std::pair<RefPtr<SecurityOrigin>, DatabaseDetails> ProposedDatabase;
       
   148     HashSet<ProposedDatabase*> m_proposedDatabases;
       
   149 
       
   150     typedef HashMap<String, long> NameCountMap;
       
   151     typedef HashMap<RefPtr<SecurityOrigin>, NameCountMap*, SecurityOriginHash> CreateSet;
       
   152     CreateSet m_beingCreated;
       
   153     typedef HashSet<String> NameSet;
       
   154     HashMap<RefPtr<SecurityOrigin>, NameSet*, SecurityOriginHash> m_beingDeleted;
       
   155     HashSet<RefPtr<SecurityOrigin>, SecurityOriginHash> m_originsBeingDeleted;
       
   156     bool canCreateDatabase(SecurityOrigin *origin, const String& name);
       
   157     void recordCreatingDatabase(SecurityOrigin *origin, const String& name);
       
   158     void doneCreatingDatabase(SecurityOrigin *origin, const String& name);
       
   159     bool creatingDatabase(SecurityOrigin *origin, const String& name);
       
   160     bool canDeleteDatabase(SecurityOrigin *origin, const String& name);
       
   161     void recordDeletingDatabase(SecurityOrigin *origin, const String& name);
       
   162     void doneDeletingDatabase(SecurityOrigin *origin, const String& name);
       
   163     bool deletingDatabase(SecurityOrigin *origin, const String& name);
       
   164     bool canDeleteOrigin(SecurityOrigin *origin);
       
   165     bool deletingOrigin(SecurityOrigin *origin);
       
   166     void recordDeletingOrigin(SecurityOrigin *origin);
       
   167     void doneDeletingOrigin(SecurityOrigin *origin);
       
   168 
       
   169     static void scheduleForNotification();
       
   170     static void notifyDatabasesChanged(void*);
       
   171 #endif // !PLATFORM(CHROMIUM)
       
   172 };
       
   173 
       
   174 } // namespace WebCore
       
   175 
       
   176 #endif // ENABLE(DATABASE)
       
   177 #endif // DatabaseTracker_h