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), | |
21 is_peer_open_(true) { | |
22 } | |
23 | |
24 ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint( | |
25 ChannelEndpoint* channel_endpoint, | |
26 bool is_peer_open) | |
27 : channel_endpoint_(channel_endpoint), | |
28 is_running_(false), | |
29 is_peer_open_(is_peer_open) { | |
30 } | 20 } |
31 | 21 |
32 ProxyMessagePipeEndpoint::~ProxyMessagePipeEndpoint() { | 22 ProxyMessagePipeEndpoint::~ProxyMessagePipeEndpoint() { |
33 DCHECK(!is_running()); | |
34 DCHECK(!is_attached()); | 23 DCHECK(!is_attached()); |
35 } | 24 } |
36 | 25 |
37 MessagePipeEndpoint::Type ProxyMessagePipeEndpoint::GetType() const { | 26 MessagePipeEndpoint::Type ProxyMessagePipeEndpoint::GetType() const { |
38 return kTypeProxy; | 27 return kTypeProxy; |
39 } | 28 } |
40 | 29 |
41 bool ProxyMessagePipeEndpoint::OnPeerClose() { | 30 bool ProxyMessagePipeEndpoint::OnPeerClose() { |
42 DCHECK(is_peer_open_); | 31 if (is_attached()) |
43 | |
44 is_peer_open_ = false; | |
45 | |
46 if (is_attached()) { | |
47 if (!is_running()) { | |
48 // If we're not running yet, we can't be destroyed yet, because we're | |
49 // still waiting for the "run" message from the other side. | |
50 return true; | |
51 } | |
52 | |
53 Detach(); | 32 Detach(); |
54 } | |
55 | 33 |
56 return false; | 34 return false; |
57 } | 35 } |
58 | 36 |
59 // Note: We may have to enqueue messages even when our (local) peer isn't open | 37 // Note: We may have to enqueue messages even when our (local) peer isn't open |
60 // -- it may have been written to and closed immediately, before we were ready. | 38 // -- it may have been written to and closed immediately, before we were ready. |
61 // This case is handled in |Run()| (which will call us). | 39 // This case is handled in |Run()| (which will call us). |
62 void ProxyMessagePipeEndpoint::EnqueueMessage( | 40 void ProxyMessagePipeEndpoint::EnqueueMessage( |
63 scoped_ptr<MessageInTransit> message) { | 41 scoped_ptr<MessageInTransit> message) { |
64 DCHECK(channel_endpoint_.get()); | 42 DCHECK(channel_endpoint_.get()); |
65 LOG_IF(WARNING, !channel_endpoint_->EnqueueMessage(message.Pass())) | 43 LOG_IF(WARNING, !channel_endpoint_->EnqueueMessage(message.Pass())) |
66 << "Failed to write enqueue message to channel"; | 44 << "Failed to write enqueue message to channel"; |
67 } | 45 } |
68 | 46 |
69 bool ProxyMessagePipeEndpoint::Run() { | |
70 // Assertions about current state: | |
71 DCHECK(is_attached()); | |
72 DCHECK(!is_running()); | |
73 | |
74 is_running_ = true; | |
75 | |
76 if (is_peer_open_) | |
77 return true; // Stay alive. | |
78 | |
79 // We were just waiting to die. | |
80 Detach(); | |
81 return false; | |
82 } | |
83 | |
84 void ProxyMessagePipeEndpoint::OnRemove() { | 47 void ProxyMessagePipeEndpoint::OnRemove() { |
85 Detach(); | 48 Detach(); |
86 } | 49 } |
87 | 50 |
88 void ProxyMessagePipeEndpoint::Detach() { | 51 void ProxyMessagePipeEndpoint::Detach() { |
89 DCHECK(is_attached()); | 52 DCHECK(is_attached()); |
90 | 53 |
91 channel_endpoint_->DetachFromMessagePipe(); | 54 channel_endpoint_->DetachFromMessagePipe(); |
92 channel_endpoint_ = nullptr; | 55 channel_endpoint_ = nullptr; |
93 is_running_ = false; | |
94 } | 56 } |
95 | 57 |
96 } // namespace system | 58 } // namespace system |
97 } // namespace mojo | 59 } // namespace mojo |
OLD | NEW |