Clean up / simplify ProcessAssertion code
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Mar 2019 21:49:22 +0000 (21:49 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 7 Mar 2019 21:49:22 +0000 (21:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=195394

Reviewed by Geoffrey Garen.

Clean up / simplify ProcessAssertion code to facilitate its maintenance and make it
less error-prone.

* Platform/IPC/mac/ConnectionMac.mm:
(IPC::ConnectionTerminationWatchdog::ConnectionTerminationWatchdog):
* UIProcess/ProcessAssertion.cpp:
(WebKit::ProcessAssertion::ProcessAssertion):
(WebKit::ProcessAssertion::setState):
* UIProcess/ProcessAssertion.h:
(WebKit::ProcessAssertion::Client::~Client):
(WebKit::ProcessAssertion::setClient):
(WebKit::ProcessAssertion::client):
* UIProcess/ProcessThrottler.cpp:
(WebKit::ProcessThrottler::didConnectToProcess):
* UIProcess/ProcessThrottler.h:
* UIProcess/ios/ProcessAssertionIOS.mm:
(-[WKProcessAssertionBackgroundTaskManager dealloc]):
(-[WKProcessAssertionBackgroundTaskManager addAssertionNeedingBackgroundTask:]):
(-[WKProcessAssertionBackgroundTaskManager removeAssertionNeedingBackgroundTask:]):
(-[WKProcessAssertionBackgroundTaskManager _notifyAssertionsOfImminentSuspension]):
(-[WKProcessAssertionBackgroundTaskManager _releaseBackgroundTask]):
(-[WKProcessAssertionBackgroundTaskManager _updateBackgroundTask]):
(WebKit::ProcessAssertion::ProcessAssertion):
(WebKit::ProcessAssertion::processAssertionWasInvalidated):
(WebKit::ProcessAssertion::setState):
(WebKit::ProcessAndUIAssertion::updateRunInBackgroundCount):
(WebKit::ProcessAndUIAssertion::ProcessAndUIAssertion):
(WebKit::ProcessAndUIAssertion::~ProcessAndUIAssertion):
(WebKit::ProcessAndUIAssertion::processAssertionWasInvalidated):
(WebKit::ProcessAndUIAssertion::setState):
(WebKit::ProcessAndUIAssertion::uiAssertionWillExpireImminently):

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

Source/WebKit/ChangeLog
Source/WebKit/Platform/IPC/mac/ConnectionMac.mm
Source/WebKit/UIProcess/ProcessAssertion.cpp
Source/WebKit/UIProcess/ProcessAssertion.h
Source/WebKit/UIProcess/ProcessThrottler.cpp
Source/WebKit/UIProcess/ProcessThrottler.h
Source/WebKit/UIProcess/ios/ProcessAssertionIOS.mm

index 4ea7b83..91606ee 100644 (file)
@@ -1,3 +1,42 @@
+2019-03-07  Chris Dumez  <cdumez@apple.com>
+
+        Clean up / simplify ProcessAssertion code
+        https://bugs.webkit.org/show_bug.cgi?id=195394
+
+        Reviewed by Geoffrey Garen.
+
+        Clean up / simplify ProcessAssertion code to facilitate its maintenance and make it
+        less error-prone.
+
+        * Platform/IPC/mac/ConnectionMac.mm:
+        (IPC::ConnectionTerminationWatchdog::ConnectionTerminationWatchdog):
+        * UIProcess/ProcessAssertion.cpp:
+        (WebKit::ProcessAssertion::ProcessAssertion):
+        (WebKit::ProcessAssertion::setState):
+        * UIProcess/ProcessAssertion.h:
+        (WebKit::ProcessAssertion::Client::~Client):
+        (WebKit::ProcessAssertion::setClient):
+        (WebKit::ProcessAssertion::client):
+        * UIProcess/ProcessThrottler.cpp:
+        (WebKit::ProcessThrottler::didConnectToProcess):
+        * UIProcess/ProcessThrottler.h:
+        * UIProcess/ios/ProcessAssertionIOS.mm:
+        (-[WKProcessAssertionBackgroundTaskManager dealloc]):
+        (-[WKProcessAssertionBackgroundTaskManager addAssertionNeedingBackgroundTask:]):
+        (-[WKProcessAssertionBackgroundTaskManager removeAssertionNeedingBackgroundTask:]):
+        (-[WKProcessAssertionBackgroundTaskManager _notifyAssertionsOfImminentSuspension]):
+        (-[WKProcessAssertionBackgroundTaskManager _releaseBackgroundTask]):
+        (-[WKProcessAssertionBackgroundTaskManager _updateBackgroundTask]):
+        (WebKit::ProcessAssertion::ProcessAssertion):
+        (WebKit::ProcessAssertion::processAssertionWasInvalidated):
+        (WebKit::ProcessAssertion::setState):
+        (WebKit::ProcessAndUIAssertion::updateRunInBackgroundCount):
+        (WebKit::ProcessAndUIAssertion::ProcessAndUIAssertion):
+        (WebKit::ProcessAndUIAssertion::~ProcessAndUIAssertion):
+        (WebKit::ProcessAndUIAssertion::processAssertionWasInvalidated):
+        (WebKit::ProcessAndUIAssertion::setState):
+        (WebKit::ProcessAndUIAssertion::uiAssertionWillExpireImminently):
+
 2019-03-07  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r242297.
index b98ac56..d94193e 100644 (file)
@@ -92,7 +92,7 @@ private:
         : m_xpcConnection(xpcConnection)
         , m_watchdogTimer(RunLoop::main(), this, &ConnectionTerminationWatchdog::watchdogTimerFired)
 #if PLATFORM(IOS_FAMILY)
-        , m_assertion(std::make_unique<WebKit::ProcessAndUIAssertion>(xpc_connection_get_pid(m_xpcConnection.get()), WebKit::AssertionState::Background))
+        , m_assertion(std::make_unique<WebKit::ProcessAndUIAssertion>(xpc_connection_get_pid(m_xpcConnection.get()), "ConnectionTerminationWatchdog"_s, WebKit::AssertionState::Background))
 #endif
     {
         m_watchdogTimer.startOneShot(interval);
index a25df2b..9adbd35 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Apple Inc. All rights reserved.
+ * Copyright (C) 2014-2019 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include "config.h"
 #include "ProcessAssertion.h"
 
-#if !PLATFORM(IOS_FAMILY)
+#if !PLATFORM(IOS_FAMILY) || PLATFORM(IOS_FAMILY_SIMULATOR)
 
 namespace WebKit {
 
-ProcessAssertion::ProcessAssertion(ProcessID, AssertionState assertionState, Function<void()>&&)
+ProcessAssertion::ProcessAssertion(ProcessID, const String&, AssertionState assertionState)
     : m_assertionState(assertionState)
 {
 }
 
-ProcessAssertion::ProcessAssertion(ProcessID, const String&, AssertionState assertionState, Function<void()>&&)
-    : m_assertionState(assertionState)
-{
-}
-
-ProcessAssertion::~ProcessAssertion()
-{
-}
+ProcessAssertion::~ProcessAssertion() = default;
 
 void ProcessAssertion::setState(AssertionState assertionState)
 {
-    if (m_assertionState == assertionState)
-        return;
-
     m_assertionState = assertionState;
 }
 
-ProcessAndUIAssertion::ProcessAndUIAssertion(ProcessID pid, AssertionState assertionState)
-    : ProcessAssertion(pid, assertionState)
-{
-}
-
-ProcessAndUIAssertion::~ProcessAndUIAssertion()
-{
-}
-
-void ProcessAndUIAssertion::setState(AssertionState assertionState)
-{
-    ProcessAssertion::setState(assertionState);
-}
-
-void ProcessAndUIAssertion::setClient(ProcessAssertionClient& client)
-{
-    ProcessAssertion::setClient(client);
-}
-
-}
+} // namespace WebKit
 
-#endif // !PLATFORM(IOS_FAMILY)
+#endif // !PLATFORM(IOS_FAMILY) || PLATFORM(IOS_FAMILY_SIMULATOR)
index 5259352..15670e0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Apple Inc. All rights reserved.
+ * Copyright (C) 2014-2019 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef ProcessAssertion_h
-#define ProcessAssertion_h
+#pragma once
 
 #include <wtf/Function.h>
 #include <wtf/ProcessID.h>
+#include <wtf/WeakPtr.h>
 #include <wtf/text/WTFString.h>
 
 #if !OS(WINDOWS)
@@ -36,7 +36,6 @@
 
 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
 #include <wtf/RetainPtr.h>
-#include <wtf/WeakPtr.h>
 OBJC_CLASS BKSProcessAssertion;
 #endif
 
@@ -49,25 +48,20 @@ enum class AssertionState {
     Foreground
 };
 
-class ProcessAssertionClient {
-public:
-    virtual ~ProcessAssertionClient() { }
-    virtual void uiAssertionWillExpireImminently() = 0;
-};
-
-class ProcessAssertion
-#if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
-    : public CanMakeWeakPtr<ProcessAssertion>
-#endif
-{
+class ProcessAssertion : public CanMakeWeakPtr<ProcessAssertion> {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    ProcessAssertion(ProcessID, AssertionState, Function<void()>&& invalidationCallback = { });
-    ProcessAssertion(ProcessID, const String& reason, AssertionState, Function<void()>&& invalidationCallback = { });
+    class Client {
+    public:
+        virtual ~Client() { }
+        virtual void uiAssertionWillExpireImminently() = 0;
+    };
+
+    ProcessAssertion(ProcessID, const String& reason, AssertionState);
     virtual ~ProcessAssertion();
 
-    virtual void setClient(ProcessAssertionClient& client) { m_client = &client; }
-    ProcessAssertionClient* client() { return m_client; }
+    void setClient(Client& client) { m_client = &client; }
+    Client* client() { return m_client; }
 
     AssertionState state() const { return m_assertionState; }
     virtual void setState(AssertionState);
@@ -76,37 +70,40 @@ public:
 protected:
     enum class Validity { No, Yes, Unset };
     Validity validity() const { return m_validity; }
+
+    virtual void processAssertionWasInvalidated();
 #endif
 
 private:
 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
-    void markAsInvalidated();
-
     RetainPtr<BKSProcessAssertion> m_assertion;
     Validity m_validity { Validity::Unset };
-    Function<void()> m_invalidationCallback;
 #endif
     AssertionState m_assertionState;
-    ProcessAssertionClient* m_client { nullptr };
+    Client* m_client { nullptr };
 };
-    
+
+#if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
+
 class ProcessAndUIAssertion final : public ProcessAssertion {
 public:
-    ProcessAndUIAssertion(ProcessID, AssertionState);
+    ProcessAndUIAssertion(ProcessID, const String& reason, AssertionState);
     ~ProcessAndUIAssertion();
 
-    void setClient(ProcessAssertionClient&) final;
-
     void setState(AssertionState) final;
+    void uiAssertionWillExpireImminently();
 
-#if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
 private:
+    void processAssertionWasInvalidated() final;
     void updateRunInBackgroundCount();
 
-    bool m_isHoldingBackgroundAssertion { false };
-#endif
+    bool m_isHoldingBackgroundTask { false };
 };
-    
-}
 
-#endif // ProcessAssertion_h
+#else
+
+using ProcessAndUIAssertion = ProcessAssertion;
+
+#endif // PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
+    
+} // namespace WebKit
index 108438d..b612da0 100644 (file)
@@ -103,9 +103,9 @@ void ProcessThrottler::didConnectToProcess(ProcessID pid)
 
     m_suspendTimer.stop();
     if (m_shouldTakeUIBackgroundAssertion)
-        m_assertion = std::make_unique<ProcessAndUIAssertion>(pid, assertionState());
+        m_assertion = std::make_unique<ProcessAndUIAssertion>(pid, "Web content visibility"_s, assertionState());
     else
-        m_assertion = std::make_unique<ProcessAssertion>(pid, assertionState());
+        m_assertion = std::make_unique<ProcessAssertion>(pid, "Web content visibility"_s, assertionState());
     m_process.didSetAssertionState(assertionState());
     m_assertion->setClient(*this);
 }
