[iOS] Stop using legacy BKSApplicationStateMonitor
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Apr 2020 14:57:32 +0000 (14:57 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Apr 2020 14:57:32 +0000 (14:57 +0000)
https://bugs.webkit.org/show_bug.cgi?id=210945

Reviewed by Tim Horton.

Source/WebKit:

Stop using legacy BKSApplicationStateMonitor and use RunningBoard API instead.

* Configurations/WebKit.xcconfig:
Stop linking against ApplicationServices when using iOS 14 SDK now that we are
fully transitioned to RunningBoard.

* Platform/spi/ios/RunningBoardServicesSPI.h:
* UIProcess/ApplicationStateTracker.h:
* UIProcess/ApplicationStateTracker.mm:
(WebKit::ApplicationStateTracker::ApplicationStateTracker):
(WebKit::isApplicationForeground):
(WebKit::ApplicationStateTracker::~ApplicationStateTracker):
(WebKit::isBackgroundState): Deleted.
* UIProcess/ios/PageClientImplIOS.mm:
(WebKit::PageClientImpl::isApplicationVisible):

WebKitLibraries:

* WebKitPrivateFrameworkStubs/iOS/13/RunningBoardServices.framework/RunningBoardServices.tbd:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@260642 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/Configurations/WebKit.xcconfig
Source/WebKit/Platform/spi/ios/RunningBoardServicesSPI.h
Source/WebKit/UIProcess/ApplicationStateTracker.h
Source/WebKit/UIProcess/ApplicationStateTracker.mm
Source/WebKit/UIProcess/ios/PageClientImplIOS.mm
WebKitLibraries/ChangeLog
WebKitLibraries/WebKitPrivateFrameworkStubs/iOS/13/RunningBoardServices.framework/RunningBoardServices.tbd

index 4df252f..93016d9 100644 (file)
@@ -1,3 +1,26 @@
+2020-04-24  Chris Dumez  <cdumez@apple.com>
+
+        [iOS] Stop using legacy BKSApplicationStateMonitor
+        https://bugs.webkit.org/show_bug.cgi?id=210945
+
+        Reviewed by Tim Horton.
+
+        Stop using legacy BKSApplicationStateMonitor and use RunningBoard API instead.
+
+        * Configurations/WebKit.xcconfig:
+        Stop linking against ApplicationServices when using iOS 14 SDK now that we are
+        fully transitioned to RunningBoard.
+
+        * Platform/spi/ios/RunningBoardServicesSPI.h:
+        * UIProcess/ApplicationStateTracker.h:
+        * UIProcess/ApplicationStateTracker.mm:
+        (WebKit::ApplicationStateTracker::ApplicationStateTracker):
+        (WebKit::isApplicationForeground):
+        (WebKit::ApplicationStateTracker::~ApplicationStateTracker):
+        (WebKit::isBackgroundState): Deleted.
+        * UIProcess/ios/PageClientImplIOS.mm:
+        (WebKit::PageClientImpl::isApplicationVisible):
+
 2020-04-23  Simon Fraser  <simon.fraser@apple.com>
 
         Bounce displayWasRefreshed() via EventDispatcher
index 084a56e..f14e8e6 100644 (file)
@@ -48,8 +48,16 @@ WK_APPKIT_LDFLAGS = $(WK_APPKIT_LDFLAGS_$(WK_PLATFORM_NAME));
 WK_APPKIT_LDFLAGS_macosx = -framework AppKit;
 WK_APPKIT_LDFLAGS_maccatalyst = -framework AppKit;
 
-WK_ASSERTION_SERVICES_LDFLAGS = $(WK_ASSERTION_SERVICES_LDFLAGS_$(WK_COCOA_TOUCH));
-WK_ASSERTION_SERVICES_LDFLAGS_cocoatouch = -framework AssertionServices;
+WK_ASSERTION_SERVICES_LDFLAGS = $(WK_ASSERTION_SERVICES_LDFLAGS_$(WK_PLATFORM_NAME));
+WK_ASSERTION_SERVICES_LDFLAGS_iphoneos = $(WK_ASSERTION_SERVICES_LDFLAGS$(WK_IOS_14));
+WK_ASSERTION_SERVICES_LDFLAGS_iphonesimulator = $(WK_ASSERTION_SERVICES_LDFLAGS$(WK_IOS_14));
+WK_ASSERTION_SERVICES_LDFLAGS_IOS_BEFORE_14 = -framework AssertionServices;
+// FIXME: It is unnecessary to link against AssertionServices with the latest SDK for the following platforms too.
+WK_ASSERTION_SERVICES_LDFLAGS_watchos = -framework AssertionServices;
+WK_ASSERTION_SERVICES_LDFLAGS_watchsimulator = -framework AssertionServices;
+WK_ASSERTION_SERVICES_LDFLAGS_appletvos = -framework AssertionServices;
+WK_ASSERTION_SERVICES_LDFLAGS_appletvsimulator = -framework AssertionServices;
+WK_ASSERTION_SERVICES_LDFLAGS_maccatalyst = -framework AssertionServices;
 
 WK_RUNNINGBOARD_SERVICES_LDFLAGS = $(WK_RUNNINGBOARD_SERVICES_LDFLAGS_$(WK_COCOA_TOUCH));
 WK_RUNNINGBOARD_SERVICES_LDFLAGS_cocoatouch = -framework RunningBoardServices;
index 6dddd2d..1a03843 100644 (file)
 + (RBSTarget *)targetWithPid:(pid_t)pid;
 @end
 
-@protocol RBSAssertionObserving <NSObject>
-- (void)assertionWillInvalidate:(RBSAssertion *)assertion;
-- (void)assertion:(RBSAssertion *)assertion didInvalidateWithError:(NSError *)error;
-@end
+@protocol RBSAssertionObserving;
 
 @interface RBSAssertion : NSObject
 - (instancetype)initWithExplanation:(NSString *)explanation target:(RBSTarget *)target attributes:(NSArray <RBSAttribute *> *)attributes;
 - (void)removeObserver:(id <RBSAssertionObserving>)observer;
 @end
 
+@protocol RBSAssertionObserving <NSObject>
+- (void)assertionWillInvalidate:(RBSAssertion *)assertion;
+- (void)assertion:(RBSAssertion *)assertion didInvalidateWithError:(NSError *)error;
+@end
+
+@interface RBSProcessIdentifier : NSObject
++ (RBSProcessIdentifier *)identifierWithPid:(pid_t)pid;
+@end
+
+typedef NS_ENUM(uint8_t, RBSTaskState) {
+    RBSTaskStateUnknown                 = 0,
+    RBSTaskStateNone                    = 1,
+    RBSTaskStateRunningUnknown          = 2,
+    RBSTaskStateRunningSuspended        = 3,
+    RBSTaskStateRunningScheduled        = 4,
+};
+
+@interface RBSProcessState : NSObject
+@property (nonatomic, readonly, assign) RBSTaskState taskState;
+@property (nonatomic, readonly, copy) NSSet<NSString *> *endowmentNamespaces;
+@end
+
+@interface RBSProcessHandle : NSObject
++ (RBSProcessHandle *)handleForIdentifier:(RBSProcessIdentifier *)identifier error:(NSError **)outError;
+@property (nonatomic, readonly, strong) RBSProcessState *currentState;
+@end
+
 #endif
index 080a169..ccc5c55 100644 (file)
@@ -62,8 +62,6 @@ private:
 
     bool m_isInBackground;
 
-    RetainPtr<BKSApplicationStateMonitor> m_applicationStateMonitor;
-
     id m_didEnterBackgroundObserver;
     id m_didFinishSnapshottingAfterEnteringBackgroundObserver;
     id m_willEnterForegroundObserver;
@@ -80,6 +78,7 @@ enum class ApplicationType {
 };
 
 ApplicationType applicationType(UIWindow *);
+bool isApplicationForeground(pid_t);
 
 }
 
