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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 } | 57 } |
58 } | 58 } |
59 UsbService* service_; | 59 UsbService* service_; |
60 content::NotificationRegistrar registrar_; | 60 content::NotificationRegistrar registrar_; |
61 }; | 61 }; |
62 | 62 |
63 } // namespace | 63 } // namespace |
64 | 64 |
65 using content::BrowserThread; | 65 using content::BrowserThread; |
66 | 66 |
67 UsbService::UsbService() | 67 UsbService::UsbService(PlatformUsbContext context) |
68 : context_(new UsbContext()), | 68 : context_(new UsbContext(context)), |
69 next_unique_id_(0) { | 69 next_unique_id_(0) { |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
71 // Will be deleted upon NOTIFICATION_APP_TERMINATING. | 71 // Will be deleted upon NOTIFICATION_APP_TERMINATING. |
72 new ExitObserver(this); | 72 new ExitObserver(this); |
73 } | 73 } |
74 | 74 |
75 UsbService::~UsbService() { | 75 UsbService::~UsbService() { |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
77 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 77 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
78 it->second->OnDisconnect(); | 78 it->second->OnDisconnect(); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 struct InitUsbContextTraits : public LeakySingletonTraits<UsbService> { | |
83 // LeakySingletonTraits<UsbService> | |
84 static UsbService* New() { | |
85 PlatformUsbContext context; | |
86 libusb_init(&context); | |
87 if (!context) | |
xiyuan
2013/09/04 20:22:22
nit: should we check the return code of libusb_ini
Bei Zhang
2013/09/04 21:04:58
I see. There's chance that context is not even ini
| |
88 return NULL; | |
89 return new UsbService(context); | |
90 } | |
91 }; | |
92 | |
82 UsbService* UsbService::GetInstance() { | 93 UsbService* UsbService::GetInstance() { |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
84 // UsbService deletes itself upon APP_TERMINATING. | 95 // UsbService deletes itself upon APP_TERMINATING. |
85 return Singleton<UsbService, LeakySingletonTraits<UsbService> >::get(); | 96 return Singleton<UsbService, InitUsbContextTraits>::get(); |
86 } | 97 } |
87 | 98 |
88 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { | 99 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
90 STLClearObject(devices); | 101 STLClearObject(devices); |
91 RefreshDevices(); | 102 RefreshDevices(); |
92 | 103 |
93 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 104 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
94 devices->push_back(it->second); | 105 devices->push_back(it->second); |
95 } | 106 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 | 154 |
144 // Remove disconnected devices from devices_. | 155 // Remove disconnected devices from devices_. |
145 for (size_t i = 0; i < disconnected_devices.size(); ++i) { | 156 for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
146 // UsbDevice will be destroyed after this. The corresponding | 157 // UsbDevice will be destroyed after this. The corresponding |
147 // PlatformUsbDevice will be unref'ed during this process. | 158 // PlatformUsbDevice will be unref'ed during this process. |
148 devices_.erase(disconnected_devices[i]); | 159 devices_.erase(disconnected_devices[i]); |
149 } | 160 } |
150 | 161 |
151 libusb_free_device_list(platform_devices, true); | 162 libusb_free_device_list(platform_devices, true); |
152 } | 163 } |
OLD | NEW |