From 263bbf17888830113e4ee12f4104177b22991833 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 6 Jun 2019 12:37:12 +0300 Subject: [PATCH] Move _authSession to Main::Account. --- Telegram/SourceFiles/core/application.cpp | 10 ++---- Telegram/SourceFiles/core/application.h | 9 ------ Telegram/SourceFiles/main/main_account.cpp | 31 ++++++++++--------- Telegram/SourceFiles/main/main_account.h | 5 +++ Telegram/SourceFiles/mainwindow.cpp | 8 +++-- .../media/player/media_player_instance.cpp | 30 ++++++++---------- .../media/player/media_player_instance.h | 2 -- .../media/view/media_view_overlay_widget.cpp | 20 ++++++------ .../mtproto/dedicated_file_loader.cpp | 12 ++++--- .../mtproto/dedicated_file_loader.h | 1 + .../platform/mac/main_window_mac.mm | 8 +++-- Telegram/SourceFiles/window/main_window.cpp | 20 +++++------- Telegram/SourceFiles/window/main_window.h | 1 - .../window/themes/window_theme.cpp | 24 +++++++------- .../SourceFiles/window/themes/window_theme.h | 3 +- 15 files changed, 85 insertions(+), 99 deletions(-) diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp index 1141d25ae..f45ae50b7 100644 --- a/Telegram/SourceFiles/core/application.cpp +++ b/Telegram/SourceFiles/core/application.cpp @@ -786,8 +786,7 @@ void Application::authSessionCreate(const MTPUser &user) { return true; })); - _authSession = std::make_unique(&activeAccount(), user); - authSessionChanged().notify(true); + activeAccount().createSession(user); } void Application::authSessionDestroy() { @@ -798,12 +797,7 @@ void Application::authSessionDestroy() { unlockTerms(); _mtproto->clearGlobalHandlers(); - // Must be called before Auth().data() is destroyed, - // because streaming media holds pointers to it. - Media::Player::instance()->handleLogout(); - - _authSession = nullptr; - authSessionChanged().notify(true); + activeAccount().destroySession(); Notify::unreadCounterUpdated(); } diff --git a/Telegram/SourceFiles/core/application.h b/Telegram/SourceFiles/core/application.h index f0968dc12..ad846c6e1 100644 --- a/Telegram/SourceFiles/core/application.h +++ b/Telegram/SourceFiles/core/application.h @@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/auth_key.h" #include "base/timer.h" -class AuthSession; class AuthSessionSettings; class MainWidget; class FileUploader; @@ -158,13 +157,7 @@ public: } // AuthSession component. - AuthSession *authSession() { - return _authSession.get(); - } void authSessionCreate(const MTPUser &user); - base::Observable &authSessionChanged() { - return _authSessionChanged; - } int unreadBadge() const; bool unreadBadgeMuted() const; void logOut(); @@ -289,8 +282,6 @@ private: std::unique_ptr _mtproto; std::unique_ptr _mtprotoForKeysDestroy; rpl::event_stream<> _configUpdates; - std::unique_ptr _authSession; - base::Observable _authSessionChanged; base::Observable _passcodedChanged; QPointer _badProxyDisableBox; diff --git a/Telegram/SourceFiles/main/main_account.cpp b/Telegram/SourceFiles/main/main_account.cpp index 86c586479..6bb88b3e5 100644 --- a/Telegram/SourceFiles/main/main_account.cpp +++ b/Telegram/SourceFiles/main/main_account.cpp @@ -18,32 +18,35 @@ Account::Account(const QString &dataName) { Account::~Account() { } +void Account::createSession(const MTPUser &user) { + Expects(_session == nullptr); + Expects(_sessionValue.current() == nullptr); + + _session = std::make_unique(this, user); + _sessionValue = _session.get(); +} + +void Account::destroySession() { + _sessionValue = nullptr; + _session = nullptr; +} + bool Account::sessionExists() const { - return Core::App().authSession() != nullptr; + return (_sessionValue.current() != nullptr); } AuthSession &Account::session() { Expects(sessionExists()); - return *Core::App().authSession(); + return *_sessionValue.current(); } rpl::producer Account::sessionValue() const { - return rpl::single( - rpl::empty_value() - ) | rpl::then( - base::ObservableViewer(Core::App().authSessionChanged()) - ) | rpl::map([] { - return Core::App().authSession(); - }); + return _sessionValue.value(); } rpl::producer Account::sessionChanges() const { - return base::ObservableViewer( - Core::App().authSessionChanged() - ) | rpl::map([] { - return Core::App().authSession(); - }); + return _sessionValue.changes(); } MTP::Instance *Account::mtp() { diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h index 727f0e8c8..2e02813f1 100644 --- a/Telegram/SourceFiles/main/main_account.h +++ b/Telegram/SourceFiles/main/main_account.h @@ -19,6 +19,9 @@ public: Account(const Account &other) = delete; Account &operator=(const Account &other) = delete; + void createSession(const MTPUser &user); + void destroySession(); + [[nodiscard]] bool sessionExists() const; [[nodiscard]] AuthSession &session(); [[nodiscard]] rpl::producer sessionValue() const; @@ -27,6 +30,8 @@ public: [[nodiscard]] MTP::Instance *mtp(); private: + std::unique_ptr _session; + rpl::variable _sessionValue; }; diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 45d140cd2..d0f01d498 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -26,6 +26,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/application.h" #include "auth_session.h" #include "intro/introwidget.h" +#include "main/main_account.h" // Account::sessionValue. #include "mainwidget.h" #include "boxes/confirm_box.h" #include "boxes/add_contact_box.h" @@ -80,12 +81,13 @@ MainWindow::MainWindow() { setLocale(QLocale(QLocale::English, QLocale::UnitedStates)); - subscribe(Core::App().authSessionChanged(), [this] { + Core::App().activeAccount().sessionValue( + ) | rpl::start_with_next([=](AuthSession *session) { updateGlobalMenu(); - if (!AuthSession::Exists()) { + if (!session) { _mediaPreview.destroy(); } - }); + }, lifetime()); subscribe(Window::Theme::Background(), [this](const Window::Theme::BackgroundUpdate &data) { themeUpdated(data); }); diff --git a/Telegram/SourceFiles/media/player/media_player_instance.cpp b/Telegram/SourceFiles/media/player/media_player_instance.cpp index 8be0b6e70..8d89226e6 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.cpp +++ b/Telegram/SourceFiles/media/player/media_player_instance.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_controller.h" #include "core/shortcuts.h" #include "core/application.h" +#include "main/main_account.h" // Account::sessionValue. #include "mainwindow.h" #include "auth_session.h" @@ -93,9 +94,10 @@ Instance::Instance() }); // While we have one Media::Player::Instance for all authsessions we have to do this. - const auto handleAuthSessionChange = [=] { - if (AuthSession::Exists()) { - subscribe(Auth().calls().currentCallChanged(), [=](Calls::Call *call) { + Core::App().activeAccount().sessionValue( + ) | rpl::start_with_next([=](AuthSession *session) { + if (session) { + subscribe(session->calls().currentCallChanged(), [=](Calls::Call *call) { if (call) { pauseOnCall(AudioMsgId::Type::Voice); pauseOnCall(AudioMsgId::Type::Song); @@ -104,12 +106,15 @@ Instance::Instance() resumeOnCall(AudioMsgId::Type::Song); } }); + } else { + const auto reset = [&](AudioMsgId::Type type) { + const auto data = getData(type); + *data = Data(type, data->overview); + }; + reset(AudioMsgId::Type::Voice); + reset(AudioMsgId::Type::Song); } - }; - subscribe( - Core::App().authSessionChanged(), - handleAuthSessionChange); - handleAuthSessionChange(); + }, _lifetime); setupShortcuts(); } @@ -636,15 +641,6 @@ void Instance::emitUpdate(AudioMsgId::Type type, CheckCallback check) { } } -void Instance::handleLogout() { - const auto reset = [&](AudioMsgId::Type type) { - const auto data = getData(type); - *data = Data(type, data->overview); - }; - reset(AudioMsgId::Type::Voice); - reset(AudioMsgId::Type::Song); -} - void Instance::setupShortcuts() { Shortcuts::Requests( ) | rpl::start_with_next([=](not_null request) { diff --git a/Telegram/SourceFiles/media/player/media_player_instance.h b/Telegram/SourceFiles/media/player/media_player_instance.h index c20a25b26..d55ae38be 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.h +++ b/Telegram/SourceFiles/media/player/media_player_instance.h @@ -156,8 +156,6 @@ public: void documentLoadProgress(DocumentData *document); - void handleLogout(); - private: using SharedMediaType = Storage::SharedMediaType; using SliceKey = SparseIdsMergedSlice::Key; diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index 280d5d611..4d577ee53 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -36,6 +36,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_user.h" #include "window/themes/window_theme_preview.h" #include "window/window_peer_menu.h" +#include "main/main_account.h" // Account::sessionValue. #include "observer_peer.h" #include "auth_session.h" #include "layout.h" @@ -242,14 +243,15 @@ OverlayWidget::OverlayWidget() connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(onScreenResized(int))); // While we have one mediaview for all authsessions we have to do this. - auto handleAuthSessionChange = [this] { - if (AuthSession::Exists()) { - subscribe(Auth().downloaderTaskFinished(), [this] { + Core::App().activeAccount().sessionValue( + ) | rpl::start_with_next([=](AuthSession *session) { + if (session) { + subscribe(session->downloaderTaskFinished(), [=] { if (!isHidden()) { updateControls(); } }); - subscribe(Auth().calls().currentCallChanged(), [this](Calls::Call *call) { + subscribe(session->calls().currentCallChanged(), [=](Calls::Call *call) { if (!_streamed) { return; } @@ -259,12 +261,12 @@ OverlayWidget::OverlayWidget() playbackResumeOnCall(); } }); - subscribe(Auth().documentUpdated, [this](DocumentData *document) { + subscribe(session->documentUpdated, [=](DocumentData *document) { if (!isHidden()) { documentUpdated(document); } }); - subscribe(Auth().messageIdChanging, [this](std::pair, MsgId> update) { + subscribe(session->messageIdChanging, [=](std::pair, MsgId> update) { changingMsgId(update.first, update.second); }); } else { @@ -272,11 +274,7 @@ OverlayWidget::OverlayWidget() _userPhotos = nullptr; _collage = nullptr; } - }; - subscribe(Core::App().authSessionChanged(), [handleAuthSessionChange] { - handleAuthSessionChange(); - }); - handleAuthSessionChange(); + }, lifetime()); #ifdef Q_OS_LINUX setWindowFlags(Qt::FramelessWindowHint | Qt::MaximizeUsingFullscreenGeometryHint); diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp index 8b11e25ea..72c3ea2fd 100644 --- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp +++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "auth_session.h" #include "core/application.h" +#include "main/main_account.h" // Account::sessionChanges. namespace MTP { namespace { @@ -89,11 +90,12 @@ WeakInstance::WeakInstance(QPointer instance) _instance = nullptr; die(); }); - subscribe(Core::App().authSessionChanged(), [=] { - if (!AuthSession::Exists()) { - die(); - } - }); + Core::App().activeAccount().sessionChanges( + ) | rpl::filter([](AuthSession *session) { + return !session; + }) | rpl::start_with_next([=] { + die(); + }, _lifetime); } bool WeakInstance::valid() const { diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.h b/Telegram/SourceFiles/mtproto/dedicated_file_loader.h index 6a194a8be..a21769f2b 100644 --- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.h +++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.h @@ -34,6 +34,7 @@ private: QPointer _instance; std::map> _requests; + rpl::lifetime _lifetime; }; diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index 28a970918..cb32d6b72 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -15,6 +15,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history.h" #include "history/history_widget.h" #include "history/history_inner_widget.h" +#include "main/main_account.h" // Account::sessionChanges. #include "storage/localstorage.h" #include "window/notifications_manager_default.h" #include "window/themes/window_theme.h" @@ -405,8 +406,9 @@ void MainWindow::initTouchBar() { return; } - subscribe(Core::App().authSessionChanged(), [this] { - if (AuthSession::Exists()) { + Core::App().activeAccount().sessionValue( + ) | rpl::start_with_next([=](AuthSession *session) { + if (session) { // We need only common pinned dialogs. if (!_private->_touchBar) { if (auto view = reinterpret_cast(winId())) { @@ -422,7 +424,7 @@ void MainWindow::initTouchBar() { } _private->_touchBar = nil; } - }); + }, lifetime()); } void MainWindow::closeWithoutDestroy() { diff --git a/Telegram/SourceFiles/window/main_window.cpp b/Telegram/SourceFiles/window/main_window.cpp index 55208d7da..b3420ed73 100644 --- a/Telegram/SourceFiles/window/main_window.cpp +++ b/Telegram/SourceFiles/window/main_window.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_lock_widgets.h" #include "window/window_outdated_bar.h" #include "boxes/confirm_box.h" +#include "main/main_account.h" // Account::authSessionValue. #include "core/click_handler_types.h" #include "core/application.h" #include "core/sandbox.h" @@ -125,11 +126,14 @@ MainWindow::MainWindow() subscribe(Global::RefWorkMode(), [=](DBIWorkMode mode) { workmodeUpdated(mode); }); - subscribe(Core::App().authSessionChanged(), [=] { - checkAuthSession(); + + Core::App().activeAccount().sessionValue( + ) | rpl::start_with_next([=](AuthSession *session) { + _controller = session + ? std::make_unique(session, this) + : nullptr; updateWindowIcon(); - }); - checkAuthSession(); + }, lifetime()); Core::App().termsLockValue( ) | rpl::start_with_next([=] { @@ -639,14 +643,6 @@ void MainWindow::launchDrag(std::unique_ptr data) { } } -void MainWindow::checkAuthSession() { - if (AuthSession::Exists()) { - _controller = std::make_unique(&Auth(), this); - } else { - _controller = nullptr; - } -} - void MainWindow::setInactivePress(bool inactive) { _wasInactivePress = inactive; if (_wasInactivePress) { diff --git a/Telegram/SourceFiles/window/main_window.h b/Telegram/SourceFiles/window/main_window.h index 3bbc8235b..ed71d584d 100644 --- a/Telegram/SourceFiles/window/main_window.h +++ b/Telegram/SourceFiles/window/main_window.h @@ -148,7 +148,6 @@ protected: QSystemTrayIcon::ActivationReason reason) = 0; private: - void checkAuthSession(); void updatePalette(); void updateUnreadCounter(); void initSize(); diff --git a/Telegram/SourceFiles/window/themes/window_theme.cpp b/Telegram/SourceFiles/window/themes/window_theme.cpp index 639c265de..90bf34b88 100644 --- a/Telegram/SourceFiles/window/themes/window_theme.cpp +++ b/Telegram/SourceFiles/window/themes/window_theme.cpp @@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/parse_helper.h" #include "base/zlib_help.h" #include "data/data_session.h" +#include "main/main_account.h" // Account::sessionValue. #include "ui/image/image.h" #include "boxes/background_box.h" #include "core/application.h" @@ -391,23 +392,20 @@ void ChatBackground::setThemeData(QImage &&themeImage, bool themeTile) { } void ChatBackground::start() { - if (Data::details::IsUninitializedWallPaper(_paper)) { - if (!Local::readBackground()) { - set(Data::ThemeWallPaper()); - } - refreshSession(); - subscribe(Core::App().authSessionChanged(), [=] { - refreshSession(); - }); + if (!Data::details::IsUninitializedWallPaper(_paper)) { + return; + } + if (!Local::readBackground()) { + set(Data::ThemeWallPaper()); } -} -void ChatBackground::refreshSession() { - const auto session = AuthSession::Exists() ? &Auth() : nullptr; - if (_session != session) { + Core::App().activeAccount().sessionValue( + ) | rpl::filter([=](AuthSession *session) { + return session != _session; + }) | rpl::start_with_next([=](AuthSession *session) { _session = session; checkUploadWallPaper(); - } + }, _lifetime); } void ChatBackground::checkUploadWallPaper() { diff --git a/Telegram/SourceFiles/window/themes/window_theme.h b/Telegram/SourceFiles/window/themes/window_theme.h index 4d91facfc..80dfe3aba 100644 --- a/Telegram/SourceFiles/window/themes/window_theme.h +++ b/Telegram/SourceFiles/window/themes/window_theme.h @@ -154,7 +154,6 @@ private: void keepApplied(const QString &path, bool write); [[nodiscard]] bool isNonDefaultThemeOrBackground(); [[nodiscard]] bool isNonDefaultBackground(); - void refreshSession(); void checkUploadWallPaper(); [[nodiscard]] bool testingPalette() const; @@ -191,6 +190,8 @@ private: mtpRequestId _wallPaperRequestId = 0; rpl::lifetime _wallPaperUploadLifetime; + rpl::lifetime _lifetime; + }; ChatBackground *Background();