#include "WebViewGroup.h"
#include "WebStorageNamespaceProvider.h"
#include "WebView.h"
#include "WebVisitedLinkStore.h"
#include <WebCore/UserContentController.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/StringHash.h>
using namespace WebCore;
static HashMap<String, RefPtr<WebViewGroup>>& webViewGroups()
{
static NeverDestroyed<HashMap<String, RefPtr<WebViewGroup>>> webViewGroups;
return webViewGroups;
}
RefPtr<WebViewGroup> WebViewGroup::getOrCreate(const String& name, const String& localStorageDatabasePath)
{
if (name.isEmpty())
return adoptRef(new WebViewGroup(String(), localStorageDatabasePath));
auto& webViewGroup = webViewGroups().add(name, nullptr).iterator->value;
if (!webViewGroup)
webViewGroup = adoptRef(new WebViewGroup(name, localStorageDatabasePath));
else if (!webViewGroup->m_storageNamespaceProvider && webViewGroup->m_localStorageDatabasePath.isEmpty() && !localStorageDatabasePath.isEmpty())
webViewGroup->m_localStorageDatabasePath = localStorageDatabasePath;
return webViewGroup;
}
WebViewGroup* WebViewGroup::get(const String& name)
{
ASSERT(!name.isEmpty());
return webViewGroups().get(name);
}
WebViewGroup::WebViewGroup(const String& name, const String& localStorageDatabasePath)
: m_name(name)
, m_localStorageDatabasePath(localStorageDatabasePath)
, m_userContentController(UserContentController::create())
, m_visitedLinkStore(WebVisitedLinkStore::create())
{
}
WebViewGroup::~WebViewGroup()
{
ASSERT(m_name.isEmpty());
ASSERT(m_webViews.isEmpty());
}
void WebViewGroup::addWebView(WebView *webView)
{
ASSERT(!m_webViews.contains(webView));
m_webViews.add(webView);
}
void WebViewGroup::removeWebView(WebView *webView)
{
ASSERT(m_webViews.contains(webView));
m_webViews.remove(webView);
}
StorageNamespaceProvider& WebViewGroup::storageNamespaceProvider()
{
if (!m_storageNamespaceProvider)
m_storageNamespaceProvider = WebKit::WebStorageNamespaceProvider::create(m_localStorageDatabasePath);
return *m_storageNamespaceProvider;
}