Title: [282313] trunk/Source
Revision
282313
Author
[email protected]
Date
2021-09-11 12:24:11 -0700 (Sat, 11 Sep 2021)

Log Message

CFNotificationCenterAddObserver() should use weak observers
<https://webkit.org/b/230168>
<rdar://problem/82987695>

Reviewed by Darin Adler.

Source/WebCore/PAL:

* PAL.xcodeproj/project.pbxproj:
- Add CFNotificationCenterSPI.h to the project.
* pal/spi/cf/CFNotificationCenterSPI.h: Add.

Source/WebKit:

* UIProcess/API/Cocoa/WKWebView.mm:
(hardwareKeyboardAvailabilityChangedCallback):
- Use RetainPtr to store `observer` for the duration of the
  function, and add an early return if it is nil.
(-[WKWebView _initializeWithConfiguration:]):
- Use _CFNotificationObserverIsObjC to fix the bug.
- Extract local variables and change notificationName cast to be
  ARC-ready.  Remove notificationName from autoreleasePool.
(-[WKWebView dealloc]):
- Extract local variable and change notificationName cast to be
  ARC-ready.  Remove notificationName from autoreleasePool.

Source/WebKitLegacy/mac:

* WebView/WebHTMLView.mm:
(hardwareKeyboardAvailabilityChangedCallback):
- Modernize syntax for WeakObjCPtr constructor.
(-[WebHTMLView initWithFrame:]):
- Use _CFNotificationObserverIsObjC to fix the bug.
- Extract local variables and change notificationName cast to be
  ARC-ready.  Remove notificationName from autoreleasePool.
(-[WebHTMLView dealloc]):
- Extract local variable and change notificationName cast to be
  ARC-ready.  Remove notificationName from autoreleasePool.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/PAL/ChangeLog (282312 => 282313)


--- trunk/Source/WebCore/PAL/ChangeLog	2021-09-11 17:43:56 UTC (rev 282312)
+++ trunk/Source/WebCore/PAL/ChangeLog	2021-09-11 19:24:11 UTC (rev 282313)
@@ -1,3 +1,15 @@
+2021-09-11  David Kilzer  <[email protected]>
+
+        CFNotificationCenterAddObserver() should use weak observers
+        <https://webkit.org/b/230168>
+        <rdar://problem/82987695>
+
+        Reviewed by Darin Adler.
+
+        * PAL.xcodeproj/project.pbxproj:
+        - Add CFNotificationCenterSPI.h to the project.
+        * pal/spi/cf/CFNotificationCenterSPI.h: Add.
+
 2021-09-08  Youenn Fablet  <[email protected]>
 
         Add video metrics logging to LocalSampleBufferDisplayLayer

Modified: trunk/Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj (282312 => 282313)


--- trunk/Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj	2021-09-11 17:43:56 UTC (rev 282312)
+++ trunk/Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj	2021-09-11 19:24:11 UTC (rev 282313)
@@ -133,6 +133,7 @@
 		41B99E4625DD70160007829A /* AVStreamDataParserSPI.h in Headers */ = {isa = PBXBuildFile; fileRef = 41B99E4525DD70150007829A /* AVStreamDataParserSPI.h */; };
 		41E1F344248A6A000022D5DE /* VideoToolboxSoftLink.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 416E995523DAEFF700E871CB /* VideoToolboxSoftLink.cpp */; };
 		442956CD218A72DF0080DB54 /* RevealSPI.h in Headers */ = {isa = PBXBuildFile; fileRef = 442956CC218A72DE0080DB54 /* RevealSPI.h */; };
+		445097BC26EBF66D003EF771 /* CFNotificationCenterSPI.h in Headers */ = {isa = PBXBuildFile; fileRef = 445097BB26EBF66D003EF771 /* CFNotificationCenterSPI.h */; };
 		4450FC9F21F5F602004DFA56 /* QuickLookSoftLink.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4450FC9D21F5F602004DFA56 /* QuickLookSoftLink.mm */; };
 		4450FCA021F5F602004DFA56 /* QuickLookSoftLink.h in Headers */ = {isa = PBXBuildFile; fileRef = 4450FC9E21F5F602004DFA56 /* QuickLookSoftLink.h */; };
 		44E1A8AF21FA54E600C3048E /* LookupSoftLink.h in Headers */ = {isa = PBXBuildFile; fileRef = 44E1A8AD21FA54DA00C3048E /* LookupSoftLink.h */; };
