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

Side by Side Diff: third_party/WebKit/Source/modules/webusb/USB.cpp

Issue 2813903007: Hide USB service unavailability from web content (Closed)
Patch Set: Keep "No device selected." error text Created 3 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "modules/webusb/USB.h" 5 #include "modules/webusb/USB.h"
6 6
7 #include "bindings/core/v8/ScriptPromise.h" 7 #include "bindings/core/v8/ScriptPromise.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "core/dom/DOMException.h" 9 #include "core/dom/DOMException.h"
10 #include "core/dom/Document.h" 10 #include "core/dom/Document.h"
11 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "device/usb/public/interfaces/device.mojom-blink.h" 12 #include "device/usb/public/interfaces/device.mojom-blink.h"
13 #include "modules/EventTargetModules.h" 13 #include "modules/EventTargetModules.h"
14 #include "modules/webusb/USBConnectionEvent.h" 14 #include "modules/webusb/USBConnectionEvent.h"
15 #include "modules/webusb/USBDevice.h" 15 #include "modules/webusb/USBDevice.h"
16 #include "modules/webusb/USBDeviceFilter.h" 16 #include "modules/webusb/USBDeviceFilter.h"
17 #include "modules/webusb/USBDeviceRequestOptions.h" 17 #include "modules/webusb/USBDeviceRequestOptions.h"
18 #include "platform/UserGestureIndicator.h" 18 #include "platform/UserGestureIndicator.h"
19 #include "platform/mojo/MojoHelper.h" 19 #include "platform/mojo/MojoHelper.h"
20 #include "platform/wtf/Functional.h" 20 #include "platform/wtf/Functional.h"
21 #include "public/platform/InterfaceProvider.h" 21 #include "public/platform/InterfaceProvider.h"
22 #include "public/platform/Platform.h" 22 #include "public/platform/Platform.h"
23 23
24 namespace usb = device::usb::blink; 24 namespace usb = device::usb::blink;
25 25
26 namespace blink { 26 namespace blink {
27 namespace { 27 namespace {
28 28
29 const char kNoServiceError[] = "USB service unavailable."; 29 const char kNoDeviceSelected[] = "No device selected.";
30 30
31 usb::DeviceFilterPtr ConvertDeviceFilter(const USBDeviceFilter& filter) { 31 usb::DeviceFilterPtr ConvertDeviceFilter(const USBDeviceFilter& filter) {
32 auto mojo_filter = usb::DeviceFilter::New(); 32 auto mojo_filter = usb::DeviceFilter::New();
33 mojo_filter->has_vendor_id = filter.hasVendorId(); 33 mojo_filter->has_vendor_id = filter.hasVendorId();
34 if (mojo_filter->has_vendor_id) 34 if (mojo_filter->has_vendor_id)
35 mojo_filter->vendor_id = filter.vendorId(); 35 mojo_filter->vendor_id = filter.vendorId();
36 mojo_filter->has_product_id = filter.hasProductId(); 36 mojo_filter->has_product_id = filter.hasProductId();
37 if (mojo_filter->has_product_id) 37 if (mojo_filter->has_product_id)
38 mojo_filter->product_id = filter.productId(); 38 mojo_filter->product_id = filter.productId();
39 mojo_filter->has_class_code = filter.hasClassCode(); 39 mojo_filter->has_class_code = filter.hasClassCode();
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 } 165 }
166 166
167 void USB::OnGetPermission(ScriptPromiseResolver* resolver, 167 void USB::OnGetPermission(ScriptPromiseResolver* resolver,
168 usb::DeviceInfoPtr device_info) { 168 usb::DeviceInfoPtr device_info) {
169 auto request_entry = chooser_service_requests_.Find(resolver); 169 auto request_entry = chooser_service_requests_.Find(resolver);
170 if (request_entry == chooser_service_requests_.end()) 170 if (request_entry == chooser_service_requests_.end())
171 return; 171 return;
172 chooser_service_requests_.erase(request_entry); 172 chooser_service_requests_.erase(request_entry);
173 173
174 EnsureDeviceManagerConnection(); 174 EnsureDeviceManagerConnection();
175 if (!device_manager_) {
176 resolver->Reject(DOMException::Create(kNotFoundError, kNoServiceError));
177 return;
178 }
179 175
180 if (device_info) { 176 if (device_manager_ && device_info) {
181 resolver->Resolve(GetOrCreateDevice(std::move(device_info))); 177 resolver->Resolve(GetOrCreateDevice(std::move(device_info)));
182 } else { 178 } else {
183 resolver->Reject( 179 resolver->Reject(DOMException::Create(kNotFoundError, kNoDeviceSelected));
184 DOMException::Create(kNotFoundError, "No device selected."));
185 } 180 }
186 } 181 }
187 182
188 void USB::OnDeviceAdded(usb::DeviceInfoPtr device_info) { 183 void USB::OnDeviceAdded(usb::DeviceInfoPtr device_info) {
189 if (!device_manager_) 184 if (!device_manager_)
190 return; 185 return;
191 186
192 DispatchEvent(USBConnectionEvent::Create( 187 DispatchEvent(USBConnectionEvent::Create(
193 EventTypeNames::connect, GetOrCreateDevice(std::move(device_info)))); 188 EventTypeNames::connect, GetOrCreateDevice(std::move(device_info))));
194 } 189 }
195 190
196 void USB::OnDeviceRemoved(usb::DeviceInfoPtr device_info) { 191 void USB::OnDeviceRemoved(usb::DeviceInfoPtr device_info) {
197 String guid = device_info->guid; 192 String guid = device_info->guid;
198 USBDevice* device = device_cache_.at(guid); 193 USBDevice* device = device_cache_.at(guid);
199 if (!device) { 194 if (!device) {
200 device = USBDevice::Create(std::move(device_info), nullptr, 195 device = USBDevice::Create(std::move(device_info), nullptr,
201 GetExecutionContext()); 196 GetExecutionContext());
202 } 197 }
203 DispatchEvent(USBConnectionEvent::Create(EventTypeNames::disconnect, device)); 198 DispatchEvent(USBConnectionEvent::Create(EventTypeNames::disconnect, device));
204 device_cache_.erase(guid); 199 device_cache_.erase(guid);
205 } 200 }
206 201
207 void USB::OnDeviceManagerConnectionError() { 202 void USB::OnDeviceManagerConnectionError() {
208 device_manager_.reset(); 203 device_manager_.reset();
209 client_binding_.Close(); 204 client_binding_.Close();
210 for (ScriptPromiseResolver* resolver : device_manager_requests_) 205 for (ScriptPromiseResolver* resolver : device_manager_requests_)
211 resolver->Reject(DOMException::Create(kNotFoundError, kNoServiceError)); 206 resolver->Resolve(HeapVector<Member<USBDevice>>(0));
212 device_manager_requests_.Clear(); 207 device_manager_requests_.Clear();
213 } 208 }
214 209
215 void USB::OnChooserServiceConnectionError() { 210 void USB::OnChooserServiceConnectionError() {
216 chooser_service_.reset(); 211 chooser_service_.reset();
217 for (ScriptPromiseResolver* resolver : chooser_service_requests_) 212 for (ScriptPromiseResolver* resolver : chooser_service_requests_)
218 resolver->Reject(DOMException::Create(kNotFoundError, kNoServiceError)); 213 resolver->Reject(DOMException::Create(kNotFoundError, kNoDeviceSelected));
219 chooser_service_requests_.Clear(); 214 chooser_service_requests_.Clear();
220 } 215 }
221 216
222 void USB::AddedEventListener(const AtomicString& event_type, 217 void USB::AddedEventListener(const AtomicString& event_type,
223 RegisteredEventListener& listener) { 218 RegisteredEventListener& listener) {
224 EventTargetWithInlineData::AddedEventListener(event_type, listener); 219 EventTargetWithInlineData::AddedEventListener(event_type, listener);
225 if (event_type == EventTypeNames::connect || 220 if (event_type == EventTypeNames::connect ||
226 event_type == EventTypeNames::disconnect) { 221 event_type == EventTypeNames::disconnect) {
227 EnsureDeviceManagerConnection(); 222 EnsureDeviceManagerConnection();
228 } 223 }
(...skipping 14 matching lines...) Expand all
243 238
244 DEFINE_TRACE(USB) { 239 DEFINE_TRACE(USB) {
245 visitor->Trace(device_manager_requests_); 240 visitor->Trace(device_manager_requests_);
246 visitor->Trace(chooser_service_requests_); 241 visitor->Trace(chooser_service_requests_);
247 visitor->Trace(device_cache_); 242 visitor->Trace(device_cache_);
248 EventTargetWithInlineData::Trace(visitor); 243 EventTargetWithInlineData::Trace(visitor);
249 ContextLifecycleObserver::Trace(visitor); 244 ContextLifecycleObserver::Trace(visitor);
250 } 245 }
251 246
252 } // namespace blink 247 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698