diff --git a/Telegram/SourceFiles/boxes/auto_lock_box.cpp b/Telegram/SourceFiles/boxes/auto_lock_box.cpp index 9cbfbbf25..bdde54b6e 100644 --- a/Telegram/SourceFiles/boxes/auto_lock_box.cpp +++ b/Telegram/SourceFiles/boxes/auto_lock_box.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "storage/localstorage.h" +#include "core/application.h" #include "mainwindow.h" #include "ui/widgets/checkbox.h" #include "facades.h" @@ -45,6 +46,6 @@ void AutoLockBox::durationChanged(int seconds) { Local::writeUserSettings(); Global::RefLocalPasscodeChanged().notify(); - _session->checkAutoLock(); + Core::App().checkAutoLock(); closeBox(); } diff --git a/Telegram/SourceFiles/boxes/passcode_box.cpp b/Telegram/SourceFiles/boxes/passcode_box.cpp index 96ce5c731..43cc168f2 100644 --- a/Telegram/SourceFiles/boxes/passcode_box.cpp +++ b/Telegram/SourceFiles/boxes/passcode_box.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwindow.h" #include "apiwrap.h" #include "main/main_session.h" +#include "core/application.h" #include "storage/localstorage.h" #include "ui/widgets/buttons.h" #include "ui/widgets/input_fields.h" @@ -519,7 +520,7 @@ void PasscodeBox::save(bool force) { const auto weak = Ui::MakeWeak(this); cSetPasscodeBadTries(0); Local::setPasscode(pwd.toUtf8()); - _session->localPasscodeChanged(); + Core::App().localPasscodeChanged(); if (weak) { closeBox(); } diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 80d871e66..d85c53496 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -75,7 +75,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Core { namespace { -constexpr auto kQuitPreventTimeoutMs = 1500; +constexpr auto kQuitPreventTimeoutMs = crl::time(1500); +constexpr auto kAutoLockTimeoutLateMs = crl::time(3000); } // namespace @@ -99,12 +100,18 @@ Application::Application(not_null launcher) , _emojiKeywords(std::make_unique()) , _audio(std::make_unique()) , _logo(Window::LoadLogo()) -, _logoNoMargin(Window::LoadLogoNoMargin()) { +, _logoNoMargin(Window::LoadLogoNoMargin()) +, _autoLockTimer([=] { checkAutoLock(); }) { Expects(!_logo.isNull()); Expects(!_logoNoMargin.isNull()); Ui::Integration::Set(&_private->uiIntegration); + passcodeLockChanges( + ) | rpl::start_with_next([=] { + _shouldLockAt = 0; + }, _lifetime); + activeAccount().sessionChanges( ) | rpl::start_with_next([=] { if (_mediaView) { @@ -697,6 +704,43 @@ void Application::lockByTerms(const Window::TermsLock &data) { } } +void Application::checkAutoLock() { + if (!Global::LocalPasscode() + || passcodeLocked() + || !_account->sessionExists()) { + _shouldLockAt = 0; + _autoLockTimer.cancel(); + return; + } + + checkLocalTime(); + const auto now = crl::now(); + const auto shouldLockInMs = Global::AutoLock() * 1000LL; + const auto checkTimeMs = now - lastNonIdleTime(); + if (checkTimeMs >= shouldLockInMs || (_shouldLockAt > 0 && now > _shouldLockAt + kAutoLockTimeoutLateMs)) { + _shouldLockAt = 0; + _autoLockTimer.cancel(); + lockByPasscode(); + } else { + _shouldLockAt = now + (shouldLockInMs - checkTimeMs); + _autoLockTimer.callOnce(shouldLockInMs - checkTimeMs); + } +} + +void Application::checkAutoLockIn(crl::time time) { + if (_autoLockTimer.isActive()) { + auto remain = _autoLockTimer.remainingTime(); + if (remain > 0 && remain <= time) return; + } + _autoLockTimer.callOnce(time); +} + +void Application::localPasscodeChanged() { + _shouldLockAt = 0; + _autoLockTimer.cancel(); + checkAutoLock(); +} + void Application::unlockTerms() { if (_termsLock) { _termsLock = nullptr; diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index b3be0aa4b..f0733ab79 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -199,6 +199,10 @@ public: rpl::producer lockChanges() const; rpl::producer lockValue() const; + void checkAutoLock(); + void checkAutoLockIn(crl::time time); + void localPasscodeChanged(); + [[nodiscard]] crl::time lastNonIdleTime() const; void updateNonIdle(); @@ -288,6 +292,9 @@ private: rpl::event_stream _termsLockChanges; std::unique_ptr _termsLock; + crl::time _shouldLockAt = 0; + base::Timer _autoLockTimer; + base::Timer _saveSettingsTimer; struct LeaveSubscription { diff --git a/Telegram/SourceFiles/main/main_session.cpp b/Telegram/SourceFiles/main/main_session.cpp index 6a1b1538a..23a3798f0 100644 --- a/Telegram/SourceFiles/main/main_session.cpp +++ b/Telegram/SourceFiles/main/main_session.cpp @@ -35,7 +35,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Main { namespace { -constexpr auto kAutoLockTimeoutLateMs = crl::time(3000); constexpr auto kLegacyCallsPeerToPeerNobody = 4; } // namespace @@ -47,7 +46,6 @@ Session::Session( : _account(account) , _settings(std::move(settings)) , _saveSettingsTimer([=] { Local::writeUserSettings(); }) -, _autoLockTimer([=] { checkAutoLock(); }) , _api(std::make_unique(this)) , _calls(std::make_unique(this)) , _downloader(std::make_unique(_api.get())) @@ -60,10 +58,6 @@ Session::Session( , _diceStickersPacks(std::make_unique(this)) , _changelogs(Core::Changelogs::Create(this)) , _supportHelper(Support::Helper::Create(this)) { - Core::App().passcodeLockChanges( - ) | rpl::start_with_next([=] { - _shouldLockAt = 0; - }, _lifetime); Core::App().lockChanges( ) | rpl::start_with_next([=] { notifications().updateAll(); @@ -157,48 +151,12 @@ not_null Session::mtp() { return _account->mtp(); } -void Session::localPasscodeChanged() { - _shouldLockAt = 0; - _autoLockTimer.cancel(); - checkAutoLock(); -} - void Session::termsDeleteNow() { api().request(MTPaccount_DeleteAccount( MTP_string("Decline ToS update") )).send(); } -void Session::checkAutoLock() { - if (!Global::LocalPasscode() - || Core::App().passcodeLocked()) { - _shouldLockAt = 0; - _autoLockTimer.cancel(); - return; - } - - Core::App().checkLocalTime(); - const auto now = crl::now(); - const auto shouldLockInMs = Global::AutoLock() * 1000LL; - const auto checkTimeMs = now - Core::App().lastNonIdleTime(); - if (checkTimeMs >= shouldLockInMs || (_shouldLockAt > 0 && now > _shouldLockAt + kAutoLockTimeoutLateMs)) { - _shouldLockAt = 0; - _autoLockTimer.cancel(); - Core::App().lockByPasscode(); - } else { - _shouldLockAt = now + (shouldLockInMs - checkTimeMs); - _autoLockTimer.callOnce(shouldLockInMs - checkTimeMs); - } -} - -void Session::checkAutoLockIn(crl::time time) { - if (_autoLockTimer.isActive()) { - auto remain = _autoLockTimer.remainingTime(); - if (remain > 0 && remain <= time) return; - } - _autoLockTimer.callOnce(time); -} - bool Session::supportMode() const { return (_supportHelper != nullptr); } diff --git a/Telegram/SourceFiles/main/main_session.h b/Telegram/SourceFiles/main/main_session.h index 87b747c73..7d68d27c2 100644 --- a/Telegram/SourceFiles/main/main_session.h +++ b/Telegram/SourceFiles/main/main_session.h @@ -121,9 +121,6 @@ public: return *_calls; } - void checkAutoLock(); - void checkAutoLockIn(crl::time time); - void localPasscodeChanged(); void termsDeleteNow(); [[nodiscard]] rpl::lifetime &lifetime() { @@ -145,9 +142,6 @@ private: Settings _settings; base::Timer _saveSettingsTimer; - crl::time _shouldLockAt = 0; - base::Timer _autoLockTimer; - const std::unique_ptr _api; const std::unique_ptr _calls; const std::unique_ptr _downloader; diff --git a/Telegram/SourceFiles/main/main_settings.cpp b/Telegram/SourceFiles/main/main_settings.cpp index b9f1f07b1..37f3db278 100644 --- a/Telegram/SourceFiles/main/main_settings.cpp +++ b/Telegram/SourceFiles/main/main_settings.cpp @@ -18,7 +18,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Main { namespace { -constexpr auto kAutoLockTimeoutLateMs = crl::time(3000); constexpr auto kLegacyCallsPeerToPeerNobody = 4; constexpr auto kVersionTag = -1; constexpr auto kVersion = 1; diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index 88ecbcf83..b804041ca 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -3061,7 +3061,7 @@ void MainWidget::feedDifference( const MTPVector &chats, const MTPVector &msgs, const MTPVector &other) { - session().checkAutoLock(); + Core::App().checkAutoLock(); session().data().processUsers(users); session().data().processChats(chats); feedMessageIds(other); @@ -3523,8 +3523,7 @@ MainWidget::~MainWidget() { } void MainWidget::updateOnline(bool gotOtherOffline) { - if (this != App::main()) return; - InvokeQueued(this, [=] { session().checkAutoLock(); }); + crl::on_main(this, [] { Core::App().checkAutoLock(); }); bool isOnline = !App::quitting() && App::wnd()->isActive(); int updateIn = Global::OnlineUpdatePeriod(); @@ -3660,7 +3659,7 @@ void MainWidget::checkIdleFinish() { } void MainWidget::mtpNewSessionCreated() { - session().checkAutoLock(); + Core::App().checkAutoLock(); updSeq = 0; MTP_LOG(0, ("getDifference { after new_session_created }%1" ).arg(cTestMode() ? " TESTMODE" : "")); @@ -3668,7 +3667,7 @@ void MainWidget::mtpNewSessionCreated() { } void MainWidget::mtpUpdateReceived(const MTPUpdates &updates) { - session().checkAutoLock(); + Core::App().checkAutoLock(); _lastUpdateTime = crl::now(); _noUpdatesTimer.callOnce(kNoUpdatesTimeout); if (!requestingDifference() diff --git a/Telegram/SourceFiles/platform/win/windows_event_filter.cpp b/Telegram/SourceFiles/platform/win/windows_event_filter.cpp index 511a7e57a..65aa3b0f9 100644 --- a/Telegram/SourceFiles/platform/win/windows_event_filter.cpp +++ b/Telegram/SourceFiles/platform/win/windows_event_filter.cpp @@ -9,9 +9,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/win/windows_dlls.h" #include "core/sandbox.h" +#include "core/application.h" #include "ui/inactive_press.h" #include "mainwindow.h" -#include "main/main_session.h" #include "facades.h" #include "app.h" @@ -95,9 +95,7 @@ bool EventFilter::mainWindowEvent( switch (msg) { case WM_TIMECHANGE: { - if (Main::Session::Exists()) { - Auth().checkAutoLockIn(100); - } + Core::App().checkAutoLockIn(100); } return false; case WM_WTSSESSION_CHANGE: {