Title: [258949] trunk
Revision
258949
Author
[email protected]
Date
2020-03-24 15:41:13 -0700 (Tue, 24 Mar 2020)

Log Message

[Cocoa] Fix launch time regression with CF prefs direct mode enabled
https://bugs.webkit.org/show_bug.cgi?id=209244
Source/WebKit:

<rdar://problem/60542149>

Reviewed by Darin Adler.

When CF prefs direct mode was enabled in https://trac.webkit.org/changeset/258064/webkit, it introduced
a significant launch time regression. This patch addresses this regression. The number of observed domains
is reduced and domain observation is initiated later when Safari is first activated. Swizzling code is
removed, since that has a performance cost in the Objective-C runtime. Normal priority instead of
QOS_CLASS_BACKGROUND is used in the thread which starts the observing, since using a background priority
class can lead to priority inversion. Finally, a dictionary comparison is removed when a notification
about a preference change is received, since this check is redundant and doubles the cost of this method.

* UIProcess/Cocoa/PreferenceObserver.mm:
(-[WKPreferenceObserver init]):
* UIProcess/Cocoa/WebProcessPoolCocoa.mm:
(WebKit::WebProcessPool::platformInitialize):
(WebKit::WebProcessPool::registerNotificationObservers):
(WebKit::WebProcessPool::unregisterNotificationObservers):
* UIProcess/WebProcessPool.h:

Source/WTF:

Reviewed by Darin Adler.

Re-enable CF prefs direct mode.

* wtf/PlatformEnable.h:
* wtf/PlatformEnableCocoa.h:

Tools:

Reviewed by Darin Adler.

* TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm:
(TEST):
(sharedInstanceMethodOverride):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (258948 => 258949)


--- trunk/Source/WTF/ChangeLog	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WTF/ChangeLog	2020-03-24 22:41:13 UTC (rev 258949)
@@ -1,5 +1,17 @@
 2020-03-24  Per Arne Vollan  <[email protected]>
 
+        [Cocoa] Fix launch time regression with CF prefs direct mode enabled
+        https://bugs.webkit.org/show_bug.cgi?id=209244
+
+        Reviewed by Darin Adler.
+
+        Re-enable CF prefs direct mode.
+
+        * wtf/PlatformEnable.h:
+        * wtf/PlatformEnableCocoa.h:
+
+2020-03-24  Per Arne Vollan  <[email protected]>
+
         [Cocoa] Deny access to database mapping service
         https://bugs.webkit.org/show_bug.cgi?id=209339
         <rdar://problem/56966010>

Modified: trunk/Source/WTF/wtf/PlatformEnable.h (258948 => 258949)


--- trunk/Source/WTF/wtf/PlatformEnable.h	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WTF/wtf/PlatformEnable.h	2020-03-24 22:41:13 UTC (rev 258949)
@@ -553,6 +553,9 @@
 #define ENABLE_MALLOC_HEAP_BREAKDOWN 0
 #endif
 
+#if !defined(ENABLE_CFPREFS_DIRECT_MODE)
+#define ENABLE_CFPREFS_DIRECT_MODE 0
+#endif
 
 
 
@@ -870,5 +873,3 @@
 #if ENABLE(WEBGL2) && !ENABLE(WEBGL)
 #error "ENABLE(WEBGL2) requires ENABLE(WEBGL)"
 #endif
-
-#define ENABLE_CFPREFS_DIRECT_MODE 0

Modified: trunk/Source/WTF/wtf/PlatformEnableCocoa.h (258948 => 258949)


--- trunk/Source/WTF/wtf/PlatformEnableCocoa.h	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WTF/wtf/PlatformEnableCocoa.h	2020-03-24 22:41:13 UTC (rev 258949)
@@ -280,3 +280,7 @@
 #if !defined(ENABLE_TAKE_UNBOUNDED_NETWORKING_ASSERTION) && (PLATFORM(IOS) || PLATFORM(MACCATALYST))
 #define ENABLE_TAKE_UNBOUNDED_NETWORKING_ASSERTION 1
 #endif
+
+#if !defined(ENABLE_CFPREFS_DIRECT_MODE)
+#define ENABLE_CFPREFS_DIRECT_MODE 1
+#endif

