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.
5 #ifndef MOJO_SYSTEM_MESSAGE_PIPE_ENDPOINT_H_
6 #define MOJO_SYSTEM_MESSAGE_PIPE_ENDPOINT_H_
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"
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 {
35 virtual ~MessagePipeEndpoint() {}
37 // All implementations must implement these.
38 virtual void Close() = 0;
39 virtual void OnPeerClose() = 0;
40 // Checks if |EnqueueMessage()| will be able to enqueue the given message
41 // (with the given set of dispatchers). |dispatchers| should be non-null only
42 // if it's nonempty. Returns |MOJO_RESULT_OK| if it will and an appropriate
43 // error code if it won't.
44 virtual MojoResult CanEnqueueMessage(
45 const MessageInTransit* message,
46 const std::vector<Dispatcher*>* dispatchers) = 0;
47 // Takes ownership of |message| and the contents of |dispatchers| (leaving
48 // it empty). This should only be called after |CanEnqueueMessage()| has
49 // indicated success. (Unlike |CanEnqueueMessage()|, |dispatchers| may be
50 // non-null but empty.)
51 virtual void EnqueueMessage(
52 MessageInTransit* message,
53 std::vector<scoped_refptr<Dispatcher> >* dispatchers) = 0;
55 // Implementations must override these if they represent a local endpoint,
56 // i.e., one for which there's a |MessagePipeDispatcher| (and thus a handle).
57 // An implementation for a proxy endpoint (for which there's no dispatcher)
58 // needs not override these methods, since they should never be called.
60 // These methods implement the methods of the same name in |MessagePipe|,
61 // though |MessagePipe|'s implementation may have to do a little more if the
62 // operation involves both endpoints.
63 virtual void CancelAllWaiters();
64 virtual MojoResult ReadMessage(
65 void* bytes, uint32_t* num_bytes,
66 std::vector<scoped_refptr<Dispatcher> >* dispatchers,
67 uint32_t* num_dispatchers,
68 MojoReadMessageFlags flags);
69 virtual MojoResult AddWaiter(Waiter* waiter,
71 MojoResult wake_result);
72 virtual void RemoveWaiter(Waiter* waiter);
74 // Implementations must override these if they represent a proxy endpoint. An
75 // implementation for a local endpoint needs not override these methods, since
76 // they should never be called.
77 virtual void Attach(scoped_refptr<Channel> channel,
78 MessageInTransit::EndpointId local_id);
79 virtual void Run(MessageInTransit::EndpointId remote_id);
82 MessagePipeEndpoint() {}
85 DISALLOW_COPY_AND_ASSIGN(MessagePipeEndpoint);
91 #endif // MOJO_SYSTEM_MESSAGE_PIPE_ENDPOINT_H_