OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/mach/composite_mach_message_server.h" |
| 16 |
| 17 #include <algorithm> |
| 18 #include <utility> |
| 19 |
| 20 #include "base/logging.h" |
| 21 #include "util/mach/mach_message.h" |
| 22 |
| 23 namespace crashpad { |
| 24 |
| 25 CompositeMachMessageServer::CompositeMachMessageServer() |
| 26 : MachMessageServer::Interface(), |
| 27 handler_map_(), |
| 28 request_size_(sizeof(mach_msg_header_t)), |
| 29 reply_size_(sizeof(mig_reply_error_t)) { |
| 30 } |
| 31 |
| 32 CompositeMachMessageServer::~CompositeMachMessageServer() { |
| 33 } |
| 34 |
| 35 void CompositeMachMessageServer::AddHandler( |
| 36 MachMessageServer::Interface* handler) { |
| 37 // Other cycles would be invalid as well, but they aren’t currently checked. |
| 38 DCHECK_NE(handler, this); |
| 39 |
| 40 std::set<mach_msg_id_t> request_ids = handler->MachMessageServerRequestIDs(); |
| 41 for (mach_msg_id_t request_id : request_ids) { |
| 42 std::pair<HandlerMap::const_iterator, bool> result = |
| 43 handler_map_.insert(std::make_pair(request_id, handler)); |
| 44 CHECK(result.second) << "duplicate request ID " << request_id; |
| 45 } |
| 46 |
| 47 request_size_ = |
| 48 std::max(request_size_, handler->MachMessageServerRequestSize()); |
| 49 reply_size_ = std::max(reply_size_, handler->MachMessageServerReplySize()); |
| 50 } |
| 51 |
| 52 bool CompositeMachMessageServer::MachMessageServerFunction( |
| 53 const mach_msg_header_t* in, |
| 54 mach_msg_header_t* out, |
| 55 bool* destroy_complex_request) { |
| 56 HandlerMap::const_iterator iterator = handler_map_.find(in->msgh_id); |
| 57 if (iterator == handler_map_.end()) { |
| 58 // Do what MIG-generated server routines do when they can’t dispatch a |
| 59 // message. |
| 60 PrepareMIGReplyFromRequest(in, out); |
| 61 SetMIGReplyError(out, MIG_BAD_ID); |
| 62 return false; |
| 63 } |
| 64 |
| 65 MachMessageServer::Interface* handler = iterator->second; |
| 66 return handler->MachMessageServerFunction(in, out, destroy_complex_request); |
| 67 } |
| 68 |
| 69 std::set<mach_msg_id_t> |
| 70 CompositeMachMessageServer::MachMessageServerRequestIDs() { |
| 71 std::set<mach_msg_id_t> request_ids; |
| 72 for (const auto& entry : handler_map_) { |
| 73 request_ids.insert(entry.first); |
| 74 } |
| 75 return request_ids; |
| 76 } |
| 77 |
| 78 mach_msg_size_t CompositeMachMessageServer::MachMessageServerRequestSize() { |
| 79 return request_size_; |
| 80 } |
| 81 |
| 82 mach_msg_size_t CompositeMachMessageServer::MachMessageServerReplySize() { |
| 83 return reply_size_; |
| 84 } |
| 85 |
| 86 } // namespace crashpad |
OLD | NEW |