1/*
2 * Copyright (C) 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#include "config.h"
29#include "OriginQuotaManager.h"
30
31#if ENABLE(DATABASE)
32
33#include "AbstractDatabase.h"
34#include "OriginUsageRecord.h"
35
36namespace WebCore {
37
38OriginQuotaManager::OriginQuotaManager()
39#ifndef NDEBUG
40    : m_usageRecordGuardLocked(false)
41#endif
42{
43}
44
45bool OriginQuotaManager::tryLock()
46{
47    bool locked = m_usageRecordGuard.tryLock();
48#ifndef NDEBUG
49    if (locked)
50        m_usageRecordGuardLocked = true;
51    else
52        ASSERT(m_usageRecordGuardLocked);
53#endif
54    return locked;
55}
56
57void OriginQuotaManager::lock()
58{
59    m_usageRecordGuard.lock();
60#ifndef NDEBUG
61    m_usageRecordGuardLocked = true;
62#endif
63}
64
65void OriginQuotaManager::unlock()
66{
67#ifndef NDEBUG
68    m_usageRecordGuardLocked = false;
69#endif
70    m_usageRecordGuard.unlock();
71}
72
73void OriginQuotaManager::trackOrigin(PassRefPtr<SecurityOrigin> origin)
74{
75    ASSERT(m_usageRecordGuardLocked);
76    ASSERT(!m_usageMap.contains(origin.get()));
77
78    m_usageMap.set(origin->threadsafeCopy(), new OriginUsageRecord);
79}
80
81bool OriginQuotaManager::tracksOrigin(SecurityOrigin* origin) const
82{
83    ASSERT(m_usageRecordGuardLocked);
84    return m_usageMap.contains(origin);
85}
86
87void OriginQuotaManager::addDatabase(SecurityOrigin* origin, const String& databaseIdentifier, const String& fullPath)
88{
89    ASSERT(m_usageRecordGuardLocked);
90
91    OriginUsageRecord* usageRecord = m_usageMap.get(origin);
92    ASSERT(usageRecord);
93
94    usageRecord->addDatabase(databaseIdentifier.threadsafeCopy(), fullPath.threadsafeCopy());
95}
96
97void OriginQuotaManager::removeDatabase(SecurityOrigin* origin, const String& databaseIdentifier)
98{
99    ASSERT(m_usageRecordGuardLocked);
100
101    if (OriginUsageRecord* usageRecord = m_usageMap.get(origin))
102        usageRecord->removeDatabase(databaseIdentifier);
103}
104
105void OriginQuotaManager::removeOrigin(SecurityOrigin* origin)
106{
107    ASSERT(m_usageRecordGuardLocked);
108
109    if (OriginUsageRecord* usageRecord = m_usageMap.get(origin)) {
110        m_usageMap.remove(origin);
111        delete usageRecord;
112    }
113}
114
115void OriginQuotaManager::markDatabase(AbstractDatabase* database)
116{
117    ASSERT(database);
118    ASSERT(m_usageRecordGuardLocked);
119    OriginUsageRecord* usageRecord = m_usageMap.get(database->securityOrigin());
120    ASSERT(usageRecord);
121
122    usageRecord->markDatabase(database->stringIdentifier());
123}
124
125unsigned long long OriginQuotaManager::diskUsage(SecurityOrigin* origin) const
126{
127    ASSERT(m_usageRecordGuardLocked);
128
129    OriginUsageRecord* usageRecord = m_usageMap.get(origin);
130    ASSERT(usageRecord);
131
132    return usageRecord->diskUsage();
133}
134
135}
136
137#endif // ENABLE(DATABASE)
138