Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/usb/web_usb_permission_provider.cc

Issue 1382783002: Store USB device permissions in website settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove UsbPermissionContext and add ChooserPermissionContext helper functions. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/usb/web_usb_permission_provider.h" 5 #include "chrome/browser/usb/web_usb_permission_provider.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/usb/usb_chooser_permission_context.h"
10 #include "chrome/browser/usb/usb_chooser_permission_context_factory.h"
9 #include "chrome/common/chrome_switches.h" 11 #include "chrome/common/chrome_switches.h"
10 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/render_frame_host.h" 13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/web_contents.h"
12 #include "device/core/device_client.h" 16 #include "device/core/device_client.h"
13 17
18 using content::RenderFrameHost;
19 using content::WebContents;
14 using device::usb::WebUsbDescriptorSet; 20 using device::usb::WebUsbDescriptorSet;
15 using device::usb::WebUsbConfigurationSubsetPtr; 21 using device::usb::WebUsbConfigurationSubsetPtr;
16 using device::usb::WebUsbFunctionSubsetPtr; 22 using device::usb::WebUsbFunctionSubsetPtr;
17 23
18 namespace { 24 namespace {
19 25
20 bool FindOriginInDescriptorSet(const WebUsbDescriptorSet* set, 26 bool FindOriginInDescriptorSet(const WebUsbDescriptorSet* set,
21 const GURL& origin, 27 const GURL& origin,
22 const uint8_t* configuration_value, 28 const uint8_t* configuration_value,
23 const uint8_t* interface_number) { 29 const uint8_t* interface_number) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 61
56 bool EnableWebUsbOnAnyOrigin() { 62 bool EnableWebUsbOnAnyOrigin() {
57 return base::CommandLine::ForCurrentProcess()->HasSwitch( 63 return base::CommandLine::ForCurrentProcess()->HasSwitch(
58 switches::kEnableWebUsbOnAnyOrigin); 64 switches::kEnableWebUsbOnAnyOrigin);
59 } 65 }
60 66
61 } // namespace 67 } // namespace
62 68
63 // static 69 // static
64 void WebUSBPermissionProvider::Create( 70 void WebUSBPermissionProvider::Create(
65 content::RenderFrameHost* render_frame_host, 71 RenderFrameHost* render_frame_host,
66 mojo::InterfaceRequest<PermissionProvider> request) { 72 mojo::InterfaceRequest<PermissionProvider> request) {
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
68 DCHECK(render_frame_host); 74 DCHECK(render_frame_host);
69 75
70 // The created object is owned by its bindings. 76 // The created object is owned by its bindings.
71 new WebUSBPermissionProvider(render_frame_host, request.Pass()); 77 new WebUSBPermissionProvider(render_frame_host, request.Pass());
72 } 78 }
73 79
74 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} 80 WebUSBPermissionProvider::~WebUSBPermissionProvider() {}
75 81
76 WebUSBPermissionProvider::WebUSBPermissionProvider( 82 WebUSBPermissionProvider::WebUSBPermissionProvider(
77 content::RenderFrameHost* render_frame_host, 83 RenderFrameHost* render_frame_host,
78 mojo::InterfaceRequest<PermissionProvider> request) 84 mojo::InterfaceRequest<PermissionProvider> request)
79 : render_frame_host_(render_frame_host) { 85 : render_frame_host_(render_frame_host) {
80 bindings_.set_connection_error_handler(base::Bind( 86 bindings_.set_connection_error_handler(base::Bind(
81 &WebUSBPermissionProvider::OnConnectionError, base::Unretained(this))); 87 &WebUSBPermissionProvider::OnConnectionError, base::Unretained(this)));
82 bindings_.AddBinding(this, request.Pass()); 88 bindings_.AddBinding(this, request.Pass());
83 } 89 }
84 90
85 void WebUSBPermissionProvider::HasDevicePermission( 91 void WebUSBPermissionProvider::HasDevicePermission(
86 mojo::Array<device::usb::DeviceInfoPtr> requested_devices, 92 mojo::Array<device::usb::DeviceInfoPtr> requested_devices,
87 const HasDevicePermissionCallback& callback) { 93 const HasDevicePermissionCallback& callback) {
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
89 GURL origin = render_frame_host_->GetLastCommittedURL().GetOrigin(); 95 mojo::Array<mojo::String> allowed_guids(0);
96 WebContents* web_contents =
97 WebContents::FromRenderFrameHost(render_frame_host_);
98 if (!web_contents) {
99 callback.Run(allowed_guids.Pass());
100 return;
101 }
90 102
91 mojo::Array<mojo::String> allowed_guids(0); 103 GURL embedding_origin =
104 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin();
105 GURL requesting_origin =
106 render_frame_host_->GetLastCommittedURL().GetOrigin();
107 Profile* profile = Profile::FromBrowserContext(
108 render_frame_host_->GetProcess()->GetBrowserContext());
109 UsbChooserPermissionContext* permission_context =
110 UsbChooserPermissionContextFactory::GetForProfile(profile);
111
92 for (size_t i = 0; i < requested_devices.size(); ++i) { 112 for (size_t i = 0; i < requested_devices.size(); ++i) {
93 const device::usb::DeviceInfoPtr& device = requested_devices[i]; 113 const device::usb::DeviceInfoPtr& device = requested_devices[i];
94 if (FindOriginInDescriptorSet(device->webusb_allowed_origins.get(), origin, 114 if (FindOriginInDescriptorSet(device->webusb_allowed_origins.get(),
95 nullptr, nullptr) && 115 requesting_origin, nullptr, nullptr) &&
96 EnableWebUsbOnAnyOrigin()) 116 (EnableWebUsbOnAnyOrigin() ||
117 permission_context->HasDevicePermission(
118 requesting_origin, embedding_origin, device->guid)))
97 allowed_guids.push_back(device->guid); 119 allowed_guids.push_back(device->guid);
98 } 120 }
99 callback.Run(allowed_guids.Pass()); 121 callback.Run(allowed_guids.Pass());
100 } 122 }
101 123
102 void WebUSBPermissionProvider::HasConfigurationPermission( 124 void WebUSBPermissionProvider::HasConfigurationPermission(
103 uint8_t requested_configuration_value, 125 uint8_t requested_configuration_value,
104 device::usb::DeviceInfoPtr device, 126 device::usb::DeviceInfoPtr device,
105 const HasInterfacePermissionCallback& callback) { 127 const HasInterfacePermissionCallback& callback) {
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 17 matching lines...) Expand all
124 146
125 void WebUSBPermissionProvider::Bind( 147 void WebUSBPermissionProvider::Bind(
126 mojo::InterfaceRequest<device::usb::PermissionProvider> request) { 148 mojo::InterfaceRequest<device::usb::PermissionProvider> request) {
127 bindings_.AddBinding(this, request.Pass()); 149 bindings_.AddBinding(this, request.Pass());
128 } 150 }
129 151
130 void WebUSBPermissionProvider::OnConnectionError() { 152 void WebUSBPermissionProvider::OnConnectionError() {
131 if (bindings_.empty()) 153 if (bindings_.empty())
132 delete this; 154 delete this;
133 } 155 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698