1/*
2 * Copyright (C) 2013 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "StorageNamespaceImpl.h"
28
29#include "StorageAreaImpl.h"
30#include "StorageAreaMap.h"
31#include "WebPage.h"
32#include "WebPageGroupProxy.h"
33#include "WebProcess.h"
34#include <WebCore/GroupSettings.h>
35#include <WebCore/PageGroup.h>
36#include <WebCore/SecurityOrigin.h>
37#include <WebCore/Settings.h>
38#include <wtf/NeverDestroyed.h>
39
40using namespace WebCore;
41
42namespace WebKit {
43
44typedef HashMap<uint64_t, StorageNamespaceImpl*> LocalStorageNamespaceMap;
45
46static LocalStorageNamespaceMap& localStorageNamespaceMap()
47{
48    static NeverDestroyed<LocalStorageNamespaceMap> localStorageNamespaceMap;
49    return localStorageNamespaceMap;
50}
51
52PassRefPtr<StorageNamespaceImpl> StorageNamespaceImpl::createLocalStorageNamespace(PageGroup* pageGroup)
53{
54    uint64_t pageGroupID = WebProcess::shared().webPageGroup(pageGroup)->pageGroupID();
55
56    LocalStorageNamespaceMap::AddResult result = localStorageNamespaceMap().add(pageGroupID, nullptr);
57    if (!result.isNewEntry)
58        return result.iterator->value;
59
60    unsigned quota = pageGroup->groupSettings().localStorageQuotaBytes();
61    RefPtr<StorageNamespaceImpl> localStorageNamespace = adoptRef(new StorageNamespaceImpl(LocalStorage, pageGroupID, quota));
62
63    result.iterator->value = localStorageNamespace.get();
64    return localStorageNamespace.release();
65}
66
67PassRefPtr<StorageNamespaceImpl> StorageNamespaceImpl::createSessionStorageNamespace(WebPage* webPage)
68{
69    return adoptRef(new StorageNamespaceImpl(SessionStorage, webPage->pageID(), webPage->corePage()->settings().sessionStorageQuota()));
70}
71
72StorageNamespaceImpl::StorageNamespaceImpl(WebCore::StorageType storageType, uint64_t storageNamespaceID, unsigned quotaInBytes)
73    : m_storageType(storageType)
74    , m_storageNamespaceID(storageNamespaceID)
75    , m_quotaInBytes(quotaInBytes)
76{
77}
78
79StorageNamespaceImpl::~StorageNamespaceImpl()
80{
81    if (m_storageType == LocalStorage) {
82        ASSERT(localStorageNamespaceMap().contains(m_storageNamespaceID));
83        localStorageNamespaceMap().remove(m_storageNamespaceID);
84    }
85}
86
87PassRefPtr<StorageArea> StorageNamespaceImpl::storageArea(PassRefPtr<SecurityOrigin> securityOrigin)
88{
89    auto result = m_storageAreaMaps.add(securityOrigin.get(), nullptr);
90    if (result.isNewEntry)
91        result.iterator->value = StorageAreaMap::create(this, securityOrigin);
92
93    return StorageAreaImpl::create(result.iterator->value);
94}
95
96PassRefPtr<StorageNamespace> StorageNamespaceImpl::copy(Page* newPage)
97{
98    ASSERT(m_storageNamespaceID);
99
100    return createSessionStorageNamespace(WebPage::fromCorePage(newPage));
101}
102
103void StorageNamespaceImpl::close()
104{
105    // FIXME: Implement this.
106    ASSERT_NOT_REACHED();
107}
108
109void StorageNamespaceImpl::clearOriginForDeletion(SecurityOrigin*)
110{
111    // FIXME: Implement this.
112    ASSERT_NOT_REACHED();
113}
114
115void StorageNamespaceImpl::clearAllOriginsForDeletion()
116{
117    // FIXME: Implement this.
118    ASSERT_NOT_REACHED();
119}
120
121void StorageNamespaceImpl::sync()
122{
123    // FIXME: Implement this.
124    ASSERT_NOT_REACHED();
125}
126
127void StorageNamespaceImpl::closeIdleLocalStorageDatabases()
128{
129    // FIXME: Implement this.
130    ASSERT_NOT_REACHED();
131}
132
133} // namespace WebKit
134