Title: [290818] trunk/Source/WebKit
Revision
290818
Author
beid...@apple.com
Date
2022-03-03 22:58:01 -0800 (Thu, 03 Mar 2022)

Log Message

Add a new "Daemon -> Client" message type to webpushd
https://bugs.webkit.org/show_bug.cgi?id=237391

Reviewed by Alex Christensen.

Future work will need more messages like this.
So let's add the mechanism in a small standalone patch that uses it for debug messages.

No behavior change for now.

* NetworkProcess/Notifications/Cocoa/WebPushDaemonConnectionCocoa.mm:
(WebKit::WebPushD::handleWebPushDaemonMessage):
(WebKit::WebPushD::Connection::connectionReceivedEvent):
(WebKit::WebPushD::Connection::connectionReceivedEvent const): Deleted.

* NetworkProcess/Notifications/WebPushDaemonConnection.cpp:
(WebKit::WebPushD::Connection::debugMessage):
* NetworkProcess/Notifications/WebPushDaemonConnection.h:

* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementConnection.h:
* NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementConnectionCocoa.mm:
(WebKit::PCM::Connection::connectionReceivedEvent):
(WebKit::PCM::Connection::connectionReceivedEvent const): Deleted.

* Platform/IPC/DaemonConnection.h:

* Shared/WebPushDaemonConstants.h:
(WebKit::WebPushD::daemonMessageTypeSendsReply):

* webpushd/ICAppBundle.mm:
(WebPushD::broadcastDebugMessage):

* webpushd/PushClientConnection.h:
* webpushd/PushClientConnection.mm:
(WebPushD::ClientConnection::broadcastDebugMessage):
(WebPushD::ClientConnection::sendDebugMessage):
(WebPushD::ClientConnection::sendDaemonMessage const):

* webpushd/WebPushDaemon.h:
* webpushd/WebPushDaemon.mm:
(WebPushD::Daemon::broadcastDebugMessage):
(WebPushD::Daemon::broadcastAllConnectionIdentities):
(WebPushD::Daemon::connectionAdded):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (290817 => 290818)


--- trunk/Source/WebKit/ChangeLog	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/ChangeLog	2022-03-04 06:58:01 UTC (rev 290818)
@@ -1,3 +1,49 @@
+2022-03-03  Brady Eidson  <beid...@apple.com>
+
+        Add a new "Daemon -> Client" message type to webpushd
+        https://bugs.webkit.org/show_bug.cgi?id=237391
+
+        Reviewed by Alex Christensen.
+
+        Future work will need more messages like this.
+        So let's add the mechanism in a small standalone patch that uses it for debug messages.
+        
+        No behavior change for now.
+
+        * NetworkProcess/Notifications/Cocoa/WebPushDaemonConnectionCocoa.mm:
+        (WebKit::WebPushD::handleWebPushDaemonMessage):
+        (WebKit::WebPushD::Connection::connectionReceivedEvent):
+        (WebKit::WebPushD::Connection::connectionReceivedEvent const): Deleted.
+
+        * NetworkProcess/Notifications/WebPushDaemonConnection.cpp:
+        (WebKit::WebPushD::Connection::debugMessage):
+        * NetworkProcess/Notifications/WebPushDaemonConnection.h:
+
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementConnection.h:
+        * NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementConnectionCocoa.mm:
+        (WebKit::PCM::Connection::connectionReceivedEvent):
+        (WebKit::PCM::Connection::connectionReceivedEvent const): Deleted.
+
+        * Platform/IPC/DaemonConnection.h:
+
+        * Shared/WebPushDaemonConstants.h:
+        (WebKit::WebPushD::daemonMessageTypeSendsReply):
+
+        * webpushd/ICAppBundle.mm:
+        (WebPushD::broadcastDebugMessage):
+
+        * webpushd/PushClientConnection.h:
+        * webpushd/PushClientConnection.mm:
+        (WebPushD::ClientConnection::broadcastDebugMessage):
+        (WebPushD::ClientConnection::sendDebugMessage):
+        (WebPushD::ClientConnection::sendDaemonMessage const):
+
+        * webpushd/WebPushDaemon.h:
+        * webpushd/WebPushDaemon.mm:
+        (WebPushD::Daemon::broadcastDebugMessage):
+        (WebPushD::Daemon::broadcastAllConnectionIdentities):
+        (WebPushD::Daemon::connectionAdded):
+
 2022-03-03  Ross Kirsling  <ross.kirsl...@sony.com>
 
         REGRESSION(r290815): Fix even more incorrect #if ENABLE(SERVICE_WORKER) usage in NetworkProcess

