1/*
2 * Copyright (C) 2010 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 "WebPageGroup.h"
28
29#include "APIArray.h"
30#include "APIContentRuleList.h"
31#include "APIUserScript.h"
32#include "APIUserStyleSheet.h"
33#include "WebCompiledContentRuleList.h"
34#include "WebPageProxy.h"
35#include "WebPreferences.h"
36#include "WebUserContentControllerProxy.h"
37#include <wtf/HashMap.h>
38#include <wtf/NeverDestroyed.h>
39#include <wtf/text/StringConcatenate.h>
40
41namespace WebKit {
42
43static uint64_t generatePageGroupID()
44{
45 static uint64_t uniquePageGroupID = 1;
46 return uniquePageGroupID++;
47}
48
49typedef HashMap<uint64_t, WebPageGroup*> WebPageGroupMap;
50
51static WebPageGroupMap& webPageGroupMap()
52{
53 static NeverDestroyed<WebPageGroupMap> map;
54 return map;
55}
56
57Ref<WebPageGroup> WebPageGroup::create(const String& identifier)
58{
59 return adoptRef(*new WebPageGroup(identifier));
60}
61
62WebPageGroup* WebPageGroup::get(uint64_t pageGroupID)
63{
64 return webPageGroupMap().get(pageGroupID);
65}
66
67static WebPageGroupData pageGroupData(const String& identifier)
68{
69 WebPageGroupData data;
70
71 data.pageGroupID = generatePageGroupID();
72
73 if (!identifier.isEmpty())
74 data.identifier = identifier;
75 else
76 data.identifier = makeString("__uniquePageGroupID-", data.pageGroupID);
77
78 return data;
79}
80
81// FIXME: Why does the WebPreferences object here use ".WebKit2" instead of "WebKit2." which all the other constructors use.
82// If it turns out that it's wrong, we can change it to to "WebKit2." and get rid of the globalDebugKeyPrefix from WebPreferences.
83WebPageGroup::WebPageGroup(const String& identifier)
84 : m_data(pageGroupData(identifier))
85 , m_preferences(WebPreferences::createWithLegacyDefaults(m_data.identifier, ".WebKit2", "WebKit2."))
86 , m_userContentController(WebUserContentControllerProxy::create())
87{
88 m_data.userContentControllerIdentifier = m_userContentController->identifier();
89
90 webPageGroupMap().set(m_data.pageGroupID, this);
91}
92
93WebPageGroup::~WebPageGroup()
94{
95 webPageGroupMap().remove(pageGroupID());
96}
97
98void WebPageGroup::addPage(WebPageProxy* page)
99{
100 m_pages.add(page);
101}
102
103void WebPageGroup::removePage(WebPageProxy* page)
104{
105 m_pages.remove(page);
106}
107
108void WebPageGroup::setPreferences(WebPreferences* preferences)
109{
110 if (preferences == m_preferences)
111 return;
112
113 m_preferences = preferences;
114
115 for (auto& webPageProxy : m_pages)
116 webPageProxy->setPreferences(*m_preferences);
117}
118
119WebPreferences& WebPageGroup::preferences() const
120{
121 return *m_preferences;
122}
123
124WebUserContentControllerProxy& WebPageGroup::userContentController()
125{
126 return m_userContentController;
127}
128
129} // namespace WebKit
130