| 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/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "mojo/common/message_pump_mojo_handler.h" | 13 #include "mojo/common/message_pump_mojo_handler.h" |
| 13 #include "mojo/common/time_helper.h" | 14 #include "mojo/common/time_helper.h" |
| 14 | 15 |
| 15 namespace mojo { | 16 namespace mojo { |
| 16 namespace common { | 17 namespace common { |
| 17 | 18 |
| 18 // 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 |
| 19 // corresponds to that of the control pipe. | 20 // corresponds to that of the control pipe. |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 uint32_t num_bytes = 0; | 148 uint32_t num_bytes = 0; |
| 148 ReadMessageRaw(run_state.read_handle.get(), NULL, &num_bytes, NULL, NULL, | 149 ReadMessageRaw(run_state.read_handle.get(), NULL, &num_bytes, NULL, NULL, |
| 149 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); | 150 MOJO_READ_MESSAGE_FLAG_MAY_DISCARD); |
| 150 } else if (result > 0) { | 151 } else if (result > 0) { |
| 151 const size_t index = static_cast<size_t>(result); | 152 const size_t index = static_cast<size_t>(result); |
| 152 DCHECK(handlers_.find(wait_state.handles[index]) != handlers_.end()); | 153 DCHECK(handlers_.find(wait_state.handles[index]) != handlers_.end()); |
| 153 handlers_[wait_state.handles[index]].handler->OnHandleReady( | 154 handlers_[wait_state.handles[index]].handler->OnHandleReady( |
| 154 wait_state.handles[index]); | 155 wait_state.handles[index]); |
| 155 } else { | 156 } else { |
| 156 switch (result) { | 157 switch (result) { |
| 158 case MOJO_RESULT_CANCELLED: |
| 159 case MOJO_RESULT_FAILED_PRECONDITION: |
| 157 case MOJO_RESULT_INVALID_ARGUMENT: | 160 case MOJO_RESULT_INVALID_ARGUMENT: |
| 158 case MOJO_RESULT_FAILED_PRECONDITION: | |
| 159 RemoveFirstInvalidHandle(wait_state); | 161 RemoveFirstInvalidHandle(wait_state); |
| 160 break; | 162 break; |
| 161 case MOJO_RESULT_DEADLINE_EXCEEDED: | 163 case MOJO_RESULT_DEADLINE_EXCEEDED: |
| 162 break; | 164 break; |
| 163 default: | 165 default: |
| 164 NOTREACHED(); | 166 base::debug::Alias(&result); |
| 167 // Unexpected result is likely fatal, crash so we can determine cause. |
| 168 CHECK(false); |
| 165 } | 169 } |
| 166 } | 170 } |
| 167 | 171 |
| 168 // Notify and remove any handlers whose time has expired. Make a copy in case | 172 // Notify and remove any handlers whose time has expired. Make a copy in case |
| 169 // someone tries to add/remove new handlers from notification. | 173 // someone tries to add/remove new handlers from notification. |
| 170 const HandleToHandler cloned_handlers(handlers_); | 174 const HandleToHandler cloned_handlers(handlers_); |
| 171 const base::TimeTicks now(internal::NowTicks()); | 175 const base::TimeTicks now(internal::NowTicks()); |
| 172 for (HandleToHandler::const_iterator i = cloned_handlers.begin(); | 176 for (HandleToHandler::const_iterator i = cloned_handlers.begin(); |
| 173 i != cloned_handlers.end(); ++i) { | 177 i != cloned_handlers.end(); ++i) { |
| 174 // Since we're iterating over a clone of the handlers, verify the handler is | 178 // Since we're iterating over a clone of the handlers, verify the handler is |
| 175 // still valid before notifying. | 179 // still valid before notifying. |
| 176 if (!i->second.deadline.is_null() && i->second.deadline < now && | 180 if (!i->second.deadline.is_null() && i->second.deadline < now && |
| 177 handlers_.find(i->first) != handlers_.end() && | 181 handlers_.find(i->first) != handlers_.end() && |
| 178 handlers_[i->first].id == i->second.id) { | 182 handlers_[i->first].id == i->second.id) { |
| 179 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); | 183 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); |
| 180 } | 184 } |
| 181 } | 185 } |
| 182 } | 186 } |
| 183 | 187 |
| 184 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { | 188 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { |
| 185 // TODO(sky): deal with control pipe going bad. | 189 // TODO(sky): deal with control pipe going bad. |
| 186 for (size_t i = 1; i < wait_state.handles.size(); ++i) { | 190 for (size_t i = 1; i < wait_state.handles.size(); ++i) { |
| 187 const MojoResult result = | 191 const MojoResult result = |
| 188 Wait(wait_state.handles[i], wait_state.wait_flags[i], 0); | 192 Wait(wait_state.handles[i], wait_state.wait_flags[i], 0); |
| 189 if (result == MOJO_RESULT_INVALID_ARGUMENT || | 193 if (result == MOJO_RESULT_INVALID_ARGUMENT || |
| 190 result == MOJO_RESULT_FAILED_PRECONDITION) { | 194 result == MOJO_RESULT_FAILED_PRECONDITION || |
| 195 result == MOJO_RESULT_CANCELLED) { |
| 191 // Remove the handle first, this way if OnHandleError() tries to remove | 196 // Remove the handle first, this way if OnHandleError() tries to remove |
| 192 // the handle our iterator isn't invalidated. | 197 // the handle our iterator isn't invalidated. |
| 193 DCHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); | 198 DCHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); |
| 194 MessagePumpMojoHandler* handler = | 199 MessagePumpMojoHandler* handler = |
| 195 handlers_[wait_state.handles[i]].handler; | 200 handlers_[wait_state.handles[i]].handler; |
| 196 handlers_.erase(wait_state.handles[i]); | 201 handlers_.erase(wait_state.handles[i]); |
| 197 handler->OnHandleError(wait_state.handles[i], result); | 202 handler->OnHandleError(wait_state.handles[i], result); |
| 198 return; | 203 return; |
| 199 } | 204 } |
| 200 } | 205 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 229 min_time = i->second.deadline; | 234 min_time = i->second.deadline; |
| 230 } | 235 } |
| 231 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : | 236 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : |
| 232 std::max(static_cast<MojoDeadline>(0), | 237 std::max(static_cast<MojoDeadline>(0), |
| 233 static_cast<MojoDeadline>( | 238 static_cast<MojoDeadline>( |
| 234 (min_time - internal::NowTicks()).InMicroseconds())); | 239 (min_time - internal::NowTicks()).InMicroseconds())); |
| 235 } | 240 } |
| 236 | 241 |
| 237 } // namespace common | 242 } // namespace common |
| 238 } // namespace mojo | 243 } // namespace mojo |
| OLD | NEW |