Guard click handlers that capture session data.

Click handlers invocation is done by posting on_main,
so in rare cases the session may be already destroyed.
This commit is contained in:
John Preston 2020-05-04 17:38:49 +04:00
parent 038d8f1781
commit 6adcf660f1
6 changed files with 75 additions and 30 deletions

View File

@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_document_good_thumbnail.h" #include "data/data_document_good_thumbnail.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "inline_bots/inline_bot_layout_item.h" #include "inline_bots/inline_bot_layout_item.h"
#include "main/main_session.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "core/file_utilities.h" #include "core/file_utilities.h"
#include "core/media_active_cache.h" #include "core/media_active_cache.h"
@ -296,6 +297,14 @@ QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = fals
return FileNameForSave(caption, filter, prefix, name, forceSavingAs, dir); return FileNameForSave(caption, filter, prefix, name, forceSavingAs, dir);
} }
DocumentClickHandler::DocumentClickHandler(
not_null<DocumentData*> document,
FullMsgId context)
: FileClickHandler(context)
, _session(&document->session())
, _document(document) {
}
void DocumentOpenClickHandler::Open( void DocumentOpenClickHandler::Open(
Data::FileOrigin origin, Data::FileOrigin origin,
not_null<DocumentData*> data, not_null<DocumentData*> data,
@ -345,7 +354,9 @@ void DocumentOpenClickHandler::Open(
} }
void DocumentOpenClickHandler::onClickImpl() const { void DocumentOpenClickHandler::onClickImpl() const {
Open(context(), document(), getActionItem()); if (valid()) {
Open(context(), document(), getActionItem());
}
} }
void DocumentSaveClickHandler::Save( void DocumentSaveClickHandler::Save(
@ -385,14 +396,20 @@ void DocumentSaveClickHandler::Save(
} }
void DocumentSaveClickHandler::onClickImpl() const { void DocumentSaveClickHandler::onClickImpl() const {
Save(context(), document()); if (valid()) {
Save(context(), document());
}
} }
void DocumentCancelClickHandler::onClickImpl() const { void DocumentCancelClickHandler::onClickImpl() const {
const auto data = document(); if (!valid()) {
if (!data->date) return; return;
}
if (data->uploading()) { const auto data = document();
if (!data->date) {
return;
} else if (data->uploading()) {
if (const auto item = data->owner().message(context())) { if (const auto item = data->owner().message(context())) {
App::main()->cancelUploadLayer(item); App::main()->cancelUploadLayer(item);
} }
@ -423,7 +440,9 @@ void DocumentOpenWithClickHandler::Open(
} }
void DocumentOpenWithClickHandler::onClickImpl() const { void DocumentOpenWithClickHandler::onClickImpl() const {
Open(context(), document()); if (valid()) {
Open(context(), document());
}
} }
Data::FileOrigin StickerData::setOrigin() const { Data::FileOrigin StickerData::setOrigin() const {

View File

@ -315,16 +315,19 @@ class DocumentClickHandler : public FileClickHandler {
public: public:
DocumentClickHandler( DocumentClickHandler(
not_null<DocumentData*> document, not_null<DocumentData*> document,
FullMsgId context = FullMsgId()) FullMsgId context = FullMsgId());
: FileClickHandler(context)
, _document(document) { [[nodiscard]] bool valid() const {
return !_session.empty();
} }
not_null<DocumentData*> document() const {
[[nodiscard]] not_null<DocumentData*> document() const {
return _document; return _document;
} }
private: private:
not_null<DocumentData*> _document; const base::weak_ptr<Main::Session> _session;
const not_null<DocumentData*> _document;
}; };

View File

@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_file_origin.h" #include "data/data_file_origin.h"
#include "ui/image/image.h" #include "ui/image/image.h"
#include "ui/image/image_source.h" #include "ui/image/image_source.h"
#include "main/main_session.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "core/application.h" #include "core/application.h"
#include "facades.h" #include "facades.h"
@ -250,22 +251,42 @@ int PhotoData::height() const {
return _large->height(); return _large->height();
} }
PhotoClickHandler::PhotoClickHandler(
not_null<PhotoData*> photo,
FullMsgId context,
PeerData *peer)
: FileClickHandler(context)
, _session(&photo->session())
, _photo(photo)
, _peer(peer) {
}
void PhotoOpenClickHandler::onClickImpl() const { void PhotoOpenClickHandler::onClickImpl() const {
Core::App().showPhoto(this); if (valid()) {
Core::App().showPhoto(this);
}
} }
void PhotoSaveClickHandler::onClickImpl() const { void PhotoSaveClickHandler::onClickImpl() const {
auto data = photo(); if (!valid()) {
if (!data->date) return; return;
}
data->download(context()); const auto data = photo();
if (!data->date) {
return;
} else {
data->download(context());
}
} }
void PhotoCancelClickHandler::onClickImpl() const { void PhotoCancelClickHandler::onClickImpl() const {
auto data = photo(); if (!valid()) {
if (!data->date) return; return;
}
if (data->uploading()) { const auto data = photo();
if (!data->date) {
return;
} else if (data->uploading()) {
if (const auto item = data->owner().message(context())) { if (const auto item = data->owner().message(context())) {
App::main()->cancelUploadLayer(item); App::main()->cancelUploadLayer(item);
} }

View File

@ -108,21 +108,23 @@ public:
PhotoClickHandler( PhotoClickHandler(
not_null<PhotoData*> photo, not_null<PhotoData*> photo,
FullMsgId context = FullMsgId(), FullMsgId context = FullMsgId(),
PeerData *peer = nullptr) PeerData *peer = nullptr);
: FileClickHandler(context)
, _photo(photo) [[nodiscard]] bool valid() const {
, _peer(peer) { return !_session.empty();
} }
not_null<PhotoData*> photo() const {
[[nodiscard]] not_null<PhotoData*> photo() const {
return _photo; return _photo;
} }
PeerData *peer() const { [[nodiscard]] PeerData *peer() const {
return _peer; return _peer;
} }
private: private:
not_null<PhotoData*> _photo; const base::weak_ptr<Main::Session> _session;
PeerData *_peer = nullptr; const not_null<PhotoData*> _photo;
PeerData * const _peer = nullptr;
}; };

View File

@ -483,7 +483,7 @@ public:
_context = context; _context = context;
} }
FullMsgId context() const { [[nodiscard]] FullMsgId context() const {
return _context; return _context;
} }

@ -1 +1 @@
Subproject commit d27c3e363254b169f8886934df8580fdc76d424e Subproject commit 9e01ede6661e2a74697283c7836d4fae433f50ce