| 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/common/message_pump_mojo.h" | 5 #include "mojo/common/message_pump_mojo.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "mojo/common/message_pump_mojo_handler.h" | 13 #include "mojo/common/message_pump_mojo_handler.h" |
| 14 #include "mojo/common/time_helper.h" | 14 #include "mojo/common/time_helper.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace common { | 17 namespace common { |
| 18 | 18 |
| 19 // State needed for one iteration of WaitMany. The first handle and flags | 19 // State needed for one iteration of WaitMany. The first handle and flags |
| 20 // corresponds to that of the control pipe. | 20 // corresponds to that of the control pipe. |
| 21 struct MessagePumpMojo::WaitState { | 21 struct MessagePumpMojo::WaitState { |
| 22 std::vector<Handle> handles; | 22 std::vector<Handle> handles; |
| 23 std::vector<MojoWaitFlags> wait_flags; | 23 std::vector<MojoHandleSignals> wait_signals; |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 struct MessagePumpMojo::RunState { | 26 struct MessagePumpMojo::RunState { |
| 27 RunState() : should_quit(false) { | 27 RunState() : should_quit(false) { |
| 28 CreateMessagePipe(NULL, &read_handle, &write_handle); | 28 CreateMessagePipe(NULL, &read_handle, &write_handle); |
| 29 } | 29 } |
| 30 | 30 |
| 31 base::TimeTicks delayed_work_time; | 31 base::TimeTicks delayed_work_time; |
| 32 | 32 |
| 33 // Used to wake up WaitForWork(). | 33 // Used to wake up WaitForWork(). |
| 34 ScopedMessagePipeHandle read_handle; | 34 ScopedMessagePipeHandle read_handle; |
| 35 ScopedMessagePipeHandle write_handle; | 35 ScopedMessagePipeHandle write_handle; |
| 36 | 36 |
| 37 bool should_quit; | 37 bool should_quit; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 MessagePumpMojo::MessagePumpMojo() : run_state_(NULL), next_handler_id_(0) { | 40 MessagePumpMojo::MessagePumpMojo() : run_state_(NULL), next_handler_id_(0) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 MessagePumpMojo::~MessagePumpMojo() { | 43 MessagePumpMojo::~MessagePumpMojo() { |
| 44 } | 44 } |
| 45 | 45 |
| 46 // static | 46 // static |
| 47 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 47 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
| 48 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 48 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 51 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, |
| 52 const Handle& handle, | 52 const Handle& handle, |
| 53 MojoWaitFlags wait_flags, | 53 MojoHandleSignals wait_signals, |
| 54 base::TimeTicks deadline) { | 54 base::TimeTicks deadline) { |
| 55 DCHECK(handler); | 55 DCHECK(handler); |
| 56 DCHECK(handle.is_valid()); | 56 DCHECK(handle.is_valid()); |
| 57 // Assume it's an error if someone tries to reregister an existing handle. | 57 // Assume it's an error if someone tries to reregister an existing handle. |
| 58 DCHECK_EQ(0u, handlers_.count(handle)); | 58 DCHECK_EQ(0u, handlers_.count(handle)); |
| 59 Handler handler_data; | 59 Handler handler_data; |
| 60 handler_data.handler = handler; | 60 handler_data.handler = handler; |
| 61 handler_data.wait_flags = wait_flags; | 61 handler_data.wait_signals = wait_signals; |
| 62 handler_data.deadline = deadline; | 62 handler_data.deadline = deadline; |
| 63 handler_data.id = next_handler_id_++; | 63 handler_data.id = next_handler_id_++; |
| 64 handlers_[handle] = handler_data; | 64 handlers_[handle] = handler_data; |
| 65 } | 65 } |
| 66 | 66 |
| 67 void MessagePumpMojo::RemoveHandler(const Handle& handle) { | 67 void MessagePumpMojo::RemoveHandler(const Handle& handle) { |
| 68 handlers_.erase(handle); | 68 handlers_.erase(handle); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void MessagePumpMojo::Run(Delegate* delegate) { | 71 void MessagePumpMojo::Run(Delegate* delegate) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 more_work_is_plausible = delegate->DoIdleWork(); | 135 more_work_is_plausible = delegate->DoIdleWork(); |
| 136 if (run_state->should_quit) | 136 if (run_state->should_quit) |
| 137 break; | 137 break; |
| 138 } | 138 } |
| 139 } | 139 } |
| 140 | 140 |
| 141 void MessagePumpMojo::DoInternalWork(const RunState& run_state, bool block) { | 141 void MessagePumpMojo::DoInternalWork(const RunState& run_state, bool block) { |
| 142 const MojoDeadline deadline = block ? GetDeadlineForWait(run_state) : 0; | 142 const MojoDeadline deadline = block ? GetDeadlineForWait(run_state) : 0; |
| 143 const WaitState wait_state = GetWaitState(run_state); | 143 const WaitState wait_state = GetWaitState(run_state); |
| 144 const MojoResult result = | 144 const MojoResult result = |
| 145 WaitMany(wait_state.handles, wait_state.wait_flags, deadline); | 145 WaitMany(wait_state.handles, wait_state.wait_signals, deadline); |
| 146 if (result == 0) { | 146 if (result == 0) { |
| 147 // Control pipe was written to. | 147 // Control pipe was written to. |
| 148 uint32_t num_bytes = 0; | 148 uint32_t num_bytes = 0; |
| 149 ReadMessageRaw(run_state.read_handle.get(), NULL, &num_bytes, NULL, NULL, | 149 ReadMessageRaw(run_state.read_handle.get(), NULL, &num_bytes, NULL, NULL, |
| 150 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); | 150 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); |
| 151 } else if (result > 0) { | 151 } else if (result > 0) { |
| 152 const size_t index = static_cast<size_t>(result); | 152 const size_t index = static_cast<size_t>(result); |
| 153 DCHECK(handlers_.find(wait_state.handles[index]) != handlers_.end()); | 153 DCHECK(handlers_.find(wait_state.handles[index]) != handlers_.end()); |
| 154 handlers_[wait_state.handles[index]].handler->OnHandleReady( | 154 handlers_[wait_state.handles[index]].handler->OnHandleReady( |
| 155 wait_state.handles[index]); | 155 wait_state.handles[index]); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 182 handlers_[i->first].id == i->second.id) { | 182 handlers_[i->first].id == i->second.id) { |
| 183 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); | 183 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { | 188 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { |
| 189 // TODO(sky): deal with control pipe going bad. | 189 // TODO(sky): deal with control pipe going bad. |
| 190 for (size_t i = 1; i < wait_state.handles.size(); ++i) { | 190 for (size_t i = 1; i < wait_state.handles.size(); ++i) { |
| 191 const MojoResult result = | 191 const MojoResult result = |
| 192 Wait(wait_state.handles[i], wait_state.wait_flags[i], 0); | 192 Wait(wait_state.handles[i], wait_state.wait_signals[i], 0); |
| 193 if (result == MOJO_RESULT_INVALID_ARGUMENT || | 193 if (result == MOJO_RESULT_INVALID_ARGUMENT || |
| 194 result == MOJO_RESULT_FAILED_PRECONDITION || | 194 result == MOJO_RESULT_FAILED_PRECONDITION || |
| 195 result == MOJO_RESULT_CANCELLED) { | 195 result == MOJO_RESULT_CANCELLED) { |
| 196 // Remove the handle first, this way if OnHandleError() tries to remove | 196 // Remove the handle first, this way if OnHandleError() tries to remove |
| 197 // the handle our iterator isn't invalidated. | 197 // the handle our iterator isn't invalidated. |
| 198 DCHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); | 198 DCHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); |
| 199 MessagePumpMojoHandler* handler = | 199 MessagePumpMojoHandler* handler = |
| 200 handlers_[wait_state.handles[i]].handler; | 200 handlers_[wait_state.handles[i]].handler; |
| 201 handlers_.erase(wait_state.handles[i]); | 201 handlers_.erase(wait_state.handles[i]); |
| 202 handler->OnHandleError(wait_state.handles[i], result); | 202 handler->OnHandleError(wait_state.handles[i], result); |
| 203 return; | 203 return; |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 } | 206 } |
| 207 | 207 |
| 208 void MessagePumpMojo::SignalControlPipe(const RunState& run_state) { | 208 void MessagePumpMojo::SignalControlPipe(const RunState& run_state) { |
| 209 // TODO(sky): deal with error? | 209 // TODO(sky): deal with error? |
| 210 WriteMessageRaw(run_state.write_handle.get(), NULL, 0, NULL, 0, | 210 WriteMessageRaw(run_state.write_handle.get(), NULL, 0, NULL, 0, |
| 211 MOJO_WRITE_MESSAGE_FLAG_NONE); | 211 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 212 } | 212 } |
| 213 | 213 |
| 214 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( | 214 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( |
| 215 const RunState& run_state) const { | 215 const RunState& run_state) const { |
| 216 WaitState wait_state; | 216 WaitState wait_state; |
| 217 wait_state.handles.push_back(run_state.read_handle.get()); | 217 wait_state.handles.push_back(run_state.read_handle.get()); |
| 218 wait_state.wait_flags.push_back(MOJO_WAIT_FLAG_READABLE); | 218 wait_state.wait_signals.push_back(MOJO_WAIT_FLAG_READABLE); |
| 219 | 219 |
| 220 for (HandleToHandler::const_iterator i = handlers_.begin(); | 220 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 221 i != handlers_.end(); ++i) { | 221 i != handlers_.end(); ++i) { |
| 222 wait_state.handles.push_back(i->first); | 222 wait_state.handles.push_back(i->first); |
| 223 wait_state.wait_flags.push_back(i->second.wait_flags); | 223 wait_state.wait_signals.push_back(i->second.wait_signals); |
| 224 } | 224 } |
| 225 return wait_state; | 225 return wait_state; |
| 226 } | 226 } |
| 227 | 227 |
| 228 MojoDeadline MessagePumpMojo::GetDeadlineForWait( | 228 MojoDeadline MessagePumpMojo::GetDeadlineForWait( |
| 229 const RunState& run_state) const { | 229 const RunState& run_state) const { |
| 230 base::TimeTicks min_time = run_state.delayed_work_time; | 230 base::TimeTicks min_time = run_state.delayed_work_time; |
| 231 for (HandleToHandler::const_iterator i = handlers_.begin(); | 231 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 232 i != handlers_.end(); ++i) { | 232 i != handlers_.end(); ++i) { |
| 233 if (min_time.is_null() && i->second.deadline < min_time) | 233 if (min_time.is_null() && i->second.deadline < min_time) |
| 234 min_time = i->second.deadline; | 234 min_time = i->second.deadline; |
| 235 } | 235 } |
| 236 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : | 236 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : |
| 237 std::max(static_cast<MojoDeadline>(0), | 237 std::max(static_cast<MojoDeadline>(0), |
| 238 static_cast<MojoDeadline>( | 238 static_cast<MojoDeadline>( |
| 239 (min_time - internal::NowTicks()).InMicroseconds())); | 239 (min_time - internal::NowTicks()).InMicroseconds())); |
| 240 } | 240 } |
| 241 | 241 |
| 242 } // namespace common | 242 } // namespace common |
| 243 } // namespace mojo | 243 } // namespace mojo |
| OLD | NEW |