From e06cf7b5d348c1f255f56e4a4f7f001e49337ca6 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sat, 4 Jul 2020 19:20:45 +0200 Subject: [PATCH] if the passphrase cache has no valid passphrase while in messageToSend(), ask the app --- callback_dispatcher.cc | 25 ++++++++++++++++++++++--- callback_dispatcher.hh | 3 +++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index 6cd6085..317247d 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -8,9 +8,6 @@ pEp::CallbackDispatcher pEp::callback_dispatcher; namespace pEp { PEP_STATUS CallbackDispatcher::messageToSend(::message *msg) { - if (Adapter::on_sync_thread() && !msg) - return PassphraseCache::messageToSend(passphrase_cache, Adapter::session()); - return callback_dispatcher._messageToSend(msg); } @@ -32,6 +29,10 @@ namespace pEp { throw std::invalid_argument("messageToSend must be set"); targets.push_back({messageToSend, notifyHandshake, on_startup, shutdown}); + + // try_unlock possibly waiting messageToSend + sync_mtx.try_lock(); + sync_mtx.unlock(); } void CallbackDispatcher::remove(::messageToSend_t messageToSend) @@ -91,6 +92,24 @@ namespace pEp { PEP_STATUS CallbackDispatcher::_messageToSend(::message *msg) { + if (Adapter::on_sync_thread() && !msg) { + PEP_STATUS status = PassphraseCache::messageToSend(passphrase_cache, Adapter::session()); + + // if the cache has no valid passphrase ask the app + if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) { + // lock mutex and call async + sync_mtx.lock(); + notifyHandshake(nullptr, nullptr, SYNC_PASSPHRASE_REQUIRED); + + // sync_mtx.wait() until mutex was unlocked by add() + sync_mtx.lock(); + sync_mtx.unlock(); + } + + // the pEp engine must try again + return PEP_STATUS_OK; + } + for (auto target : targets) { ::message *_msg = nullptr; if (msg) { diff --git a/callback_dispatcher.hh b/callback_dispatcher.hh index aeee166..95e5a31 100644 --- a/callback_dispatcher.hh +++ b/callback_dispatcher.hh @@ -2,6 +2,7 @@ #include #include +#include #include "Adapter.hh" namespace pEp { @@ -21,6 +22,8 @@ namespace pEp { }; std::vector targets; + std::mutex sync_mtx; + public: void add( ::messageToSend_t messageToSend,