Re-enable CustomDisplayName and DefaultDisplayName API tests on Monterey
https://bugs.webkit.org/show_bug.cgi?id=234613

Patch by Alex Christensen <achristensen@webkit.org> on 2022-01-24
Reviewed by Brady Eidson.

Source/WebKit:

When we introduced setting the display name from the network process,
we didn't update the tests to get the information from the process that can access it.

* NetworkProcess/NetworkConnectionToWebProcess.h:
* NetworkProcess/NetworkConnectionToWebProcess.messages.in:
* NetworkProcess/mac/NetworkConnectionToWebProcessMac.mm:
(WebKit::NetworkConnectionToWebProcess::updateActivePages):
(WebKit::NetworkConnectionToWebProcess::getProcessDisplayName):
* WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
(WebKit::WebPage::getProcessDisplayName):
* WebProcess/WebProcess.h:
* WebProcess/cocoa/WebProcessCocoa.mm:
(WebKit::WebProcess::auditTokenForSelf):
(WebKit::WebProcess::updateProcessName):
(WebKit::WebProcess::getProcessDisplayName):
(WebKit::WebProcess::updateActivePages):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm:
(TestWebKitAPI::TEST):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@288540 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebKit/ChangeLog b/Source/WebKit/ChangeLog
index 8426db7..a5f6956 100644
--- a/Source/WebKit/ChangeLog
+++ b/Source/WebKit/ChangeLog
@@ -1,3 +1,27 @@
+2022-01-24  Alex Christensen  <achristensen@webkit.org>
+
+        Re-enable CustomDisplayName and DefaultDisplayName API tests on Monterey
+        https://bugs.webkit.org/show_bug.cgi?id=234613
+
+        Reviewed by Brady Eidson.
+
+        When we introduced setting the display name from the network process,
+        we didn't update the tests to get the information from the process that can access it.
+
+        * NetworkProcess/NetworkConnectionToWebProcess.h:
+        * NetworkProcess/NetworkConnectionToWebProcess.messages.in:
+        * NetworkProcess/mac/NetworkConnectionToWebProcessMac.mm:
+        (WebKit::NetworkConnectionToWebProcess::updateActivePages):
+        (WebKit::NetworkConnectionToWebProcess::getProcessDisplayName):
+        * WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
+        (WebKit::WebPage::getProcessDisplayName):
+        * WebProcess/WebProcess.h:
+        * WebProcess/cocoa/WebProcessCocoa.mm:
+        (WebKit::WebProcess::auditTokenForSelf):
+        (WebKit::WebProcess::updateProcessName):
+        (WebKit::WebProcess::getProcessDisplayName):
+        (WebKit::WebProcess::updateActivePages):
+
 2022-01-24  Sihui Liu  <sihui_liu@apple.com>
 
         Regression (r286936): SessionStorage data is not cleared when deleting website data by modification time
diff --git a/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h b/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h
index 992ec05..0a6e684 100644
--- a/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h
+++ b/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.h
@@ -278,6 +278,7 @@
 
 #if PLATFORM(MAC)
     void updateActivePages(const String& name, const Vector<String>& activePagesOrigins, audit_token_t);
+    void getProcessDisplayName(audit_token_t, CompletionHandler<void(const String&)>&&);
 #endif
 
 #if USE(LIBWEBRTC)
diff --git a/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in b/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in
index 650b2aa..2428350 100644
--- a/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in
+++ b/Source/WebKit/NetworkProcess/NetworkConnectionToWebProcess.messages.in
@@ -105,6 +105,7 @@
     RegisterURLSchemesAsCORSEnabled(Vector<String> schemes);
     SetCORSDisablingPatterns(WebCore::PageIdentifier pageIdentifier, Vector<String> patterns)
 #if PLATFORM(MAC)
+    GetProcessDisplayName(audit_token_t auditToken) -> (String displayName) Async
     UpdateActivePages(String name, Vector<String> activePagesOrigins, audit_token_t auditToken)
 #endif
     SetResourceLoadSchedulingMode(WebCore::PageIdentifier webPageID, enum:uint8_t WebCore::LoadSchedulingMode mode)
