storage_account.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "base/timer.h"
  9. #include "base/flags.h"
  10. #include "storage/cache/storage_cache_database.h"
  11. #include "data/stickers/data_stickers_set.h"
  12. #include "data/data_drafts.h"
  13. #include "webview/webview_common.h"
  14. class History;
  15. namespace Core {
  16. class FileLocation;
  17. } // namespace Core
  18. namespace Export {
  19. struct Settings;
  20. } // namespace Export
  21. namespace Main {
  22. class Account;
  23. class SessionSettings;
  24. } // namespace Main
  25. namespace Data {
  26. class WallPaper;
  27. } // namespace Data
  28. namespace MTP {
  29. class Config;
  30. class AuthKey;
  31. using AuthKeyPtr = std::shared_ptr<AuthKey>;
  32. } // namespace MTP
  33. namespace Storage {
  34. namespace details {
  35. struct ReadSettingsContext;
  36. struct FileReadDescriptor;
  37. } // namespace details
  38. class EncryptionKey;
  39. using FileKey = quint64;
  40. enum class StartResult : uchar;
  41. struct MessageDraft {
  42. FullReplyTo reply;
  43. TextWithTags textWithTags;
  44. Data::WebPageDraft webpage;
  45. };
  46. struct MessageDraftSource {
  47. Fn<MessageDraft()> draft;
  48. Fn<MessageCursor()> cursor;
  49. };
  50. class Account final {
  51. public:
  52. Account(not_null<Main::Account*> owner, const QString &dataName);
  53. ~Account();
  54. [[nodiscard]] StartResult legacyStart(const QByteArray &passcode);
  55. [[nodiscard]] std::unique_ptr<MTP::Config> start(
  56. MTP::AuthKeyPtr localKey);
  57. void startAdded(MTP::AuthKeyPtr localKey);
  58. [[nodiscard]] int oldMapVersion() const {
  59. return _oldMapVersion;
  60. }
  61. [[nodiscard]] QString tempDirectory() const;
  62. [[nodiscard]] QString supportModePath() const;
  63. [[nodiscard]] MTP::AuthKeyPtr peekLegacyLocalKey() const {
  64. return _localKey;
  65. }
  66. void writeSessionSettings();
  67. void writeMtpData();
  68. void writeMtpConfig();
  69. void registerDraftSource(
  70. not_null<History*> history,
  71. Data::DraftKey key,
  72. MessageDraftSource source);
  73. void unregisterDraftSource(
  74. not_null<History*> history,
  75. Data::DraftKey key);
  76. void writeDrafts(not_null<History*> history);
  77. void readDraftsWithCursors(not_null<History*> history);
  78. void writeDraftCursors(not_null<History*> history);
  79. [[nodiscard]] bool hasDraftCursors(PeerId peerId);
  80. [[nodiscard]] bool hasDraft(PeerId peerId);
  81. void writeFileLocation(
  82. MediaKey location,
  83. const Core::FileLocation &local);
  84. [[nodiscard]] Core::FileLocation readFileLocation(MediaKey location);
  85. void removeFileLocation(MediaKey location);
  86. void updateDownloads(Fn<std::optional<QByteArray>()> downloadsSerialize);
  87. [[nodiscard]] QByteArray downloadsSerialized() const;
  88. [[nodiscard]] EncryptionKey cacheKey() const;
  89. [[nodiscard]] QString cachePath() const;
  90. [[nodiscard]] Cache::Database::Settings cacheSettings() const;
  91. void updateCacheSettings(
  92. Cache::Database::SettingsUpdate &update,
  93. Cache::Database::SettingsUpdate &updateBig);
  94. [[nodiscard]] EncryptionKey cacheBigFileKey() const;
  95. [[nodiscard]] QString cacheBigFilePath() const;
  96. [[nodiscard]] Cache::Database::Settings cacheBigFileSettings() const;
  97. void writeInstalledStickers();
  98. void writeFeaturedStickers();
  99. void writeRecentStickers();
  100. void writeFavedStickers();
  101. void writeArchivedStickers();
  102. void writeArchivedMasks();
  103. void readInstalledStickers();
  104. void readFeaturedStickers();
  105. void readRecentStickers();
  106. void readFavedStickers();
  107. void readArchivedStickers();
  108. void readArchivedMasks();
  109. void writeSavedGifs();
  110. void readSavedGifs();
  111. void writeInstalledMasks();
  112. void writeRecentMasks();
  113. void readInstalledMasks();
  114. void readRecentMasks();
  115. void writeInstalledCustomEmoji();
  116. void writeFeaturedCustomEmoji();
  117. void readInstalledCustomEmoji();
  118. void readFeaturedCustomEmoji();
  119. void writeRecentHashtagsAndBots();
  120. void readRecentHashtagsAndBots();
  121. void saveRecentSentHashtags(const QString &text);
  122. void saveRecentSearchHashtags(const QString &text);
  123. void writeExportSettings(const Export::Settings &settings);
  124. [[nodiscard]] Export::Settings readExportSettings();
  125. void setMediaLastPlaybackPosition(DocumentId id, crl::time time);
  126. [[nodiscard]] crl::time mediaLastPlaybackPosition(DocumentId id) const;
  127. void writeSearchSuggestionsDelayed();
  128. void writeSearchSuggestionsIfNeeded();
  129. void writeSearchSuggestions();
  130. void readSearchSuggestions();
  131. void writeSelf();
  132. // Read self is special, it can't get session from account, because
  133. // it is not really there yet - it is still being constructed.
  134. void readSelf(
  135. not_null<Main::Session*> session,
  136. const QByteArray& serialized,
  137. int32 streamVersion);
  138. void markPeerTrustedOpenGame(PeerId peerId);
  139. [[nodiscard]] bool isPeerTrustedOpenGame(PeerId peerId);
  140. void markPeerTrustedPayment(PeerId peerId);
  141. [[nodiscard]] bool isPeerTrustedPayment(PeerId peerId);
  142. void markPeerTrustedOpenWebView(PeerId peerId);
  143. [[nodiscard]] bool isPeerTrustedOpenWebView(PeerId peerId);
  144. void markPeerTrustedPayForMessage(PeerId peerId, int starsPerMessage);
  145. [[nodiscard]] bool isPeerTrustedPayForMessage(
  146. PeerId peerId,
  147. int starsPerMessage);
  148. [[nodiscard]] bool peerTrustedPayForMessageRead() const;
  149. [[nodiscard]] bool hasPeerTrustedPayForMessageEntry(PeerId peerId) const;
  150. void clearPeerTrustedPayForMessage(PeerId peerId);
  151. void enforceModernStorageIdBots();
  152. [[nodiscard]] Webview::StorageId resolveStorageIdBots();
  153. [[nodiscard]] Webview::StorageId resolveStorageIdOther();
  154. [[nodiscard]] QImage readRoundPlaceholder();
  155. void writeRoundPlaceholder(const QImage &placeholder);
  156. [[nodiscard]] QByteArray readInlineBotsDownloads();
  157. void writeInlineBotsDownloads(const QByteArray &bytes);
  158. [[nodiscard]] bool encrypt(
  159. const void *src,
  160. void *dst,
  161. uint32 len,
  162. const void *key128) const;
  163. [[nodiscard]] bool decrypt(
  164. const void *src,
  165. void *dst,
  166. uint32 len,
  167. const void *key128) const;
  168. void reset();
  169. private:
  170. enum class ReadMapResult {
  171. Success,
  172. IncorrectPasscode,
  173. Failed,
  174. };
  175. enum class PeerTrustFlag : uchar {
  176. NoOpenGame = (1 << 0),
  177. Payment = (1 << 1),
  178. OpenWebView = (1 << 2),
  179. };
  180. friend inline constexpr bool is_flag_type(PeerTrustFlag) { return true; };
  181. [[nodiscard]] base::flat_set<QString> collectGoodNames() const;
  182. [[nodiscard]] auto prepareReadSettingsContext() const
  183. -> details::ReadSettingsContext;
  184. ReadMapResult readMapWith(
  185. MTP::AuthKeyPtr localKey,
  186. const QByteArray &legacyPasscode = QByteArray());
  187. void clearLegacyFiles();
  188. void writeMapDelayed();
  189. void writeMapQueued();
  190. void writeMap();
  191. void readLocations();
  192. void writeLocations();
  193. void writeLocationsQueued();
  194. void writeLocationsDelayed();
  195. std::unique_ptr<Main::SessionSettings> readSessionSettings();
  196. void writeSessionSettings(Main::SessionSettings *stored);
  197. std::unique_ptr<MTP::Config> readMtpConfig();
  198. void readMtpData();
  199. std::unique_ptr<Main::SessionSettings> applyReadContext(
  200. details::ReadSettingsContext &&context);
  201. void readDraftCursors(PeerId peerId, Data::HistoryDrafts &map);
  202. void readDraftCursorsLegacy(
  203. PeerId peerId,
  204. details::FileReadDescriptor &draft,
  205. quint64 draftPeerSerialized,
  206. Data::HistoryDrafts &map);
  207. void clearDraftCursors(PeerId peerId);
  208. void readDraftsWithCursorsLegacy(
  209. not_null<History*> history,
  210. details::FileReadDescriptor &draft,
  211. quint64 draftPeerSerialized);
  212. void writeStickerSet(
  213. QDataStream &stream,
  214. const Data::StickersSet &set);
  215. template <typename CheckSet>
  216. void writeStickerSets(
  217. FileKey &stickersKey,
  218. CheckSet checkSet,
  219. const Data::StickersSetsOrder &order);
  220. void readStickerSets(
  221. FileKey &stickersKey,
  222. Data::StickersSetsOrder *outOrder = nullptr,
  223. Data::StickersSetFlags readingFlags = 0);
  224. void importOldRecentStickers();
  225. void readTrustedPeers();
  226. void writeTrustedPeers();
  227. void readMediaLastPlaybackPositions();
  228. void writeMediaLastPlaybackPositions();
  229. std::optional<RecentHashtagPack> saveRecentHashtags(
  230. Fn<RecentHashtagPack()> getPack,
  231. const QString &text);
  232. const not_null<Main::Account*> _owner;
  233. const QString _dataName;
  234. const FileKey _dataNameKey = 0;
  235. const QString _basePath;
  236. const QString _tempPath;
  237. const QString _databasePath;
  238. MTP::AuthKeyPtr _localKey;
  239. base::flat_map<PeerId, FileKey> _draftsMap;
  240. base::flat_map<PeerId, FileKey> _draftCursorsMap;
  241. base::flat_map<PeerId, bool> _draftsNotReadMap;
  242. base::flat_map<
  243. not_null<History*>,
  244. base::flat_map<Data::DraftKey, MessageDraftSource>> _draftSources;
  245. QMultiMap<MediaKey, Core::FileLocation> _fileLocations;
  246. QMap<QString, QPair<MediaKey, Core::FileLocation>> _fileLocationPairs;
  247. QMap<MediaKey, MediaKey> _fileLocationAliases;
  248. QByteArray _downloadsSerialized;
  249. Fn<std::optional<QByteArray>()> _downloadsSerialize;
  250. FileKey _locationsKey = 0;
  251. FileKey _trustedPeersKey = 0;
  252. FileKey _installedStickersKey = 0;
  253. FileKey _featuredStickersKey = 0;
  254. FileKey _recentStickersKey = 0;
  255. FileKey _favedStickersKey = 0;
  256. FileKey _archivedStickersKey = 0;
  257. FileKey _archivedMasksKey = 0;
  258. FileKey _savedGifsKey = 0;
  259. FileKey _recentStickersKeyOld = 0;
  260. FileKey _legacyBackgroundKeyDay = 0;
  261. FileKey _legacyBackgroundKeyNight = 0;
  262. FileKey _settingsKey = 0;
  263. FileKey _recentHashtagsAndBotsKey = 0;
  264. FileKey _exportSettingsKey = 0;
  265. FileKey _installedMasksKey = 0;
  266. FileKey _recentMasksKey = 0;
  267. FileKey _installedCustomEmojiKey = 0;
  268. FileKey _featuredCustomEmojiKey = 0;
  269. FileKey _archivedCustomEmojiKey = 0;
  270. FileKey _searchSuggestionsKey = 0;
  271. FileKey _roundPlaceholderKey = 0;
  272. FileKey _inlineBotsDownloadsKey = 0;
  273. FileKey _mediaLastPlaybackPositionsKey = 0;
  274. qint64 _cacheTotalSizeLimit = 0;
  275. qint64 _cacheBigFileTotalSizeLimit = 0;
  276. qint32 _cacheTotalTimeLimit = 0;
  277. qint32 _cacheBigFileTotalTimeLimit = 0;
  278. base::flat_map<PeerId, base::flags<PeerTrustFlag>> _trustedPeers;
  279. base::flat_map<PeerId, int> _trustedPayPerMessage;
  280. bool _trustedPeersRead = false;
  281. bool _readingUserSettings = false;
  282. bool _recentHashtagsAndBotsWereRead = false;
  283. bool _searchSuggestionsRead = false;
  284. bool _inlineBotsDownloadsRead = false;
  285. bool _mediaLastPlaybackPositionsRead = false;
  286. std::vector<std::pair<DocumentId, crl::time>> _mediaLastPlaybackPosition;
  287. Webview::StorageId _webviewStorageIdBots;
  288. Webview::StorageId _webviewStorageIdOther;
  289. int _oldMapVersion = 0;
  290. base::Timer _writeMapTimer;
  291. base::Timer _writeLocationsTimer;
  292. base::Timer _writeSearchSuggestionsTimer;
  293. bool _mapChanged = false;
  294. bool _locationsChanged = false;
  295. QImage _roundPlaceholder;
  296. };
  297. [[nodiscard]] Webview::StorageId TonSiteStorageId();
  298. } // namespace Storage