Chromium Code Reviews| 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/edk/system/core.h" | 5 #include "mojo/edk/system/core.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 53 void CallWatchCallback(MojoWatchCallback callback, | 53 void CallWatchCallback(MojoWatchCallback callback, |
| 54 uintptr_t context, | 54 uintptr_t context, |
| 55 MojoResult result, | 55 MojoResult result, |
| 56 const HandleSignalsState& signals_state, | 56 const HandleSignalsState& signals_state, |
| 57 MojoWatchNotificationFlags flags) { | 57 MojoWatchNotificationFlags flags) { |
| 58 callback(context, result, static_cast<MojoHandleSignalsState>(signals_state), | 58 callback(context, result, static_cast<MojoHandleSignalsState>(signals_state), |
| 59 flags); | 59 flags); |
| 60 } | 60 } |
| 61 | 61 |
| 62 MojoResult MojoPlatformHandleToScopedPlatformHandle( | |
| 63 const MojoPlatformHandle* platform_handle, | |
| 64 ScopedPlatformHandle* out_handle) { | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
indenting
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
done
| |
| 65 if (platform_handle->struct_size != sizeof(MojoPlatformHandle)) | |
| 66 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 67 | |
| 68 if (platform_handle->type == MOJO_PLATFORM_HANDLE_TYPE_INVALID) { | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
This case exists twice with different error return
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
Woops - the first one is correct. Removed the seco
| |
| 69 out_handle->reset(); | |
| 70 return MOJO_RESULT_OK; | |
| 71 } | |
| 72 | |
| 73 PlatformHandle handle; | |
| 74 switch (platform_handle->type) { | |
| 75 case MOJO_PLATFORM_HANDLE_TYPE_INVALID: | |
| 76 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 77 | |
| 78 #if defined(OS_POSIX) | |
| 79 case MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR: | |
| 80 handle.handle = static_cast<int>(platform_handle->value); | |
| 81 break; | |
| 82 #endif | |
| 83 | |
| 84 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 85 case MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT: | |
| 86 handle.type = PlatformHandle::Type::MACH; | |
| 87 handle.port = static_cast<mach_port_t>(platform_handle->value); | |
| 88 break; | |
| 89 #endif | |
| 90 | |
| 91 #if defined(OS_WIN) | |
| 92 case MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE: | |
| 93 handle.handle = reinterpret_cast<HANDLE>(platform_handle->value); | |
| 94 break; | |
| 95 #endif | |
| 96 | |
| 97 default: | |
| 98 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 99 } | |
| 100 | |
| 101 out_handle->reset(handle); | |
| 102 return MOJO_RESULT_OK; | |
| 103 } | |
| 104 | |
| 105 MojoResult ScopedPlatformHandleToMojoPlatformHandle( | |
| 106 ScopedPlatformHandle handle, | |
| 107 MojoPlatformHandle* platform_handle) { | |
| 108 if (platform_handle->struct_size != sizeof(MojoPlatformHandle)) | |
| 109 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 110 | |
| 111 if (!handle.is_valid()) { | |
| 112 platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_INVALID; | |
| 113 return MOJO_RESULT_OK; | |
| 114 } | |
| 115 | |
| 116 #if defined(OS_POSIX) | |
| 117 switch (handle.get().type) { | |
| 118 case PlatformHandle::Type::POSIX: | |
| 119 platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_FILE_DESCRIPTOR; | |
| 120 platform_handle->value = static_cast<uint64_t>(handle.release().handle); | |
| 121 break; | |
| 122 | |
| 123 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
| 124 case PlatformHandle::Type::MACH: | |
| 125 platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_MACH_PORT; | |
| 126 platform_handle->value = static_cast<uint64_t>(handle.release().port); | |
| 127 break; | |
| 128 #endif | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
#endif // defined(OS_MACOSX) && !defined(OS_IOS)
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
Done
| |
| 129 | |
| 130 default: | |
| 131 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 132 } | |
| 133 #elif defined(OS_WIN) | |
| 134 platform_handle->type = MOJO_PLATFORM_HANDLE_TYPE_WINDOWS_HANDLE; | |
| 135 platform_handle->value = reinterpret_cast<uint64_t>(handle.release().handle); | |
| 136 #endif // defined(OS_WIN) | |
| 137 | |
| 138 return MOJO_RESULT_OK; | |
| 139 } | |
| 140 | |
| 62 } // namespace | 141 } // namespace |
| 63 | 142 |
| 64 Core::Core() {} | 143 Core::Core() {} |
| 65 | 144 |
| 66 Core::~Core() { | 145 Core::~Core() { |
| 67 if (node_controller_ && node_controller_->io_task_runner()) { | 146 if (node_controller_ && node_controller_->io_task_runner()) { |
| 68 // If this races with IO thread shutdown the callback will be dropped and | 147 // If this races with IO thread shutdown the callback will be dropped and |
| 69 // the NodeController will be shutdown on this thread anyway, which is also | 148 // the NodeController will be shutdown on this thread anyway, which is also |
| 70 // just fine. | 149 // just fine. |
| 71 scoped_refptr<base::TaskRunner> io_task_runner = | 150 scoped_refptr<base::TaskRunner> io_task_runner = |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 } | 221 } |
| 143 | 222 |
| 144 MojoResult Core::PassWrappedPlatformHandle( | 223 MojoResult Core::PassWrappedPlatformHandle( |
| 145 MojoHandle wrapper_handle, | 224 MojoHandle wrapper_handle, |
| 146 ScopedPlatformHandle* platform_handle) { | 225 ScopedPlatformHandle* platform_handle) { |
| 147 base::AutoLock lock(handles_lock_); | 226 base::AutoLock lock(handles_lock_); |
| 148 scoped_refptr<Dispatcher> d; | 227 scoped_refptr<Dispatcher> d; |
| 149 MojoResult result = handles_.GetAndRemoveDispatcher(wrapper_handle, &d); | 228 MojoResult result = handles_.GetAndRemoveDispatcher(wrapper_handle, &d); |
| 150 if (result != MOJO_RESULT_OK) | 229 if (result != MOJO_RESULT_OK) |
| 151 return result; | 230 return result; |
| 152 PlatformHandleDispatcher* phd = | 231 if (d->GetType() == Dispatcher::Type::PLATFORM_HANDLE) { |
| 153 static_cast<PlatformHandleDispatcher*>(d.get()); | 232 PlatformHandleDispatcher* phd = |
| 154 *platform_handle = phd->PassPlatformHandle(); | 233 static_cast<PlatformHandleDispatcher*>(d.get()); |
| 155 phd->Close(); | 234 *platform_handle = phd->PassPlatformHandle(); |
| 156 return MOJO_RESULT_OK; | 235 } else { |
| 236 result = MOJO_RESULT_INVALID_ARGUMENT; | |
| 237 } | |
| 238 d->Close(); | |
| 239 return result; | |
| 157 } | 240 } |
| 158 | 241 |
| 159 MojoResult Core::CreateSharedBufferWrapper( | 242 MojoResult Core::CreateSharedBufferWrapper( |
| 160 base::SharedMemoryHandle shared_memory_handle, | 243 base::SharedMemoryHandle shared_memory_handle, |
| 161 size_t num_bytes, | 244 size_t num_bytes, |
| 162 bool read_only, | 245 bool read_only, |
| 163 MojoHandle* mojo_wrapper_handle) { | 246 MojoHandle* mojo_wrapper_handle) { |
| 164 DCHECK(num_bytes); | 247 DCHECK(num_bytes); |
| 165 scoped_refptr<PlatformSharedBuffer> platform_buffer = | 248 scoped_refptr<PlatformSharedBuffer> platform_buffer = |
| 166 PlatformSharedBuffer::CreateFromSharedMemoryHandle(num_bytes, read_only, | 249 PlatformSharedBuffer::CreateFromSharedMemoryHandle(num_bytes, read_only, |
| (...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 873 *buffer = address; | 956 *buffer = address; |
| 874 return MOJO_RESULT_OK; | 957 return MOJO_RESULT_OK; |
| 875 } | 958 } |
| 876 | 959 |
| 877 MojoResult Core::UnmapBuffer(void* buffer) { | 960 MojoResult Core::UnmapBuffer(void* buffer) { |
| 878 RequestContext request_context; | 961 RequestContext request_context; |
| 879 base::AutoLock lock(mapping_table_lock_); | 962 base::AutoLock lock(mapping_table_lock_); |
| 880 return mapping_table_.RemoveMapping(buffer); | 963 return mapping_table_.RemoveMapping(buffer); |
| 881 } | 964 } |
| 882 | 965 |
| 966 MojoResult Core::WrapPlatformHandle(const MojoPlatformHandle* platform_handle, | |
| 967 MojoHandle* mojo_handle) { | |
| 968 ScopedPlatformHandle handle; | |
| 969 MojoResult result = MojoPlatformHandleToScopedPlatformHandle(platform_handle, | |
| 970 &handle); | |
| 971 if (result != MOJO_RESULT_OK) | |
| 972 return result; | |
| 973 | |
| 974 return CreatePlatformHandleWrapper(std::move(handle), mojo_handle); | |
| 975 } | |
| 976 | |
| 977 MojoResult Core::UnwrapPlatformHandle(MojoHandle mojo_handle, | |
| 978 MojoPlatformHandle* platform_handle) { | |
| 979 ScopedPlatformHandle handle; | |
| 980 MojoResult result = PassWrappedPlatformHandle(mojo_handle, &handle); | |
| 981 if (result != MOJO_RESULT_OK) | |
| 982 return result; | |
| 983 | |
| 984 return ScopedPlatformHandleToMojoPlatformHandle(std::move(handle), | |
| 985 platform_handle); | |
| 986 } | |
| 987 | |
| 988 MojoResult Core::WrapPlatformSharedBufferHandle( | |
| 989 const MojoPlatformHandle* platform_handle, | |
| 990 size_t size, | |
| 991 MojoPlatformSharedBufferHandleFlags flags, | |
| 992 MojoHandle* mojo_handle) { | |
| 993 DCHECK(size); | |
| 994 ScopedPlatformHandle handle; | |
| 995 MojoResult result = MojoPlatformHandleToScopedPlatformHandle(platform_handle, | |
| 996 &handle); | |
| 997 if (result != MOJO_RESULT_OK) | |
| 998 return result; | |
| 999 | |
| 1000 bool read_only = flags & MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY; | |
| 1001 scoped_refptr<PlatformSharedBuffer> platform_buffer = | |
| 1002 PlatformSharedBuffer::CreateFromPlatformHandle(size, read_only, | |
| 1003 std::move(handle)); | |
| 1004 if (!platform_buffer) | |
| 1005 return MOJO_RESULT_UNKNOWN; | |
| 1006 | |
| 1007 scoped_refptr<SharedBufferDispatcher> dispatcher; | |
| 1008 result = SharedBufferDispatcher::CreateFromPlatformSharedBuffer( | |
| 1009 platform_buffer, &dispatcher); | |
| 1010 if (result != MOJO_RESULT_OK) | |
| 1011 return result; | |
| 1012 | |
| 1013 MojoHandle h = AddDispatcher(dispatcher); | |
| 1014 if (h == MOJO_HANDLE_INVALID) | |
| 1015 return MOJO_RESULT_RESOURCE_EXHAUSTED; | |
|
Anand Mistry (off Chromium)
2016/05/23 07:16:52
You need to call Close() on the dispatcher if it w
Ken Rockot(use gerrit already)
2016/05/23 17:17:53
Done
| |
| 1016 | |
| 1017 *mojo_handle = h; | |
| 1018 return MOJO_RESULT_OK; | |
| 1019 } | |
| 1020 | |
| 1021 MojoResult Core::UnwrapPlatformSharedBufferHandle( | |
| 1022 MojoHandle mojo_handle, | |
| 1023 MojoPlatformHandle* platform_handle, | |
| 1024 size_t* size, | |
| 1025 MojoPlatformSharedBufferHandleFlags* flags) { | |
| 1026 scoped_refptr<Dispatcher> dispatcher; | |
| 1027 MojoResult result = MOJO_RESULT_OK; | |
| 1028 { | |
| 1029 base::AutoLock lock(handles_lock_); | |
| 1030 result = handles_.GetAndRemoveDispatcher(mojo_handle, &dispatcher); | |
| 1031 if (result != MOJO_RESULT_OK) | |
| 1032 return result; | |
| 1033 } | |
| 1034 | |
| 1035 if (dispatcher->GetType() != Dispatcher::Type::SHARED_BUFFER) { | |
| 1036 dispatcher->Close(); | |
| 1037 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 1038 } | |
| 1039 | |
| 1040 SharedBufferDispatcher* shm_dispatcher = | |
| 1041 static_cast<SharedBufferDispatcher*>(dispatcher.get()); | |
| 1042 scoped_refptr<PlatformSharedBuffer> platform_shared_buffer = | |
| 1043 shm_dispatcher->PassPlatformSharedBuffer(); | |
| 1044 CHECK(platform_shared_buffer); | |
| 1045 | |
| 1046 CHECK(size); | |
| 1047 *size = platform_shared_buffer->GetNumBytes(); | |
| 1048 | |
| 1049 CHECK(flags); | |
| 1050 *flags = MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_NONE; | |
| 1051 if (platform_shared_buffer->IsReadOnly()) | |
| 1052 *flags |= MOJO_PLATFORM_SHARED_BUFFER_HANDLE_FLAG_READ_ONLY; | |
| 1053 | |
| 1054 ScopedPlatformHandle handle = platform_shared_buffer->PassPlatformHandle(); | |
| 1055 return ScopedPlatformHandleToMojoPlatformHandle(std::move(handle), | |
| 1056 platform_handle); | |
| 1057 } | |
| 1058 | |
| 883 void Core::GetActiveHandlesForTest(std::vector<MojoHandle>* handles) { | 1059 void Core::GetActiveHandlesForTest(std::vector<MojoHandle>* handles) { |
| 884 base::AutoLock lock(handles_lock_); | 1060 base::AutoLock lock(handles_lock_); |
| 885 handles_.GetActiveHandlesForTest(handles); | 1061 handles_.GetActiveHandlesForTest(handles); |
| 886 } | 1062 } |
| 887 | 1063 |
| 888 MojoResult Core::WaitManyInternal(const MojoHandle* handles, | 1064 MojoResult Core::WaitManyInternal(const MojoHandle* handles, |
| 889 const MojoHandleSignals* signals, | 1065 const MojoHandleSignals* signals, |
| 890 uint32_t num_handles, | 1066 uint32_t num_handles, |
| 891 MojoDeadline deadline, | 1067 MojoDeadline deadline, |
| 892 uint32_t *result_index, | 1068 uint32_t *result_index, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 958 std::unique_ptr<NodeController> node_controller) { | 1134 std::unique_ptr<NodeController> node_controller) { |
| 959 // It's OK to leak this reference. At this point we know the IO loop is still | 1135 // It's OK to leak this reference. At this point we know the IO loop is still |
| 960 // running, and we know the NodeController will observe its eventual | 1136 // running, and we know the NodeController will observe its eventual |
| 961 // destruction. This tells the NodeController to delete itself when that | 1137 // destruction. This tells the NodeController to delete itself when that |
| 962 // happens. | 1138 // happens. |
| 963 node_controller.release()->DestroyOnIOThreadShutdown(); | 1139 node_controller.release()->DestroyOnIOThreadShutdown(); |
| 964 } | 1140 } |
| 965 | 1141 |
| 966 } // namespace edk | 1142 } // namespace edk |
| 967 } // namespace mojo | 1143 } // namespace mojo |
| OLD | NEW |