OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
6 #define MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <queue> | 10 #include <queue> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "mojo/edk/system/awakable_list.h" | 14 #include "mojo/edk/system/awakable_list.h" |
15 #include "mojo/edk/system/dispatcher.h" | 15 #include "mojo/edk/system/dispatcher.h" |
16 #include "mojo/edk/system/ports/port_ref.h" | 16 #include "mojo/edk/system/ports/port_ref.h" |
| 17 #include "mojo/edk/system/watcher_set.h" |
17 | 18 |
18 namespace mojo { | 19 namespace mojo { |
19 namespace edk { | 20 namespace edk { |
20 | 21 |
21 class NodeController; | 22 class NodeController; |
22 class PortsMessage; | 23 class PortsMessage; |
23 | 24 |
24 class MessagePipeDispatcher : public Dispatcher { | 25 class MessagePipeDispatcher : public Dispatcher { |
25 public: | 26 public: |
26 // Constructs a MessagePipeDispatcher permanently tied to a specific port. | 27 // Constructs a MessagePipeDispatcher permanently tied to a specific port. |
27 // |connected| must indicate the state of the port at construction time; if | 28 // |connected| must indicate the state of the port at construction time; if |
28 // the port is initialized with a peer, |connected| must be true. Otherwise it | 29 // the port is initialized with a peer, |connected| must be true. Otherwise it |
29 // must be false. | 30 // must be false. |
30 // | 31 // |
31 // A MessagePipeDispatcher may not be transferred while in a disconnected | 32 // A MessagePipeDispatcher may not be transferred while in a disconnected |
32 // state, and one can never return to a disconnected once connected. | 33 // state, and one can never return to a disconnected once connected. |
33 // | 34 // |
34 // |pipe_id| is a unique identifier which can be used to track pipe endpoints | 35 // |pipe_id| is a unique identifier which can be used to track pipe endpoints |
35 // as they're passed around. |endpoint| is either 0 or 1 and again is only | 36 // as they're passed around. |endpoint| is either 0 or 1 and again is only |
36 // used for tracking pipes (one side is always 0, the other is always 1.) | 37 // used for tracking pipes (one side is always 0, the other is always 1.) |
37 MessagePipeDispatcher(NodeController* node_controller, | 38 MessagePipeDispatcher(NodeController* node_controller, |
38 const ports::PortRef& port, | 39 const ports::PortRef& port, |
39 uint64_t pipe_id, | 40 uint64_t pipe_id, |
40 int endpoint); | 41 int endpoint); |
41 | 42 |
42 // Dispatcher: | 43 // Dispatcher: |
43 Type GetType() const override; | 44 Type GetType() const override; |
44 MojoResult Close() override; | 45 MojoResult Close(RequestContext* request_context) override; |
| 46 MojoResult Watch(MojoHandleSignals signals, |
| 47 const WatchCallback& callback, |
| 48 uintptr_t context, |
| 49 RequestContext* request_context) override; |
| 50 MojoResult CancelWatch(uintptr_t context) override; |
45 MojoResult WriteMessage(const void* bytes, | 51 MojoResult WriteMessage(const void* bytes, |
46 uint32_t num_bytes, | 52 uint32_t num_bytes, |
47 const DispatcherInTransit* dispatchers, | 53 const DispatcherInTransit* dispatchers, |
48 uint32_t num_dispatchers, | 54 uint32_t num_dispatchers, |
49 MojoWriteMessageFlags flags) override; | 55 MojoWriteMessageFlags flags, |
| 56 RequestContext* request_context) override; |
50 MojoResult ReadMessage(void* bytes, | 57 MojoResult ReadMessage(void* bytes, |
51 uint32_t* num_bytes, | 58 uint32_t* num_bytes, |
52 MojoHandle* handles, | 59 MojoHandle* handles, |
53 uint32_t* num_handles, | 60 uint32_t* num_handles, |
54 MojoReadMessageFlags flags) override; | 61 MojoReadMessageFlags flags, |
| 62 RequestContext* request_context) override; |
55 HandleSignalsState GetHandleSignalsState() const override; | 63 HandleSignalsState GetHandleSignalsState() const override; |
56 MojoResult AddAwakable(Awakable* awakable, | 64 MojoResult AddAwakable(Awakable* awakable, |
57 MojoHandleSignals signals, | 65 MojoHandleSignals signals, |
58 uintptr_t context, | 66 uintptr_t context, |
59 HandleSignalsState* signals_state) override; | 67 HandleSignalsState* signals_state) override; |
60 void RemoveAwakable(Awakable* awakable, | 68 void RemoveAwakable(Awakable* awakable, |
61 HandleSignalsState* signals_state) override; | 69 HandleSignalsState* signals_state) override; |
62 void StartSerialize(uint32_t* num_bytes, | 70 void StartSerialize(uint32_t* num_bytes, |
63 uint32_t* num_ports, | 71 uint32_t* num_ports, |
64 uint32_t* num_handles) override; | 72 uint32_t* num_handles) override; |
65 bool EndSerialize(void* destination, | 73 bool EndSerialize(void* destination, |
66 ports::PortName* ports, | 74 ports::PortName* ports, |
67 PlatformHandle* handles) override; | 75 PlatformHandle* handles) override; |
68 bool BeginTransit() override; | 76 bool BeginTransit(RequestContext* request_context) override; |
69 void CompleteTransitAndClose() override; | 77 void CompleteTransitAndClose(RequestContext* request_context) override; |
70 void CancelTransit() override; | 78 void CancelTransit(RequestContext* request_context) override; |
71 | 79 |
72 static scoped_refptr<Dispatcher> Deserialize( | 80 static scoped_refptr<Dispatcher> Deserialize( |
73 const void* data, | 81 const void* data, |
74 size_t num_bytes, | 82 size_t num_bytes, |
75 const ports::PortName* ports, | 83 const ports::PortName* ports, |
76 size_t num_ports, | 84 size_t num_ports, |
77 PlatformHandle* handles, | 85 PlatformHandle* handles, |
78 size_t num_handles); | 86 size_t num_handles); |
79 | 87 |
80 private: | 88 private: |
81 class PortObserverThunk; | 89 class PortObserverThunk; |
82 friend class PortObserverThunk; | 90 friend class PortObserverThunk; |
83 | 91 |
84 ~MessagePipeDispatcher() override; | 92 ~MessagePipeDispatcher() override; |
85 | 93 |
86 MojoResult CloseNoLock(); | 94 MojoResult CloseNoLock(RequestContext* request_context); |
87 HandleSignalsState GetHandleSignalsStateNoLock() const; | 95 HandleSignalsState GetHandleSignalsStateNoLock() const; |
88 void OnPortStatusChanged(); | 96 void OnPortStatusChanged(); |
| 97 void NotifyObserversForStateChangeNoLock(RequestContext* request_context); |
89 | 98 |
90 // These are safe to access from any thread without locking. | 99 // These are safe to access from any thread without locking. |
91 NodeController* const node_controller_; | 100 NodeController* const node_controller_; |
92 const ports::PortRef port_; | 101 const ports::PortRef port_; |
93 const uint64_t pipe_id_; | 102 const uint64_t pipe_id_; |
94 const int endpoint_; | 103 const int endpoint_; |
95 | 104 |
96 // Guards access to all the fields below. | 105 // Guards access to all the fields below. |
97 mutable base::Lock signal_lock_; | 106 mutable base::Lock signal_lock_; |
98 | 107 |
99 // This is not the same is |port_transferred_|. It's only held true between | 108 // This is not the same is |port_transferred_|. It's only held true between |
100 // BeginTransit() and Complete/CancelTransit(). | 109 // BeginTransit() and Complete/CancelTransit(). |
101 bool in_transit_ = false; | 110 bool in_transit_ = false; |
102 | 111 |
103 bool port_transferred_ = false; | 112 bool port_transferred_ = false; |
104 bool port_closed_ = false; | 113 bool port_closed_ = false; |
105 AwakableList awakables_; | 114 AwakableList awakables_; |
| 115 WatcherSet watchers_; |
106 | 116 |
107 DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); | 117 DISALLOW_COPY_AND_ASSIGN(MessagePipeDispatcher); |
108 }; | 118 }; |
109 | 119 |
110 } // namespace edk | 120 } // namespace edk |
111 } // namespace mojo | 121 } // namespace mojo |
112 | 122 |
113 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ | 123 #endif // MOJO_EDK_SYSTEM_MESSAGE_PIPE_DISPATCHER_H_ |
OLD | NEW |