tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / storage / StorageAreaImpl.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 "StorageAreaImpl.h"
28
29 #include "Document.h"
30 #include "ExceptionCode.h"
31 #include "Frame.h"
32 #include "Page.h"
33 #include "SchemeRegistry.h"
34 #include "SecurityOrigin.h"
35 #include "Settings.h"
36 #include "StorageAreaSync.h"
37 #include "StorageEventDispatcher.h"
38 #include "StorageMap.h"
39 #include "StorageSyncManager.h"
40 #include "StorageTracker.h"
41 #include <wtf/MainThread.h>
42
43 namespace WebCore {
44
45 StorageAreaImpl::~StorageAreaImpl()
46 {
47     ASSERT(isMainThread());
48 }
49
50 inline StorageAreaImpl::StorageAreaImpl(StorageType storageType, PassRefPtr<SecurityOrigin> origin, PassRefPtr<StorageSyncManager> syncManager, unsigned quota)
51     : m_storageType(storageType)
52     , m_securityOrigin(origin)
53     , m_storageMap(StorageMap::create(quota))
54     , m_storageSyncManager(syncManager)
55 #ifndef NDEBUG
56     , m_isShutdown(false)
57 #endif
58 {
59     ASSERT(isMainThread());
60     ASSERT(m_securityOrigin);
61     ASSERT(m_storageMap);
62     
63     // Accessing the shared global StorageTracker when a StorageArea is created 
64     // ensures that the tracker is properly initialized before anyone actually needs to use it.
65     StorageTracker::tracker();
66 }
67
68 PassRefPtr<StorageAreaImpl> StorageAreaImpl::create(StorageType storageType, PassRefPtr<SecurityOrigin> origin, PassRefPtr<StorageSyncManager> syncManager, unsigned quota)
69 {
70     RefPtr<StorageAreaImpl> area = adoptRef(new StorageAreaImpl(storageType, origin, syncManager, quota));
71
72     // FIXME: If there's no backing storage for LocalStorage, the default WebKit behavior should be that of private browsing,
73     // not silently ignoring it. https://bugs.webkit.org/show_bug.cgi?id=25894
74     if (area->m_storageSyncManager) {
75         area->m_storageAreaSync = StorageAreaSync::create(area->m_storageSyncManager, area.get(), area->m_securityOrigin->databaseIdentifier());
76         ASSERT(area->m_storageAreaSync);
77     }
78
79     return area.release();
80 }
81
82 PassRefPtr<StorageAreaImpl> StorageAreaImpl::copy()
83 {
84     ASSERT(!m_isShutdown);
85     return adoptRef(new StorageAreaImpl(this));
86 }
87
88 StorageAreaImpl::StorageAreaImpl(StorageAreaImpl* area)
89     : m_storageType(area->m_storageType)
90     , m_securityOrigin(area->m_securityOrigin)
91     , m_storageMap(area->m_storageMap)
92     , m_storageSyncManager(area->m_storageSyncManager)
93 #ifndef NDEBUG
94     , m_isShutdown(area->m_isShutdown)
95 #endif
96 {
97     ASSERT(isMainThread());
98     ASSERT(m_securityOrigin);
99     ASSERT(m_storageMap);
100     ASSERT(!m_isShutdown);
101 }
102
103 bool StorageAreaImpl::disabledByPrivateBrowsingInFrame(const Frame* frame) const
104 {
105 #if PLATFORM(CHROMIUM)
106     // The frame pointer can be NULL in Chromium since this call is made in a different
107     // process from where the Frame object exists.  Luckily, private browseing is
108     // implemented differently in Chromium, so it'd never return true anyway.
109     ASSERT(!frame);
110     return false;
111 #else
112     if (!frame->page() || !frame->page()->settings()->privateBrowsingEnabled())
113         return false;
114     if (m_storageType != LocalStorage)
115         return true;
116     return !SchemeRegistry::allowsLocalStorageAccessInPrivateBrowsing(frame->document()->securityOrigin()->protocol());
117 #endif
118 }
119
120 unsigned StorageAreaImpl::length(Frame*) const
121 {
122     ASSERT(!m_isShutdown);
123     blockUntilImportComplete();
124
125     return m_storageMap->length();
126 }
127
128 String StorageAreaImpl::key(unsigned index, Frame*) const
129 {
130     ASSERT(!m_isShutdown);
131     blockUntilImportComplete();
132
133     return m_storageMap->key(index);
134 }
135
136 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
137 std::pair<String, bool> StorageAreaImpl::getItem(const String& key) const
138 #else
139 String StorageAreaImpl::getItem(const String& key, Frame*) const
140 #endif
141 {
142     ASSERT(!m_isShutdown);
143     blockUntilImportComplete();
144
145     return m_storageMap->getItem(key);
146 }
147
148 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
149 std::pair<String, bool> StorageAreaImpl::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame, bool readOnly)
150 #else
151 String StorageAreaImpl::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame)
152 #endif
153 {
154     ASSERT(!m_isShutdown);
155     ASSERT(!value.isNull());
156     blockUntilImportComplete();
157
158     if (disabledByPrivateBrowsingInFrame(frame)) {
159         ec = QUOTA_EXCEEDED_ERR;
160 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
161         return std::make_pair(String(), false);
162 #else
163         return String();
164 #endif
165     }
166
167 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
168     std::pair<String, bool> currentValue = m_storageMap->getItem(key);
169     if (!currentValue.first.isNull() && currentValue.second)
170     {
171         ec = NO_MODIFICATION_ALLOWED_ERR;
172         return std::make_pair(String(), false);
173     }
174 #endif
175
176 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
177     std::pair<String, bool> oldValue;
178     std::pair<String, bool> newValue = std::make_pair(value, readOnly);
179 #else
180     String oldValue;
181 #endif
182     bool quotaException;
183 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
184     RefPtr<StorageMap> newMap = m_storageMap->setItem(key, newValue, oldValue, quotaException);
185 #else
186     RefPtr<StorageMap> newMap = m_storageMap->setItem(key, value, oldValue, quotaException);
187 #endif
188     if (newMap)
189         m_storageMap = newMap.release();
190
191     if (quotaException) {
192         ec = QUOTA_EXCEEDED_ERR;
193         return oldValue;
194     }
195
196 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
197     if (oldValue == newValue)
198 #else
199     if (oldValue == value)
200 #endif
201         return oldValue;
202
203     if (m_storageAreaSync)
204 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
205         m_storageAreaSync->scheduleItemForSync(key, newValue);
206     StorageEventDispatcher::dispatch(key, oldValue.first, newValue.first, m_storageType, m_securityOrigin.get(), frame);
207 #else
208         m_storageAreaSync->scheduleItemForSync(key, value);
209     StorageEventDispatcher::dispatch(key, oldValue, value, m_storageType, m_securityOrigin.get(), frame);
210 #endif
211     return oldValue;
212 }
213
214 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
215 std::pair<String, bool> StorageAreaImpl::removeItem(const String& key, Frame* frame, ExceptionCode* ec)
216 #else
217 String StorageAreaImpl::removeItem(const String& key, Frame* frame)
218 #endif
219 {
220     ASSERT(!m_isShutdown);
221     blockUntilImportComplete();
222
223     if (disabledByPrivateBrowsingInFrame(frame))
224 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
225         return std::make_pair(String(), false);
226 #else
227         return String();
228 #endif
229
230 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
231     std::pair<String, bool> currentValue = m_storageMap->getItem(key);
232     if (!currentValue.first.isNull() && currentValue.second)
233     {
234        if (ec != NULL)
235            *ec = NO_MODIFICATION_ALLOWED_ERR;
236        return std::make_pair(String(), false);
237     }
238 #endif
239
240 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
241     std::pair<String, bool> oldValue;
242 #else
243     String oldValue;
244 #endif
245     RefPtr<StorageMap> newMap = m_storageMap->removeItem(key, oldValue);
246     if (newMap)
247         m_storageMap = newMap.release();
248
249 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
250     if (oldValue.first.isNull())
251 #else
252     if (oldValue.isNull())
253 #endif
254         return oldValue;
255
256     if (m_storageAreaSync)
257 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
258         m_storageAreaSync->scheduleItemForSync(key, std::make_pair(String(), false));
259     StorageEventDispatcher::dispatch(key, oldValue.first, String(), m_storageType, m_securityOrigin.get(), frame);
260 #else
261         m_storageAreaSync->scheduleItemForSync(key, String());
262     StorageEventDispatcher::dispatch(key, oldValue, String(), m_storageType, m_securityOrigin.get(), frame);
263 #endif
264     return oldValue;
265 }
266
267 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
268 bool StorageAreaImpl::clear(Frame* frame, bool removeReadOnly)
269 #else
270 bool StorageAreaImpl::clear(Frame* frame)
271 #endif
272 {
273     ASSERT(!m_isShutdown);
274     blockUntilImportComplete();
275
276     if (disabledByPrivateBrowsingInFrame(frame))
277         return false;
278
279     if (!m_storageMap->length())
280         return false;
281
282 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
283     if (removeReadOnly) {
284         unsigned quota = m_storageMap->quota();
285         m_storageMap = StorageMap::create(quota);
286     } else {
287         int numberItemsToRemove = 0;
288         for (int i = 0; i < m_storageMap->length(); ++i) {
289             std::pair<String, bool> item = m_storageMap->getItem(m_storageMap->key(i));
290             if (!item.second) {    // if attribute read only isn't set it will be removed
291                 std::pair<String, bool> oldValue;
292                 m_storageMap->removeItem(m_storageMap->key(i), oldValue);
293                 ++numberItemsToRemove;
294             }
295         }
296         if (!numberItemsToRemove)
297             return false;
298      }
299 #else
300     unsigned quota = m_storageMap->quota();
301     m_storageMap = StorageMap::create(quota);
302 #endif
303
304     if (m_storageAreaSync)
305 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
306         m_storageAreaSync->scheduleClear(removeReadOnly);
307 #else
308         m_storageAreaSync->scheduleClear();
309 #endif
310     StorageEventDispatcher::dispatch(String(), String(), String(), m_storageType, m_securityOrigin.get(), frame);
311     return true;
312 }
313
314 bool StorageAreaImpl::contains(const String& key, Frame*) const
315 {
316     ASSERT(!m_isShutdown);
317     blockUntilImportComplete();
318
319     return m_storageMap->contains(key);
320 }
321
322 #if ENABLE(TIZEN_LOCAL_STORAGE_READ_ONLY_EXTENSION)
323 void StorageAreaImpl::importItem(const String& key, const std::pair<String, bool>& value)
324 #else
325 void StorageAreaImpl::importItem(const String& key, const String& value)
326 #endif
327 {
328     ASSERT(!m_isShutdown);
329     m_storageMap->importItem(key, value);
330 }
331
332 void StorageAreaImpl::close()
333 {
334     if (m_storageAreaSync)
335         m_storageAreaSync->scheduleFinalSync();
336
337 #ifndef NDEBUG
338     m_isShutdown = true;
339 #endif
340 }
341
342 void StorageAreaImpl::clearForOriginDeletion()
343 {
344     ASSERT(!m_isShutdown);
345     blockUntilImportComplete();
346     
347     if (m_storageMap->length()) {
348         unsigned quota = m_storageMap->quota();
349         m_storageMap = StorageMap::create(quota);
350     }
351
352     if (m_storageAreaSync) {
353         m_storageAreaSync->scheduleClear();
354         m_storageAreaSync->scheduleCloseDatabase();
355     }
356 }
357     
358 void StorageAreaImpl::sync()
359 {
360     ASSERT(!m_isShutdown);
361     blockUntilImportComplete();
362     
363     if (m_storageAreaSync)
364         m_storageAreaSync->scheduleSync();
365 }
366
367 void StorageAreaImpl::blockUntilImportComplete() const
368 {
369     if (m_storageAreaSync)
370         m_storageAreaSync->blockUntilImportComplete();
371 }
372
373 }