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

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

Issue 1408193003: Add chrome side webusb permission UI code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address sky@'s comments, added TODO to chrome_bubble_manager.cc Created 5 years 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
« no previous file with comments | « chrome/browser/usb/usb_chooser_bubble_delegate.h ('k') | chrome/browser/usb/usb_tab_helper.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_bubble_delegate.h"
6
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/usb/usb_chooser_context.h"
11 #include "chrome/browser/usb/usb_chooser_context_factory.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_contents.h"
14 #include "device/core/device_client.h"
15 #include "device/devices_app/usb/type_converters.h"
16 #include "device/usb/usb_device.h"
17 #include "device/usb/usb_device_filter.h"
18 #include "url/gurl.h"
19
20 namespace {
21
22 // Check if the origin is in the description set.
23 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set,
24 const GURL& origin) {
25 if (!set)
26 return false;
27
28 if (ContainsValue(set->origins, origin))
29 return true;
30
31 for (const auto& config : set->configurations) {
32 if (ContainsValue(config.origins, origin))
33 return true;
34
35 for (const auto& function : config.functions) {
36 if (ContainsValue(function.origins, origin))
37 return true;
38 }
39 }
40
41 return false;
42 }
43
44 } // namespace
45
46 UsbChooserBubbleDelegate::UsbChooserBubbleDelegate(
47 Browser* browser,
48 mojo::Array<device::usb::DeviceFilterPtr> device_filters,
49 content::RenderFrameHost* render_frame_host,
50 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback)
51 : ChooserBubbleDelegate(browser),
52 render_frame_host_(render_frame_host),
53 callback_(callback),
54 usb_service_observer_(this),
55 weak_factory_(this) {
56 device::UsbService* usb_service =
57 device::DeviceClient::Get()->GetUsbService();
58 if (!usb_service)
59 return;
60
61 if (!usb_service_observer_.IsObserving(usb_service))
62 usb_service_observer_.Add(usb_service);
63
64 if (!device_filters.is_null())
65 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>();
66
67 usb_service->GetDevices(base::Bind(
68 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr()));
69 }
70
71 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() {
72 if (!callback_.is_null())
73 callback_.Run(nullptr);
74 }
75
76 const std::vector<base::string16>& UsbChooserBubbleDelegate::GetOptions()
77 const {
78 return devices_names_;
79 }
80
81 void UsbChooserBubbleDelegate::Select(int index) {
82 size_t idx = static_cast<size_t>(index);
83 size_t num_options = devices_.size();
84 DCHECK_LT(idx, num_options);
85 if (idx < num_options) {
86 content::WebContents* web_contents =
87 content::WebContents::FromRenderFrameHost(render_frame_host_);
88 GURL embedding_origin =
89 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin();
90 Profile* profile =
91 Profile::FromBrowserContext(web_contents->GetBrowserContext());
92 UsbChooserContext* chooser_context =
93 UsbChooserContextFactory::GetForProfile(profile);
94 chooser_context->GrantDevicePermission(
95 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin,
96 devices_[idx]->guid());
97
98 device::usb::DeviceInfoPtr device_info_ptr =
99 device::usb::DeviceInfo::From(*devices_[idx]);
100 callback_.Run(device_info_ptr.Pass());
101 } else {
102 callback_.Run(nullptr);
103 }
104 callback_.reset(); // Reset |callback_| so that it is only run once.
105 }
106
107 void UsbChooserBubbleDelegate::Cancel() {}
108
109 void UsbChooserBubbleDelegate::Close() {}
110
111 void UsbChooserBubbleDelegate::OnDeviceAdded(
112 scoped_refptr<device::UsbDevice> device) {
113 DCHECK(!ContainsValue(devices_, device));
114 if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
115 FindOriginInDescriptorSet(
116 device->webusb_allowed_origins(),
117 render_frame_host_->GetLastCommittedURL().GetOrigin())) {
118 devices_.push_back(device);
119 devices_names_.push_back(device->product_string());
120 if (observer())
121 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1);
122 }
123 }
124
125 void UsbChooserBubbleDelegate::OnDeviceRemoved(
126 scoped_refptr<device::UsbDevice> device) {
127 auto iter = std::find(devices_.begin(), devices_.end(), device);
128 if (iter != devices_.end()) {
129 int index = iter - devices_.begin();
130 devices_.erase(iter);
131 devices_names_.erase(devices_names_.begin() + index);
132 if (observer())
133 observer()->OnOptionRemoved(index);
134 }
135 }
136
137 // Get a list of devices that can be shown in the chooser bubble UI for
138 // user to grant permsssion.
139 void UsbChooserBubbleDelegate::GotUsbDeviceList(
140 const std::vector<scoped_refptr<device::UsbDevice>>& devices) {
141 for (const auto& device : devices) {
142 if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
143 FindOriginInDescriptorSet(
144 device->webusb_allowed_origins(),
145 render_frame_host_->GetLastCommittedURL().GetOrigin())) {
146 devices_.push_back(device);
147 devices_names_.push_back(device->product_string());
148 }
149 }
150 if (observer())
151 observer()->OnOptionsInitialized();
152 }
OLDNEW
« no previous file with comments | « chrome/browser/usb/usb_chooser_bubble_delegate.h ('k') | chrome/browser/usb/usb_tab_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698