diff --git a/Source/WebKit/NetworkProcess/mac/NetworkConnectionToWebProcessMac.mm b/Source/WebKit/NetworkProcess/mac/NetworkConnectionToWebProcessMac.mm
index bf35d1d..6b18dd8 100644
--- a/Source/WebKit/NetworkProcess/mac/NetworkConnectionToWebProcessMac.mm
+++ b/Source/WebKit/NetworkProcess/mac/NetworkConnectionToWebProcessMac.mm
@@ -32,14 +32,33 @@
 namespace WebKit {
 
 #if PLATFORM(MAC)
+
 void NetworkConnectionToWebProcess::updateActivePages(const String& overrideDisplayName, const Vector<String>& activePagesOrigins, audit_token_t auditToken)
 {
+    // Setting and getting the display name of another process requires a private entitlement.
+#if USE(APPLE_INTERNAL_SDK)
     auto asn = adoptCF(_LSCopyLSASNForAuditToken(kLSDefaultSessionID, auditToken));
     if (!overrideDisplayName)
         _LSSetApplicationInformationItem(kLSDefaultSessionID, asn.get(), CFSTR("LSActivePageUserVisibleOriginsKey"), (__bridge CFArrayRef)createNSArray(activePagesOrigins).get(), nullptr);
     else
         _LSSetApplicationInformationItem(kLSDefaultSessionID, asn.get(), _kLSDisplayNameKey, overrideDisplayName.createCFString().get(), nullptr);
-}
+#else
+    UNUSED_PARAM(overrideDisplayName);
+    UNUSED_PARAM(activePagesOrigins);
+    UNUSED_PARAM(auditToken);
 #endif
+}
+
+void NetworkConnectionToWebProcess::getProcessDisplayName(audit_token_t auditToken, CompletionHandler<void(const String&)>&& completionHandler)
+{
+#if USE(APPLE_INTERNAL_SDK)
+    auto asn = adoptCF(_LSCopyLSASNForAuditToken(kLSDefaultSessionID, auditToken));
+    return completionHandler(adoptCF((CFStringRef)_LSCopyApplicationInformationItem(kLSDefaultSessionID, asn.get(), _kLSDisplayNameKey)).get());
+#else
+    completionHandler({ });
+#endif
+}
+
+#endif // PLATFORM(MAC)
 
 } // namespace WebKit
diff --git a/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm b/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm
index 72bc489..3456bef 100644
--- a/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm
+++ b/Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm
@@ -335,7 +335,11 @@
 void WebPage::getProcessDisplayName(CompletionHandler<void(String&&)>&& completionHandler)
 {
 #if PLATFORM(MAC)
+#if ENABLE(SET_WEBCONTENT_PROCESS_INFORMATION_IN_NETWORK_PROCESS)
+    WebProcess::singleton().getProcessDisplayName(WTFMove(completionHandler));
+#else
     completionHandler(adoptCF((CFStringRef)_LSCopyApplicationInformationItem(kLSDefaultSessionID, _LSGetCurrentApplicationASN(), _kLSDisplayNameKey)).get());
+#endif
 #else
     completionHandler({ });
 #endif
diff --git a/Source/WebKit/WebProcess/WebProcess.h b/Source/WebKit/WebProcess/WebProcess.h
index e87d733..b8032fb 100644
--- a/Source/WebKit/WebProcess/WebProcess.h
+++ b/Source/WebKit/WebProcess/WebProcess.h
@@ -289,6 +289,8 @@
 #if PLATFORM(COCOA)
     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
     void destroyRenderingResources();
+    void getProcessDisplayName(CompletionHandler<void(String&&)>&&);
+    std::optional<audit_token_t> auditTokenForSelf();
 #endif
 
     const String& uiProcessBundleIdentifier() const { return m_uiProcessBundleIdentifier; }
diff --git a/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm b/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm
index da79030..7dbd391 100644
--- a/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm
+++ b/Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm
@@ -488,6 +488,18 @@
 #endif
 }
 
+std::optional<audit_token_t> WebProcess::auditTokenForSelf()
+{
+    audit_token_t auditToken = { 0 };
+    mach_msg_type_number_t info_size = TASK_AUDIT_TOKEN_COUNT;
+    kern_return_t kr = task_info(mach_task_self(), TASK_AUDIT_TOKEN, reinterpret_cast<integer_t *>(&auditToken), &info_size);
+    if (kr != KERN_SUCCESS) {
+        WEBPROCESS_RELEASE_LOG_ERROR(Process, "Unable to get audit token for self. Error: %{public}s (%x)", mach_error_string(kr), kr);
+        return std::nullopt;
+    }
+    return auditToken;
+}
+
 void WebProcess::updateProcessName(IsInProcessInitialization isInProcessInitialization)
 {
 #if PLATFORM(MAC)
@@ -514,16 +526,13 @@
     // During WebProcess initialization, we are still able to talk to LaunchServices to set the process name so there is no need to go
     // via the NetworkProcess. Prewarmed WebProcesses also do not have a network process connection until they are actually used by
     // a page.
+
     if (isInProcessInitialization == IsInProcessInitialization::No) {
-        audit_token_t auditToken = { 0 };
-        mach_msg_type_number_t info_size = TASK_AUDIT_TOKEN_COUNT;
-        kern_return_t kr = task_info(mach_task_self(), TASK_AUDIT_TOKEN, reinterpret_cast<integer_t *>(&auditToken), &info_size);
-        if (kr != KERN_SUCCESS) {
-            WEBPROCESS_RELEASE_LOG_ERROR(Process, "updateProcessName: Unable to get audit token for self. Error: %{public}s (%x)", mach_error_string(kr), kr);
+        auto auditToken = auditTokenForSelf();
+        if (!auditToken)
             return;
-        }
         String displayName = applicationName.get();
-        ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::UpdateActivePages(displayName, Vector<String>(), auditToken), 0);
+        ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::UpdateActivePages(displayName, Vector<String>(), *auditToken), 0);
         return;
     }
 #endif // ENABLE(SET_WEBCONTENT_PROCESS_INFORMATION_IN_NETWORK_PROCESS)
