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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/usb/usb_service.h" 5 #include "device/usb/usb_service.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "components/device_event_log/device_event_log.h" 8 #include "components/device_event_log/device_event_log.h"
9 #include "device/usb/usb_device.h" 9 #include "device/usb/usb_device.h"
10 #include "device/usb/usb_service_impl.h" 10 #include "device/usb/usb_service_impl.h"
11 11
12 namespace device { 12 namespace device {
13 13
14 namespace { 14 namespace {
15 15
16 UsbService* g_service; 16 UsbService* g_service;
17 17
18 } // namespace 18 } // namespace
19 19
20 // This class manages the lifetime of the global UsbService instance so that
21 // it is destroyed when the current message loop is destroyed. A lazy instance
22 // cannot be used because this object does not live on the main thread.
23 class UsbService::Destroyer : private base::MessageLoop::DestructionObserver {
24 public:
25 explicit Destroyer(UsbService* usb_service) : usb_service_(usb_service) {
26 base::MessageLoop::current()->AddDestructionObserver(this);
27 }
28 ~Destroyer() override {}
29
30 private:
31 // base::MessageLoop::DestructionObserver implementation.
32 void WillDestroyCurrentMessageLoop() override {
33 base::MessageLoop::current()->RemoveDestructionObserver(this);
34 delete usb_service_;
35 delete this;
36 g_service = nullptr;
37 }
38
39 UsbService* usb_service_;
40 };
41
42 void UsbService::Observer::OnDeviceAdded(scoped_refptr<UsbDevice> device) { 20 void UsbService::Observer::OnDeviceAdded(scoped_refptr<UsbDevice> device) {
43 } 21 }
44 22
45 void UsbService::Observer::OnDeviceRemoved(scoped_refptr<UsbDevice> device) { 23 void UsbService::Observer::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
46 } 24 }
47 25
48 void UsbService::Observer::OnDeviceRemovedCleanup( 26 void UsbService::Observer::OnDeviceRemovedCleanup(
49 scoped_refptr<UsbDevice> device) { 27 scoped_refptr<UsbDevice> device) {
50 } 28 }
51 29
52 // static 30 // static
53 UsbService* UsbService::GetInstance( 31 UsbService* UsbService::GetInstance(
54 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { 32 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) {
55 if (!g_service) { 33 if (!g_service) {
56 g_service = UsbServiceImpl::Create(ui_task_runner); 34 // UsbService constructor saves the pointer this returns and UsbServiceImpl
57 // This object will clean itself up when the message loop is destroyed. 35 // will destroy itself when the current message loop exits.
58 new Destroyer(g_service); 36 UsbServiceImpl::Create(blocking_task_runner);
59 } 37 }
60 return g_service; 38 return g_service;
61 } 39 }
62 40
63 // static
64 void UsbService::SetInstanceForTest(UsbService* instance) {
65 g_service = instance;
66 new Destroyer(instance);
67 }
68
69 UsbService::UsbService() { 41 UsbService::UsbService() {
42 DCHECK(!g_service);
43 g_service = this;
70 } 44 }
71 45
72 UsbService::~UsbService() { 46 UsbService::~UsbService() {
47 DCHECK(g_service);
48 g_service = nullptr;
73 } 49 }
74 50
75 void UsbService::AddObserver(Observer* observer) { 51 void UsbService::AddObserver(Observer* observer) {
76 DCHECK(CalledOnValidThread()); 52 DCHECK(CalledOnValidThread());
77 observer_list_.AddObserver(observer); 53 observer_list_.AddObserver(observer);
78 } 54 }
79 55
80 void UsbService::RemoveObserver(Observer* observer) { 56 void UsbService::RemoveObserver(Observer* observer) {
81 DCHECK(CalledOnValidThread()); 57 DCHECK(CalledOnValidThread());
82 observer_list_.RemoveObserver(observer); 58 observer_list_.RemoveObserver(observer);
83 } 59 }
84 60
85 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) { 61 void UsbService::NotifyDeviceAdded(scoped_refptr<UsbDevice> device) {
86 DCHECK(CalledOnValidThread()); 62 DCHECK(CalledOnValidThread());
87 63
88 USB_LOG(USER) << "USB device added: vendorId = " << device->vendor_id()
89 << ", productId = " << device->product_id()
90 << ", uniqueId = " << device->unique_id();
91
92 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device)); 64 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceAdded(device));
93 } 65 }
94 66
95 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) { 67 void UsbService::NotifyDeviceRemoved(scoped_refptr<UsbDevice> device) {
96 DCHECK(CalledOnValidThread()); 68 DCHECK(CalledOnValidThread());
97 69
98 USB_LOG(USER) << "USB device removed: uniqueId = " << device->unique_id();
99
100 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device)); 70 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemoved(device));
101 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device)); 71 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceRemovedCleanup(device));
102 } 72 }
103 73
104 } // namespace device 74 } // namespace device
OLDNEW
« 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