• 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 "StorageSyncManager.h"
35 #include <wtf/StdLibExtras.h>
36 
37 namespace WebCore {
38 
39 typedef HashMap<String, StorageNamespace*> LocalStorageNamespaceMap;
40 
localStorageNamespaceMap()41 static LocalStorageNamespaceMap& localStorageNamespaceMap()
42 {
43     DEFINE_STATIC_LOCAL(LocalStorageNamespaceMap, localStorageNamespaceMap, ());
44     return localStorageNamespaceMap;
45 }
46 
localStorageNamespace(const String & path)47 PassRefPtr<StorageNamespace> StorageNamespaceImpl::localStorageNamespace(const String& path)
48 {
49     const String lookupPath = path.isNull() ? String("") : path;
50     LocalStorageNamespaceMap::iterator it = localStorageNamespaceMap().find(lookupPath);
51     if (it == localStorageNamespaceMap().end()) {
52         RefPtr<StorageNamespace> storageNamespace = adoptRef(new StorageNamespaceImpl(LocalStorage, lookupPath));
53         localStorageNamespaceMap().set(lookupPath, storageNamespace.get());
54         return storageNamespace.release();
55     }
56 
57     return it->second;
58 }
59 
sessionStorageNamespace()60 PassRefPtr<StorageNamespace> StorageNamespaceImpl::sessionStorageNamespace()
61 {
62     return adoptRef(new StorageNamespaceImpl(SessionStorage, String()));
63 }
64 
StorageNamespaceImpl(StorageType storageType,const String & path)65 StorageNamespaceImpl::StorageNamespaceImpl(StorageType storageType, const String& path)
66     : m_storageType(storageType)
67     , m_path(path.copy())  // Copy makes it safe for our other thread to access the path.
68     , m_syncManager(0)
69     , m_isShutdown(false)
70 {
71     if (m_storageType == LocalStorage && !m_path.isEmpty())
72         m_syncManager = StorageSyncManager::create(m_path);
73 }
74 
~StorageNamespaceImpl()75 StorageNamespaceImpl::~StorageNamespaceImpl()
76 {
77     ASSERT(isMainThread());
78 
79     if (m_storageType == LocalStorage) {
80         ASSERT(localStorageNamespaceMap().get(m_path) == this);
81         localStorageNamespaceMap().remove(m_path);
82     }
83 
84     if (!m_isShutdown)
85         close();
86 }
87 
copy()88 PassRefPtr<StorageNamespace> StorageNamespaceImpl::copy()
89 {
90     ASSERT(isMainThread());
91     ASSERT(!m_isShutdown);
92     ASSERT(m_storageType == SessionStorage);
93 
94     StorageNamespaceImpl* newNamespace = new StorageNamespaceImpl(m_storageType, m_path);
95 
96     StorageAreaMap::iterator end = m_storageAreaMap.end();
97     for (StorageAreaMap::iterator i = m_storageAreaMap.begin(); i != end; ++i)
98         newNamespace->m_storageAreaMap.set(i->first, i->second->copy());
99     return adoptRef(newNamespace);
100 }
101 
storageArea(SecurityOrigin * origin)102 PassRefPtr<StorageArea> StorageNamespaceImpl::storageArea(SecurityOrigin* origin)
103 {
104     ASSERT(isMainThread());
105     ASSERT(!m_isShutdown);
106 
107     RefPtr<StorageAreaImpl> storageArea;
108     if (storageArea = m_storageAreaMap.get(origin))
109         return storageArea.release();
110 
111     storageArea = adoptRef(new StorageAreaImpl(m_storageType, origin, m_syncManager));
112     m_storageAreaMap.set(origin, storageArea);
113     return storageArea.release();
114 }
115 
close()116 void StorageNamespaceImpl::close()
117 {
118     ASSERT(isMainThread());
119     ASSERT(!m_isShutdown);
120 
121     // If we're session storage, we shouldn't need to do any work here.
122     if (m_storageType == SessionStorage) {
123         ASSERT(!m_syncManager);
124         return;
125     }
126 
127     StorageAreaMap::iterator end = m_storageAreaMap.end();
128     for (StorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
129         it->second->close();
130 
131     if (m_syncManager)
132         m_syncManager->close();
133 
134     m_isShutdown = true;
135 }
136 
137 } // namespace WebCore
138 
139 #endif // ENABLE(DOM_STORAGE)
140