Modified: trunk/Source/WebKit/NetworkProcess/Notifications/Cocoa/WebPushDaemonConnectionCocoa.mm (290817 => 290818)


--- trunk/Source/WebKit/NetworkProcess/Notifications/Cocoa/WebPushDaemonConnectionCocoa.mm	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/NetworkProcess/Notifications/Cocoa/WebPushDaemonConnectionCocoa.mm	2022-03-04 06:58:01 UTC (rev 290818)
@@ -28,8 +28,10 @@
 
 #if PLATFORM(COCOA) && ENABLE(BUILT_IN_NOTIFICATIONS)
 
+#import "DaemonDecoder.h"
 #import "DaemonEncoder.h"
 #import "DaemonUtilities.h"
+#import "HandleMessage.h"
 #import "WebPushDaemonConstants.h"
 #import <wtf/spi/darwin/XPCSPI.h>
 
@@ -53,15 +55,53 @@
     // FIXME: Track connection state
 }
 
-void Connection::connectionReceivedEvent(xpc_object_t request) const
+namespace MessageInfo {
+
+#define FUNCTION(mf) struct mf { static constexpr auto MemberFunction = &WebKit::WebPushD::Connection::mf;
+#define ARGUMENTS(...) using ArgsTuple = std::tuple<__VA_ARGS__>;
+#define END };
+
+FUNCTION(debugMessage)
+ARGUMENTS(String)
+END
+
+} // namespace MessageInfo
+
+template<typename Info>
+void handleWebPushDaemonMessage(WebKit::WebPushD::Connection* connection, Span<const uint8_t> encodedMessage)
 {
+    WebKit::Daemon::Decoder decoder(encodedMessage);
+
+    std::optional<typename Info::ArgsTuple> arguments;
+    decoder >> arguments;
+    if (UNLIKELY(!arguments))
+        return;
+
+    IPC::callMemberFunction(WTFMove(*arguments), connection, Info::MemberFunction);
+}
+
+void Connection::connectionReceivedEvent(xpc_object_t request)
+{
     if (xpc_get_type(request) != XPC_TYPE_DICTIONARY)
         return;
-    const char* debugMessage = xpc_dictionary_get_string(request, protocolDebugMessageKey);
-    if (!debugMessage)
+
+    if (xpc_dictionary_get_uint64(request, protocolVersionKey) != protocolVersionValue) {
+        RELEASE_LOG(Push, "Received request that was not the current protocol version");
         return;
-    auto messageLevel = static_cast<JSC::MessageLevel>(xpc_dictionary_get_uint64(request, protocolDebugMessageLevelKey));
-    networkSession().networkProcess().broadcastConsoleMessage(networkSession().sessionID(), MessageSource::Other, messageLevel, String::fromUTF8(debugMessage));
+    }
+
+    auto messageType { static_cast<DaemonMessageType>(xpc_dictionary_get_uint64(request, protocolMessageTypeKey)) };
+    size_t dataSize { 0 };
+    const void* data = "" protocolEncodedMessageKey, &dataSize);
+    Span<const uint8_t> encodedMessage { static_cast<const uint8_t*>(data), dataSize };
+
+    ASSERT(!daemonMessageTypeSendsReply(messageType));
+
+    switch (messageType) {
+    case DaemonMessageType::DebugMessage:
+        handleWebPushDaemonMessage<MessageInfo::debugMessage>(this, encodedMessage);
+        break;
+    };
 }
 
 RetainPtr<xpc_object_t> Connection::dictionaryFromMessage(MessageType messageType, EncodedMessage&& message) const

Modified: trunk/Source/WebKit/NetworkProcess/Notifications/WebPushDaemonConnection.cpp (290817 => 290818)


--- trunk/Source/WebKit/NetworkProcess/Notifications/WebPushDaemonConnection.cpp	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/NetworkProcess/Notifications/WebPushDaemonConnection.cpp	2022-03-04 06:58:01 UTC (rev 290818)
@@ -46,6 +46,11 @@
     return m_notificationManager.networkSession();
 }
 
