Task Description: Implement the Watcher Class for Mojo Message Pipes
Your objective is to implement the Watcher class which is responsible for monitoring the state of Mojo message pipes. The implementation should be placed in the /app/ directory. Below are the specific requirements your implementation must satisfy based on the provided test code:
Class Definition:
Create a class named Watcher. This class should handle the registration of callback functions to listen for specific events (signals) on Mojo message pipes.
Start Method:
Implement a method MojoResult Start(MojoHandle handle, MojoSignals signals, ReadyCallback callback).
It should take a message pipe handle, a set of signals (e.g., MOJO_HANDLE_SIGNAL_READABLE), and a callback function to be executed when the specified signal is triggered.
Return MOJO_RESULT_OK if the watcher starts successfully.
Return MOJO_RESULT_FAILED_PRECONDITION if the handle is closed (null).
Return MOJO_RESULT_INVALID_ARGUMENT if the handle is invalid.
Cancel Method:
Implement a void Cancel() method that stops watching the message pipe and cleans up any associated resources.
Ensure that if the watcher is canceled, the callbacks should not be called (as validated in tests).
Watching State:
Implement a method bool IsWatching() that returns whether the watcher is currently registered to listen for events.
Callback Handling:
The class should be able to invoke the registered callback with the result of the monitored event when it occurs.
Handle the scenario where the associated message pipe is closed while still watching it, invoking the callback with MOJO_RESULT_CANCELLED.
Destructor Behavior:
Ensure that the watcher properly cleans up and cancels itself if it goes out of scope, invoking the necessary callbacks appropriately (e.g., MOJO_RESULT_CANCELLED).
Integration with Base Classes:
Use existing classes and utilities from the provided dependencies (like base::RunLoop) to manage the lifecycle of the message loop appropriately.
Signal Handling:
Implement the necessary mechanisms to handle signal events on the Mojo messages, ensuring callbacks are executed correctly as defined by the tests.
By fulfilling these requirements, your implementation will provide the required functionality for the Watcher class to pass all the tests in the given test code.