Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d2588273 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use unique_ptr when creating InputChannel

InputChannel api will return unique_ptr, and it is up to the caller to
decide how to use that.

In most cases, the caller will proceed with making is a shared_ptr.

Bug: 142581626
Test: atest libinput_tests inputflinger_tests
Change-Id: Ice473a4c85241c63e3f4d6cdd30f8258ed7485e7
parent ce5ab080
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -193,7 +193,7 @@ struct InputMessage {
 */
class InputChannel : public Parcelable {
public:
    static std::shared_ptr<InputChannel> create(const std::string& name,
    static std::unique_ptr<InputChannel> create(const std::string& name,
                                                android::base::unique_fd fd, sp<IBinder> token);
    InputChannel() = default;
    InputChannel(const InputChannel& other)
@@ -208,8 +208,8 @@ public:
     * Return OK on success.
     */
    static status_t openInputChannelPair(const std::string& name,
                                         std::shared_ptr<InputChannel>& outServerChannel,
                                         std::shared_ptr<InputChannel>& outClientChannel);
                                         std::unique_ptr<InputChannel>& outServerChannel,
                                         std::unique_ptr<InputChannel>& outClientChannel);

    inline std::string getName() const { return mName; }
    inline const android::base::unique_fd& getFd() const { return mFd; }
@@ -241,7 +241,7 @@ public:
    status_t receiveMessage(InputMessage* msg);

    /* Return a new object that has a duplicate of this channel's fd. */
    std::shared_ptr<InputChannel> dup() const;
    std::unique_ptr<InputChannel> dup() const;

    status_t readFromParcel(const android::Parcel* parcel) override;
    status_t writeToParcel(android::Parcel* parcel) const override;
+4 −3
Original line number Diff line number Diff line
@@ -68,8 +68,9 @@ class InputSurface {
public:
    InputSurface(const sp<SurfaceControl> &sc, int width, int height) {
        mSurfaceControl = sc;

        InputChannel::openInputChannelPair("testchannels", mServerChannel, mClientChannel);
        std::unique_ptr<InputChannel> clientChannel;
        InputChannel::openInputChannelPair("testchannels", mServerChannel, clientChannel);
        mClientChannel = std::move(clientChannel);

        mInputFlinger = getInputFlinger();
        mInputFlinger->registerInputChannel(*mServerChannel);
@@ -211,7 +212,7 @@ private:
    }
public:
    sp<SurfaceControl> mSurfaceControl;
    std::shared_ptr<InputChannel> mServerChannel;
    std::unique_ptr<InputChannel> mServerChannel;
    std::shared_ptr<InputChannel> mClientChannel;
    sp<IInputFlinger> mInputFlinger;

+5 −5
Original line number Diff line number Diff line
@@ -243,7 +243,7 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {

// --- InputChannel ---

std::shared_ptr<InputChannel> InputChannel::create(const std::string& name,
std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
                                                   android::base::unique_fd fd, sp<IBinder> token) {
    const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
    if (result != 0) {
@@ -252,7 +252,7 @@ std::shared_ptr<InputChannel> InputChannel::create(const std::string& name,
        return nullptr;
    }
    // using 'new' to access a non-public constructor
    return std::shared_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
    return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
}

InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
@@ -269,8 +269,8 @@ InputChannel::~InputChannel() {
}

status_t InputChannel::openInputChannelPair(const std::string& name,
                                            std::shared_ptr<InputChannel>& outServerChannel,
                                            std::shared_ptr<InputChannel>& outClientChannel) {
                                            std::unique_ptr<InputChannel>& outServerChannel,
                                            std::unique_ptr<InputChannel>& outClientChannel) {
    int sockets[2];
    if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
        status_t result = -errno;
@@ -376,7 +376,7 @@ status_t InputChannel::receiveMessage(InputMessage* msg) {
    return OK;
}

std::shared_ptr<InputChannel> InputChannel::dup() const {
std::unique_ptr<InputChannel> InputChannel::dup() const {
    android::base::unique_fd newFd(::dup(getFd()));
    if (!newFd.ok()) {
        ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
+10 −13
Original line number Diff line number Diff line
@@ -33,9 +33,6 @@
namespace android {

class InputChannelTest : public testing::Test {
protected:
    virtual void SetUp() { }
    virtual void TearDown() { }
};


@@ -47,7 +44,7 @@ TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptor

    android::base::unique_fd sendFd(pipe.sendFd);

    std::shared_ptr<InputChannel> inputChannel =
    std::unique_ptr<InputChannel> inputChannel =
            InputChannel::create("channel name", std::move(sendFd), new BBinder());

    EXPECT_NE(inputChannel, nullptr) << "channel should be successfully created";
@@ -62,14 +59,14 @@ TEST_F(InputChannelTest, ConstructorAndDestructor_TakesOwnershipOfFileDescriptor
TEST_F(InputChannelTest, SetAndGetToken) {
    Pipe pipe;
    sp<IBinder> token = new BBinder();
    std::shared_ptr<InputChannel> channel =
    std::unique_ptr<InputChannel> channel =
            InputChannel::create("test channel", android::base::unique_fd(pipe.sendFd), token);

    EXPECT_EQ(token, channel->getConnectionToken());
}

TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;

    status_t result = InputChannel::openInputChannelPair("channel name",
            serverChannel, clientChannel);
@@ -120,7 +117,7 @@ TEST_F(InputChannelTest, OpenInputChannelPair_ReturnsAPairOfConnectedChannels) {
}

TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;

    status_t result = InputChannel::openInputChannelPair("channel name",
            serverChannel, clientChannel);
@@ -134,7 +131,7 @@ TEST_F(InputChannelTest, ReceiveSignal_WhenNoSignalPresent_ReturnsAnError) {
}

TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;

    status_t result = InputChannel::openInputChannelPair("channel name",
            serverChannel, clientChannel);
@@ -150,7 +147,7 @@ TEST_F(InputChannelTest, ReceiveSignal_WhenPeerClosed_ReturnsAnError) {
}

TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;

    status_t result = InputChannel::openInputChannelPair("channel name",
            serverChannel, clientChannel);
@@ -167,7 +164,7 @@ TEST_F(InputChannelTest, SendSignal_WhenPeerClosed_ReturnsAnError) {
}

TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;
    status_t result = InputChannel::openInputChannelPair("channel name",
            serverChannel, clientChannel);
    ASSERT_EQ(OK, result)
@@ -199,7 +196,7 @@ TEST_F(InputChannelTest, SendAndReceive_MotionClassification) {
}

TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;

    status_t result =
            InputChannel::openInputChannelPair("channel parceling", serverChannel, clientChannel);
@@ -218,14 +215,14 @@ TEST_F(InputChannelTest, InputChannelParcelAndUnparcel) {
}

TEST_F(InputChannelTest, DuplicateChannelAndAssertEqual) {
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    std::unique_ptr<InputChannel> serverChannel, clientChannel;

    status_t result =
            InputChannel::openInputChannelPair("channel dup", serverChannel, clientChannel);

    ASSERT_EQ(OK, result) << "should have successfully opened a channel pair";

    std::shared_ptr<InputChannel> dupChan = serverChannel->dup();
    std::unique_ptr<InputChannel> dupChan = serverChannel->dup();

    EXPECT_EQ(*serverChannel == *dupChan, true) << "inputchannel should be equal after duplication";
}
+11 −23
Original line number Diff line number Diff line
@@ -30,33 +30,21 @@ namespace android {

class InputPublisherAndConsumerTest : public testing::Test {
protected:
    std::shared_ptr<InputChannel> serverChannel, clientChannel;
    InputPublisher* mPublisher;
    InputConsumer* mConsumer;
    std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
    std::unique_ptr<InputPublisher> mPublisher;
    std::unique_ptr<InputConsumer> mConsumer;
    PreallocatedInputEventFactory mEventFactory;

    virtual void SetUp() {
    void SetUp() override {
        std::unique_ptr<InputChannel> serverChannel, clientChannel;
        status_t result = InputChannel::openInputChannelPair("channel name",
                serverChannel, clientChannel);
        ASSERT_EQ(OK, result);
        mServerChannel = std::move(serverChannel);
        mClientChannel = std::move(clientChannel);

        mPublisher = new InputPublisher(serverChannel);
        mConsumer = new InputConsumer(clientChannel);
    }

    virtual void TearDown() {
        if (mPublisher) {
            delete mPublisher;
            mPublisher = nullptr;
        }

        if (mConsumer) {
            delete mConsumer;
            mConsumer = nullptr;
        }

        serverChannel.reset();
        clientChannel.reset();
        mPublisher = std::make_unique<InputPublisher>(mServerChannel);
        mConsumer = std::make_unique<InputConsumer>(mClientChannel);
    }

    void PublishAndConsumeKeyEvent();
@@ -65,8 +53,8 @@ protected:
};

TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
    EXPECT_EQ(serverChannel.get(), mPublisher->getChannel().get());
    EXPECT_EQ(clientChannel.get(), mConsumer->getChannel().get());
    EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
    EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
}

void InputPublisherAndConsumerTest::PublishAndConsumeKeyEvent() {
Loading