index 5b444d9..0576e38 100644 (file)
@@ -43,7 +43,7 @@ typedef ProcessSuppressionDisabledCounter::Token ProcessSuppressionDisabledToken
 
 class ProcessThrottlerClient;
 
-class ProcessThrottler : private ProcessAssertionClient {
+class ProcessThrottler : private ProcessAssertion::Client {
 public:
     enum ForegroundActivityCounterType { };
     typedef RefCounter<ForegroundActivityCounterType> ForegroundActivityCounter;
index 9942b66..a73c789 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Apple Inc. All rights reserved.
+ * Copyright (C) 2014-2019 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #import "config.h"
 #import "ProcessAssertion.h"
 
-#if PLATFORM(IOS_FAMILY)
+#if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
 
 #import "AssertionServicesSPI.h"
 #import "Logging.h"
 #import <UIKit/UIApplication.h>
-#import <wtf/HashSet.h>
+#import <wtf/HashMap.h>
 #import <wtf/RunLoop.h>
 #import <wtf/Vector.h>
 
-#if !PLATFORM(IOS_FAMILY_SIMULATOR)
-
-using WebKit::ProcessAssertionClient;
+using WebKit::ProcessAndUIAssertion;
 
 @interface WKProcessAssertionBackgroundTaskManager : NSObject
 
 + (WKProcessAssertionBackgroundTaskManager *)shared;
 
-- (void)incrementNeedsToRunInBackgroundCount;
-- (void)decrementNeedsToRunInBackgroundCount;
-
-- (void)addClient:(ProcessAssertionClient&)client;
-- (void)removeClient:(ProcessAssertionClient&)client;
+- (void)addAssertionNeedingBackgroundTask:(ProcessAndUIAssertion&)assertion;
+- (void)removeAssertionNeedingBackgroundTask:(ProcessAndUIAssertion&)assertion;
 
 @end
 
 @implementation WKProcessAssertionBackgroundTaskManager
 {
-    unsigned _needsToRunInBackgroundCount;
     UIBackgroundTaskIdentifier _backgroundTask;
-    HashSet<ProcessAssertionClient*> _clients;
+    HashSet<ProcessAndUIAssertion*> _assertionsNeedingBackgroundTask;
 }
 
 + (WKProcessAssertionBackgroundTaskManager *)shared
@@ -77,66 +71,58 @@ using WebKit::ProcessAssertionClient;
 
 - (void)dealloc
 {
-    if (_backgroundTask != UIBackgroundTaskInvalid)
-        [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
-
+    [self _releaseBackgroundTask];
     [super dealloc];
 }
 
-- (void)addClient:(ProcessAssertionClient&)client
+- (void)addAssertionNeedingBackgroundTask:(ProcessAndUIAssertion&)assertion
 {
-    _clients.add(&client);
+    _assertionsNeedingBackgroundTask.add(&assertion);
+    [self _updateBackgroundTask];
 }
 
-- (void)removeClient:(ProcessAssertionClient&)client
+- (void)removeAssertionNeedingBackgroundTask:(ProcessAndUIAssertion&)assertion
 {
-    _clients.remove(&client);
+    _assertionsNeedingBackgroundTask.remove(&assertion);
+    [self _updateBackgroundTask];
 }
 
-- (void)_notifyClientsOfImminentSuspension
+- (void)_notifyAssertionsOfImminentSuspension
 {
     ASSERT(RunLoop::isMain());
 
-    for (auto* client : copyToVector(_clients))
-        client->uiAssertionWillExpireImminently();
+    for (auto* assertion : copyToVector(_assertionsNeedingBackgroundTask))
+        assertion->uiAssertionWillExpireImminently();
 }
 
 - (void)_updateBackgroundTask
 {
-    if (_needsToRunInBackgroundCount && _backgroundTask == UIBackgroundTaskInvalid) {
+    if (!_assertionsNeedingBackgroundTask.isEmpty() && _backgroundTask == UIBackgroundTaskInvalid) {
         RELEASE_LOG(ProcessSuspension, "%p - WKProcessAssertionBackgroundTaskManager - beginBackgroundTaskWithName", self);
         _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"com.apple.WebKit.ProcessAssertion" expirationHandler:^{
             RELEASE_LOG_ERROR(ProcessSuspension, "Background task expired while holding WebKit ProcessAssertion (isMainThread? %d).", RunLoop::isMain());
             // The expiration handler gets called on a non-main thread when the underlying assertion could not be taken (rdar://problem/27278419).
             if (RunLoop::isMain())
-                [self _notifyClientsOfImminentSuspension];
+                [self _notifyAssertionsOfImminentSuspension];
             else {
                 dispatch_sync(dispatch_get_main_queue(), ^{
-                    [self _notifyClientsOfImminentSuspension];
+                    [self _notifyAssertionsOfImminentSuspension];
                 });
             }
-            [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
-            _backgroundTask = UIBackgroundTaskInvalid;
+            [self _releaseBackgroundTask];
         }];
-    }
-
-    if (!_needsToRunInBackgroundCount && _backgroundTask != UIBackgroundTaskInvalid) {
-        RELEASE_LOG(ProcessSuspension, "%p - WKProcessAssertionBackgroundTaskManager - endBackgroundTask", self);
-        [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
-        _backgroundTask = UIBackgroundTaskInvalid;
-    }
+    } else if (_assertionsNeedingBackgroundTask.isEmpty())
+        [self _releaseBackgroundTask];
 }
 
-- (void)incrementNeedsToRunInBackgroundCount
+- (void)_releaseBackgroundTask
 {
-    ++_needsToRunInBackgroundCount;
-    [self _updateBackgroundTask];
-}
+    if (_backgroundTask == UIBackgroundTaskInvalid)
+        return;
 
-- (void)decrementNeedsToRunInBackgroundCount
-{
-    --_needsToRunInBackgroundCount;
-    [self _updateBackgroundTask];
+    RELEASE_LOG(ProcessSuspension, "%p - WKProcessAssertionBackgroundTaskManager - endBackgroundTask", self);
+    [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
+    _backgroundTask = UIBackgroundTaskInvalid;
 }
 
 @end
@@ -172,14 +158,8 @@ static BKSProcessAssertionReason reasonForState(AssertionState assertionState)
     }
 }
 
-ProcessAssertion::ProcessAssertion(pid_t pid, AssertionState assertionState, Function<void()>&& invalidationCallback)
-    : ProcessAssertion(pid, "Web content visibility"_s, assertionState, WTFMove(invalidationCallback))
-{
-}
-
-ProcessAssertion::ProcessAssertion(pid_t pid, const String& name, AssertionState assertionState, Function<void()>&& invalidationCallback)
-    : m_invalidationCallback(WTFMove(invalidationCallback))
-    , m_assertionState(assertionState)
+ProcessAssertion::ProcessAssertion(pid_t pid, const String& name, AssertionState assertionState)
+    : m_assertionState(assertionState)
 {
     auto weakThis = makeWeakPtr(*this);
     BKSProcessAssertionAcquisitionHandler handler = ^(BOOL acquired) {
@@ -188,7 +168,7 @@ ProcessAssertion::ProcessAssertion(pid_t pid, const String& name, AssertionState
             ASSERT_NOT_REACHED();
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (weakThis)
-                    markAsInvalidated();
+                    processAssertionWasInvalidated();
             });
         }
     };
@@ -199,7 +179,7 @@ ProcessAssertion::ProcessAssertion(pid_t pid, const String& name, AssertionState
         dispatch_async(dispatch_get_main_queue(), ^{
             RELEASE_LOG(ProcessSuspension, "%p - ProcessAssertion() Process assertion for process with PID %d was invalidated", this, pid);
             if (weakThis)
-                markAsInvalidated();
+                processAssertionWasInvalidated();
         });
     };
 }
@@ -212,13 +192,12 @@ ProcessAssertion::~ProcessAssertion()
     [m_assertion invalidate];
 }
 
