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 = NULL; |
| 86 if (libusb_init(&context) != LIBUSB_SUCCESS) |
| 87 return NULL; |
| 88 if (!context) |
| 89 return NULL; |
| 90 return new UsbService(context); |
| 91 } |
| 92 }; |
| 93 |
82 UsbService* UsbService::GetInstance() { | 94 UsbService* UsbService::GetInstance() { |
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
84 // UsbService deletes itself upon APP_TERMINATING. | 96 // UsbService deletes itself upon APP_TERMINATING. |
85 return Singleton<UsbService, LeakySingletonTraits<UsbService> >::get(); | 97 return Singleton<UsbService, InitUsbContextTraits>::get(); |
86 } | 98 } |
87 | 99 |
88 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { | 100 void UsbService::GetDevices(std::vector<scoped_refptr<UsbDevice> >* devices) { |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
90 STLClearObject(devices); | 102 STLClearObject(devices); |
91 RefreshDevices(); | 103 RefreshDevices(); |
92 | 104 |
93 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { | 105 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
94 devices->push_back(it->second); | 106 devices->push_back(it->second); |
95 } | 107 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 155 |
144 // Remove disconnected devices from devices_. | 156 // Remove disconnected devices from devices_. |
145 for (size_t i = 0; i < disconnected_devices.size(); ++i) { | 157 for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
146 // UsbDevice will be destroyed after this. The corresponding | 158 // UsbDevice will be destroyed after this. The corresponding |
147 // PlatformUsbDevice will be unref'ed during this process. | 159 // PlatformUsbDevice will be unref'ed during this process. |
148 devices_.erase(disconnected_devices[i]); | 160 devices_.erase(disconnected_devices[i]); |
149 } | 161 } |
150 | 162 |
151 libusb_free_device_list(platform_devices, true); | 163 libusb_free_device_list(platform_devices, true); |
152 } | 164 } |
OLD | NEW |