Move autolock checking to Core::Application.

This commit is contained in:
John Preston 2020-06-08 19:56:52 +04:00
parent 4b354b0928
commit 27af83267e
9 changed files with 63 additions and 62 deletions

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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*> launcher)
, _emojiKeywords(std::make_unique<ChatHelpers::EmojiKeywords>())
, _audio(std::make_unique<Media::Audio::Instance>())
, _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;

View file

@ -199,6 +199,10 @@ public:
rpl::producer<bool> lockChanges() const;
rpl::producer<bool> 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<bool> _termsLockChanges;
std::unique_ptr<Window::TermsLock> _termsLock;
crl::time _shouldLockAt = 0;
base::Timer _autoLockTimer;
base::Timer _saveSettingsTimer;
struct LeaveSubscription {

View file

@ -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<ApiWrap>(this))
, _calls(std::make_unique<Calls::Instance>(this))
, _downloader(std::make_unique<Storage::DownloadManagerMtproto>(_api.get()))
@ -60,10 +58,6 @@ Session::Session(
, _diceStickersPacks(std::make_unique<Stickers::DicePacks>(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<MTP::Instance*> 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);
}

View file

@ -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<ApiWrap> _api;
const std::unique_ptr<Calls::Instance> _calls;
const std::unique_ptr<Storage::DownloadManagerMtproto> _downloader;

View file

@ -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;

View file

@ -3061,7 +3061,7 @@ void MainWidget::feedDifference(
const MTPVector<MTPChat> &chats,
const MTPVector<MTPMessage> &msgs,
const MTPVector<MTPUpdate> &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()

View file

@ -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: {