+void Connection::debugMessage(const String& message)
+{
+    networkSession().networkProcess().broadcastConsoleMessage(networkSession().sessionID(), MessageSource::Other, JSC::MessageLevel::Info, message);
+}
+
 } // namespace WebKit::WebPushD
 
 #endif // ENABLE(BUILT_IN_NOTIFICATIONS)

Modified: trunk/Source/WebKit/NetworkProcess/Notifications/WebPushDaemonConnection.h (290817 => 290818)


--- trunk/Source/WebKit/NetworkProcess/Notifications/WebPushDaemonConnection.h	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/NetworkProcess/Notifications/WebPushDaemonConnection.h	2022-03-04 06:58:01 UTC (rev 290818)
@@ -49,11 +49,13 @@
 public:
     Connection(CString&& machServiceName, NetworkNotificationManager&);
 
+    void debugMessage(const String&);
+
 private:
     void newConnectionWasInitialized() const final;
 #if PLATFORM(COCOA)
     RetainPtr<xpc_object_t> dictionaryFromMessage(MessageType, Daemon::EncodedMessage&&) const final;
-    void connectionReceivedEvent(xpc_object_t) const final;
+    void connectionReceivedEvent(xpc_object_t) final;
 #endif
     void sendDebugModeIsEnabledMessageIfNecessary() const;
 

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementConnection.h (290817 => 290818)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementConnection.h	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementConnection.h	2022-03-04 06:58:01 UTC (rev 290818)
@@ -51,7 +51,7 @@
     void newConnectionWasInitialized() const final;
 #if PLATFORM(COCOA)
     RetainPtr<xpc_object_t> dictionaryFromMessage(MessageType, Daemon::EncodedMessage&&) const final;
-    void connectionReceivedEvent(xpc_object_t) const final;
+    void connectionReceivedEvent(xpc_object_t) final;
 #endif
     void sendDebugModeIsEnabledMessageIfNecessary() const;
 

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementConnectionCocoa.mm (290817 => 290818)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementConnectionCocoa.mm	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementConnectionCocoa.mm	2022-03-04 06:58:01 UTC (rev 290818)
@@ -45,7 +45,7 @@
     send(MessageType::SetDebugModeIsEnabled, encoder.takeBuffer());
 }
 
-void Connection::connectionReceivedEvent(xpc_object_t request) const
+void Connection::connectionReceivedEvent(xpc_object_t request)
 {
     if (xpc_get_type(request) != XPC_TYPE_DICTIONARY)
         return;

Modified: trunk/Source/WebKit/Platform/IPC/DaemonConnection.h (290817 => 290818)


--- trunk/Source/WebKit/Platform/IPC/DaemonConnection.h	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/Platform/IPC/DaemonConnection.h	2022-03-04 06:58:01 UTC (rev 290818)
@@ -68,7 +68,7 @@
     virtual void newConnectionWasInitialized() const = 0;
 #if PLATFORM(COCOA)
     virtual RetainPtr<xpc_object_t> dictionaryFromMessage(typename Traits::MessageType, EncodedMessage&&) const = 0;
-    virtual void connectionReceivedEvent(xpc_object_t) const = 0;
+    virtual void connectionReceivedEvent(xpc_object_t) = 0;
 #endif
 
     const CString& machServiceName() const { return m_machServiceName; }

Modified: trunk/Source/WebKit/Shared/WebPushDaemonConstants.h (290817 => 290818)


--- trunk/Source/WebKit/Shared/WebPushDaemonConstants.h	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/Shared/WebPushDaemonConstants.h	2022-03-04 06:58:01 UTC (rev 290818)
@@ -79,4 +79,18 @@
     return false;
 }
 
+enum class DaemonMessageType : uint8_t {
+    DebugMessage = 1,
+};
+
+inline bool daemonMessageTypeSendsReply(DaemonMessageType messageType)
+{
+    switch (messageType) {
+    case DaemonMessageType::DebugMessage:
+        return false;
+    }
+    ASSERT_NOT_REACHED();
+    return false;
+}
+
 } // namespace WebKit::WebPushD

Modified: trunk/Source/WebKit/webpushd/ICAppBundle.mm (290817 => 290818)


