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

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: updated comments for ChooserBubbleDelegate 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
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 #if defined(OS_MACOSX)
21 #include "components/bubble/bubble_ui.h"
22 #else
23 #include "chrome/browser/ui/views/website_settings/chooser_bubble_ui_view.h"
24 #endif
25
26 namespace {
27
28 // Check if the origin is in the description set.
29 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set,
30 const GURL& origin) {
31 if (!set)
32 return false;
33
34 if (ContainsValue(set->origins, origin))
35 return true;
36
37 for (const auto& config : set->configurations) {
38 if (ContainsValue(config.origins, origin))
39 return true;
40
41 for (const auto& function : config.functions) {
42 if (ContainsValue(function.origins, origin))
43 return true;
44 }
45 }
46
47 return false;
48 }
49
50 } // namespace
51
52 UsbChooserBubbleDelegate::UsbChooserBubbleDelegate(
53 Browser* browser,
54 mojo::Array<device::usb::DeviceFilterPtr> device_filters,
55 content::RenderFrameHost* render_frame_host,
56 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback)
57 : browser_(browser),
58 render_frame_host_(render_frame_host),
59 callback_(callback),
60 usb_service_observer_(this),
61 weak_factory_(this) {
62 DCHECK(browser_);
63 device::UsbService* usb_service =
64 device::DeviceClient::Get()->GetUsbService();
65 if (!usb_service)
66 return;
67
68 if (!usb_service_observer_.IsObserving(usb_service))
69 usb_service_observer_.Add(usb_service);
70
71 if (!device_filters.is_null())
72 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>();
73
74 usb_service->GetDevices(base::Bind(
75 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr()));
76 }
77
78 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() {
79 if (!callback_.is_null())
80 callback_.Run(nullptr);
81 }
82
83 scoped_ptr<BubbleUi> UsbChooserBubbleDelegate::BuildBubbleUi() {
84 scoped_ptr<BubbleUi> bubble_ui;
85 // TODO(juncai): Add chooser bubble ui cocoa code for Mac.
86 // Please refer to http://crbug.com/492204 for more information.
87 #if !defined(OS_MACOSX)
88 bubble_ui.reset(new ChooserBubbleUiView(browser_, this));
89 #endif
90 return bubble_ui.Pass();
91 }
92
93 const std::vector<base::string16>& UsbChooserBubbleDelegate::GetOptions()
94 const {
95 return devices_names_;
96 }
97
98 void UsbChooserBubbleDelegate::Select(int index) {
99 size_t idx = static_cast<size_t>(index);
100 size_t num_options = devices_.size();
101 DCHECK_LT(idx, num_options);
102 if (idx < num_options) {
103 content::WebContents* web_contents =
104 content::WebContents::FromRenderFrameHost(render_frame_host_);
105 GURL embedding_origin =
106 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin();
107 Profile* profile =
108 Profile::FromBrowserContext(web_contents->GetBrowserContext());
109 UsbChooserContext* chooser_context =
110 UsbChooserContextFactory::GetForProfile(profile);
111 chooser_context->GrantDevicePermission(
112 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin,
113 devices_[idx]->guid());
114
115 device::usb::DeviceInfoPtr device_info_ptr =
116 device::usb::DeviceInfo::From(*devices_[idx]);
117 callback_.Run(device_info_ptr.Pass());
118 } else {
119 callback_.Run(nullptr);
120 }
121 callback_.reset(); // Reset |callback_| so that it is only run once.
122 }
123
124 void UsbChooserBubbleDelegate::Cancel() {}
125
126 void UsbChooserBubbleDelegate::Close() {}
127
128 void UsbChooserBubbleDelegate::OnDeviceAdded(
129 scoped_refptr<device::UsbDevice> device) {
130 DCHECK(!ContainsValue(devices_, device));
131 if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
132 FindOriginInDescriptorSet(
133 device->webusb_allowed_origins(),
134 render_frame_host_->GetLastCommittedURL().GetOrigin())) {
135 devices_.push_back(device);
136 devices_names_.push_back(device->product_string());
137 if (observer())
138 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1);
139 }
140 }
141
142 void UsbChooserBubbleDelegate::OnDeviceRemoved(
143 scoped_refptr<device::UsbDevice> device) {
144 auto iter = std::find(devices_.begin(), devices_.end(), device);
145 if (iter != devices_.end()) {
146 int index = iter - devices_.begin();
147 devices_.erase(iter);
148 devices_names_.erase(devices_names_.begin() + index);
149 if (observer())
150 observer()->OnOptionRemoved(index);
151 }
152 }
153
154 // Get a list of devices that can be shown in the chooser bubble UI for
155 // user to grant permsssion.
156 void UsbChooserBubbleDelegate::GotUsbDeviceList(
157 const std::vector<scoped_refptr<device::UsbDevice>>& devices) {
158 for (const auto& device : devices) {
159 if (device::UsbDeviceFilter::MatchesAny(device, filters_) &&
160 FindOriginInDescriptorSet(
161 device->webusb_allowed_origins(),
162 render_frame_host_->GetLastCommittedURL().GetOrigin())) {
163 devices_.push_back(device);
164 devices_names_.push_back(device->product_string());
165 }
166 }
167 if (observer())
168 observer()->OnOptionsInitialized();
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698