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_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "mojo/public/system/core.h"
16 #include "mojo/system/message_in_transit.h"
17 #include "mojo/system/message_pipe_endpoint.h"
18 #include "mojo/system/system_impl_export.h"
26 // A |ProxyMessagePipeEndpoint| connects an end of a |MessagePipe| to a
27 // |Channel|, over which it transmits and receives data (to/from another
28 // |ProxyMessagePipeEndpoint|). So a |MessagePipe| with one endpoint local and
29 // the other endpoint remote consists of a |LocalMessagePipeEndpoint| and a
30 // |ProxyMessagePipeEndpoint|, with only the local endpoint being accessible via
31 // a |MessagePipeDispatcher|.
33 // Like any |MessagePipeEndpoint|, a |ProxyMessagePipeEndpoint| is owned by a
35 // - A |ProxyMessagePipeEndpoint| starts out *detached*, i.e., not associated
36 // to any |Channel|. When *attached*, it gets a reference to a |Channel| and
37 // is assigned a local ID. A |ProxyMessagePipeEndpoint| must be detached
38 // before destruction; this is done inside |Close()|.
39 // - When attached, a |ProxyMessagePipeEndpoint| starts out not running. When
40 // run, it gets a remote ID.
41 class MOJO_SYSTEM_IMPL_EXPORT ProxyMessagePipeEndpoint
42 : public MessagePipeEndpoint {
44 ProxyMessagePipeEndpoint();
45 virtual ~ProxyMessagePipeEndpoint();
47 // |MessagePipeEndpoint| implementation:
48 virtual void Close() OVERRIDE;
49 virtual void OnPeerClose() OVERRIDE;
50 virtual MojoResult EnqueueMessage(
51 MessageInTransit* message,
52 std::vector<DispatcherTransport>* transports) OVERRIDE;
53 virtual void Attach(scoped_refptr<Channel> channel,
54 MessageInTransit::EndpointId local_id) OVERRIDE;
55 virtual void Run(MessageInTransit::EndpointId remote_id) OVERRIDE;
58 bool is_attached() const {
59 return !!channel_.get();
62 bool is_running() const {
63 return remote_id_ != MessageInTransit::kInvalidEndpointId;
66 // "Attaches" |transports| (which must be non-null and nonempty) to |message|
67 // by "serializing" them in an appropriate way, and closes each dispatcher.
68 void AttachAndCloseDispatchers(MessageInTransit* message,
69 std::vector<DispatcherTransport>* transports);
70 void EnqueueMessageInternal(MessageInTransit* message);
73 void AssertConsistentState() const {}
75 void AssertConsistentState() const;
78 // This should only be set if we're attached.
79 scoped_refptr<Channel> channel_;
81 // |local_id_| should be set to something other than
82 // |MessageInTransit::kInvalidEndpointId| when we're attached.
83 MessageInTransit::EndpointId local_id_;
85 // |remote_id_| being set to anything other than
86 // |MessageInTransit::kInvalidEndpointId| indicates that we're "running",
87 // i.e., actively able to send messages. We should only ever be running if
89 MessageInTransit::EndpointId remote_id_;
94 // This queue is only used while we're detached, to store messages while we're
95 // not ready to send them yet.
96 std::deque<MessageInTransit*> paused_message_queue_;
98 DISALLOW_COPY_AND_ASSIGN(ProxyMessagePipeEndpoint);
101 } // namespace system
104 #endif // MOJO_SYSTEM_PROXY_MESSAGE_PIPE_ENDPOINT_H_