153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)/*
253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved.
353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *
453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * modification, are permitted provided that the following conditions
653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * are met:
753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *
853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * 1.  Redistributions of source code must retain the above copyright
953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     notice, this list of conditions and the following disclaimer.
1053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * 2.  Redistributions in binary form must reproduce the above copyright
1153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     notice, this list of conditions and the following disclaimer in the
1253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     documentation and/or other materials provided with the distribution.
1353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
1453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     its contributors may be used to endorse or promote products derived
1553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *     from this software without specific prior written permission.
1653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *
1753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
1853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
2153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) */
2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
2953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#ifndef DatabaseTracker_h
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#define DatabaseTracker_h
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "modules/webdatabase/DatabaseError.h"
3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/HashMap.h"
3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/HashSet.h"
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/ThreadingPrimitives.h"
3653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/text/StringHash.h"
3753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "wtf/text/WTFString.h"
3853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
3953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)namespace WebCore {
4053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
4153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class DatabaseBackendBase;
4253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class DatabaseBackendContext;
4353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class OriginLock;
4453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class SecurityOrigin;
4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
4653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class DatabaseTracker {
4753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(DatabaseTracker); WTF_MAKE_FAST_ALLOCATED;
4853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)public:
4953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    static DatabaseTracker& tracker();
5053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    // This singleton will potentially be used from multiple worker threads and the page's context thread simultaneously.  To keep this safe, it's
5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    // currently using 4 locks.  In order to avoid deadlock when taking multiple locks, you must take them in the correct order:
5253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    // m_databaseGuard before quotaManager if both locks are needed.
5353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    // m_openDatabaseMapGuard before quotaManager if both locks are needed.
5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    // m_databaseGuard and m_openDatabaseMapGuard currently don't overlap.
5553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    // notificationMutex() is currently independent of the other locks.
5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
5753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    bool canEstablishDatabase(DatabaseBackendContext*, const String& name, const String& displayName, unsigned long estimatedSize, DatabaseError&);
5853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    String fullPathForDatabase(SecurityOrigin*, const String& name, bool createIfDoesNotExist = true);
5953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void addOpenDatabase(DatabaseBackendBase*);
6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void removeOpenDatabase(DatabaseBackendBase*);
6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void getOpenDatabases(SecurityOrigin*, const String& name, HashSet<RefPtr<DatabaseBackendBase> >* databases);
6353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    unsigned long long getMaxSizeForDatabase(const DatabaseBackendBase*);
6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void interruptAllDatabasesForContext(const DatabaseBackendContext*);
6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void closeDatabasesImmediately(const String& originIdentifier, const String& name);
6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void prepareToOpenDatabase(DatabaseBackendBase*);
7053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void failedToOpenDatabase(DatabaseBackendBase*);
7153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)private:
7353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    typedef HashSet<DatabaseBackendBase*> DatabaseSet;
7453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    typedef HashMap<String, DatabaseSet*> DatabaseNameMap;
7553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    typedef HashMap<String, DatabaseNameMap*> DatabaseOriginMap;
7653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    class CloseOneDatabaseImmediatelyTask;
7753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    DatabaseTracker();
7953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
8053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void closeOneDatabaseImmediately(const String& originIdentifier, const String& name, DatabaseBackendBase*);
8153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
8253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    Mutex m_openDatabaseMapGuard;
8353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    mutable OwnPtr<DatabaseOriginMap> m_openDatabaseMap;
8453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)};
8553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
8653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)} // namespace WebCore
8753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
8853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#endif // DatabaseTracker_h
89