Optimize out most of LastUserInputTime() calls.

Fixes #16118.
This commit is contained in:
John Preston 2021-06-18 19:22:36 +04:00
parent 8897f9e46a
commit e1120d1cb5
12 changed files with 53 additions and 32 deletions

View file

@ -862,8 +862,8 @@ int32 Updates::pts() const {
return _ptsWaiter.current();
}
void Updates::updateOnline() {
updateOnline(false);
void Updates::updateOnline(crl::time lastNonIdleTime) {
updateOnline(lastNonIdleTime, false);
}
bool Updates::isIdle() const {
@ -874,15 +874,20 @@ rpl::producer<bool> Updates::isIdleValue() const {
return _isIdle.value();
}
void Updates::updateOnline(bool gotOtherOffline) {
crl::on_main(&session(), [] { Core::App().checkAutoLock(); });
void Updates::updateOnline(crl::time lastNonIdleTime, bool gotOtherOffline) {
if (!lastNonIdleTime) {
lastNonIdleTime = Core::App().lastNonIdleTime();
}
crl::on_main(&session(), [=] {
Core::App().checkAutoLock(lastNonIdleTime);
});
const auto &config = _session->serverConfig();
bool isOnline = Core::App().hasActiveWindow(&session());
int updateIn = config.onlineUpdatePeriod;
Assert(updateIn >= 0);
if (isOnline) {
const auto idle = crl::now() - Core::App().lastNonIdleTime();
const auto idle = crl::now() - lastNonIdleTime;
if (idle >= config.offlineIdleTimeout) {
isOnline = false;
if (!isIdle()) {
@ -933,10 +938,13 @@ void Updates::updateOnline(bool gotOtherOffline) {
_onlineTimer.callOnce(updateIn);
}
void Updates::checkIdleFinish() {
if (crl::now() - Core::App().lastNonIdleTime()
void Updates::checkIdleFinish(crl::time lastNonIdleTime) {
if (!lastNonIdleTime) {
lastNonIdleTime = Core::App().lastNonIdleTime();
}
if (crl::now() - lastNonIdleTime
< _session->serverConfig().offlineIdleTimeout) {
updateOnline();
updateOnline(lastNonIdleTime);
_idleFinishTimer.cancel();
_isIdle = false;
} else {
@ -957,9 +965,10 @@ bool Updates::isQuitPrevent() {
return false;
}
LOG(("Api::Updates prevents quit, sending offline status..."));
updateOnline();
updateOnline(crl::now());
return true;
}
void Updates::handleSendActionUpdate(
PeerId peerId,
MsgId rootId,
@ -1750,7 +1759,7 @@ void Updates::feedUpdate(const MTPUpdate &update) {
if (UserId(d.vuser_id()) == session().userId()) {
if (d.vstatus().type() == mtpc_userStatusOffline
|| d.vstatus().type() == mtpc_userStatusEmpty) {
updateOnline(true);
updateOnline(Core::App().lastNonIdleTime(), true);
if (d.vstatus().type() == mtpc_userStatusOffline) {
cSetOtherOnline(
d.vstatus().c_userStatusOffline().vwas_online().v);

View file

@ -38,10 +38,10 @@ public:
[[nodiscard]] int32 pts() const;
void updateOnline();
void updateOnline(crl::time lastNonIdleTime = 0);
[[nodiscard]] bool isIdle() const;
[[nodiscard]] rpl::producer<bool> isIdleValue() const;
void checkIdleFinish();
void checkIdleFinish(crl::time lastNonIdleTime = 0);
bool lastWasOnline() const;
crl::time lastSetOnline() const;
bool isQuitPrevent();
@ -87,7 +87,7 @@ private:
MsgRange range,
const MTPupdates_ChannelDifference &result);
void updateOnline(bool gotOtherOffline);
void updateOnline(crl::time lastNonIdleTime, bool gotOtherOffline);
void sendPing();
void getDifferenceByPts();
void getDifferenceAfterFail();

View file

@ -45,6 +45,6 @@ void AutoLockBox::durationChanged(int seconds) {
Core::App().settings().setAutoLock(seconds);
Core::App().saveSettingsDelayed();
Core::App().checkAutoLock();
Core::App().checkAutoLock(crl::now());
closeBox();
}

View file

@ -849,7 +849,7 @@ bool Application::passcodeLocked() const {
void Application::updateNonIdle() {
_lastNonIdleTime = crl::now();
if (const auto session = maybeActiveSession()) {
session->updates().checkIdleFinish();
session->updates().checkIdleFinish(_lastNonIdleTime);
}
}
@ -876,19 +876,21 @@ bool Application::someSessionExists() const {
return false;
}
void Application::checkAutoLock() {
void Application::checkAutoLock(crl::time lastNonIdleTime) {
if (!_domain->local().hasLocalPasscode()
|| passcodeLocked()
|| !someSessionExists()) {
_shouldLockAt = 0;
_autoLockTimer.cancel();
return;
} else if (!lastNonIdleTime) {
lastNonIdleTime = this->lastNonIdleTime();
}
checkLocalTime();
const auto now = crl::now();
const auto shouldLockInMs = _settings.autoLock() * 1000LL;
const auto checkTimeMs = now - lastNonIdleTime();
const auto checkTimeMs = now - lastNonIdleTime;
if (checkTimeMs >= shouldLockInMs || (_shouldLockAt > 0 && now > _shouldLockAt + kAutoLockTimeoutLateMs)) {
_shouldLockAt = 0;
_autoLockTimer.cancel();
@ -910,7 +912,7 @@ void Application::checkAutoLockIn(crl::time time) {
void Application::localPasscodeChanged() {
_shouldLockAt = 0;
_autoLockTimer.cancel();
checkAutoLock();
checkAutoLock(crl::now());
}
bool Application::hasActiveWindow(not_null<Main::Session*> session) const {

View file

@ -245,7 +245,7 @@ public:
rpl::producer<bool> passcodeLockChanges() const;
rpl::producer<bool> passcodeLockValue() const;
void checkAutoLock();
void checkAutoLock(crl::time lastNonIdleTime = 0);
void checkAutoLockIn(crl::time time);
void localPasscodeChanged();

View file

@ -945,7 +945,7 @@ void MainWindow::sendPaths() {
}
}
void MainWindow::updateIsActiveHook() {
void MainWindow::activeChangedHook() {
if (const auto controller = sessionController()) {
controller->session().updates().updateOnline();
}

View file

@ -114,7 +114,7 @@ protected:
void closeEvent(QCloseEvent *e) override;
void initHook() override;
void updateIsActiveHook() override;
void activeChangedHook() override;
void clearWidgetsHook() override;
private:

View file

@ -237,8 +237,11 @@ void MainWindow::clearWidgets() {
}
void MainWindow::updateIsActive() {
_isActive = computeIsActive();
updateIsActiveHook();
const auto isActive = computeIsActive();
if (_isActive != isActive) {
_isActive = isActive;
activeChangedHook();
}
}
bool MainWindow::computeIsActive() const {

View file

@ -139,7 +139,7 @@ protected:
virtual void initHook() {
}
virtual void updateIsActiveHook() {
virtual void activeChangedHook() {
}
virtual void handleActiveChangedHook() {

View file

@ -158,8 +158,11 @@ float64 Manager::demoMasterOpacity() const {
void Manager::checkLastInput() {
auto replying = hasReplyingNotification();
auto waiting = false;
for_const (auto &notification, _notifications) {
if (!notification->checkLastInput(replying)) {
const auto lastInputTime = base::Platform::LastUserInputTimeSupported()
? std::make_optional(Core::App().lastNonIdleTime())
: std::nullopt;
for (const auto &notification : _notifications) {
if (!notification->checkLastInput(replying, lastInputTime)) {
waiting = true;
}
}
@ -682,12 +685,14 @@ void Notification::prepareActionsCache() {
_buttonsCache = App::pixmapFromImageInPlace(std::move(actionsCacheImg));
}
bool Notification::checkLastInput(bool hasReplyingNotifications) {
bool Notification::checkLastInput(
bool hasReplyingNotifications,
std::optional<crl::time> lastInputTime) {
if (!_waitingForInput) return true;
const auto waitForUserInput = base::Platform::LastUserInputTimeSupported()
? (Core::App().lastNonIdleTime() <= _started)
: false;
const auto waitForUserInput = lastInputTime.has_value()
&& (*lastInputTime <= _started);
if (!waitForUserInput) {
_waitingForInput = false;
if (!hasReplyingNotifications) {

View file

@ -232,7 +232,9 @@ public:
bool unlinkItem(HistoryItem *del);
bool unlinkHistory(History *history = nullptr);
bool unlinkSession(not_null<Main::Session*> session);
bool checkLastInput(bool hasReplyingNotifications);
bool checkLastInput(
bool hasReplyingNotifications,
std::optional<crl::time> lastInputTime);
protected:
void enterEventHook(QEvent *e) override;

View file

@ -67,7 +67,7 @@ void Controller::showAccount(not_null<Main::Account*> account) {
for (auto &[index, account] : _account->domain().accounts()) {
if (const auto anotherSession = account->maybeSession()) {
if (anotherSession->uniqueId() == prevSessionUniqueId) {
anotherSession->updates().updateOnline();
anotherSession->updates().updateOnline(crl::now());
return;
}
}