Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Unified Diff: device/usb/usb_service.cc

Issue 980023002: Move device/usb classes from the FILE thread to UI thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more thread assertions. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/usb/usb_service.h ('k') | device/usb/usb_service_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/usb/usb_service.cc
diff --git a/device/usb/usb_service.cc b/device/usb/usb_service.cc
index 10338fc7e14e45f832ae342e43c2447d10604828..18732b50d3e14d581dca34d2641184e6b9cf7908 100644
--- a/device/usb/usb_service.cc
+++ b/device/usb/usb_service.cc
@@ -17,28 +17,6 @@ UsbService* g_service;
} // namespace
-// This class manages the lifetime of the global UsbService instance so that
-// it is destroyed when the current message loop is destroyed. A lazy instance
-// cannot be used because this object does not live on the main thread.
-class UsbService::Destroyer : private base::MessageLoop::DestructionObserver {
- public:
- explicit Destroyer(UsbService* usb_service) : usb_service_(usb_service) {
- base::MessageLoop::current()->AddDestructionObserver(this);
- }
- ~Destroyer() override {}
-
- private:
- // base::MessageLoop::DestructionObserver implementation.
- void WillDestroyCurrentMessageLoop() override {
- base::MessageLoop::current()->RemoveDestructionObserver(this);
- delete usb_service_;
- delete this;
- g_service = nullptr;
- }
-
- UsbService* usb_service_;
-};
-
void UsbService::Observer::OnDeviceAdded(scoped_refptr<UsbDevice> device) {
}
@@ -51,25 +29,23 @@ void UsbService::Observer::OnDeviceRemovedCleanup(
// static
UsbService* UsbService::GetInstance(
- scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
+ scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) {
if (!g_service) {
- g_service = UsbServiceImpl::Create(ui_task_runner);
- // This object will clean itself up when the message loop is destroyed.
- new Destroyer(g_service);
+ // UsbService constructor saves the pointer this returns and UsbServiceImpl
+ // will destroy itself when the current message loop exits.
+ UsbServiceImpl::Create(blocking_task_runner);
}
return g_service;
}
-// static
-void UsbService::SetInstanceForTest(UsbService* instance) {
- g_service = instance;
- new Destroyer(instance);
-}
-
UsbService::UsbService() {
+ DCHECK(!g_service);
+ g_service = this;
}
UsbService::~UsbService() {
+ DCHECK(g_service);
+ g_service = nullptr;
}
void UsbService::AddObserver(Observer* observer) {
@@ -85,18 +61,12 @@ void UsbService::RemoveObserver(Observer* observer) {
void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
DCHECK(CalledOnValidThread());
- USB_LOG(USER) << "USB device added: vendorId = " << device->vendor_id()
- << ", productId = " << device->product_id()
- << ", uniqueId = " << device->unique_id();
-
FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device));
}
void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
DCHECK(CalledOnValidThread());
- USB_LOG(USER) << "USB device removed: uniqueId = " << device->unique_id();
-
FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device));
FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device));
}
« no previous file with comments | « device/usb/usb_service.h ('k') | device/usb/usb_service_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698