-void ProcessAssertion::markAsInvalidated()
+void ProcessAssertion::processAssertionWasInvalidated()
 {
     ASSERT(RunLoop::isMain());
+    RELEASE_LOG_ERROR(ProcessSuspension, "%p - ProcessAssertion::processAssertionWasInvalidated()", this);
 
     m_validity = Validity::No;
-    if (m_invalidationCallback)
-        m_invalidationCallback();
 }
 
 void ProcessAssertion::setState(AssertionState assertionState)
@@ -226,39 +205,35 @@ void ProcessAssertion::setState(AssertionState assertionState)
     if (m_assertionState == assertionState)
         return;
 
-    RELEASE_LOG(ProcessSuspension, "%p - ProcessAssertion::setState(%u)", this, static_cast<unsigned>(assertionState));
+    RELEASE_LOG(ProcessSuspension, "%p - ProcessAssertion::setState(%u) previousState: %u", this, static_cast<unsigned>(assertionState), static_cast<unsigned>(m_assertionState));
     m_assertionState = assertionState;
     [m_assertion setFlags:flagsForState(assertionState)];
 }
 
 void ProcessAndUIAssertion::updateRunInBackgroundCount()
 {
-    bool shouldHoldBackgroundAssertion = validity() != Validity::No && state() != AssertionState::Suspended;
-
-    if (shouldHoldBackgroundAssertion) {
-        if (!m_isHoldingBackgroundAssertion)
-            [[WKProcessAssertionBackgroundTaskManager shared] incrementNeedsToRunInBackgroundCount];
-    } else {
-        if (m_isHoldingBackgroundAssertion)
-            [[WKProcessAssertionBackgroundTaskManager shared] decrementNeedsToRunInBackgroundCount];
-    }
+    bool shouldHoldBackgroundTask = validity() != Validity::No && state() != AssertionState::Suspended;
+    if (m_isHoldingBackgroundTask == shouldHoldBackgroundTask)
+        return;
+
+    if (shouldHoldBackgroundTask)
+        [[WKProcessAssertionBackgroundTaskManager shared] addAssertionNeedingBackgroundTask:*this];
+    else
+        [[WKProcessAssertionBackgroundTaskManager shared] removeAssertionNeedingBackgroundTask:*this];
 
-    m_isHoldingBackgroundAssertion = shouldHoldBackgroundAssertion;
+    m_isHoldingBackgroundTask = shouldHoldBackgroundTask;
 }
 
