Chromium Code Reviews| Index: chrome/browser/usb/usb_chooser_choices.cc |
| diff --git a/chrome/browser/usb/usb_chooser_choices.cc b/chrome/browser/usb/usb_chooser_choices.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c20c5edabe2dc448bbfaaf0b6b2a6e408aa62c6f |
| --- /dev/null |
| +++ b/chrome/browser/usb/usb_chooser_choices.cc |
| @@ -0,0 +1,126 @@ |
| +// 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_choices.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/stl_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "device/core/device_client.h" |
| +#include "device/devices_app/usb/type_converters.h" |
| +#include "device/usb/usb_device.h" |
| +#include "device/usb/usb_device_filter.h" |
| +#include "device/usb/usb_service.h" |
| + |
| +namespace { |
| + |
| +// Check if the origin is in the description set. |
| +bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, |
| + const GURL& origin) { |
| + if (!set) |
| + return false; |
| + |
| + if (ContainsValue(set->origins, origin)) { |
| + return true; |
| + } |
| + |
| + for (const auto& config : set->configurations) { |
| + if (ContainsValue(config.origins, origin)) { |
| + return true; |
| + } |
| + |
| + for (const auto& function : config.functions) { |
| + if (ContainsValue(function.origins, origin)) { |
| + return true; |
| + } |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +UsbChooserChoices::UsbChooserChoices( |
| + mojo::Array<device::usb::DeviceFilterPtr> device_filters, |
| + content::RenderFrameHost* render_frame_host, |
| + const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) |
| + : device_filters_(device_filters.Pass()), |
| + render_frame_host_(render_frame_host), |
| + callback_(callback), |
| + usb_service_observer_(this) { |
| + device::UsbService* usb_service = |
| + device::DeviceClient::Get()->GetUsbService(); |
| + if (!usb_service) |
| + return; |
| + |
| + usb_service_observer_.Add(usb_service); |
| + |
| + if (!device_filters_.is_null()) |
| + filters_ = device_filters_.To<std::vector<device::UsbDeviceFilter>>(); |
| + origin_ = render_frame_host_->GetLastCommittedURL().GetOrigin(); |
| + |
| + usb_service->GetDevices(base::Bind(&UsbChooserChoices::GetUsbDevicesList, |
| + base::Unretained(this))); |
| +} |
| + |
| +UsbChooserChoices::~UsbChooserChoices() { |
| + if (!callback_.is_null()) { |
|
Reilly Grant (use Gerrit)
2015/10/29 01:11:33
nit: No braces for single line ifs.
juncai
2015/10/31 04:15:36
Done.
|
| + callback_.Run(nullptr); |
| + } |
| +} |
| + |
| +const std::vector<std::string>& UsbChooserChoices::GetChoices() const { |
| + return devices_names_; |
| +} |
| + |
| +// index < 0 means cancel button was pressed. |
| +void UsbChooserChoices::Select(int index) { |
| + if (index >= 0 && index < static_cast<int>(devices_.size())) { |
| + device::usb::DeviceInfoPtr device_info_ptr = |
| + device::usb::DeviceInfo::From(*devices_[index]); |
| + callback_.Run(device_info_ptr.Pass()); |
| + } else { |
| + callback_.Run(nullptr); |
| + } |
| + callback_.reset(); // reset callback so that it is only called once. |
|
Reilly Grant (use Gerrit)
2015/10/29 01:11:32
nit: "Reset |callback| so that it is only run once
juncai
2015/10/31 04:15:36
Done.
|
| +} |
| + |
| +void UsbChooserChoices::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { |
| + auto itor = std::find(devices_.begin(), devices_.end(), device); |
| + if (itor == devices_.end() && |
|
Reilly Grant (use Gerrit)
2015/10/29 01:11:32
There will never be an OnDeviceAdded call for a de
juncai
2015/10/31 04:15:36
Done.
|
| + device::UsbDeviceFilter::MatchesAny(device, filters_) && |
| + FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { |
| + devices_.push_back(device); |
| + devices_names_.push_back(base::UTF16ToASCII(device->product_string())); |
| + recently_updated_index_ = static_cast<int>(devices_names_.size()) - 1; |
| + observer()->OnChoicesChanged(); |
| + } |
| +} |
| + |
| +void UsbChooserChoices::OnDeviceRemoved( |
| + scoped_refptr<device::UsbDevice> device) { |
| + auto itor = std::find(devices_.begin(), devices_.end(), device); |
| + if (itor != devices_.end()) { |
| + recently_updated_index_ = itor - devices_.begin(); |
| + devices_.erase(itor); |
| + devices_names_.erase(devices_names_.begin() + recently_updated_index_); |
| + observer()->OnChoicesChanged(); |
| + } |
| +} |
| + |
| +// Get a list of devices that can be shown in the chooser bubble UI for |
| +// user to grant permsssion. |
| +void UsbChooserChoices::GetUsbDevicesList( |
| + const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
| + for (const auto& device : devices) { |
| + if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
| + FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { |
| + devices_.push_back(device); |
| + devices_names_.push_back(base::UTF16ToASCII(device->product_string())); |
| + } |
| + } |
| + observer()->OnChoicesChanged(); |
| +} |