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