From 2577bedeac3e236230d1bfabf30f124b74ba4034 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Wed, 13 Nov 2019 11:31:12 +0300
Subject: [PATCH] Remove mtproto/facade.h from main pch.

---
 Telegram/SourceFiles/boxes/calendar_box.cpp   |  1 +
 Telegram/SourceFiles/boxes/confirm_box.h      |  1 +
 .../SourceFiles/boxes/confirm_phone_box.cpp   |  1 +
 .../SourceFiles/boxes/confirm_phone_box.h     |  1 +
 Telegram/SourceFiles/boxes/edit_caption_box.h |  1 +
 .../boxes/peers/edit_participant_box.h        |  2 +
 Telegram/SourceFiles/boxes/report_box.cpp     |  1 +
 Telegram/SourceFiles/boxes/report_box.h       |  1 +
 Telegram/SourceFiles/boxes/share_box.h        |  1 +
 Telegram/SourceFiles/boxes/username_box.h     |  1 +
 .../dialogs/dialogs_inner_widget.h            |  1 +
 Telegram/SourceFiles/dialogs/dialogs_widget.h |  1 +
 Telegram/SourceFiles/main/main_account.h      |  1 +
 Telegram/SourceFiles/main/main_session.h      |  4 ++
 Telegram/SourceFiles/mainwidget.h             |  1 +
 Telegram/SourceFiles/mtproto/auth_key.cpp     | 58 ++++++++++++++++++
 Telegram/SourceFiles/mtproto/auth_key.h       | 60 ++++---------------
 .../SourceFiles/mtproto/concurrent_sender.cpp |  1 +
 .../SourceFiles/mtproto/config_loader.cpp     |  1 +
 Telegram/SourceFiles/mtproto/connection.cpp   | 16 ++---
 Telegram/SourceFiles/mtproto/connection.h     |  1 +
 .../SourceFiles/mtproto/connection_abstract.h |  3 +
 .../mtproto/connection_resolving.cpp          |  2 +
 Telegram/SourceFiles/mtproto/dc_options.cpp   |  1 +
 .../mtproto/dedicated_file_loader.cpp         |  1 +
 .../mtproto/dedicated_file_loader.h           |  4 +-
 Telegram/SourceFiles/mtproto/mtp_instance.cpp | 25 ++++----
 Telegram/SourceFiles/mtproto/mtp_instance.h   |  5 +-
 Telegram/SourceFiles/mtproto/sender.h         |  3 +
 Telegram/SourceFiles/stdafx.h                 |  3 +-
 Telegram/SourceFiles/storage/file_download.h  |  1 +
 Telegram/SourceFiles/storage/file_upload.h    |  1 +
 .../window/window_connecting_widget.cpp       |  1 +
 33 files changed, 133 insertions(+), 73 deletions(-)

diff --git a/Telegram/SourceFiles/boxes/calendar_box.cpp b/Telegram/SourceFiles/boxes/calendar_box.cpp
index 25bbbfe79..54b14b221 100644
--- a/Telegram/SourceFiles/boxes/calendar_box.cpp
+++ b/Telegram/SourceFiles/boxes/calendar_box.cpp
@@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "boxes/calendar_box.h"
 
+#include "mtproto/rpc_sender.h"
 #include "ui/widgets/buttons.h"
 #include "lang/lang_keys.h"
 #include "ui/effects/ripple_animation.h"
diff --git a/Telegram/SourceFiles/boxes/confirm_box.h b/Telegram/SourceFiles/boxes/confirm_box.h
index 07c4971c0..cda3e0670 100644
--- a/Telegram/SourceFiles/boxes/confirm_box.h
+++ b/Telegram/SourceFiles/boxes/confirm_box.h
@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #pragma once
 
 #include "boxes/abstract_box.h"
