Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: mojo/edk/system/routed_raw_channel.cc

Issue 1488853002: Add multiplexing of message pipes in the new EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tsepez review comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/routed_raw_channel.h ('k') | mojo/edk/system/run_all_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/edk/system/routed_raw_channel.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "mojo/edk/embedder/embedder_internal.h"
10 #include "mojo/edk/system/message_pipe_dispatcher.h"
11
12 namespace mojo {
13 namespace edk {
14
15 namespace {
16 const uint64_t kInternalRoutingId = 0;
17
18 // These are messages sent over our internal routing id above, meant for the
19 // other side's RoutedRawChannel to dispatch.
20 enum InternalMessages {
21 ROUTE_CLOSED = 0,
22 };
23 }
24
25 RoutedRawChannel::PendingMessage::PendingMessage() {
26 }
27
28 RoutedRawChannel::PendingMessage::~PendingMessage() {
29 }
30
31 RoutedRawChannel::RoutedRawChannel(
32 ScopedPlatformHandle handle,
33 const base::Callback<void(RoutedRawChannel*)>& destruct_callback)
34 : channel_(RawChannel::Create(handle.Pass())),
35 destruct_callback_(destruct_callback) {
36 internal::g_io_thread_task_runner->PostTask(
37 FROM_HERE,
38 base::Bind(&RawChannel::Init, base::Unretained(channel_), this));
39 internal::g_io_thread_task_runner->PostTask(
40 FROM_HERE,
41 base::Bind(&RawChannel::EnsureLazyInitialized,
42 base::Unretained(channel_)));
43 }
44
45 void RoutedRawChannel::AddRoute(uint64_t pipe_id, MessagePipeDispatcher* pipe) {
46 CHECK_NE(pipe_id, kInternalRoutingId) << kInternalRoutingId << " is reserved";
47 base::AutoLock auto_lock(lock_);
48 CHECK(routes_.find(pipe_id) == routes_.end());
49 routes_[pipe_id] = pipe;
50
51 for (size_t i = 0; i < pending_messages_.size();) {
52 MessageInTransit::View view(pending_messages_[i]->message.size(),
53 &pending_messages_[i]->message[0]);
54 if (view.route_id() == pipe_id) {
55 pipe->OnReadMessage(view, pending_messages_[i]->handles.Pass());
56 pending_messages_.erase(pending_messages_.begin() + i);
57 } else {
58 ++i;
59 }
60 }
61
62 if (close_routes_.find(pipe_id) != close_routes_.end())
63 pipe->OnError(ERROR_READ_SHUTDOWN);
64 }
65
66 void RoutedRawChannel::RemoveRoute(uint64_t pipe_id,
67 MessagePipeDispatcher* pipe) {
68 base::AutoLock auto_lock(lock_);
69 CHECK(routes_.find(pipe_id) != routes_.end());
70 CHECK_EQ(routes_[pipe_id], pipe);
71 routes_.erase(pipe_id);
72
73 // Only send a message to the other side to close the route if we hadn't
74 // received a close route message. Otherwise they would keep going back and
75 // forth.
76 if (close_routes_.find(pipe_id) != close_routes_.end()) {
77 close_routes_.erase(pipe_id);
78 } else if (channel_) {
79 // Default route id of 0 to reach the other side's RoutedRawChannel.
80 char message_data[sizeof(char) + sizeof(uint64_t)];
81 message_data[0] = ROUTE_CLOSED;
82 memcpy(&message_data[1], &pipe_id, sizeof(uint64_t));
83 scoped_ptr<MessageInTransit> message(new MessageInTransit(
84 MessageInTransit::Type::MESSAGE, arraysize(message_data),
85 message_data));
86 message->set_route_id(kInternalRoutingId);
87 channel_->WriteMessage(message.Pass());
88 }
89
90 if (!channel_ && routes_.empty()) {
91 // PostTask to avoid reentrancy since the broker might be calling us.
92 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
93 }
94 }
95
96 RoutedRawChannel::~RoutedRawChannel() {
97 destruct_callback_.Run(this);
98 }
99
100 void RoutedRawChannel::OnReadMessage(
101 const MessageInTransit::View& message_view,
102 ScopedPlatformHandleVectorPtr platform_handles) {
103 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread());
104 // Note: normally, when a message arrives here we should find a corresponding
105 // entry for the MessagePipeDispatcher with the given route_id. However it is
106 // possible that they just connected, and due to race conditions one side has
107 // connected and sent a message (and even closed) before the other side had a
108 // chance to register with this RoutedRawChannel. In that case, we must buffer
109 // all messages.
110 base::AutoLock auto_lock(lock_);
111 uint64_t route_id = message_view.route_id();
112 if (route_id == kInternalRoutingId) {
113 if (message_view.num_bytes() != sizeof(char) + sizeof(uint64_t)) {
114 NOTREACHED() << "Invalid internal message in RoutedRawChannel.";
115 return;
116 }
117 const char* bytes = static_cast<const char*>(message_view.bytes());
118 if (bytes[0] != ROUTE_CLOSED) {
119 NOTREACHED() << "Unknown internal message in RoutedRawChannel.";
120 return;
121 }
122 uint64_t closed_route = *reinterpret_cast<const uint64_t*>(&bytes[1]);
123 if (close_routes_.find(closed_route) != close_routes_.end()) {
124 NOTREACHED() << "Should only receive one ROUTE_CLOSED per route.";
125 return;
126 }
127 close_routes_.insert(closed_route);
128 if (routes_.find(closed_route) == routes_.end())
129 return; // This side hasn't connected yet.
130
131 routes_[closed_route]->OnError(ERROR_READ_SHUTDOWN);
132 return;
133 }
134
135 if (routes_.find(route_id) != routes_.end()) {
136 routes_[route_id]->OnReadMessage(message_view, platform_handles.Pass());
137 } else {
138 scoped_ptr<PendingMessage> msg(new PendingMessage);
139 msg->message.resize(message_view.total_size());
140 memcpy(&msg->message[0], message_view.main_buffer(),
141 message_view.total_size());
142 msg->handles = platform_handles.Pass();
143 pending_messages_.push_back(msg.Pass());
144 }
145 }
146
147 void RoutedRawChannel::OnError(Error error) {
148 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread());
149 bool destruct = false;
150 {
151 base::AutoLock auto_lock(lock_);
152
153 channel_->Shutdown();
154 channel_ = nullptr;
155 if (routes_.empty()) {
156 destruct = true;
157 } else {
158 for (auto it = routes_.begin(); it != routes_.end(); ++it)
159 it->second->OnError(error);
160 }
161 }
162
163 if (destruct)
164 delete this;
165 }
166
167 } // namespace edk
168 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/routed_raw_channel.h ('k') | mojo/edk/system/run_all_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698