Title: [286579] trunk
Revision
286579
Author
beid...@apple.com
Date
2021-12-06 16:38:11 -0800 (Mon, 06 Dec 2021)

Log Message

webpushd/webpushtool debugging additions
https://bugs.webkit.org/show_bug.cgi?id=233864

Reviewed by Alex Christensen.

Source/WebKit:

Covered by API tests.

This patch:
- Teaches webpushtool the ability to wait for a reconnect after losing its connection
- Starts actually broadcasting meaningful debug messages from webpushd

* webpushd/AppBundleRequest.mm:
(WebPushD::AppBundleRequest::start):
(WebPushD::AppBundlePermissionsRequest::didCheckForExistingBundle):
(WebPushD::AppBundlePermissionsRequest::didCreateAppBundle):
(WebPushD::AppBundleDeletionRequest::didDeleteExistingBundleWithError):

* webpushd/PushClientConnection.h:
* webpushd/PushClientConnection.mm:
(WebPushD::ClientConnection::setDebugModeIsEnabled):
(WebPushD::ClientConnection::broadcastDebugMessage):
(WebPushD::ClientConnection::connectionClosed):

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

* webpushd/webpushtool/WebPushToolConnection.h:
* webpushd/webpushtool/WebPushToolConnection.mm:
(WebPushTool::Connection::create):
(WebPushTool::Connection::Connection):
(WebPushTool::Connection::connectToService):
(WebPushTool::Connection::connectionDropped):

* webpushd/webpushtool/WebPushToolMain.mm:
(printUsageAndTerminate):
(main):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/WebPushDaemon.mm:
(TestWebKitAPI::TEST): Adapt to debug message changes.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (286578 => 286579)


--- trunk/Source/WebKit/ChangeLog	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/ChangeLog	2021-12-07 00:38:11 UTC (rev 286579)
@@ -1,3 +1,42 @@
+2021-12-06  Brady Eidson  <beid...@apple.com>
+
+        webpushd/webpushtool debugging additions
+        https://bugs.webkit.org/show_bug.cgi?id=233864
+
+        Reviewed by Alex Christensen.
+
+        Covered by API tests.
+        
+        This patch:
+        - Teaches webpushtool the ability to wait for a reconnect after losing its connection
+        - Starts actually broadcasting meaningful debug messages from webpushd
+
+        * webpushd/AppBundleRequest.mm:
+        (WebPushD::AppBundleRequest::start):
+        (WebPushD::AppBundlePermissionsRequest::didCheckForExistingBundle):
+        (WebPushD::AppBundlePermissionsRequest::didCreateAppBundle):
+        (WebPushD::AppBundleDeletionRequest::didDeleteExistingBundleWithError):
+
+        * webpushd/PushClientConnection.h:
+        * webpushd/PushClientConnection.mm:
+        (WebPushD::ClientConnection::setDebugModeIsEnabled):
+        (WebPushD::ClientConnection::broadcastDebugMessage):
+        (WebPushD::ClientConnection::connectionClosed):
+
+        * webpushd/WebPushDaemon.mm:
+        (WebPushD::Daemon::connectionAdded):
+
+        * webpushd/webpushtool/WebPushToolConnection.h:
+        * webpushd/webpushtool/WebPushToolConnection.mm:
+        (WebPushTool::Connection::create):
+        (WebPushTool::Connection::Connection):
+        (WebPushTool::Connection::connectToService):
+        (WebPushTool::Connection::connectionDropped):
+
+        * webpushd/webpushtool/WebPushToolMain.mm:
+        (printUsageAndTerminate):
+        (main):
+
 2021-12-06  Chris Dumez  <cdu...@apple.com>
 
         Regression(r286505) imported/w3c/web-platform-tests/html/cross-origin-opener-policy/_javascript_-url.https.html is a flaky crash

Modified: trunk/Source/WebKit/webpushd/AppBundleRequest.mm (286578 => 286579)