Modified: trunk/Source/WebKit/ChangeLog (258948 => 258949)


--- trunk/Source/WebKit/ChangeLog	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/ChangeLog	2020-03-24 22:41:13 UTC (rev 258949)
@@ -1,3 +1,27 @@
+2020-03-24  Per Arne Vollan  <[email protected]>
+
+        [Cocoa] Fix launch time regression with CF prefs direct mode enabled
+        https://bugs.webkit.org/show_bug.cgi?id=209244
+        <rdar://problem/60542149>
+
+        Reviewed by Darin Adler.
+
+        When CF prefs direct mode was enabled in https://trac.webkit.org/changeset/258064/webkit, it introduced
+        a significant launch time regression. This patch addresses this regression. The number of observed domains
+        is reduced and domain observation is initiated later when Safari is first activated. Swizzling code is
+        removed, since that has a performance cost in the Objective-C runtime. Normal priority instead of
+        QOS_CLASS_BACKGROUND is used in the thread which starts the observing, since using a background priority
+        class can lead to priority inversion. Finally, a dictionary comparison is removed when a notification
+        about a preference change is received, since this check is redundant and doubles the cost of this method.
+
+        * UIProcess/Cocoa/PreferenceObserver.mm:
+        (-[WKPreferenceObserver init]):
+        * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+        (WebKit::WebProcessPool::platformInitialize):
+        (WebKit::WebProcessPool::registerNotificationObservers):
+        (WebKit::WebProcessPool::unregisterNotificationObservers):
+        * UIProcess/WebProcessPool.h:
+
 2020-03-24  Daniel Bates  <[email protected]>
 
         Element context character rects may be in wrong coordinate system

Modified: trunk/Source/WebKit/Resources/SandboxProfiles/ios/com.apple.WebKit.WebContent.sb (258948 => 258949)


--- trunk/Source/WebKit/Resources/SandboxProfiles/ios/com.apple.WebKit.WebContent.sb	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/Resources/SandboxProfiles/ios/com.apple.WebKit.WebContent.sb	2020-03-24 22:41:13 UTC (rev 258949)
@@ -534,7 +534,6 @@
 (allow mach-lookup
     (global-name "com.apple.logd")
     (global-name "com.apple.logd.events")
-    (global-name "com.apple.cfprefsd.daemon")
 )
 
 (deny mach-lookup (with telemetry-backtrace)

Modified: trunk/Source/WebKit/UIProcess/Cocoa/PreferenceObserver.h (258948 => 258949)


--- trunk/Source/WebKit/UIProcess/Cocoa/PreferenceObserver.h	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/UIProcess/Cocoa/PreferenceObserver.h	2020-03-24 22:41:13 UTC (rev 258949)
@@ -46,6 +46,5 @@
     Vector<RetainPtr<WKUserDefaults>> m_userDefaults;
 }
 + (id)sharedInstance;
-+ (void)swizzleRegisterDefaults;
 - (void)preferenceDidChange:(NSString *)domain key:(NSString *)key encodedValue:(NSString *)encodedValue;
 @end

Modified: trunk/Source/WebKit/UIProcess/Cocoa/PreferenceObserver.mm (258948 => 258949)


--- trunk/Source/WebKit/UIProcess/Cocoa/PreferenceObserver.mm	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/UIProcess/Cocoa/PreferenceObserver.mm	2020-03-24 22:41:13 UTC (rev 258949)
@@ -28,31 +28,12 @@
 
 #import "WebProcessPool.h"
 
