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

Unified Diff: content/renderer/usb/web_usb_client_impl.cc

Issue 1293253002: Connect WebUSB client interface to the devices app (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix memory leak in WeakBindingSet Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/usb/web_usb_client_impl.h ('k') | content/renderer/usb/web_usb_device_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/usb/web_usb_client_impl.cc
diff --git a/content/renderer/usb/web_usb_client_impl.cc b/content/renderer/usb/web_usb_client_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..84062d9f866ef6527d919c33dcfb2ae509a595eb
--- /dev/null
+++ b/content/renderer/usb/web_usb_client_impl.cc
@@ -0,0 +1,102 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/usb/web_usb_client_impl.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/move.h"
+#include "base/strings/utf_string_conversions.h"
+#include "content/child/scoped_web_callbacks.h"
+#include "content/renderer/usb/type_converters.h"
+#include "content/renderer/usb/web_usb_device_impl.h"
+#include "device/devices_app/public/cpp/constants.h"
+#include "mojo/application/public/cpp/connect.h"
+#include "mojo/application/public/interfaces/shell.mojom.h"
+#include "third_party/WebKit/public/platform/WebCallbacks.h"
+#include "third_party/WebKit/public/platform/WebPassOwnPtr.h"
+#include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceFilter.h"
+#include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h"
+#include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceRequestOptions.h"
+#include "third_party/WebKit/public/platform/modules/webusb/WebUSBError.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
+
+namespace content {
+
+namespace {
+
+const char kNoServiceError[] = "USB service unavailable.";
+
+// Generic default rejection handler for any WebUSB callbacks type. Assumes
+// |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&>
+// for any type |T|.
+template <typename CallbacksType>
+void RejectCallbacksWithError(const blink::WebUSBError& error,
+ scoped_ptr<CallbacksType> callbacks) {
+ callbacks->onError(error);
+}
+
+// Create a new ScopedWebCallbacks for WebUSB client callbacks, defaulting to
+// a "no service" rejection.
+template <typename CallbacksType>
+ScopedWebCallbacks<CallbacksType> MakeScopedUSBCallbacks(
+ CallbacksType* callbacks) {
+ return make_scoped_web_callbacks(
+ callbacks,
+ base::Bind(&RejectCallbacksWithError<CallbacksType>,
+ blink::WebUSBError(blink::WebUSBError::Error::Service,
+ base::UTF8ToUTF16(kNoServiceError))));
+}
+
+void OnGetDevicesComplete(
+ ScopedWebCallbacks<blink::WebUSBClientGetDevicesCallbacks> scoped_callbacks,
+ mojo::ServiceProvider* device_services,
+ mojo::Array<device::usb::DeviceInfoPtr> results) {
+ blink::WebVector<blink::WebUSBDevice*>* devices =
+ new blink::WebVector<blink::WebUSBDevice*>(results.size());
+ for (size_t i = 0; i < results.size(); ++i) {
+ device::usb::DeviceManagerPtr device_manager;
+ mojo::ConnectToService(device_services, &device_manager);
+ (*devices)[i] = new WebUSBDeviceImpl(
+ device_manager.Pass(),
+ mojo::ConvertTo<blink::WebUSBDeviceInfo>(results[i]));
+ }
+ scoped_callbacks.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices));
+}
+
+} // namespace
+
+WebUSBClientImpl::WebUSBClientImpl(mojo::ServiceProviderPtr device_services)
+ : device_services_(device_services.Pass()) {
+ mojo::ConnectToService(device_services_.get(), &device_manager_);
+}
+
+WebUSBClientImpl::~WebUSBClientImpl() {}
+
+void WebUSBClientImpl::getDevices(
+ blink::WebUSBClientGetDevicesCallbacks* callbacks) {
+ auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks);
+ // TODO(rockot): Remove this once DeviceManager is updated. It should no
+ // longer take enumeration options.
+ device::usb::EnumerationOptionsPtr options =
+ device::usb::EnumerationOptions::New();
+ options->filters = mojo::Array<device::usb::DeviceFilterPtr>::New(0);
+ device_manager_->GetDevices(
+ options.Pass(),
+ base::Bind(&OnGetDevicesComplete, base::Passed(&scoped_callbacks),
+ base::Unretained(device_services_.get())));
+}
+
+void WebUSBClientImpl::requestDevice(
+ const blink::WebUSBDeviceRequestOptions& options,
+ blink::WebUSBClientRequestDeviceCallbacks* callbacks) {
+ callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service,
+ base::UTF8ToUTF16("Not implemented.")));
+ delete callbacks;
+}
+
+} // namespace content
« no previous file with comments | « content/renderer/usb/web_usb_client_impl.h ('k') | content/renderer/usb/web_usb_device_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698