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

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

Issue 1408193003: Add chrome side webusb permission UI code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: updated code to use ScopedPtrMap, added #ifdef for mobile platforms Created 5 years, 1 month 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 "chrome/browser/usb/usb_chooser_choices.h"
6
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "device/core/device_client.h"
12 #include "device/devices_app/usb/type_converters.h"
13 #include "device/usb/usb_device.h"
14 #include "device/usb/usb_device_filter.h"
15 #include "device/usb/usb_service.h"
16
17 namespace {
18
19 // Check if the origin is in the description set.
20 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set,
21 const GURL& origin) {
22 if (!set)
23 return false;
24
25 if (ContainsValue(set->origins, origin)) {
26 return true;
27 }
28
29 for (const auto& config : set->configurations) {
30 if (ContainsValue(config.origins, origin)) {
31 return true;
32 }
33
34 for (const auto& function : config.functions) {
35 if (ContainsValue(function.origins, origin)) {
36 return true;
37 }
38 }
39 }
40
41 return false;
42 }
43
44 } // namespace
45
46 UsbChooserChoices::UsbChooserChoices(
47 mojo::Array<device::usb::DeviceFilterPtr> device_filters,
48 content::RenderFrameHost* render_frame_host,
49 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback)
50 : device_filters_(device_filters.Pass()),
51 render_frame_host_(render_frame_host),
52 callback_(callback),
53 usb_service_observer_(this) {
54 device::UsbService* usb_service =
55 device::DeviceClient::Get()->GetUsbService();
56 if (!usb_service)
57 return;
58
59 usb_service_observer_.Add(usb_service);
60
61 if (!device_filters_.is_null())
62 filters_ = device_filters_.To<std::vector<device::UsbDeviceFilter>>();
63 origin_ = render_frame_host_->GetLastCommittedURL().GetOrigin();
64
65 usb_service->GetDevices(base::Bind(&UsbChooserChoices::GetUsbDevicesList,
66 base::Unretained(this)));
67 }
68
69 UsbChooserChoices::~UsbChooserChoices() {
70 if (!callback_.is_null()) {
Reilly Grant (use Gerrit) 2015/10/29 01:11:33 nit: No braces for single line ifs.
juncai 2015/10/31 04:15:36 Done.
71 callback_.Run(nullptr);
72 }
73 }
74
75 const std::vector<std::string>& UsbChooserChoices::GetChoices() const {
76 return devices_names_;
77 }
78
79 // index < 0 means cancel button was pressed.
80 void UsbChooserChoices::Select(int index) {
81 if (index >= 0 && index < static_cast<int>(devices_.size())) {
82 device::usb::DeviceInfoPtr device_info_ptr =
83 device::usb::DeviceInfo::From(*devices_[index]);
84 callback_.Run(device_info_ptr.Pass());
85 } else {
86 callback_.Run(nullptr);
87 }
88 callback_.reset(); // reset callback so that it is only called once.
Reilly Grant (use Gerrit) 2015/10/29 01:11:32 nit: "Reset |callback| so that it is only run once
juncai 2015/10/31 04:15:36 Done.
89 }
90
91 void UsbChooserChoices::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) {
92 auto itor = std::find(devices_.begin(), devices_.end(), device);
93 if (itor == devices_.end() &&
Reilly Grant (use Gerrit) 2015/10/29 01:11:32 There will never be an OnDeviceAdded call for a de
juncai 2015/10/31 04:15:36 Done.
94 device::UsbDeviceFilter::MatchesAny(device, filters_) &&
95 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) {
96 devices_.push_back(device);
97 devices_names_.push_back(base::UTF16ToASCII(device->product_string()));
98 recently_updated_index_ = static_cast<int>(devices_names_.size()) - 1;
99 observer()->OnChoicesChanged();
100 }
101 }
102
103 void UsbChooserChoices::OnDeviceRemoved(
104 scoped_refptr<device::UsbDevice> device) {
105 auto itor = std::find(devices_.begin(), devices_.end(), device);
106 if (itor != devices_.end()) {
107 recently_updated_index_ = itor - devices_.begin();
108 devices_.erase(itor);
109 devices_names_.erase(devices_names_.begin() + recently_updated_index_);
110 observer()->OnChoicesChanged();
111 }
112 }
113
114 // Get a list of devices that can be shown in the chooser bubble UI for
115 // user to grant permsssion.
116 void UsbChooserChoices::GetUsbDevicesList(
117 const std::vector<scoped_refptr<device::UsbDevice>>& devices) {
118 for (const auto& device : devices) {
119 if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
120 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) {
121 devices_.push_back(device);
122 devices_names_.push_back(base::UTF16ToASCII(device->product_string()));
123 }
124 }
125 observer()->OnChoicesChanged();
126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698