 Chromium Code Reviews
 Chromium Code Reviews Issue 1408193003:
  Add chrome side webusb permission UI code  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1408193003:
  Add chrome side webusb permission UI code  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 if (index >= 0 && index < static_cast<int>(devices_.size())) { | |
| 
felt
2015/12/03 01:19:19
should the inverse of this condition (index >= # o
 
juncai
2015/12/03 18:33:59
Done.
 | |
| 81 content::WebContents* web_contents = | |
| 82 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
| 83 GURL embedding_origin = | |
| 84 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
| 85 Profile* profile = | |
| 86 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 87 UsbChooserContext* chooser_context = | |
| 88 UsbChooserContextFactory::GetForProfile(profile); | |
| 89 chooser_context->GrantDevicePermission( | |
| 90 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
| 91 devices_[index]->guid()); | |
| 92 | |
| 93 device::usb::DeviceInfoPtr device_info_ptr = | |
| 94 device::usb::DeviceInfo::From(*devices_[index]); | |
| 95 callback_.Run(device_info_ptr.Pass()); | |
| 96 } else { | |
| 97 callback_.Run(nullptr); | |
| 98 } | |
| 99 callback_.reset(); // Reset |callback_| so that it is only run once. | |
| 100 } | |
| 101 | |
| 102 void UsbChooserOptions::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { | |
| 103 DCHECK(!ContainsValue(devices_, device)); | |
| 104 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 105 FindOriginInDescriptorSet( | |
| 106 device->webusb_allowed_origins(), | |
| 107 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
| 108 devices_.push_back(device); | |
| 109 devices_names_.push_back(device->product_string()); | |
| 110 if (observer()) | |
| 111 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void UsbChooserOptions::OnDeviceRemoved( | |
| 116 scoped_refptr<device::UsbDevice> device) { | |
| 117 auto iter = std::find(devices_.begin(), devices_.end(), device); | |
| 118 if (iter != devices_.end()) { | |
| 119 int index = iter - devices_.begin(); | |
| 120 devices_.erase(iter); | |
| 121 devices_names_.erase(devices_names_.begin() + index); | |
| 122 if (observer()) | |
| 123 observer()->OnOptionRemoved(index); | |
| 124 } | |
| 
felt
2015/12/03 01:19:19
why doesn't anything happen if it's the last devic
 
juncai
2015/12/03 18:33:59
User may accidentally unplug the listed device, an
 | |
| 125 } | |
| 126 | |
| 127 // Get a list of devices that can be shown in the chooser bubble UI for | |
| 128 // user to grant permsssion. | |
| 129 void UsbChooserOptions::GotUsbDeviceList( | |
| 130 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
| 131 for (const auto& device : devices) { | |
| 132 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
| 133 FindOriginInDescriptorSet( | |
| 134 device->webusb_allowed_origins(), | |
| 135 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
| 136 devices_.push_back(device); | |
| 137 devices_names_.push_back(device->product_string()); | |
| 138 } | |
| 139 } | |
| 140 if (observer()) | |
| 141 observer()->OnOptionsInitialized(); | |
| 142 } | |
| OLD | NEW |