1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/usb/usb_chooser_bubble_controller.h"
#include <stddef.h>
#include <utility>
#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/stl_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/common/url_constants.h"
#include "components/bubble/bubble_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "device/core/device_client.h"
#include "device/usb/mojo/type_converters.h"
#include "device/usb/usb_device.h"
#include "device/usb/usb_device_filter.h"
#include "url/gurl.h"
namespace {
// Reasons the chooser may be closed. These are used in histograms so do not
// remove/reorder entries. Only add at the end just before
// WEBUSB_CHOOSER_CLOSED_MAX. Also remember to update the enum listing in
// tools/metrics/histograms/histograms.xml.
enum WebUsbChooserClosed {
// The user cancelled the permission prompt without selecting a device.
WEBUSB_CHOOSER_CLOSED_CANCELLED = 0,
// The user probably cancelled the permission prompt without selecting a
// device because there were no devices to select.
WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES,
// The user granted permission to access a device.
WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED,
// The user granted permission to access a device but that permission will be
// revoked when the device is disconnected.
WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED,
// Maximum value for the enum.
WEBUSB_CHOOSER_CLOSED_MAX
};
void RecordChooserClosure(WebUsbChooserClosed disposition) {
UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", disposition,
WEBUSB_CHOOSER_CLOSED_MAX);
}
// Check if the origin is allowed.
bool FindInAllowedOrigins(const device::WebUsbAllowedOrigins* allowed_origins,
const GURL& origin) {
if (!allowed_origins)
return false;
if (ContainsValue(allowed_origins->origins, origin))
return true;
for (const auto& config : allowed_origins->configurations) {
if (ContainsValue(config.origins, origin))
return true;
for (const auto& function : config.functions) {
if (ContainsValue(function.origins, origin))
return true;
}
}
return false;
}
} // namespace
UsbChooserBubbleController::UsbChooserBubbleController(
content::RenderFrameHost* owner,
mojo::Array<device::usb::DeviceFilterPtr> device_filters,
content::RenderFrameHost* render_frame_host,
const device::usb::ChooserService::GetPermissionCallback& callback)
: ChooserBubbleController(owner),
render_frame_host_(render_frame_host),
callback_(callback),
usb_service_observer_(this),
weak_factory_(this) {
device::UsbService* usb_service =
device::DeviceClient::Get()->GetUsbService();
if (!usb_service)
return;
if (!usb_service_observer_.IsObserving(usb_service))
usb_service_observer_.Add(usb_service);
if (!device_filters.is_null())
filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>();
usb_service->GetDevices(
base::Bind(&UsbChooserBubbleController::GotUsbDeviceList,
weak_factory_.GetWeakPtr()));
}
UsbChooserBubbleController::~UsbChooserBubbleController() {
if (!callback_.is_null())
callback_.Run(nullptr);
}
size_t UsbChooserBubbleController::NumOptions() const {
return devices_.size();
}
const base::string16& UsbChooserBubbleController::GetOption(
size_t index) const {
DCHECK_LT(index, devices_.size());
return devices_[index].second;
}
void UsbChooserBubbleController::Select(size_t index) {
DCHECK_LT(index, devices_.size());
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host_);
GURL embedding_origin =
web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin();
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
UsbChooserContext* chooser_context =
UsbChooserContextFactory::GetForProfile(profile);
chooser_context->GrantDevicePermission(
render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin,
devices_[index].first->guid());
device::usb::DeviceInfoPtr device_info_ptr =
device::usb::DeviceInfo::From(*devices_[index].first);
callback_.Run(std::move(device_info_ptr));
callback_.reset(); // Reset |callback_| so that it is only run once.
RecordChooserClosure(devices_[index].first->serial_number().empty()
? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED
: WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED);
if (bubble_reference_)
bubble_reference_->CloseBubble(BUBBLE_CLOSE_ACCEPTED);
}
void UsbChooserBubbleController::Cancel() {
RecordChooserClosure(devices_.size() == 0
? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES
: WEBUSB_CHOOSER_CLOSED_CANCELLED);
if (bubble_reference_)
bubble_reference_->CloseBubble(BUBBLE_CLOSE_CANCELED);
}
void UsbChooserBubbleController::Close() {}
void UsbChooserBubbleController::OnDeviceAdded(
scoped_refptr<device::UsbDevice> device) {
if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
FindInAllowedOrigins(
device->webusb_allowed_origins(),
render_frame_host_->GetLastCommittedURL().GetOrigin())) {
devices_.push_back(std::make_pair(device, device->product_string()));
if (observer())
observer()->OnOptionAdded(devices_.size() - 1);
}
}
GURL UsbChooserBubbleController::GetHelpCenterUrl() const {
return GURL(chrome::kChooserUsbOverviewURL);
}
void UsbChooserBubbleController::OnDeviceRemoved(
scoped_refptr<device::UsbDevice> device) {
for (auto it = devices_.begin(); it != devices_.end(); ++it) {
if (it->first == device) {
size_t index = it - devices_.begin();
devices_.erase(it);
if (observer())
observer()->OnOptionRemoved(index);
return;
}
}
}
// Get a list of devices that can be shown in the chooser bubble UI for
// user to grant permsssion.
void UsbChooserBubbleController::GotUsbDeviceList(
const std::vector<scoped_refptr<device::UsbDevice>>& devices) {
for (const auto& device : devices) {
if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
FindInAllowedOrigins(
device->webusb_allowed_origins(),
render_frame_host_->GetLastCommittedURL().GetOrigin())) {
devices_.push_back(std::make_pair(device, device->product_string()));
}
}
if (observer())
observer()->OnOptionsInitialized();
}
void UsbChooserBubbleController::set_bubble_reference(
BubbleReference bubble_reference) {
bubble_reference_ = bubble_reference;
}
|