--- trunk/Source/WebKit/webpushd/AppBundleRequest.mm	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/AppBundleRequest.mm	2021-12-07 00:38:11 UTC (rev 286579)
@@ -47,6 +47,8 @@
 {
     ASSERT(m_connection);
 
+    m_connection->broadcastDebugMessage(makeString("Starting ", transactionDescription(), " request for origin ", m_originString));
+
     m_transaction = adoptOSObject(os_transaction_create(transactionDescription()));
 
     if (m_connection->useMockBundlesForTesting())
@@ -91,6 +93,8 @@
 {
     ASSERT_UNUSED(bundle, &bundle == m_appBundle.get());
 
+    m_connection->broadcastDebugMessage(makeString("Origin ", m_originString, " app bundle request: didCheckForExistingBundle - ", exists == PushAppBundleExists::Yes ? "Exists" : "Does not exist"));
+
     if (exists == PushAppBundleExists::Yes)
         return callCompletionHandlerAndCleanup(true);
 
@@ -101,6 +105,8 @@
 {
     ASSERT_UNUSED(bundle, &bundle == m_appBundle.get());
 
+    m_connection->broadcastDebugMessage(makeString("Origin ", m_originString, " app bundle request: didCreateAppBundle - ", result == PushAppBundleCreationResult::Success ? "Created" : "Failed to create"));
+
     if (result == PushAppBundleCreationResult::Failure)
         return callCompletionHandlerAndCleanup(false);
 
@@ -118,8 +124,10 @@
 {
     ASSERT_UNUSED(bundle, &bundle == m_appBundle.get());
 
+    m_connection->broadcastDebugMessage(makeString("Origin ", m_originString, " app bundle request: didDeleteExistingBundleWithError"));
+
     if (error)
-        Daemon::singleton().broadcastDebugMessage(MessageLevel::Info, makeString("Failed to delete app bundle: ", String([error description])));
+        m_connection->broadcastDebugMessage(makeString("Failed to delete app bundle: ", String([error description])));
 
     callCompletionHandlerAndCleanup(error ? String([error description]) : "");
 }

Modified: trunk/Source/WebKit/webpushd/PushClientConnection.h (286578 => 286579)


--- trunk/Source/WebKit/webpushd/PushClientConnection.h	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/PushClientConnection.h	2021-12-07 00:38:11 UTC (rev 286579)
@@ -67,6 +67,8 @@
 
     void connectionClosed();
 
+    void broadcastDebugMessage(const String&);
+
 private:
     ClientConnection(xpc_connection_t);
 

Modified: trunk/Source/WebKit/webpushd/PushClientConnection.mm (286578 => 286579)


--- trunk/Source/WebKit/webpushd/PushClientConnection.mm	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/PushClientConnection.mm	2021-12-07 00:38:11 UTC (rev 286579)
@@ -31,6 +31,7 @@
 #import "WebPushDaemon.h"
 #import "WebPushDaemonConnectionConfiguration.h"
 #import <_javascript_Core/ConsoleTypes.h>
+#import <wtf/HexNumber.h>
 #import <wtf/Vector.h>
 #import <wtf/cocoa/Entitlements.h>
 
@@ -103,15 +104,19 @@
         return;
 
     m_debugModeEnabled = enabled;
+    broadcastDebugMessage(makeString("Turned Debug Mode ", m_debugModeEnabled ? "on" : "off"));
+}
 
-    auto identifier = hostAppCodeSigningIdentifier();
-    String message;
-    if (!identifier.isEmpty())
-        message = makeString("[webpushd - ", identifier, "] Turned Debug Mode ", m_debugModeEnabled ? "on" : "off");
+void ClientConnection::broadcastDebugMessage(const String& message)
+{
+    String messageIdentifier;
+    auto signingIdentifer = hostAppCodeSigningIdentifier();
+    if (signingIdentifer.isEmpty())
+        messageIdentifier = makeString ("[(0x", hex(reinterpret_cast<uint64_t>(m_xpcConnection.get()), WTF::HexConversionMode::Lowercase), ")] ");
     else
-        message = makeString("[webpushd] Turned Debug Mode ", m_debugModeEnabled ? "on" : "off");
+        messageIdentifier = makeString ("[", signingIdentifer, " (0x", hex(reinterpret_cast<uint64_t>(m_xpcConnection.get()), WTF::HexConversionMode::Lowercase), ")] ");
 
-    Daemon::singleton().broadcastDebugMessage(MessageLevel::Info, message);
+    Daemon::singleton().broadcastDebugMessage(JSC::MessageLevel::Info, makeString(messageIdentifier, message));
 }
 
 void ClientConnection::enqueueAppBundleRequest(std::unique_ptr<AppBundleRequest>&& request)
@@ -145,6 +150,8 @@
 
 void ClientConnection::connectionClosed()
 {
+    broadcastDebugMessage("Connection closed");
+
     RELEASE_ASSERT(m_xpcConnection);
     m_xpcConnection = nullptr;
 

Modified: trunk/Source/WebKit/webpushd/WebPushDaemon.mm (286578 => 286579)


--- trunk/Source/WebKit/webpushd/WebPushDaemon.mm	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/WebPushDaemon.mm	2021-12-07 00:38:11 UTC (rev 286579)
@@ -34,6 +34,7 @@
 #import "MockAppBundleRegistry.h"
 
 #import <wtf/CompletionHandler.h>
+#import <wtf/HexNumber.h>
 #import <wtf/NeverDestroyed.h>
 #import <wtf/Span.h>
 
@@ -180,6 +181,8 @@
 
 void Daemon::connectionAdded(xpc_connection_t connection)
 {
+    broadcastDebugMessage((JSC::MessageLevel)0, 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));
 }

Modified: trunk/Source/WebKit/webpushd/webpushtool/WebPushToolConnection.h (286578 => 286579)


--- trunk/Source/WebKit/webpushd/webpushtool/WebPushToolConnection.h	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/webpushtool/WebPushToolConnection.h	2021-12-07 00:38:11 UTC (rev 286579)
@@ -36,11 +36,21 @@
     StreamDebugMessages,
 };
 
+enum class PreferTestService : bool {
+    Yes,
+    No,
+};
+
+enum class Reconnect : bool {
+    Yes,
+    No,
+};
+
 class Connection : public CanMakeWeakPtr<Connection> {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static std::unique_ptr<Connection> create(Action, bool preferTestService);
-    Connection(Action, bool preferTestService);
+    static std::unique_ptr<Connection> create(Action, PreferTestService, Reconnect);
+    Connection(Action, PreferTestService, Reconnect);
 
     void connectToService();
 
@@ -54,6 +64,7 @@
     void sendAuditToken();
     
     Action m_action;
+    bool m_reconnect { false };
     RetainPtr<xpc_connection_t> m_connection;
     const char* m_serviceName;
 };

Modified: trunk/Source/WebKit/webpushd/webpushtool/WebPushToolConnection.mm (286578 => 286579)


--- trunk/Source/WebKit/webpushd/webpushtool/WebPushToolConnection.mm	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/webpushtool/WebPushToolConnection.mm	2021-12-07 00:38:11 UTC (rev 286579)
@@ -29,13 +29,14 @@
 #import <mach/mach_init.h>
 #import <mach/task.h>
 #import <pal/spi/cocoa/ServersSPI.h>
+#import <wtf/MainThread.h>
 #import <wtf/RetainPtr.h>
 
 namespace WebPushTool {
 
-std::unique_ptr<Connection> Connection::create(Action action, bool preferTestService)
+std::unique_ptr<Connection> Connection::create(Action action, PreferTestService preferTestService, Reconnect reconnect)
 {
-    return makeUnique<Connection>(action, preferTestService);
+    return makeUnique<Connection>(action, preferTestService, reconnect);
 }
 
 static mach_port_t maybeConnectToService(const char* serviceName)
@@ -52,14 +53,21 @@
     return MACH_PORT_NULL;
 }
 
-Connection::Connection(Action action, bool preferTestService)
+Connection::Connection(Action action, PreferTestService preferTestService, Reconnect reconnect)
     : m_action(action)
+    , m_reconnect(reconnect == Reconnect::Yes)
 {
-    if (preferTestService)
+    if (preferTestService == PreferTestService::Yes)
         m_serviceName = "org.webkit.webpushtestdaemon.service";
     else
         m_serviceName = "com.apple.webkit.webpushd.service";
+}
 
+void Connection::connectToService()
+{
+    if (m_connection)
+        return;
+
     m_connection = adoptNS(xpc_connection_create_mach_service(m_serviceName, dispatch_get_main_queue(), 0));
 
     xpc_connection_set_event_handler(m_connection.get(), [this, weakThis = WeakPtr { *this }](xpc_object_t event) {
@@ -74,6 +82,8 @@
 
         if (event == XPC_ERROR_CONNECTION_INTERRUPTED) {
             printf("Connection closed\n");
+            if (m_reconnect)
+                printf("===============\nReconnecting...\n");
             connectionDropped();
             return;
         }
@@ -85,13 +95,7 @@
 
         RELEASE_ASSERT_NOT_REACHED();
     });
-}
 
-void Connection::connectToService()
-{
-    if (!m_connection)
-        return;
-
     auto result = maybeConnectToService(m_serviceName);
     if (result == MACH_PORT_NULL)
         printf("Waiting for service '%s' to be available\n", m_serviceName);
@@ -154,6 +158,14 @@
 void Connection::connectionDropped()
 {
     m_connection = nullptr;
+    if (m_reconnect) {
+        callOnMainRunLoop([this, weakThis = WeakPtr { this }] {
+            if (weakThis)
+                connectToService();
+        });
+        return;
+    }
+
     CFRunLoopStop(CFRunLoopGetCurrent());
 }
 

Modified: trunk/Source/WebKit/webpushd/webpushtool/WebPushToolMain.mm (286578 => 286579)


--- trunk/Source/WebKit/webpushd/webpushtool/WebPushToolMain.mm	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Source/WebKit/webpushd/webpushtool/WebPushToolMain.mm	2021-12-07 00:38:11 UTC (rev 286579)
@@ -27,6 +27,7 @@
 #import "WebPushToolConnection.h"
 #import <Foundation/Foundation.h>
 #import <optional>
+#import <wtf/MainThread.h>
 
 __attribute__((__noreturn__))
 static void printUsageAndTerminate(NSString *message)
@@ -38,6 +39,7 @@
     fprintf(stderr, "  --development              Connects to mach service \"org.webkit.webpushtestdaemon.service\" (Default)\n");
     fprintf(stderr, "  --production               Connects to mach service \"com.apple.webkit.webpushd.service\"\n");
     fprintf(stderr, "  --streamDebugMessages      Stream debug messages from webpushd\n");
+    fprintf(stderr, "  --reconnect                Reconnect after connection is lost\n");
     fprintf(stderr, "\n");
 
     exit(-1);
@@ -45,7 +47,10 @@
 
 int main(int, const char **)
 {
-    bool preferTestService = true;
+    WTF::initializeMainThread();
+
+    auto preferTestService = WebPushTool::PreferTestService::Yes;
+    auto reconnect = WebPushTool::Reconnect::No;
     std::optional<WebPushTool::Action> action;
 
     @autoreleasepool {
@@ -55,11 +60,13 @@
 
         for (NSString *argument in [arguments subarrayWithRange:NSMakeRange(1, arguments.count - 1)]) {
             if ([argument isEqualToString:@"--production"])
-                preferTestService = false;
-            if ([argument isEqualToString:@"--development"])
-                preferTestService = true;
+                preferTestService = WebPushTool::PreferTestService::No;
+            else if ([argument isEqualToString:@"--development"])
+                preferTestService = WebPushTool::PreferTestService::Yes;
             else if ([argument isEqualToString:@"--streamDebugMessages"])
                 action = ""
+            else if ([argument isEqualToString:@"--reconnect"])
+                reconnect = WebPushTool::Reconnect::Yes;
             else
                 printUsageAndTerminate([NSString stringWithFormat:@"Invalid option provided: %@", argument]);
         }
@@ -68,7 +75,7 @@
     if (!action)
         printUsageAndTerminate(@"No action provided");
 
-    auto connection = WebPushTool::Connection::create(*action, preferTestService);
+    auto connection = WebPushTool::Connection::create(*action, preferTestService, reconnect);
     connection->connectToService();
 
     CFRunLoopRun();

Modified: trunk/Tools/ChangeLog (286578 => 286579)


--- trunk/Tools/ChangeLog	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Tools/ChangeLog	2021-12-07 00:38:11 UTC (rev 286579)
@@ -1,3 +1,13 @@
+2021-12-06  Brady Eidson  <beid...@apple.com>
+
+        webpushd/webpushtool debugging additions
+        https://bugs.webkit.org/show_bug.cgi?id=233864
+
+        Reviewed by Alex Christensen.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WebPushDaemon.mm:
+        (TestWebKitAPI::TEST): Adapt to debug message changes.
+
 2021-12-04  Jonathan Bedard  <jbed...@apple.com>
 
         [reporelaypy] Add implementation of commits.webkit.org

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebPushDaemon.mm (286578 => 286579)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebPushDaemon.mm	2021-12-07 00:36:58 UTC (rev 286578)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebPushDaemon.mm	2021-12-07 00:38:11 UTC (rev 286579)
@@ -181,10 +181,15 @@
         if (!debugMessage)
             return;
 
-        bool stringMatches = !strcmp(debugMessage, "[webpushd - TestWebKitAPI] Turned Debug Mode on");
-        if (!stringMatches)
-            stringMatches = !strcmp(debugMessage, "[webpushd - com.apple.WebKit.TestWebKitAPI] Turned Debug Mode on");
+        NSString *nsMessage = [NSString stringWithUTF8String:debugMessage];
 
+        // Ignore possible connections/messages from webpushtool
+        if ([nsMessage hasPrefix:@"[webpushtool "])
+            return;
+
+        bool stringMatches = [nsMessage hasPrefix:@"[com.apple.WebKit.TestWebKitAPI"] || [nsMessage hasPrefix:@"[TestWebKitAPI"];
+        stringMatches = stringMatches && [nsMessage hasSuffix:@" Turned Debug Mode on"];
+
         EXPECT_TRUE(stringMatches);
 
         done = true;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to