Home | History | Annotate | Download | only in storage
      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     typedef HashSet<AbstractDatabase*> DatabaseSet;
     88     typedef HashMap<String, DatabaseSet*> DatabaseNameMap;
     89     typedef HashMap<RefPtr<SecurityOrigin>, DatabaseNameMap*, SecurityOriginHash> DatabaseOriginMap;
     90 
     91     Mutex m_openDatabaseMapGuard;
     92     mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap;
     93 
     94 #if !PLATFORM(CHROMIUM)
     95 public:
     96     void setDatabaseDirectoryPath(const String&);
     97     String databaseDirectoryPath() const;
     98 
     99     void origins(Vector<RefPtr<SecurityOrigin> >& result);
    100     bool databaseNamesForOrigin(SecurityOrigin*, Vector<String>& result);
    101 
    102     DatabaseDetails detailsForNameAndOrigin(const String&, SecurityOrigin*);
    103 
    104     unsigned long long usageForDatabase(const String&, SecurityOrigin*);
    105     unsigned long long usageForOrigin(SecurityOrigin*);
    106     unsigned long long quotaForOrigin(SecurityOrigin*);
    107     void setQuota(SecurityOrigin*, unsigned long long);
    108 
    109     void deleteAllDatabases();
    110     bool deleteOrigin(SecurityOrigin*);
    111     bool deleteDatabase(SecurityOrigin*, const String& name);
    112 
    113     void setClient(DatabaseTrackerClient*);
    114 
    115     // From a secondary thread, must be thread safe with its data
    116     void scheduleNotifyDatabaseChanged(SecurityOrigin*, const String& name);
    117 
    118     bool hasEntryForOrigin(SecurityOrigin*);
    119 
    120 private:
    121     bool hasEntryForOriginNoLock(SecurityOrigin* origin);
    122     String fullPathForDatabaseNoLock(SecurityOrigin*, const String& name, bool createIfDoesNotExist);
    123     bool databaseNamesForOriginNoLock(SecurityOrigin* origin, Vector<String>& resultVector);
    124     unsigned long long usageForOriginNoLock(SecurityOrigin* origin);
    125     unsigned long long quotaForOriginNoLock(SecurityOrigin* origin);
    126 
    127     String trackerDatabasePath() const;
    128     void openTrackerDatabase(bool createIfDoesNotExist);
    129 
    130     String originPath(SecurityOrigin*) const;
    131 
    132     bool hasEntryForDatabase(SecurityOrigin*, const String& databaseIdentifier);
    133 
    134     bool addDatabase(SecurityOrigin*, const String& name, const String& path);
    135     void populateOrigins();
    136 
    137     bool deleteDatabaseFile(SecurityOrigin*, const String& name);
    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 #endif // !PLATFORM(CHROMIUM)
    175 };
    176 
    177 } // namespace WebCore
    178 
    179 #endif // ENABLE(DATABASE)
    180 #endif // DatabaseTracker_h
    181