• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "StringHash.h"
33 #include "StorageAreaImpl.h"
34 #include "StorageMap.h"
35 #include "StorageSyncManager.h"
36 #include <wtf/StdLibExtras.h>
37 
38 #ifdef ANDROID
39 #include "Page.h"
40 #endif
41 
42 namespace WebCore {
43 
44 typedef HashMap<String, StorageNamespace*> LocalStorageNamespaceMap;
45 
localStorageNamespaceMap()46 static LocalStorageNamespaceMap& localStorageNamespaceMap()
47 {
48     DEFINE_STATIC_LOCAL(LocalStorageNamespaceMap, localStorageNamespaceMap, ());
49     return localStorageNamespaceMap;
50 }
51 
localStorageNamespace(const String & path,unsigned quota)52 PassRefPtr<StorageNamespace> StorageNamespaceImpl::localStorageNamespace(const String& path, unsigned quota)
53 {
54     const String lookupPath = path.isNull() ? String("") : path;
55     LocalStorageNamespaceMap::iterator it = localStorageNamespaceMap().find(lookupPath);
56     if (it == localStorageNamespaceMap().end()) {
57         RefPtr<StorageNamespace> storageNamespace = adoptRef(new StorageNamespaceImpl(LocalStorage, lookupPath, quota));
58         localStorageNamespaceMap().set(lookupPath, storageNamespace.get());
59         return storageNamespace.release();
60     }
61 
62     return it->second;
63 }
64 
sessionStorageNamespace()65 PassRefPtr<StorageNamespace> StorageNamespaceImpl::sessionStorageNamespace()
66 {
67     return adoptRef(new StorageNamespaceImpl(SessionStorage, String(), StorageMap::noQuota));
68 }
69 
StorageNamespaceImpl(StorageType storageType,const String & path,unsigned quota)70 StorageNamespaceImpl::StorageNamespaceImpl(StorageType storageType, const String& path, unsigned quota)
71     : m_storageType(storageType)
72     , m_path(path.crossThreadString())
73     , m_syncManager(0)
74     , m_quota(quota)
75     , m_isShutdown(false)
76 {
77     if (m_storageType == LocalStorage && !m_path.isEmpty())
78         m_syncManager = StorageSyncManager::create(m_path);
79 }
80 
~StorageNamespaceImpl()81 StorageNamespaceImpl::~StorageNamespaceImpl()
82 {
83     ASSERT(isMainThread());
84 
85     if (m_storageType == LocalStorage) {
86         ASSERT(localStorageNamespaceMap().get(m_path) == this);
87         localStorageNamespaceMap().remove(m_path);
88     }
89 
90     if (!m_isShutdown)
91         close();
92 }
93 
copy()94 PassRefPtr<StorageNamespace> StorageNamespaceImpl::copy()
95 {
96     ASSERT(isMainThread());
97     ASSERT(!m_isShutdown);
98     ASSERT(m_storageType == SessionStorage);
99 
100     StorageNamespaceImpl* newNamespace = new StorageNamespaceImpl(m_storageType, m_path, m_quota);
101 
102     StorageAreaMap::iterator end = m_storageAreaMap.end();
103     for (StorageAreaMap::iterator i = m_storageAreaMap.begin(); i != end; ++i)
104         newNamespace->m_storageAreaMap.set(i->first, i->second->copy());
105     return adoptRef(newNamespace);
106 }
107 
storageArea(PassRefPtr<SecurityOrigin> prpOrigin)108 PassRefPtr<StorageArea> StorageNamespaceImpl::storageArea(PassRefPtr<SecurityOrigin> prpOrigin)
109 {
110     ASSERT(isMainThread());
111     ASSERT(!m_isShutdown);
112 
113     RefPtr<SecurityOrigin> origin = prpOrigin;
114     RefPtr<StorageAreaImpl> storageArea;
115     if (storageArea = m_storageAreaMap.get(origin))
116         return storageArea.release();
117 
118     storageArea = StorageAreaImpl::create(m_storageType, origin, m_syncManager, m_quota);
119     m_storageAreaMap.set(origin.release(), storageArea);
120     return storageArea.release();
121 }
122 
close()123 void StorageNamespaceImpl::close()
124 {
125     ASSERT(isMainThread());
126     ASSERT(!m_isShutdown);
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 #ifdef ANDROID
clear(Page * page)145 void StorageNamespaceImpl::clear(Page* page)
146 {
147     ASSERT(isMainThread());
148     if (m_isShutdown)
149         return;
150 
151     // Clear all the keys for each of the storage areas.
152     StorageAreaMap::iterator end = m_storageAreaMap.end();
153     for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it) {
154         // if there is no page provided, then the user tried to clear storage
155         // with only pages in private browsing mode open. So we do not need to
156         // provide a Frame* here (as the frame is only used to dispatch storage events
157         // and private browsing pages won't be using them).
158         it->second->clear(page ? page->mainFrame() : 0);
159     }
160 }
161 #endif
162 
unlock()163 void StorageNamespaceImpl::unlock()
164 {
165     // Because there's a single event loop per-process, this is a no-op.
166 }
167 
168 } // namespace WebCore
169 
170 #endif // ENABLE(DOM_STORAGE)
171