Chromium Code Reviews| 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_choices.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/usb/usb_chooser_context.h" | |
| 12 #include "chrome/browser/usb/usb_chooser_context_factory.h" | |
| 13 #include "content/public/browser/render_frame_host.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "device/core/device_client.h" | |
| 16 #include "device/devices_app/usb/type_converters.h" | |
| 17 #include "device/usb/usb_device.h" | |
| 18 #include "device/usb/usb_device_filter.h" | |
| 19 #include "device/usb/usb_service.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Check if the origin is in the description set. | |
| 24 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | |
| 25 const GURL& origin) { | |
| 26 if (!set) | |
| 27 return false; | |
| 28 | |
| 29 if (ContainsValue(set->origins, origin)) { | |
| 30 return true; | |
| 31 } | |
| 32 | |
| 33 for (const auto& config : set->configurations) { | |
| 34 if (ContainsValue(config.origins, origin)) { | |
| 35 return true; | |
| 36 } | |
| 37 | |
| 38 for (const auto& function : config.functions) { | |
| 39 if (ContainsValue(function.origins, origin)) { | |
| 40 return true; | |
| 41 } | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 UsbChooserChoices::UsbChooserChoices( | |
| 51 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | |
| 52 content::RenderFrameHost* render_frame_host, | |
| 53 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) | |
| 54 : device_filters_(device_filters.Pass()), | |
| 55 render_frame_host_(render_frame_host), | |
| 56 callback_(callback), | |
| 57 usb_service_observer_(this) { | |
| 58 device::UsbService* usb_service = | |
| 59 device::DeviceClient::Get()->GetUsbService(); | |
| 60 if (!usb_service) | |
| 61 return; | |
| 62 | |
| 63 if (!usb_service_observer_.IsObserving(usb_service)) | |
| 64 usb_service_observer_.Add(usb_service); | |
| 65 | |
| 66 if (!device_filters_.is_null()) | |
| 67 filters_ = device_filters_.To<std::vector<device::UsbDeviceFilter>>(); | |
| 68 origin_ = render_frame_host_->GetLastCommittedURL().GetOrigin(); | |
| 69 | |
| 70 usb_service->GetDevices(base::Bind(&UsbChooserChoices::GetUsbDevicesList, | |
| 71 base::Unretained(this))); | |
| 72 } | |
| 73 | |
| 74 UsbChooserChoices::~UsbChooserChoices() { | |
| 75 if (!callback_.is_null()) | |
| 76 callback_.Run(nullptr); | |
| 77 } | |
| 78 | |
| 79 const std::vector<std::string>& UsbChooserChoices::GetChoices() const { | |
| 80 return devices_names_; | |
| 81 } | |
| 82 | |
| 83 // index < 0 means cancel button was pressed. | |
| 84 void UsbChooserChoices::Select(int index) { | |
| 85 if (index >= 0 && index < static_cast<int>(devices_.size())) { | |
| 86 content::WebContents* web_contents = | |
| 87 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
| 88 GURL embedding_origin = | |
| 89 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
| 90 Profile* profile = | |
| 91 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 92 UsbChooserContext* chooser_context = | |
| 93 UsbChooserContextFactory::GetForProfile(profile); | |
| 94 chooser_context->GrantDevicePermission(origin_, embedding_origin, | |
| 95 devices_[index]->guid()); | |
| 96 | |
| 97 device::usb::DeviceInfoPtr device_info_ptr = | |
| 98 device::usb::DeviceInfo::From(*devices_[index]); | |
| 99 callback_.Run(device_info_ptr.Pass()); | |
| 100 } else { | |
| 101 callback_.Run(nullptr); | |
| 102 } | |
| 103 callback_.reset(); // Reset |callback| so that it is only run once. | |
| 104 } | |
| 105 | |
| 106 void UsbChooserChoices::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { | |
| 107 DCHECK(!ContainsValue(devices_, device)); | |
| 108 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 109 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { | |
| 110 devices_.push_back(device); | |
| 111 devices_names_.push_back(base::UTF16ToASCII(device->product_string())); | |
|
Reilly Grant (use Gerrit)
2015/11/12 21:44:37
Keep the UTF16 string all the way through, definit
juncai
2015/11/14 01:54:36
Done.
| |
| 112 if (observer()) | |
| 113 observer()->OnChoiceAdded(static_cast<int>(devices_names_.size()) - 1); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void UsbChooserChoices::OnDeviceRemoved( | |
| 118 scoped_refptr<device::UsbDevice> device) { | |
| 119 auto itor = std::find(devices_.begin(), devices_.end(), device); | |
|
Reilly Grant (use Gerrit)
2015/11/12 21:44:37
s/itor/iter/g
juncai
2015/11/14 01:54:36
Done.
| |
| 120 if (itor != devices_.end()) { | |
| 121 int index = itor - devices_.begin(); | |
| 122 devices_.erase(itor); | |
| 123 devices_names_.erase(devices_names_.begin() + index); | |
| 124 if (observer()) | |
| 125 observer()->OnChoiceRemoved(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 UsbChooserChoices::GetUsbDevicesList( | |
| 132 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
| 133 for (const auto& device : devices) { | |
| 134 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 135 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { | |
| 136 devices_.push_back(device); | |
| 137 devices_names_.push_back(base::UTF16ToASCII(device->product_string())); | |
| 138 } | |
| 139 } | |
| 140 if (observer()) | |
| 141 observer()->OnChoicesInitialized(); | |
| 142 } | |
| OLD | NEW |