Home | History | Annotate | Download | only in ipc
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef IPC_IPC_MESSAGE_PIPE_READER_H_
      6 #define IPC_IPC_MESSAGE_PIPE_READER_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include <memory>
     11 #include <vector>
     12 
     13 #include "base/atomicops.h"
     14 #include "base/compiler_specific.h"
     15 #include "base/component_export.h"
     16 #include "base/macros.h"
     17 #include "base/process/process_handle.h"
     18 #include "base/threading/thread_checker.h"
     19 #include "ipc/ipc.mojom.h"
     20 #include "ipc/ipc_message.h"
     21 #include "mojo/public/cpp/bindings/associated_binding.h"
     22 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
     23 #include "mojo/public/cpp/system/core.h"
     24 #include "mojo/public/cpp/system/message_pipe.h"
     25 
     26 namespace IPC {
     27 namespace internal {
     28 
     29 // A helper class to handle bytestream directly over mojo::MessagePipe
     30 // in template-method pattern. MessagePipeReader manages the lifetime
     31 // of given MessagePipe and participates the event loop, and
     32 // read the stream and call the client when it is ready.
     33 //
     34 // Each client has to:
     35 //
     36 //  * Provide a subclass implemenation of a specific use of a MessagePipe
     37 //    and implement callbacks.
     38 //  * Create the subclass instance with a MessagePipeHandle.
     39 //    The constructor automatically start listening on the pipe.
     40 //
     41 // All functions must be called on the IO thread, except for Send(), which can
     42 // be called on any thread. All |Delegate| functions will be called on the IO
     43 // thread.
     44 //
     45 class COMPONENT_EXPORT(IPC) MessagePipeReader : public mojom::Channel {
     46  public:
     47   class Delegate {
     48    public:
     49     virtual void OnPeerPidReceived(int32_t peer_pid) = 0;
     50     virtual void OnMessageReceived(const Message& message) = 0;
     51     virtual void OnBrokenDataReceived() = 0;
     52     virtual void OnPipeError() = 0;
     53     virtual void OnAssociatedInterfaceRequest(
     54         const std::string& name,
     55         mojo::ScopedInterfaceEndpointHandle handle) = 0;
     56   };
     57 
     58   // Builds a reader that reads messages from |receive_handle| and lets
     59   // |delegate| know.
     60   //
     61   // |pipe| is the message pipe handle corresponding to the channel's master
     62   // interface. This is the message pipe underlying both |sender| and
     63   // |receiver|.
     64   //
     65   // Both |sender| and |receiver| must be non-null.
     66   //
     67   // Note that MessagePipeReader doesn't delete |delegate|.
     68   MessagePipeReader(mojo::MessagePipeHandle pipe,
     69                     mojom::ChannelAssociatedPtr sender,
     70                     mojo::AssociatedInterfaceRequest<mojom::Channel> receiver,
     71                     Delegate* delegate);
     72   ~MessagePipeReader() override;
     73 
     74   // Close and destroy the MessagePipe.
     75   void Close();
     76 
     77   // Return true if the MessagePipe is alive.
     78   bool IsValid() { return sender_.is_bound(); }
     79 
     80   // Sends an IPC::Message to the other end of the pipe. Safe to call from any
     81   // thread.
     82   bool Send(std::unique_ptr<Message> message);
     83 
     84   // Requests an associated interface from the other end of the pipe.
     85   void GetRemoteInterface(const std::string& name,
     86                           mojo::ScopedInterfaceEndpointHandle handle);
     87 
     88   mojom::ChannelAssociatedPtr& sender() { return sender_; }
     89 
     90  protected:
     91   void OnPipeClosed();
     92   void OnPipeError(MojoResult error);
     93 
     94  private:
     95   // mojom::Channel:
     96   void SetPeerPid(int32_t peer_pid) override;
     97   void Receive(MessageView message_view) override;
     98   void GetAssociatedInterface(
     99       const std::string& name,
    100       mojom::GenericInterfaceAssociatedRequest request) override;
    101 
    102   // |delegate_| is null once the message pipe is closed.
    103   Delegate* delegate_;
    104   mojom::ChannelAssociatedPtr sender_;
    105   mojo::AssociatedBinding<mojom::Channel> binding_;
    106   base::ThreadChecker thread_checker_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(MessagePipeReader);
    109 };
    110 
    111 }  // namespace internal
    112 }  // namespace IPC
    113 
    114 #endif  // IPC_IPC_MESSAGE_PIPE_READER_H_
    115