main_account.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "mtproto/mtproto_auth_key.h"
  9. #include "mtproto/mtp_instance.h"
  10. #include "base/weak_ptr.h"
  11. namespace Storage {
  12. class Account;
  13. class Domain;
  14. enum class StartResult : uchar;
  15. } // namespace Storage
  16. namespace MTP {
  17. class AuthKey;
  18. class Config;
  19. } // namespace MTP
  20. namespace Main {
  21. class Domain;
  22. class Session;
  23. class SessionSettings;
  24. class AppConfig;
  25. class Account final : public base::has_weak_ptr {
  26. public:
  27. Account(not_null<Domain*> domain, const QString &dataName, int index);
  28. ~Account();
  29. [[nodiscard]] Domain &domain() const {
  30. return *_domain;
  31. }
  32. [[nodiscard]] Storage::Domain &domainLocal() const;
  33. [[nodiscard]] Storage::StartResult legacyStart(
  34. const QByteArray &passcode);
  35. [[nodiscard]] std::unique_ptr<MTP::Config> prepareToStart(
  36. std::shared_ptr<MTP::AuthKey> localKey);
  37. void prepareToStartAdded(
  38. std::shared_ptr<MTP::AuthKey> localKey);
  39. void start(std::unique_ptr<MTP::Config> config);
  40. [[nodiscard]] uint64 willHaveSessionUniqueId(MTP::Config *config) const;
  41. void createSession(
  42. const MTPUser &user,
  43. std::unique_ptr<SessionSettings> settings = nullptr);
  44. void createSession(
  45. UserId id,
  46. QByteArray serialized,
  47. int streamVersion,
  48. std::unique_ptr<SessionSettings> settings);
  49. void logOut();
  50. void forcedLogOut();
  51. [[nodiscard]] bool loggingOut() const;
  52. [[nodiscard]] AppConfig &appConfig() const {
  53. Expects(_appConfig != nullptr);
  54. return *_appConfig;
  55. }
  56. [[nodiscard]] Storage::Account &local() const {
  57. return *_local;
  58. }
  59. [[nodiscard]] bool sessionExists() const;
  60. [[nodiscard]] Session &session() const;
  61. [[nodiscard]] Session *maybeSession() const;
  62. [[nodiscard]] rpl::producer<Session*> sessionValue() const;
  63. [[nodiscard]] rpl::producer<Session*> sessionChanges() const;
  64. [[nodiscard]] MTP::Instance &mtp() const {
  65. return *_mtp;
  66. }
  67. [[nodiscard]] rpl::producer<not_null<MTP::Instance*>> mtpValue() const;
  68. // Each time the main session changes a new copy of the pointer is fired.
  69. // This allows to resend the requests that were not requiring auth, and
  70. // which could be forgotten without calling .done() or .fail() because
  71. // of the main dc changing.
  72. [[nodiscard]] auto mtpMainSessionValue() const
  73. -> rpl::producer<not_null<MTP::Instance*>>;
  74. // Set from legacy storage.
  75. void setLegacyMtpKey(std::shared_ptr<MTP::AuthKey> key);
  76. void setMtpMainDcId(MTP::DcId mainDcId);
  77. void setSessionUserId(UserId userId);
  78. void setSessionFromStorage(
  79. std::unique_ptr<SessionSettings> data,
  80. QByteArray &&selfSerialized,
  81. int32 selfStreamVersion);
  82. [[nodiscard]] SessionSettings *getSessionSettings();
  83. [[nodiscard]] rpl::producer<> mtpNewSessionCreated() const;
  84. [[nodiscard]] rpl::producer<MTPUpdates> mtpUpdates() const;
  85. // Serialization.
  86. [[nodiscard]] QByteArray serializeMtpAuthorization() const;
  87. void setMtpAuthorization(const QByteArray &serialized);
  88. void suggestMainDcId(MTP::DcId mainDcId);
  89. void destroyStaleAuthorizationKeys();
  90. void setHandleLoginCode(Fn<void(QString)> callback);
  91. void handleLoginCode(const QString &code) const;
  92. [[nodiscard]] rpl::lifetime &lifetime() {
  93. return _lifetime;
  94. }
  95. private:
  96. static constexpr auto kDefaultSaveDelay = crl::time(1000);
  97. enum class DestroyReason {
  98. Quitting,
  99. LoggedOut,
  100. };
  101. void startMtp(std::unique_ptr<MTP::Config> config);
  102. void createSession(
  103. const MTPUser &user,
  104. QByteArray serialized,
  105. int streamVersion,
  106. std::unique_ptr<SessionSettings> settings);
  107. void watchProxyChanges();
  108. void watchSessionChanges();
  109. bool checkForUpdates(const MTP::Response &message);
  110. bool checkForNewSession(const MTP::Response &message);
  111. void destroyMtpKeys(MTP::AuthKeysList &&keys);
  112. void resetAuthorizationKeys();
  113. void loggedOut();
  114. void destroySession(DestroyReason reason);
  115. const not_null<Domain*> _domain;
  116. const std::unique_ptr<Storage::Account> _local;
  117. std::unique_ptr<MTP::Instance> _mtp;
  118. rpl::variable<MTP::Instance*> _mtpValue;
  119. std::unique_ptr<MTP::Instance> _mtpForKeysDestroy;
  120. rpl::event_stream<MTPUpdates> _mtpUpdates;
  121. rpl::event_stream<> _mtpNewSessionCreated;
  122. std::unique_ptr<AppConfig> _appConfig;
  123. std::unique_ptr<Session> _session;
  124. rpl::variable<Session*> _sessionValue;
  125. Fn<void(QString)> _handleLoginCode = nullptr;
  126. UserId _sessionUserId = 0;
  127. QByteArray _sessionUserSerialized;
  128. int32 _sessionUserStreamVersion = 0;
  129. std::unique_ptr<SessionSettings> _storedSessionSettings;
  130. MTP::Instance::Fields _mtpFields;
  131. MTP::AuthKeysList _mtpKeysToDestroy;
  132. bool _loggingOut = false;
  133. rpl::lifetime _lifetime;
  134. };
  135. } // namespace Main