Commit 1b26f574 authored by Andy Paicu's avatar Andy Paicu Committed by Commit Bot

[hid] Replicate self-destruct logic in connection fake

HidConnectionImpl is self-owned and will self-destruct on
disconnection of the HidConnection or HidConnectionWatcher.
This CL modifies the FakeHidConnection used in tests so it
behaves the same way.

BUG=1126689

Change-Id: I8bbdcca7c77699749c6022f01b8a456c4891cdb8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2441151
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Auto-Submit: Matt Reynolds <mattreynolds@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812858}
parent cef2ddd6
......@@ -370,17 +370,18 @@ TEST_F(HidServiceTest, RevokeDevicePermission) {
EXPECT_TRUE(contents()->IsConnectedToHidDevice());
EXPECT_TRUE(connection);
base::RunLoop disconnect_loop;
connection.set_disconnect_handler(
base::BindLambdaForTesting([&] { disconnect_loop.Quit(); }));
// Simulate user revoking permission.
EXPECT_CALL(hid_delegate(), HasDevicePermission).WillOnce(Return(false));
url::Origin origin = url::Origin::Create(GURL(kTestUrl));
hid_delegate().OnPermissionRevoked(origin, origin);
// TODO(mattreynolds): Use a disconnect handler with a run loop instead of the
// potentially flaky `RunUntilIdle`. This depends on fixing
// `FakeHidConnection` to monitor the watcher just as `HidConnectionImpl`
// does.
base::RunLoop().RunUntilIdle();
disconnect_loop.Run();
EXPECT_FALSE(contents()->IsConnectedToHidDevice());
EXPECT_FALSE(connection.is_connected());
}
} // namespace content
......@@ -13,8 +13,23 @@
namespace device {
FakeHidConnection::FakeHidConnection(mojom::HidDeviceInfoPtr device)
: device_(std::move(device)) {}
FakeHidConnection::FakeHidConnection(
mojom::HidDeviceInfoPtr device,
mojo::PendingReceiver<mojom::HidConnection> receiver,
mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
mojo::PendingRemote<mojom::HidConnectionWatcher> watcher)
: receiver_(this, std::move(receiver)),
device_(std::move(device)),
watcher_(std::move(watcher)) {
receiver_.set_disconnect_handler(base::BindOnce(
[](FakeHidConnection* self) { delete self; }, base::Unretained(this)));
if (watcher_) {
watcher_.set_disconnect_handler(base::BindOnce(
[](FakeHidConnection* self) { delete self; }, base::Unretained(this)));
}
if (connection_client)
client_.Bind(std::move(connection_client));
}
FakeHidConnection::~FakeHidConnection() = default;
......@@ -135,9 +150,10 @@ void FakeHidManager::Connect(
}
mojo::PendingRemote<mojom::HidConnection> connection;
mojo::MakeSelfOwnedReceiver(
std::make_unique<FakeHidConnection>(devices_[device_guid]->Clone()),
connection.InitWithNewPipeAndPassReceiver());
// FakeHidConnection is self-owned.
new FakeHidConnection(devices_[device_guid]->Clone(),
connection.InitWithNewPipeAndPassReceiver(),
std::move(connection_client), std::move(watcher));
std::move(callback).Run(std::move(connection));
}
......
......@@ -19,7 +19,11 @@ namespace device {
class FakeHidConnection : public mojom::HidConnection {
public:
explicit FakeHidConnection(mojom::HidDeviceInfoPtr device);
FakeHidConnection(
mojom::HidDeviceInfoPtr device,
mojo::PendingReceiver<mojom::HidConnection> receiver,
mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
mojo::PendingRemote<mojom::HidConnectionWatcher> watcher);
FakeHidConnection(FakeHidConnection&) = delete;
FakeHidConnection& operator=(FakeHidConnection&) = delete;
~FakeHidConnection() override;
......@@ -37,7 +41,10 @@ class FakeHidConnection : public mojom::HidConnection {
SendFeatureReportCallback callback) override;
private:
mojo::Receiver<mojom::HidConnection> receiver_;
mojom::HidDeviceInfoPtr device_;
mojo::Remote<mojom::HidConnectionClient> client_;
mojo::Remote<mojom::HidConnectionWatcher> watcher_;
};
class FakeHidManager : public mojom::HidManager {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment