Skip to content

Commit

Permalink
Move passcode management from MainWindow.
Browse files Browse the repository at this point in the history
Check for auto lock in AuthSession. Don't autolock while video plays.
Closes telegramdesktop#3219
  • Loading branch information
john-preston committed Apr 15, 2017
1 parent e3aacc8 commit de7c886
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 52 deletions.
39 changes: 38 additions & 1 deletion Telegram/SourceFiles/auth_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "storage/file_download.h"
#include "storage/localstorage.h"
#include "window/notifications_manager.h"
#include "platform/platform_specific.h"

namespace {

constexpr auto kAutoLockTimeoutLateMs = TimeMs(3000);

} // namespace

QByteArray AuthSessionData::serialize() const {
auto size = sizeof(qint32) * 2;
Expand Down Expand Up @@ -85,11 +92,16 @@ AuthSession::AuthSession(UserId userId)
: _userId(userId)
, _api(std::make_unique<ApiWrap>())
, _downloader(std::make_unique<Storage::Downloader>())
, _notifications(std::make_unique<Window::Notifications::System>(this)) {
, _notifications(std::make_unique<Window::Notifications::System>(this))
, _autoLockTimer([this] { checkAutoLock(); }) {
Expects(_userId != 0);
_saveDataTimer.setCallback([this] {
Local::writeUserSettings();
});
subscribe(Messenger::Instance().passcodedChanged(), [this] {
_shouldLockAt = 0;
notifications().updateAll();
});
}

bool AuthSession::Exists() {
Expand Down Expand Up @@ -127,4 +139,29 @@ void AuthSession::saveDataDelayed(TimeMs delay) {
_saveDataTimer.callOnce(delay);
}

void AuthSession::checkAutoLock() {
if (!Global::LocalPasscode() || App::passcoded()) return;

Messenger::Instance().checkLocalTime();
auto now = getms(true);
auto shouldLockInMs = Global::AutoLock() * 1000LL;
auto idleForMs = psIdleTime();
auto notPlayingVideoForMs = now - data().lastTimeVideoPlayedAt();
auto checkTimeMs = qMin(idleForMs, notPlayingVideoForMs);
if (checkTimeMs >= shouldLockInMs || (_shouldLockAt > 0 && now > _shouldLockAt + kAutoLockTimeoutLateMs)) {
Messenger::Instance().setupPasscode();
} else {
_shouldLockAt = now + (shouldLockInMs - checkTimeMs);
_autoLockTimer.callOnce(shouldLockInMs - checkTimeMs);
}
}

void AuthSession::checkAutoLockIn(TimeMs time) {
if (_autoLockTimer.isActive()) {
auto remain = _autoLockTimer.remainingTime();
if (remain > 0 && remain <= time) return;
}
_autoLockTimer.callOnce(time);
}

AuthSession::~AuthSession() = default;
15 changes: 14 additions & 1 deletion Telegram/SourceFiles/auth_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ class AuthSessionData final {
void setTabbedSelectorSectionEnabled(bool enabled) {
_variables.tabbedSelectorSectionEnabled = enabled;
}
void setLastTimeVideoPlayedAt(TimeMs time) {
_lastTimeVideoPlayedAt = time;
}
TimeMs lastTimeVideoPlayedAt() const {
return _lastTimeVideoPlayedAt;
}

private:
struct Variables {
Expand All @@ -92,10 +98,11 @@ class AuthSessionData final {
base::Observable<void> _moreChatsLoaded;
base::Observable<void> _savedGifsUpdated;
Variables _variables;
TimeMs _lastTimeVideoPlayedAt = 0;

};

class AuthSession final {
class AuthSession final : private base::Subscriber {
public:
AuthSession(UserId userId);

Expand Down Expand Up @@ -137,13 +144,19 @@ class AuthSession final {
return *_api;
}

void checkAutoLock();
void checkAutoLockIn(TimeMs time);

~AuthSession();

private:
const UserId _userId = 0;
AuthSessionData _data;
base::Timer _saveDataTimer;

TimeMs _shouldLockAt = 0;
base::Timer _autoLockTimer;

const std::unique_ptr<ApiWrap> _api;
const std::unique_ptr<Storage::Downloader> _downloader;
const std::unique_ptr<Window::Notifications::System> _notifications;
Expand Down
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/boxes/autolock_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ void AutoLockBox::durationChanged(int seconds) {
Local::writeUserSettings();
Global::RefLocalPasscodeChanged().notify();

App::wnd()->checkAutoLock();
AuthSession::Current().checkAutoLock();
closeBox();
}
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/boxes/passcode_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void PasscodeBox::onSave(bool force) {
} else {
cSetPasscodeBadTries(0);
Local::setPasscode(pwd.toUtf8());
App::wnd()->checkAutoLock();
AuthSession::Current().checkAutoLock();
closeBox();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Telegram/SourceFiles/dialogswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "autoupdater.h"
#include "observer_peer.h"
#include "auth_session.h"
#include "messenger.h"
#include "window/notifications_manager.h"
#include "ui/effects/widget_fade_wrap.h"
#include "window/window_controller.h"
Expand Down Expand Up @@ -2305,7 +2306,7 @@ DialogsWidget::DialogsWidget(QWidget *parent, gsl::not_null<Window::Controller*>
subscribe(Global::RefLocalPasscodeChanged(), [this] { updateLockUnlockVisibility(); });
_lockUnlock->setClickedCallback([this] {
_lockUnlock->setIconOverride(&st::dialogsUnlockIcon, &st::dialogsUnlockIconOver);
App::wnd()->setupPasscode();
Messenger::Instance().setupPasscode();
_lockUnlock->setIconOverride(nullptr);
});
_mainMenuToggle->setClickedCallback([this] { showMainMenu(); });
Expand Down
6 changes: 3 additions & 3 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3501,7 +3501,7 @@ void MainWidget::ptsApplySkippedUpdates() {
}

void MainWidget::feedDifference(const MTPVector<MTPUser> &users, const MTPVector<MTPChat> &chats, const MTPVector<MTPMessage> &msgs, const MTPVector<MTPUpdate> &other) {
App::wnd()->checkAutoLock();
AuthSession::Current().checkAutoLock();
App::feedUsers(users);
App::feedChats(chats);
feedMessageIds(other);
Expand Down Expand Up @@ -4167,7 +4167,7 @@ MainWidget::~MainWidget() {

void MainWidget::updateOnline(bool gotOtherOffline) {
if (this != App::main()) return;
App::wnd()->checkAutoLock();
AuthSession::Current().checkAutoLock();

bool isOnline = App::wnd()->isActive();
int updateIn = Global::OnlineUpdatePeriod();
Expand Down Expand Up @@ -4262,7 +4262,7 @@ void MainWidget::checkIdleFinish() {
void MainWidget::updateReceived(const mtpPrime *from, const mtpPrime *end) {
if (end <= from) return;

App::wnd()->checkAutoLock();
AuthSession::Current().checkAutoLock();

if (mtpTypeId(*from) == mtpc_new_session_created) {
try {
Expand Down
35 changes: 2 additions & 33 deletions Telegram/SourceFiles/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,12 @@ MainWindow::MainWindow() {
_inactiveTimer.setSingleShot(true);
connect(&_inactiveTimer, SIGNAL(timeout()), this, SLOT(onInactiveTimer()));

connect(&_autoLockTimer, SIGNAL(timeout()), this, SLOT(checkAutoLock()));

subscribe(Global::RefSelfChanged(), [this] { updateGlobalMenu(); });
subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &data) {
themeUpdated(data);
});
subscribe(Messenger::Instance().authSessionChanged(), [this] { checkAuthSession(); });
subscribe(Messenger::Instance().passcodedChanged(), [this] { updateGlobalMenu(); });
checkAuthSession();

setAttribute(Qt::WA_NoSystemBackground);
Expand Down Expand Up @@ -237,12 +236,8 @@ void MainWindow::clearPasscode() {
if (_intro) {
_intro->showAnimated(bg, true);
} else {
t_assert(_main != nullptr);
_main->showAnimated(bg, true);
}
AuthSession::Current().notifications().updateAll();
updateGlobalMenu();

if (_main) {
_main->checkStartUrl();
}
}
Expand All @@ -262,32 +257,6 @@ void MainWindow::setupPasscode() {
} else {
setInnerFocus();
}
_shouldLockAt = 0;
if (AuthSession::Exists()) {
AuthSession::Current().notifications().updateAll();
}
updateGlobalMenu();
}

void MainWindow::checkAutoLockIn(int msec) {
if (_autoLockTimer.isActive()) {
int remain = _autoLockTimer.remainingTime();
if (remain > 0 && remain <= msec) return;
}
_autoLockTimer.start(msec);
}

void MainWindow::checkAutoLock() {
if (!Global::LocalPasscode() || App::passcoded()) return;

App::app()->checkLocalTime();
auto ms = getms(true), idle = psIdleTime(), should = Global::AutoLock() * 1000LL;
if (idle >= should || (_shouldLockAt > 0 && ms > _shouldLockAt + 3000LL)) {
setupPasscode();
} else {
_shouldLockAt = ms + (should - idle);
_autoLockTimer.start(should - idle);
}
}

void MainWindow::setupIntro() {
Expand Down
6 changes: 0 additions & 6 deletions Telegram/SourceFiles/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class MainWindow : public Platform::MainWindow {

void setupPasscode();
void clearPasscode();
void checkAutoLockIn(int msec);
void setupIntro();
void setupMain(const MTPUser *user = nullptr);
void serviceNotification(const TextWithEntities &message, const MTPMessageMedia &media = MTP_messageMediaEmpty(), int32 date = 0, bool force = false);
Expand Down Expand Up @@ -164,8 +163,6 @@ class MainWindow : public Platform::MainWindow {
void updateControlsGeometry() override;

public slots:
void checkAutoLock();

void showSettings();
void layerHidden();
void setInnerFocus();
Expand Down Expand Up @@ -238,9 +235,6 @@ private slots:
bool _inactivePress = false;
QTimer _inactiveTimer;

SingleTimer _autoLockTimer;
TimeMs _shouldLockAt = 0;

};

class PreLaunchWindow : public TWidget {
Expand Down
2 changes: 2 additions & 0 deletions Telegram/SourceFiles/mediaview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,8 @@ void MediaView::onVideoPlayProgress(const AudioMsgId &audioId) {
if (state.duration) {
updateVideoPlaybackState(state);
}

AuthSession::Current().data().setLastTimeVideoPlayedAt(getms(true));
}

void MediaView::updateVideoPlaybackState(const Media::Player::TrackState &state) {
Expand Down
13 changes: 12 additions & 1 deletion Telegram/SourceFiles/messenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Messenger::Messenger() : QObject()

DEBUG_LOG(("Application Info: showing."));
if (state == Local::ReadMapPassNeeded) {
_window->setupPasscode();
setupPasscode();
} else {
if (AuthSession::Exists()) {
_window->setupMain();
Expand Down Expand Up @@ -728,6 +728,17 @@ void Messenger::checkMapVersion() {
}
}

void Messenger::setupPasscode() {
_window->setupPasscode();
_passcodedChanged.notify();
}

void Messenger::clearPasscode() {
cSetPasscodeBadTries(0);
_window->clearPasscode();
_passcodedChanged.notify();
}

void Messenger::prepareToDestroy() {
_window.reset();

Expand Down
6 changes: 6 additions & 0 deletions Telegram/SourceFiles/messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class Messenger final : public QObject, public RPCSender, private base::Subscrib

void checkLocalTime();
void checkMapVersion();
void setupPasscode();
void clearPasscode();
base::Observable<void> &passcodedChanged() {
return _passcodedChanged;
}

void handleAppActivated();
void handleAppDeactivated();
Expand Down Expand Up @@ -161,5 +166,6 @@ public slots:
std::unique_ptr<MTP::Instance> _mtprotoForKeysDestroy;
std::unique_ptr<AuthSession> _authSession;
base::Observable<void> _authSessionChanged;
base::Observable<void> _passcodedChanged;

};
3 changes: 1 addition & 2 deletions Telegram/SourceFiles/passcodewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ void PasscodeWidget::onSubmit() {

if (App::main()) {
if (Local::checkPasscode(_passcode->text().toUtf8())) {
cSetPasscodeBadTries(0);
App::wnd()->clearPasscode(); // Destroys this widget.
Messenger::Instance().clearPasscode(); // Destroys this widget.
return;
} else {
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
Expand Down
5 changes: 4 additions & 1 deletion Telegram/SourceFiles/platform/win/windows_event_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "platform/win/windows_event_filter.h"

#include "mainwindow.h"
#include "auth_session.h"

namespace Platform {
namespace {
Expand Down Expand Up @@ -73,7 +74,9 @@ bool EventFilter::mainWindowEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPa
switch (msg) {

case WM_TIMECHANGE: {
App::wnd()->checkAutoLockIn(100);
if (AuthSession::Exists()) {
AuthSession::Current().checkAutoLockIn(100);
}
} return false;

case WM_WTSSESSION_CHANGE: {
Expand Down
3 changes: 2 additions & 1 deletion Telegram/SourceFiles/shortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "mainwindow.h"
#include "passcodewidget.h"
#include "mainwidget.h"
#include "messenger.h"
#include "media/player/media_player_instance.h"
#include "platform/platform_specific.h"
#include "base/parse_helper.h"
Expand All @@ -37,7 +38,7 @@ bool lock_telegram() {
w->passcodeWidget()->onSubmit();
return true;
} else if (Global::LocalPasscode()) {
w->setupPasscode();
Messenger::Instance().setupPasscode();
return true;
}
}
Expand Down

0 comments on commit de7c886

Please sign in to comment.