index 356fcfb..3a678d6 100644 (file)
@@ -30,6 +30,7 @@
 
 #import "AssertionServicesSPI.h"
 #import "Logging.h"
+#import "RunningBoardServicesSPI.h"
 #import "SandboxUtilities.h"
 #import "UIKitSPI.h"
 #import <wtf/ObjCRuntimeExtras.h>
@@ -63,18 +64,6 @@ ApplicationType applicationType(UIWindow *window)
     return ApplicationType::Application;
 }
 
-static bool isBackgroundState(BKSApplicationState state)
-{
-    switch (state) {
-    case BKSApplicationStateBackgroundRunning:
-    case BKSApplicationStateBackgroundTaskSuspended:
-        return true;
-
-    default:
-        return false;
-    }
-}
-
 ApplicationStateTracker::ApplicationStateTracker(UIView *view, SEL didEnterBackgroundSelector, SEL didFinishSnapshottingAfterEnteringBackgroundSelector, SEL willEnterForegroundSelector, SEL willBeginSnapshotSequenceSelector, SEL didCompleteSnapshotSequenceSelector)
     : m_view(view)
     , m_didEnterBackgroundSelector(didEnterBackgroundSelector)
@@ -168,9 +157,7 @@ ApplicationStateTracker::ApplicationStateTracker(UIView *view, SEL didEnterBackg
         pid_t applicationPID = serviceViewController._hostProcessIdentifier;
         ASSERT(applicationPID);
 
-        auto applicationStateMonitor = adoptNS([[BKSApplicationStateMonitor alloc] init]);
-        m_isInBackground = isBackgroundState([applicationStateMonitor mostElevatedApplicationStateForPID:applicationPID]);
-        [applicationStateMonitor invalidate];
+        m_isInBackground = !isApplicationForeground(applicationPID);
 
         // Workaround for <rdar://problem/34028921>. If the host application is StoreKitUIService then it is also a ViewService
         // and is always in the background. We need to treat StoreKitUIService as foreground for the purpose of process suspension
@@ -194,13 +181,33 @@ ApplicationStateTracker::ApplicationStateTracker(UIView *view, SEL didEnterBackg
     }
 }
 
