OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/usb/usb_service.h" | 5 #include "chrome/browser/usb/usb_service.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 } // namespace content | 31 } // namespace content |
32 | 32 |
33 using content::BrowserThread; | 33 using content::BrowserThread; |
34 using std::vector; | 34 using std::vector; |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 class ExitObserver : public content::NotificationObserver { | 38 class ExitObserver : public content::NotificationObserver { |
39 public: | 39 public: |
40 explicit ExitObserver(UsbService* service) : service_(service) { | 40 explicit ExitObserver(UsbService* service) : service_(service) { |
41 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 41 BrowserThread::PostTask( |
42 content::NotificationService::AllSources()); | 42 BrowserThread::UI, FROM_HERE, |
| 43 base::Bind(&content::NotificationRegistrar::Add, |
| 44 base::Unretained(®istrar_), this, |
| 45 chrome::NOTIFICATION_APP_TERMINATING, |
| 46 content::NotificationService::AllSources())); |
43 } | 47 } |
44 | 48 |
45 private: | 49 private: |
46 // content::NotificationObserver | 50 // content::NotificationObserver |
47 virtual void Observe(int type, | 51 virtual void Observe(int type, |
48 const content::NotificationSource& source, | 52 const content::NotificationSource& source, |
49 const content::NotificationDetails& details) OVERRIDE { | 53 const content::NotificationDetails& details) OVERRIDE { |
50 if (type == chrome::NOTIFICATION_APP_TERMINATING) { | 54 if (type == chrome::NOTIFICATION_APP_TERMINATING) { |
51 registrar_.RemoveAll(); | |
52 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, service_); | 55 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, service_); |
| 56 delete this; |
53 } | 57 } |
54 } | 58 } |
55 UsbService* service_; | 59 UsbService* service_; |
56 content::NotificationRegistrar registrar_; | 60 content::NotificationRegistrar registrar_; |
57 }; | 61 }; |
58 | 62 |
59 } // namespace | 63 } // namespace |
60 | 64 |
61 using content::BrowserThread; | 65 using content::BrowserThread; |
62 | 66 |
63 UsbService::UsbService() | 67 UsbService::UsbService() |
64 : context_(new UsbContext()), | 68 : context_(new UsbContext()), |
65 next_unique_id_(0) { | 69 next_unique_id_(0) { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 71 // Will be deleted upon NOTIFICATION_APP_TERMINATING. |
| 72 new ExitObserver(this); |
67 } | 73 } |
68 | 74 |
69 UsbService::~UsbService() { | 75 UsbService::~UsbService() { |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
71 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 77 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
72 it->second->OnDisconnect(); | 78 it->second->OnDisconnect(); |
73 } | 79 } |
74 } | 80 } |
75 | 81 |
76 UsbService* UsbService::GetInstance() { | 82 UsbService* UsbService::GetInstance() { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 143 |
138 // Remove disconnected devices from devices_. | 144 // Remove disconnected devices from devices_. |
139 for (size_t i = 0; i < disconnected_devices.size(); ++i) { | 145 for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
140 // UsbDevice will be destroyed after this. The corresponding | 146 // UsbDevice will be destroyed after this. The corresponding |
141 // PlatformUsbDevice will be unref'ed during this process. | 147 // PlatformUsbDevice will be unref'ed during this process. |
142 devices_.erase(disconnected_devices[i]); | 148 devices_.erase(disconnected_devices[i]); |
143 } | 149 } |
144 | 150 |
145 libusb_free_device_list(platform_devices, true); | 151 libusb_free_device_list(platform_devices, true); |
146 } | 152 } |
OLD | NEW |