1 | /* |
2 | * Copyright (C) 2018 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 "NetworkContentRuleListManager.h" |
28 | |
29 | #if ENABLE(CONTENT_EXTENSIONS) |
30 | |
31 | #include "NetworkProcess.h" |
32 | #include "NetworkProcessProxyMessages.h" |
33 | #include "WebCompiledContentRuleList.h" |
34 | |
35 | namespace WebKit { |
36 | using namespace WebCore; |
37 | |
38 | NetworkContentRuleListManager::NetworkContentRuleListManager(NetworkProcess& networkProcess) |
39 | : m_networkProcess(networkProcess) |
40 | { |
41 | } |
42 | |
43 | NetworkContentRuleListManager::~NetworkContentRuleListManager() |
44 | { |
45 | auto pendingCallbacks = WTFMove(m_pendingCallbacks); |
46 | if (pendingCallbacks.isEmpty()) |
47 | return; |
48 | |
49 | WebCore::ContentExtensions::ContentExtensionsBackend backend; |
50 | for (auto& callbacks : pendingCallbacks.values()) { |
51 | for (auto& callback : callbacks) |
52 | callback(backend); |
53 | } |
54 | } |
55 | |
56 | void NetworkContentRuleListManager::contentExtensionsBackend(UserContentControllerIdentifier identifier, BackendCallback&& callback) |
57 | { |
58 | auto iterator = m_contentExtensionBackends.find(identifier); |
59 | if (iterator != m_contentExtensionBackends.end()) { |
60 | callback(*iterator->value); |
61 | return; |
62 | } |
63 | m_pendingCallbacks.ensure(identifier, [] { |
64 | return Vector<BackendCallback> { }; |
65 | }).iterator->value.append(WTFMove(callback)); |
66 | m_networkProcess.parentProcessConnection()->send(Messages::NetworkProcessProxy::ContentExtensionRules { identifier }, 0); |
67 | } |
68 | |
69 | void NetworkContentRuleListManager::addContentRuleLists(UserContentControllerIdentifier identifier, Vector<std::pair<String, WebCompiledContentRuleListData>>&& contentRuleLists) |
70 | { |
71 | auto& backend = *m_contentExtensionBackends.ensure(identifier, [] { |
72 | return std::make_unique<WebCore::ContentExtensions::ContentExtensionsBackend>(); |
73 | }).iterator->value; |
74 | |
75 | for (auto&& contentRuleList : contentRuleLists) { |
76 | auto compiledContentRuleList = WebCompiledContentRuleList::create(WTFMove(contentRuleList.second)); |
77 | backend.addContentExtension(contentRuleList.first, WTFMove(compiledContentRuleList), ContentExtensions::ContentExtension::ShouldCompileCSS::No); |
78 | } |
79 | |
80 | auto pendingCallbacks = m_pendingCallbacks.take(identifier); |
81 | for (auto& callback : pendingCallbacks) |
82 | callback(backend); |
83 | |
84 | } |
85 | |
86 | void NetworkContentRuleListManager::removeContentRuleList(UserContentControllerIdentifier identifier, const String& name) |
87 | { |
88 | auto iterator = m_contentExtensionBackends.find(identifier); |
89 | if (iterator == m_contentExtensionBackends.end()) |
90 | return; |
91 | |
92 | iterator->value->removeContentExtension(name); |
93 | } |
94 | |
95 | void NetworkContentRuleListManager::removeAllContentRuleLists(UserContentControllerIdentifier identifier) |
96 | { |
97 | auto iterator = m_contentExtensionBackends.find(identifier); |
98 | if (iterator == m_contentExtensionBackends.end()) |
99 | return; |
100 | |
101 | iterator->value->removeAllContentExtensions(); |
102 | } |
103 | |
104 | void NetworkContentRuleListManager::remove(UserContentControllerIdentifier identifier) |
105 | { |
106 | m_contentExtensionBackends.remove(identifier); |
107 | } |
108 | |
109 | } // namespace WebKit |
110 | |
111 | #endif // ENABLE(CONTENT_EXTENSIONS) |
112 | |