+bool isApplicationForeground(pid_t pid)
+{
+    RBSProcessIdentifier *processIdentifier = [RBSProcessIdentifier identifierWithPid:pid];
+    if (!processIdentifier) {
+        RELEASE_LOG_ERROR(ProcessSuspension, "isApplicationForeground: Failed to construct RBSProcessIdentifier from PID %d", pid);
+        return false;
+    }
+
+    NSError *error = nil;
+    RBSProcessHandle *processHandle = [RBSProcessHandle handleForIdentifier:processIdentifier error:&error];
+    if (!processHandle) {
+        RELEASE_LOG_ERROR(ProcessSuspension, "isApplicationForeground: Failed to get RBSProcessHandle for process with PID %d, error: %{public}@", pid, error);
+        return false;
+    }
+
+    RBSProcessState *state = processHandle.currentState;
+    if (state.taskState != RBSTaskStateRunningScheduled) {
+        RELEASE_LOG_ERROR(ProcessSuspension, "isApplicationForeground: Process with PID %d is not running", pid);
+        return false;
+    }
+
+    return [[state endowmentNamespaces] containsObject:@"com.apple.frontboard.visibility"];
+}
+
 ApplicationStateTracker::~ApplicationStateTracker()
 {
     RELEASE_LOG(ViewState, "%p - ~ApplicationStateTracker", this);
-    if (m_applicationStateMonitor) {
-        [m_applicationStateMonitor invalidate];
-        return;
-    }
 
     NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
     [notificationCenter removeObserver:m_didEnterBackgroundObserver];
index 31b3f47..66b398a 100644 (file)
@@ -38,6 +38,7 @@
 #import "InteractionInformationAtPosition.h"
 #import "NativeWebKeyboardEvent.h"
 #import "NavigationState.h"
+#import "RunningBoardServicesSPI.h"
 #import "StringUtilities.h"
 #import "UIKitSPI.h"
 #import "UndoOrRedo.h"
@@ -168,10 +169,7 @@ bool PageClientImpl::isApplicationVisible()
     pid_t applicationPID = serviceViewController._hostProcessIdentifier;
     ASSERT(applicationPID);
 
-    auto applicationStateMonitor = adoptNS([[BKSApplicationStateMonitor alloc] init]);
-    auto applicationState = [applicationStateMonitor mostElevatedApplicationStateForPID:applicationPID];
-    [applicationStateMonitor invalidate];
-    return applicationState != BKSApplicationStateBackgroundRunning && applicationState != BKSApplicationStateBackgroundTaskSuspended;
+    return isApplicationForeground(applicationPID);
 }
 
 bool PageClientImpl::isViewInWindow()
index 623d314..9f0cc3e 100644 (file)
@@ -1,3 +1,12 @@
+2020-04-24  Chris Dumez  <cdumez@apple.com>
+
+        [iOS] Stop using legacy BKSApplicationStateMonitor
+        https://bugs.webkit.org/show_bug.cgi?id=210945
+
+        Reviewed by Tim Horton.
+
+        * WebKitPrivateFrameworkStubs/iOS/13/RunningBoardServices.framework/RunningBoardServices.tbd:
+
 2020-03-16  Keith Rollin  <krollin@apple.com>
 
         Remove support for WebKitSystemInterface
index 2cb7ffc..9c56eb9 100644 (file)
@@ -5,5 +5,5 @@ objc-constraint: none
 platform: ios
 exports:
   - archs:           [ x86_64, arm64, arm64e ]
-    objc-classes:    [ RBSAttribute, RBSDomainAttribute, RBSTarget, RBSAssertion ]
+    objc-classes:    [ RBSAttribute, RBSDomainAttribute, RBSTarget, RBSAssertion, RBSProcessIdentifier, RBSProcessState, RBSProcessHandle ]
 ...