| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ipc/ipc_sync_message_filter.h" | 5 #include "ipc/ipc_sync_message_filter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 160 |
| 161 { | 161 { |
| 162 base::AutoLock auto_lock(lock_); | 162 base::AutoLock auto_lock(lock_); |
| 163 delete pending_message.deserializer; | 163 delete pending_message.deserializer; |
| 164 pending_sync_messages_.erase(&pending_message); | 164 pending_sync_messages_.erase(&pending_message); |
| 165 } | 165 } |
| 166 | 166 |
| 167 return pending_message.send_result; | 167 return pending_message.send_result; |
| 168 } | 168 } |
| 169 | 169 |
| 170 void SyncMessageFilter::OnFilterAdded(Sender* sender) { | 170 void SyncMessageFilter::OnFilterAdded(Channel* channel) { |
| 171 std::vector<std::unique_ptr<Message>> pending_messages; | 171 std::vector<std::unique_ptr<Message>> pending_messages; |
| 172 { | 172 { |
| 173 base::AutoLock auto_lock(lock_); | 173 base::AutoLock auto_lock(lock_); |
| 174 sender_ = sender; | 174 channel_ = channel; |
| 175 io_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 175 io_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 176 shutdown_watcher_.StartWatching( | 176 shutdown_watcher_.StartWatching( |
| 177 shutdown_event_, | 177 shutdown_event_, |
| 178 base::Bind(&SyncMessageFilter::OnShutdownEventSignaled, this)); | 178 base::Bind(&SyncMessageFilter::OnShutdownEventSignaled, this)); |
| 179 io_message_loop_observer_->StartOnIOThread(); | 179 io_message_loop_observer_->StartOnIOThread(); |
| 180 std::swap(pending_messages_, pending_messages); | 180 std::swap(pending_messages_, pending_messages); |
| 181 } | 181 } |
| 182 for (auto& msg : pending_messages) | 182 for (auto& msg : pending_messages) |
| 183 SendOnIOThread(msg.release()); | 183 SendOnIOThread(msg.release()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void SyncMessageFilter::OnChannelError() { | 186 void SyncMessageFilter::OnChannelError() { |
| 187 base::AutoLock auto_lock(lock_); | 187 base::AutoLock auto_lock(lock_); |
| 188 sender_ = NULL; | 188 channel_ = nullptr; |
| 189 shutdown_watcher_.StopWatching(); | 189 shutdown_watcher_.StopWatching(); |
| 190 SignalAllEvents(); | 190 SignalAllEvents(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void SyncMessageFilter::OnChannelClosing() { | 193 void SyncMessageFilter::OnChannelClosing() { |
| 194 base::AutoLock auto_lock(lock_); | 194 base::AutoLock auto_lock(lock_); |
| 195 sender_ = NULL; | 195 channel_ = nullptr; |
| 196 shutdown_watcher_.StopWatching(); | 196 shutdown_watcher_.StopWatching(); |
| 197 SignalAllEvents(); | 197 SignalAllEvents(); |
| 198 } | 198 } |
| 199 | 199 |
| 200 bool SyncMessageFilter::OnMessageReceived(const Message& message) { | 200 bool SyncMessageFilter::OnMessageReceived(const Message& message) { |
| 201 base::AutoLock auto_lock(lock_); | 201 base::AutoLock auto_lock(lock_); |
| 202 for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin(); | 202 for (PendingSyncMessages::iterator iter = pending_sync_messages_.begin(); |
| 203 iter != pending_sync_messages_.end(); ++iter) { | 203 iter != pending_sync_messages_.end(); ++iter) { |
| 204 if (SyncMessage::IsMessageReplyTo(message, (*iter)->id)) { | 204 if (SyncMessage::IsMessageReplyTo(message, (*iter)->id)) { |
| 205 if (!message.is_reply_error()) { | 205 if (!message.is_reply_error()) { |
| 206 (*iter)->send_result = | 206 (*iter)->send_result = |
| 207 (*iter)->deserializer->SerializeOutputParameters(message); | 207 (*iter)->deserializer->SerializeOutputParameters(message); |
| 208 } | 208 } |
| 209 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), | 209 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 210 "SyncMessageFilter::OnMessageReceived", | 210 "SyncMessageFilter::OnMessageReceived", |
| 211 (*iter)->done_event); | 211 (*iter)->done_event); |
| 212 (*iter)->done_event->Signal(); | 212 (*iter)->done_event->Signal(); |
| 213 return true; | 213 return true; |
| 214 } | 214 } |
| 215 } | 215 } |
| 216 | 216 |
| 217 return false; | 217 return false; |
| 218 } | 218 } |
| 219 | 219 |
| 220 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event) | 220 SyncMessageFilter::SyncMessageFilter(base::WaitableEvent* shutdown_event) |
| 221 : sender_(NULL), | 221 : channel_(nullptr), |
| 222 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 222 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 223 shutdown_event_(shutdown_event), | 223 shutdown_event_(shutdown_event), |
| 224 weak_factory_(this) { | 224 weak_factory_(this) { |
| 225 io_message_loop_observer_ = new IOMessageLoopObserver( | 225 io_message_loop_observer_ = new IOMessageLoopObserver( |
| 226 weak_factory_.GetWeakPtr(), listener_task_runner_); | 226 weak_factory_.GetWeakPtr(), listener_task_runner_); |
| 227 } | 227 } |
| 228 | 228 |
| 229 SyncMessageFilter::~SyncMessageFilter() { | 229 SyncMessageFilter::~SyncMessageFilter() { |
| 230 io_message_loop_observer_->Stop(); | 230 io_message_loop_observer_->Stop(); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void SyncMessageFilter::SendOnIOThread(Message* message) { | 233 void SyncMessageFilter::SendOnIOThread(Message* message) { |
| 234 if (sender_) { | 234 if (channel_) { |
| 235 sender_->Send(message); | 235 channel_->Send(message); |
| 236 return; | 236 return; |
| 237 } | 237 } |
| 238 | 238 |
| 239 if (message->is_sync()) { | 239 if (message->is_sync()) { |
| 240 // We don't know which thread sent it, but it doesn't matter, just signal | 240 // We don't know which thread sent it, but it doesn't matter, just signal |
| 241 // them all. | 241 // them all. |
| 242 base::AutoLock auto_lock(lock_); | 242 base::AutoLock auto_lock(lock_); |
| 243 SignalAllEvents(); | 243 SignalAllEvents(); |
| 244 } | 244 } |
| 245 | 245 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 264 | 264 |
| 265 void SyncMessageFilter::OnIOMessageLoopDestroyed() { | 265 void SyncMessageFilter::OnIOMessageLoopDestroyed() { |
| 266 // Since we use an async WaitableEventWatcher to watch the shutdown event | 266 // Since we use an async WaitableEventWatcher to watch the shutdown event |
| 267 // from the IO thread, we can't forward the shutdown signal after the IO | 267 // from the IO thread, we can't forward the shutdown signal after the IO |
| 268 // message loop is destroyed. Since that destruction indicates shutdown | 268 // message loop is destroyed. Since that destruction indicates shutdown |
| 269 // anyway, we manually signal the shutdown event in this case. | 269 // anyway, we manually signal the shutdown event in this case. |
| 270 shutdown_mojo_event_.Signal(); | 270 shutdown_mojo_event_.Signal(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 } // namespace IPC | 273 } // namespace IPC |
| OLD | NEW |