@@ -354,6 +355,7 @@
 		416E995623DAEFF700E871CB /* VideoToolboxSoftLink.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VideoToolboxSoftLink.h; sourceTree = "<group>"; };
 		41B99E4525DD70150007829A /* AVStreamDataParserSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AVStreamDataParserSPI.h; sourceTree = "<group>"; };
 		442956CC218A72DE0080DB54 /* RevealSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RevealSPI.h; sourceTree = "<group>"; };
+		445097BB26EBF66D003EF771 /* CFNotificationCenterSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFNotificationCenterSPI.h; sourceTree = "<group>"; };
 		4450FC9D21F5F602004DFA56 /* QuickLookSoftLink.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = QuickLookSoftLink.mm; sourceTree = "<group>"; };
 		4450FC9E21F5F602004DFA56 /* QuickLookSoftLink.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QuickLookSoftLink.h; sourceTree = "<group>"; };
 		44E1A8AD21FA54DA00C3048E /* LookupSoftLink.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LookupSoftLink.h; sourceTree = "<group>"; };
@@ -491,6 +493,7 @@
 				0C2DA0671F33CA8400DBC317 /* CFLocaleSPI.h */,
 				0C2DA0681F33CA8400DBC317 /* CFNetworkConnectionCacheSPI.h */,
 				0C2DA0691F33CA8400DBC317 /* CFNetworkSPI.h */,
+				445097BB26EBF66D003EF771 /* CFNotificationCenterSPI.h */,
 				0C2DA06A1F33CA8400DBC317 /* CFUtilitiesSPI.h */,
 				0C2DA06B1F33CA8400DBC317 /* CoreAudioSPI.h */,
 				0C2DA06C1F33CA8400DBC317 /* CoreMediaSPI.h */,
@@ -871,6 +874,7 @@
 				0C2DA06D1F33CA8400DBC317 /* CFLocaleSPI.h in Headers */,
 				0C2DA06E1F33CA8400DBC317 /* CFNetworkConnectionCacheSPI.h in Headers */,
 				0C2DA06F1F33CA8400DBC317 /* CFNetworkSPI.h in Headers */,
+				445097BC26EBF66D003EF771 /* CFNotificationCenterSPI.h in Headers */,
 				0C2DA13F1F3BEB4900DBC317 /* CFNSURLConnectionSPI.h in Headers */,
 				0C2DA0701F33CA8400DBC317 /* CFUtilitiesSPI.h in Headers */,
 				0C5FFF0C1F78D990009EFF1A /* Clock.h in Headers */,

Added: trunk/Source/WebCore/PAL/pal/spi/cf/CFNotificationCenterSPI.h (0 => 282313)


