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 "APIObject.h"
29#include "MessageReceiver.h"
30#include "UserContentControllerIdentifier.h"
31#include <WebCore/PageIdentifier.h>
32#include <wtf/Forward.h>
33#include <wtf/HashCountedSet.h>
34#include <wtf/HashMap.h>
35#include <wtf/HashSet.h>
36#include <wtf/Ref.h>
37#include <wtf/RefCounted.h>
38#include <wtf/text/StringHash.h>
39
40namespace API {
41class Array;
42class ContentRuleList;
43class UserContentWorld;
44class UserScript;
45class UserStyleSheet;
46}
47
48namespace IPC {
49class DataReference;
50}
51
52namespace WebCore {
53struct SecurityOriginData;
54}
55
56namespace WebKit {
57
58class NetworkProcessProxy;
59class WebProcessProxy;
60class WebScriptMessageHandler;
61struct FrameInfoData;
62struct WebPageCreationParameters;
63enum class InjectUserScriptImmediately : bool;
64
65class WebUserContentControllerProxy : public API::ObjectImpl<API::Object::Type::UserContentController>, private IPC::MessageReceiver {
66public:
67 static Ref<WebUserContentControllerProxy> create()
68 {
69 return adoptRef(*new WebUserContentControllerProxy);
70 }
71 WebUserContentControllerProxy();
72 ~WebUserContentControllerProxy();
73
74 static WebUserContentControllerProxy* get(UserContentControllerIdentifier);
75
76 void addProcess(WebProcessProxy&, WebPageCreationParameters&);
77 void removeProcess(WebProcessProxy&);
78
79 API::Array& userScripts() { return m_userScripts.get(); }
80 void addUserScript(API::UserScript&, InjectUserScriptImmediately);
81 void removeUserScript(API::UserScript&);
82 void removeAllUserScripts(API::UserContentWorld&);
83 void removeAllUserScripts();
84
85 API::Array& userStyleSheets() { return m_userStyleSheets.get(); }
86 void addUserStyleSheet(API::UserStyleSheet&);
87 void removeUserStyleSheet(API::UserStyleSheet&);
88 void removeAllUserStyleSheets(API::UserContentWorld&);
89 void removeAllUserStyleSheets();
90
91 void removeAllUserContent(API::UserContentWorld&);
92
93 // Returns false if there was a name conflict.
94 bool addUserScriptMessageHandler(WebScriptMessageHandler&);
95 void removeUserMessageHandlerForName(const String&, API::UserContentWorld&);
96 void removeAllUserMessageHandlers(API::UserContentWorld&);
97
98#if ENABLE(CONTENT_EXTENSIONS)
99 void addNetworkProcess(NetworkProcessProxy& proxy) { m_networkProcesses.add(&proxy); }
100 void removeNetworkProcess(NetworkProcessProxy& proxy) { m_networkProcesses.remove(&proxy); }
101
102 void addContentRuleList(API::ContentRuleList&);
103 void removeContentRuleList(const String&);
104 void removeAllContentRuleLists();
105 const HashMap<String, RefPtr<API::ContentRuleList>>& contentExtensionRules() { return m_contentRuleLists; }
106#endif
107
108 UserContentControllerIdentifier identifier() const { return m_identifier; }
109
110private:
111 // IPC::MessageReceiver.
112 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
113
114 void didPostMessage(IPC::Connection&, WebCore::PageIdentifier, const FrameInfoData&, uint64_t messageHandlerID, const IPC::DataReference&);
115
116 void addUserContentWorldUse(API::UserContentWorld&);
117 void removeUserContentWorldUses(API::UserContentWorld&, unsigned numberOfUsesToRemove);
118 void removeUserContentWorldUses(HashCountedSet<RefPtr<API::UserContentWorld>>&);
119 bool shouldSendRemoveUserContentWorldsMessage(API::UserContentWorld&, unsigned numberOfUsesToRemove);
120
121 UserContentControllerIdentifier m_identifier;
122 HashSet<WebProcessProxy*> m_processes;
123 Ref<API::Array> m_userScripts;
124 Ref<API::Array> m_userStyleSheets;
125 HashMap<uint64_t, RefPtr<WebScriptMessageHandler>> m_scriptMessageHandlers;
126 HashCountedSet<RefPtr<API::UserContentWorld>> m_userContentWorlds;
127
128#if ENABLE(CONTENT_EXTENSIONS)
129 HashSet<NetworkProcessProxy*> m_networkProcesses;
130 HashMap<String, RefPtr<API::ContentRuleList>> m_contentRuleLists;
131#endif
132};
133
134} // namespace WebKit
135