1/*
2 * Copyright (C) 2014 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#pragma once
27
28#include "MessageReceiver.h"
29#include "UserContentControllerIdentifier.h"
30#include "WebScriptMessageHandler.h"
31#include "WebUserContentControllerDataTypes.h"
32#include <WebCore/UserContentProvider.h>
33#include <wtf/HashMap.h>
34
35#if ENABLE(CONTENT_EXTENSIONS)
36#include <WebCore/ContentExtensionsBackend.h>
37#endif
38
39namespace WebCore {
40namespace ContentExtensions {
41class CompiledContentExtension;
42}
43}
44
45namespace WebKit {
46
47class InjectedBundleScriptWorld;
48class WebCompiledContentRuleListData;
49class WebUserMessageHandlerDescriptorProxy;
50enum class InjectUserScriptImmediately : bool;
51
52class WebUserContentController final : public WebCore::UserContentProvider, private IPC::MessageReceiver {
53public:
54 static Ref<WebUserContentController> getOrCreate(UserContentControllerIdentifier);
55 virtual ~WebUserContentController();
56
57 UserContentControllerIdentifier identifier() { return m_identifier; }
58
59 void addUserScript(InjectedBundleScriptWorld&, WebCore::UserScript&&);
60 void removeUserScriptWithURL(InjectedBundleScriptWorld&, const URL&);
61 void removeUserScripts(InjectedBundleScriptWorld&);
62 void addUserStyleSheet(InjectedBundleScriptWorld&, WebCore::UserStyleSheet&&);
63 void removeUserStyleSheetWithURL(InjectedBundleScriptWorld&, const URL&);
64 void removeUserStyleSheets(InjectedBundleScriptWorld&);
65 void removeAllUserContent();
66
67 void addUserContentWorlds(const Vector<std::pair<uint64_t, String>>&);
68 void addUserScripts(Vector<WebUserScriptData>&&, InjectUserScriptImmediately);
69 void addUserStyleSheets(const Vector<WebUserStyleSheetData>&);
70 void addUserScriptMessageHandlers(const Vector<WebScriptMessageHandlerData>&);
71#if ENABLE(CONTENT_EXTENSIONS)
72 void addContentRuleLists(Vector<std::pair<String, WebCompiledContentRuleListData>>&&);
73#endif
74
75private:
76 explicit WebUserContentController(UserContentControllerIdentifier);
77
78 // WebCore::UserContentProvider
79 void forEachUserScript(Function<void(WebCore::DOMWrapperWorld&, const WebCore::UserScript&)>&&) const final;
80 void forEachUserStyleSheet(Function<void(const WebCore::UserStyleSheet&)>&&) const final;
81#if ENABLE(USER_MESSAGE_HANDLERS)
82 void forEachUserMessageHandler(Function<void(const WebCore::UserMessageHandlerDescriptor&)>&&) const final;
83#endif
84#if ENABLE(CONTENT_EXTENSIONS)
85 WebCore::ContentExtensions::ContentExtensionsBackend& userContentExtensionBackend() override { return m_contentExtensionBackend; }
86#endif
87
88 // IPC::MessageReceiver.
89 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
90
91 void removeUserContentWorlds(const Vector<uint64_t>&);
92
93 void removeUserScript(uint64_t worldIdentifier, uint64_t userScriptIdentifier);
94 void removeAllUserScripts(const Vector<uint64_t>&);
95
96 void removeUserStyleSheet(uint64_t worldIdentifier, uint64_t userScriptIdentifier);
97 void removeAllUserStyleSheets(const Vector<uint64_t>&);
98
99 void removeUserScriptMessageHandler(uint64_t worldIdentifier, uint64_t userScriptIdentifier);
100 void removeAllUserScriptMessageHandlers(const Vector<uint64_t>&);
101
102#if ENABLE(CONTENT_EXTENSIONS)
103 void removeContentRuleList(const String& name);
104 void removeAllContentRuleLists();
105#endif
106
107 void addUserScriptInternal(InjectedBundleScriptWorld&, const Optional<uint64_t>& userScriptIdentifier, WebCore::UserScript&&, InjectUserScriptImmediately);
108 void removeUserScriptInternal(InjectedBundleScriptWorld&, uint64_t userScriptIdentifier);
109 void addUserStyleSheetInternal(InjectedBundleScriptWorld&, const Optional<uint64_t>& userStyleSheetIdentifier, WebCore::UserStyleSheet&&);
110 void removeUserStyleSheetInternal(InjectedBundleScriptWorld&, uint64_t userStyleSheetIdentifier);
111#if ENABLE(USER_MESSAGE_HANDLERS)
112 void addUserScriptMessageHandlerInternal(InjectedBundleScriptWorld&, uint64_t userScriptMessageHandlerIdentifier, const String& name);
113 void removeUserScriptMessageHandlerInternal(InjectedBundleScriptWorld&, uint64_t userScriptMessageHandlerIdentifier);
114#endif
115
116 UserContentControllerIdentifier m_identifier;
117
118 typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<Optional<uint64_t>, WebCore::UserScript>>> WorldToUserScriptMap;
119 WorldToUserScriptMap m_userScripts;
120
121 typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<Optional<uint64_t>, WebCore::UserStyleSheet>>> WorldToUserStyleSheetMap;
122 WorldToUserStyleSheetMap m_userStyleSheets;
123
124#if ENABLE(USER_MESSAGE_HANDLERS)
125 typedef HashMap<RefPtr<InjectedBundleScriptWorld>, Vector<std::pair<uint64_t, RefPtr<WebUserMessageHandlerDescriptorProxy>>>> WorldToUserMessageHandlerVectorMap;
126 WorldToUserMessageHandlerVectorMap m_userMessageHandlers;
127#endif
128#if ENABLE(CONTENT_EXTENSIONS)
129 WebCore::ContentExtensions::ContentExtensionsBackend m_contentExtensionBackend;
130#endif
131};
132
133} // namespace WebKit
134