Commit 731dc1c7 authored by Adrien Béraud's avatar Adrien Béraud

jamiaccount: check account state, lock mutex for public ops

Change-Id: Idc8c36466c9563d6ed9fbd1c0f7ec401807ba382
parent 958a991e
...@@ -380,7 +380,7 @@ ContactList::foundAccountDevice(const dht::InfoHash& device, ...@@ -380,7 +380,7 @@ ContactList::foundAccountDevice(const dht::InfoHash& device,
if (it.second) { if (it.second) {
JAMI_DBG("[Contacts] Found account device: %s %s", name.c_str(), device.toString().c_str()); JAMI_DBG("[Contacts] Found account device: %s %s", name.c_str(), device.toString().c_str());
saveKnownDevices(); saveKnownDevices();
callbacks_.devicesChanged(); callbacks_.devicesChanged(knownDevices_);
} else { } else {
// update device name // update device name
if (not name.empty() and it.first->second.name != name) { if (not name.empty() and it.first->second.name != name) {
...@@ -389,7 +389,7 @@ ContactList::foundAccountDevice(const dht::InfoHash& device, ...@@ -389,7 +389,7 @@ ContactList::foundAccountDevice(const dht::InfoHash& device,
device.toString().c_str()); device.toString().c_str());
it.first->second.name = name; it.first->second.name = name;
saveKnownDevices(); saveKnownDevices();
callbacks_.devicesChanged(); callbacks_.devicesChanged(knownDevices_);
} }
} }
} }
...@@ -419,7 +419,7 @@ ContactList::foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& ...@@ -419,7 +419,7 @@ ContactList::foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>&
crt->getId().toString().c_str()); crt->getId().toString().c_str());
tls::CertificateStore::instance().pinCertificate(crt); tls::CertificateStore::instance().pinCertificate(crt);
saveKnownDevices(); saveKnownDevices();
callbacks_.devicesChanged(); callbacks_.devicesChanged(knownDevices_);
} else { } else {
// update device name // update device name
if (not name.empty() and it.first->second.name != name) { if (not name.empty() and it.first->second.name != name) {
...@@ -428,7 +428,7 @@ ContactList::foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& ...@@ -428,7 +428,7 @@ ContactList::foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>&
crt->getId().toString().c_str()); crt->getId().toString().c_str());
it.first->second.name = name; it.first->second.name = name;
saveKnownDevices(); saveKnownDevices();
callbacks_.devicesChanged(); callbacks_.devicesChanged(knownDevices_);
} }
} }
return true; return true;
......
...@@ -42,7 +42,7 @@ public: ...@@ -42,7 +42,7 @@ public:
using OnContactRemoved = std::function<void(const std::string&, bool)>; using OnContactRemoved = std::function<void(const std::string&, bool)>;
using OnIncomingTrustRequest using OnIncomingTrustRequest
= std::function<void(const std::string&, const std::vector<uint8_t>&, time_t)>; = std::function<void(const std::string&, const std::vector<uint8_t>&, time_t)>;
using OnDevicesChanged = std::function<void()>; using OnDevicesChanged = std::function<void(const std::map<dht::InfoHash, KnownDevice>&)>;
struct OnChangeCallback struct OnChangeCallback
{ {
......
...@@ -1167,11 +1167,17 @@ JamiAccount::loadAccount(const std::string& archive_password, ...@@ -1167,11 +1167,17 @@ JamiAccount::loadAccount(const std::string& archive_password,
received); received);
}); });
}, },
[this]() { [this](const std::map<dht::InfoHash, KnownDevice>& devices) {
dht::ThreadPool::computation().run([id = getAccountID(), devices = getKnownDevices()] { std::map<std::string, std::string> ids;
for (auto& d : devices) {
auto id = d.first.toString();
auto label = d.second.name.empty() ? id.substr(0, 8) : d.second.name;
ids.emplace(std::move(id), std::move(label));
}
dht::ThreadPool::computation().run([id=getAccountID(), devices=std::move(ids)] {
emitSignal<DRing::ConfigurationSignal::KnownDevicesChanged>(id, devices); emitSignal<DRing::ConfigurationSignal::KnownDevicesChanged>(id, devices);
}); });
}, }
}; };
try { try {
...@@ -1497,6 +1503,7 @@ JamiAccount::getVolatileAccountDetails() const ...@@ -1497,6 +1503,7 @@ JamiAccount::getVolatileAccountDetails() const
void void
JamiAccount::lookupName(const std::string& name) JamiAccount::lookupName(const std::string& name)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
accountManager_->lookupUri(name, accountManager_->lookupUri(name,
nameServer_, nameServer_,
...@@ -1510,6 +1517,7 @@ JamiAccount::lookupName(const std::string& name) ...@@ -1510,6 +1517,7 @@ JamiAccount::lookupName(const std::string& name)
void void
JamiAccount::lookupAddress(const std::string& addr) JamiAccount::lookupAddress(const std::string& addr)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
auto acc = getAccountID(); auto acc = getAccountID();
if (accountManager_) if (accountManager_)
accountManager_->lookupAddress( accountManager_->lookupAddress(
...@@ -1524,6 +1532,7 @@ JamiAccount::lookupAddress(const std::string& addr) ...@@ -1524,6 +1532,7 @@ JamiAccount::lookupAddress(const std::string& addr)
void void
JamiAccount::registerName(const std::string& password, const std::string& name) JamiAccount::registerName(const std::string& password, const std::string& name)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
accountManager_->registerName( accountManager_->registerName(
password, password,
...@@ -2695,6 +2704,7 @@ JamiAccount::isMessageTreated(const std::string& id) ...@@ -2695,6 +2704,7 @@ JamiAccount::isMessageTreated(const std::string& id)
std::map<std::string, std::string> std::map<std::string, std::string>
JamiAccount::getKnownDevices() const JamiAccount::getKnownDevices() const
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (not accountManager_ or not accountManager_->getInfo()) if (not accountManager_ or not accountManager_->getInfo())
return {}; return {};
std::map<std::string, std::string> ids; std::map<std::string, std::string> ids;
...@@ -2935,6 +2945,7 @@ JamiAccount::getContactHeader(pjsip_transport* t) ...@@ -2935,6 +2945,7 @@ JamiAccount::getContactHeader(pjsip_transport* t)
void void
JamiAccount::addContact(const std::string& uri, bool confirmed) JamiAccount::addContact(const std::string& uri, bool confirmed)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
accountManager_->addContact(uri, confirmed); accountManager_->addContact(uri, confirmed);
else else
...@@ -2944,33 +2955,34 @@ JamiAccount::addContact(const std::string& uri, bool confirmed) ...@@ -2944,33 +2955,34 @@ JamiAccount::addContact(const std::string& uri, bool confirmed)
void void
JamiAccount::removeContact(const std::string& uri, bool ban) JamiAccount::removeContact(const std::string& uri, bool ban)
{ {
if (accountManager_) {
accountManager_->removeContact(uri, ban); std::lock_guard<std::mutex> lock(configurationMutex_);
else if (accountManager_)
JAMI_WARN("[Account %s] removeContact: account not loaded", getAccountID().c_str()); accountManager_->removeContact(uri, ban);
else
JAMI_WARN("[Account %s] removeContact: account not loaded", getAccountID().c_str());
}
// Remove current connections with contact // Remove current connections with contact
dht::InfoHash peer_account(uri);
std::unique_lock<std::mutex> lk(sipConnectionsMtx_);
std::set<std::string> devices; std::set<std::string> devices;
for (const auto& deviceConn : sipConnections_[uri]) { {
devices.emplace(deviceConn.first); std::unique_lock<std::mutex> lk(sipConnectionsMtx_);
} for (const auto& deviceConn: sipConnections_[uri]) {
sipConnections_.erase(uri); devices.emplace(deviceConn.first);
}
sipConnections_.erase(uri);
for (auto pendingIt = pendingSipConnections_.begin(); for (auto pendingIt = pendingSipConnections_.begin(); pendingIt != pendingSipConnections_.end();) {
pendingIt != pendingSipConnections_.end();) { if (uri == pendingIt->first) {
if (uri == pendingIt->first) { devices.emplace(pendingIt->second);
devices.emplace(pendingIt->second); pendingIt = pendingSipConnections_.erase(pendingIt);
pendingIt = pendingSipConnections_.erase(pendingIt); } else {
} else { ++pendingIt;
++pendingIt; }
} }
} }
lk.unlock(); for (const auto& device: devices) {
for (const auto& device : devices) {
if (connectionManager_) if (connectionManager_)
connectionManager_->closeConnectionsWith(device); connectionManager_->closeConnectionsWith(device);
} }
...@@ -2979,6 +2991,7 @@ JamiAccount::removeContact(const std::string& uri, bool ban) ...@@ -2979,6 +2991,7 @@ JamiAccount::removeContact(const std::string& uri, bool ban)
std::map<std::string, std::string> std::map<std::string, std::string>
JamiAccount::getContactDetails(const std::string& uri) const JamiAccount::getContactDetails(const std::string& uri) const
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
return (accountManager_ and accountManager_->getInfo()) return (accountManager_ and accountManager_->getInfo())
? accountManager_->getContactDetails(uri) ? accountManager_->getContactDetails(uri)
: std::map<std::string, std::string> {}; : std::map<std::string, std::string> {};
...@@ -2987,6 +3000,7 @@ JamiAccount::getContactDetails(const std::string& uri) const ...@@ -2987,6 +3000,7 @@ JamiAccount::getContactDetails(const std::string& uri) const
std::vector<std::map<std::string, std::string>> std::vector<std::map<std::string, std::string>>
JamiAccount::getContacts() const JamiAccount::getContacts() const
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (not accountManager_) if (not accountManager_)
return {}; return {};
return accountManager_->getContacts(); return accountManager_->getContacts();
...@@ -2997,13 +3011,14 @@ JamiAccount::getContacts() const ...@@ -2997,13 +3011,14 @@ JamiAccount::getContacts() const
std::vector<std::map<std::string, std::string>> std::vector<std::map<std::string, std::string>>
JamiAccount::getTrustRequests() const JamiAccount::getTrustRequests() const
{ {
return accountManager_ ? accountManager_->getTrustRequests() std::lock_guard<std::mutex> lock(configurationMutex_);
: std::vector<std::map<std::string, std::string>> {}; return accountManager_ ? accountManager_->getTrustRequests() : std::vector<std::map<std::string, std::string>>{};
} }
bool bool
JamiAccount::acceptTrustRequest(const std::string& from) JamiAccount::acceptTrustRequest(const std::string& from)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
return accountManager_->acceptTrustRequest(from); return accountManager_->acceptTrustRequest(from);
JAMI_WARN("[Account %s] acceptTrustRequest: account not loaded", getAccountID().c_str()); JAMI_WARN("[Account %s] acceptTrustRequest: account not loaded", getAccountID().c_str());
...@@ -3013,6 +3028,7 @@ JamiAccount::acceptTrustRequest(const std::string& from) ...@@ -3013,6 +3028,7 @@ JamiAccount::acceptTrustRequest(const std::string& from)
bool bool
JamiAccount::discardTrustRequest(const std::string& from) JamiAccount::discardTrustRequest(const std::string& from)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
return accountManager_->discardTrustRequest(from); return accountManager_->discardTrustRequest(from);
JAMI_WARN("[Account %s] discardTrustRequest: account not loaded", getAccountID().c_str()); JAMI_WARN("[Account %s] discardTrustRequest: account not loaded", getAccountID().c_str());
...@@ -3022,6 +3038,7 @@ JamiAccount::discardTrustRequest(const std::string& from) ...@@ -3022,6 +3038,7 @@ JamiAccount::discardTrustRequest(const std::string& from)
void void
JamiAccount::sendTrustRequest(const std::string& to, const std::vector<uint8_t>& payload) JamiAccount::sendTrustRequest(const std::string& to, const std::vector<uint8_t>& payload)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
accountManager_->sendTrustRequest(to, payload); accountManager_->sendTrustRequest(to, payload);
else else
...@@ -3031,6 +3048,7 @@ JamiAccount::sendTrustRequest(const std::string& to, const std::vector<uint8_t>& ...@@ -3031,6 +3048,7 @@ JamiAccount::sendTrustRequest(const std::string& to, const std::vector<uint8_t>&
void void
JamiAccount::sendTrustRequestConfirm(const std::string& to) JamiAccount::sendTrustRequestConfirm(const std::string& to)
{ {
std::lock_guard<std::mutex> lock(configurationMutex_);
if (accountManager_) if (accountManager_)
accountManager_->sendTrustRequestConfirm(dht::InfoHash(to)); accountManager_->sendTrustRequestConfirm(dht::InfoHash(to));
else else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment