}
FdBroker::FdBroker(bool mock) : mock_(mock) {
- FdBrokerManager::GetInst().Add(this);
}
FdBroker::~FdBroker() {
- FdBrokerManager::GetInst().Remove(this);
Cancel();
}
void FdBroker::Cancel() {
- std::lock_guard<std::recursive_mutex> lock(mutex_);
+ std::lock_guard<std::recursive_mutex> lock(GetMutex());
LOGI("FdBroker::Cancel!");
if (cancellable_) {
cancellable_ = nullptr;
}
- if (conn_timer_ > 0) {
- g_source_remove(conn_timer_);
- conn_timer_ = 0;
+ if (res_data_) {
+ DestroyWeakPtr(res_data_);
+ res_data_ = nullptr;
}
+ UnsetConnTimer();
+
if (watcher_id_ > 0) {
g_bus_unwatch_name(watcher_id_);
watcher_id_ = 0;
return -1; // LCOV_EXCL_LINE
}
+ std::lock_guard<std::recursive_mutex> lock(GetMutex());
if (cancellable_) {
g_cancellable_cancel(cancellable_); // LCOV_EXCL_LINE
g_object_unref(cancellable_); // LCOV_EXCL_LINE
return -1; // LCOV_EXCL_LINE
}
+ if (res_data_)
+ DestroyWeakPtr(res_data_);
+
+ res_data_ = CreateWeakPtr();
+ if (res_data_ == nullptr) {
+ LOGE("Failed to create weak ptr");
+ g_object_unref(msg);
+ return -1;
+ }
+
g_dbus_message_set_unix_fd_list(msg, fd_list.GetRaw());
g_dbus_connection_send_message_with_reply(
DBusConnectionManager::GetInst().GetConnection(),
msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE,
- 5000, NULL, cancellable_, OnResultReceived, this);
+ 5000, NULL, cancellable_, OnResultReceived, res_data_);
g_object_unref(msg);
(*fds)[0] = main_sock_pair.Detach(SocketPair::SENDER);
const gchar *iface_name, const gchar *method_name,
GVariant *parameters, GDBusMethodInvocation *invocation,
gpointer user_data) {
- FdBroker* broker = static_cast<FdBroker*>(user_data);
- if (!FdBrokerManager::GetInst().Exist(broker)) {
- LOGE("No such FdBroker(%p)", broker);
+ auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(user_data);
+ auto broker = ptr->lock();
+ if (broker == nullptr) {
+ LOGE("broker is nullptr");
return;
}
- std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+ std::lock_guard<std::recursive_mutex> lock(broker->GetMutex());
if (broker->registration_id_ == 0) {
- LOGE("Invalid context. FdBroker(%p)", broker);
+ LOGE("Invalid context. FdBroker(%p)", broker.get());
return;
}
return -1;
}
+ auto broker = CreateWeakPtr();
+ if (broker == nullptr) {
+ LOGE("Failed to create weak ptr");
+ return -1;
+ }
+
+ std::lock_guard<std::recursive_mutex> lock(GetMutex());
registration_id_ = g_dbus_connection_register_object(
DBusConnectionManager::GetInst().GetConnection(),
RPC_PORT_OBJECT_PATH, introspection_data->interfaces[0],
- &interface_vtable, this, nullptr, nullptr);
+ &interface_vtable, broker, DestroyWeakPtr, nullptr);
g_dbus_node_info_unref(introspection_data);
if (registration_id_ == 0) {
const gchar *name,
const gchar *name_owner,
gpointer user_data) {
- FdBroker* broker = static_cast<FdBroker*>(user_data);
- if (!FdBrokerManager::GetInst().Exist(broker)) {
- LOGE("No such FdBroker(%p)", broker);
+ auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(user_data);
+ auto broker = ptr->lock();
+ if (broker == nullptr) {
+ LOGE("broker is nullptr");
return;
}
- std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+ std::lock_guard<std::recursive_mutex> lock(broker->GetMutex());
if (broker->watcher_id_ == 0) {
- LOGE("Invalid context. FdBroker(%p)", broker);
+ LOGE("Invalid context. FdBroker(%p)", broker.get());
return;
}
- if (broker->conn_timer_ > 0) {
- g_source_remove(broker->conn_timer_);
- broker->conn_timer_ = 0;
- }
-
+ broker->UnsetConnTimer();
int pid = broker->GetSenderPid(connection, name_owner);
char owner_appid[255] = { 0, };
void FdBroker::OnNameVanished(GDBusConnection *connection,
const gchar *name,
gpointer user_data) {
- FdBroker* broker = static_cast<FdBroker*>(user_data);
- if (!FdBrokerManager::GetInst().Exist(broker)) {
- LOGE("No such FdBroker(%p)", broker);
+ auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(user_data);
+ auto broker = ptr->lock();
+ if (broker == nullptr) {
+ LOGE("broker is nullptr");
return;
}
- std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+ std::lock_guard<std::recursive_mutex> lock(broker->GetMutex());
if (broker->watcher_id_ == 0) {
- LOGE("Invalid context. FdBroker(%p)", broker);
+ LOGE("Invalid context. FdBroker(%p)", broker.get());
return;
}
int FdBroker::Watch(IEventWatcher* ev, const std::string& target_appid,
const std::string& port_name) {
- std::lock_guard<std::recursive_mutex> lock(mutex_);
+ std::lock_guard<std::recursive_mutex> lock(GetMutex());
if (ev == nullptr)
return -1;
watch_appid_ = target_appid;
watch_port_name_ = port_name;
- if (conn_timer_ > 0)
- g_source_remove(conn_timer_);
-
- conn_timer_ = g_timeout_add(10 * 1000, OnDbusNameTimeout, this);
+ UnsetConnTimer();
+ SetConnTimer();
if (mock_) {
// LCOV_EXCL_START
// LCOV_EXCL_STOP
}
+ auto broker = CreateWeakPtr();
+ if (broker == nullptr) {
+ LOGE("Failed to create weak ptr");
+ return -1;
+ }
+
watcher_id_ = g_bus_watch_name_on_connection(
DBusConnectionManager::GetInst().GetConnection(),
interface_name.c_str(),
G_BUS_NAME_WATCHER_FLAGS_NONE,
OnNameAppeared,
OnNameVanished,
- this,
- NULL);
+ broker,
+ DestroyWeakPtr);
if (watcher_id_ == 0) {
// LCOV_EXCL_START
LOGE("Failed to watch connection(%s)", interface_name.c_str());
void FdBroker::OnResultReceived(GObject* source_object,
GAsyncResult* res,
gpointer user_data) {
- LOGW("OnResultReceived() : FdBroker(%p)", user_data);
+ LOGW("OnResultReceived()");
GDBusConnection* conn = reinterpret_cast<GDBusConnection*>(source_object);
GError* err = nullptr;
GDBusMessage* reply = g_dbus_connection_send_message_with_reply_finish(conn,
return;
}
- FdBroker* broker = static_cast<FdBroker*>(user_data);
- if (!FdBrokerManager::GetInst().Exist(broker)) {
- LOGE("No such FdBroker(%p)", broker);
+ auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(user_data);
+ auto broker = ptr->lock();
+ if (broker == nullptr) {
+ LOGE("broker is nullptr");
return;
}
- std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
+ std::lock_guard<std::recursive_mutex> lock(broker->GetMutex());
if (broker->cancellable_ == nullptr) {
- LOGE("Invalid context. Fdbroker(%p)", broker);
+ LOGE("Invalid context. Fdbroker(%p)", broker.get());
return;
}
}
gboolean FdBroker::OnDbusNameTimeout(gpointer user_data) {
- FdBroker* broker = static_cast<FdBroker*>(user_data);
- if (!FdBrokerManager::GetInst().Exist(broker)) {
- LOGE("No such FdBroker(%p)", broker);
+ auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(user_data);
+ auto broker = ptr->lock();
+ if (broker == nullptr) {
+ LOGE("broker is nullptr");
return G_SOURCE_REMOVE;
}
- std::lock_guard<std::recursive_mutex> lock(broker->mutex_);
- if (broker->conn_timer_ == 0) {
- LOGE("Invalid context. FdBroker(%p)", broker);
+ std::lock_guard<std::recursive_mutex> lock(broker->GetMutex());
+ if (broker->conn_data_ == nullptr) {
+ LOGE("Invalid context. FdBroker(%p)", broker.get());
return G_SOURCE_REMOVE;
}
- broker->conn_timer_ = 0;
+ DestroyWeakPtr(broker->conn_data_);
+ broker->conn_data_ = nullptr;
broker->Cancel();
auto* watcher = broker->watcher_;
watcher->OnPortRejected(broker->watch_appid_, RPC_PORT_ERROR_IO_ERROR);
return G_SOURCE_REMOVE;
}
-FdBrokerManager& FdBrokerManager::GetInst() {
- static FdBrokerManager inst;
- return inst;
+std::shared_ptr<FdBroker> FdBroker::GetSharedPtr() {
+ return shared_from_this();
+}
+
+gpointer FdBroker::CreateWeakPtr() {
+ auto* ptr = new (std::nothrow) std::weak_ptr<FdBroker>(GetSharedPtr());
+ return static_cast<gpointer>(ptr);
}
-void FdBrokerManager::Add(FdBroker* broker) {
- std::lock_guard<std::recursive_mutex> lock(mutex_);
- brokers_.push_back(broker);
+void FdBroker::DestroyWeakPtr(gpointer data) {
+ auto* ptr = static_cast<std::weak_ptr<FdBroker>*>(data);
+ delete ptr;
}
-void FdBrokerManager::Remove(FdBroker* broker) {
- std::lock_guard<std::recursive_mutex> lock(mutex_);
- brokers_.remove(broker);
+void FdBroker::SetConnTimer() {
+ if (conn_data_) {
+ LOGW("Already exists");
+ return;
+ }
+
+ conn_data_ = CreateWeakPtr();
+ if (conn_data_ == nullptr)
+ LOGE("Out of memory");
+
+ g_timeout_add(10 * 1000, OnDbusNameTimeout, conn_data_);
}
-bool FdBrokerManager::Exist(FdBroker* broker) {
- std::lock_guard<std::recursive_mutex> lock(mutex_);
- auto iter = std::find(brokers_.begin(), brokers_.end(), broker);
- if (iter != brokers_.end())
- return true;
+void FdBroker::UnsetConnTimer() {
+ if (conn_data_ == nullptr)
+ return;
+
+ GSource* source = g_main_context_find_source_by_user_data(nullptr,
+ conn_data_);
+ if (source && !g_source_is_destroyed(source))
+ g_source_destroy(source);
+
+ DestroyWeakPtr(conn_data_);
+ conn_data_ = nullptr;
+}
- return false;
+std::recursive_mutex& FdBroker::GetMutex() const {
+ return mutex_;
}
} // namespace internal