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

Side by Side Diff: device/usb/device_manager_impl.cc

Issue 1155163008: Build a basic Mojo service framework for device/usb (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "device/usb/device_manager_impl.h"
6
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "device/core/device_client.h"
10 #include "device/usb/device_impl.h"
11 #include "device/usb/public/cpp/device_manager_delegate.h"
12 #include "device/usb/public/cpp/device_manager_factory.h"
13 #include "device/usb/public/interfaces/device.mojom.h"
14 #include "device/usb/type_converters.h"
15 #include "device/usb/usb_device.h"
16 #include "device/usb/usb_device_filter.h"
17 #include "device/usb/usb_service.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
20
21 namespace device {
22 namespace usb {
23
24 // static
25 void DeviceManagerFactory::Build(mojo::InterfaceRequest<DeviceManager> request,
26 scoped_ptr<DeviceManagerDelegate> delegate) {
27 // Owned by its MessagePipe.
28 new DeviceManagerImpl(request.Pass(), delegate.Pass());
29 }
30
31 DeviceManagerImpl::DeviceManagerImpl(
32 mojo::InterfaceRequest<DeviceManager> request,
33 scoped_ptr<DeviceManagerDelegate> delegate)
34 : binding_(this, request.Pass()),
35 delegate_(delegate.Pass()),
36 weak_factory_(this) {
37 DCHECK(DeviceClient::Get());
38 usb_service_ = DeviceClient::Get()->GetUsbService();
Reilly Grant (use Gerrit) 2015/05/27 21:00:52 I would wait until GetDevices() is called to fetch
Ken Rockot(use gerrit already) 2015/05/27 22:35:03 Done.
39 }
40
41 DeviceManagerImpl::~DeviceManagerImpl() {
42 }
43
44 void DeviceManagerImpl::GetDevices(EnumerationOptionsPtr options,
45 const GetDevicesCallback& callback) {
46 if (!usb_service_) {
47 mojo::Array<EnumerationResultPtr> results(0);
48 callback.Run(results.Pass());
49 return;
50 }
51 std::vector<UsbDeviceFilter> filters =
52 options->filters.To<std::vector<UsbDeviceFilter>>();
53 usb_service_->GetDevices(base::Bind(&DeviceManagerImpl::OnGetDevices,
54 weak_factory_.GetWeakPtr(), callback,
55 filters));
56 }
57
58 void DeviceManagerImpl::OnGetDevices(
59 const GetDevicesCallback& callback,
60 const std::vector<UsbDeviceFilter>& filters,
61 const std::vector<scoped_refptr<UsbDevice>>& devices) {
62 mojo::Array<EnumerationResultPtr> results(0);
63 for (size_t i = 0; i < devices.size(); ++i) {
64 DeviceInfoPtr device_info = DeviceInfo::From(*devices[i]);
65 if (UsbDeviceFilter::MatchesAny(devices[i], filters) &&
66 delegate_->IsDeviceAllowed(*device_info)) {
67 EnumerationResultPtr result = EnumerationResult::New();
68 DevicePtr device;
69 // Owned by its message pipe.
70 new DeviceImpl(devices[i], mojo::GetProxy(&device));
71 result->device = device.Pass();
72 results.push_back(result.Pass());
73 }
74 }
75 callback.Run(results.Pass());
76 }
77
78 } // namespace usb
79 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698