| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/usb/usb_chooser_bubble_delegate.h" | 5 #include "chrome/browser/usb/usb_chooser_bubble_delegate.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED, | 41 WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED, |
| 42 // Maximum value for the enum. | 42 // Maximum value for the enum. |
| 43 WEBUSB_CHOOSER_CLOSED_MAX | 43 WEBUSB_CHOOSER_CLOSED_MAX |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 void RecordChooserClosure(WebUsbChooserClosed disposition) { | 46 void RecordChooserClosure(WebUsbChooserClosed disposition) { |
| 47 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", disposition, | 47 UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", disposition, |
| 48 WEBUSB_CHOOSER_CLOSED_MAX); | 48 WEBUSB_CHOOSER_CLOSED_MAX); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Check if the origin is in the description set. | 51 // Check if the origin is allowed. |
| 52 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | 52 bool FindInAllowedOrigins(const device::WebUsbAllowedOrigins* allowed_origins, |
| 53 const GURL& origin) { | 53 const GURL& origin) { |
| 54 if (!set) | 54 if (!allowed_origins) |
| 55 return false; | 55 return false; |
| 56 | 56 |
| 57 if (ContainsValue(set->origins, origin)) | 57 if (ContainsValue(allowed_origins->origins, origin)) |
| 58 return true; | 58 return true; |
| 59 | 59 |
| 60 for (const auto& config : set->configurations) { | 60 for (const auto& config : allowed_origins->configurations) { |
| 61 if (ContainsValue(config.origins, origin)) | 61 if (ContainsValue(config.origins, origin)) |
| 62 return true; | 62 return true; |
| 63 | 63 |
| 64 for (const auto& function : config.functions) { | 64 for (const auto& function : config.functions) { |
| 65 if (ContainsValue(function.origins, origin)) | 65 if (ContainsValue(function.origins, origin)) |
| 66 return true; | 66 return true; |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 return false; | 70 return false; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 if (bubble_controller_) | 146 if (bubble_controller_) |
| 147 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 147 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void UsbChooserBubbleDelegate::Close() {} | 150 void UsbChooserBubbleDelegate::Close() {} |
| 151 | 151 |
| 152 void UsbChooserBubbleDelegate::OnDeviceAdded( | 152 void UsbChooserBubbleDelegate::OnDeviceAdded( |
| 153 scoped_refptr<device::UsbDevice> device) { | 153 scoped_refptr<device::UsbDevice> device) { |
| 154 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 154 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
| 155 FindOriginInDescriptorSet( | 155 FindInAllowedOrigins( |
| 156 device->webusb_allowed_origins(), | 156 device->webusb_allowed_origins(), |
| 157 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 157 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
| 158 devices_.push_back(std::make_pair(device, device->product_string())); | 158 devices_.push_back(std::make_pair(device, device->product_string())); |
| 159 if (observer()) | 159 if (observer()) |
| 160 observer()->OnOptionAdded(devices_.size() - 1); | 160 observer()->OnOptionAdded(devices_.size() - 1); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 | 163 |
| 164 void UsbChooserBubbleDelegate::OnDeviceRemoved( | 164 void UsbChooserBubbleDelegate::OnDeviceRemoved( |
| 165 scoped_refptr<device::UsbDevice> device) { | 165 scoped_refptr<device::UsbDevice> device) { |
| 166 for (auto it = devices_.begin(); it != devices_.end(); ++it) { | 166 for (auto it = devices_.begin(); it != devices_.end(); ++it) { |
| 167 if (it->first == device) { | 167 if (it->first == device) { |
| 168 size_t index = it - devices_.begin(); | 168 size_t index = it - devices_.begin(); |
| 169 devices_.erase(it); | 169 devices_.erase(it); |
| 170 if (observer()) | 170 if (observer()) |
| 171 observer()->OnOptionRemoved(index); | 171 observer()->OnOptionRemoved(index); |
| 172 return; | 172 return; |
| 173 } | 173 } |
| 174 } | 174 } |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Get a list of devices that can be shown in the chooser bubble UI for | 177 // Get a list of devices that can be shown in the chooser bubble UI for |
| 178 // user to grant permsssion. | 178 // user to grant permsssion. |
| 179 void UsbChooserBubbleDelegate::GotUsbDeviceList( | 179 void UsbChooserBubbleDelegate::GotUsbDeviceList( |
| 180 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | 180 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
| 181 for (const auto& device : devices) { | 181 for (const auto& device : devices) { |
| 182 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 182 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
| 183 FindOriginInDescriptorSet( | 183 FindInAllowedOrigins( |
| 184 device->webusb_allowed_origins(), | 184 device->webusb_allowed_origins(), |
| 185 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 185 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
| 186 devices_.push_back(std::make_pair(device, device->product_string())); | 186 devices_.push_back(std::make_pair(device, device->product_string())); |
| 187 } | 187 } |
| 188 } | 188 } |
| 189 if (observer()) | 189 if (observer()) |
| 190 observer()->OnOptionsInitialized(); | 190 observer()->OnOptionsInitialized(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void UsbChooserBubbleDelegate::set_bubble_controller( | 193 void UsbChooserBubbleDelegate::set_bubble_controller( |
| 194 BubbleReference bubble_controller) { | 194 BubbleReference bubble_controller) { |
| 195 bubble_controller_ = bubble_controller; | 195 bubble_controller_ = bubble_controller; |
| 196 } | 196 } |
| OLD | NEW |