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_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)) { | |
|
Reilly Grant (use Gerrit)
2015/11/16 22:45:45
nit: no braces
juncai
2015/11/17 03:42:15
Done.
| |
| 29 return true; | |
| 30 } | |
| 31 | |
| 32 for (const auto& config : set->configurations) { | |
| 33 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.
| |
| 34 return true; | |
| 35 } | |
| 36 | |
| 37 for (const auto& function : config.functions) { | |
| 38 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.
| |
| 39 return true; | |
| 40 } | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 UsbChooserOptions::UsbChooserOptions( | |
| 50 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | |
| 51 content::RenderFrameHost* render_frame_host, | |
| 52 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) | |
| 53 : device_filters_(device_filters.Pass()), | |
| 54 render_frame_host_(render_frame_host), | |
| 55 callback_(callback), | |
| 56 usb_service_observer_(this) { | |
| 57 device::UsbService* usb_service = | |
| 58 device::DeviceClient::Get()->GetUsbService(); | |
| 59 if (!usb_service) | |
| 60 return; | |
| 61 | |
| 62 if (!usb_service_observer_.IsObserving(usb_service)) | |
| 63 usb_service_observer_.Add(usb_service); | |
| 64 | |
| 65 if (!device_filters_.is_null()) | |
| 66 filters_ = device_filters_.To<std::vector<device::UsbDeviceFilter>>(); | |
| 67 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.
| |
| 68 | |
| 69 usb_service->GetDevices(base::Bind(&UsbChooserOptions::GetUsbDevicesList, | |
| 70 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.
| |
| 71 } | |
| 72 | |
| 73 UsbChooserOptions::~UsbChooserOptions() { | |
| 74 if (!callback_.is_null()) | |
| 75 callback_.Run(nullptr); | |
| 76 } | |
| 77 | |
| 78 const std::vector<base::string16>& UsbChooserOptions::GetOptions() const { | |
| 79 return devices_names_; | |
| 80 } | |
| 81 | |
| 82 // index < 0 means cancel button was pressed. | |
| 83 void UsbChooserOptions::Select(int index) { | |
| 84 if (index >= 0 && index < static_cast<int>(devices_.size())) { | |
| 85 content::WebContents* web_contents = | |
| 86 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
| 87 GURL embedding_origin = | |
| 88 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
| 89 Profile* profile = | |
| 90 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 91 UsbChooserContext* chooser_context = | |
| 92 UsbChooserContextFactory::GetForProfile(profile); | |
| 93 chooser_context->GrantDevicePermission(origin_, embedding_origin, | |
| 94 devices_[index]->guid()); | |
| 95 | |
| 96 device::usb::DeviceInfoPtr device_info_ptr = | |
| 97 device::usb::DeviceInfo::From(*devices_[index]); | |
| 98 callback_.Run(device_info_ptr.Pass()); | |
| 99 } else { | |
| 100 callback_.Run(nullptr); | |
| 101 } | |
| 102 callback_.reset(); // Reset |callback| so that it is only run once. | |
| 103 } | |
| 104 | |
| 105 void UsbChooserOptions::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { | |
| 106 DCHECK(!ContainsValue(devices_, device)); | |
| 107 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 108 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { | |
| 109 devices_.push_back(device); | |
| 110 devices_names_.push_back(device->product_string()); | |
| 111 if (observer()) | |
| 112 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void UsbChooserOptions::OnDeviceRemoved( | |
| 117 scoped_refptr<device::UsbDevice> device) { | |
| 118 auto iter = std::find(devices_.begin(), devices_.end(), device); | |
| 119 if (iter != devices_.end()) { | |
| 120 int index = iter - devices_.begin(); | |
| 121 devices_.erase(iter); | |
| 122 devices_names_.erase(devices_names_.begin() + index); | |
| 123 if (observer()) | |
| 124 observer()->OnOptionRemoved(index); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 // Get a list of devices that can be shown in the chooser bubble UI for | |
| 129 // user to grant permsssion. | |
| 130 void UsbChooserOptions::GetUsbDevicesList( | |
| 131 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
| 132 for (const auto& device : devices) { | |
| 133 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 134 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin_)) { | |
| 135 devices_.push_back(device); | |
| 136 devices_names_.push_back(device->product_string()); | |
| 137 } | |
| 138 } | |
| 139 if (observer()) | |
| 140 observer()->OnOptionsInitialized(); | |
| 141 } | |
| OLD | NEW |