Index: third_party/mojo/src/mojo/edk/embedder/embedder.cc |
diff --git a/third_party/mojo/src/mojo/edk/embedder/embedder.cc b/third_party/mojo/src/mojo/edk/embedder/embedder.cc |
index e6c27208ef5bb6df5d697a3d87b54267e8ee9076..6cc75754517f0b3097f1b258f492fefd1dd37a18 100644 |
--- a/third_party/mojo/src/mojo/edk/embedder/embedder.cc |
+++ b/third_party/mojo/src/mojo/edk/embedder/embedder.cc |
@@ -10,7 +10,6 @@ |
#include "base/location.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
-#include "base/message_loop/message_loop_proxy.h" |
#include "base/task_runner.h" |
#include "mojo/edk/embedder/embedder_internal.h" |
#include "mojo/edk/embedder/master_process_delegate.h" |
@@ -20,13 +19,11 @@ |
#include "mojo/edk/system/channel.h" |
#include "mojo/edk/system/channel_manager.h" |
#include "mojo/edk/system/configuration.h" |
-#include "mojo/edk/system/connection_manager.h" |
#include "mojo/edk/system/core.h" |
-#include "mojo/edk/system/master_connection_manager.h" |
+#include "mojo/edk/system/ipc_support.h" |
#include "mojo/edk/system/message_pipe_dispatcher.h" |
#include "mojo/edk/system/platform_handle_dispatcher.h" |
#include "mojo/edk/system/raw_channel.h" |
-#include "mojo/edk/system/slave_connection_manager.h" |
namespace mojo { |
namespace embedder { |
@@ -36,30 +33,12 @@ namespace internal { |
// Declared in embedder_internal.h. |
PlatformSupport* g_platform_support = nullptr; |
system::Core* g_core = nullptr; |
-ProcessType g_process_type = ProcessType::UNINITIALIZED; |
+system::IPCSupport* g_ipc_support = nullptr; |
} // namespace internal |
namespace { |
-// The following global variables are set in |InitIPCSupport()| and reset by |
-// |ShutdownIPCSupport()|/|ShutdownIPCSupportOnIOThread()|. |
- |
-// Note: This needs to be |AddRef()|ed/|Release()|d. |
-base::TaskRunner* g_delegate_thread_task_runner = nullptr; |
- |
-ProcessDelegate* g_process_delegate = nullptr; |
- |
-// Note: This needs to be |AddRef()|ed/|Release()|d. |
-base::TaskRunner* g_io_thread_task_runner = nullptr; |
- |
-// Instance of |ConnectionManager| used by the channel manager (below). |
-system::ConnectionManager* g_connection_manager = nullptr; |
- |
-// Instance of |ChannelManager| used by the channel management functions |
-// (|CreateChannel()|, etc.). |
-system::ChannelManager* g_channel_manager = nullptr; |
- |
// TODO(vtl): For now, we need this to be thread-safe (since theoretically we |
// currently support multiple channel creation threads -- possibly one per |
// channel). Eventually, we won't need it to be thread-safe (we'll require a |
@@ -83,10 +62,11 @@ system::ChannelId MakeChannelId() { |
// Note: Called on the I/O thread. |
void ShutdownIPCSupportHelper() { |
- // Save these before nuking them using |ShutdownChannelOnIOThread()|. |
+ // Save these before they get nuked by |ShutdownChannelOnIOThread()|. |
scoped_refptr<base::TaskRunner> delegate_thread_task_runner( |
- g_delegate_thread_task_runner); |
- ProcessDelegate* process_delegate = g_process_delegate; |
+ internal::g_ipc_support->delegate_thread_task_runner()); |
+ ProcessDelegate* process_delegate = |
+ internal::g_ipc_support->process_delegate(); |
ShutdownIPCSupportOnIOThread(); |
@@ -166,131 +146,85 @@ void InitIPCSupport(ProcessType process_type, |
// |Init()| must have already been called. |
DCHECK(internal::g_core); |
// And not |InitIPCSupport()| (without |ShutdownIPCSupport()|). |
- DCHECK(internal::g_process_type == ProcessType::UNINITIALIZED); |
- |
- internal::g_process_type = process_type; |
- |
- DCHECK(delegate_thread_task_runner); |
- DCHECK(!g_delegate_thread_task_runner); |
- g_delegate_thread_task_runner = delegate_thread_task_runner.get(); |
- g_delegate_thread_task_runner->AddRef(); |
- |
- DCHECK(process_delegate->GetType() == process_type); |
- DCHECK(!g_process_delegate); |
- g_process_delegate = process_delegate; |
- |
- DCHECK(io_thread_task_runner); |
- DCHECK(!g_io_thread_task_runner); |
- g_io_thread_task_runner = io_thread_task_runner.get(); |
- g_io_thread_task_runner->AddRef(); |
- |
- DCHECK(!g_connection_manager); |
- switch (process_type) { |
- case ProcessType::UNINITIALIZED: |
- CHECK(false); |
- break; |
- case ProcessType::NONE: |
- DCHECK(!platform_handle.is_valid()); // We wouldn't do anything with it. |
- // Nothing to do. |
- break; |
- case ProcessType::MASTER: |
- DCHECK(!platform_handle.is_valid()); // We wouldn't do anything with it. |
- g_connection_manager = |
- new system::MasterConnectionManager(internal::g_platform_support); |
- static_cast<system::MasterConnectionManager*>(g_connection_manager) |
- ->Init(g_delegate_thread_task_runner, |
- static_cast<MasterProcessDelegate*>(g_process_delegate)); |
- break; |
- case ProcessType::SLAVE: |
- DCHECK(platform_handle.is_valid()); |
- g_connection_manager = |
- new system::SlaveConnectionManager(internal::g_platform_support); |
- static_cast<system::SlaveConnectionManager*>(g_connection_manager) |
- ->Init(g_delegate_thread_task_runner, |
- static_cast<SlaveProcessDelegate*>(g_process_delegate), |
- platform_handle.Pass()); |
- break; |
- } |
+ DCHECK(!internal::g_ipc_support); |
- DCHECK(!g_channel_manager); |
- g_channel_manager = |
- new system::ChannelManager(internal::g_platform_support, |
- io_thread_task_runner, g_connection_manager); |
+ internal::g_ipc_support = new system::IPCSupport( |
+ internal::g_platform_support, process_type, |
+ delegate_thread_task_runner.Pass(), process_delegate, |
+ io_thread_task_runner.Pass(), platform_handle.Pass()); |
} |
void ShutdownIPCSupportOnIOThread() { |
- DCHECK(internal::g_process_type != ProcessType::UNINITIALIZED); |
- |
- g_channel_manager->ShutdownOnIOThread(); |
- delete g_channel_manager; |
- g_channel_manager = nullptr; |
- |
- if (g_connection_manager) { |
- g_connection_manager->Shutdown(); |
- delete g_connection_manager; |
- g_connection_manager = nullptr; |
- } |
- |
- g_io_thread_task_runner->Release(); |
- g_io_thread_task_runner = nullptr; |
+ DCHECK(internal::g_ipc_support); |
- g_delegate_thread_task_runner->Release(); |
- g_delegate_thread_task_runner = nullptr; |
- |
- g_process_delegate = nullptr; |
- |
- internal::g_process_type = ProcessType::UNINITIALIZED; |
+ internal::g_ipc_support->ShutdownOnIOThread(); |
+ delete internal::g_ipc_support; |
+ internal::g_ipc_support = nullptr; |
} |
void ShutdownIPCSupport() { |
- DCHECK(internal::g_process_type != ProcessType::UNINITIALIZED); |
+ DCHECK(internal::g_ipc_support); |
- bool ok = g_io_thread_task_runner->PostTask( |
+ bool ok = internal::g_ipc_support->io_thread_task_runner()->PostTask( |
FROM_HERE, base::Bind(&ShutdownIPCSupportHelper)); |
DCHECK(ok); |
} |
-void ConnectToSlave(SlaveInfo slave_info, |
- ScopedPlatformHandle platform_handle, |
- ScopedPlatformHandle* platform_connection_handle, |
- std::string* platform_connection_id) { |
- DCHECK(platform_handle.is_valid()); |
- DCHECK(platform_connection_handle); |
+ScopedMessagePipeHandle ConnectToSlave( |
+ SlaveInfo slave_info, |
+ ScopedPlatformHandle platform_handle, |
+ const DidConnectToSlaveCallback& callback, |
+ scoped_refptr<base::TaskRunner> callback_thread_task_runner, |
+ std::string* platform_connection_id, |
+ ChannelInfo** channel_info) { |
DCHECK(platform_connection_id); |
- DCHECK(internal::g_process_type == ProcessType::MASTER); |
- DCHECK(g_connection_manager); |
+ DCHECK(channel_info); |
+ DCHECK(internal::g_ipc_support); |
system::ConnectionIdentifier connection_id = |
- g_connection_manager->GenerateConnectionIdentifier(); |
- system::ProcessIdentifier slave_id = system::kInvalidProcessIdentifier; |
- CHECK(static_cast<system::MasterConnectionManager*>(g_connection_manager) |
- ->AddSlaveAndBootstrap(slave_info, platform_handle.Pass(), |
- connection_id, &slave_id)); |
- |
- system::ProcessIdentifier peer_id = system::kInvalidProcessIdentifier; |
- CHECK(g_connection_manager->Connect(connection_id, &peer_id, |
- platform_connection_handle)); |
- DCHECK_EQ(peer_id, slave_id); |
- DCHECK(platform_connection_handle->is_valid()); |
- |
+ internal::g_ipc_support->GenerateConnectionIdentifier(); |
*platform_connection_id = connection_id.ToString(); |
+ system::ChannelId channel_id = system::kInvalidChannelId; |
+ scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
+ internal::g_ipc_support->ConnectToSlave( |
+ connection_id, slave_info, platform_handle.Pass(), callback, |
+ callback_thread_task_runner.Pass(), &channel_id); |
+ *channel_info = new ChannelInfo(channel_id); |
+ |
+ ScopedMessagePipeHandle rv( |
+ MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); |
+ CHECK(rv.is_valid()); |
+ // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it |
+ // once that's fixed. |
+ return rv.Pass(); |
} |
-void ConnectToMaster(const std::string& platform_connection_id, |
- ScopedPlatformHandle* platform_connection_handle) { |
- DCHECK(internal::g_process_type == ProcessType::SLAVE); |
- DCHECK(g_connection_manager); |
+ScopedMessagePipeHandle ConnectToMaster( |
+ const std::string& platform_connection_id, |
+ const DidConnectToMasterCallback& callback, |
+ scoped_refptr<base::TaskRunner> callback_thread_task_runner, |
+ ChannelInfo** channel_info) { |
+ DCHECK(channel_info); |
+ DCHECK(internal::g_ipc_support); |
bool ok = false; |
system::ConnectionIdentifier connection_id = |
system::ConnectionIdentifier::FromString(platform_connection_id, &ok); |
CHECK(ok); |
- system::ProcessIdentifier peer_id; |
- CHECK(g_connection_manager->Connect(connection_id, &peer_id, |
- platform_connection_handle)); |
- DCHECK_EQ(peer_id, system::kMasterProcessIdentifier); |
- DCHECK(platform_connection_handle->is_valid()); |
+ system::ChannelId channel_id = system::kInvalidChannelId; |
+ scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
+ internal::g_ipc_support->ConnectToMaster( |
+ connection_id, callback, callback_thread_task_runner.Pass(), |
+ &channel_id); |
+ *channel_info = new ChannelInfo(channel_id); |
+ |
+ ScopedMessagePipeHandle rv( |
+ MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); |
+ CHECK(rv.is_valid()); |
+ // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it |
+ // once that's fixed. |
+ return rv.Pass(); |
} |
// TODO(vtl): Write tests for this. |
@@ -299,11 +233,15 @@ ScopedMessagePipeHandle CreateChannelOnIOThread( |
ChannelInfo** channel_info) { |
DCHECK(platform_handle.is_valid()); |
DCHECK(channel_info); |
+ DCHECK(internal::g_ipc_support); |
+ |
+ system::ChannelManager* channel_manager = |
+ internal::g_ipc_support->channel_manager(); |
*channel_info = new ChannelInfo(MakeChannelId()); |
scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
- g_channel_manager->CreateChannelOnIOThread((*channel_info)->channel_id, |
- platform_handle.Pass()); |
+ channel_manager->CreateChannelOnIOThread((*channel_info)->channel_id, |
+ platform_handle.Pass()); |
ScopedMessagePipeHandle rv( |
MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); |
@@ -315,18 +253,20 @@ ScopedMessagePipeHandle CreateChannelOnIOThread( |
ScopedMessagePipeHandle CreateChannel( |
ScopedPlatformHandle platform_handle, |
- scoped_refptr<base::TaskRunner> io_thread_task_runner, |
const DidCreateChannelCallback& callback, |
scoped_refptr<base::TaskRunner> callback_thread_task_runner) { |
DCHECK(platform_handle.is_valid()); |
- DCHECK(io_thread_task_runner); |
DCHECK(!callback.is_null()); |
+ DCHECK(internal::g_ipc_support); |
+ |
+ system::ChannelManager* channel_manager = |
+ internal::g_ipc_support->channel_manager(); |
system::ChannelId channel_id = MakeChannelId(); |
scoped_ptr<ChannelInfo> channel_info(new ChannelInfo(channel_id)); |
scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
- g_channel_manager->CreateChannel( |
- channel_id, platform_handle.Pass(), io_thread_task_runner, |
+ channel_manager->CreateChannel( |
+ channel_id, platform_handle.Pass(), |
base::Bind(callback, base::Unretained(channel_info.release())), |
callback_thread_task_runner); |
@@ -342,8 +282,11 @@ ScopedMessagePipeHandle CreateChannel( |
void DestroyChannelOnIOThread(ChannelInfo* channel_info) { |
DCHECK(channel_info); |
DCHECK(channel_info->channel_id); |
- DCHECK(g_channel_manager); |
- g_channel_manager->ShutdownChannelOnIOThread(channel_info->channel_id); |
+ DCHECK(internal::g_ipc_support); |
+ |
+ system::ChannelManager* channel_manager = |
+ internal::g_ipc_support->channel_manager(); |
+ channel_manager->ShutdownChannelOnIOThread(channel_info->channel_id); |
delete channel_info; |
} |
@@ -355,16 +298,22 @@ void DestroyChannel( |
DCHECK(channel_info); |
DCHECK(channel_info->channel_id); |
DCHECK(!callback.is_null()); |
- DCHECK(g_channel_manager); |
- g_channel_manager->ShutdownChannel(channel_info->channel_id, callback, |
- callback_thread_task_runner); |
+ DCHECK(internal::g_ipc_support); |
+ |
+ system::ChannelManager* channel_manager = |
+ internal::g_ipc_support->channel_manager(); |
+ channel_manager->ShutdownChannel(channel_info->channel_id, callback, |
+ callback_thread_task_runner); |
delete channel_info; |
} |
void WillDestroyChannelSoon(ChannelInfo* channel_info) { |
DCHECK(channel_info); |
- DCHECK(g_channel_manager); |
- g_channel_manager->WillShutdownChannel(channel_info->channel_id); |
+ DCHECK(internal::g_ipc_support); |
+ |
+ system::ChannelManager* channel_manager = |
+ internal::g_ipc_support->channel_manager(); |
+ channel_manager->WillShutdownChannel(channel_info->channel_id); |
} |
} // namespace embedder |