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 "lang/lang_keys.h"
#include "inline_bots/inline_bot_layout_item.h"
#include "main/main_session.h"
#include "mainwidget.h"
#include "core/file_utilities.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);
}
DocumentClickHandler::DocumentClickHandler(
not_null<DocumentData*> document,
FullMsgId context)
: FileClickHandler(context)
, _session(&document->session())
, _document(document) {
}
void DocumentOpenClickHandler::Open(
Data::FileOrigin origin,
not_null<DocumentData*> data,
@ -345,8 +354,10 @@ void DocumentOpenClickHandler::Open(
}
void DocumentOpenClickHandler::onClickImpl() const {
if (valid()) {
Open(context(), document(), getActionItem());
}
}
void DocumentSaveClickHandler::Save(
Data::FileOrigin origin,
@ -385,14 +396,20 @@ void DocumentSaveClickHandler::Save(
}
void DocumentSaveClickHandler::onClickImpl() const {
if (valid()) {
Save(context(), document());
}
}
void DocumentCancelClickHandler::onClickImpl() const {
const auto data = document();
if (!data->date) return;
if (!valid()) {
return;
}
if (data->uploading()) {
const auto data = document();
if (!data->date) {
return;
} else if (data->uploading()) {
if (const auto item = data->owner().message(context())) {
App::main()->cancelUploadLayer(item);
}
@ -423,8 +440,10 @@ void DocumentOpenWithClickHandler::Open(
}
void DocumentOpenWithClickHandler::onClickImpl() const {
if (valid()) {
Open(context(), document());
}
}
Data::FileOrigin StickerData::setOrigin() const {
return set.match([&](const MTPDinputStickerSetID &data) {

View File

@ -315,16 +315,19 @@ class DocumentClickHandler : public FileClickHandler {
public:
DocumentClickHandler(
not_null<DocumentData*> document,
FullMsgId context = FullMsgId())
: FileClickHandler(context)
, _document(document) {
FullMsgId context = FullMsgId());
[[nodiscard]] bool valid() const {
return !_session.empty();
}
not_null<DocumentData*> document() const {
[[nodiscard]] not_null<DocumentData*> document() const {
return _document;
}
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 "ui/image/image.h"
#include "ui/image/image_source.h"
#include "main/main_session.h"
#include "mainwidget.h"
#include "core/application.h"
#include "facades.h"
@ -250,22 +251,42 @@ int PhotoData::height() const {
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 {
if (valid()) {
Core::App().showPhoto(this);
}
}
void PhotoSaveClickHandler::onClickImpl() const {
auto data = photo();
if (!data->date) return;
if (!valid()) {
return;
}
const auto data = photo();
if (!data->date) {
return;
} else {
data->download(context());
}
}
void PhotoCancelClickHandler::onClickImpl() const {
auto data = photo();
if (!data->date) return;
if (data->uploading()) {
if (!valid()) {
return;
}
const auto data = photo();
if (!data->date) {
return;
} else if (data->uploading()) {
if (const auto item = data->owner().message(context())) {
App::main()->cancelUploadLayer(item);
}

View File

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

View File

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

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