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_PROXY_MESSAGE_PIPE_ENDPOINT_H_
6 #define MOJO_SYSTEM_PROXY_MESSAGE_PIPE_ENDPOINT_H_
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "mojo/system/message_in_transit.h"
14 #include "mojo/system/message_in_transit_queue.h"
15 #include "mojo/system/message_pipe_endpoint.h"
16 #include "mojo/system/system_impl_export.h"
22 class LocalMessagePipeEndpoint
;
25 // A |ProxyMessagePipeEndpoint| connects an end of a |MessagePipe| to a
26 // |Channel|, over which it transmits and receives data (to/from another
27 // |ProxyMessagePipeEndpoint|). So a |MessagePipe| with one endpoint local and
28 // the other endpoint remote consists of a |LocalMessagePipeEndpoint| and a
29 // |ProxyMessagePipeEndpoint|, with only the local endpoint being accessible via
30 // a |MessagePipeDispatcher|.
32 // Like any |MessagePipeEndpoint|, a |ProxyMessagePipeEndpoint| is owned by a
34 // - A |ProxyMessagePipeEndpoint| starts out *detached*, i.e., not associated
35 // to any |Channel|. When *attached*, it gets a reference to a |Channel| and
36 // is assigned a local ID. A |ProxyMessagePipeEndpoint| must be detached
37 // before destruction; this is done inside |Close()|.
38 // - When attached, a |ProxyMessagePipeEndpoint| starts out not running. When
39 // run, it gets a remote ID.
40 class MOJO_SYSTEM_IMPL_EXPORT ProxyMessagePipeEndpoint
41 : public MessagePipeEndpoint
{
43 ProxyMessagePipeEndpoint();
44 // Constructs a |ProxyMessagePipeEndpoint| that replaces the given
45 // |LocalMessagePipeEndpoint| (which this constructor will close), taking its
46 // message queue's contents. This is done when transferring a message pipe
47 // handle over a remote message pipe.
48 ProxyMessagePipeEndpoint(
49 LocalMessagePipeEndpoint
* local_message_pipe_endpoint
,
51 virtual ~ProxyMessagePipeEndpoint();
53 // |MessagePipeEndpoint| implementation:
54 virtual Type
GetType() const OVERRIDE
;
55 virtual bool OnPeerClose() OVERRIDE
;
56 virtual void EnqueueMessage(scoped_ptr
<MessageInTransit
> message
) OVERRIDE
;
57 virtual void Attach(scoped_refptr
<Channel
> channel
,
58 MessageInTransit::EndpointId local_id
) OVERRIDE
;
59 virtual bool Run(MessageInTransit::EndpointId remote_id
) OVERRIDE
;
60 virtual void OnRemove() OVERRIDE
;
66 void AssertConsistentState() const {}
68 void AssertConsistentState() const;
71 bool is_attached() const { return !!channel_
.get(); }
73 bool is_running() const {
74 return remote_id_
!= MessageInTransit::kInvalidEndpointId
;
77 // This should only be set if we're attached.
78 scoped_refptr
<Channel
> channel_
;
80 // |local_id_| should be set to something other than
81 // |MessageInTransit::kInvalidEndpointId| when we're attached.
82 MessageInTransit::EndpointId local_id_
;
84 // |remote_id_| being set to anything other than
85 // |MessageInTransit::kInvalidEndpointId| indicates that we're "running",
86 // i.e., actively able to send messages. We should only ever be running if
88 MessageInTransit::EndpointId remote_id_
;
92 // This queue is only used while we're detached, to store messages while we're
93 // not ready to send them yet.
94 MessageInTransitQueue paused_message_queue_
;
96 DISALLOW_COPY_AND_ASSIGN(ProxyMessagePipeEndpoint
);
102 #endif // MOJO_SYSTEM_PROXY_MESSAGE_PIPE_ENDPOINT_H_