-#import <wtf/ObjCRuntimeExtras.h>
-
-static IMP registerDefaultsOriginal = nil;
-static bool registeringDefaults = false;
-
-static void registerDefaultsOverride(id self, SEL selector, NSDictionary<NSString *, id> *dictionary)
-{
-    registeringDefaults = true;
-    if (registerDefaultsOriginal)
-        wtfCallIMP<void>(registerDefaultsOriginal, self, selector, dictionary);
-    registeringDefaults = false;
-}
-
 @implementation WKUserDefaults
 
 - (void)_notifyObserversOfChangeFromValuesForKeys:(NSDictionary<NSString *, id> *)oldValues toValuesForKeys:(NSDictionary<NSString *, id> *)newValues
 {
-    if (registeringDefaults)
-        return;
-
     [super _notifyObserversOfChangeFromValuesForKeys:oldValues toValuesForKeys:newValues];
 
-    if ([oldValues isEqualToDictionary:newValues])
-        return;
-
     for (NSString *key in oldValues) {
         id oldValue = oldValues[key];
         id newValue = newValues[key];
@@ -101,103 +82,35 @@
     return instance;
 }
 
-+ (void)swizzleRegisterDefaults
-{
-    static std::once_flag onceFlag;
-    std::call_once(onceFlag, [] {
-        Method registerDefaultsMethod = class_getInstanceMethod(objc_getClass("NSUserDefaults"), @selector(registerDefaults:));
-        registerDefaultsOriginal = method_setImplementation(registerDefaultsMethod, (IMP)registerDefaultsOverride);
-    });
-}
-
 - (instancetype)init
 {
     std::initializer_list<NSString*> domains = {
+#if PLATFORM(IOS_FAMILY)
         @"com.apple.Accessibility",
-#if PLATFORM(IOS_FAMILY)
         @"com.apple.AdLib",
-        @"com.apple.CFNetwork",
-        @"com.apple.EmojiPreferences",
-        @"com.apple.FontParser",
-        @"com.apple.ImageIO",
-        @"com.apple.InputModePreferences",
-#else
-        @"com.apple.ATS",
-        @"com.apple.CoreGraphics",
-        @"com.apple.DownloadAssessment",
-        @"com.apple.HIToolbox",
-#endif
-        @"com.apple.LaunchServices",
-#if PLATFORM(IOS_FAMILY)
-        @"com.apple.Metal",
-        @"com.apple.MobileAsset",
         @"com.apple.Preferences",
         @"com.apple.SpeakSelection",
         @"com.apple.UIKit",
-        @"com.apple.VoiceOverTouch",
-#else
-        @"com.apple.MultitouchSupport"
-        @"com.apple.ServicesMenu.Services"
-        @"com.apple.ViewBridge"
-#endif
-        @"com.apple.WebFoundation",
-#if PLATFORM(IOS_FAMILY)
-        @"com.apple.WebKit.WebContent",
         @"com.apple.WebUI",
-        @"com.apple.airplay",
-        @"com.apple.applejpeg",
-        @"com.apple.audio.virtualaudio",
         @"com.apple.avfaudio",
-#endif
+        @"com.apple.itunesstored",
+        @"com.apple.mediaremote",
+        @"com.apple.preferences.sounds",
+        @"com.apple.voiceservices",
+        @"kCFPreferencesAnyApplication",
+#else
+        @"com.apple.CoreGraphics",
+        @"com.apple.HIToolbox",
+        @"com.apple.ServicesMenu.Services",
+        @"com.apple.ViewBridge",
         @"com.apple.avfoundation",
-        @"com.apple.avfoundation.frecents",
         @"com.apple.avfoundation.videoperformancehud",
-#if PLATFORM(IOS_FAMILY)
-        @"com.apple.avkit",
-        @"com.apple.coreanimation",
-        @"com.apple.coreaudio",
-#endif
-        @"com.apple.coremedia",
-#if PLATFORM(IOS_FAMILY)
-        @"com.apple.corevideo",
-        @"com.apple.da",
-        @"com.apple.hangtracer",
-        @"com.apple.indigo",
-        @"com.apple.iokit.IOMobileGraphicsFamily",
-        @"com.apple.itunesstored",
-        @"com.apple.keyboard",
-#else
-        @"com.apple.crypto",
         @"com.apple.driver.AppleBluetoothMultitouch.mouse",
         @"com.apple.driver.AppleBluetoothMultitouch.trackpad",
-        @"com.apple.driver.AppleHIDMouse"
-#endif
-        @"com.apple.lookup.shared",
         @"com.apple.mediaaccessibility",
-#if PLATFORM(IOS_FAMILY)
-        @"com.apple.mediaaccessibility.public",
-        @"com.apple.mediaremote",
-        @"com.apple.mobileipod",
-        @"com.apple.mt",
-#else
-        @"com.apple.networkConnect"
-#endif
-        @"com.apple.opengl",
-#if PLATFORM(IOS_FAMILY)
-        @"com.apple.preferences.sounds",
-        @"com.apple.security",
-        @"com.apple.voiceservices",
-        @"com.apple.voiceservices.logging",
-#else
         @"com.apple.speech.voice.prefs",
-        @"com.apple.systemsound",
         @"com.apple.universalaccess",
-        @"com.nvidia.OpenGL",
-        @"edu.mit.Kerberos",
-#endif
         @"kCFPreferencesAnyApplication",
-#if !PLATFORM(IOS_FAMILY)
-        @"pbs",
 #endif
     };
 

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm (258948 => 258949)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm	2020-03-24 22:41:13 UTC (rev 258949)
@@ -173,14 +173,6 @@
         installMemoryPressureHandler();
 
     setLegacyCustomProtocolManagerClient(makeUnique<LegacyCustomProtocolManagerClient>());
-
-#if ENABLE(CFPREFS_DIRECT_MODE)
-    [WKPreferenceObserver swizzleRegisterDefaults];
-    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
-        // Start observing preference changes.
-        [WKPreferenceObserver sharedInstance];
-    });
-#endif
 }
 
 #if PLATFORM(IOS_FAMILY)