--- trunk/Source/WebCore/PAL/pal/spi/cf/CFNotificationCenterSPI.h	                        (rev 0)
+++ trunk/Source/WebCore/PAL/pal/spi/cf/CFNotificationCenterSPI.h	2021-09-11 19:24:11 UTC (rev 282313)
@@ -0,0 +1,32 @@
+/*
+ * 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 <CoreFoundation/CFNotificationCenterPriv.h>
+#else
+static const CFIndex _CFNotificationObserverIsObjC = 1 << 10;
+#endif

Modified: trunk/Source/WebKit/ChangeLog (282312 => 282313)


--- trunk/Source/WebKit/ChangeLog	2021-09-11 17:43:56 UTC (rev 282312)
+++ trunk/Source/WebKit/ChangeLog	2021-09-11 19:24:11 UTC (rev 282313)
@@ -1,3 +1,23 @@
+2021-09-11  David Kilzer  <[email protected]>
+
+        CFNotificationCenterAddObserver() should use weak observers
+        <https://webkit.org/b/230168>
+        <rdar://problem/82987695>
+
+        Reviewed by Darin Adler.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (hardwareKeyboardAvailabilityChangedCallback):
+        - Use RetainPtr to store `observer` for the duration of the
+          function, and add an early return if it is nil.
+        (-[WKWebView _initializeWithConfiguration:]):
+        - Use _CFNotificationObserverIsObjC to fix the bug.
+        - Extract local variables and change notificationName cast to be
+          ARC-ready.  Remove notificationName from autoreleasePool.
+        (-[WKWebView dealloc]):
+        - Extract local variable and change notificationName cast to be
+          ARC-ready.  Remove notificationName from autoreleasePool.
+
 2021-09-11  Philippe Normand  <[email protected]>
 
         [GLIB] MediaSession is not enabled

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm (282312 => 282313)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2021-09-11 17:43:56 UTC (rev 282312)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2021-09-11 19:24:11 UTC (rev 282313)
@@ -170,6 +170,7 @@
 #import "WKWebViewContentProviderRegistry.h"
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <UIKit/UIApplication.h>
+#import <pal/spi/cf/CFNotificationCenterSPI.h>
 #import <pal/spi/cocoa/QuartzCoreSPI.h>
 #import <pal/spi/ios/GraphicsServicesSPI.h>
 #import <wtf/cocoa/Entitlements.h>
@@ -324,9 +325,11 @@
 static void hardwareKeyboardAvailabilityChangedCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
 {
     ASSERT(observer);
-    WKWebView *webView = (__bridge WKWebView *)observer;
+    RetainPtr webView { (__bridge WKWebView *)observer };
+    if (!webView)
+        return;
     [webView->_contentView _hardwareKeyboardAvailabilityChanged];
-    webView._page->hardwareKeyboardAvailabilityChanged(GSEventIsHardwareKeyboardAttached());
+    webView->_page->hardwareKeyboardAvailabilityChanged(GSEventIsHardwareKeyboardAttached());
 }
 #endif
 
@@ -394,7 +397,9 @@
 
     _page->contentSizeCategoryDidChange([self _contentSizeCategory]);
 
-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), hardwareKeyboardAvailabilityChangedCallback, (CFStringRef)[NSString stringWithUTF8String:kGSEventHardwareKeyboardAvailabilityChangedNotification], nullptr, CFNotificationSuspensionBehaviorCoalesce);
+    auto notificationName = adoptNS([[NSString alloc] initWithCString:kGSEventHardwareKeyboardAvailabilityChangedNotification encoding:NSUTF8StringEncoding]);
+    auto notificationBehavior = static_cast<CFNotificationSuspensionBehavior>(CFNotificationSuspensionBehaviorCoalesce | _CFNotificationObserverIsObjC);
+    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), hardwareKeyboardAvailabilityChangedCallback, (__bridge CFStringRef)notificationName.get(), nullptr, notificationBehavior);
 #endif // PLATFORM(IOS_FAMILY)
 
 #if ENABLE(META_VIEWPORT)
@@ -639,7 +644,8 @@
     [[NSNotificationCenter defaultCenter] removeObserver:self];
     [_scrollView setInternalDelegate:nil];
 
-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), (CFStringRef)[NSString stringWithUTF8String:kGSEventHardwareKeyboardAvailabilityChangedNotification], nullptr);
+    auto notificationName = adoptNS([[NSString alloc] initWithCString:kGSEventHardwareKeyboardAvailabilityChangedNotification encoding:NSUTF8StringEncoding]);
+    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), (__bridge CFStringRef)notificationName.get(), nullptr);
 #endif
 
     [super dealloc];

Modified: trunk/Source/WebKitLegacy/mac/ChangeLog (282312 => 282313)


--- trunk/Source/WebKitLegacy/mac/ChangeLog	2021-09-11 17:43:56 UTC (rev 282312)
+++ trunk/Source/WebKitLegacy/mac/ChangeLog	2021-09-11 19:24:11 UTC (rev 282313)
@@ -1,3 +1,22 @@
+2021-09-11  David Kilzer  <[email protected]>
+
+        CFNotificationCenterAddObserver() should use weak observers
+        <https://webkit.org/b/230168>
+        <rdar://problem/82987695>
+
+        Reviewed by Darin Adler.
+
+        * WebView/WebHTMLView.mm:
+        (hardwareKeyboardAvailabilityChangedCallback):
+        - Modernize syntax for WeakObjCPtr constructor.
+        (-[WebHTMLView initWithFrame:]):
+        - Use _CFNotificationObserverIsObjC to fix the bug.
+        - Extract local variables and change notificationName cast to be
+          ARC-ready.  Remove notificationName from autoreleasePool.
+        (-[WebHTMLView dealloc]):
+        - Extract local variable and change notificationName cast to be
+          ARC-ready.  Remove notificationName from autoreleasePool.
+
 2021-09-08  Jean-Yves Avenard  <[email protected]>
 
         Safari TP 131 audio canPlayType() reports false negatives

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm (282312 => 282313)


--- trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2021-09-11 17:43:56 UTC (rev 282312)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2021-09-11 19:24:11 UTC (rev 282313)
@@ -174,6 +174,7 @@
 #import <WebCore/WKGraphics.h>
 #import <WebCore/WebCoreThreadRun.h>
 #import <WebCore/WebEvent.h>
+#import <pal/spi/cf/CFNotificationCenterSPI.h>
 #import <pal/spi/ios/GraphicsServicesSPI.h>
 #endif
 
@@ -832,7 +833,7 @@
 static void hardwareKeyboardAvailabilityChangedCallback(CFNotificationCenterRef, void* observer, CFStringRef, const void*, CFDictionaryRef)
 {
     ASSERT(observer);
-    auto weakWebView = WeakObjCPtr<WebHTMLView>((__bridge WebHTMLView *)observer);
+    WeakObjCPtr<WebHTMLView> weakWebView { (__bridge WebHTMLView *)observer };
     WebThreadRun(^{
         if (auto webView = weakWebView.get()) {
             if (auto* coreFrame = core([webView _frame]))
@@ -2601,7 +2602,9 @@
     [[NSNotificationCenter defaultCenter] 
             addObserver:self selector:@selector(markedTextUpdate:) 
                    name:WebMarkedTextUpdatedNotification object:nil];
-    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), hardwareKeyboardAvailabilityChangedCallback, (CFStringRef)[NSString stringWithUTF8String:kGSEventHardwareKeyboardAvailabilityChangedNotification], nullptr, CFNotificationSuspensionBehaviorCoalesce);
+    auto notificationName = adoptNS([[NSString alloc] initWithCString:kGSEventHardwareKeyboardAvailabilityChangedNotification encoding:NSUTF8StringEncoding]);
+    auto notificationBehavior = static_cast<CFNotificationSuspensionBehavior>(CFNotificationSuspensionBehaviorCoalesce | _CFNotificationObserverIsObjC);
+    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), hardwareKeyboardAvailabilityChangedCallback, (__bridge CFStringRef)notificationName.get(), nullptr, notificationBehavior);
 #endif
 
 #if PLATFORM(MAC)
@@ -2618,7 +2621,8 @@
 
 #if PLATFORM(IOS_FAMILY)
     [[NSNotificationCenter defaultCenter] removeObserver:self name:WebMarkedTextUpdatedNotification object:nil];
-    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), (CFStringRef)[NSString stringWithUTF8String:kGSEventHardwareKeyboardAvailabilityChangedNotification], nullptr);
+    auto notificationName = adoptNS([[NSString alloc] initWithCString:kGSEventHardwareKeyboardAvailabilityChangedNotification encoding:NSUTF8StringEncoding]);
+    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), (__bridge CFStringRef)notificationName.get(), nullptr);
 #endif
 
     // We can't assert that close has already been called because
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to