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

Side by Side Diff: chrome/browser/usb/usb_service.cc

Issue 22914023: Introducing chrome.usb.getDevices/openDevice API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-interface
Patch Set: Created 7 years, 4 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
OLDNEW
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/bind.h" 10 #include "base/bind.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 } 57 }
58 UsbService* service_; 58 UsbService* service_;
59 content::NotificationRegistrar registrar_; 59 content::NotificationRegistrar registrar_;
60 }; 60 };
61 61
62 } // namespace 62 } // namespace
63 63
64 using content::BrowserThread; 64 using content::BrowserThread;
65 65
66 UsbService::UsbService() 66 UsbService::UsbService()
67 : context_(new UsbContext()) { 67 : context_(new UsbContext()),
68 next_unique_id_(0) {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
69 } 70 }
70 71
71 UsbService::~UsbService() { 72 UsbService::~UsbService() {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
73 for (DeviceMap::iterator it = devices_.begin(); 74 for (DeviceMap::iterator it = devices_.begin();
74 it != devices_.end(); ++it) { 75 it != devices_.end(); ++it) {
75 it->second->OnDisconnect(); 76 it->second->OnDisconnect();
76 } 77 }
77 } 78 }
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 // devices then there is no point in attempting to enumerate the devices. On 161 // devices then there is no point in attempting to enumerate the devices. On
161 // platforms without a permission broker, we assume permission is granted. 162 // platforms without a permission broker, we assume permission is granted.
162 if (!success) { 163 if (!success) {
163 callback.Run(devices.Pass()); 164 callback.Run(devices.Pass());
164 return; 165 return;
165 } 166 }
166 167
167 RefreshDevices(); 168 RefreshDevices();
168 169
169 for (DeviceMap::iterator it = devices_.begin(); 170 for (DeviceMap::iterator it = devices_.begin();
170 it != devices_.end(); ++it) { 171 it != devices_.end(); ++it) {
171 if (DeviceMatches(it->second, vendor_id, product_id)) 172 if (DeviceMatches(it->second, vendor_id, product_id))
172 devices->push_back(it->second); 173 devices->push_back(it->second);
173 } 174 }
174 175
175 callback.Run(devices.Pass()); 176 callback.Run(devices.Pass());
176 } 177 }
177 178
179 scoped_refptr<UsbDevice> UsbService::GetDeviceById(uint32 unique_id) {
180 RefreshDevices();
181 for (DeviceMap::iterator it = devices_.begin();
182 it != devices_.end(); ++it) {
183 if (it->second->unique_id() == unique_id)
184 return it->second;
185 }
186 return NULL;
187 }
188
178 void UsbService::RefreshDevices() { 189 void UsbService::RefreshDevices() {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
180 191
181 libusb_device** platform_devices = NULL; 192 libusb_device** platform_devices = NULL;
182 const ssize_t device_count = 193 const ssize_t device_count =
183 libusb_get_device_list(context_->context(), &platform_devices); 194 libusb_get_device_list(context_->context(), &platform_devices);
184 195
185 std::set<UsbDevice*> connected_devices; 196 std::set<UsbDevice*> connected_devices;
186 vector<PlatformUsbDevice> disconnected_devices; 197 vector<PlatformUsbDevice> disconnected_devices;
187 198
188 // Populates new devices. 199 // Populates new devices.
189 for (ssize_t i = 0; i < device_count; ++i) { 200 for (ssize_t i = 0; i < device_count; ++i) {
190 if (!ContainsKey(devices_, platform_devices[i])) { 201 if (!ContainsKey(devices_, platform_devices[i])) {
191 libusb_device_descriptor descriptor; 202 libusb_device_descriptor descriptor;
192 // This test is needed. A valid vendor/produce pair is required. 203 // This test is needed. A valid vendor/produce pair is required.
193 if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor)) 204 if (0 != libusb_get_device_descriptor(platform_devices[i], &descriptor))
194 continue; 205 continue;
195 UsbDevice* new_device = new UsbDevice(context_, 206 UsbDevice* new_device = new UsbDevice(context_,
196 platform_devices[i], 207 platform_devices[i],
197 descriptor.idVendor, 208 descriptor.idVendor,
198 descriptor.idProduct); 209 descriptor.idProduct,
210 ++next_unique_id_);
199 devices_[platform_devices[i]] = new_device; 211 devices_[platform_devices[i]] = new_device;
200 connected_devices.insert(new_device); 212 connected_devices.insert(new_device);
201 } else { 213 } else {
202 connected_devices.insert(devices_[platform_devices[i]].get()); 214 connected_devices.insert(devices_[platform_devices[i]].get());
203 } 215 }
204 } 216 }
205 217
206 // Find disconnected devices. 218 // Find disconnected devices.
207 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { 219 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) {
208 if (!ContainsKey(connected_devices, it->second)) { 220 if (!ContainsKey(connected_devices, it->second)) {
209 disconnected_devices.push_back(it->first); 221 disconnected_devices.push_back(it->first);
210 } 222 }
211 } 223 }
212 224
213 // Remove disconnected devices from devices_. 225 // Remove disconnected devices from devices_.
214 for (size_t i = 0; i < disconnected_devices.size(); ++i) { 226 for (size_t i = 0; i < disconnected_devices.size(); ++i) {
215 // UsbDevice will be destroyed after this. The corresponding 227 // UsbDevice will be destroyed after this. The corresponding
216 // PlatformUsbDevice will be unref'ed during this process. 228 // PlatformUsbDevice will be unref'ed during this process.
217 devices_.erase(disconnected_devices[i]); 229 devices_.erase(disconnected_devices[i]);
218 } 230 }
219 231
220 libusb_free_device_list(platform_devices, true); 232 libusb_free_device_list(platform_devices, true);
221 } 233 }
222 234
223 bool UsbService::DeviceMatches(scoped_refptr<UsbDevice> device, 235 bool UsbService::DeviceMatches(scoped_refptr<UsbDevice> device,
224 const uint16 vendor_id, 236 const uint16 vendor_id,
225 const uint16 product_id) { 237 const uint16 product_id) {
226 return device->vendor_id() == vendor_id && device->product_id() == product_id; 238 return device->vendor_id() == vendor_id && device->product_id() == product_id;
227 } 239 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698