@@ -611,6 +603,19 @@
 }
 #endif
 
+#if ENABLE(CFPREFS_DIRECT_MODE)
+void WebProcessPool::startObservingPreferenceChanges()
+{
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+            // Start observing preference changes.
+            [WKPreferenceObserver sharedInstance];
+        });
+    });
+}
+#endif
+
 void WebProcessPool::registerNotificationObservers()
 {
 #if !PLATFORM(IOS_FAMILY)
@@ -655,6 +660,9 @@
 #endif
 
     m_activationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidBecomeActiveNotification object:NSApp queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
+#if ENABLE(CFPREFS_DIRECT_MODE)
+        startObservingPreferenceChanges();
+#endif
         setApplicationIsActive(true);
     }];
 
@@ -676,6 +684,11 @@
             m_processes[i]->unblockAccessibilityServerIfNeeded();
         }
     }];
+#if ENABLE(CFPREFS_DIRECT_MODE)
+    m_activationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"UIApplicationDidBecomeActiveNotification" object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
+        startObservingPreferenceChanges();
+    }];
+#endif
 #endif // !PLATFORM(IOS_FAMILY)
 }
 
@@ -691,7 +704,6 @@
 #if ENABLE(WEBPROCESS_WINDOWSERVER_BLOCKING)
     [[NSNotificationCenter defaultCenter] removeObserver:m_scrollerStyleNotificationObserver.get()];
 #endif
-    [[NSNotificationCenter defaultCenter] removeObserver:m_activationObserver.get()];
     [[NSNotificationCenter defaultCenter] removeObserver:m_deactivationObserver.get()];
 #elif !PLATFORM(MACCATALYST)
     CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), this, static_cast<CFStringRef>(UIBacklightLevelChangedNotification) , nullptr);
@@ -701,6 +713,7 @@
 #endif
 #endif // PLATFORM(IOS)
     [[NSNotificationCenter defaultCenter] removeObserver:m_accessibilityEnabledObserver.get()];
+    [[NSNotificationCenter defaultCenter] removeObserver:m_activationObserver.get()];
 #endif // !PLATFORM(IOS_FAMILY)
 }
 

Modified: trunk/Source/WebKit/UIProcess/WebProcessPool.h (258948 => 258949)


--- trunk/Source/WebKit/UIProcess/WebProcessPool.h	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/UIProcess/WebProcessPool.h	2020-03-24 22:41:13 UTC (rev 258949)
@@ -611,6 +611,10 @@
 #endif
 #endif
 
+#if ENABLE(CFPREFS_DIRECT_MODE)
+    void startObservingPreferenceChanges();
+#endif
+
     Ref<API::ProcessPoolConfiguration> m_configuration;
 
     IPC::MessageReceiverMap m_messageReceiverMap;