--- trunk/Source/WebKit/webpushd/ICAppBundle.mm	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/webpushd/ICAppBundle.mm	2022-03-04 06:58:01 UTC (rev 290818)
@@ -81,7 +81,7 @@
 
 static void broadcastDebugMessage(const String& message)
 {
-    Daemon::singleton().broadcastDebugMessage(JSC::MessageLevel::Info, makeString("ICAppBundle: ", message));
+    Daemon::singleton().broadcastDebugMessage(makeString("ICAppBundle: ", message));
 }
 
 ICAppBundle::ICAppBundle(ClientConnection& clientConnection, const String& originString, PushAppBundleClient& client)

Modified: trunk/Source/WebKit/webpushd/PushClientConnection.h (290817 => 290818)


--- trunk/Source/WebKit/webpushd/PushClientConnection.h	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/webpushd/PushClientConnection.h	2022-03-04 06:58:01 UTC (rev 290818)
@@ -37,9 +37,12 @@
 
 namespace WebKit {
 namespace WebPushD {
+enum class DaemonMessageType : uint8_t;
 struct WebPushDaemonConnectionConfiguration;
 }
 }
+
+using WebKit::WebPushD::DaemonMessageType;
 using WebKit::WebPushD::WebPushDaemonConnectionConfiguration;
 
 namespace WebPushD {
@@ -70,6 +73,7 @@
     void connectionClosed();
 
     void broadcastDebugMessage(const String&);
+    void sendDebugMessage(const String&);
 
 private:
     ClientConnection(xpc_connection_t);
@@ -79,6 +83,9 @@
 
     bool hostHasEntitlement(const char*);
 
+    template<DaemonMessageType messageType, typename... Args>
+    void sendDaemonMessage(Args&&...) const;
+
     OSObjectPtr<xpc_connection_t> m_xpcConnection;
 
     std::optional<audit_token_t> m_hostAppAuditToken;

Modified: trunk/Source/WebKit/webpushd/PushClientConnection.mm (290817 => 290818)


--- trunk/Source/WebKit/webpushd/PushClientConnection.mm	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/webpushd/PushClientConnection.mm	2022-03-04 06:58:01 UTC (rev 290818)
@@ -28,13 +28,18 @@
 
 #import "AppBundleRequest.h"
 #import "CodeSigning.h"
+#import "DaemonEncoder.h"
+#import "DaemonUtilities.h"
 #import "WebPushDaemon.h"
 #import "WebPushDaemonConnectionConfiguration.h"
+#import "WebPushDaemonConstants.h"
 #import <_javascript_Core/ConsoleTypes.h>
 #import <wtf/HexNumber.h>
 #import <wtf/Vector.h>
 #import <wtf/cocoa/Entitlements.h>
 
+using WebKit::Daemon::Encoder;
+
 namespace WebPushD {
 
 Ref<ClientConnection> ClientConnection::create(xpc_connection_t connection)
@@ -135,9 +140,21 @@
     else
         messageIdentifier = makeString("[", signingIdentifer, " (", String::number(identifier()), ")] ");
 
-    Daemon::singleton().broadcastDebugMessage(JSC::MessageLevel::Info, makeString(messageIdentifier, message));
+    Daemon::singleton().broadcastDebugMessage(makeString(messageIdentifier, message));
 }
 
+void ClientConnection::sendDebugMessage(const String& message)
+{
+    // FIXME: We currently send the debug message twice.
+    // After getting all debug message clients onto the encoder/decoder mechanism, remove the old style message.
+    auto dictionary = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
+    xpc_dictionary_set_uint64(dictionary.get(), WebKit::WebPushD::protocolDebugMessageLevelKey, static_cast<uint64_t>(JSC::MessageLevel::Info));
+    xpc_dictionary_set_string(dictionary.get(), WebKit::WebPushD::protocolDebugMessageKey, message.utf8().data());
+    xpc_connection_send_message(m_xpcConnection.get(), dictionary.get());
+
+    sendDaemonMessage<DaemonMessageType::DebugMessage>(message);
+}
+
 void ClientConnection::enqueueAppBundleRequest(std::unique_ptr<AppBundleRequest>&& request)
 {
     RELEASE_ASSERT(m_xpcConnection);
@@ -185,4 +202,21 @@
         requst->cancel();
 }
 
+template<DaemonMessageType messageType, typename... Args>
+void ClientConnection::sendDaemonMessage(Args&&... args) const
+{
+    if (!m_xpcConnection)
+        return;
+
+    Encoder encoder;
+    encoder.encode(std::forward<Args>(args)...);
+
+    auto dictionary = adoptNS(xpc_dictionary_create(nullptr, nullptr, 0));
+    xpc_dictionary_set_uint64(dictionary.get(), WebKit::WebPushD::protocolVersionKey, WebKit::WebPushD::protocolVersionValue);
+    xpc_dictionary_set_value(dictionary.get(), WebKit::WebPushD::protocolEncodedMessageKey, WebKit::vectorToXPCData(encoder.takeBuffer()).get());
+    xpc_dictionary_set_uint64(dictionary.get(), WebKit::WebPushD::protocolMessageTypeKey, static_cast<uint64_t>(messageType));
+
+    xpc_connection_send_message(m_xpcConnection.get(), dictionary.get());
+}
+
 } // namespace WebPushD

