Commit d6265a0a authored by Trent Begin's avatar Trent Begin Committed by Commit Bot

cros_healthd: use weak_ptr factory in service_connection

This is cleaning up uses of base::Unretained(this) in the cros_healthd
service connection. There are cases where the service connection could
be destroyed before the callback returns.

Bug: chromium:1108065
Change-Id: I66639581e213cc088080de5ec407f06968b8559c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2311074
Commit-Queue: Trent Begin <tbegin@chromium.org>
Reviewed-by: default avatarPaul Moy <pmoy@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791995}
parent 706ac3cd
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "chromeos/dbus/cros_healthd/cros_healthd_client.h" #include "chromeos/dbus/cros_healthd/cros_healthd_client.h"
...@@ -145,6 +146,8 @@ class ServiceConnectionImpl : public ServiceConnection { ...@@ -145,6 +146,8 @@ class ServiceConnectionImpl : public ServiceConnection {
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<ServiceConnectionImpl> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ServiceConnectionImpl); DISALLOW_COPY_AND_ASSIGN(ServiceConnectionImpl);
}; };
...@@ -368,10 +371,10 @@ void ServiceConnectionImpl::EnsureCrosHealthdServiceFactoryIsBound() { ...@@ -368,10 +371,10 @@ void ServiceConnectionImpl::EnsureCrosHealthdServiceFactoryIsBound() {
cros_healthd_service_factory_ = client->BootstrapMojoConnection( cros_healthd_service_factory_ = client->BootstrapMojoConnection(
base::BindOnce(&ServiceConnectionImpl::OnBootstrapMojoConnectionResponse, base::BindOnce(&ServiceConnectionImpl::OnBootstrapMojoConnectionResponse,
base::Unretained(this))); weak_factory_.GetWeakPtr()));
cros_healthd_service_factory_.set_disconnect_handler(base::BindOnce( cros_healthd_service_factory_.set_disconnect_handler(base::BindOnce(
&ServiceConnectionImpl::OnDisconnect, base::Unretained(this))); &ServiceConnectionImpl::OnDisconnect, weak_factory_.GetWeakPtr()));
} }
void ServiceConnectionImpl::BindCrosHealthdDiagnosticsServiceIfNeeded() { void ServiceConnectionImpl::BindCrosHealthdDiagnosticsServiceIfNeeded() {
...@@ -383,7 +386,7 @@ void ServiceConnectionImpl::BindCrosHealthdDiagnosticsServiceIfNeeded() { ...@@ -383,7 +386,7 @@ void ServiceConnectionImpl::BindCrosHealthdDiagnosticsServiceIfNeeded() {
cros_healthd_service_factory_->GetDiagnosticsService( cros_healthd_service_factory_->GetDiagnosticsService(
cros_healthd_diagnostics_service_.BindNewPipeAndPassReceiver()); cros_healthd_diagnostics_service_.BindNewPipeAndPassReceiver());
cros_healthd_diagnostics_service_.set_disconnect_handler(base::BindOnce( cros_healthd_diagnostics_service_.set_disconnect_handler(base::BindOnce(
&ServiceConnectionImpl::OnDisconnect, base::Unretained(this))); &ServiceConnectionImpl::OnDisconnect, weak_factory_.GetWeakPtr()));
} }
void ServiceConnectionImpl::BindCrosHealthdEventServiceIfNeeded() { void ServiceConnectionImpl::BindCrosHealthdEventServiceIfNeeded() {
...@@ -395,7 +398,7 @@ void ServiceConnectionImpl::BindCrosHealthdEventServiceIfNeeded() { ...@@ -395,7 +398,7 @@ void ServiceConnectionImpl::BindCrosHealthdEventServiceIfNeeded() {
cros_healthd_service_factory_->GetEventService( cros_healthd_service_factory_->GetEventService(
cros_healthd_event_service_.BindNewPipeAndPassReceiver()); cros_healthd_event_service_.BindNewPipeAndPassReceiver());
cros_healthd_event_service_.set_disconnect_handler(base::BindOnce( cros_healthd_event_service_.set_disconnect_handler(base::BindOnce(
&ServiceConnectionImpl::OnDisconnect, base::Unretained(this))); &ServiceConnectionImpl::OnDisconnect, weak_factory_.GetWeakPtr()));
} }
void ServiceConnectionImpl::BindCrosHealthdProbeServiceIfNeeded() { void ServiceConnectionImpl::BindCrosHealthdProbeServiceIfNeeded() {
...@@ -407,7 +410,7 @@ void ServiceConnectionImpl::BindCrosHealthdProbeServiceIfNeeded() { ...@@ -407,7 +410,7 @@ void ServiceConnectionImpl::BindCrosHealthdProbeServiceIfNeeded() {
cros_healthd_service_factory_->GetProbeService( cros_healthd_service_factory_->GetProbeService(
cros_healthd_probe_service_.BindNewPipeAndPassReceiver()); cros_healthd_probe_service_.BindNewPipeAndPassReceiver());
cros_healthd_probe_service_.set_disconnect_handler(base::BindOnce( cros_healthd_probe_service_.set_disconnect_handler(base::BindOnce(
&ServiceConnectionImpl::OnDisconnect, base::Unretained(this))); &ServiceConnectionImpl::OnDisconnect, weak_factory_.GetWeakPtr()));
} }
ServiceConnectionImpl::ServiceConnectionImpl() { ServiceConnectionImpl::ServiceConnectionImpl() {
......
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