Skip to content

Commit

Permalink
ArcUsbHostUiDelegate
Browse files Browse the repository at this point in the history
Add ArcUsbHostUiDelegate interface.

Bug: 776476
Bug: b:24572867
Change-Id: I0ff523c24ebc1786f1fee3e77bbd36ebed72831e
Reviewed-on: https://chromium-review.googlesource.com/903059
Commit-Queue: Long Cheng <lgcheng@google.com>
Reviewed-by: Luis Hector Chavez <lhchavez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539507}
  • Loading branch information
lgcheng authored and Commit Bot committed Feb 27, 2018
1 parent 3838654 commit 51465ff
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
1 change: 1 addition & 0 deletions components/arc/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static_library("arc") {
"timer/create_timer_request.h",
"usb/usb_host_bridge.cc",
"usb/usb_host_bridge.h",
"usb/usb_host_ui_delegate.h",
"volume_mounter/arc_volume_mounter_bridge.cc",
"volume_mounter/arc_volume_mounter_bridge.h",
]
Expand Down
49 changes: 45 additions & 4 deletions components/arc/usb/usb_host_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "components/arc/usb/usb_host_bridge.h"

#include <utility>

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
Expand All @@ -12,6 +14,7 @@
#include "components/arc/arc_bridge_service.h"
#include "components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "components/arc/arc_features.h"
#include "components/arc/usb/usb_host_ui_delegate.h"
#include "device/base/device_client.h"
#include "device/usb/mojo/type_converters.h"
#include "device/usb/usb_device_handle.h"
Expand Down Expand Up @@ -107,6 +110,10 @@ ArcUsbHostBridge::~ArcUsbHostBridge() {
arc_bridge_service_->usb_host()->SetHost(nullptr);
}

BrowserContextKeyedServiceFactory* ArcUsbHostBridge::GetFactory() {
return ArcUsbHostBridgeFactory::GetInstance();
}

void ArcUsbHostBridge::RequestPermission(const std::string& guid,
const std::string& package,
bool interactive,
Expand Down Expand Up @@ -202,6 +209,9 @@ void ArcUsbHostBridge::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) {

void ArcUsbHostBridge::OnDeviceRemoved(
scoped_refptr<device::UsbDevice> device) {
if (ui_delegate_)
ui_delegate_->DeviceRemoved(device.get()->guid());

mojom::UsbHostInstance* usb_host_instance = ARC_GET_INSTANCE_FOR_METHOD(
arc_bridge_service_->usb_host(), OnDeviceAdded);

Expand Down Expand Up @@ -229,6 +239,19 @@ void ArcUsbHostBridge::OnConnectionReady() {
weak_factory_.GetWeakPtr())));
}

void ArcUsbHostBridge::OnConnectionClosed() {
if (ui_delegate_)
ui_delegate_->ClearPermissionRequests();
}

void ArcUsbHostBridge::Shutdown() {
ui_delegate_ = nullptr;
}

void ArcUsbHostBridge::SetUiDelegate(ArcUsbHostUiDelegate* ui_delegate) {
ui_delegate_ = ui_delegate;
}

