Upstream version 9.38.198.0
[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 "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"
17
18 namespace mojo {
19 namespace system {
20
21 class Channel;
22 class LocalMessagePipeEndpoint;
23 class MessagePipe;
24
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|.
31 //
32 // Like any |MessagePipeEndpoint|, a |ProxyMessagePipeEndpoint| is owned by a
33 // |MessagePipe|.
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 {
42  public:
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,
50       bool is_peer_open);
51   virtual ~ProxyMessagePipeEndpoint();
52
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;
61
62  private:
63   void Detach();
64
65 #ifdef NDEBUG
66   void AssertConsistentState() const {}
67 #else
68   void AssertConsistentState() const;
69 #endif
70
71   bool is_attached() const { return !!channel_; }
72
73   bool is_running() const {
74     return remote_id_ != MessageInTransit::kInvalidEndpointId;
75   }
76
77   // This should only be set if we're attached.
78   scoped_refptr<Channel> channel_;
79
80   // |local_id_| should be set to something other than
81   // |MessageInTransit::kInvalidEndpointId| when we're attached.
82   MessageInTransit::EndpointId local_id_;
83
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
87   // we're attached.
88   MessageInTransit::EndpointId remote_id_;
89
90   bool is_peer_open_;
91
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_;
95
96   DISALLOW_COPY_AND_ASSIGN(ProxyMessagePipeEndpoint);
97 };
98
99 }  // namespace system
100 }  // namespace mojo
101
102 #endif  // MOJO_SYSTEM_PROXY_MESSAGE_PIPE_ENDPOINT_H_