@@ -691,7 +695,6 @@
 #if ENABLE(WEBPROCESS_WINDOWSERVER_BLOCKING)
     RetainPtr<NSObject> m_scrollerStyleNotificationObserver;
 #endif
-    RetainPtr<NSObject> m_activationObserver;
     RetainPtr<NSObject> m_deactivationObserver;
 
     std::unique_ptr<HighPerformanceGraphicsUsageSampler> m_highPerformanceGraphicsUsageSampler;
@@ -699,6 +702,7 @@
 #endif
 
 #if PLATFORM(COCOA)
+    RetainPtr<NSObject> m_activationObserver;
     RetainPtr<NSObject> m_accessibilityEnabledObserver;
 #endif
 
@@ -802,10 +806,6 @@
     bool m_isDelayedWebProcessLaunchDisabled { false };
 #endif
     bool m_useSeparateServiceWorkerProcess { false };
-
-#if PLATFORM(COCOA)
-    RetainPtr<WKPreferenceObserver> m_preferenceObserver;
-#endif
 };
 
 template<typename T>

Modified: trunk/Source/WebKit/WebProcess/com.apple.WebProcess.sb.in (258948 => 258949)


--- trunk/Source/WebKit/WebProcess/com.apple.WebProcess.sb.in	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Source/WebKit/WebProcess/com.apple.WebProcess.sb.in	2020-03-24 22:41:13 UTC (rev 258949)
@@ -677,7 +677,6 @@
     (with report) (with telemetry)
 #endif
     (global-name "com.apple.PowerManagement.control")
-    (global-name "com.apple.cfprefsd.daemon")
     (global-name "com.apple.coreservices.launchservicesd")
     (global-name "com.apple.lsd.mapdb")
     (global-name "com.apple.trustd.agent")

Modified: trunk/Tools/ChangeLog (258948 => 258949)


--- trunk/Tools/ChangeLog	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Tools/ChangeLog	2020-03-24 22:41:13 UTC (rev 258949)
@@ -1,3 +1,14 @@
+2020-03-24  Per Arne Vollan  <[email protected]>
+
+        [Cocoa] Fix launch time regression with CF prefs direct mode enabled
+        https://bugs.webkit.org/show_bug.cgi?id=209244
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm:
+        (TEST):
+        (sharedInstanceMethodOverride):
+
 2020-03-24  Jonathan Bedard  <[email protected]>
 
         results.webkit.org: Correct UUID capitalization in documentation

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm (258948 => 258949)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm	2020-03-24 22:34:20 UTC (rev 258948)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm	2020-03-24 22:41:13 UTC (rev 258949)
@@ -31,6 +31,8 @@
 #import "TestWKWebView.h"
 #import <WebKit/PreferenceObserver.h>
 
+#import <wtf/ObjCRuntimeExtras.h>
+
 static bool done = false;
 
 @interface WKTestPreferenceObserver : WKPreferenceObserver
@@ -44,15 +46,18 @@
 }
 @end
 
+static const CFStringRef testKey = CFSTR("testkey");
+static const CFStringRef testDomain = CFSTR("kCFPreferencesAnyApplication");
+
 TEST(WebKit, PreferenceObserver)
 {
     done = false;
 
-    CFPreferencesSetAppValue(CFSTR("testkey"), CFSTR("1"), CFSTR("com.apple.coremedia"));
+    CFPreferencesSetAppValue(testKey, CFSTR("1"), testDomain);
 
     auto observer = adoptNS([[WKTestPreferenceObserver alloc] init]);
 
-    CFPreferencesSetAppValue(CFSTR("testkey"), CFSTR("2"), CFSTR("com.apple.coremedia"));
+    CFPreferencesSetAppValue(testKey, CFSTR("2"), testDomain);
 
     TestWebKitAPI::Util::run(&done);
 }
@@ -63,13 +68,13 @@
 
     NSArray *array = @[@1, @2, @3];
 
-    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:@"com.apple.coremedia"]);
-    [userDefaults.get() setObject:array forKey:@"testkey"];
+    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:(NSString *)testDomain]);
+    [userDefaults.get() setObject:array forKey:(NSString *)testKey];
 
     auto observer = adoptNS([[WKTestPreferenceObserver alloc] init]);
 
     NSArray *changedArray = @[@3, @2, @1];