@@ -741,19 +750,26 @@
 }
 #endif
 
+void WebProcess::getProcessDisplayName(CompletionHandler<void(String&&)>&& completionHandler)
+{
+#if PLATFORM(MAC)
+    auto auditToken = auditTokenForSelf();
+    if (!auditToken)
+        return completionHandler({ });
+    ensureNetworkProcessConnection().connection().sendWithAsyncReply(Messages::NetworkConnectionToWebProcess::GetProcessDisplayName(*auditToken), WTFMove(completionHandler));
+#else
+    completionHandler({ });
+#endif
+}
+
 void WebProcess::updateActivePages(const String& overrideDisplayName)
 {
 #if PLATFORM(MAC)
 #if ENABLE(SET_WEBCONTENT_PROCESS_INFORMATION_IN_NETWORK_PROCESS)
-    audit_token_t auditToken = { 0 };
-    mach_msg_type_number_t info_size = TASK_AUDIT_TOKEN_COUNT;
-    kern_return_t kr = task_info(mach_task_self(), TASK_AUDIT_TOKEN, reinterpret_cast<integer_t *>(&auditToken), &info_size);
-    if (kr != KERN_SUCCESS) {
-        WEBPROCESS_RELEASE_LOG_ERROR(Process, "updateActivePages: Unable to get audit token for self. Error: %{public}s (%x)", mach_error_string(kr), kr);
+    auto auditToken = auditTokenForSelf();
+    if (!auditToken)
         return;
-    }
-
-    ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::UpdateActivePages(overrideDisplayName, activePagesOrigins(m_pageMap), auditToken), 0);
+    ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::UpdateActivePages(overrideDisplayName, activePagesOrigins(m_pageMap), *auditToken), 0);
 #else
     if (!overrideDisplayName) {
         RunLoop::main().dispatch([activeOrigins = activePagesOrigins(m_pageMap)] {
diff --git a/Tools/ChangeLog b/Tools/ChangeLog
index f562c91..9477ec6 100644
--- a/Tools/ChangeLog
+++ b/Tools/ChangeLog
@@ -1,3 +1,13 @@
+2022-01-24  Alex Christensen  <achristensen@webkit.org>
+
+        Re-enable CustomDisplayName and DefaultDisplayName API tests on Monterey
+        https://bugs.webkit.org/show_bug.cgi?id=234613
+
+        Reviewed by Brady Eidson.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm:
+        (TestWebKitAPI::TEST):
+
 2022-01-20  Jonathan Bedard  <jbedard@apple.com>
 
         [EWS] Support pull request in AnaylzePatch
diff --git a/Tools/TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm b/Tools/TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm
index e596326..b3ad41c 100644
--- a/Tools/TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm
+++ b/Tools/TestWebKitAPI/Tests/WebKitCocoa/DisplayName.mm
@@ -53,12 +53,9 @@
     }];
 }
 
-// FIXME: Re-enable this test for Monterey+ once rdar://80353834 is resolved.
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000
-TEST(WebKit, DISABLED_CustomDisplayName)
-#else
+// The network process requires a private entitlement to get and set the process name for the web process.
+#if !ENABLE(SET_WEBCONTENT_PROCESS_INFORMATION_IN_NETWORK_PROCESS) || USE(APPLE_INTERNAL_SDK)
 TEST(WebKit, CustomDisplayName)
-#endif
 {
     auto configuration = adoptNS([WKWebViewConfiguration new]);
     NSString *displayNameToSet = @"test display name";
@@ -71,12 +68,7 @@
     Util::run(&done);
 }
 
-// FIXME: Re-enable this test for Monterey+ once rdar://80353834 is resolved.
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000
-TEST(WebKit, DISABLED_DefaultDisplayName)
-#else
 TEST(WebKit, DefaultDisplayName)
-#endif
 {
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
     [webView synchronouslyLoadHTMLString:@"start web process"];
@@ -85,6 +77,7 @@
     checkUntilDisplayNameIs(webView.get(), @"TestWebKitAPI Web Content", &done);
     Util::run(&done);
 }
+#endif // !ENABLE(SET_WEBCONTENT_PROCESS_INFORMATION_IN_NETWORK_PROCESS) || USE(APPLE_INTERNAL_SDK)
 #endif // PLATFORM(MAC)
 
 }