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