-    [userDefaults.get() setObject:changedArray forKey:@"testkey"];
+    [userDefaults.get() setObject:changedArray forKey:(NSString *)testKey];
 
     TestWebKitAPI::Util::run(&done);
 }
@@ -76,9 +81,9 @@
 
 TEST(WebKit, PreferenceChanges)
 {
-    CFPreferencesSetAppValue(CFSTR("testkey"), CFSTR("1"), CFSTR("com.apple.coremedia"));
+    CFPreferencesSetAppValue(testKey, CFSTR("1"), testDomain);
 
-    EXPECT_EQ(1, CFPreferencesGetAppIntegerValue(CFSTR("testkey"), CFSTR("com.apple.coremedia"), nullptr));
+    EXPECT_EQ(1, CFPreferencesGetAppIntegerValue(testKey, testDomain, nullptr));
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
@@ -86,12 +91,13 @@
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
 
     auto preferenceValue = [&] {
-        return [webView stringByEvaluatingJavaScript:@"window.internals.readPreferenceInteger(\"com.apple.coremedia\", \"testkey\")"].intValue;
+        NSString *js = [NSString stringWithFormat:@"window.internals.readPreferenceInteger(\"%@\",\"%@\")", (NSString *)testDomain, (NSString *)testKey];
+        return [webView stringByEvaluatingJavaScript:js].intValue;
     };
 
     EXPECT_EQ(preferenceValue(), 1);
 
-    CFPreferencesSetAppValue(CFSTR("testkey"), CFSTR("2"), CFSTR("com.apple.coremedia"));
+    CFPreferencesSetAppValue(testKey, CFSTR("2"), testDomain);
 
     EXPECT_EQ(preferenceValue(), 2);
 }
@@ -100,8 +106,8 @@
 {
     NSArray *array = @[@1, @2, @3];
 
-    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:@"com.apple.coremedia"]);
-    [userDefaults.get() setObject:array forKey:@"testkey"];
+    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:(NSString *)testDomain]);
+    [userDefaults.get() setObject:array forKey:(NSString *)testKey];
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
@@ -109,13 +115,14 @@
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
 
     auto preferenceValue = [&] {
-        return [webView stringByEvaluatingJavaScript:@"window.internals.encodedPreferenceValue(\"com.apple.coremedia\", \"testkey\")"];
+        NSString *js = [NSString stringWithFormat:@"window.internals.encodedPreferenceValue(\"%@\",\"%@\")", (NSString *)testDomain, (NSString *)testKey];
+        return [webView stringByEvaluatingJavaScript:js];
     };
 
     preferenceValue();
 
     NSArray *changedArray = @[@3, @2, @1];
-    [userDefaults.get() setObject:changedArray forKey:@"testkey"];
+    [userDefaults.get() setObject:changedArray forKey:(NSString *)testKey];
 
     auto encodedString = preferenceValue();
     auto encodedData = adoptNS([[NSData alloc] initWithBase64EncodedString:encodedString options:0]);
@@ -134,8 +141,8 @@
         @"b" : @2,
     };
 
-    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:@"com.apple.coremedia"]);
-    [userDefaults.get() setObject:dict forKey:@"testkey"];
+    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:(NSString *)testDomain]);
+    [userDefaults.get() setObject:dict forKey:(NSString *)testKey];
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
@@ -143,7 +150,8 @@
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
 
     auto preferenceValue = [&] {
-        return [webView stringByEvaluatingJavaScript:@"window.internals.encodedPreferenceValue(\"com.apple.coremedia\", \"testkey\")"];
+        NSString *js = [NSString stringWithFormat:@"window.internals.encodedPreferenceValue(\"%@\",\"%@\")", (NSString *)testDomain, (NSString *)testKey];
+        return [webView stringByEvaluatingJavaScript:js];
     };
 
     preferenceValue();
@@ -153,7 +161,7 @@
         @"b" : @2,
         @"c" : @3,
     };
