OLD | NEW |
---|---|
(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_options.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 "device/usb/usb_service.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 UsbChooserOptions::UsbChooserOptions( | |
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()), | |
Reilly Grant (use Gerrit)
2015/11/18 00:17:38
This doesn't need to be a member variable if it's
juncai
2015/11/19 05:42:00
Done.
| |
51 render_frame_host_(render_frame_host), | |
52 callback_(callback), | |
53 usb_service_observer_(this), | |
54 weak_factory_(this) { | |
55 device::UsbService* usb_service = | |
56 device::DeviceClient::Get()->GetUsbService(); | |
57 if (!usb_service) | |
58 return; | |
59 | |
60 if (!usb_service_observer_.IsObserving(usb_service)) | |
61 usb_service_observer_.Add(usb_service); | |
62 | |
63 if (!device_filters_.is_null()) | |
64 filters_ = device_filters_.To<std::vector<device::UsbDeviceFilter>>(); | |
65 | |
66 usb_service->GetDevices(base::Bind(&UsbChooserOptions::GotUsbDeviceList, | |
67 weak_factory_.GetWeakPtr())); | |
68 } | |
69 | |
70 UsbChooserOptions::~UsbChooserOptions() { | |
71 if (!callback_.is_null()) | |
72 callback_.Run(nullptr); | |
73 } | |
74 | |
75 const std::vector<base::string16>& UsbChooserOptions::GetOptions() const { | |
76 return devices_names_; | |
77 } | |
78 | |
79 // index < 0 means cancel button was pressed. | |
80 void UsbChooserOptions::Select(int index) { | |
81 if (index >= 0 && index < static_cast<int>(devices_.size())) { | |
82 content::WebContents* web_contents = | |
83 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
84 GURL embedding_origin = | |
85 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
86 Profile* profile = | |
87 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
88 UsbChooserContext* chooser_context = | |
89 UsbChooserContextFactory::GetForProfile(profile); | |
90 chooser_context->GrantDevicePermission( | |
91 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
92 devices_[index]->guid()); | |
93 | |
94 device::usb::DeviceInfoPtr device_info_ptr = | |
95 device::usb::DeviceInfo::From(*devices_[index]); | |
96 callback_.Run(device_info_ptr.Pass()); | |
97 } else { | |
98 callback_.Run(nullptr); | |
99 } | |
100 callback_.reset(); // Reset |callback| so that it is only run once. | |
Reilly Grant (use Gerrit)
2015/11/18 00:17:38
|callback_|
juncai
2015/11/19 05:42:00
Done.
| |
101 } | |
102 | |
103 void UsbChooserOptions::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { | |
104 DCHECK(!ContainsValue(devices_, device)); | |
105 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
106 FindOriginInDescriptorSet( | |
107 device->webusb_allowed_origins(), | |
108 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
109 devices_.push_back(device); | |
110 devices_names_.push_back(device->product_string()); | |
111 if (observer()) | |
112 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1); | |
113 } | |
114 } | |
115 | |
116 void UsbChooserOptions::OnDeviceRemoved( | |
117 scoped_refptr<device::UsbDevice> device) { | |
118 auto iter = std::find(devices_.begin(), devices_.end(), device); | |
119 if (iter != devices_.end()) { | |
120 int index = iter - devices_.begin(); | |
121 devices_.erase(iter); | |
122 devices_names_.erase(devices_names_.begin() + index); | |
123 if (observer()) | |
124 observer()->OnOptionRemoved(index); | |
125 } | |
126 } | |
127 | |
128 // Get a list of devices that can be shown in the chooser bubble UI for | |
129 // user to grant permsssion. | |
130 void UsbChooserOptions::GotUsbDeviceList( | |
131 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
132 for (const auto& device : devices) { | |
133 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
134 FindOriginInDescriptorSet( | |
135 device->webusb_allowed_origins(), | |
136 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
137 devices_.push_back(device); | |
138 devices_names_.push_back(device->product_string()); | |
139 } | |
140 } | |
141 if (observer()) | |
142 observer()->OnOptionsInitialized(); | |
143 } | |
OLD | NEW |