Commit 592e81d4 authored by Donna Wu's avatar Donna Wu Committed by Commit Bot

Support multiple bindings in device::usb::DeviceManagerImpl.

Support multiple bindings in DeviceManagerImpl so that the Device
Service can own a unique DeviceManagerImpl instance easily and
the unique UsbService instance will be owned by DeviceManagerImpl,
then we can git rid of DeviceClient after all users being converted
to mojom interface.

Bug: 699790
Change-Id: I8a62739796f99a98bf87a1ed71562c09cba9e050
Reviewed-on: https://chromium-review.googlesource.com/1221475
Commit-Queue: Donna Wu <donna.wu@intel.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592706}
parent 14d57280
...@@ -99,8 +99,8 @@ void UsbChooserContext::EnsureConnectionWithDeviceManager() { ...@@ -99,8 +99,8 @@ void UsbChooserContext::EnsureConnectionWithDeviceManager() {
// TODO(donna.wu@intel.com): Request UsbDeviceManagerPtr from DeviceService // TODO(donna.wu@intel.com): Request UsbDeviceManagerPtr from DeviceService
// after moving //device/usb to //services/device. // after moving //device/usb to //services/device.
device_manager_instance_ = device::usb::DeviceManagerImpl::Create( device_manager_instance_ = std::make_unique<device::usb::DeviceManagerImpl>();
mojo::MakeRequest(&device_manager_)); device_manager_instance_->AddBinding(mojo::MakeRequest(&device_manager_));
device_manager_.set_connection_error_handler( device_manager_.set_connection_error_handler(
base::BindOnce(&UsbChooserContext::OnDeviceManagerConnectionError, base::BindOnce(&UsbChooserContext::OnDeviceManagerConnectionError,
base::Unretained(this))); base::Unretained(this)));
......
...@@ -24,32 +24,19 @@ ...@@ -24,32 +24,19 @@
namespace device { namespace device {
namespace usb { namespace usb {
// static DeviceManagerImpl::DeviceManagerImpl() : observer_(this), weak_factory_(this) {
std::unique_ptr<DeviceManagerImpl> DeviceManagerImpl::Create( usb_service_ = DeviceClient::Get()->GetUsbService();
mojom::UsbDeviceManagerRequest request) { if (usb_service_)
DCHECK(DeviceClient::Get()); observer_.Add(usb_service_);
UsbService* service = DeviceClient::Get()->GetUsbService();
if (!service)
return nullptr;
auto* device_manager = new DeviceManagerImpl(service);
device_manager->binding_.Bind(std::move(request));
return base::WrapUnique(device_manager);
}
DeviceManagerImpl::DeviceManagerImpl(UsbService* usb_service)
: usb_service_(usb_service),
observer_(this),
binding_(this),
weak_factory_(this) {
// This object owns itself and will be destroyed if the message pipe it is
// bound to is closed, the message loop is destructed, or the UsbService is
// shut down.
observer_.Add(usb_service_);
} }
DeviceManagerImpl::~DeviceManagerImpl() = default; DeviceManagerImpl::~DeviceManagerImpl() = default;
void DeviceManagerImpl::AddBinding(mojom::UsbDeviceManagerRequest request) {
if (usb_service_)
bindings_.AddBinding(this, std::move(request));
}
void DeviceManagerImpl::GetDevices(mojom::UsbEnumerationOptionsPtr options, void DeviceManagerImpl::GetDevices(mojom::UsbEnumerationOptionsPtr options,
GetDevicesCallback callback) { GetDevicesCallback callback) {
usb_service_->GetDevices( usb_service_->GetDevices(
...@@ -71,7 +58,9 @@ void DeviceManagerImpl::GetDevice(const std::string& guid, ...@@ -71,7 +58,9 @@ void DeviceManagerImpl::GetDevice(const std::string& guid,
void DeviceManagerImpl::SetClient( void DeviceManagerImpl::SetClient(
mojom::UsbDeviceManagerClientAssociatedPtrInfo client) { mojom::UsbDeviceManagerClientAssociatedPtrInfo client) {
DCHECK(client); DCHECK(client);
client_.Bind(std::move(client)); mojom::UsbDeviceManagerClientAssociatedPtr client_ptr;
client_ptr.Bind(std::move(client));
clients_.AddPtr(std::move(client_ptr));
} }
void DeviceManagerImpl::OnGetDevices( void DeviceManagerImpl::OnGetDevices(
...@@ -93,19 +82,28 @@ void DeviceManagerImpl::OnGetDevices( ...@@ -93,19 +82,28 @@ void DeviceManagerImpl::OnGetDevices(
} }
void DeviceManagerImpl::OnDeviceAdded(scoped_refptr<UsbDevice> device) { void DeviceManagerImpl::OnDeviceAdded(scoped_refptr<UsbDevice> device) {
if (client_) auto device_info = device::mojom::UsbDeviceInfo::From(*device);
client_->OnDeviceAdded(mojom::UsbDeviceInfo::From(*device)); DCHECK(device_info);
clients_.ForAllPtrs([&device_info](mojom::UsbDeviceManagerClient* client) {
client->OnDeviceAdded(device_info->Clone());
});
} }
void DeviceManagerImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { void DeviceManagerImpl::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
if (client_) auto device_info = device::mojom::UsbDeviceInfo::From(*device);
client_->OnDeviceRemoved(mojom::UsbDeviceInfo::From(*device)); DCHECK(device_info);
clients_.ForAllPtrs([&device_info](mojom::UsbDeviceManagerClient* client) {
client->OnDeviceRemoved(device_info->Clone());
});
} }
void DeviceManagerImpl::WillDestroyUsbService() { void DeviceManagerImpl::WillDestroyUsbService() {
observer_.RemoveAll(); observer_.RemoveAll();
binding_.Close(); usb_service_ = nullptr;
client_.reset();
// Close all the connections.
bindings_.CloseAllBindings();
clients_.CloseAll();
} }
} // namespace usb } // namespace usb
......
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "device/usb/public/mojom/device_manager.mojom.h" #include "device/usb/public/mojom/device_manager.mojom.h"
#include "device/usb/usb_service.h" #include "device/usb/usb_service.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/public/cpp/bindings/interface_ptr_set.h"
namespace device { namespace device {
...@@ -31,14 +32,12 @@ namespace usb { ...@@ -31,14 +32,12 @@ namespace usb {
class DeviceManagerImpl : public mojom::UsbDeviceManager, class DeviceManagerImpl : public mojom::UsbDeviceManager,
public UsbService::Observer { public UsbService::Observer {
public: public:
static std::unique_ptr<DeviceManagerImpl> Create( DeviceManagerImpl();
mojom::UsbDeviceManagerRequest request);
~DeviceManagerImpl() override; ~DeviceManagerImpl() override;
private: void AddBinding(mojom::UsbDeviceManagerRequest request);
explicit DeviceManagerImpl(UsbService* usb_service);
private:
// DeviceManager implementation: // DeviceManager implementation:
void GetDevices(mojom::UsbEnumerationOptionsPtr options, void GetDevices(mojom::UsbEnumerationOptionsPtr options,
GetDevicesCallback callback) override; GetDevicesCallback callback) override;
...@@ -63,8 +62,8 @@ class DeviceManagerImpl : public mojom::UsbDeviceManager, ...@@ -63,8 +62,8 @@ class DeviceManagerImpl : public mojom::UsbDeviceManager,
UsbService* usb_service_; UsbService* usb_service_;
ScopedObserver<UsbService, UsbService::Observer> observer_; ScopedObserver<UsbService, UsbService::Observer> observer_;
mojo::Binding<mojom::UsbDeviceManager> binding_; mojo::BindingSet<mojom::UsbDeviceManager> bindings_;
mojom::UsbDeviceManagerClientAssociatedPtr client_; mojo::AssociatedInterfacePtrSet<mojom::UsbDeviceManagerClient> clients_;
base::WeakPtrFactory<DeviceManagerImpl> weak_factory_; base::WeakPtrFactory<DeviceManagerImpl> weak_factory_;
......
...@@ -54,15 +54,12 @@ class USBDeviceManagerImplTest : public testing::Test { ...@@ -54,15 +54,12 @@ class USBDeviceManagerImplTest : public testing::Test {
~USBDeviceManagerImplTest() override = default; ~USBDeviceManagerImplTest() override = default;
protected: protected:
void TearDown() override {
// Clean up the device manager for next test case.
device_manager_instance_.reset();
}
UsbDeviceManagerPtr ConnectToDeviceManager() { UsbDeviceManagerPtr ConnectToDeviceManager() {
UsbDeviceManagerPtr device_manager; UsbDeviceManagerPtr device_manager;
device_manager_instance_ = if (!device_manager_instance_)
DeviceManagerImpl::Create(mojo::MakeRequest(&device_manager)); device_manager_instance_ = std::make_unique<DeviceManagerImpl>();
device_manager_instance_->AddBinding(mojo::MakeRequest(&device_manager));
return device_manager; return device_manager;
} }
......
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