137f192566051e24e7fba1d0b1097b1b919a476f
[platform/framework/web/crosswalk.git] / src / mojo / system / proxy_message_pipe_endpoint.h
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_PROXY_MESSAGE_PIPE_ENDPOINT_H_
6 #define MOJO_SYSTEM_PROXY_MESSAGE_PIPE_ENDPOINT_H_
7
8 #include <stdint.h>
9
10 #include <deque>
11
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"
19
20 namespace mojo {
21 namespace system {
22
23 class Channel;
24 class MessagePipe;
25
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|.
32 //
33 // Like any |MessagePipeEndpoint|, a |ProxyMessagePipeEndpoint| is owned by a
34 // |MessagePipe|.
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 {
43  public:
44   ProxyMessagePipeEndpoint();
45   virtual ~ProxyMessagePipeEndpoint();
46
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;
56
57  private:
58   bool is_attached() const {
59     return !!channel_.get();
60   }
61
62   bool is_running() const {
63     return remote_id_ != MessageInTransit::kInvalidEndpointId;
64   }
65
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);
71
72 #ifdef NDEBUG
73   void AssertConsistentState() const {}
74 #else
75   void AssertConsistentState() const;
76 #endif
77
78   // This should only be set if we're attached.
79   scoped_refptr<Channel> channel_;
80
81   // |local_id_| should be set to something other than
82   // |MessageInTransit::kInvalidEndpointId| when we're attached.
83   MessageInTransit::EndpointId local_id_;
84
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
88   // we're attached.
89   MessageInTransit::EndpointId remote_id_;
90
91   bool is_open_;
92   bool is_peer_open_;
93
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_;
97
98   DISALLOW_COPY_AND_ASSIGN(ProxyMessagePipeEndpoint);
99 };
100
101 }  // namespace system
102 }  // namespace mojo
103
104 #endif  // MOJO_SYSTEM_PROXY_MESSAGE_PIPE_ENDPOINT_H_