Home | History | Annotate | Download | only in system
      1 // Copyright 2013 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 MOJO_SYSTEM_MESSAGE_PIPE_ENDPOINT_H_
      6 #define MOJO_SYSTEM_MESSAGE_PIPE_ENDPOINT_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include <vector>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "mojo/public/system/core.h"
     15 #include "mojo/system/message_in_transit.h"
     16 #include "mojo/system/system_impl_export.h"
     17 
     18 namespace mojo {
     19 namespace system {
     20 
     21 class Channel;
     22 class Dispatcher;
     23 class Waiter;
     24 
     25 // This is an interface to one of the ends of a message pipe, and is used by
     26 // |MessagePipe|. Its most important role is to provide a sink for messages
     27 // (i.e., a place where messages can be sent). It has a secondary role: When the
     28 // endpoint is local (i.e., in the current process), there'll be a dispatcher
     29 // corresponding to the endpoint. In that case, the implementation of
     30 // |MessagePipeEndpoint| also implements the functionality required by the
     31 // dispatcher, e.g., to read messages and to wait. Implementations of this class
     32 // are not thread-safe; instances are protected by |MesssagePipe|'s lock.
     33 class MOJO_SYSTEM_IMPL_EXPORT MessagePipeEndpoint {
     34  public:
     35   virtual ~MessagePipeEndpoint() {}
     36 
     37   // All implementations must implement these.
     38   virtual void Close() = 0;
     39   // Returns false if the endpoint should be closed and destroyed, else true.
     40   virtual bool OnPeerClose() = 0;
     41   // Checks if |EnqueueMessage()| will be able to enqueue the given message
     42   // (with the given set of dispatchers). |dispatchers| should be non-null only
     43   // if it's nonempty. Returns |MOJO_RESULT_OK| if it will and an appropriate
     44   // error code if it won't.
     45   virtual MojoResult CanEnqueueMessage(
     46       const MessageInTransit* message,
     47       const std::vector<Dispatcher*>* dispatchers) = 0;
     48   // Takes ownership of |message| and the contents of |dispatchers| (leaving
     49   // it empty). This should only be called after |CanEnqueueMessage()| has
     50   // indicated success. (Unlike |CanEnqueueMessage()|, |dispatchers| may be
     51   // non-null but empty.)
     52   virtual void EnqueueMessage(
     53       MessageInTransit* message,
     54       std::vector<scoped_refptr<Dispatcher> >* dispatchers) = 0;
     55 
     56   // Implementations must override these if they represent a local endpoint,
     57   // i.e., one for which there's a |MessagePipeDispatcher| (and thus a handle).
     58   // An implementation for a proxy endpoint (for which there's no dispatcher)
     59   // needs not override these methods, since they should never be called.
     60   //
     61   // These methods implement the methods of the same name in |MessagePipe|,
     62   // though |MessagePipe|'s implementation may have to do a little more if the
     63   // operation involves both endpoints.
     64   virtual void CancelAllWaiters();
     65   virtual MojoResult ReadMessage(
     66       void* bytes, uint32_t* num_bytes,
     67       std::vector<scoped_refptr<Dispatcher> >* dispatchers,
     68       uint32_t* num_dispatchers,
     69       MojoReadMessageFlags flags);
     70   virtual MojoResult AddWaiter(Waiter* waiter,
     71                                MojoWaitFlags flags,
     72                                MojoResult wake_result);
     73   virtual void RemoveWaiter(Waiter* waiter);
     74 
     75   // Implementations must override these if they represent a proxy endpoint. An
     76   // implementation for a local endpoint needs not override these methods, since
     77   // they should never be called.
     78   virtual void Attach(scoped_refptr<Channel> channel,
     79                       MessageInTransit::EndpointId local_id);
     80   // Returns false if the endpoint should be closed and destroyed, else true.
     81   virtual bool Run(MessageInTransit::EndpointId remote_id);
     82 
     83  protected:
     84   MessagePipeEndpoint() {}
     85 
     86  private:
     87   DISALLOW_COPY_AND_ASSIGN(MessagePipeEndpoint);
     88 };
     89 
     90 }  // namespace system
     91 }  // namespace mojo
     92 
     93 #endif  // MOJO_SYSTEM_MESSAGE_PIPE_ENDPOINT_H_
     94