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 #include "mojo/system/proxy_message_pipe_endpoint.h" | 5 #include "mojo/system/proxy_message_pipe_endpoint.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "mojo/system/channel_endpoint.h" | 10 #include "mojo/system/channel_endpoint.h" |
11 #include "mojo/system/local_message_pipe_endpoint.h" | 11 #include "mojo/system/local_message_pipe_endpoint.h" |
12 #include "mojo/system/message_pipe_dispatcher.h" | 12 #include "mojo/system/message_pipe_dispatcher.h" |
13 | 13 |
14 namespace mojo { | 14 namespace mojo { |
15 namespace system { | 15 namespace system { |
16 | 16 |
17 ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint( | 17 ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint( |
18 ChannelEndpoint* channel_endpoint) | 18 ChannelEndpoint* channel_endpoint) |
19 : channel_endpoint_(channel_endpoint), | 19 : channel_endpoint_(channel_endpoint), |
20 is_running_(false), | 20 is_running_(false), |
21 is_peer_open_(true) { | 21 is_peer_open_(true) { |
22 } | 22 } |
23 | 23 |
24 ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint( | 24 ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint( |
25 ChannelEndpoint* channel_endpoint, | 25 ChannelEndpoint* channel_endpoint, |
26 LocalMessagePipeEndpoint* local_message_pipe_endpoint, | |
27 bool is_peer_open) | 26 bool is_peer_open) |
28 : channel_endpoint_(channel_endpoint), | 27 : channel_endpoint_(channel_endpoint), |
29 is_running_(false), | 28 is_running_(false), |
30 is_peer_open_(is_peer_open) { | 29 is_peer_open_(is_peer_open) { |
31 paused_message_queue_.Swap(local_message_pipe_endpoint->message_queue()); | |
32 local_message_pipe_endpoint->Close(); | |
33 } | 30 } |
34 | 31 |
35 ProxyMessagePipeEndpoint::~ProxyMessagePipeEndpoint() { | 32 ProxyMessagePipeEndpoint::~ProxyMessagePipeEndpoint() { |
36 DCHECK(!is_running()); | 33 DCHECK(!is_running()); |
37 DCHECK(!is_attached()); | 34 DCHECK(!is_attached()); |
38 DCHECK(paused_message_queue_.IsEmpty()); | |
39 } | 35 } |
40 | 36 |
41 MessagePipeEndpoint::Type ProxyMessagePipeEndpoint::GetType() const { | 37 MessagePipeEndpoint::Type ProxyMessagePipeEndpoint::GetType() const { |
42 return kTypeProxy; | 38 return kTypeProxy; |
43 } | 39 } |
44 | 40 |
45 bool ProxyMessagePipeEndpoint::OnPeerClose() { | 41 bool ProxyMessagePipeEndpoint::OnPeerClose() { |
46 DCHECK(is_peer_open_); | 42 DCHECK(is_peer_open_); |
47 | 43 |
48 is_peer_open_ = false; | 44 is_peer_open_ = false; |
49 | 45 |
50 // If our outgoing message queue isn't empty, we shouldn't be destroyed yet. | |
51 if (!paused_message_queue_.IsEmpty()) | |
52 return true; | |
53 | |
54 if (is_attached()) { | 46 if (is_attached()) { |
55 if (!is_running()) { | 47 if (!is_running()) { |
56 // If we're not running yet, we can't be destroyed yet, because we're | 48 // If we're not running yet, we can't be destroyed yet, because we're |
57 // still waiting for the "run" message from the other side. | 49 // still waiting for the "run" message from the other side. |
58 return true; | 50 return true; |
59 } | 51 } |
60 | 52 |
61 Detach(); | 53 Detach(); |
62 } | 54 } |
63 | 55 |
64 return false; | 56 return false; |
65 } | 57 } |
66 | 58 |
67 // Note: We may have to enqueue messages even when our (local) peer isn't open | 59 // Note: We may have to enqueue messages even when our (local) peer isn't open |
68 // -- it may have been written to and closed immediately, before we were ready. | 60 // -- it may have been written to and closed immediately, before we were ready. |
69 // This case is handled in |Run()| (which will call us). | 61 // This case is handled in |Run()| (which will call us). |
70 void ProxyMessagePipeEndpoint::EnqueueMessage( | 62 void ProxyMessagePipeEndpoint::EnqueueMessage( |
71 scoped_ptr<MessageInTransit> message) { | 63 scoped_ptr<MessageInTransit> message) { |
72 if (is_running()) { | 64 DCHECK(channel_endpoint_.get()); |
73 DCHECK(channel_endpoint_.get()); | 65 LOG_IF(WARNING, !channel_endpoint_->EnqueueMessage(message.Pass())) |
74 LOG_IF(WARNING, !channel_endpoint_->EnqueueMessage(message.Pass())) | 66 << "Failed to write enqueue message to channel"; |
75 << "Failed to write enqueue message to channel"; | |
76 } else { | |
77 paused_message_queue_.AddMessage(message.Pass()); | |
78 } | |
79 } | 67 } |
80 | 68 |
81 bool ProxyMessagePipeEndpoint::Run() { | 69 bool ProxyMessagePipeEndpoint::Run() { |
82 // Assertions about current state: | 70 // Assertions about current state: |
83 DCHECK(is_attached()); | 71 DCHECK(is_attached()); |
84 DCHECK(!is_running()); | 72 DCHECK(!is_running()); |
85 | 73 |
86 is_running_ = true; | 74 is_running_ = true; |
87 | 75 |
88 while (!paused_message_queue_.IsEmpty()) { | |
89 LOG_IF( | |
90 WARNING, | |
91 !channel_endpoint_->EnqueueMessage(paused_message_queue_.GetMessage())) | |
92 << "Failed to write enqueue message to channel"; | |
93 } | |
94 | |
95 if (is_peer_open_) | 76 if (is_peer_open_) |
96 return true; // Stay alive. | 77 return true; // Stay alive. |
97 | 78 |
98 // We were just waiting to die. | 79 // We were just waiting to die. |
99 Detach(); | 80 Detach(); |
100 return false; | 81 return false; |
101 } | 82 } |
102 | 83 |
103 void ProxyMessagePipeEndpoint::OnRemove() { | 84 void ProxyMessagePipeEndpoint::OnRemove() { |
104 Detach(); | 85 Detach(); |
105 } | 86 } |
106 | 87 |
107 void ProxyMessagePipeEndpoint::Detach() { | 88 void ProxyMessagePipeEndpoint::Detach() { |
108 DCHECK(is_attached()); | 89 DCHECK(is_attached()); |
109 | 90 |
110 channel_endpoint_->DetachFromMessagePipe(); | 91 channel_endpoint_->DetachFromMessagePipe(); |
111 channel_endpoint_ = nullptr; | 92 channel_endpoint_ = nullptr; |
112 is_running_ = false; | 93 is_running_ = false; |
113 paused_message_queue_.Clear(); | |
114 } | 94 } |
115 | 95 |
116 } // namespace system | 96 } // namespace system |
117 } // namespace mojo | 97 } // namespace mojo |
OLD | NEW |