Modified: trunk/Source/WebKit/webpushd/WebPushDaemon.h (290817 => 290818)


--- trunk/Source/WebKit/webpushd/WebPushDaemon.h	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/webpushd/WebPushDaemon.h	2022-03-04 06:58:01 UTC (rev 290818)
@@ -86,7 +86,7 @@
     void getPushPermissionState(ClientConnection*, const URL& scopeURL, CompletionHandler<void(const Expected<uint8_t, WebCore::ExceptionData>&)>&& replySender);
     void incrementSilentPushCount(ClientConnection*, const WebCore::SecurityOriginData&, CompletionHandler<void(unsigned)>&&);
 
-    void broadcastDebugMessage(JSC::MessageLevel, const String&);
+    void broadcastDebugMessage(const String&);
     void broadcastAllConnectionIdentities();
 
 private:

Modified: trunk/Source/WebKit/webpushd/WebPushDaemon.mm (290817 => 290818)


--- trunk/Source/WebKit/webpushd/WebPushDaemon.mm	2022-03-04 06:38:04 UTC (rev 290817)
+++ trunk/Source/WebKit/webpushd/WebPushDaemon.mm	2022-03-04 06:58:01 UTC (rev 290818)
@@ -296,20 +296,17 @@
     function();
 }
 
-void Daemon::broadcastDebugMessage(JSC::MessageLevel messageLevel, const String& message)
+void Daemon::broadcastDebugMessage(const String& message)
 {
-    auto dictionary = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0));
-    xpc_dictionary_set_uint64(dictionary.get(), protocolDebugMessageLevelKey, static_cast<uint64_t>(messageLevel));
-    xpc_dictionary_set_string(dictionary.get(), protocolDebugMessageKey, message.utf8().data());
     for (auto& iterator : m_connectionMap) {
         if (iterator.value->debugModeIsEnabled())
-            xpc_connection_send_message(iterator.key, dictionary.get());
+            iterator.value->sendDebugMessage(message);
     }
 }
 
 void Daemon::broadcastAllConnectionIdentities()
 {
-    broadcastDebugMessage((JSC::MessageLevel)4, "===\nCurrent connections:");
+    broadcastDebugMessage("===\nCurrent connections:");
 
     auto connections = copyToVector(m_connectionMap.values());
     std::sort(connections.begin(), connections.end(), [] (const Ref<ClientConnection>& a, const Ref<ClientConnection>& b) {
@@ -318,7 +315,7 @@
 
     for (auto& iterator : connections)
         iterator->broadcastDebugMessage("");
-    broadcastDebugMessage((JSC::MessageLevel)4, "===");
+    broadcastDebugMessage("===");
 }
 
 void Daemon::connectionEventHandler(xpc_object_t request)
@@ -342,7 +339,7 @@
 
 void Daemon::connectionAdded(xpc_connection_t connection)
 {
-    broadcastDebugMessage((JSC::MessageLevel)0, makeString("New connection: 0x", hex(reinterpret_cast<uint64_t>(connection), WTF::HexConversionMode::Lowercase)));
+    broadcastDebugMessage(makeString("New connection: 0x", hex(reinterpret_cast<uint64_t>(connection), WTF::HexConversionMode::Lowercase)));
 
     RELEASE_ASSERT(!m_connectionMap.contains(connection));
     m_connectionMap.set(connection, ClientConnection::create(connection));
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to