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