| 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/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "mojo/edk/system/child_broker.h" | 21 #include "mojo/edk/system/child_broker.h" |
| 22 #include "mojo/edk/system/child_broker_host.h" | 22 #include "mojo/edk/system/child_broker_host.h" |
| 23 #include "mojo/edk/system/configuration.h" | 23 #include "mojo/edk/system/configuration.h" |
| 24 #include "mojo/edk/system/core.h" | 24 #include "mojo/edk/system/core.h" |
| 25 #include "mojo/edk/system/message_pipe_dispatcher.h" | 25 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 26 #include "mojo/edk/system/platform_handle_dispatcher.h" | 26 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 27 | 27 |
| 28 namespace mojo { | 28 namespace mojo { |
| 29 namespace edk { | 29 namespace edk { |
| 30 | 30 |
| 31 // TODO(jam): move into annonymous namespace. Keep outside for debugging in VS | |
| 32 // temporarily. | |
| 33 int g_channel_count = 0; | |
| 34 bool g_wait_for_no_more_channels = false; | |
| 35 base::TaskRunner* g_delegate_task_runner = nullptr; // Used at shutdown. | |
| 36 | |
| 37 namespace { | 31 namespace { |
| 38 | 32 |
| 39 // Note: Called on the I/O thread. | 33 // Note: Called on the I/O thread. |
| 40 void ShutdownIPCSupportHelper(bool wait_for_no_more_channels) { | 34 void ShutdownIPCSupportHelper() { |
| 41 if (wait_for_no_more_channels && g_channel_count) { | |
| 42 g_wait_for_no_more_channels = true; | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 g_delegate_task_runner->PostTask( | |
| 47 FROM_HERE, base::Bind(&ProcessDelegate::OnShutdownComplete, | |
| 48 base::Unretained(internal::g_process_delegate))); | |
| 49 g_delegate_task_runner = nullptr; | |
| 50 } | 35 } |
| 51 | 36 |
| 52 } // namespace | 37 } // namespace |
| 53 | 38 |
| 54 namespace internal { | 39 namespace internal { |
| 55 | 40 |
| 56 // Declared in embedder_internal.h. | 41 // Declared in embedder_internal.h. |
| 57 Broker* g_broker = nullptr; | 42 Broker* g_broker = nullptr; |
| 58 PlatformSupport* g_platform_support = nullptr; | 43 PlatformSupport* g_platform_support = nullptr; |
| 59 Core* g_core = nullptr; | 44 Core* g_core = nullptr; |
| 60 | 45 |
| 61 ProcessDelegate* g_process_delegate; | 46 ProcessDelegate* g_process_delegate; |
| 62 base::TaskRunner* g_io_thread_task_runner = nullptr; | 47 base::TaskRunner* g_io_thread_task_runner = nullptr; |
| 63 | 48 |
| 64 Core* GetCore() { | 49 Core* GetCore() { |
| 65 return g_core; | 50 return g_core; |
| 66 } | 51 } |
| 67 | 52 |
| 68 void ChannelStarted() { | |
| 69 DCHECK(g_io_thread_task_runner->RunsTasksOnCurrentThread()); | |
| 70 g_channel_count++; | |
| 71 } | |
| 72 | |
| 73 void ChannelShutdown() { | |
| 74 DCHECK(g_io_thread_task_runner->RunsTasksOnCurrentThread()); | |
| 75 DCHECK_GT(g_channel_count, 0); | |
| 76 g_channel_count--; | |
| 77 if (!g_channel_count && g_wait_for_no_more_channels) { | |
| 78 // Reset g_wait_for_no_more_channels for unit tests which initialize and | |
| 79 // tear down multiple times in a process. | |
| 80 g_wait_for_no_more_channels = false; | |
| 81 ShutdownIPCSupportHelper(false); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 } // namespace internal | 53 } // namespace internal |
| 86 | 54 |
| 87 void SetMaxMessageSize(size_t bytes) { | 55 void SetMaxMessageSize(size_t bytes) { |
| 88 GetMutableConfiguration()->max_message_num_bytes = bytes; | 56 GetMutableConfiguration()->max_message_num_bytes = bytes; |
| 89 } | 57 } |
| 90 | 58 |
| 91 void PreInitializeParentProcess() { | 59 void PreInitializeParentProcess() { |
| 92 BrokerState::GetInstance(); | 60 BrokerState::GetInstance(); |
| 93 } | 61 } |
| 94 | 62 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 // |Init()| must have already been called. | 149 // |Init()| must have already been called. |
| 182 DCHECK(internal::g_core); | 150 DCHECK(internal::g_core); |
| 183 internal::g_process_delegate = process_delegate; | 151 internal::g_process_delegate = process_delegate; |
| 184 internal::g_io_thread_task_runner = io_thread_task_runner.get(); | 152 internal::g_io_thread_task_runner = io_thread_task_runner.get(); |
| 185 } | 153 } |
| 186 | 154 |
| 187 void ShutdownIPCSupportOnIOThread() { | 155 void ShutdownIPCSupportOnIOThread() { |
| 188 } | 156 } |
| 189 | 157 |
| 190 void ShutdownIPCSupport() { | 158 void ShutdownIPCSupport() { |
| 191 g_delegate_task_runner = base::MessageLoop::current()->task_runner().get(); | 159 internal::g_io_thread_task_runner->PostTaskAndReply( |
| 192 internal::g_io_thread_task_runner->PostTask( | 160 FROM_HERE, |
| 193 FROM_HERE, base::Bind(&ShutdownIPCSupportHelper, false)); | 161 base::Bind(&ShutdownIPCSupportHelper), |
| 194 } | 162 base::Bind(&ProcessDelegate::OnShutdownComplete, |
| 195 | 163 base::Unretained(internal::g_process_delegate))); |
| 196 void ShutdownIPCSupportAndWaitForNoChannels() { | |
| 197 g_delegate_task_runner = base::MessageLoop::current()->task_runner().get(); | |
| 198 internal::g_io_thread_task_runner->PostTask( | |
| 199 FROM_HERE, base::Bind(&ShutdownIPCSupportHelper, true)); | |
| 200 } | 164 } |
| 201 | 165 |
| 202 ScopedMessagePipeHandle CreateMessagePipe( | 166 ScopedMessagePipeHandle CreateMessagePipe( |
| 203 ScopedPlatformHandle platform_handle) { | 167 ScopedPlatformHandle platform_handle) { |
| 204 scoped_refptr<MessagePipeDispatcher> dispatcher = | 168 scoped_refptr<MessagePipeDispatcher> dispatcher = |
| 205 MessagePipeDispatcher::Create( | 169 MessagePipeDispatcher::Create( |
| 206 MessagePipeDispatcher::kDefaultCreateOptions); | 170 MessagePipeDispatcher::kDefaultCreateOptions); |
| 207 | 171 |
| 208 ScopedMessagePipeHandle rv( | 172 ScopedMessagePipeHandle rv( |
| 209 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); | 173 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); |
| 210 CHECK(rv.is_valid()); | 174 CHECK(rv.is_valid()); |
| 211 dispatcher->Init(platform_handle.Pass(), nullptr, 0, nullptr, 0, nullptr, | 175 dispatcher->Init(platform_handle.Pass(), nullptr, 0, nullptr, 0, nullptr, |
| 212 nullptr); | 176 nullptr); |
| 213 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it | 177 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it |
| 214 // once that's fixed. | 178 // once that's fixed. |
| 215 return rv.Pass(); | 179 return rv.Pass(); |
| 216 } | 180 } |
| 217 | 181 |
| 218 } // namespace edk | 182 } // namespace edk |
| 219 } // namespace mojo | 183 } // namespace mojo |
| OLD | NEW |