From ec1f1c7f010d38beabcbbc653e0df4b299133c7a Mon Sep 17 00:00:00 2001 From: reillyg Date: Mon, 5 Jan 2015 20:06:26 -0800 Subject: Create DeviceMonitorWin to manage device change notifications. This change factors out the code in HidServiceWin that manages a MessageWindow for listening to device change notification messages. A single window is created for all registered device interfaces and consumers can use a simple observer interface to subscribe. The HidService is now destroyed using AtExitManager so that the two singletons are destroyed in the opposite of the order of their construction. BUG=411715 Review URL: https://codereview.chromium.org/818353002 Cr-Commit-Position: refs/heads/master@{#310046} --- device/hid/hid_service.cc | 40 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 29 deletions(-) (limited to 'device/hid/hid_service.cc') diff --git a/device/hid/hid_service.cc b/device/hid/hid_service.cc index f78bccd..4953f25 100644 --- a/device/hid/hid_service.cc +++ b/device/hid/hid_service.cc @@ -4,6 +4,7 @@ #include "device/hid/hid_service.h" +#include "base/at_exit.h" #include "base/bind.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" @@ -21,27 +22,8 @@ namespace device { namespace { -HidService* g_service = NULL; - -} // namespace - -class HidService::Destroyer : public base::MessageLoop::DestructionObserver { - public: - explicit Destroyer(HidService* hid_service) - : hid_service_(hid_service) {} - ~Destroyer() override {} - - private: - // base::MessageLoop::DestructionObserver implementation. - void WillDestroyCurrentMessageLoop() override { - base::MessageLoop::current()->RemoveDestructionObserver(this); - delete hid_service_; - delete this; - g_service = NULL; - } - - HidService* hid_service_; -}; +HidService* g_service; +} HidService* HidService::GetInstance( scoped_refptr file_task_runner) { @@ -54,8 +36,8 @@ HidService* HidService::GetInstance( g_service = new HidServiceWin(); #endif if (g_service != nullptr) { - Destroyer* destroyer = new Destroyer(g_service); - base::MessageLoop::current()->AddDestructionObserver(destroyer); + base::AtExitManager::RegisterTask(base::Bind( + &base::DeletePointer, base::Unretained(g_service))); } } return g_service; @@ -64,12 +46,8 @@ HidService* HidService::GetInstance( void HidService::SetInstanceForTest(HidService* instance) { DCHECK(!g_service); g_service = instance; - Destroyer* destroyer = new Destroyer(g_service); - base::MessageLoop::current()->AddDestructionObserver(destroyer); -} - -HidService::~HidService() { - DCHECK(thread_checker_.CalledOnValidThread()); + base::AtExitManager::RegisterTask(base::Bind(&base::DeletePointer, + base::Unretained(g_service))); } void HidService::GetDevices(const GetDevicesCallback& callback) { @@ -108,6 +86,10 @@ bool HidService::GetDeviceInfo(const HidDeviceId& device_id, HidService::HidService() : enumeration_ready_(false) { } +HidService::~HidService() { + DCHECK(thread_checker_.CalledOnValidThread()); +} + void HidService::AddDevice(const HidDeviceInfo& info) { DCHECK(thread_checker_.CalledOnValidThread()); if (!ContainsKey(devices_, info.device_id)) { -- cgit v1.1