Chromium Code Reviews| Index: chrome/browser/usb/usb_chooser_options.cc |
| diff --git a/chrome/browser/usb/usb_chooser_options.cc b/chrome/browser/usb/usb_chooser_options.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eec9e7dac3dc19631e1998a5138af6da18317fdf |
| --- /dev/null |
| +++ b/chrome/browser/usb/usb_chooser_options.cc |
| @@ -0,0 +1,141 @@ |
| +// 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_options.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/stl_util.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/usb/usb_chooser_context.h" |
| +#include "chrome/browser/usb/usb_chooser_context_factory.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/browser/web_contents.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)) { |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:45
nit: no braces
juncai
2015/11/17 03:42:15
Done.
|
| + return true; |
| + } |
| + |
| + for (const auto& config : set->configurations) { |
| + if (ContainsValue(config.origins, origin)) { |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:45
nit: no braces
juncai
2015/11/17 03:42:15
Done.
|
| + return true; |
| + } |
| + |
| + for (const auto& function : config.functions) { |
| + if (ContainsValue(function.origins, origin)) { |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:46
nit: no braces
juncai
2015/11/17 03:42:15
Done.
|
| + return true; |
| + } |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +UsbChooserOptions::UsbChooserOptions( |
| + 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; |
| + |
| + if (!usb_service_observer_.IsObserving(usb_service)) |
| + 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(); |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:45
Since this object has a reference to the RenderFra
juncai
2015/11/17 03:42:15
Done.
|
| + |
| + usb_service->GetDevices(base::Bind(&UsbChooserOptions::GetUsbDevicesList, |
| + base::Unretained(this))); |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:45
Use a WeakPtr here in case the bubble is closed be
juncai
2015/11/17 03:42:15
Done.
|
| +} |
| + |
| +UsbChooserOptions::~UsbChooserOptions() { |
| + if (!callback_.is_null()) |
| + callback_.Run(nullptr); |
| +} |
| + |
| +const std::vector<base::string16>& UsbChooserOptions::GetOptions() const { |
| + return devices_names_; |
| +} |
| + |
| +// index < 0 means cancel button was pressed. |
| +void UsbChooserOptions::Select(int index) { |
| + if (index >= 0 && index < static_cast<int>(devices_.size())) { |
| + content::WebContents* web_contents = |
| + content::WebContents::FromRenderFrameHost(render_frame_host_); |
| + GURL embedding_origin = |
| + web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
| + Profile* profile = |
| + Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| + UsbChooserContext* chooser_context = |
| + UsbChooserContextFactory::GetForProfile(profile); |
| + chooser_context->GrantDevicePermission(origin_, embedding_origin, |
| + devices_[index]->guid()); |
| + |
| + 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 run once. |
| +} |
| + |
| +void UsbChooserOptions::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { |
| + DCHECK(!ContainsValue(devices_, device)); |
| + if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
| + FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { |
| + devices_.push_back(device); |
| + devices_names_.push_back(device->product_string()); |
| + if (observer()) |
| + observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1); |
| + } |
| +} |
| + |
| +void UsbChooserOptions::OnDeviceRemoved( |
| + scoped_refptr<device::UsbDevice> device) { |
| + auto iter = std::find(devices_.begin(), devices_.end(), device); |
| + if (iter != devices_.end()) { |
| + int index = iter - devices_.begin(); |
| + devices_.erase(iter); |
| + devices_names_.erase(devices_names_.begin() + index); |
| + if (observer()) |
| + observer()->OnOptionRemoved(index); |
| + } |
| +} |
| + |
| +// Get a list of devices that can be shown in the chooser bubble UI for |
| +// user to grant permsssion. |
| +void UsbChooserOptions::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(device->product_string()); |
| + } |
| + } |
| + if (observer()) |
| + observer()->OnOptionsInitialized(); |
| +} |