ProcessAssertion.h [plain text]
#ifndef ProcessAssertion_h
#define ProcessAssertion_h
#include <unistd.h>
#include <wtf/Function.h>
#if PLATFORM(IOS) && !PLATFORM(IOS_SIMULATOR)
#include <wtf/RetainPtr.h>
#include <wtf/WeakPtr.h>
OBJC_CLASS BKSProcessAssertion;
#endif
namespace WebKit {
enum class AssertionState {
Suspended,
Background,
Foreground
};
class ProcessAssertionClient {
public:
virtual ~ProcessAssertionClient() { };
virtual void assertionWillExpireImminently() = 0;
};
class ProcessAssertion {
public:
ProcessAssertion(pid_t, AssertionState, Function<void()>&& invalidationCallback = { });
virtual ~ProcessAssertion();
virtual void setClient(ProcessAssertionClient& client) { m_client = &client; }
ProcessAssertionClient* client() { return m_client; }
AssertionState state() const { return m_assertionState; }
virtual void setState(AssertionState);
#if PLATFORM(IOS) && !PLATFORM(IOS_SIMULATOR)
protected:
enum class Validity { No, Yes, Unset };
Validity validity() const { return m_validity; }
#endif
private:
#if PLATFORM(IOS) && !PLATFORM(IOS_SIMULATOR)
WeakPtr<ProcessAssertion> createWeakPtr() { return m_weakFactory.createWeakPtr(); }
void markAsInvalidated();
RetainPtr<BKSProcessAssertion> m_assertion;
Validity m_validity { Validity::Unset };
WeakPtrFactory<ProcessAssertion> m_weakFactory;
Function<void()> m_invalidationCallback;
#endif
AssertionState m_assertionState;
ProcessAssertionClient* m_client { nullptr };
};
class ProcessAndUIAssertion final : public ProcessAssertion {
public:
ProcessAndUIAssertion(pid_t, AssertionState);
~ProcessAndUIAssertion();
void setClient(ProcessAssertionClient&) final;
void setState(AssertionState) final;
#if PLATFORM(IOS) && !PLATFORM(IOS_SIMULATOR)
private:
void updateRunInBackgroundCount();
bool m_isHoldingBackgroundAssertion { false };
#endif
};
}
#endif // ProcessAssertion_h