<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[285274] branches/safari-612-branch</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/285274">285274</a></dd>
<dt>Author</dt> <dd>repstein@apple.com</dd>
<dt>Date</dt> <dd>2021-11-04 12:38:47 -0700 (Thu, 04 Nov 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Cherry-pick <a href="http://trac.webkit.org/projects/webkit/changeset/282632">r282632</a>. rdar://problem/83067708

    WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
    <https://webkit.org/b/230227>
    <rdar://problem/83067708>

    Reviewed by Darin Adler.

    Source/WebKit:

    To fix the bug, implement an Objective-C class named
    WKProcessPoolWeakObserver which contains an instance variable
    holding a WeakPtr<WebProcessPool>, and tell CFNotificationCenter
    to hold a weak reference to WKProcessPoolWeakObserver.

    Since WKProcessPoolWeakObserver is self-contained within the
    source file, it uses the NS_DIRECT_MEMBERS attribute since it
    does not require the overhead of dynamic Objective-C method
    dispatch.

    * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
    (-[WKProcessPoolWeakObserver initWithWeakPtr:]): Add.
    (-[WKProcessPoolWeakObserver pool]): Add.
    - Implement WKProcessPoolWeakObserver class.
    - Note that init methods can never be marked as NS_DIRECT, and
      @property statements can't use NS_DIRECT in their declaration,
      so the @property is declared in a category which uses
      NS_DIRECT_MEMBERS.
    (WebKit::extractWebProcessPool): Add.
    - Static helper method for extracting RefPtr<WebProcessPool>
      from type-punned WKProcessPoolWeakObserver.
    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
    - Clean up function parameter list.
    - Use extractWebProcessPool() helper method to get a
      RefPtr<WebProcessPool> from `observer`.
    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
    - Private helper methods to reduce duplicate code.
    - Use m_weakObserver for CFNotificationCenter observer and
      include _CFNotificationObserverIsObjC to fix the bug.
    (WebKit::WebProcessPool::registerNotificationObservers):
    - Make use of new addCFNotificationObserver() helper method.
    - Fixes use of static_cast<CFStringRef> to make code ready for
      ARC by using a __bridge cast or removing the static_cast when
      CFSTR() is used.
    (WebKit::WebProcessPool::unregisterNotificationObservers):
    - Make use of new removeCFNotificationObserver() helper method.

    * UIProcess/WebProcessPool.h:
    - Add m_weakObserver instance variable to hold the
      WKProcessPoolWeakObserver object.
    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
    - Add declarations for new helper methods.
    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
    - Clean up function parameter list.

    Source/WTF:

    Tests (API):
        TestWTF.TypeCastsNS.checked_ns_cast
        TestWTF.TypeCastsNS.dynamic_ns_cast
        TestWTF.TypeCastsNS.dynamic_ns_cast_RetainPtr

    * WTF.xcodeproj/project.pbxproj:
    * wtf/PlatformMac.cmake:
    - Add new header files to the project.

    * wtf/cocoa/TypeCastsNS.h: Add.
    (WTF::checked_ns_cast):
    (WTF::dynamic_ns_cast):
    - Add casts for NS objects similar to TypeCastsCF.h.

    * wtf/PlatformHave.h:
    (HAVE_NS_DIRECT_SUPPORT): Add.
    - Note that clang for macOS 11 Big Sur claims to know about the
      attributes, but will fail to compile if they are actually
      used.

    * wtf/spi/cocoa/NSObjCRuntimeSPI.h: Add.
    (NS_DIRECT):
    (NS_DIRECT_MEMBERS):
    - Define compiler attributes for direct dispatch of Objective-C
      methods.

    Tools:

    * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
    - Add TypeCastsNS.mm to the project.
    * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm: Add.
    (TestWebKitAPI::TEST): Add tests for <wtf/TypeCastsNS.h>.

    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@282632 268f45cc-cd09-0410-ab3c-d52691b4dbfc</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branchessafari612branchSourceWTFChangeLog">branches/safari-612-branch/Source/WTF/ChangeLog</a></li>
<li><a href="#branchessafari612branchSourceWTFWTFxcodeprojprojectpbxproj">branches/safari-612-branch/Source/WTF/WTF.xcodeproj/project.pbxproj</a></li>
<li><a href="#branchessafari612branchSourceWTFwtfPlatformHaveh">branches/safari-612-branch/Source/WTF/wtf/PlatformHave.h</a></li>
<li><a href="#branchessafari612branchSourceWTFwtfPlatformMaccmake">branches/safari-612-branch/Source/WTF/wtf/PlatformMac.cmake</a></li>
<li><a href="#branchessafari612branchSourceWebKitChangeLog">branches/safari-612-branch/Source/WebKit/ChangeLog</a></li>
<li><a href="#branchessafari612branchSourceWebKitUIProcessCocoaWebProcessPoolCocoamm">branches/safari-612-branch/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm</a></li>
<li><a href="#branchessafari612branchSourceWebKitUIProcessWebProcessPoolh">branches/safari-612-branch/Source/WebKit/UIProcess/WebProcessPool.h</a></li>
<li><a href="#branchessafari612branchToolsChangeLog">branches/safari-612-branch/Tools/ChangeLog</a></li>
<li><a href="#branchessafari612branchToolsTestWebKitAPITestWebKitAPIxcodeprojprojectpbxproj">branches/safari-612-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#branchessafari612branchSourceWTFwtfcocoaTypeCastsNSh">branches/safari-612-branch/Source/WTF/wtf/cocoa/TypeCastsNS.h</a></li>
<li><a href="#branchessafari612branchSourceWTFwtfspicocoaNSObjCRuntimeSPIh">branches/safari-612-branch/Source/WTF/wtf/spi/cocoa/NSObjCRuntimeSPI.h</a></li>
<li><a href="#branchessafari612branchToolsTestWebKitAPITestsWTFcocoaTypeCastsNSmm">branches/safari-612-branch/Tools/TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branchessafari612branchSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WTF/ChangeLog (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WTF/ChangeLog  2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WTF/ChangeLog     2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -1,3 +1,142 @@
</span><ins>+2021-11-04  Russell Epstein  <repstein@apple.com>
+
+        Cherry-pick r282632. rdar://problem/83067708
+
+    WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
+    <https://webkit.org/b/230227>
+    <rdar://problem/83067708>
+    
+    Reviewed by Darin Adler.
+    
+    Source/WebKit:
+    
+    To fix the bug, implement an Objective-C class named
+    WKProcessPoolWeakObserver which contains an instance variable
+    holding a WeakPtr<WebProcessPool>, and tell CFNotificationCenter
+    to hold a weak reference to WKProcessPoolWeakObserver.
+    
+    Since WKProcessPoolWeakObserver is self-contained within the
+    source file, it uses the NS_DIRECT_MEMBERS attribute since it
+    does not require the overhead of dynamic Objective-C method
+    dispatch.
+    
+    * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+    (-[WKProcessPoolWeakObserver initWithWeakPtr:]): Add.
+    (-[WKProcessPoolWeakObserver pool]): Add.
+    - Implement WKProcessPoolWeakObserver class.
+    - Note that init methods can never be marked as NS_DIRECT, and
+      @property statements can't use NS_DIRECT in their declaration,
+      so the @property is declared in a category which uses
+      NS_DIRECT_MEMBERS.
+    (WebKit::extractWebProcessPool): Add.
+    - Static helper method for extracting RefPtr<WebProcessPool>
+      from type-punned WKProcessPoolWeakObserver.
+    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+    - Clean up function parameter list.
+    - Use extractWebProcessPool() helper method to get a
+      RefPtr<WebProcessPool> from `observer`.
+    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+    - Private helper methods to reduce duplicate code.
+    - Use m_weakObserver for CFNotificationCenter observer and
+      include _CFNotificationObserverIsObjC to fix the bug.
+    (WebKit::WebProcessPool::registerNotificationObservers):
+    - Make use of new addCFNotificationObserver() helper method.
+    - Fixes use of static_cast<CFStringRef> to make code ready for
+      ARC by using a __bridge cast or removing the static_cast when
+      CFSTR() is used.
+    (WebKit::WebProcessPool::unregisterNotificationObservers):
+    - Make use of new removeCFNotificationObserver() helper method.
+    
+    * UIProcess/WebProcessPool.h:
+    - Add m_weakObserver instance variable to hold the
+      WKProcessPoolWeakObserver object.
+    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+    - Add declarations for new helper methods.
+    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+    - Clean up function parameter list.
+    
+    Source/WTF:
+    
+    Tests (API):
+        TestWTF.TypeCastsNS.checked_ns_cast
+        TestWTF.TypeCastsNS.dynamic_ns_cast
+        TestWTF.TypeCastsNS.dynamic_ns_cast_RetainPtr
+    
+    * WTF.xcodeproj/project.pbxproj:
+    * wtf/PlatformMac.cmake:
+    - Add new header files to the project.
+    
+    * wtf/cocoa/TypeCastsNS.h: Add.
+    (WTF::checked_ns_cast):
+    (WTF::dynamic_ns_cast):
+    - Add casts for NS objects similar to TypeCastsCF.h.
+    
+    * wtf/PlatformHave.h:
+    (HAVE_NS_DIRECT_SUPPORT): Add.
+    - Note that clang for macOS 11 Big Sur claims to know about the
+      attributes, but will fail to compile if they are actually
+      used.
+    
+    * wtf/spi/cocoa/NSObjCRuntimeSPI.h: Add.
+    (NS_DIRECT):
+    (NS_DIRECT_MEMBERS):
+    - Define compiler attributes for direct dispatch of Objective-C
+      methods.
+    
+    Tools:
+    
+    * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+    - Add TypeCastsNS.mm to the project.
+    * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm: Add.
+    (TestWebKitAPI::TEST): Add tests for <wtf/TypeCastsNS.h>.
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@282632 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2021-09-16  David Kilzer  <ddkilzer@apple.com>
+
+            WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
+            <https://webkit.org/b/230227>
+            <rdar://problem/83067708>
+
+            Reviewed by Darin Adler.
+
+            Tests (API):
+                TestWTF.TypeCastsNS.checked_ns_cast
+                TestWTF.TypeCastsNS.dynamic_ns_cast
+                TestWTF.TypeCastsNS.dynamic_ns_cast_RetainPtr
+
+            * WTF.xcodeproj/project.pbxproj:
+            * wtf/PlatformMac.cmake:
+            - Add new header files to the project.
+
+            * wtf/cocoa/TypeCastsNS.h: Add.
+            (WTF::checked_ns_cast):
+            (WTF::dynamic_ns_cast):
+            - Add casts for NS objects similar to TypeCastsCF.h.
+
+            * wtf/PlatformHave.h:
+            (HAVE_NS_DIRECT_SUPPORT): Add.
+            - Note that clang for macOS 11 Big Sur claims to know about the
+              attributes, but will fail to compile if they are actually
+              used.
+
+            * wtf/spi/cocoa/NSObjCRuntimeSPI.h: Add.
+            (NS_DIRECT):
+            (NS_DIRECT_MEMBERS):
+            - Define compiler attributes for direct dispatch of Objective-C
+              methods.
+
</ins><span class="cx"> 2021-11-02  Russell Epstein  <repstein@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Cherry-pick r283732. rdar://problem/84349027
</span></span></pre></div>
<a id="branchessafari612branchSourceWTFWTFxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WTF/WTF.xcodeproj/project.pbxproj (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WTF/WTF.xcodeproj/project.pbxproj      2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WTF/WTF.xcodeproj/project.pbxproj 2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -389,7 +389,9 @@
</span><span class="cx">          413FE8F51F8D2EAB00F6D7D7 /* CallbackAggregator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CallbackAggregator.h; sourceTree = "<group>"; };
</span><span class="cx">          430B47871AAAAC1A001223DA /* StringCommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringCommon.h; sourceTree = "<group>"; };
</span><span class="cx">          4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ASCIICType.cpp; sourceTree = "<group>"; };
</span><ins>+               44309D4026EFC14A00402A67 /* NSObjCRuntimeSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NSObjCRuntimeSPI.h; sourceTree = "<group>"; };
</ins><span class="cx">           4468567225094FE8008CCA05 /* ThreadSanitizerSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadSanitizerSupport.h; sourceTree = "<group>"; };
</span><ins>+               44CDE4D226EE6CDA009F6ACB /* TypeCastsNS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TypeCastsNS.h; sourceTree = "<group>"; };
</ins><span class="cx">           46209A27266D543A007F8F4A /* CancellableTask.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CancellableTask.h; sourceTree = "<group>"; };
</span><span class="cx">          46BA9EAB1F4CD61E009A2BBC /* CompletionHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompletionHandler.h; sourceTree = "<group>"; };
</span><span class="cx">          46BEB6E922FFDDD500269867 /* RefCounted.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RefCounted.cpp; sourceTree = "<group>"; };
</span><span class="lines">@@ -1535,6 +1537,7 @@
</span><span class="cx">                          CEA072A7236FFA160018839C /* CrashReporterClientSPI.h */,
</span><span class="cx">                          E31BDE2822E913CC0029B105 /* MachVMSPI.h */,
</span><span class="cx">                          1C503BE423AADEEA0072E66B /* NSLocaleSPI.h */,
</span><ins>+                               44309D4026EFC14A00402A67 /* NSObjCRuntimeSPI.h */,
</ins><span class="cx">                           93D191CF20CAECE800C51B8E /* objcSPI.h */,
</span><span class="cx">                          2DA442FC24904CA000C9C59D /* OSLogSPI.h */,
</span><span class="cx">                          A5098B011C16A4F900087797 /* SecuritySPI.h */,
</span><span class="lines">@@ -1615,6 +1618,7 @@
</span><span class="cx">                          A30D412C1F0DE0BA00B71954 /* SoftLinking.h */,
</span><span class="cx">                          BC3FEB5E267FCD460054006A /* SpanCocoa.h */,
</span><span class="cx">                          EB61EDC62409CCC0001EFE36 /* SystemTracingCocoa.cpp */,
</span><ins>+                               44CDE4D226EE6CDA009F6ACB /* TypeCastsNS.h */,
</ins><span class="cx">                           5CC0EE862162BC2200A1A842 /* URLCocoa.mm */,
</span><span class="cx">                          93241657243BC2E50032FAAE /* VectorCocoa.h */,
</span><span class="cx">                          E4A0AD3C1A96253C00536DF6 /* WorkQueueCocoa.cpp */,
</span></span></pre></div>
<a id="branchessafari612branchSourceWTFwtfPlatformHaveh"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WTF/wtf/PlatformHave.h (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WTF/wtf/PlatformHave.h 2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WTF/wtf/PlatformHave.h    2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -859,6 +859,14 @@
</span><span class="cx"> #endif
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+#if COMPILER(GCC_COMPATIBLE) && defined(__has_attribute)
+#if __has_attribute(objc_direct) && (!PLATFORM(MAC) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 120000)
+#if !defined(HAVE_NS_DIRECT_SUPPORT)
+#define HAVE_NS_DIRECT_SUPPORT 1
+#endif
+#endif
+#endif
+
</ins><span class="cx"> #if PLATFORM(COCOA)
</span><span class="cx"> #if !defined(HAVE_CG_CONTEXT_DRAW_PATH_DIRECT)
</span><span class="cx"> #define HAVE_CG_CONTEXT_DRAW_PATH_DIRECT 1
</span></span></pre></div>
<a id="branchessafari612branchSourceWTFwtfPlatformMaccmake"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WTF/wtf/PlatformMac.cmake (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WTF/wtf/PlatformMac.cmake      2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WTF/wtf/PlatformMac.cmake 2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -20,6 +20,7 @@
</span><span class="cx">     cocoa/RuntimeApplicationChecksCocoa.h
</span><span class="cx">     cocoa/SoftLinking.h
</span><span class="cx">     cocoa/SpanCocoa.h
</span><ins>+    cocoa/TypeCastsNS.h
</ins><span class="cx">     cocoa/VectorCocoa.h
</span><span class="cx"> 
</span><span class="cx">     darwin/WeakLinking.h
</span><span class="lines">@@ -31,6 +32,7 @@
</span><span class="cx">     spi/cocoa/CrashReporterClientSPI.h
</span><span class="cx">     spi/cocoa/MachVMSPI.h
</span><span class="cx">     spi/cocoa/NSLocaleSPI.h
</span><ins>+    spi/cocoa/NSObjCRuntimeSPI.h
</ins><span class="cx">     spi/cocoa/SecuritySPI.h
</span><span class="cx">     spi/cocoa/objcSPI.h
</span><span class="cx"> 
</span></span></pre></div>
<a id="branchessafari612branchSourceWTFwtfcocoaTypeCastsNSh"></a>
<div class="addfile"><h4>Added: branches/safari-612-branch/Source/WTF/wtf/cocoa/TypeCastsNS.h (0 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WTF/wtf/cocoa/TypeCastsNS.h                            (rev 0)
+++ branches/safari-612-branch/Source/WTF/wtf/cocoa/TypeCastsNS.h       2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -0,0 +1,73 @@
</span><ins>+/*
+ * Copyright (C) 2014-2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/Assertions.h>
+#include <wtf/RetainPtr.h>
+
+namespace WTF {
+
+// Use checked_ns_cast<> instead of dynamic_ns_cast<> when a specific NS type is required.
+
+template<typename T> T* checked_ns_cast(id object)
+{
+    using ValueType = std::remove_pointer_t<T>;
+    using PtrType = ValueType*;
+
+    if (!object)
+        return nullptr;
+
+    RELEASE_ASSERT_WITH_SECURITY_IMPLICATION([object isKindOfClass:[ValueType class]]);
+
+    return reinterpret_cast<PtrType>(object);
+}
+
+// Use dynamic_ns_cast<> instead of checked_ns_cast<> when actively checking NS types,
+// similar to dynamic_cast<> in C++. Be sure to include a nil check.
+
+template<typename T> T* dynamic_ns_cast(id object)
+{
+    using ValueType = std::remove_pointer_t<T>;
+    using PtrType = ValueType*;
+
+    if (![object isKindOfClass:[ValueType class]])
+        return nullptr;
+
+    return reinterpret_cast<PtrType>(object);
+}
+
+template<typename T, typename U> RetainPtr<T> dynamic_ns_cast(RetainPtr<U>&& object)
+{
+    if (![object isKindOfClass:[T class]])
+        return nullptr;
+
+    return WTFMove(object);
+}
+
+} // namespace WTF
+
+using WTF::checked_ns_cast;
+using WTF::dynamic_ns_cast;
</ins></span></pre></div>
<a id="branchessafari612branchSourceWTFwtfspicocoaNSObjCRuntimeSPIh"></a>
<div class="addfile"><h4>Added: branches/safari-612-branch/Source/WTF/wtf/spi/cocoa/NSObjCRuntimeSPI.h (0 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WTF/wtf/spi/cocoa/NSObjCRuntimeSPI.h                           (rev 0)
+++ branches/safari-612-branch/Source/WTF/wtf/spi/cocoa/NSObjCRuntimeSPI.h      2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -0,0 +1,54 @@
</span><ins>+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if USE(APPLE_INTERNAL_SDK)
+
+#include <Foundation/NSObjCRuntime_Private.h>
+
+#else
+
+#include <wtf/Platform.h>
+
+// Apply this to a specific method in the @interface or @implementation.
+#ifndef NS_DIRECT
+#if HAVE(NS_DIRECT_SUPPORT)
+#define NS_DIRECT __attribute__((objc_direct))
+#else
+#define NS_DIRECT
+#endif
+#endif
+
+// Apply this to the @interface or @implementation of a class.
+#ifndef NS_DIRECT_MEMBERS
+#if HAVE(NS_DIRECT_SUPPORT)
+#define NS_DIRECT_MEMBERS __attribute__((objc_direct_members))
+#else
+#define NS_DIRECT_MEMBERS
+#endif
+#endif
+
+#endif
</ins></span></pre></div>
<a id="branchessafari612branchSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WebKit/ChangeLog (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WebKit/ChangeLog       2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WebKit/ChangeLog  2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -1,5 +1,173 @@
</span><span class="cx"> 2021-11-04  Russell Epstein  <repstein@apple.com>
</span><span class="cx"> 
</span><ins>+        Cherry-pick r282632. rdar://problem/83067708
+
+    WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
+    <https://webkit.org/b/230227>
+    <rdar://problem/83067708>
+    
+    Reviewed by Darin Adler.
+    
+    Source/WebKit:
+    
+    To fix the bug, implement an Objective-C class named
+    WKProcessPoolWeakObserver which contains an instance variable
+    holding a WeakPtr<WebProcessPool>, and tell CFNotificationCenter
+    to hold a weak reference to WKProcessPoolWeakObserver.
+    
+    Since WKProcessPoolWeakObserver is self-contained within the
+    source file, it uses the NS_DIRECT_MEMBERS attribute since it
+    does not require the overhead of dynamic Objective-C method
+    dispatch.
+    
+    * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+    (-[WKProcessPoolWeakObserver initWithWeakPtr:]): Add.
+    (-[WKProcessPoolWeakObserver pool]): Add.
+    - Implement WKProcessPoolWeakObserver class.
+    - Note that init methods can never be marked as NS_DIRECT, and
+      @property statements can't use NS_DIRECT in their declaration,
+      so the @property is declared in a category which uses
+      NS_DIRECT_MEMBERS.
+    (WebKit::extractWebProcessPool): Add.
+    - Static helper method for extracting RefPtr<WebProcessPool>
+      from type-punned WKProcessPoolWeakObserver.
+    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+    - Clean up function parameter list.
+    - Use extractWebProcessPool() helper method to get a
+      RefPtr<WebProcessPool> from `observer`.
+    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+    - Private helper methods to reduce duplicate code.
+    - Use m_weakObserver for CFNotificationCenter observer and
+      include _CFNotificationObserverIsObjC to fix the bug.
+    (WebKit::WebProcessPool::registerNotificationObservers):
+    - Make use of new addCFNotificationObserver() helper method.
+    - Fixes use of static_cast<CFStringRef> to make code ready for
+      ARC by using a __bridge cast or removing the static_cast when
+      CFSTR() is used.
+    (WebKit::WebProcessPool::unregisterNotificationObservers):
+    - Make use of new removeCFNotificationObserver() helper method.
+    
+    * UIProcess/WebProcessPool.h:
+    - Add m_weakObserver instance variable to hold the
+      WKProcessPoolWeakObserver object.
+    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+    - Add declarations for new helper methods.
+    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+    - Clean up function parameter list.
+    
+    Source/WTF:
+    
+    Tests (API):
+        TestWTF.TypeCastsNS.checked_ns_cast
+        TestWTF.TypeCastsNS.dynamic_ns_cast
+        TestWTF.TypeCastsNS.dynamic_ns_cast_RetainPtr
+    
+    * WTF.xcodeproj/project.pbxproj:
+    * wtf/PlatformMac.cmake:
+    - Add new header files to the project.
+    
+    * wtf/cocoa/TypeCastsNS.h: Add.
+    (WTF::checked_ns_cast):
+    (WTF::dynamic_ns_cast):
+    - Add casts for NS objects similar to TypeCastsCF.h.
+    
+    * wtf/PlatformHave.h:
+    (HAVE_NS_DIRECT_SUPPORT): Add.
+    - Note that clang for macOS 11 Big Sur claims to know about the
+      attributes, but will fail to compile if they are actually
+      used.
+    
+    * wtf/spi/cocoa/NSObjCRuntimeSPI.h: Add.
+    (NS_DIRECT):
+    (NS_DIRECT_MEMBERS):
+    - Define compiler attributes for direct dispatch of Objective-C
+      methods.
+    
+    Tools:
+    
+    * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+    - Add TypeCastsNS.mm to the project.
+    * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm: Add.
+    (TestWebKitAPI::TEST): Add tests for <wtf/TypeCastsNS.h>.
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@282632 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2021-09-16  David Kilzer  <ddkilzer@apple.com>
+
+            WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
+            <https://webkit.org/b/230227>
+            <rdar://problem/83067708>
+
+            Reviewed by Darin Adler.
+
+            To fix the bug, implement an Objective-C class named
+            WKProcessPoolWeakObserver which contains an instance variable
+            holding a WeakPtr<WebProcessPool>, and tell CFNotificationCenter
+            to hold a weak reference to WKProcessPoolWeakObserver.
+
+            Since WKProcessPoolWeakObserver is self-contained within the
+            source file, it uses the NS_DIRECT_MEMBERS attribute since it
+            does not require the overhead of dynamic Objective-C method
+            dispatch.
+
+            * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+            (-[WKProcessPoolWeakObserver initWithWeakPtr:]): Add.
+            (-[WKProcessPoolWeakObserver pool]): Add.
+            - Implement WKProcessPoolWeakObserver class.
+            - Note that init methods can never be marked as NS_DIRECT, and
+              @property statements can't use NS_DIRECT in their declaration,
+              so the @property is declared in a category which uses
+              NS_DIRECT_MEMBERS.
+            (WebKit::extractWebProcessPool): Add.
+            - Static helper method for extracting RefPtr<WebProcessPool>
+              from type-punned WKProcessPoolWeakObserver.
+            (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+            (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+            (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+            (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+            (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+            - Clean up function parameter list.
+            - Use extractWebProcessPool() helper method to get a
+              RefPtr<WebProcessPool> from `observer`.
+            (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+            (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+            - Private helper methods to reduce duplicate code.
+            - Use m_weakObserver for CFNotificationCenter observer and
+              include _CFNotificationObserverIsObjC to fix the bug.
+            (WebKit::WebProcessPool::registerNotificationObservers):
+            - Make use of new addCFNotificationObserver() helper method.
+            - Fixes use of static_cast<CFStringRef> to make code ready for
+              ARC by using a __bridge cast or removing the static_cast when
+              CFSTR() is used.
+            (WebKit::WebProcessPool::unregisterNotificationObservers):
+            - Make use of new removeCFNotificationObserver() helper method.
+
+            * UIProcess/WebProcessPool.h:
+            - Add m_weakObserver instance variable to hold the
+              WKProcessPoolWeakObserver object.
+            (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+            (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+            - Add declarations for new helper methods.
+            (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+            (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+            (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+            (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+            (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+            - Clean up function parameter list.
+
+2021-11-04  Russell Epstein  <repstein@apple.com>
+
</ins><span class="cx">         Cherry-pick r282313. rdar://problem/82987695
</span><span class="cx"> 
</span><span class="cx">     CFNotificationCenterAddObserver() should use weak observers
</span></span></pre></div>
<a id="branchessafari612branchSourceWebKitUIProcessCocoaWebProcessPoolCocoamm"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm  2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm     2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -65,6 +65,7 @@
</span><span class="cx"> #import <WebCore/VersionChecks.h>
</span><span class="cx"> #import <objc/runtime.h>
</span><span class="cx"> #import <pal/spi/cf/CFNetworkSPI.h>
</span><ins>+#import <pal/spi/cf/CFNotificationCenterSPI.h>
</ins><span class="cx"> #import <sys/param.h>
</span><span class="cx"> #import <wtf/FileSystem.h>
</span><span class="cx"> #import <wtf/ProcessPrivilege.h>
</span><span class="lines">@@ -71,6 +72,8 @@
</span><span class="cx"> #import <wtf/SoftLinking.h>
</span><span class="cx"> #import <wtf/cocoa/Entitlements.h>
</span><span class="cx"> #import <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
</span><ins>+#import <wtf/cocoa/TypeCastsNS.h>
+#import <wtf/spi/cocoa/NSObjCRuntimeSPI.h>
</ins><span class="cx"> #import <wtf/spi/darwin/SandboxSPI.h>
</span><span class="cx"> #import <wtf/spi/darwin/dyldSPI.h>
</span><span class="cx"> #import <wtf/text/TextStream.h>
</span><span class="lines">@@ -131,6 +134,35 @@
</span><span class="cx"> 
</span><span class="cx"> #define WEBPROCESSPOOL_RELEASE_LOG(channel, fmt, ...) RELEASE_LOG(channel, "%p - WebProcessPool::" fmt, this, ##__VA_ARGS__)
</span><span class="cx"> 
</span><ins>+@interface WKProcessPoolWeakObserver : NSObject {
+    WeakPtr<WebKit::WebProcessPool> m_weakPtr;
+}
+- (instancetype)init NS_UNAVAILABLE;
+- (instancetype)initWithWeakPtr:(WeakPtr<WebKit::WebProcessPool>&&)weakPtr NS_DESIGNATED_INITIALIZER;
+@end
+
+NS_DIRECT_MEMBERS
+@interface WKProcessPoolWeakObserver (Direct)
+@property (nonatomic, readonly) RefPtr<WebKit::WebProcessPool> pool;
+@end
+
+@implementation WKProcessPoolWeakObserver
+- (instancetype)initWithWeakPtr:(WeakPtr<WebKit::WebProcessPool>&&)weakPtr
+{
+    if ((self = [super init]))
+        m_weakPtr = WTFMove(weakPtr);
+    return self;
+}
+@end
+
+NS_DIRECT_MEMBERS
+@implementation WKProcessPoolWeakObserver (Direct)
+- (RefPtr<WebKit::WebProcessPool>)pool
+{
+    return makeRefPtr(m_weakPtr.get());
+}
+@end
+
</ins><span class="cx"> namespace WebKit {
</span><span class="cx"> using namespace WebCore;
</span><span class="cx"> 
</span><span class="lines">@@ -598,29 +630,43 @@
</span><span class="cx">     return !m_userObservablePageCounter.value() && !m_processSuppressionDisabledForPageCounter.value();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static inline RefPtr<WebProcessPool> extractWebProcessPool(void* observer)
+{
+    RetainPtr strongObserver { dynamic_ns_cast<WKProcessPoolWeakObserver>(reinterpret_cast<id>(observer)) };
+    if (!strongObserver)
+        return nullptr;
+    return [strongObserver pool];
+}
+
</ins><span class="cx"> #if PLATFORM(IOS_FAMILY) && !PLATFORM(MACCATALYST)
</span><span class="cx"> float WebProcessPool::displayBrightness()
</span><span class="cx"> {
</span><span class="cx">     return BKSDisplayBrightnessGetCurrent();
</span><span class="cx"> }
</span><del>-    
-void WebProcessPool::backlightLevelDidChangeCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
</del><ins>+
+void WebProcessPool::backlightLevelDidChangeCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
</ins><span class="cx"> {
</span><del>-    auto* pool = reinterpret_cast<WebProcessPool*>(observer);
</del><ins>+    auto pool = extractWebProcessPool(observer);
+    if (!pool)
+        return;
</ins><span class="cx">     pool->sendToAllProcesses(Messages::WebProcess::BacklightLevelDidChange(BKSDisplayBrightnessGetCurrent()));
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><del>-void WebProcessPool::accessibilityPreferencesChangedCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo)
</del><ins>+void WebProcessPool::accessibilityPreferencesChangedCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
</ins><span class="cx"> {
</span><del>-    auto* pool = reinterpret_cast<WebProcessPool*>(observer);
</del><ins>+    auto pool = extractWebProcessPool(observer);
+    if (!pool)
+        return;
</ins><span class="cx">     pool->sendToAllProcesses(Messages::WebProcess::AccessibilityPreferencesDidChange(accessibilityPreferences()));
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #if HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
</span><del>-void WebProcessPool::mediaAccessibilityPreferencesChangedCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo)
</del><ins>+void WebProcessPool::mediaAccessibilityPreferencesChangedCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
</ins><span class="cx"> {
</span><del>-    auto* pool = reinterpret_cast<WebProcessPool*>(observer);
</del><ins>+    auto pool = extractWebProcessPool(observer);
+    if (!pool)
+        return;
</ins><span class="cx">     auto captionDisplayMode = WebCore::CaptionUserPreferencesMediaAF::platformCaptionDisplayMode();
</span><span class="cx">     auto preferredLanguages = WebCore::CaptionUserPreferencesMediaAF::platformPreferredLanguages();
</span><span class="cx">     pool->sendToAllProcesses(Messages::WebProcess::SetMediaAccessibilityPreferences(captionDisplayMode, preferredLanguages));
</span><span class="lines">@@ -628,17 +674,21 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(MAC)
</span><del>-void WebProcessPool::colorPreferencesDidChangeCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
</del><ins>+void WebProcessPool::colorPreferencesDidChangeCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
</ins><span class="cx"> {
</span><del>-    auto* pool = reinterpret_cast<WebProcessPool*>(observer);
</del><ins>+    auto pool = extractWebProcessPool(observer);
+    if (!pool)
+        return;
</ins><span class="cx">     pool->sendToAllProcesses(Messages::WebProcess::ColorPreferencesDidChange());
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(REMOTE_INSPECTOR) && PLATFORM(IOS_FAMILY) && !PLATFORM(MACCATALYST)
</span><del>-void WebProcessPool::remoteWebInspectorEnabledCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo)
</del><ins>+void WebProcessPool::remoteWebInspectorEnabledCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
</ins><span class="cx"> {
</span><del>-    auto* pool = reinterpret_cast<WebProcessPool*>(observer);
</del><ins>+    auto pool = extractWebProcessPool(observer);
+    if (!pool)
+        return;
</ins><span class="cx">     for (auto& process : pool->m_processes)
</span><span class="cx">         process->enableRemoteInspectorIfNeeded();
</span><span class="cx"> }
</span><span class="lines">@@ -657,6 +707,17 @@
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+void WebProcessPool::addCFNotificationObserver(CFNotificationCallback callback, CFStringRef name, CFNotificationCenterRef center)
+{
+    auto coalesceBehavior = static_cast<CFNotificationSuspensionBehavior>(CFNotificationSuspensionBehaviorCoalesce | _CFNotificationObserverIsObjC);
+    CFNotificationCenterAddObserver(center, (__bridge const void*)m_weakObserver.get(), callback, name, nullptr, coalesceBehavior);
+}
+
+void WebProcessPool::removeCFNotificationObserver(CFStringRef name, CFNotificationCenterRef center)
+{
+    CFNotificationCenterRemoveObserver(center, (__bridge const void*)m_weakObserver.get(), name, nullptr);
+}
+
</ins><span class="cx"> void WebProcessPool::registerNotificationObservers()
</span><span class="cx"> {
</span><span class="cx"> #if !PLATFORM(IOS_FAMILY)
</span><span class="lines">@@ -716,12 +777,14 @@
</span><span class="cx">         setApplicationIsActive(false);
</span><span class="cx">     }];
</span><span class="cx">     
</span><del>-    CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), this, colorPreferencesDidChangeCallback, AppleColorPreferencesChangedNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
</del><ins>+    m_weakObserver = adoptNS([[WKProcessPoolWeakObserver alloc] initWithWeakPtr:makeWeakPtr(*this)]);
+
+    addCFNotificationObserver(colorPreferencesDidChangeCallback, AppleColorPreferencesChangedNotification, CFNotificationCenterGetDistributedCenter());
</ins><span class="cx"> #elif !PLATFORM(MACCATALYST)
</span><del>-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, backlightLevelDidChangeCallback, static_cast<CFStringRef>(UIBacklightLevelChangedNotification), nullptr, CFNotificationSuspensionBehaviorCoalesce);
</del><ins>+    addCFNotificationObserver(backlightLevelDidChangeCallback, (__bridge CFStringRef)UIBacklightLevelChangedNotification);
</ins><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> #if ENABLE(REMOTE_INSPECTOR)
</span><del>-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, remoteWebInspectorEnabledCallback, static_cast<CFStringRef>(CFSTR(WIRServiceEnabledNotification)), nullptr, CFNotificationSuspensionBehaviorCoalesce);
</del><ins>+    addCFNotificationObserver(remoteWebInspectorEnabledCallback, CFSTR(WIRServiceEnabledNotification));
</ins><span class="cx"> #endif
</span><span class="cx"> #endif // PLATFORM(IOS)
</span><span class="cx"> #endif // !PLATFORM(IOS_FAMILY)
</span><span class="lines">@@ -757,14 +820,14 @@
</span><span class="cx">     });
</span><span class="cx"> 
</span><span class="cx"> #if HAVE(PER_APP_ACCESSIBILITY_PREFERENCES)
</span><del>-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, accessibilityPreferencesChangedCallback, kAXSReduceMotionChangedNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, accessibilityPreferencesChangedCallback, kAXSIncreaseButtonLegibilityNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, accessibilityPreferencesChangedCallback, kAXSEnhanceTextLegibilityChangedNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, accessibilityPreferencesChangedCallback, kAXSDarkenSystemColorsEnabledNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, accessibilityPreferencesChangedCallback, kAXSInvertColorsEnabledNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
</del><ins>+    addCFNotificationObserver(accessibilityPreferencesChangedCallback, kAXSReduceMotionChangedNotification);
+    addCFNotificationObserver(accessibilityPreferencesChangedCallback, kAXSIncreaseButtonLegibilityNotification);
+    addCFNotificationObserver(accessibilityPreferencesChangedCallback, kAXSEnhanceTextLegibilityChangedNotification);
+    addCFNotificationObserver(accessibilityPreferencesChangedCallback, kAXSDarkenSystemColorsEnabledNotification);
+    addCFNotificationObserver(accessibilityPreferencesChangedCallback, kAXSInvertColorsEnabledNotification);
</ins><span class="cx"> #endif
</span><span class="cx"> #if HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
</span><del>-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, mediaAccessibilityPreferencesChangedCallback, kMAXCaptionAppearanceSettingsChangedNotification, nullptr, CFNotificationSuspensionBehaviorCoalesce);
</del><ins>+    addCFNotificationObserver(mediaAccessibilityPreferencesChangedCallback, kMAXCaptionAppearanceSettingsChangedNotification);
</ins><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -781,12 +844,12 @@
</span><span class="cx">     [[NSWorkspace.sharedWorkspace notificationCenter] removeObserver:m_accessibilityDisplayOptionsNotificationObserver.get()];
</span><span class="cx">     [[NSNotificationCenter defaultCenter] removeObserver:m_scrollerStyleNotificationObserver.get()];
</span><span class="cx">     [[NSNotificationCenter defaultCenter] removeObserver:m_deactivationObserver.get()];
</span><del>-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDistributedCenter(), this, AppleColorPreferencesChangedNotification, nullptr);
</del><ins>+    removeCFNotificationObserver(AppleColorPreferencesChangedNotification, CFNotificationCenterGetDistributedCenter());
</ins><span class="cx"> #elif !PLATFORM(MACCATALYST)
</span><del>-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, static_cast<CFStringRef>(UIBacklightLevelChangedNotification) , nullptr);
</del><ins>+    removeCFNotificationObserver((__bridge CFStringRef)UIBacklightLevelChangedNotification);
</ins><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> #if ENABLE(REMOTE_INSPECTOR)
</span><del>-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, CFSTR(WIRServiceEnabledNotification), nullptr);
</del><ins>+    removeCFNotificationObserver(CFSTR(WIRServiceEnabledNotification));
</ins><span class="cx"> #endif
</span><span class="cx"> #endif // PLATFORM(IOS)
</span><span class="cx"> #endif // !PLATFORM(IOS_FAMILY)
</span><span class="lines">@@ -801,15 +864,17 @@
</span><span class="cx">     m_powerSourceNotifier = nullptr;
</span><span class="cx"> 
</span><span class="cx"> #if HAVE(PER_APP_ACCESSIBILITY_PREFERENCES)
</span><del>-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, kAXSReduceMotionChangedNotification, nullptr);
-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, kAXSIncreaseButtonLegibilityNotification, nullptr);
-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, kAXSEnhanceTextLegibilityChangedNotification, nullptr);
-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, kAXSDarkenSystemColorsEnabledNotification, nullptr);
-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, kAXSInvertColorsEnabledNotification, nullptr);
</del><ins>+    removeCFNotificationObserver(kAXSReduceMotionChangedNotification);
+    removeCFNotificationObserver(kAXSIncreaseButtonLegibilityNotification);
+    removeCFNotificationObserver(kAXSEnhanceTextLegibilityChangedNotification);
+    removeCFNotificationObserver(kAXSDarkenSystemColorsEnabledNotification);
+    removeCFNotificationObserver(kAXSInvertColorsEnabledNotification);
</ins><span class="cx"> #endif
</span><span class="cx"> #if HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
</span><del>-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, kMAXCaptionAppearanceSettingsChangedNotification, nullptr);
</del><ins>+    removeCFNotificationObserver(kMAXCaptionAppearanceSettingsChangedNotification);
</ins><span class="cx"> #endif
</span><ins>+
+    m_weakObserver = nil;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool WebProcessPool::isURLKnownHSTSHost(const String& urlString) const
</span></span></pre></div>
<a id="branchessafari612branchSourceWebKitUIProcessWebProcessPoolh"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Source/WebKit/UIProcess/WebProcessPool.h (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Source/WebKit/UIProcess/WebProcessPool.h      2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Source/WebKit/UIProcess/WebProcessPool.h 2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -64,6 +64,7 @@
</span><span class="cx"> OBJC_CLASS NSSet;
</span><span class="cx"> OBJC_CLASS NSString;
</span><span class="cx"> OBJC_CLASS WKPreferenceObserver;
</span><ins>+OBJC_CLASS WKProcessPoolWeakObserver;
</ins><span class="cx"> #if PLATFORM(MAC)
</span><span class="cx"> OBJC_CLASS WKWebInspectorPreferenceObserver;
</span><span class="cx"> #endif
</span><span class="lines">@@ -549,6 +550,9 @@
</span><span class="cx">     bool usesSingleWebProcess() const { return m_configuration->usesSingleWebProcess(); }
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(COCOA)
</span><ins>+    void addCFNotificationObserver(CFNotificationCallback, CFStringRef name, CFNotificationCenterRef = CFNotificationCenterGetDarwinNotifyCenter());
+    void removeCFNotificationObserver(CFStringRef name, CFNotificationCenterRef = CFNotificationCenterGetDarwinNotifyCenter());
+
</ins><span class="cx">     void registerNotificationObservers();
</span><span class="cx">     void unregisterNotificationObservers();
</span><span class="cx"> #endif
</span><span class="lines">@@ -567,22 +571,22 @@
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS_FAMILY) && !PLATFORM(MACCATALYST)
</span><span class="cx">     static float displayBrightness();
</span><del>-    static void backlightLevelDidChangeCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo);    
</del><ins>+    static void backlightLevelDidChangeCallback(CFNotificationCenterRef, void* observer, CFStringRef name, const void* postingObject, CFDictionaryRef userInfo);
</ins><span class="cx"> #if ENABLE(REMOTE_INSPECTOR)
</span><del>-    static void remoteWebInspectorEnabledCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo);
</del><ins>+    static void remoteWebInspectorEnabledCallback(CFNotificationCenterRef, void* observer, CFStringRef name, const void* postingObject, CFDictionaryRef userInfo);
</ins><span class="cx"> #endif
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(COCOA)
</span><del>-    static void accessibilityPreferencesChangedCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo);
</del><ins>+    static void accessibilityPreferencesChangedCallback(CFNotificationCenterRef, void* observer, CFStringRef name, const void* postingObject, CFDictionaryRef userInfo);
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
</span><del>-    static void mediaAccessibilityPreferencesChangedCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo);
</del><ins>+    static void mediaAccessibilityPreferencesChangedCallback(CFNotificationCenterRef, void* observer, CFStringRef name, const void* postingObject, CFDictionaryRef userInfo);
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(MAC)
</span><del>-    static void colorPreferencesDidChangeCallback(CFNotificationCenterRef, void *observer, CFStringRef name, const void *, CFDictionaryRef userInfo);
</del><ins>+    static void colorPreferencesDidChangeCallback(CFNotificationCenterRef, void* observer, CFStringRef name, const void* postingObject, CFDictionaryRef userInfo);
</ins><span class="cx"> #endif
</span><span class="cx">     
</span><span class="cx"> #if ENABLE(CFPREFS_DIRECT_MODE)
</span><span class="lines">@@ -689,6 +693,8 @@
</span><span class="cx">     RetainPtr<NSObject> m_activationObserver;
</span><span class="cx">     RetainPtr<NSObject> m_accessibilityEnabledObserver;
</span><span class="cx">     RetainPtr<NSObject> m_applicationLaunchObserver;
</span><ins>+
+    RetainPtr<WKProcessPoolWeakObserver> m_weakObserver;
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     bool m_processTerminationEnabled { true };
</span></span></pre></div>
<a id="branchessafari612branchToolsChangeLog"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Tools/ChangeLog (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Tools/ChangeLog       2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Tools/ChangeLog  2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -1,3 +1,121 @@
</span><ins>+2021-11-04  Russell Epstein  <repstein@apple.com>
+
+        Cherry-pick r282632. rdar://problem/83067708
+
+    WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
+    <https://webkit.org/b/230227>
+    <rdar://problem/83067708>
+    
+    Reviewed by Darin Adler.
+    
+    Source/WebKit:
+    
+    To fix the bug, implement an Objective-C class named
+    WKProcessPoolWeakObserver which contains an instance variable
+    holding a WeakPtr<WebProcessPool>, and tell CFNotificationCenter
+    to hold a weak reference to WKProcessPoolWeakObserver.
+    
+    Since WKProcessPoolWeakObserver is self-contained within the
+    source file, it uses the NS_DIRECT_MEMBERS attribute since it
+    does not require the overhead of dynamic Objective-C method
+    dispatch.
+    
+    * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+    (-[WKProcessPoolWeakObserver initWithWeakPtr:]): Add.
+    (-[WKProcessPoolWeakObserver pool]): Add.
+    - Implement WKProcessPoolWeakObserver class.
+    - Note that init methods can never be marked as NS_DIRECT, and
+      @property statements can't use NS_DIRECT in their declaration,
+      so the @property is declared in a category which uses
+      NS_DIRECT_MEMBERS.
+    (WebKit::extractWebProcessPool): Add.
+    - Static helper method for extracting RefPtr<WebProcessPool>
+      from type-punned WKProcessPoolWeakObserver.
+    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+    - Clean up function parameter list.
+    - Use extractWebProcessPool() helper method to get a
+      RefPtr<WebProcessPool> from `observer`.
+    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+    - Private helper methods to reduce duplicate code.
+    - Use m_weakObserver for CFNotificationCenter observer and
+      include _CFNotificationObserverIsObjC to fix the bug.
+    (WebKit::WebProcessPool::registerNotificationObservers):
+    - Make use of new addCFNotificationObserver() helper method.
+    - Fixes use of static_cast<CFStringRef> to make code ready for
+      ARC by using a __bridge cast or removing the static_cast when
+      CFSTR() is used.
+    (WebKit::WebProcessPool::unregisterNotificationObservers):
+    - Make use of new removeCFNotificationObserver() helper method.
+    
+    * UIProcess/WebProcessPool.h:
+    - Add m_weakObserver instance variable to hold the
+      WKProcessPoolWeakObserver object.
+    (WebKit::WebProcessPool::addCFNotificationObserver): Add.
+    (WebKit::WebProcessPool::removeCFNotificationObserver): Add.
+    - Add declarations for new helper methods.
+    (WebKit::WebProcessPool::backlightLevelDidChangeCallback):
+    (WebKit::WebProcessPool::accessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::mediaAccessibilityPreferencesChangedCallback):
+    (WebKit::WebProcessPool::colorPreferencesDidChangeCallback):
+    (WebKit::WebProcessPool::remoteWebInspectorEnabledCallback):
+    - Clean up function parameter list.
+    
+    Source/WTF:
+    
+    Tests (API):
+        TestWTF.TypeCastsNS.checked_ns_cast
+        TestWTF.TypeCastsNS.dynamic_ns_cast
+        TestWTF.TypeCastsNS.dynamic_ns_cast_RetainPtr
+    
+    * WTF.xcodeproj/project.pbxproj:
+    * wtf/PlatformMac.cmake:
+    - Add new header files to the project.
+    
+    * wtf/cocoa/TypeCastsNS.h: Add.
+    (WTF::checked_ns_cast):
+    (WTF::dynamic_ns_cast):
+    - Add casts for NS objects similar to TypeCastsCF.h.
+    
+    * wtf/PlatformHave.h:
+    (HAVE_NS_DIRECT_SUPPORT): Add.
+    - Note that clang for macOS 11 Big Sur claims to know about the
+      attributes, but will fail to compile if they are actually
+      used.
+    
+    * wtf/spi/cocoa/NSObjCRuntimeSPI.h: Add.
+    (NS_DIRECT):
+    (NS_DIRECT_MEMBERS):
+    - Define compiler attributes for direct dispatch of Objective-C
+      methods.
+    
+    Tools:
+    
+    * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+    - Add TypeCastsNS.mm to the project.
+    * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm: Add.
+    (TestWebKitAPI::TEST): Add tests for <wtf/TypeCastsNS.h>.
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@282632 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2021-09-16  David Kilzer  <ddkilzer@apple.com>
+
+            WebKit::WebProcessPool should use a weak observer with CFNotificationCenter
+            <https://webkit.org/b/230227>
+            <rdar://problem/83067708>
+
+            Reviewed by Darin Adler.
+
+            * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+            - Add TypeCastsNS.mm to the project.
+            * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm: Add.
+            (TestWebKitAPI::TEST): Add tests for <wtf/TypeCastsNS.h>.
+
</ins><span class="cx"> 2021-10-26  Russell Epstein  <repstein@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Cherry-pick r284669. rdar://problem/83971417
</span></span></pre></div>
<a id="branchessafari612branchToolsTestWebKitAPITestWebKitAPIxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: branches/safari-612-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (285273 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj   2021-11-04 19:38:42 UTC (rev 285273)
+++ branches/safari-612-branch/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj      2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -266,6 +266,7 @@
</span><span class="cx">          448D7E471EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */; };
</span><span class="cx">          44AC8BC621D0245A00CAFB34 /* RetainPtr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BC029B161486AD6400817DA9 /* RetainPtr.cpp */; };
</span><span class="cx">          44C2FBE225E7592C00ABC72F /* WKAppHighlights.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44C2FBE125E7592C00ABC72F /* WKAppHighlights.mm */; };
</span><ins>+               44CDE4D426EE6E4A009F6ACB /* TypeCastsNS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44CDE4D326EE6E41009F6ACB /* TypeCastsNS.mm */; };
</ins><span class="cx">           44CF31FD249941E8009CB6CB /* ContextMenuAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 44CF31FB24993F66009CB6CB /* ContextMenuAction.cpp */; };
</span><span class="cx">          4612C2B9210A6ACE00B788A6 /* LoadFileThenReload.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4612C2B8210A6ABF00B788A6 /* LoadFileThenReload.mm */; };
</span><span class="cx">          4628C8E92367ABD100B073F0 /* WKSecurityOrigin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4628C8E82367ABBC00B073F0 /* WKSecurityOrigin.cpp */; };
</span><span class="lines">@@ -2096,6 +2097,7 @@
</span><span class="cx">          448D7E451EA6C55500ECC756 /* EnvironmentUtilitiesTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EnvironmentUtilitiesTest.cpp; sourceTree = "<group>"; };
</span><span class="cx">          44A622C114A0E2B60048515B /* WTFStringUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFStringUtilities.h; sourceTree = "<group>"; };
</span><span class="cx">          44C2FBE125E7592C00ABC72F /* WKAppHighlights.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKAppHighlights.mm; sourceTree = "<group>"; };
</span><ins>+               44CDE4D326EE6E41009F6ACB /* TypeCastsNS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypeCastsNS.mm; sourceTree = "<group>"; };
</ins><span class="cx">           44CF31FB24993F66009CB6CB /* ContextMenuAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContextMenuAction.cpp; sourceTree = "<group>"; };
</span><span class="cx">          4612C2B8210A6ABF00B788A6 /* LoadFileThenReload.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LoadFileThenReload.mm; sourceTree = "<group>"; };
</span><span class="cx">          4628C8E82367ABBC00B073F0 /* WKSecurityOrigin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKSecurityOrigin.cpp; sourceTree = "<group>"; };
</span><span class="lines">@@ -4987,6 +4989,7 @@
</span><span class="cx">                          BC3FEB63267FCF740054006A /* SpanCocoa.mm */,
</span><span class="cx">                          1C4616A626BB172F00F8C9F6 /* TextStreamCocoa.cpp */,
</span><span class="cx">                          1C46169E26BA510700F8C9F6 /* TextStreamCocoa.mm */,
</span><ins>+                               44CDE4D326EE6E41009F6ACB /* TypeCastsNS.mm */,
</ins><span class="cx">                           E3C21A7B21B25CA2003B31A3 /* URLExtras.mm */,
</span><span class="cx">                  );
</span><span class="cx">                  path = cocoa;
</span><span class="lines">@@ -5334,6 +5337,7 @@
</span><span class="cx">                          E38A0D351FD50CC300E98C8B /* Threading.cpp in Sources */,
</span><span class="cx">                          5311BD5E1EA9490E00525281 /* ThreadMessages.cpp in Sources */,
</span><span class="cx">                          0F2C20B81DCD545000542D9E /* Time.cpp in Sources */,
</span><ins>+                               44CDE4D426EE6E4A009F6ACB /* TypeCastsNS.mm in Sources */,
</ins><span class="cx">                           E324A6F02041C82000A76593 /* UniqueArray.cpp in Sources */,
</span><span class="cx">                          E3A1E78221B25B7A008C6007 /* URL.cpp in Sources */,
</span><span class="cx">                          E3C21A7C21B25CA2003B31A3 /* URLExtras.mm in Sources */,
</span></span></pre></div>
<a id="branchessafari612branchToolsTestWebKitAPITestsWTFcocoaTypeCastsNSmm"></a>
<div class="addfile"><h4>Added: branches/safari-612-branch/Tools/TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm (0 => 285274)</h4>
<pre class="diff"><span>
<span class="info">--- branches/safari-612-branch/Tools/TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm                            (rev 0)
+++ branches/safari-612-branch/Tools/TestWebKitAPI/Tests/WTF/cocoa/TypeCastsNS.mm       2021-11-04 19:38:47 UTC (rev 285274)
</span><span class="lines">@@ -0,0 +1,196 @@
</span><ins>+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import <wtf/cocoa/TypeCastsNS.h>
+
+#import <wtf/StdLibExtras.h>
+
+@interface MyObjectSubtype : NSObject
+@end
+
+@implementation MyObjectSubtype
+@end
+
+namespace TestWebKitAPI {
+
+using namespace WTF;
+
+TEST(TypeCastsNS, checked_ns_cast)
+{
+    EXPECT_EQ(nil, checked_ns_cast<NSString>(nil));
+
+    {
+        auto object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        EXPECT_EQ(object.get(), checked_ns_cast<NSString>(object.get()));
+        EXPECT_EQ(object.get(), checked_ns_cast<NSObject>(object.get()));
+    }
+
+    {
+        auto object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        EXPECT_EQ(object.get(), checked_ns_cast<NSString>(object.get()));
+    }
+
+    {
+        auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
+        EXPECT_EQ(object.get(), checked_ns_cast<NSObject>(object.get()));
+    }
+}
+
+TEST(TypeCastsNS, dynamic_ns_cast)
+{
+    EXPECT_EQ(nil, dynamic_ns_cast<NSString>(nil));
+
+    {
+        auto object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        EXPECT_EQ(object.get(), dynamic_ns_cast<NSString>(object.get()));
+        EXPECT_EQ(object.get(), dynamic_ns_cast<NSObject>(object.get()));
+        EXPECT_EQ(nil, dynamic_ns_cast<NSArray>(object.get()));
+    }
+
+    {
+        auto object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        EXPECT_EQ(object.get(), dynamic_ns_cast<NSString>(object.get()));
+        EXPECT_EQ(nil, dynamic_ns_cast<NSArray>(object.get()));
+    }
+
+    {
+        auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
+        EXPECT_EQ(object.get(), dynamic_ns_cast<NSObject>(object.get()));
+        EXPECT_EQ(nil, dynamic_ns_cast<NSArray>(object.get()));
+    }
+
+    {
+        auto object = adoptNS(reinterpret_cast<id>([[NSObject alloc] init]));
+        EXPECT_EQ(object.get(), dynamic_ns_cast<NSObject>(object.get()));
+        EXPECT_EQ(nil, dynamic_ns_cast<MyObjectSubtype>(object.get()));
+    }
+
+    {
+        auto object = adoptNS([[NSObject alloc] init]);
+        EXPECT_EQ(nil, dynamic_ns_cast<MyObjectSubtype>(object.get()));
+    }
+}
+
+TEST(TypeCastsNS, dynamic_ns_cast_RetainPtr)
+{
+    {
+        RetainPtr<NSString> object;
+        auto objectCast = dynamic_ns_cast<NSString>(WTFMove(object));
+        EXPECT_EQ(nil, object.get());
+        EXPECT_EQ(nil, objectCast.get());
+    }
+
+    {
+        auto object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        id objectPtr = object.get();
+        auto objectCast = dynamic_ns_cast<NSString>(WTFMove(object));
+        EXPECT_EQ(nil, object.get());
+        EXPECT_EQ(objectPtr, objectCast.get());
+        objectPtr = nil; // For ARC.
+        EXPECT_EQ(1U, [objectCast retainCount]);
+
+        object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        objectPtr = object.get();
+        auto objectCast2 = dynamic_ns_cast<NSObject>(WTFMove(object));
+        EXPECT_EQ(nil, object.get());
+        EXPECT_EQ(objectPtr, objectCast2.get());
+        objectPtr = nil; // For ARC.
+        EXPECT_EQ(1U, [objectCast2 retainCount]);
+
+        object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        objectPtr = object.get();
+        auto objectCast3 = dynamic_ns_cast<NSArray>(WTFMove(object));
+        EXPECT_EQ(objectPtr, object.get());
+        EXPECT_EQ(nil, objectCast3.get());
+        objectPtr = nil; // For ARC.
+        EXPECT_EQ(1U, [object retainCount]);
+    }
+
+    {
+        auto object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        id objectPtr = object.get();
+        auto objectCast = dynamic_ns_cast<NSString>(WTFMove(object));
+        EXPECT_EQ(nil, object.get());
+        EXPECT_EQ(objectPtr, objectCast.get());
+        objectPtr = nil;
+        EXPECT_EQ(1U, [objectCast retainCount]);
+
+        object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
+        objectPtr = object.get();
+        auto objectCast2 = dynamic_ns_cast<NSArray>(WTFMove(object));
+        EXPECT_EQ(objectPtr, object.get());
+        EXPECT_EQ(nil, objectCast2.get());
+        objectPtr = nil;
+        EXPECT_EQ(1U, [object retainCount]);
+    }
+
+    {
+        auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
+        id objectPtr = object.get();
+        auto objectCast = dynamic_ns_cast<NSObject>(WTFMove(object));
+        EXPECT_EQ(nil, object.get());
+        EXPECT_EQ(objectPtr, objectCast.get());
+        objectPtr = nil;
+        EXPECT_EQ(1U, [objectCast retainCount]);
+
+        object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
+        objectPtr = object.get();
+        auto objectCast2 = dynamic_ns_cast<NSArray>(WTFMove(object));
+        EXPECT_EQ(objectPtr, object.get());
+        EXPECT_EQ(nil, objectCast2.get());
+        objectPtr = nil;
+        EXPECT_EQ(1U, [object retainCount]);
+    }
+
+    {
+        auto object = adoptNS(reinterpret_cast<id>([[NSObject alloc] init]));
+        id objectPtr = object.get();
+        auto objectCast = dynamic_ns_cast<NSObject>(WTFMove(object));
+        EXPECT_EQ(nil, object.get());
+        EXPECT_EQ(objectPtr, objectCast.get());
+        objectPtr = nil; // For ARC.
+        EXPECT_EQ(1U, [objectCast retainCount]);
+
+        object = adoptNS(reinterpret_cast<id>([[NSObject alloc] init]));
+        objectPtr = object.get();
+        auto objectCast2 = dynamic_ns_cast<MyObjectSubtype>(WTFMove(object));
+        EXPECT_EQ(objectPtr, object.get());
+        EXPECT_EQ(nil, objectCast2.get());
+        objectPtr = nil; // For ARC.
+        EXPECT_EQ(1U, [object retainCount]);
+    }
+
+    {
+        auto object = adoptNS([[NSObject alloc] init]);
+        id objectPtr = object.get();
+        auto objectCast = dynamic_ns_cast<MyObjectSubtype>(WTFMove(object));
+        EXPECT_EQ(objectPtr, object.get());
+        EXPECT_EQ(nil, objectCast.get());
+        objectPtr = nil; // For ARC.
+        EXPECT_EQ(1U, [object retainCount]);
+    }
+}
+
+} // namespace TestWebKitAPI
</ins></span></pre>
</div>
</div>

</body>
</html>