| 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/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "mojo/edk/embedder/embedder_internal.h" | 13 #include "mojo/edk/embedder/embedder_internal.h" |
| 14 #include "mojo/edk/embedder/platform_support.h" | 14 #include "mojo/edk/embedder/platform_support.h" |
| 15 #include "mojo/edk/system/channel.h" | 15 #include "mojo/edk/system/channel.h" |
| 16 #include "mojo/edk/system/channel_endpoint.h" |
| 16 #include "mojo/edk/system/channel_manager.h" | 17 #include "mojo/edk/system/channel_manager.h" |
| 17 #include "mojo/edk/system/configuration.h" | 18 #include "mojo/edk/system/configuration.h" |
| 18 #include "mojo/edk/system/core.h" | 19 #include "mojo/edk/system/core.h" |
| 19 #include "mojo/edk/system/message_pipe_dispatcher.h" | 20 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 20 #include "mojo/edk/system/platform_handle_dispatcher.h" | 21 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 21 #include "mojo/edk/system/raw_channel.h" | 22 #include "mojo/edk/system/raw_channel.h" |
| 22 | 23 |
| 23 namespace mojo { | 24 namespace mojo { |
| 24 namespace embedder { | 25 namespace embedder { |
| 25 | 26 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 47 } | 48 } |
| 48 | 49 |
| 49 } // namespace | 50 } // namespace |
| 50 | 51 |
| 51 namespace internal { | 52 namespace internal { |
| 52 | 53 |
| 53 // Declared in embedder_internal.h. | 54 // Declared in embedder_internal.h. |
| 54 PlatformSupport* g_platform_support = nullptr; | 55 PlatformSupport* g_platform_support = nullptr; |
| 55 system::Core* g_core = nullptr; | 56 system::Core* g_core = nullptr; |
| 56 system::ChannelManager* g_channel_manager = nullptr; | 57 system::ChannelManager* g_channel_manager = nullptr; |
| 57 MasterProcessDelegate* g_master_process_delegate = nullptr; | |
| 58 SlaveProcessDelegate* g_slave_process_delegate = nullptr; | |
| 59 | 58 |
| 60 } // namespace internal | 59 } // namespace internal |
| 61 | 60 |
| 62 Configuration* GetConfiguration() { | |
| 63 return system::GetMutableConfiguration(); | |
| 64 } | |
| 65 | |
| 66 void Init(scoped_ptr<PlatformSupport> platform_support) { | 61 void Init(scoped_ptr<PlatformSupport> platform_support) { |
| 67 DCHECK(platform_support); | 62 DCHECK(platform_support); |
| 68 | 63 |
| 69 DCHECK(!internal::g_platform_support); | 64 DCHECK(!internal::g_platform_support); |
| 70 internal::g_platform_support = platform_support.release(); | 65 internal::g_platform_support = platform_support.release(); |
| 71 | 66 |
| 72 DCHECK(!internal::g_core); | 67 DCHECK(!internal::g_core); |
| 73 internal::g_core = new system::Core(internal::g_platform_support); | 68 internal::g_core = new system::Core(internal::g_platform_support); |
| 74 | 69 |
| 75 DCHECK(!internal::g_channel_manager); | 70 DCHECK(!internal::g_channel_manager); |
| 76 internal::g_channel_manager = | 71 internal::g_channel_manager = |
| 77 new system::ChannelManager(internal::g_platform_support); | 72 new system::ChannelManager(internal::g_platform_support); |
| 78 } | 73 } |
| 79 | 74 |
| 80 void InitMaster(scoped_refptr<base::TaskRunner> delegate_thread_task_runner, | 75 Configuration* GetConfiguration() { |
| 81 MasterProcessDelegate* master_process_delegate, | 76 return system::GetMutableConfiguration(); |
| 82 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | |
| 83 // |Init()| must have already been called. | |
| 84 DCHECK(internal::g_core); | |
| 85 | |
| 86 // TODO(vtl): This is temporary. We really want to construct a | |
| 87 // |MasterConnectionManager| here, which will in turn hold on to the delegate. | |
| 88 internal::g_master_process_delegate = master_process_delegate; | |
| 89 } | |
| 90 | |
| 91 void InitSlave(scoped_refptr<base::TaskRunner> delegate_thread_task_runner, | |
| 92 SlaveProcessDelegate* slave_process_delegate, | |
| 93 scoped_refptr<base::TaskRunner> io_thread_task_runner, | |
| 94 ScopedPlatformHandle platform_handle) { | |
| 95 // |Init()| must have already been called. | |
| 96 DCHECK(internal::g_core); | |
| 97 | |
| 98 // TODO(vtl): This is temporary. We really want to construct a | |
| 99 // |SlaveConnectionManager| here, which will in turn hold on to the delegate. | |
| 100 internal::g_slave_process_delegate = slave_process_delegate; | |
| 101 } | 77 } |
| 102 | 78 |
| 103 // TODO(vtl): Write tests for this. | 79 // TODO(vtl): Write tests for this. |
| 104 ScopedMessagePipeHandle CreateChannelOnIOThread( | 80 ScopedMessagePipeHandle CreateChannelOnIOThread( |
| 105 ScopedPlatformHandle platform_handle, | 81 ScopedPlatformHandle platform_handle, |
| 106 ChannelInfo** channel_info) { | 82 ChannelInfo** channel_info) { |
| 107 DCHECK(platform_handle.is_valid()); | 83 DCHECK(platform_handle.is_valid()); |
| 108 DCHECK(channel_info); | 84 DCHECK(channel_info); |
| 109 | 85 |
| 110 *channel_info = new ChannelInfo(MakeChannelId()); | 86 scoped_refptr<system::ChannelEndpoint> channel_endpoint; |
| 111 scoped_refptr<system::MessagePipeDispatcher> dispatcher = | 87 scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
| 112 internal::g_channel_manager->CreateChannelOnIOThread( | 88 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); |
| 113 (*channel_info)->channel_id, platform_handle.Pass()); | |
| 114 | 89 |
| 90 DCHECK(internal::g_core); |
| 115 ScopedMessagePipeHandle rv( | 91 ScopedMessagePipeHandle rv( |
| 116 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); | 92 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); |
| 117 CHECK(rv.is_valid()); | 93 |
| 118 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it | 94 *channel_info = new ChannelInfo(MakeChannelId()); |
| 119 // once that's fixed. | 95 internal::g_channel_manager->CreateChannelOnIOThread( |
| 96 (*channel_info)->channel_id, platform_handle.Pass(), channel_endpoint); |
| 97 |
| 120 return rv.Pass(); | 98 return rv.Pass(); |
| 121 } | 99 } |
| 122 | 100 |
| 123 ScopedMessagePipeHandle CreateChannel( | 101 ScopedMessagePipeHandle CreateChannel( |
| 124 ScopedPlatformHandle platform_handle, | 102 ScopedPlatformHandle platform_handle, |
| 125 scoped_refptr<base::TaskRunner> io_thread_task_runner, | 103 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
| 126 DidCreateChannelCallback callback, | 104 DidCreateChannelCallback callback, |
| 127 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { | 105 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { |
| 128 DCHECK(platform_handle.is_valid()); | 106 DCHECK(platform_handle.is_valid()); |
| 129 DCHECK(io_thread_task_runner); | 107 DCHECK(io_thread_task_runner); |
| 130 DCHECK(!callback.is_null()); | 108 DCHECK(!callback.is_null()); |
| 131 | 109 |
| 132 system::ChannelId channel_id = MakeChannelId(); | 110 scoped_refptr<system::ChannelEndpoint> channel_endpoint; |
| 133 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo(channel_id)); | |
| 134 scoped_refptr<system::MessagePipeDispatcher> dispatcher = | 111 scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
| 135 internal::g_channel_manager->CreateChannel( | 112 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); |
| 136 channel_id, platform_handle.Pass(), io_thread_task_runner, | |
| 137 base::Bind(callback, base::Unretained(channel_info.release())), | |
| 138 callback_thread_task_runner); | |
| 139 | 113 |
| 114 DCHECK(internal::g_core); |
| 140 ScopedMessagePipeHandle rv( | 115 ScopedMessagePipeHandle rv( |
| 141 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); | 116 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); |
| 142 CHECK(rv.is_valid()); | 117 |
| 143 // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it | 118 // We'll have to set |channel_info->channel_id| on the I/O thread. |
| 144 // once that's fixed. | 119 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo()); |
| 120 |
| 121 if (rv.is_valid()) { |
| 122 system::ChannelId channel_id = MakeChannelId(); |
| 123 channel_info->channel_id = channel_id; |
| 124 internal::g_channel_manager->CreateChannel( |
| 125 channel_id, platform_handle.Pass(), channel_endpoint, |
| 126 io_thread_task_runner, |
| 127 base::Bind(callback, base::Unretained(channel_info.release())), |
| 128 callback_thread_task_runner); |
| 129 } else { |
| 130 (callback_thread_task_runner ? callback_thread_task_runner |
| 131 : io_thread_task_runner) |
| 132 ->PostTask(FROM_HERE, base::Bind(callback, channel_info.release())); |
| 133 } |
| 134 |
| 145 return rv.Pass(); | 135 return rv.Pass(); |
| 146 } | 136 } |
| 147 | 137 |
| 148 // TODO(vtl): Write tests for this. | 138 // TODO(vtl): Write tests for this. |
| 149 void DestroyChannel(ChannelInfo* channel_info) { | 139 void DestroyChannel(ChannelInfo* channel_info) { |
| 150 DCHECK(channel_info); | 140 DCHECK(channel_info); |
| 151 DCHECK(channel_info->channel_id); | 141 DCHECK(channel_info->channel_id); |
| 152 DCHECK(internal::g_channel_manager); | 142 DCHECK(internal::g_channel_manager); |
| 153 // This will destroy the channel synchronously if called from the channel | 143 // This will destroy the channel synchronously if called from the channel |
| 154 // thread. | 144 // thread. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 } | 193 } |
| 204 | 194 |
| 205 MojoResult AsyncWait(MojoHandle handle, | 195 MojoResult AsyncWait(MojoHandle handle, |
| 206 MojoHandleSignals signals, | 196 MojoHandleSignals signals, |
| 207 base::Callback<void(MojoResult)> callback) { | 197 base::Callback<void(MojoResult)> callback) { |
| 208 return internal::g_core->AsyncWait(handle, signals, callback); | 198 return internal::g_core->AsyncWait(handle, signals, callback); |
| 209 } | 199 } |
| 210 | 200 |
| 211 } // namespace embedder | 201 } // namespace embedder |
| 212 } // namespace mojo | 202 } // namespace mojo |
| OLD | NEW |