initial import
[vuplus_webkit] / Source / WebCore / storage / StorageNamespaceImpl.cpp
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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "StorageNamespaceImpl.h"
28
29 #if ENABLE(DOM_STORAGE)
30
31 #include "SecurityOriginHash.h"
32 #include "StorageAreaImpl.h"
33 #include "StorageMap.h"
34 #include "StorageSyncManager.h"
35 #include "StorageTracker.h"
36 #include <wtf/MainThread.h>
37 #include <wtf/StdLibExtras.h>
38 #include <wtf/text/StringHash.h>
39
40 namespace WebCore {
41
42 typedef HashMap<String, StorageNamespace*> LocalStorageNamespaceMap;
43
44 static LocalStorageNamespaceMap& localStorageNamespaceMap()
45 {
46     DEFINE_STATIC_LOCAL(LocalStorageNamespaceMap, localStorageNamespaceMap, ());
47     return localStorageNamespaceMap;
48 }
49
50 PassRefPtr<StorageNamespace> StorageNamespaceImpl::localStorageNamespace(const String& path, unsigned quota)
51 {
52     const String lookupPath = path.isNull() ? String("") : path;
53     LocalStorageNamespaceMap::iterator it = localStorageNamespaceMap().find(lookupPath);
54     if (it == localStorageNamespaceMap().end()) {
55         RefPtr<StorageNamespace> storageNamespace = adoptRef(new StorageNamespaceImpl(LocalStorage, lookupPath, quota));
56         localStorageNamespaceMap().set(lookupPath, storageNamespace.get());
57         return storageNamespace.release();
58     }
59
60     return it->second;
61 }
62
63 PassRefPtr<StorageNamespace> StorageNamespaceImpl::sessionStorageNamespace(unsigned quota)
64 {
65     return adoptRef(new StorageNamespaceImpl(SessionStorage, String(), quota));
66 }
67
68 StorageNamespaceImpl::StorageNamespaceImpl(StorageType storageType, const String& path, unsigned quota)
69     : m_storageType(storageType)
70     , m_path(path.crossThreadString())
71     , m_syncManager(0)
72     , m_quota(quota)
73     , m_isShutdown(false)
74 {
75     if (m_storageType == LocalStorage && !m_path.isEmpty())
76         m_syncManager = StorageSyncManager::create(m_path);
77 }
78
79 StorageNamespaceImpl::~StorageNamespaceImpl()
80 {
81     ASSERT(isMainThread());
82
83     if (m_storageType == LocalStorage) {
84         ASSERT(localStorageNamespaceMap().get(m_path) == this);
85         localStorageNamespaceMap().remove(m_path);
86     }
87
88     if (!m_isShutdown)
89         close();
90 }
91
92 PassRefPtr<StorageNamespace> StorageNamespaceImpl::copy()
93 {
94     ASSERT(isMainThread());
95     ASSERT(!m_isShutdown);
96     ASSERT(m_storageType == SessionStorage);
97
98     RefPtr<StorageNamespaceImpl> newNamespace = adoptRef(new StorageNamespaceImpl(m_storageType, m_path, m_quota));
99
100     StorageAreaMap::iterator end = m_storageAreaMap.end();
101     for (StorageAreaMap::iterator i = m_storageAreaMap.begin(); i != end; ++i)
102         newNamespace->m_storageAreaMap.set(i->first, i->second->copy());
103     return newNamespace.release();
104 }
105
106 PassRefPtr<StorageArea> StorageNamespaceImpl::storageArea(PassRefPtr<SecurityOrigin> prpOrigin)
107 {
108     ASSERT(isMainThread());
109     ASSERT(!m_isShutdown);
110
111     RefPtr<SecurityOrigin> origin = prpOrigin;
112     RefPtr<StorageAreaImpl> storageArea;
113     if ((storageArea = m_storageAreaMap.get(origin)))
114         return storageArea.release();
115
116     storageArea = StorageAreaImpl::create(m_storageType, origin, m_syncManager, m_quota);
117     m_storageAreaMap.set(origin.release(), storageArea);
118     return storageArea.release();
119 }
120
121 void StorageNamespaceImpl::close()
122 {
123     ASSERT(isMainThread());
124
125     if (m_isShutdown)
126         return;
127
128     // If we're session storage, we shouldn't need to do any work here.
129     if (m_storageType == SessionStorage) {
130         ASSERT(!m_syncManager);
131         return;
132     }
133
134     StorageAreaMap::iterator end = m_storageAreaMap.end();
135     for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
136         it->second->close();
137
138     if (m_syncManager)
139         m_syncManager->close();
140
141     m_isShutdown = true;
142 }
143
144 void StorageNamespaceImpl::unlock()
145 {
146     // Because there's a single event loop per-process, this is a no-op.
147 }
148
149 void StorageNamespaceImpl::clearOriginForDeletion(SecurityOrigin* origin)
150 {
151     ASSERT(isMainThread());
152
153     RefPtr<StorageAreaImpl> storageArea = m_storageAreaMap.get(origin);
154     if (storageArea)
155         storageArea->clearForOriginDeletion();
156 }
157
158 void StorageNamespaceImpl::clearAllOriginsForDeletion()
159 {
160     ASSERT(isMainThread());
161
162     StorageAreaMap::iterator end = m_storageAreaMap.end();
163     for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
164         it->second->clearForOriginDeletion();
165 }
166     
167 void StorageNamespaceImpl::sync()
168 {
169     ASSERT(isMainThread());
170     StorageAreaMap::iterator end = m_storageAreaMap.end();
171     for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
172         it->second->sync();
173 }
174
175 } // namespace WebCore
176
177 #endif // ENABLE(DOM_STORAGE)