OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SYSTEM_MESSAGE_PIPE_H_ | 5 #ifndef MOJO_SYSTEM_MESSAGE_PIPE_H_ |
6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_ | 6 #define MOJO_SYSTEM_MESSAGE_PIPE_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
12 #include "mojo/public/system/core.h" | 13 #include "mojo/public/system/core.h" |
13 #include "mojo/public/system/system_export.h" | 14 #include "mojo/public/system/system_export.h" |
| 15 #include "mojo/system/message_in_transit.h" |
14 | 16 |
15 namespace mojo { | 17 namespace mojo { |
16 namespace system { | 18 namespace system { |
17 | 19 |
| 20 class Channel; |
18 class MessagePipeEndpoint; | 21 class MessagePipeEndpoint; |
19 class Waiter; | 22 class Waiter; |
20 | 23 |
21 // |MessagePipe| is the secondary object implementing a message pipe (see the | 24 // |MessagePipe| is the secondary object implementing a message pipe (see the |
22 // explanatory comment in core_impl.cc). It is typically owned by the | 25 // explanatory comment in core_impl.cc). It is typically owned by the |
23 // dispatcher(s) corresponding to the local endpoints. This class is | 26 // dispatcher(s) corresponding to the local endpoints. This class is |
24 // thread-safe. | 27 // thread-safe. |
25 class MOJO_SYSTEM_EXPORT MessagePipe : | 28 class MOJO_SYSTEM_EXPORT MessagePipe : |
26 public base::RefCountedThreadSafe<MessagePipe> { | 29 public base::RefCountedThreadSafe<MessagePipe> { |
27 public: | 30 public: |
28 MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint_0, | 31 MessagePipe(scoped_ptr<MessagePipeEndpoint> endpoint_0, |
29 scoped_ptr<MessagePipeEndpoint> endpoint_1); | 32 scoped_ptr<MessagePipeEndpoint> endpoint_1); |
30 | 33 |
31 // Convenience constructor that constructs a |MessagePipe| with two new | 34 // Convenience constructor that constructs a |MessagePipe| with two new |
32 // |LocalMessagePipeEndpoint|s. | 35 // |LocalMessagePipeEndpoint|s. |
33 MessagePipe(); | 36 MessagePipe(); |
34 | 37 |
| 38 // Gets the other port number (i.e., 0 -> 1, 1 -> 0). |
| 39 static unsigned GetPeerPort(unsigned port); |
| 40 |
35 // These are called by the dispatcher to implement its methods of | 41 // These are called by the dispatcher to implement its methods of |
36 // corresponding names. In all cases, the port |port| must be open. | 42 // corresponding names. In all cases, the port |port| must be open. |
37 void CancelAllWaiters(unsigned port); | 43 void CancelAllWaiters(unsigned port); |
38 void Close(unsigned port); | 44 void Close(unsigned port); |
39 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its | 45 // Unlike |MessagePipeDispatcher::WriteMessage()|, this does not validate its |
40 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. | 46 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. |
41 MojoResult WriteMessage(unsigned port, | 47 MojoResult WriteMessage(unsigned port, |
42 const void* bytes, uint32_t num_bytes, | 48 const void* bytes, uint32_t num_bytes, |
43 const MojoHandle* handles, uint32_t num_handles, | 49 const MojoHandle* handles, uint32_t num_handles, |
44 MojoWriteMessageFlags flags); | 50 MojoWriteMessageFlags flags); |
45 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its | 51 // Unlike |MessagePipeDispatcher::ReadMessage()|, this does not validate its |
46 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. | 52 // arguments. |bytes|/|num_bytes| and |handles|/|num_handles| must be valid. |
47 MojoResult ReadMessage(unsigned port, | 53 MojoResult ReadMessage(unsigned port, |
48 void* bytes, uint32_t* num_bytes, | 54 void* bytes, uint32_t* num_bytes, |
49 MojoHandle* handles, uint32_t* num_handles, | 55 MojoHandle* handles, uint32_t* num_handles, |
50 MojoReadMessageFlags flags); | 56 MojoReadMessageFlags flags); |
51 MojoResult AddWaiter(unsigned port, | 57 MojoResult AddWaiter(unsigned port, |
52 Waiter* waiter, | 58 Waiter* waiter, |
53 MojoWaitFlags flags, | 59 MojoWaitFlags flags, |
54 MojoResult wake_result); | 60 MojoResult wake_result); |
55 void RemoveWaiter(unsigned port, Waiter* waiter); | 61 void RemoveWaiter(unsigned port, Waiter* waiter); |
56 | 62 |
| 63 // This is used internally by |WriteMessage()| and by |Channel| to enqueue |
| 64 // messages (typically to a |LocalMessagePipeEndpoint|). Unlike |
| 65 // |WriteMessage()|, |port| is the *destination* port. Takes ownership of |
| 66 // |message|. |
| 67 MojoResult EnqueueMessage(unsigned port, MessageInTransit* message); |
| 68 |
| 69 // These are used by |Channel|. |
| 70 void Attach(unsigned port, |
| 71 scoped_refptr<Channel> channel, |
| 72 MessageInTransit::EndpointId local_id); |
| 73 void Run(unsigned port, MessageInTransit::EndpointId remote_id); |
| 74 |
57 private: | 75 private: |
58 friend class base::RefCountedThreadSafe<MessagePipe>; | 76 friend class base::RefCountedThreadSafe<MessagePipe>; |
59 virtual ~MessagePipe(); | 77 virtual ~MessagePipe(); |
60 | 78 |
| 79 // Used by |EnqueueMessage()| to handle control messages that are actually |
| 80 // meant for us. |
| 81 MojoResult HandleControlMessage(unsigned port, MessageInTransit* message); |
| 82 |
61 base::Lock lock_; // Protects the following members. | 83 base::Lock lock_; // Protects the following members. |
62 scoped_ptr<MessagePipeEndpoint> endpoints_[2]; | 84 scoped_ptr<MessagePipeEndpoint> endpoints_[2]; |
63 | 85 |
64 DISALLOW_COPY_AND_ASSIGN(MessagePipe); | 86 DISALLOW_COPY_AND_ASSIGN(MessagePipe); |
65 }; | 87 }; |
66 | 88 |
67 } // namespace system | 89 } // namespace system |
68 } // namespace mojo | 90 } // namespace mojo |
69 | 91 |
70 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_ | 92 #endif // MOJO_SYSTEM_MESSAGE_PIPE_H_ |
OLD | NEW |