forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this CL: 1) Add the HidManager and HidConnection interfaces in hid.mojom, and implements those mojo interfaces in //device/hid. 2) Convert the clients in //extensions to use hid mojo interfaces. 3) Change the type of HidUsageAndPage::usage_page to uint16_t. 4) Rewrite the hid_apitest base on the new added mojo interfaces. TODO: 1) Move client library files into device/hid/public/cpp. 2) Host HidService by DeviceService instead of DeviceClient. 3) Mojofy //device/u2f Or just move it into DeviceService. BUG=728223 Change-Id: I1de1b5211f7ef37f54d4bc18c7995a3b47cc7da2 Reviewed-on: https://chromium-review.googlesource.com/648949 Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Reilly Grant <reillyg@chromium.org> Commit-Queue: Ke He <ke.he@intel.com> Cr-Commit-Position: refs/heads/master@{#502109}
- Loading branch information
Ke He
authored and
Commit Bot
committed
Sep 15, 2017
1 parent
c359292
commit d2eea93
Showing
27 changed files
with
733 additions
and
470 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2017 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 "device/hid/hid_connection_impl.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/memory/ptr_util.h" | ||
|
||
namespace device { | ||
|
||
HidConnectionImpl::HidConnectionImpl( | ||
scoped_refptr<device::HidConnection> connection) | ||
: hid_connection_(std::move(connection)), weak_factory_(this) {} | ||
|
||
HidConnectionImpl::~HidConnectionImpl() { | ||
DCHECK(hid_connection_); | ||
|
||
// Close |hid_connection_| on destruction because this class is owned by a | ||
// mojo::StrongBinding and will be destroyed when the pipe is closed. | ||
hid_connection_->Close(); | ||
} | ||
|
||
void HidConnectionImpl::Read(ReadCallback callback) { | ||
DCHECK(hid_connection_); | ||
hid_connection_->Read(base::BindOnce(&HidConnectionImpl::OnRead, | ||
weak_factory_.GetWeakPtr(), | ||
std::move(callback))); | ||
} | ||
|
||
void HidConnectionImpl::OnRead(ReadCallback callback, | ||
bool success, | ||
scoped_refptr<net::IOBuffer> buffer, | ||
size_t size) { | ||
if (!success) { | ||
std::move(callback).Run(false, 0, base::nullopt); | ||
return; | ||
} | ||
DCHECK(buffer); | ||
|
||
std::vector<uint8_t> data(buffer->data() + 1, buffer->data() + size); | ||
std::move(callback).Run(true, buffer->data()[0], data); | ||
} | ||
|
||
void HidConnectionImpl::Write(uint8_t report_id, | ||
const std::vector<uint8_t>& buffer, | ||
WriteCallback callback) { | ||
DCHECK(hid_connection_); | ||
|
||
auto io_buffer = | ||
base::MakeRefCounted<net::IOBufferWithSize>(buffer.size() + 1); | ||
io_buffer->data()[0] = report_id; | ||
|
||
const char* data = reinterpret_cast<const char*>(buffer.data()); | ||
memcpy(io_buffer->data() + 1, data, buffer.size()); | ||
|
||
hid_connection_->Write( | ||
io_buffer, io_buffer->size(), | ||
base::BindOnce(&HidConnectionImpl::OnWrite, weak_factory_.GetWeakPtr(), | ||
std::move(callback))); | ||
} | ||
|
||
void HidConnectionImpl::OnWrite(WriteCallback callback, bool success) { | ||
std::move(callback).Run(success); | ||
} | ||
|
||
void HidConnectionImpl::GetFeatureReport(uint8_t report_id, | ||
GetFeatureReportCallback callback) { | ||
DCHECK(hid_connection_); | ||
hid_connection_->GetFeatureReport( | ||
report_id, | ||
base::BindOnce(&HidConnectionImpl::OnGetFeatureReport, | ||
weak_factory_.GetWeakPtr(), std::move(callback))); | ||
} | ||
|
||
void HidConnectionImpl::OnGetFeatureReport(GetFeatureReportCallback callback, | ||
bool success, | ||
scoped_refptr<net::IOBuffer> buffer, | ||
size_t size) { | ||
if (!success) { | ||
std::move(callback).Run(false, base::nullopt); | ||
return; | ||
} | ||
DCHECK(buffer); | ||
|
||
std::vector<uint8_t> data(buffer->data(), buffer->data() + size); | ||
std::move(callback).Run(true, data); | ||
} | ||
|
||
void HidConnectionImpl::SendFeatureReport(uint8_t report_id, | ||
const std::vector<uint8_t>& buffer, | ||
SendFeatureReportCallback callback) { | ||
DCHECK(hid_connection_); | ||
|
||
auto io_buffer = | ||
base::MakeRefCounted<net::IOBufferWithSize>(buffer.size() + 1); | ||
io_buffer->data()[0] = report_id; | ||
|
||
const char* data = reinterpret_cast<const char*>(buffer.data()); | ||
memcpy(io_buffer->data() + 1, data, buffer.size()); | ||
|
||
hid_connection_->SendFeatureReport( | ||
io_buffer, io_buffer->size(), | ||
base::BindOnce(&HidConnectionImpl::OnSendFeatureReport, | ||
weak_factory_.GetWeakPtr(), std::move(callback))); | ||
} | ||
|
||
void HidConnectionImpl::OnSendFeatureReport(SendFeatureReportCallback callback, | ||
bool success) { | ||
std::move(callback).Run(success); | ||
} | ||
|
||
} // namespace device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2017 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 DEVICE_HID_HID_CONNECTION_IMPL_H_ | ||
#define DEVICE_HID_HID_CONNECTION_IMPL_H_ | ||
|
||
#include "base/memory/ref_counted.h" | ||
#include "device/hid/hid_connection.h" | ||
#include "device/hid/public/interfaces/hid.mojom.h" | ||
#include "net/base/io_buffer.h" | ||
|
||
namespace device { | ||
|
||
// HidConnectionImpl is reponsible for handling mojo communications from | ||
// clients. It delegates to HidConnection the real work of creating | ||
// connections in different platforms. | ||
class HidConnectionImpl : public device::mojom::HidConnection { | ||
public: | ||
explicit HidConnectionImpl(scoped_refptr<device::HidConnection> connection); | ||
~HidConnectionImpl() final; | ||
|
||
// device::mojom::HidConnection implementation: | ||
void Read(ReadCallback callback) override; | ||
void Write(uint8_t report_id, | ||
const std::vector<uint8_t>& buffer, | ||
WriteCallback callback) override; | ||
void GetFeatureReport(uint8_t report_id, | ||
GetFeatureReportCallback callback) override; | ||
void SendFeatureReport(uint8_t report_id, | ||
const std::vector<uint8_t>& buffer, | ||
SendFeatureReportCallback callback) override; | ||
|
||
private: | ||
void OnRead(ReadCallback callback, | ||
bool success, | ||
scoped_refptr<net::IOBuffer> buffer, | ||
size_t size); | ||
void OnWrite(WriteCallback callback, bool success); | ||
void OnGetFeatureReport(GetFeatureReportCallback callback, | ||
bool success, | ||
scoped_refptr<net::IOBuffer> buffer, | ||
size_t size); | ||
void OnSendFeatureReport(SendFeatureReportCallback callback, bool success); | ||
|
||
scoped_refptr<device::HidConnection> hid_connection_; | ||
base::WeakPtrFactory<HidConnectionImpl> weak_factory_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(HidConnectionImpl); | ||
}; | ||
|
||
} // namespace device | ||
|
||
#endif // DEVICE_HID_HID_CONNECTION_IMPL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2017 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 "device/hid/hid_manager_impl.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/callback_helpers.h" | ||
#include "base/memory/ptr_util.h" | ||
#include "base/stl_util.h" | ||
#include "device/base/device_client.h" | ||
#include "device/hid/hid_connection_impl.h" | ||
#include "mojo/public/cpp/bindings/strong_binding.h" | ||
|
||
namespace device { | ||
|
||
HidManagerImpl::HidManagerImpl() | ||
// TODO(ke.he@intel.com): Temporarily we still keep the HidService being | ||
// owned and hosted in ChromeDeviceClient. The device service is shutdown | ||
// earlier than ChromeDeviceClient, it is safe to hold a raw pointer of | ||
// HidService here. After //device/u2f be mojofied or be moved into device | ||
// service, we will remove HidService from the ChromeDeviceClient and let | ||
// HidManagerImpl to own the HidService. | ||
: hid_service_(DeviceClient::Get()->GetHidService()), | ||
hid_service_observer_(this), | ||
weak_factory_(this) { | ||
DCHECK(hid_service_); | ||
hid_service_observer_.Add(hid_service_); | ||
} | ||
|
||
HidManagerImpl::~HidManagerImpl() {} | ||
|
||
void HidManagerImpl::AddBinding(device::mojom::HidManagerRequest request) { | ||
bindings_.AddBinding(this, std::move(request)); | ||
} | ||
|
||
void HidManagerImpl::GetDevicesAndSetClient( | ||
device::mojom::HidManagerClientAssociatedPtrInfo client, | ||
GetDevicesCallback callback) { | ||
hid_service_->GetDevices(AdaptCallbackForRepeating(base::BindOnce( | ||
&HidManagerImpl::CreateDeviceList, weak_factory_.GetWeakPtr(), | ||
std::move(callback), std::move(client)))); | ||
} | ||
|
||
void HidManagerImpl::GetDevices(GetDevicesCallback callback) { | ||
hid_service_->GetDevices(AdaptCallbackForRepeating(base::BindOnce( | ||
&HidManagerImpl::CreateDeviceList, weak_factory_.GetWeakPtr(), | ||
std::move(callback), nullptr))); | ||
} | ||
|
||
void HidManagerImpl::CreateDeviceList( | ||
GetDevicesCallback callback, | ||
device::mojom::HidManagerClientAssociatedPtrInfo client, | ||
std::vector<device::mojom::HidDeviceInfoPtr> devices) { | ||
std::move(callback).Run(std::move(devices)); | ||
|
||
if (!client.is_valid()) | ||
return; | ||
|
||
device::mojom::HidManagerClientAssociatedPtr client_ptr; | ||
client_ptr.Bind(std::move(client)); | ||
clients_.AddPtr(std::move(client_ptr)); | ||
} | ||
|
||
void HidManagerImpl::Connect(const std::string& device_guid, | ||
ConnectCallback callback) { | ||
hid_service_->Connect(device_guid, | ||
AdaptCallbackForRepeating(base::BindOnce( | ||
&HidManagerImpl::CreateConnection, | ||
weak_factory_.GetWeakPtr(), std::move(callback)))); | ||
} | ||
|
||
void HidManagerImpl::CreateConnection( | ||
ConnectCallback callback, | ||
scoped_refptr<device::HidConnection> connection) { | ||
if (!connection) { | ||
std::move(callback).Run(nullptr); | ||
return; | ||
} | ||
|
||
device::mojom::HidConnectionPtr client; | ||
mojo::MakeStrongBinding(base::MakeUnique<HidConnectionImpl>(connection), | ||
mojo::MakeRequest(&client)); | ||
std::move(callback).Run(std::move(client)); | ||
} | ||
|
||
void HidManagerImpl::OnDeviceAdded(device::mojom::HidDeviceInfoPtr device) { | ||
device::mojom::HidDeviceInfo* device_info = device.get(); | ||
clients_.ForAllPtrs([device_info](device::mojom::HidManagerClient* client) { | ||
client->DeviceAdded(device_info->Clone()); | ||
}); | ||
} | ||
|
||
void HidManagerImpl::OnDeviceRemoved(device::mojom::HidDeviceInfoPtr device) { | ||
device::mojom::HidDeviceInfo* device_info = device.get(); | ||
clients_.ForAllPtrs([device_info](device::mojom::HidManagerClient* client) { | ||
client->DeviceRemoved(device_info->Clone()); | ||
}); | ||
} | ||
|
||
} // namespace device |
Oops, something went wrong.