• 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 "LocalStorage.h"
28 
29 #include "CString.h"
30 #include "EventNames.h"
31 #include "FileSystem.h"
32 #include "Frame.h"
33 #include "FrameTree.h"
34 #include "LocalStorageArea.h"
35 #include "Page.h"
36 #include "PageGroup.h"
37 #include "StorageArea.h"
38 #include <wtf/StdLibExtras.h>
39 
40 namespace WebCore {
41 
42 typedef HashMap<String, LocalStorage*> LocalStorageMap;
43 
localStorageMap()44 static LocalStorageMap& localStorageMap()
45 {
46     DEFINE_STATIC_LOCAL(LocalStorageMap, localStorageMap, ());
47     return localStorageMap;
48 }
49 
localStorage(const String & path)50 PassRefPtr<LocalStorage> LocalStorage::localStorage(const String& path)
51 {
52     const String lookupPath = path.isNull() ? String("") : path;
53     LocalStorageMap::iterator it = localStorageMap().find(lookupPath);
54     if (it == localStorageMap().end()) {
55         RefPtr<LocalStorage> localStorage = adoptRef(new LocalStorage(lookupPath));
56         localStorageMap().set(lookupPath, localStorage.get());
57         return localStorage.release();
58     }
59 
60     return it->second;
61 }
62 
LocalStorage(const String & path)63 LocalStorage::LocalStorage(const String& path)
64     : m_path(path.copy())
65 {
66     // If the path is empty, we know we're never going to be using the thread for anything, so don't start it.
67     // In the future, we might also want to consider removing it from the DOM in that case - <rdar://problem/5960470>
68     if (path.isEmpty())
69         return;
70 
71     m_thread = LocalStorageThread::create();
72     m_thread->start();
73     m_thread->scheduleImport(this);
74 }
75 
~LocalStorage()76 LocalStorage::~LocalStorage()
77 {
78     ASSERT(localStorageMap().get(m_path) == this);
79     localStorageMap().remove(m_path);
80 }
81 
storageArea(SecurityOrigin * origin)82 PassRefPtr<StorageArea> LocalStorage::storageArea(SecurityOrigin* origin)
83 {
84     ASSERT(isMainThread());
85 
86     // FIXME: If the security origin in question has never had a storage area established,
87     // we need to ask a client call if establishing it is okay.  If the client denies the request,
88     // this method will return null.
89     // The sourceFrame argument exists for the purpose of asking a client.
90     // To know if an area has previously been established, we need to wait until this LocalStorage
91     // object has finished it's AreaImport task.
92 
93 
94     // FIXME: If the storage area is being established for the first time here, we need to
95     // sync its existance and quota out to disk via an task of type AreaSync
96 
97     RefPtr<LocalStorageArea> storageArea;
98     if (storageArea = m_storageAreaMap.get(origin))
99         return storageArea.release();
100 
101     storageArea = LocalStorageArea::create(origin, this);
102     m_storageAreaMap.set(origin, storageArea);
103     return storageArea.release();
104 }
105 
fullDatabaseFilename(SecurityOrigin * origin)106 String LocalStorage::fullDatabaseFilename(SecurityOrigin* origin)
107 {
108     // FIXME: Once we actually track origin/quota entries to see which origins have local storage established,
109     // we will return an empty path name if the origin isn't allowed to have LocalStorage.
110     // We'll need to wait here until the AreaImport task to complete before making that decision.
111 
112     if (m_path.isEmpty())
113         return String();
114 
115     ASSERT(origin);
116     if (!origin)
117         return String();
118 
119     if (!makeAllDirectories(m_path)) {
120         LOG_ERROR("Unabled to create LocalStorage database path %s", m_path.utf8().data());
121         return String();
122     }
123 
124     return pathByAppendingComponent(m_path, origin->databaseIdentifier() + ".localstorage");
125 }
126 
performImport()127 void LocalStorage::performImport()
128 {
129     ASSERT(!isMainThread());
130 
131     // FIXME: Import all known local storage origins here along with their quotas
132 }
133 
performSync()134 void LocalStorage::performSync()
135 {
136     ASSERT(!isMainThread());
137 
138     // FIXME: Write out new origins and quotas here
139 }
140 
close()141 void LocalStorage::close()
142 {
143     ASSERT(isMainThread());
144 
145     LocalStorageAreaMap::iterator end = m_storageAreaMap.end();
146     for (LocalStorageAreaMap::iterator it = m_storageAreaMap.begin(); it != end; ++it)
147         it->second->scheduleFinalSync();
148 
149     if (m_thread) {
150         m_thread->terminate();
151         m_thread = 0;
152     }
153 }
154 
scheduleImport(PassRefPtr<LocalStorageArea> area)155 bool LocalStorage::scheduleImport(PassRefPtr<LocalStorageArea> area)
156 {
157     ASSERT(isMainThread());
158 
159     if (m_thread)
160         m_thread->scheduleImport(area);
161 
162     return m_thread;
163 }
164 
scheduleSync(PassRefPtr<LocalStorageArea> area)165 void LocalStorage::scheduleSync(PassRefPtr<LocalStorageArea> area)
166 {
167     ASSERT(isMainThread());
168     if (m_thread)
169         m_thread->scheduleSync(area);
170 }
171 
172 } // namespace WebCore
173