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/web_usb_permission_bubble.h" | |
| 6 | |
| 7 #include "base/stl_util.h" | |
| 8 #include "chrome/browser/profiles/profile_manager.h" | |
| 9 #include "chrome/browser/ui/browser_finder.h" | |
| 10 #include "chrome/browser/ui/chrome_bubble_manager.h" | |
| 11 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS) | |
|
Reilly Grant (use Gerrit)
2015/10/20 22:17:07
or... !defined(OS_MACOSX)
juncai
2015/10/23 03:36:43
Done.
| |
| 12 #include "chrome/browser/ui/views/website_settings/chooser_bubble_delegate.h" | |
| 13 #endif | |
| 14 #include "chrome/browser/usb/web_usb_permission_bubble_request.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/render_frame_host.h" | |
| 17 #include "device/core/device_client.h" | |
| 18 #include "device/devices_app/usb/type_converters.h" | |
| 19 #include "device/usb/usb_device.h" | |
| 20 #include "device/usb/usb_device_filter.h" | |
| 21 #include "device/usb/usb_service.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Check if the origin is in the description set. | |
| 26 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | |
| 27 const GURL& origin) { | |
| 28 if (!set) | |
| 29 return false; | |
| 30 | |
| 31 if (ContainsValue(set->origins, origin)) { | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 for (const auto& config : set->configurations) { | |
| 36 for (const auto& config_origin : config.origins) { | |
| 37 if (origin == config_origin) | |
| 38 return true; | |
|
Reilly Grant (use Gerrit)
2015/10/20 22:17:07
Replace with if (ContainsValue(config.origins, ori
juncai
2015/10/23 03:36:42
Done.
| |
| 39 } | |
| 40 | |
| 41 for (const auto& function : config.functions) { | |
| 42 for (const auto& function_origin : function.origins) { | |
| 43 if (origin == function_origin) | |
| 44 return true; | |
|
Reilly Grant (use Gerrit)
2015/10/20 22:17:07
Replace with if (ContainsValue(function.origins, o
juncai
2015/10/23 03:36:43
Done.
| |
| 45 } | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 // Get a list of devices that can be shown in the chooser bubble UI for | |
| 53 // user to grant permsssion. | |
| 54 void GetUsbDevicesList( | |
| 55 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | |
| 56 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback, | |
| 57 content::RenderFrameHost* render_frame_host, | |
| 58 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
| 59 std::vector<device::UsbDeviceFilter> filters; | |
| 60 if (!device_filters.is_null()) | |
| 61 filters = device_filters.To<std::vector<device::UsbDeviceFilter>>(); | |
| 62 GURL origin = render_frame_host->GetLastCommittedURL().GetOrigin(); | |
| 63 | |
| 64 std::vector<scoped_refptr<device::UsbDevice>> listed_devices; | |
| 65 | |
| 66 for (const auto& device : devices) { | |
| 67 if (device::UsbDeviceFilter::MatchesAny(device, filters) && | |
| 68 FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin)) { | |
| 69 listed_devices.push_back(device); | |
| 70 } | |
| 71 } | |
| 72 #if defined(OS_LINUX) || defined(OS_WIN) || defined(OS_CHROMEOS) | |
| 73 Browser* browser = chrome::FindBrowserWithProfile( | |
| 74 ProfileManager::GetActiveUserProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE); | |
|
Reilly Grant (use Gerrit)
2015/10/20 22:17:07
This is wrong and fragile. Use FindBrowserFromWebC
juncai
2015/10/23 03:36:42
Done.
| |
| 75 | |
| 76 scoped_ptr<WebUsbPermissionBubbleRequest> request( | |
| 77 new WebUsbPermissionBubbleRequest(listed_devices, callback)); | |
| 78 scoped_ptr<BubbleDelegate> bubble_delegate( | |
| 79 new ChooserBubbleDelegate(browser, request.Pass())); | |
| 80 browser->GetBubbleManager()->ShowBubble(bubble_delegate.Pass()); | |
| 81 #endif | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 // static | |
| 87 void ChromeWebUsbPermissionBubble::Create( | |
| 88 content::RenderFrameHost* render_frame_host, | |
| 89 mojo::InterfaceRequest<webusb::WebUsbPermissionBubble> request) { | |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 91 DCHECK(render_frame_host); | |
| 92 | |
| 93 new ChromeWebUsbPermissionBubble(render_frame_host, request.Pass()); | |
| 94 } | |
| 95 | |
| 96 ChromeWebUsbPermissionBubble::ChromeWebUsbPermissionBubble( | |
| 97 content::RenderFrameHost* render_frame_host, | |
| 98 mojo::InterfaceRequest<WebUsbPermissionBubble> request) | |
| 99 : render_frame_host_(render_frame_host), binding_(this, request.Pass()) {} | |
| 100 | |
| 101 ChromeWebUsbPermissionBubble::~ChromeWebUsbPermissionBubble() {} | |
| 102 | |
| 103 void ChromeWebUsbPermissionBubble::GetPermission( | |
| 104 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | |
| 105 const GetPermissionCallback& callback) { | |
| 106 device::UsbService* usb_service = | |
| 107 device::DeviceClient::Get()->GetUsbService(); | |
| 108 | |
| 109 usb_service->GetDevices(base::Bind(&GetUsbDevicesList, | |
| 110 base::Passed(&device_filters), callback, | |
| 111 render_frame_host_)); | |
| 112 } | |
| OLD | NEW |