-    [userDefaults.get() setObject:changedDict forKey:@"testkey"];
+    [userDefaults.get() setObject:changedDict forKey:(NSString *)testKey];
 
     auto encodedString = preferenceValue();
     auto encodedData = adoptNS([[NSData alloc] initWithBase64EncodedString:encodedString options:0]);
@@ -169,8 +177,8 @@
 {
     NSData *data = "" dataWithBytes:"abc" length:3];
 
-    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:@"com.apple.coremedia"]);
-    [userDefaults.get() setObject:data forKey:@"testkey"];
+    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:(NSString *)testDomain]);
+    [userDefaults.get() setObject:data forKey:(NSString *)testKey];
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
@@ -178,13 +186,14 @@
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
 
     auto preferenceValue = [&] {
-        return [webView stringByEvaluatingJavaScript:@"window.internals.encodedPreferenceValue(\"com.apple.coremedia\", \"testkey\")"];
+        NSString *js = [NSString stringWithFormat:@"window.internals.encodedPreferenceValue(\"%@\",\"%@\")", (NSString *)testDomain, (NSString *)testKey];
+        return [webView stringByEvaluatingJavaScript:js];
     };
 
     preferenceValue();
 
     NSData *changedData = [NSData dataWithBytes:"abcd" length:4];
-    [userDefaults.get() setObject:changedData forKey:@"testkey"];
+    [userDefaults.get() setObject:changedData forKey:(NSString *)testKey];
 
     auto encodedString = preferenceValue();
     auto encodedData = adoptNS([[NSData alloc] initWithBase64EncodedString:encodedString options:0]);
@@ -200,8 +209,8 @@
 {
     NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
 
-    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:@"com.apple.coremedia"]);
-    [userDefaults.get() setObject:date forKey:@"testkey"];
+    auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:(NSString *)testDomain]);
+    [userDefaults.get() setObject:date forKey:(NSString *)testKey];
 
     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
@@ -209,13 +218,14 @@
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
 
     auto preferenceValue = [&] {
-        return [webView stringByEvaluatingJavaScript:@"window.internals.encodedPreferenceValue(\"com.apple.coremedia\", \"testkey\")"];
+        NSString *js = [NSString stringWithFormat:@"window.internals.encodedPreferenceValue(\"%@\",\"%@\")", (NSString *)testDomain, (NSString *)testKey];
+        return [webView stringByEvaluatingJavaScript:js];
     };
 
     preferenceValue();
 
     NSDate *changedDate = [NSDate dateWithTimeIntervalSinceNow:10];
-    [userDefaults.get() setObject:changedDate forKey:@"testkey"];
+    [userDefaults.get() setObject:changedDate forKey:(NSString *)testKey];
 
     auto encodedString = preferenceValue();
     auto encodedData = adoptNS([[NSData alloc] initWithBase64EncodedString:encodedString options:0]);
@@ -227,4 +237,32 @@
     ASSERT_TRUE([object isEqual:changedDate]);
 }
 
+static IMP sharedInstanceMethodOriginal = nil;
+
+static WKPreferenceObserver *sharedInstanceMethodOverride(id self, SEL selector)
+{
+    done = true;
+    return wtfCallIMP<WKPreferenceObserver *>(sharedInstanceMethodOriginal, self, selector);
+}
+
+TEST(WebKit, PreferenceObserverStartedOnActivation)
+{
+    done = false;
+    Method sharedInstanceMethod = class_getClassMethod(objc_getClass("WKPreferenceObserver"), @selector(sharedInstance));
+    ASSERT(sharedInstanceMethod);
+    sharedInstanceMethodOriginal = method_setImplementation(sharedInstanceMethod, (IMP)sharedInstanceMethodOverride);
+    ASSERT(sharedInstanceMethodOriginal);
+
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
+    configuration.get().processPool = (WKProcessPool *)context.get();
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
+
+    [webView synchronouslyLoadTestPageNamed:@"simple"];
+
+    [[NSNotificationCenter defaultCenter] postNotificationName:NSApplicationDidBecomeActiveNotification object:NSApp userInfo:nil];
+
+    TestWebKitAPI::Util::run(&done);
+}
+
 #endif // WK_HAVE_C_SPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to