1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/system/proxy_message_pipe_endpoint.h"
#include <string.h>
#include "base/containers/hash_tables.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "mojo/system/channel.h"
#include "mojo/system/message_pipe_dispatcher.h"
namespace mojo {
namespace system {
ProxyMessagePipeEndpoint::ProxyMessagePipeEndpoint()
: local_id_(MessageInTransit::kInvalidEndpointId),
remote_id_(MessageInTransit::kInvalidEndpointId),
is_open_(true),
is_peer_open_(true) {
}
ProxyMessagePipeEndpoint::~ProxyMessagePipeEndpoint() {
DCHECK(!is_running());
DCHECK(!is_attached());
AssertConsistentState();
DCHECK(paused_message_queue_.empty());
}
void ProxyMessagePipeEndpoint::Close() {
DCHECK(is_open_);
is_open_ = false;
DCHECK(is_attached());
channel_->DetachMessagePipeEndpoint(local_id_);
channel_ = NULL;
local_id_ = MessageInTransit::kInvalidEndpointId;
remote_id_ = MessageInTransit::kInvalidEndpointId;
STLDeleteElements(&paused_message_queue_);
}
void ProxyMessagePipeEndpoint::OnPeerClose() {
DCHECK(is_open_);
DCHECK(is_peer_open_);
is_peer_open_ = false;
EnqueueMessageInternal(make_scoped_ptr(
new MessageInTransit(MessageInTransit::OWNED_BUFFER,
MessageInTransit::kTypeMessagePipe,
MessageInTransit::kSubtypeMessagePipePeerClosed,
0, 0, NULL)));
}
void ProxyMessagePipeEndpoint::EnqueueMessage(
scoped_ptr<MessageInTransit> message,
std::vector<DispatcherTransport>* transports) {
DCHECK(!transports || !transports->empty());
if (transports)
AttachAndCloseDispatchers(message.get(), transports);
EnqueueMessageInternal(message.Pass());
}
void ProxyMessagePipeEndpoint::Attach(scoped_refptr<Channel> channel,
MessageInTransit::EndpointId local_id) {
DCHECK(channel.get());
DCHECK_NE(local_id, MessageInTransit::kInvalidEndpointId);
DCHECK(!is_attached());
AssertConsistentState();
channel_ = channel;
local_id_ = local_id;
AssertConsistentState();
}
void ProxyMessagePipeEndpoint::Run(MessageInTransit::EndpointId remote_id) {
// Assertions about arguments:
DCHECK_NE(remote_id, MessageInTransit::kInvalidEndpointId);
// Assertions about current state:
DCHECK(is_attached());
DCHECK(!is_running());
AssertConsistentState();
remote_id_ = remote_id;
AssertConsistentState();
for (std::deque<MessageInTransit*>::iterator it =
paused_message_queue_.begin(); it != paused_message_queue_.end();
++it)
EnqueueMessageInternal(make_scoped_ptr(*it));
paused_message_queue_.clear();
}
void ProxyMessagePipeEndpoint::AttachAndCloseDispatchers(
MessageInTransit* message,
std::vector<DispatcherTransport>* transports) {
DCHECK(transports);
DCHECK(!transports->empty());
// TODO(vtl)
LOG(ERROR) << "Sending handles over remote message pipes not yet supported "
"(closing sent handles)";
for (size_t i = 0; i < transports->size(); i++)
(*transports)[i].Close();
}
// Note: We may have to enqueue messages even when our (local) peer isn't open
// -- it may have been written to and closed immediately, before we were ready.
// This case is handled in |Run()| (which will call us).
void ProxyMessagePipeEndpoint::EnqueueMessageInternal(
scoped_ptr<MessageInTransit> message) {
DCHECK(is_open_);
if (is_running()) {
message->set_source_id(local_id_);
message->set_destination_id(remote_id_);
// If it fails at this point, the message gets dropped. (This is no
// different from any other in-transit errors.)
// Note: |WriteMessage()| will destroy the message even on failure.
if (!channel_->WriteMessage(message.Pass()))
LOG(WARNING) << "Failed to write message to channel";
} else {
paused_message_queue_.push_back(message.release());
}
}
#ifndef NDEBUG
void ProxyMessagePipeEndpoint::AssertConsistentState() const {
if (is_attached()) {
DCHECK_NE(local_id_, MessageInTransit::kInvalidEndpointId);
} else { // Not attached.
DCHECK_EQ(local_id_, MessageInTransit::kInvalidEndpointId);
DCHECK_EQ(remote_id_, MessageInTransit::kInvalidEndpointId);
}
}
#endif
} // namespace system
} // namespace mojo
|