-ProcessAndUIAssertion::ProcessAndUIAssertion(pid_t pid, AssertionState assertionState)
-    : ProcessAssertion(pid, assertionState, [this] { updateRunInBackgroundCount(); })
+ProcessAndUIAssertion::ProcessAndUIAssertion(pid_t pid, const String& reason, AssertionState assertionState)
+    : ProcessAssertion(pid, reason, assertionState)
 {
     updateRunInBackgroundCount();
 }
 
 ProcessAndUIAssertion::~ProcessAndUIAssertion()
 {
-    if (m_isHoldingBackgroundAssertion)
-        [[WKProcessAssertionBackgroundTaskManager shared] decrementNeedsToRunInBackgroundCount];
-
-    if (auto* client = this->client())
-        [[WKProcessAssertionBackgroundTaskManager shared] removeClient:*client];
+    if (m_isHoldingBackgroundTask)
+        [[WKProcessAssertionBackgroundTaskManager shared] removeAssertionNeedingBackgroundTask:*this];
 }
 
 void ProcessAndUIAssertion::setState(AssertionState assertionState)
@@ -267,60 +242,18 @@ void ProcessAndUIAssertion::setState(AssertionState assertionState)
     updateRunInBackgroundCount();
 }
 
-void ProcessAndUIAssertion::setClient(ProcessAssertionClient& newClient)
-{
-    [[WKProcessAssertionBackgroundTaskManager shared] addClient:newClient];
-    if (auto* oldClient = this->client())
-        [[WKProcessAssertionBackgroundTaskManager shared] removeClient:*oldClient];
-    ProcessAssertion::setClient(newClient);
-}
-
-} // namespace WebKit
-
-#else // PLATFORM(IOS_FAMILY_SIMULATOR)
-
-namespace WebKit {
-
-ProcessAssertion::ProcessAssertion(pid_t, AssertionState assertionState, Function<void()>&&)
-    : m_assertionState(assertionState)
-{
-}
-
-ProcessAssertion::ProcessAssertion(pid_t, const String&, AssertionState assertionState, Function<void()>&&)
-    : m_assertionState(assertionState)
-{
-}
-
-ProcessAssertion::~ProcessAssertion()
-{
-}
-
-void ProcessAssertion::setState(AssertionState assertionState)
+void ProcessAndUIAssertion::uiAssertionWillExpireImminently()
 {
-    m_assertionState = assertionState;
-}
-
-ProcessAndUIAssertion::ProcessAndUIAssertion(pid_t pid, AssertionState assertionState)
-    : ProcessAssertion(pid, assertionState)
-{
-}
-
-ProcessAndUIAssertion::~ProcessAndUIAssertion()
-{
-}
-
-void ProcessAndUIAssertion::setState(AssertionState assertionState)
-{
-    ProcessAssertion::setState(assertionState);
+    if (auto* client = this->client())
+        client->uiAssertionWillExpireImminently();
 }
 
-void ProcessAndUIAssertion::setClient(ProcessAssertionClient& newClient)
+void ProcessAndUIAssertion::processAssertionWasInvalidated()
 {
-    ProcessAssertion::setClient(newClient);
+    ProcessAssertion::processAssertionWasInvalidated();
+    updateRunInBackgroundCount();
 }
 
 } // namespace WebKit
 
-#endif // PLATFORM(IOS_FAMILY_SIMULATOR)
-
-#endif // PLATFORM(IOS_FAMILY)
+#endif // PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)