| 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 #include "mojo/edk/system/routed_raw_channel.h" | 5 #include "mojo/edk/system/routed_raw_channel.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "mojo/edk/embedder/embedder_internal.h" | 11 #include "mojo/edk/embedder/embedder_internal.h" |
| 10 | 12 |
| 11 namespace mojo { | 13 namespace mojo { |
| 12 namespace edk { | 14 namespace edk { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 const uint64_t kInternalRouteId = 0; | 17 const uint64_t kInternalRouteId = 0; |
| 16 } | 18 } |
| 17 | 19 |
| 18 RoutedRawChannel::PendingMessage::PendingMessage() { | 20 RoutedRawChannel::PendingMessage::PendingMessage() { |
| 19 } | 21 } |
| 20 | 22 |
| 21 RoutedRawChannel::PendingMessage::~PendingMessage() { | 23 RoutedRawChannel::PendingMessage::~PendingMessage() { |
| 22 } | 24 } |
| 23 | 25 |
| 24 RoutedRawChannel::RoutedRawChannel( | 26 RoutedRawChannel::RoutedRawChannel( |
| 25 ScopedPlatformHandle handle, | 27 ScopedPlatformHandle handle, |
| 26 const base::Callback<void(RoutedRawChannel*)>& destruct_callback) | 28 const base::Callback<void(RoutedRawChannel*)>& destruct_callback) |
| 27 : channel_(RawChannel::Create(handle.Pass())), | 29 : channel_(RawChannel::Create(std::move(handle))), |
| 28 destruct_callback_(destruct_callback) { | 30 destruct_callback_(destruct_callback) { |
| 29 internal::g_io_thread_task_runner->PostTask( | 31 internal::g_io_thread_task_runner->PostTask( |
| 30 FROM_HERE, | 32 FROM_HERE, |
| 31 base::Bind(&RawChannel::Init, base::Unretained(channel_), this)); | 33 base::Bind(&RawChannel::Init, base::Unretained(channel_), this)); |
| 32 internal::g_io_thread_task_runner->PostTask( | 34 internal::g_io_thread_task_runner->PostTask( |
| 33 FROM_HERE, | 35 FROM_HERE, |
| 34 base::Bind(&RawChannel::EnsureLazyInitialized, | 36 base::Bind(&RawChannel::EnsureLazyInitialized, |
| 35 base::Unretained(channel_))); | 37 base::Unretained(channel_))); |
| 36 } | 38 } |
| 37 | 39 |
| 38 void RoutedRawChannel::AddRoute(uint64_t route_id, | 40 void RoutedRawChannel::AddRoute(uint64_t route_id, |
| 39 RawChannel::Delegate* delegate) { | 41 RawChannel::Delegate* delegate) { |
| 40 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); | 42 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); |
| 41 CHECK_NE(route_id, kInternalRouteId) << kInternalRouteId << " is reserved"; | 43 CHECK_NE(route_id, kInternalRouteId) << kInternalRouteId << " is reserved"; |
| 42 CHECK(routes_.find(route_id) == routes_.end()); | 44 CHECK(routes_.find(route_id) == routes_.end()); |
| 43 routes_[route_id] = delegate; | 45 routes_[route_id] = delegate; |
| 44 | 46 |
| 45 for (size_t i = 0; i < pending_messages_.size();) { | 47 for (size_t i = 0; i < pending_messages_.size();) { |
| 46 MessageInTransit::View view(pending_messages_[i]->message.size(), | 48 MessageInTransit::View view(pending_messages_[i]->message.size(), |
| 47 &pending_messages_[i]->message[0]); | 49 &pending_messages_[i]->message[0]); |
| 48 if (view.route_id() == route_id) { | 50 if (view.route_id() == route_id) { |
| 49 delegate->OnReadMessage(view, pending_messages_[i]->handles.Pass()); | 51 delegate->OnReadMessage(view, std::move(pending_messages_[i]->handles)); |
| 50 pending_messages_.erase(pending_messages_.begin() + i); | 52 pending_messages_.erase(pending_messages_.begin() + i); |
| 51 } else { | 53 } else { |
| 52 ++i; | 54 ++i; |
| 53 } | 55 } |
| 54 } | 56 } |
| 55 | 57 |
| 56 if (close_routes_.find(route_id) != close_routes_.end()) | 58 if (close_routes_.find(route_id) != close_routes_.end()) |
| 57 delegate->OnError(ERROR_READ_SHUTDOWN); | 59 delegate->OnError(ERROR_READ_SHUTDOWN); |
| 58 } | 60 } |
| 59 | 61 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 70 if (close_routes_.find(route_id) != close_routes_.end()) { | 72 if (close_routes_.find(route_id) != close_routes_.end()) { |
| 71 close_routes_.erase(route_id); | 73 close_routes_.erase(route_id); |
| 72 } else if (channel_) { | 74 } else if (channel_) { |
| 73 // Default route id of 0 to reach the other side's RoutedRawChannel. | 75 // Default route id of 0 to reach the other side's RoutedRawChannel. |
| 74 char message_data[sizeof(uint64_t)]; | 76 char message_data[sizeof(uint64_t)]; |
| 75 memcpy(&message_data[0], &route_id, sizeof(uint64_t)); | 77 memcpy(&message_data[0], &route_id, sizeof(uint64_t)); |
| 76 scoped_ptr<MessageInTransit> message(new MessageInTransit( | 78 scoped_ptr<MessageInTransit> message(new MessageInTransit( |
| 77 MessageInTransit::Type::MESSAGE, arraysize(message_data), | 79 MessageInTransit::Type::MESSAGE, arraysize(message_data), |
| 78 message_data)); | 80 message_data)); |
| 79 message->set_route_id(kInternalRouteId); | 81 message->set_route_id(kInternalRouteId); |
| 80 channel_->WriteMessage(message.Pass()); | 82 channel_->WriteMessage(std::move(message)); |
| 81 } | 83 } |
| 82 | 84 |
| 83 if (!channel_ && routes_.empty()) { | 85 if (!channel_ && routes_.empty()) { |
| 84 // PostTask to avoid reentrancy since the broker might be calling us. | 86 // PostTask to avoid reentrancy since the broker might be calling us. |
| 85 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 87 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 86 } | 88 } |
| 87 } | 89 } |
| 88 | 90 |
| 89 RoutedRawChannel::~RoutedRawChannel() { | 91 RoutedRawChannel::~RoutedRawChannel() { |
| 90 DCHECK(!channel_); | 92 DCHECK(!channel_); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 114 } | 116 } |
| 115 close_routes_.insert(closed_route); | 117 close_routes_.insert(closed_route); |
| 116 if (routes_.find(closed_route) == routes_.end()) | 118 if (routes_.find(closed_route) == routes_.end()) |
| 117 return; // This side hasn't connected yet. | 119 return; // This side hasn't connected yet. |
| 118 | 120 |
| 119 routes_[closed_route]->OnError(ERROR_READ_SHUTDOWN); | 121 routes_[closed_route]->OnError(ERROR_READ_SHUTDOWN); |
| 120 return; | 122 return; |
| 121 } | 123 } |
| 122 | 124 |
| 123 if (routes_.find(route_id) != routes_.end()) { | 125 if (routes_.find(route_id) != routes_.end()) { |
| 124 routes_[route_id]->OnReadMessage(message_view, platform_handles.Pass()); | 126 routes_[route_id]->OnReadMessage(message_view, std::move(platform_handles)); |
| 125 } else { | 127 } else { |
| 126 scoped_ptr<PendingMessage> msg(new PendingMessage); | 128 scoped_ptr<PendingMessage> msg(new PendingMessage); |
| 127 msg->message.resize(message_view.total_size()); | 129 msg->message.resize(message_view.total_size()); |
| 128 memcpy(&msg->message[0], message_view.main_buffer(), | 130 memcpy(&msg->message[0], message_view.main_buffer(), |
| 129 message_view.total_size()); | 131 message_view.total_size()); |
| 130 msg->handles = platform_handles.Pass(); | 132 msg->handles = std::move(platform_handles); |
| 131 pending_messages_.push_back(msg.Pass()); | 133 pending_messages_.push_back(std::move(msg)); |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 | 136 |
| 135 void RoutedRawChannel::OnError(Error error) { | 137 void RoutedRawChannel::OnError(Error error) { |
| 136 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); | 138 DCHECK(internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()); |
| 137 | 139 |
| 138 // Note: we must ensure we don't call RawChannel::Shutdown until after we've | 140 // Note: we must ensure we don't call RawChannel::Shutdown until after we've |
| 139 // called OnError on each route's delegate. | 141 // called OnError on each route's delegate. |
| 140 for (auto it = routes_.begin(); it != routes_.end();) { | 142 for (auto it = routes_.begin(); it != routes_.end();) { |
| 141 // The delegate might call RemoveRoute in their OnError implementation which | 143 // The delegate might call RemoveRoute in their OnError implementation which |
| 142 // would invalidate |it|. So increment it first. | 144 // would invalidate |it|. So increment it first. |
| 143 auto cur_it = it++; | 145 auto cur_it = it++; |
| 144 cur_it->second->OnError(error); | 146 cur_it->second->OnError(error); |
| 145 } | 147 } |
| 146 | 148 |
| 147 if (routes_.empty()) { | 149 if (routes_.empty()) { |
| 148 channel_->Shutdown(); | 150 channel_->Shutdown(); |
| 149 channel_ = nullptr; | 151 channel_ = nullptr; |
| 150 delete this; | 152 delete this; |
| 151 return; | 153 return; |
| 152 } | 154 } |
| 153 } | 155 } |
| 154 | 156 |
| 155 } // namespace edk | 157 } // namespace edk |
| 156 } // namespace mojo | 158 } // namespace mojo |
| OLD | NEW |