| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/handle_table.h" | 5 #include "mojo/edk/system/handle_table.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 if (handles->at(i)) { | 101 if (handles->at(i)) { |
| 102 handle_values[i] = AddHandleNoSizeCheck(std::move(handles->at(i))); | 102 handle_values[i] = AddHandleNoSizeCheck(std::move(handles->at(i))); |
| 103 } else { | 103 } else { |
| 104 LOG(WARNING) << "Invalid dispatcher at index " << i; | 104 LOG(WARNING) << "Invalid dispatcher at index " << i; |
| 105 handle_values[i] = MOJO_HANDLE_INVALID; | 105 handle_values[i] = MOJO_HANDLE_INVALID; |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 return true; | 108 return true; |
| 109 } | 109 } |
| 110 | 110 |
| 111 MojoResult HandleTable::ReplaceHandleWithReducedRights( |
| 112 MojoHandle handle_value, |
| 113 MojoHandleRights rights_to_remove, |
| 114 MojoHandle* replacement_handle_value) { |
| 115 DCHECK_NE(handle_value, MOJO_HANDLE_INVALID); |
| 116 DCHECK(replacement_handle_value); |
| 117 |
| 118 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle_value); |
| 119 if (it == handle_to_entry_map_.end()) |
| 120 return MOJO_RESULT_INVALID_ARGUMENT; |
| 121 |
| 122 Entry entry = it->second; |
| 123 if (entry.busy) |
| 124 return MOJO_RESULT_BUSY; |
| 125 // We don't need to mark the entry as busy, since we do everything under the |
| 126 // handle table lock (unlike sending messages). |
| 127 |
| 128 // Try to start the transport. (This just tries to take the dispatcher's |
| 129 // lock.) |
| 130 HandleTransport transport = |
| 131 Dispatcher::HandleTableAccess::TryStartTransport(entry.handle); |
| 132 if (!transport.is_valid()) |
| 133 return MOJO_RESULT_BUSY; |
| 134 |
| 135 // We don't need to check the capacity of the handle table, since we're just |
| 136 // going to replace the old handle. (Nothing below can fail, so we won't need |
| 137 // to unwind.) |
| 138 |
| 139 Handle replacement_handle = |
| 140 transport.CreateEquivalentHandleAndClose(nullptr, 0); |
| 141 replacement_handle.rights &= ~rights_to_remove; |
| 142 transport.End(); |
| 143 |
| 144 // |it| is still valid here. |
| 145 handle_to_entry_map_.erase(it); |
| 146 |
| 147 *replacement_handle_value = |
| 148 AddHandleNoSizeCheck(std::move(replacement_handle)); |
| 149 return MOJO_RESULT_OK; |
| 150 } |
| 151 |
| 111 MojoResult HandleTable::MarkBusyAndStartTransport( | 152 MojoResult HandleTable::MarkBusyAndStartTransport( |
| 112 MojoHandle disallowed_handle, | 153 MojoHandle disallowed_handle_value, |
| 113 const MojoHandle* handle_values, | 154 const MojoHandle* handle_values, |
| 114 uint32_t num_handles, | 155 uint32_t num_handles, |
| 115 std::vector<HandleTransport>* transports) { | 156 std::vector<HandleTransport>* transports) { |
| 116 DCHECK_NE(disallowed_handle, MOJO_HANDLE_INVALID); | 157 DCHECK_NE(disallowed_handle_value, MOJO_HANDLE_INVALID); |
| 117 DCHECK(handle_values); | 158 DCHECK(handle_values); |
| 118 DCHECK_LE(num_handles, GetConfiguration().max_message_num_handles); | 159 DCHECK_LE(num_handles, GetConfiguration().max_message_num_handles); |
| 119 DCHECK(transports); | 160 DCHECK(transports); |
| 120 DCHECK_EQ(transports->size(), num_handles); | 161 DCHECK_EQ(transports->size(), num_handles); |
| 121 | 162 |
| 122 std::vector<Entry*> entries(num_handles); | 163 std::vector<Entry*> entries(num_handles); |
| 123 | 164 |
| 124 // First verify all the handle values and get their dispatchers. | 165 // First verify all the handle values and get their dispatchers. |
| 125 uint32_t i; | 166 uint32_t i; |
| 126 MojoResult error_result = MOJO_RESULT_INTERNAL; | 167 MojoResult error_result = MOJO_RESULT_INTERNAL; |
| 127 for (i = 0; i < num_handles; i++) { | 168 for (i = 0; i < num_handles; i++) { |
| 128 // Sending your own handle is not allowed (and, for consistency, returns | 169 // Sending your own handle is not allowed (and, for consistency, returns |
| 129 // "busy"). | 170 // "busy"). |
| 130 if (handle_values[i] == disallowed_handle) { | 171 if (handle_values[i] == disallowed_handle_value) { |
| 131 error_result = MOJO_RESULT_BUSY; | 172 error_result = MOJO_RESULT_BUSY; |
| 132 break; | 173 break; |
| 133 } | 174 } |
| 134 | 175 |
| 135 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle_values[i]); | 176 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle_values[i]); |
| 136 if (it == handle_to_entry_map_.end()) { | 177 if (it == handle_to_entry_map_.end()) { |
| 137 error_result = MOJO_RESULT_INVALID_ARGUMENT; | 178 error_result = MOJO_RESULT_INVALID_ARGUMENT; |
| 138 break; | 179 break; |
| 139 } | 180 } |
| 140 | 181 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 for (uint32_t i = 0; i < num_handles; i++) { | 274 for (uint32_t i = 0; i < num_handles; i++) { |
| 234 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle_values[i]); | 275 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle_values[i]); |
| 235 DCHECK(it != handle_to_entry_map_.end()); | 276 DCHECK(it != handle_to_entry_map_.end()); |
| 236 DCHECK(it->second.busy); | 277 DCHECK(it->second.busy); |
| 237 it->second.busy = false; | 278 it->second.busy = false; |
| 238 } | 279 } |
| 239 } | 280 } |
| 240 | 281 |
| 241 } // namespace system | 282 } // namespace system |
| 242 } // namespace mojo | 283 } // namespace mojo |
| OLD | NEW |