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

Commit 7b9f4f53 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Pass unique_ptr of InputChannel to Connection

This gets us closer to the goal of storing unique_ptr of InputChannel
inside the InputPublisher.

In this CL, we are still allowing the "copy" and "dup" functions to
exist. Removing those requires significant refactoring on the Java side,
including the jni layer and object ownership models.

Test: TEST=inputflinger_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST
Bug: 161009324
Change-Id: If6f44d78c7fc9f3e12729068de1753847abf0ebb
parent 8dc87156
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -300,6 +300,15 @@ public:

    void copyTo(android::os::InputChannelCore& outChannel) const;

    /**
     * Similar to "copyTo", but it takes ownership of the provided InputChannel (and after this is
     * called, it destroys it).
     * @param from the InputChannel that should be converted to InputChannelCore
     * @param outChannel the pre-allocated InputChannelCore to which to transfer the 'from' channel
     */
    static void moveChannel(std::unique_ptr<InputChannel> from,
                            android::os::InputChannelCore& outChannel);

    /**
     * The connection token is used to identify the input connection, i.e.
     * the pair of input channels that were created simultaneously. Input channels
@@ -333,7 +342,7 @@ public:
    ~InputPublisher();

    /* Gets the underlying input channel. */
    inline std::shared_ptr<InputChannel> getChannel() const { return mChannel; }
    inline InputChannel& getChannel() const { return *mChannel; }

    /* Publishes a key event to the input channel.
     *
+8 −1
Original line number Diff line number Diff line
@@ -584,6 +584,13 @@ void InputChannel::copyTo(android::os::InputChannelCore& outChannel) const {
    outChannel.token = getConnectionToken();
}

void InputChannel::moveChannel(std::unique_ptr<InputChannel> from,
                               android::os::InputChannelCore& outChannel) {
    outChannel.name = from->getName();
    outChannel.fd = android::os::ParcelFileDescriptor(std::move(from->fd));
    outChannel.token = from->getConnectionToken();
}

sp<IBinder> InputChannel::getConnectionToken() const {
    return token;
}
@@ -591,7 +598,7 @@ sp<IBinder> InputChannel::getConnectionToken() const {
// --- InputPublisher ---

InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel)
      : mChannel(channel), mInputVerifier(channel->getName()) {}
      : mChannel(channel), mInputVerifier(mChannel->getName()) {}

InputPublisher::~InputPublisher() {
}
+3 −10
Original line number Diff line number Diff line
@@ -220,7 +220,6 @@ void waitUntilInputAvailable(const InputConsumer& inputConsumer) {

class InputPublisherAndConsumerTest : public testing::Test {
protected:
    std::shared_ptr<InputChannel> mServerChannel, mClientChannel;
    std::unique_ptr<InputPublisher> mPublisher;
    std::unique_ptr<InputConsumer> mConsumer;
    PreallocatedInputEventFactory mEventFactory;
@@ -230,11 +229,9 @@ protected:
        status_t result = InputChannel::openInputChannelPair("channel name",
                serverChannel, clientChannel);
        ASSERT_EQ(OK, result);
        mServerChannel = std::move(serverChannel);
        mClientChannel = std::move(clientChannel);

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

    void publishAndConsumeKeyEvent();
@@ -254,11 +251,7 @@ private:
};

TEST_F(InputPublisherAndConsumerTest, GetChannel_ReturnsTheChannel) {
    ASSERT_NE(nullptr, mPublisher->getChannel());
    ASSERT_NE(nullptr, mConsumer->getChannel());
    EXPECT_EQ(mServerChannel.get(), mPublisher->getChannel().get());
    EXPECT_EQ(mClientChannel.get(), mConsumer->getChannel().get());
    ASSERT_EQ(mPublisher->getChannel()->getConnectionToken(),
    ASSERT_EQ(mPublisher->getChannel().getConnectionToken(),
              mConsumer->getChannel()->getConnectionToken());
}

+1 −1
Original line number Diff line number Diff line
@@ -277,7 +277,7 @@ binder::Status InputManager::createInputChannel(const std::string& name,
        return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
                                                 channel.error().message().c_str());
    }
    (*channel)->copyTo(*outChannel);
    InputChannel::moveChannel(std::move(*channel), *outChannel);
    return binder::Status::ok();
}

+3 −3
Original line number Diff line number Diff line
@@ -20,15 +20,15 @@

namespace android::inputdispatcher {

Connection::Connection(const std::shared_ptr<InputChannel>& inputChannel, bool monitor,
Connection::Connection(std::unique_ptr<InputChannel> inputChannel, bool monitor,
                       const IdGenerator& idGenerator)
      : status(Status::NORMAL),
        monitor(monitor),
        inputPublisher(inputChannel),
        inputPublisher(std::move(inputChannel)),
        inputState(idGenerator) {}

sp<IBinder> Connection::getToken() const {
    return inputPublisher.getChannel()->getConnectionToken();
    return inputPublisher.getChannel().getConnectionToken();
};

} // namespace android::inputdispatcher
Loading