| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/embedder/embedder.h" | 5 #include "mojo/edk/embedder/multiprocess_embedder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "mojo/edk/embedder/embedder_internal.h" | 11 #include "mojo/edk/embedder/embedder_internal.h" |
| 12 #include "mojo/edk/embedder/master_process_delegate.h" | 12 #include "mojo/edk/embedder/master_process_delegate.h" |
| 13 #include "mojo/edk/embedder/platform_support.h" | |
| 14 #include "mojo/edk/embedder/process_delegate.h" | 13 #include "mojo/edk/embedder/process_delegate.h" |
| 15 #include "mojo/edk/embedder/slave_process_delegate.h" | 14 #include "mojo/edk/embedder/slave_process_delegate.h" |
| 16 #include "mojo/edk/system/channel.h" | 15 #include "mojo/edk/system/channel.h" |
| 17 #include "mojo/edk/system/channel_manager.h" | 16 #include "mojo/edk/system/channel_manager.h" |
| 18 #include "mojo/edk/system/configuration.h" | |
| 19 #include "mojo/edk/system/core.h" | 17 #include "mojo/edk/system/core.h" |
| 20 #include "mojo/edk/system/ipc_support.h" | 18 #include "mojo/edk/system/ipc_support.h" |
| 21 #include "mojo/edk/system/message_pipe_dispatcher.h" | 19 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 22 #include "mojo/edk/system/platform_handle_dispatcher.h" | |
| 23 #include "mojo/edk/system/raw_channel.h" | 20 #include "mojo/edk/system/raw_channel.h" |
| 24 #include "mojo/edk/util/ref_ptr.h" | 21 #include "mojo/edk/util/ref_ptr.h" |
| 25 | 22 |
| 26 using mojo::platform::PlatformHandleWatcher; | 23 using mojo::platform::PlatformHandleWatcher; |
| 27 using mojo::platform::ScopedPlatformHandle; | 24 using mojo::platform::ScopedPlatformHandle; |
| 28 using mojo::platform::TaskRunner; | 25 using mojo::platform::TaskRunner; |
| 29 using mojo::util::RefPtr; | 26 using mojo::util::RefPtr; |
| 30 | 27 |
| 31 namespace mojo { | 28 namespace mojo { |
| 32 namespace embedder { | 29 namespace embedder { |
| 33 | 30 |
| 34 namespace internal { | 31 namespace internal { |
| 35 | 32 |
| 36 // Declared in embedder_internal.h. | 33 // Declared in embedder_internal.h. |
| 37 PlatformSupport* g_platform_support = nullptr; | |
| 38 system::Core* g_core = nullptr; | |
| 39 system::IPCSupport* g_ipc_support = nullptr; | 34 system::IPCSupport* g_ipc_support = nullptr; |
| 40 | 35 |
| 41 } // namespace internal | 36 } // namespace internal |
| 42 | 37 |
| 43 namespace { | 38 namespace { |
| 44 | 39 |
| 45 // TODO(vtl): For now, we need this to be thread-safe (since theoretically we | 40 // TODO(vtl): For now, we need this to be thread-safe (since theoretically we |
| 46 // currently support multiple channel creation threads -- possibly one per | 41 // currently support multiple channel creation threads -- possibly one per |
| 47 // channel). Eventually, we won't need it to be thread-safe (we'll require a | 42 // channel). Eventually, we won't need it to be thread-safe (we'll require a |
| 48 // single I/O thread), and eventually we won't need it at all. Remember to | 43 // single I/O thread), and eventually we won't need it at all. Remember to |
| 49 // remove the base/atomicops.h include. | 44 // remove the base/atomicops.h include. |
| 50 system::ChannelId MakeChannelId() { | 45 system::ChannelId MakeChannelId() { |
| 51 // Note that |AtomicWord| is signed. | 46 // Note that |AtomicWord| is signed. |
| 52 static base::subtle::AtomicWord counter = 0; | 47 static base::subtle::AtomicWord counter = 0; |
| 53 | 48 |
| 54 base::subtle::AtomicWord new_counter_value = | 49 base::subtle::AtomicWord new_counter_value = |
| 55 base::subtle::NoBarrier_AtomicIncrement(&counter, 1); | 50 base::subtle::NoBarrier_AtomicIncrement(&counter, 1); |
| 56 // Don't allow the counter to wrap. Note that any (strictly) positive value is | 51 // Don't allow the counter to wrap. Note that any (strictly) positive value is |
| 57 // a valid |ChannelId| (and |NoBarrier_AtomicIncrement()| returns the value | 52 // a valid |ChannelId| (and |NoBarrier_AtomicIncrement()| returns the value |
| 58 // post-increment). | 53 // post-increment). |
| 59 CHECK_GT(new_counter_value, 0); | 54 CHECK_GT(new_counter_value, 0); |
| 60 // Use "negative" values for these IDs, so that we'll also be able to use | 55 // Use "negative" values for these IDs, so that we'll also be able to use |
| 61 // "positive" "process identifiers" (see connection_manager.h) as IDs (and | 56 // "positive" "process identifiers" (see connection_manager.h) as IDs (and |
| 62 // they won't conflict). | 57 // they won't conflict). |
| 63 return static_cast<system::ChannelId>(-new_counter_value); | 58 return static_cast<system::ChannelId>(-new_counter_value); |
| 64 } | 59 } |
| 65 | 60 |
| 66 } // namespace | 61 } // namespace |
| 67 | 62 |
| 68 Configuration* GetConfiguration() { | |
| 69 return system::GetMutableConfiguration(); | |
| 70 } | |
| 71 | |
| 72 void Init(std::unique_ptr<PlatformSupport> platform_support) { | |
| 73 DCHECK(platform_support); | |
| 74 | |
| 75 DCHECK(!internal::g_platform_support); | |
| 76 internal::g_platform_support = platform_support.release(); | |
| 77 | |
| 78 DCHECK(!internal::g_core); | |
| 79 internal::g_core = new system::Core(internal::g_platform_support); | |
| 80 } | |
| 81 | |
| 82 MojoResult AsyncWait(MojoHandle handle, | |
| 83 MojoHandleSignals signals, | |
| 84 const std::function<void(MojoResult)>& callback) { | |
| 85 return internal::g_core->AsyncWait(handle, signals, callback); | |
| 86 } | |
| 87 | |
| 88 MojoResult CreatePlatformHandleWrapper( | |
| 89 ScopedPlatformHandle platform_handle, | |
| 90 MojoHandle* platform_handle_wrapper_handle) { | |
| 91 DCHECK(platform_handle_wrapper_handle); | |
| 92 | |
| 93 auto dispatcher = | |
| 94 system::PlatformHandleDispatcher::Create(platform_handle.Pass()); | |
| 95 | |
| 96 DCHECK(internal::g_core); | |
| 97 MojoHandle h = internal::g_core->AddDispatcher(dispatcher.get()); | |
| 98 if (h == MOJO_HANDLE_INVALID) { | |
| 99 LOG(ERROR) << "Handle table full"; | |
| 100 dispatcher->Close(); | |
| 101 return MOJO_RESULT_RESOURCE_EXHAUSTED; | |
| 102 } | |
| 103 | |
| 104 *platform_handle_wrapper_handle = h; | |
| 105 return MOJO_RESULT_OK; | |
| 106 } | |
| 107 | |
| 108 MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle, | |
| 109 ScopedPlatformHandle* platform_handle) { | |
| 110 DCHECK(platform_handle); | |
| 111 | |
| 112 DCHECK(internal::g_core); | |
| 113 auto dispatcher = | |
| 114 internal::g_core->GetDispatcher(platform_handle_wrapper_handle); | |
| 115 if (!dispatcher) | |
| 116 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 117 | |
| 118 if (dispatcher->GetType() != system::Dispatcher::Type::PLATFORM_HANDLE) | |
| 119 return MOJO_RESULT_INVALID_ARGUMENT; | |
| 120 | |
| 121 *platform_handle = | |
| 122 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get()) | |
| 123 ->PassPlatformHandle(); | |
| 124 return MOJO_RESULT_OK; | |
| 125 } | |
| 126 | |
| 127 void InitIPCSupport(ProcessType process_type, | 63 void InitIPCSupport(ProcessType process_type, |
| 128 RefPtr<TaskRunner>&& delegate_thread_task_runner, | 64 RefPtr<TaskRunner>&& delegate_thread_task_runner, |
| 129 ProcessDelegate* process_delegate, | 65 ProcessDelegate* process_delegate, |
| 130 RefPtr<TaskRunner>&& io_task_runner, | 66 RefPtr<TaskRunner>&& io_task_runner, |
| 131 PlatformHandleWatcher* io_watcher, | 67 PlatformHandleWatcher* io_watcher, |
| 132 ScopedPlatformHandle platform_handle) { | 68 ScopedPlatformHandle platform_handle) { |
| 133 // |Init()| must have already been called. | 69 // |Init()| must have already been called. |
| 134 DCHECK(internal::g_core); | 70 DCHECK(internal::g_core); |
| 135 // And not |InitIPCSupport()| (without |ShutdownIPCSupport()|). | 71 // And not |InitIPCSupport()| (without |ShutdownIPCSupport()|). |
| 136 DCHECK(!internal::g_ipc_support); | 72 DCHECK(!internal::g_ipc_support); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 DCHECK(channel_info); | 239 DCHECK(channel_info); |
| 304 DCHECK(internal::g_ipc_support); | 240 DCHECK(internal::g_ipc_support); |
| 305 | 241 |
| 306 system::ChannelManager* channel_manager = | 242 system::ChannelManager* channel_manager = |
| 307 internal::g_ipc_support->channel_manager(); | 243 internal::g_ipc_support->channel_manager(); |
| 308 channel_manager->WillShutdownChannel(channel_info->channel_id); | 244 channel_manager->WillShutdownChannel(channel_info->channel_id); |
| 309 } | 245 } |
| 310 | 246 |
| 311 } // namespace embedder | 247 } // namespace embedder |
| 312 } // namespace mojo | 248 } // namespace mojo |
| OLD | NEW |