2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "WebViewGroup.h"
28 #include "WebStorageNamespaceProvider.h"
30 #include "WebVisitedLinkStore.h"
31 #include <WebCore/UserContentController.h>
32 #include <wtf/NeverDestroyed.h>
33 #include <wtf/text/StringHash.h>
35 using namespace WebCore;
37 // Any named groups will live for the lifetime of the process, thanks to the reference held by the RefPtr.
38 static HashMap<String, RefPtr<WebViewGroup>>& webViewGroups()
40 static NeverDestroyed<HashMap<String, RefPtr<WebViewGroup>>> webViewGroups;
45 RefPtr<WebViewGroup> WebViewGroup::getOrCreate(const String& name, const String& localStorageDatabasePath)
48 return adoptRef(new WebViewGroup(String(), localStorageDatabasePath));
50 auto& webViewGroup = webViewGroups().add(name, nullptr).iterator->value;
52 webViewGroup = adoptRef(new WebViewGroup(name, localStorageDatabasePath));
53 else if (!webViewGroup->m_storageNamespaceProvider && webViewGroup->m_localStorageDatabasePath.isEmpty() && !localStorageDatabasePath.isEmpty())
54 webViewGroup->m_localStorageDatabasePath = localStorageDatabasePath;
59 WebViewGroup* WebViewGroup::get(const String& name)
61 ASSERT(!name.isEmpty());
63 return webViewGroups().get(name);
66 WebViewGroup::WebViewGroup(const String& name, const String& localStorageDatabasePath)
68 , m_localStorageDatabasePath(localStorageDatabasePath)
69 , m_userContentController(UserContentController::create())
70 , m_visitedLinkStore(WebVisitedLinkStore::create())
74 WebViewGroup::~WebViewGroup()
76 ASSERT(m_name.isEmpty());
77 ASSERT(m_webViews.isEmpty());
80 void WebViewGroup::addWebView(WebView *webView)
82 ASSERT(!m_webViews.contains(webView));
84 m_webViews.add(webView);
87 void WebViewGroup::removeWebView(WebView *webView)
89 ASSERT(m_webViews.contains(webView));
91 m_webViews.remove(webView);
94 StorageNamespaceProvider& WebViewGroup::storageNamespaceProvider()
96 if (!m_storageNamespaceProvider)
97 m_storageNamespaceProvider = WebStorageNamespaceProvider::create(m_localStorageDatabasePath);
99 return *m_storageNamespaceProvider;