1 | /*
|
---|
2 | * Copyright (C) 2021 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 "NetworkNotificationManager.h"
|
---|
28 |
|
---|
29 | #if ENABLE(BUILT_IN_NOTIFICATIONS)
|
---|
30 |
|
---|
31 | #include "DaemonDecoder.h"
|
---|
32 | #include "DaemonEncoder.h"
|
---|
33 | #include "NetworkSession.h"
|
---|
34 | #include "WebPushDaemonConnectionConfiguration.h"
|
---|
35 | #include <WebCore/SecurityOriginData.h>
|
---|
36 |
|
---|
37 | namespace WebKit {
|
---|
38 | using namespace WebCore;
|
---|
39 |
|
---|
40 | NetworkNotificationManager::NetworkNotificationManager(NetworkSession& networkSession, const String& webPushMachServiceName)
|
---|
41 | : m_networkSession(networkSession)
|
---|
42 | {
|
---|
43 | if (!m_networkSession.sessionID().isEphemeral() && !webPushMachServiceName.isEmpty())
|
---|
44 | m_connection = makeUnique<WebPushD::Connection>(webPushMachServiceName.utf8(), *this);
|
---|
45 | }
|
---|
46 |
|
---|
47 | void NetworkNotificationManager::maybeSendConnectionConfiguration() const
|
---|
48 | {
|
---|
49 | if (m_sentConnectionConfiguration)
|
---|
50 | return;
|
---|
51 | m_sentConnectionConfiguration = true;
|
---|
52 |
|
---|
53 | WebPushD::WebPushDaemonConnectionConfiguration configuration;
|
---|
54 | configuration.useMockBundlesForTesting = m_networkSession.webPushDaemonUsesMockBundlesForTesting();
|
---|
55 |
|
---|
56 | #if PLATFORM(COCOA)
|
---|
57 | auto token = m_networkSession.networkProcess().parentProcessConnection()->getAuditToken();
|
---|
58 | if (token) {
|
---|
59 | Vector<uint8_t> auditTokenData;
|
---|
60 | auditTokenData.resize(sizeof(*token));
|
---|
61 | memcpy(auditTokenData.data(), &(*token), sizeof(*token));
|
---|
62 | configuration.hostAppAuditTokenData = WTFMove(auditTokenData);
|
---|
63 | }
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | sendMessage<WebPushD::MessageType::UpdateConnectionConfiguration>(configuration);
|
---|
67 | }
|
---|
68 |
|
---|
69 | void NetworkNotificationManager::requestSystemNotificationPermission(const String& originString, CompletionHandler<void(bool)>&& completionHandler)
|
---|
70 | {
|
---|
71 | sendMessageWithReply<WebPushD::MessageType::RequestSystemNotificationPermission>(WTFMove(completionHandler), originString);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void NetworkNotificationManager::deletePushAndNotificationRegistration(const SecurityOriginData& origin, CompletionHandler<void(const String&)>&& completionHandler)
|
---|
75 | {
|
---|
76 | sendMessageWithReply<WebPushD::MessageType::DeletePushAndNotificationRegistration>(WTFMove(completionHandler), origin.toString());
|
---|
77 | }
|
---|
78 |
|
---|
79 | void NetworkNotificationManager::getOriginsWithPushAndNotificationPermissions(CompletionHandler<void(const Vector<SecurityOriginData>&)>&& completionHandler)
|
---|
80 | {
|
---|
81 | CompletionHandler<void(Vector<String>&&)> replyHandler = [completionHandler = WTFMove(completionHandler)] (Vector<String> originStrings) mutable {
|
---|
82 | Vector<SecurityOriginData> origins;
|
---|
83 | for (auto& originString : originStrings)
|
---|
84 | origins.append(SecurityOriginData::fromURL({ { }, originString }));
|
---|
85 | completionHandler(WTFMove(origins));
|
---|
86 | };
|
---|
87 |
|
---|
88 | sendMessageWithReply<WebPushD::MessageType::GetOriginsWithPushAndNotificationPermissions>(WTFMove(replyHandler));
|
---|
89 | }
|
---|
90 |
|
---|
91 | void NetworkNotificationManager::showNotification(const String&, const String&, const String&, const String&, const String&, WebCore::NotificationDirection, const String&, uint64_t)
|
---|
92 | {
|
---|
93 | if (!m_connection)
|
---|
94 | return;
|
---|
95 |
|
---|
96 | // FIXME: While we don't normally land commented-out code in the tree,
|
---|
97 | // this is a nice bookmark for a development milestone; Roundtrip communication with webpushd
|
---|
98 | // Will make next development steps obvious.
|
---|
99 | //
|
---|
100 | // CompletionHandler<void(String)>&& completionHandler = [] (String reply) {
|
---|
101 | // printf("Got reply: %s\n", reply.utf8().data());
|
---|
102 | // };
|
---|
103 | //
|
---|
104 | // sendMessageWithReply<WebPushD::MessageType::EchoTwice>(WTFMove(completionHandler), String("FIXME: Do useful work here"));
|
---|
105 | }
|
---|
106 |
|
---|
107 | void NetworkNotificationManager::cancelNotification(uint64_t)
|
---|
108 | {
|
---|
109 | if (!m_connection)
|
---|
110 | return;
|
---|
111 | }
|
---|
112 |
|
---|
113 | void NetworkNotificationManager::clearNotifications(const Vector<uint64_t>&)
|
---|
114 | {
|
---|
115 | if (!m_connection)
|
---|
116 | return;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void NetworkNotificationManager::didDestroyNotification(uint64_t)
|
---|
120 | {
|
---|
121 | if (!m_connection)
|
---|
122 | return;
|
---|
123 | }
|
---|
124 |
|
---|
125 | template<WebPushD::MessageType messageType, typename... Args>
|
---|
126 | void NetworkNotificationManager::sendMessage(Args&&... args) const
|
---|
127 | {
|
---|
128 | RELEASE_ASSERT(m_connection);
|
---|
129 |
|
---|
130 | maybeSendConnectionConfiguration();
|
---|
131 |
|
---|
132 | Daemon::Encoder encoder;
|
---|
133 | encoder.encode(std::forward<Args>(args)...);
|
---|
134 | m_connection->send(messageType, encoder.takeBuffer());
|
---|
135 | }
|
---|
136 |
|
---|
137 | template<typename... Args> struct ReplyCaller;
|
---|
138 | template<> struct ReplyCaller<> {
|
---|
139 | static void callReply(Daemon::Decoder&& decoder, CompletionHandler<void()>&& completionHandler)
|
---|
140 | {
|
---|
141 | completionHandler();
|
---|
142 | }
|
---|
143 | };
|
---|
144 |
|
---|
145 | template<> struct ReplyCaller<String> {
|
---|
146 | static void callReply(Daemon::Decoder&& decoder, CompletionHandler<void(String&&)>&& completionHandler)
|
---|
147 | {
|
---|
148 | std::optional<String> string;
|
---|
149 | decoder >> string;
|
---|
150 | if (!string)
|
---|
151 | return completionHandler({ });
|
---|
152 | completionHandler(WTFMove(*string));
|
---|
153 | }
|
---|
154 | };
|
---|
155 |
|
---|
156 | template<> struct ReplyCaller<const String&> {
|
---|
157 | static void callReply(Daemon::Decoder&& decoder, CompletionHandler<void(const String&)>&& completionHandler)
|
---|
158 | {
|
---|
159 | std::optional<String> string;
|
---|
160 | decoder >> string;
|
---|
161 | if (!string)
|
---|
162 | return completionHandler({ });
|
---|
163 | completionHandler(WTFMove(*string));
|
---|
164 | }
|
---|
165 | };
|
---|
166 |
|
---|
167 | template<> struct ReplyCaller<bool> {
|
---|
168 | static void callReply(Daemon::Decoder&& decoder, CompletionHandler<void(bool)>&& completionHandler)
|
---|
169 | {
|
---|
170 | std::optional<bool> boolean;
|
---|
171 | decoder >> boolean;
|
---|
172 | if (!boolean)
|
---|
173 | return completionHandler(false);
|
---|
174 | completionHandler(*boolean);
|
---|
175 | }
|
---|
176 | };
|
---|
177 |
|
---|
178 | template<> struct ReplyCaller<Vector<String>&&> {
|
---|
179 | static void callReply(Daemon::Decoder&& decoder, CompletionHandler<void(Vector<String>&&)>&& completionHandler)
|
---|
180 | {
|
---|
181 | std::optional<Vector<String>> strings;
|
---|
182 | decoder >> strings;
|
---|
183 | if (!strings)
|
---|
184 | return completionHandler({ });
|
---|
185 | completionHandler(WTFMove(*strings));
|
---|
186 | }
|
---|
187 | };
|
---|
188 |
|
---|
189 | template<WebPushD::MessageType messageType, typename... Args, typename... ReplyArgs>
|
---|
190 | void NetworkNotificationManager::sendMessageWithReply(CompletionHandler<void(ReplyArgs...)>&& completionHandler, Args&&... args) const
|
---|
191 | {
|
---|
192 | RELEASE_ASSERT(m_connection);
|
---|
193 |
|
---|
194 | maybeSendConnectionConfiguration();
|
---|
195 |
|
---|
196 | Daemon::Encoder encoder;
|
---|
197 | encoder.encode(std::forward<Args>(args)...);
|
---|
198 | m_connection->sendWithReply(messageType, encoder.takeBuffer(), [completionHandler = WTFMove(completionHandler)] (auto replyBuffer) mutable {
|
---|
199 | Daemon::Decoder decoder(WTFMove(replyBuffer));
|
---|
200 | ReplyCaller<ReplyArgs...>::callReply(WTFMove(decoder), WTFMove(completionHandler));
|
---|
201 | });
|
---|
202 | }
|
---|
203 |
|
---|
204 | } // namespace WebKit
|
---|
205 | #endif // ENABLE(BUILT_IN_NOTIFICATIONS)
|
---|