void ArcUsbHostBridge::OnDeviceChecked(const std::string& guid, bool allowed) {
if (!base::FeatureList::IsEnabled(arc::kUsbHostFeature)) {
VLOG(1) << "AndroidUSBHost: feature is disabled; ignoring";
Expand All @@ -251,13 +274,31 @@ void ArcUsbHostBridge::DoRequestUserAuthorization(
const std::string& guid,
const std::string& package,
RequestPermissionCallback callback) {
// TODO: implement the UI dialog
// fail close for now
std::move(callback).Run(false);
if (!ui_delegate_) {
std::move(callback).Run(false);
return;
}

if (!usb_service_) {
std::move(callback).Run(false);
return;
}

scoped_refptr<device::UsbDevice> device = usb_service_->GetDevice(guid);
if (!device.get()) {
LOG(WARNING) << "Unknown USB device " << guid;
std::move(callback).Run(false);
return;
}

ui_delegate_->RequestUsbAccessPermission(
package, guid, device->serial_number(), device->manufacturer_string(),
device->product_string(), device->vendor_id(), device->product_id(),
std::move(callback));
}

bool ArcUsbHostBridge::HasPermissionForDevice(const std::string& guid) {
// TODO: implement permission settings
// TODO(lgcheng): implement permission settings
// fail close for now
return false;
}
Expand Down
14 changes: 13 additions & 1 deletion components/arc/usb/usb_host_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <vector>

#include "base/callback_forward.h"
#include "base/files/scoped_file.h"
#include "base/macros.h"
#include "base/scoped_observer.h"
#include "components/arc/common/usb_host.mojom.h"
Expand All @@ -23,9 +22,12 @@ namespace content {
class BrowserContext;
} // namespace content

class BrowserContextKeyedServiceFactory;

namespace arc {

class ArcBridgeService;
class ArcUsbHostUiDelegate;

// Private implementation of UsbHostHost.
class ArcUsbHostBridge : public KeyedService,
Expand All @@ -43,6 +45,9 @@ class ArcUsbHostBridge : public KeyedService,
ArcBridgeService* bridge_service);
~ArcUsbHostBridge() override;

// Returns the factory instance for this class.
static BrowserContextKeyedServiceFactory* GetFactory();

// mojom::UsbHostHost overrides:
void RequestPermission(const std::string& guid,
const std::string& package,
Expand All @@ -60,6 +65,12 @@ class ArcUsbHostBridge : public KeyedService,

// ConnectionObserver<mojom::UsbHostInstance> overrides:
void OnConnectionReady() override;
void OnConnectionClosed() override;

// KeyedService overrides:
void Shutdown() override;

void SetUiDelegate(ArcUsbHostUiDelegate* ui_delegate);

private:
void OnDeviceChecked(const std::string& guid, bool allowed);
Expand All @@ -73,6 +84,7 @@ class ArcUsbHostBridge : public KeyedService,
ScopedObserver<device::UsbService, device::UsbService::Observer>
usb_observer_;
device::UsbService* usb_service_;
ArcUsbHostUiDelegate* ui_delegate_ = nullptr;

// WeakPtrFactory to use for callbacks.
base::WeakPtrFactory<ArcUsbHostBridge> weak_factory_;
Expand Down
58 changes: 58 additions & 0 deletions components/arc/usb/usb_host_ui_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2018 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.

#ifndef COMPONENTS_ARC_USB_USB_HOST_UI_DELEGATE_H_
#define COMPONENTS_ARC_USB_USB_HOST_UI_DELEGATE_H_

#include <string>

#include "base/callback.h"
#include "base/strings/string16.h"

namespace arc {

class ArcUsbHostUiDelegate {
public:
using RequestPermissionCallback = base::OnceCallback<void(bool)>;
// Requests scan device list permission when app tries to get USB device list.
// Since the calling application will block on the callback being resolved, it
// should be done as soon as possible to prevent the caller from becoming
// non-responsive.
virtual void RequestUsbScanDeviceListPermission(
const std::string& package_name,
RequestPermissionCallback callback) = 0;

// Requests USB device access permission.
virtual void RequestUsbAccessPermission(
const std::string& package_name,
const std::string& guid,
const base::string16& serial_number,
const base::string16& manufacturer_string,
const base::string16& product_string,
uint16_t vendor_id,
uint16_t product_id,
RequestPermissionCallback callback) = 0;

// Checks if package have access to USB device.
virtual bool HasUsbAccessPermission(const std::string& package_name,
const std::string& guid,
const base::string16& serial_number,
uint16_t vendor_id,
uint16_t product_id) = 0;

// Device is detached. Remove pending permission request to the device and
// ephemeral device permission if the device is not persistent.
virtual void DeviceRemoved(const std::string& guid) = 0;

// Clears all pending permission requests. Called when USB host instance
// connection is closed.
virtual void ClearPermissionRequests() = 0;

protected:
~ArcUsbHostUiDelegate() = default;
};

} // namespace arc

#endif // COMPONENTS_ARC_USB_USB_HOST_UI_DELEGATE_H_

0 comments on commit 51465ff

Please sign in to comment.