+#include "mtproto/rpc_sender.h"
 
 namespace Main {
 class Session;
diff --git a/Telegram/SourceFiles/boxes/confirm_phone_box.cpp b/Telegram/SourceFiles/boxes/confirm_phone_box.cpp
index 8e4b8bf48..6cd1a62e2 100644
--- a/Telegram/SourceFiles/boxes/confirm_phone_box.cpp
+++ b/Telegram/SourceFiles/boxes/confirm_phone_box.cpp
@@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "numbers.h"
 #include "app.h"
 #include "lang/lang_keys.h"
+#include "mtproto/facade.h"
 #include "styles/style_layers.h"
 #include "styles/style_boxes.h"
 
diff --git a/Telegram/SourceFiles/boxes/confirm_phone_box.h b/Telegram/SourceFiles/boxes/confirm_phone_box.h
index d2963f960..b474df152 100644
--- a/Telegram/SourceFiles/boxes/confirm_phone_box.h
+++ b/Telegram/SourceFiles/boxes/confirm_phone_box.h
@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "boxes/abstract_box.h"
 #include "base/timer.h"
 #include "ui/widgets/input_fields.h"
+#include "mtproto/rpc_sender.h"
 
 namespace Ui {
 class InputField;
diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.h b/Telegram/SourceFiles/boxes/edit_caption_box.h
index 14e7cd5c3..59e821954 100644
--- a/Telegram/SourceFiles/boxes/edit_caption_box.h
+++ b/Telegram/SourceFiles/boxes/edit_caption_box.h
@@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "storage/storage_media_prepare.h"
 #include "ui/wrap/slide_wrap.h"
 #include "media/clip/media_clip_reader.h"
+#include "mtproto/rpc_sender.h"
 
 namespace ChatHelpers {
 class TabbedPanel;
diff --git a/Telegram/SourceFiles/boxes/peers/edit_participant_box.h b/Telegram/SourceFiles/boxes/peers/edit_participant_box.h
index 7ef9e877e..689b08a7b 100644
--- a/Telegram/SourceFiles/boxes/peers/edit_participant_box.h
+++ b/Telegram/SourceFiles/boxes/peers/edit_participant_box.h
@@ -10,6 +10,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "boxes/abstract_box.h"
 #include "base/unique_qptr.h"
 
+class RPCError;
+
 namespace Ui {
 class FlatLabel;
 class LinkButton;
diff --git a/Telegram/SourceFiles/boxes/report_box.cpp b/Telegram/SourceFiles/boxes/report_box.cpp
index 5ad271db2..28697df52 100644
--- a/Telegram/SourceFiles/boxes/report_box.cpp
+++ b/Telegram/SourceFiles/boxes/report_box.cpp
@@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "ui/widgets/buttons.h"
 #include "ui/widgets/input_fields.h"
 #include "ui/toast/toast.h"
+#include "mtproto/facade.h"
 #include "mainwindow.h"
 #include "styles/style_layers.h"
 #include "styles/style_boxes.h"
diff --git a/Telegram/SourceFiles/boxes/report_box.h b/Telegram/SourceFiles/boxes/report_box.h
index d7afc3761..17ed36236 100644
--- a/Telegram/SourceFiles/boxes/report_box.h
+++ b/Telegram/SourceFiles/boxes/report_box.h
@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #pragma once
 
 #include "boxes/abstract_box.h"
+#include "mtproto/rpc_sender.h"
 
 namespace Ui {
 template <typename Enum>
diff --git a/Telegram/SourceFiles/boxes/share_box.h b/Telegram/SourceFiles/boxes/share_box.h
index 55d223c04..822c7477f 100644
--- a/Telegram/SourceFiles/boxes/share_box.h
+++ b/Telegram/SourceFiles/boxes/share_box.h
@@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/timer.h"
 #include "ui/effects/animations.h"
 #include "ui/effects/round_checkbox.h"
+#include "mtproto/rpc_sender.h"
 
 enum class SendMenuType;
 
diff --git a/Telegram/SourceFiles/boxes/username_box.h b/Telegram/SourceFiles/boxes/username_box.h
index 2cc31dd52..433d9c05c 100644
--- a/Telegram/SourceFiles/boxes/username_box.h
+++ b/Telegram/SourceFiles/boxes/username_box.h
@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #pragma once
 
 #include "boxes/abstract_box.h"
+#include "mtproto/rpc_sender.h"
 
 namespace Ui {
 class UsernameInput;
diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h
index b5b29a017..d0b8d5d7e 100644
--- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h
+++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.h
@@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "ui/rp_widget.h"
 #include "base/flags.h"
 #include "base/object_ptr.h"
+#include "mtproto/rpc_sender.h"
 
 namespace Main {
 class Session;
diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.h b/Telegram/SourceFiles/dialogs/dialogs_widget.h
index 9fd8509e6..8d99ef5cc 100644
--- a/Telegram/SourceFiles/dialogs/dialogs_widget.h
+++ b/Telegram/SourceFiles/dialogs/dialogs_widget.h
@@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "ui/widgets/scroll_area.h"
 #include "ui/special_buttons.h"
 #include "api/api_single_message_search.h"
+#include "mtproto/rpc_sender.h"
 
 namespace Main {
 class Session;
diff --git a/Telegram/SourceFiles/main/main_account.h b/Telegram/SourceFiles/main/main_account.h
index 44765995a..6729c086e 100644
--- a/Telegram/SourceFiles/main/main_account.h
+++ b/Telegram/SourceFiles/main/main_account.h
@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #pragma once
 
 #include "mtproto/auth_key.h"
+#include "mtproto/mtp_instance.h"
 #include "base/weak_ptr.h"
 
 namespace Main {
diff --git a/Telegram/SourceFiles/main/main_session.h b/Telegram/SourceFiles/main/main_session.h
index 91cf0c45b..34231c0a2 100644
--- a/Telegram/SourceFiles/main/main_session.h
+++ b/Telegram/SourceFiles/main/main_session.h
@@ -15,6 +15,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 class ApiWrap;
 
+namespace MTP {
+class Instance;
+} // namespace MTP
+
 namespace Support {
 class Helper;
 class Templates;
diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h
index f2bd4ce59..cdce6b299 100644
--- a/Telegram/SourceFiles/mainwidget.h
+++ b/Telegram/SourceFiles/mainwidget.h
@@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "ui/effects/animations.h"
 #include "media/player/media_player_float.h"
 #include "data/data_pts_waiter.h"
+#include "mtproto/rpc_sender.h"
 
 struct HistoryMessageMarkupButton;
 class MainWindow;
diff --git a/Telegram/SourceFiles/mtproto/auth_key.cpp b/Telegram/SourceFiles/mtproto/auth_key.cpp
index b9ef48800..f116a707b 100644
--- a/Telegram/SourceFiles/mtproto/auth_key.cpp
+++ b/Telegram/SourceFiles/mtproto/auth_key.cpp
@@ -14,6 +14,29 @@ extern "C" {
 
 namespace MTP {
 
+AuthKey::AuthKey(Type type, DcId dcId, const Data &data)
+: _type(type)
+, _dcId(dcId)
+, _key(data) {
+	countKeyId();
+}
+
+AuthKey::AuthKey(const Data &data) : _type(Type::Local), _key(data) {
+	countKeyId();
+}
+
+AuthKey::Type AuthKey::type() const {
+	return _type;
+}
+
+int AuthKey::dcId() const {
+	return _dcId;
+}
+
+AuthKey::KeyId AuthKey::keyId() const {
+	return _keyId;
+}
+
 void AuthKey::prepareAES_oldmtp(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const {
 	uint32 x = send ? 0 : 8;
 
@@ -72,6 +95,41 @@ void AuthKey::prepareAES(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &
 	memcpy(iv + 8 + 16, sha256_b + 24, 8);
 }
 
+const void *AuthKey::partForMsgKey(bool send) const {
+	return _key.data() + 88 + (send ? 0 : 8);
+}
+
+void AuthKey::write(QDataStream &to) const {
+	to.writeRawData(reinterpret_cast<const char*>(_key.data()), _key.size());
+}
+
+bytes::const_span AuthKey::data() const {
+	return _key;
+}
+
+bool AuthKey::equals(const std::shared_ptr<AuthKey> &other) const {
+	return other ? (_key == other->_key) : false;
+}
+
+void AuthKey::FillData(Data &authKey, bytes::const_span computedAuthKey) {
+	auto computedAuthKeySize = computedAuthKey.size();
+	Assert(computedAuthKeySize <= kSize);
+	auto authKeyBytes = gsl::make_span(authKey);
+	if (computedAuthKeySize < kSize) {
+		bytes::set_with_const(authKeyBytes.subspan(0, kSize - computedAuthKeySize), gsl::byte());
+		bytes::copy(authKeyBytes.subspan(kSize - computedAuthKeySize), computedAuthKey);
+	} else {
+		bytes::copy(authKeyBytes, computedAuthKey);
+	}
+}
+
+void AuthKey::countKeyId() {
+	auto sha1 = hashSha1(_key.data(), _key.size());
+
+	// Lower 64 bits = 8 bytes of 20 byte SHA1 hash.
+	_keyId = *reinterpret_cast<KeyId*>(sha1.data() + 12);
+}
+
 void aesIgeEncryptRaw(const void *src, void *dst, uint32 len, const void *key, const void *iv) {
 	uchar aes_key[32], aes_iv[32];
 	memcpy(aes_key, key, 32);
diff --git a/Telegram/SourceFiles/mtproto/auth_key.h b/Telegram/SourceFiles/mtproto/auth_key.h
index cf8f7169e..dcfae3244 100644
--- a/Telegram/SourceFiles/mtproto/auth_key.h
+++ b/Telegram/SourceFiles/mtproto/auth_key.h
@@ -7,9 +7,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #pragma once
 
+#include "base/bytes.h"
 #include <array>
 #include <memory>
-#include "base/bytes.h"
 
 namespace MTP {
 
@@ -24,65 +24,29 @@ public:
 		ReadFromFile,
 		Local,
 	};
-	AuthKey(Type type, DcId dcId, const Data &data) : _type(type), _dcId(dcId), _key(data) {
-		countKeyId();
-	}
-	AuthKey(const Data &data) : _type(Type::Local), _key(data) {
-		countKeyId();
-	}
+	AuthKey(Type type, DcId dcId, const Data &data);
+	explicit AuthKey(const Data &data);
 
 	AuthKey(const AuthKey &other) = delete;
 	AuthKey &operator=(const AuthKey &other) = delete;
 
-	Type type() const {
-		return _type;
-	}
-
-	int dcId() const {
-		return _dcId;
-	}
-
-	KeyId keyId() const {
-		return _keyId;
-	}
+	Type type() const;
+	int dcId() const;
+	KeyId keyId() const;
 
 	void prepareAES_oldmtp(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const;
 	void prepareAES(const MTPint128 &msgKey, MTPint256 &aesKey, MTPint256 &aesIV, bool send) const;
 
-	const void *partForMsgKey(bool send) const {
-		return _key.data() + 88 + (send ? 0 : 8);
-	}
+	const void *partForMsgKey(bool send) const;
 
-	void write(QDataStream &to) const {
-		to.writeRawData(reinterpret_cast<const char*>(_key.data()), _key.size());
-	}
-	bytes::const_span data() const {
-		return _key;
-	}
+	void write(QDataStream &to) const;
+	bytes::const_span data() const;
+	bool equals(const std::shared_ptr<AuthKey> &other) const;
 
-	bool equals(const std::shared_ptr<AuthKey> &other) const {
-		return other ? (_key == other->_key) : false;
-	}
-
-	static void FillData(Data &authKey, bytes::const_span computedAuthKey) {
-		auto computedAuthKeySize = computedAuthKey.size();
-		Assert(computedAuthKeySize <= kSize);
-		auto authKeyBytes = gsl::make_span(authKey);
-		if (computedAuthKeySize < kSize) {
-			bytes::set_with_const(authKeyBytes.subspan(0, kSize - computedAuthKeySize), gsl::byte());
-			bytes::copy(authKeyBytes.subspan(kSize - computedAuthKeySize), computedAuthKey);
-		} else {
-			bytes::copy(authKeyBytes, computedAuthKey);
-		}
-	}
+	static void FillData(Data &authKey, bytes::const_span computedAuthKey);
 
 private:
-	void countKeyId() {
-		auto sha1 = hashSha1(_key.data(), _key.size());
-
-		// Lower 64 bits = 8 bytes of 20 byte SHA1 hash.
-		_keyId = *reinterpret_cast<KeyId*>(sha1.data() + 12);
-	}
+	void countKeyId();
 
 	Type _type = Type::Generated;
 	DcId _dcId = 0;
diff --git a/Telegram/SourceFiles/mtproto/concurrent_sender.cpp b/Telegram/SourceFiles/mtproto/concurrent_sender.cpp
index a17e7031f..1d687bbdb 100644
--- a/Telegram/SourceFiles/mtproto/concurrent_sender.cpp
+++ b/Telegram/SourceFiles/mtproto/concurrent_sender.cpp
@@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include "mtproto/mtp_instance.h"
 #include "mtproto/rpc_sender.h"
+#include "mtproto/facade.h"
 
 namespace MTP {
 
diff --git a/Telegram/SourceFiles/mtproto/config_loader.cpp b/Telegram/SourceFiles/mtproto/config_loader.cpp
index 7abc822cc..3f60590a5 100644
--- a/Telegram/SourceFiles/mtproto/config_loader.cpp
+++ b/Telegram/SourceFiles/mtproto/config_loader.cpp
@@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "mtproto/config_loader.h"
 
+#include "mtproto/facade.h"
 #include "mtproto/dc_options.h"
 #include "mtproto/mtp_instance.h"
 #include "mtproto/special_config_request.h"
diff --git a/Telegram/SourceFiles/mtproto/connection.cpp b/Telegram/SourceFiles/mtproto/connection.cpp
index 90b8d2056..c52b3aa7b 100644
--- a/Telegram/SourceFiles/mtproto/connection.cpp
+++ b/Telegram/SourceFiles/mtproto/connection.cpp
@@ -1187,7 +1187,7 @@ void ConnectionPrivate::connectToServer(bool afterConfig) {
 	if (_testConnections.empty()) {
 		if (_instance->isKeysDestroyer()) {
 			LOG(("MTP Error: DC %1 options for not found for auth key destruction!").arg(_shiftedDcId));
-			emit _instance->keyDestroyed(_shiftedDcId);
+			_instance->checkIfKeyWasDestroyed(_shiftedDcId);
 			return;
 		} else if (afterConfig) {
 			LOG(("MTP Error: DC %1 options for not found right after config load!").arg(_shiftedDcId));
@@ -2606,7 +2606,7 @@ void ConnectionPrivate::updateAuthKey() 	{
 	} else if (_instance->isKeysDestroyer()) {
 		// We are here to destroy an old key, so we're done.
 		LOG(("MTP Error: No key %1 in updateAuthKey() for destroying.").arg(_shiftedDcId));
-		emit _instance->keyDestroyed(_shiftedDcId);
+		_instance->checkIfKeyWasDestroyed(_shiftedDcId);
 		return;
 	}
 
@@ -3104,15 +3104,17 @@ void ConnectionPrivate::handleError(int errorCode) {
 	_waitForConnectedTimer.cancel();
 
 	if (errorCode == -404) {
-		if (_instance->isKeysDestroyer()) {
-			LOG(("MTP Info: -404 error received on destroying key %1, assuming it is destroyed.").arg(_shiftedDcId));
-			emit _instance->keyDestroyed(_shiftedDcId);
-			return;
-		} else if (_dcType == DcType::Cdn) {
+		if (_dcType == DcType::Cdn) {
 			LOG(("MTP Info: -404 error received in CDN dc %1, assuming it was destroyed, recreating.").arg(_shiftedDcId));
 			clearMessages();
 			keyId = kRecreateKeyId;
 			return restart();
+		} else {
+			LOG(("MTP Info: -404 error received, informing instance."));
+			_instance->checkIfKeyWasDestroyed(_shiftedDcId);
+			if (_instance->isKeysDestroyer()) {
+				return;
+			}
 		}
 	}
 	MTP_LOG(_shiftedDcId, ("Restarting after error in connection, error code: %1...").arg(errorCode));
diff --git a/Telegram/SourceFiles/mtproto/connection.h b/Telegram/SourceFiles/mtproto/connection.h
index 3155ddbcf..d947e537d 100644
--- a/Telegram/SourceFiles/mtproto/connection.h
+++ b/Telegram/SourceFiles/mtproto/connection.h
@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "mtproto/auth_key.h"
 #include "mtproto/dc_options.h"
 #include "mtproto/connection_abstract.h"
+#include "mtproto/facade.h"
 #include "base/openssl_help.h"
 #include "base/timer.h"
 
diff --git a/Telegram/SourceFiles/mtproto/connection_abstract.h b/Telegram/SourceFiles/mtproto/connection_abstract.h
index b524388e3..889fa831e 100644
--- a/Telegram/SourceFiles/mtproto/connection_abstract.h
+++ b/Telegram/SourceFiles/mtproto/connection_abstract.h
@@ -11,6 +11,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/bytes.h"
 
 namespace MTP {
+
+class Instance;
+
 namespace internal {
 
 struct ConnectionOptions;
diff --git a/Telegram/SourceFiles/mtproto/connection_resolving.cpp b/Telegram/SourceFiles/mtproto/connection_resolving.cpp
index 0822d009b..171b8885b 100644
--- a/Telegram/SourceFiles/mtproto/connection_resolving.cpp
+++ b/Telegram/SourceFiles/mtproto/connection_resolving.cpp
@@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "mtproto/connection_resolving.h"
 
+#include "mtproto/mtp_instance.h"
+
 namespace MTP {
 namespace internal {
 namespace {
diff --git a/Telegram/SourceFiles/mtproto/dc_options.cpp b/Telegram/SourceFiles/mtproto/dc_options.cpp
index 4dda90416..54d15ca1c 100644
--- a/Telegram/SourceFiles/mtproto/dc_options.cpp
+++ b/Telegram/SourceFiles/mtproto/dc_options.cpp
@@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "mtproto/dc_options.h"
 
+#include "mtproto/facade.h"
 #include "storage/serialize_common.h"
 #include "mtproto/connection_tcp.h"
 
diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp
index 543418a20..c6ab7d640 100644
--- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp
+++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.cpp
@@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "mtproto/dedicated_file_loader.h"
 
+#include "mtproto/facade.h"
 #include "main/main_session.h"
 #include "main/main_account.h" // Account::sessionChanges.
 #include "core/application.h"
diff --git a/Telegram/SourceFiles/mtproto/dedicated_file_loader.h b/Telegram/SourceFiles/mtproto/dedicated_file_loader.h
index a21769f2b..645f7e29e 100644
--- a/Telegram/SourceFiles/mtproto/dedicated_file_loader.h
+++ b/Telegram/SourceFiles/mtproto/dedicated_file_loader.h
@@ -7,9 +7,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #pragma once
 
-namespace MTP {
+#include "mtproto/mtp_instance.h"
 
-class Instance;
+namespace MTP {
 
 class WeakInstance : private QObject, private base::Subscriber {
 public:
diff --git a/Telegram/SourceFiles/mtproto/mtp_instance.cpp b/Telegram/SourceFiles/mtproto/mtp_instance.cpp
index 87f5b4f88..14ea8fae7 100644
--- a/Telegram/SourceFiles/mtproto/mtp_instance.cpp
+++ b/Telegram/SourceFiles/mtproto/mtp_instance.cpp
@@ -17,8 +17,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "mtproto/rsa_public_key.h"
 #include "storage/localstorage.h"
 #include "calls/calls_instance.h"
-#include "main/main_account.h"
-#include "main/main_session.h"
+#include "main/main_session.h" // Session::Exists.
+#include "main/main_account.h" // Account::configUpdated.
 #include "apiwrap.h"
 #include "core/application.h"
 #include "lang/lang_instance.h"
@@ -26,7 +26,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/unixtime.h"
 #include "base/call_delayed.h"
 #include "base/timer.h"
-#include "facades.h"
+#include "facades.h" // Proxies list.
 
 namespace MTP {
 namespace {
@@ -246,10 +246,6 @@ void Instance::Private::start(Config &&config) {
 	_deviceModel = std::move(config.deviceModel);
 	_systemVersion = std::move(config.systemVersion);
 
-	if (isKeysDestroyer()) {
-		_instance->connect(_instance, SIGNAL(keyDestroyed(qint32)), _instance, SLOT(onKeyDestroyed(qint32)), Qt::QueuedConnection);
-	}
-
 	for (auto &key : config.keys) {
 		auto dcId = key->dcId();
 		auto shiftedDcId = dcId;
@@ -1487,10 +1483,10 @@ void Instance::Private::performKeyDestroy(ShiftedDcId shiftedDcId) {
 		} break;
 		case mtpc_destroy_auth_key_none: LOG(("MTP Info: key %1 already destroyed.").arg(shiftedDcId)); break;
 		}
-		emit _instance->keyDestroyed(shiftedDcId);
+		_instance->checkIfKeyWasDestroyed(shiftedDcId);
 	}), rpcFail([this, shiftedDcId](const RPCError &error) {
 		LOG(("MTP Error: key %1 destruction resulted in error: %2").arg(shiftedDcId).arg(error.type()));
-		emit _instance->keyDestroyed(shiftedDcId);
+		_instance->checkIfKeyWasDestroyed(shiftedDcId);
 		return true;
 	}), shiftedDcId);
 }
@@ -1749,9 +1745,16 @@ void Instance::scheduleKeyDestroy(ShiftedDcId shiftedDcId) {
 	_private->scheduleKeyDestroy(shiftedDcId);
 }
 
-void Instance::onKeyDestroyed(qint32 shiftedDcId) {
-	_private->completedKeyDestroy(shiftedDcId);
+void Instance::checkIfKeyWasDestroyed(ShiftedDcId shiftedDcId) {
+	crl::on_main(this, [=] {
+		if (isKeysDestroyer()) {
+			LOG(("MTP Info: checkIfKeyWasDestroyed on destroying key %1, "
+				"assuming it is destroyed.").arg(shiftedDcId));
+			_private->completedKeyDestroy(shiftedDcId);
+		}
+	});
 }
+
 void Instance::sendRequest(
 		mtpRequestId requestId,
 		SecureRequest &&request,
diff --git a/Telegram/SourceFiles/mtproto/mtp_instance.h b/Telegram/SourceFiles/mtproto/mtp_instance.h
index c12d7432c..1e8424582 100644
--- a/Telegram/SourceFiles/mtproto/mtp_instance.h
+++ b/Telegram/SourceFiles/mtproto/mtp_instance.h
@@ -173,6 +173,7 @@ public:
 
 	bool isKeysDestroyer() const;
 	void scheduleKeyDestroy(ShiftedDcId shiftedDcId);
+	void checkIfKeyWasDestroyed(ShiftedDcId shiftedDcId);
 
 	void requestConfig();
 	void requestConfigIfOld();
@@ -190,16 +191,12 @@ public slots:
 signals:
 	void configLoaded();
 	void cdnConfigLoaded();
-	void keyDestroyed(qint32 shiftedDcId);
 	void allKeysDestroyed();
 	void proxyDomainResolved(
 		QString host,
 		QStringList ips,
 		qint64 expireAt);
 
-private slots:
-	void onKeyDestroyed(qint32 shiftedDcId);
-
 private:
 	void sendRequest(
 		mtpRequestId requestId,
diff --git a/Telegram/SourceFiles/mtproto/sender.h b/Telegram/SourceFiles/mtproto/sender.h
index 37210d3fa..ab6521735 100644
--- a/Telegram/SourceFiles/mtproto/sender.h
+++ b/Telegram/SourceFiles/mtproto/sender.h
@@ -8,6 +8,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #pragma once
 
 #include "base/variant.h"
+#include "mtproto/rpc_sender.h"
+#include "mtproto/mtp_instance.h"
+#include "mtproto/facade.h"
 
 namespace MTP {
 
diff --git a/Telegram/SourceFiles/stdafx.h b/Telegram/SourceFiles/stdafx.h
index 2f93879cf..0cc9711e8 100644
--- a/Telegram/SourceFiles/stdafx.h
+++ b/Telegram/SourceFiles/stdafx.h
@@ -120,7 +120,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "core/utils.h"
 #include "config.h"
 
-#include "mtproto/facade.h"
+#include "scheme.h"
+#include "mtproto/type_utils.h"
 
 #include "ui/style/style_core.h"
 #include "styles/palette.h"
diff --git a/Telegram/SourceFiles/storage/file_download.h b/Telegram/SourceFiles/storage/file_download.h
index 558a89bb4..c15d8745d 100644
--- a/Telegram/SourceFiles/storage/file_download.h
+++ b/Telegram/SourceFiles/storage/file_download.h
@@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/timer.h"
 #include "base/binary_guard.h"
 #include "data/data_file_origin.h"
+#include "mtproto/facade.h"
 
 #include <QtNetwork/QNetworkReply>
 
diff --git a/Telegram/SourceFiles/storage/file_upload.h b/Telegram/SourceFiles/storage/file_upload.h
index df6f3bc2c..7890845b2 100644
--- a/Telegram/SourceFiles/storage/file_upload.h
+++ b/Telegram/SourceFiles/storage/file_upload.h
@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #pragma once
 
 #include "api/api_common.h"
+#include "mtproto/facade.h"
 
 #include <QtCore/QTimer>
 
diff --git a/Telegram/SourceFiles/window/window_connecting_widget.cpp b/Telegram/SourceFiles/window/window_connecting_widget.cpp
index 718b17ba5..154614247 100644
--- a/Telegram/SourceFiles/window/window_connecting_widget.cpp
+++ b/Telegram/SourceFiles/window/window_connecting_widget.cpp
@@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "ui/widgets/buttons.h"
 #include "ui/effects/radial_animation.h"
 #include "ui/ui_utility.h"
+#include "mtproto/facade.h"
 #include "window/themes/window_theme.h"
 #include "boxes/connection_box.h"
 #include "boxes/abstract_box.h"