| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "mojo/edk/embedder/platform_support.h" | 12 #include "mojo/edk/embedder/platform_support.h" |
| 12 #include "mojo/edk/system/channel.h" | 13 #include "mojo/edk/system/channel.h" |
| 13 #include "mojo/edk/system/channel_endpoint.h" | 14 #include "mojo/edk/system/channel_endpoint.h" |
| 15 #include "mojo/edk/system/channel_info.h" |
| 14 #include "mojo/edk/system/core.h" | 16 #include "mojo/edk/system/core.h" |
| 15 #include "mojo/edk/system/entrypoints.h" | 17 #include "mojo/edk/system/entrypoints.h" |
| 16 #include "mojo/edk/system/message_pipe_dispatcher.h" | 18 #include "mojo/edk/system/message_pipe_dispatcher.h" |
| 17 #include "mojo/edk/system/platform_handle_dispatcher.h" | 19 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 18 #include "mojo/edk/system/raw_channel.h" | 20 #include "mojo/edk/system/raw_channel.h" |
| 19 | 21 |
| 20 namespace mojo { | 22 namespace mojo { |
| 21 namespace embedder { | 23 namespace embedder { |
| 22 | 24 |
| 23 // This is defined here (instead of a header file), since it's opaque to the | |
| 24 // outside world. But we need to define it before our (internal-only) functions | |
| 25 // that use it. | |
| 26 struct ChannelInfo { | |
| 27 ChannelInfo() {} | |
| 28 ~ChannelInfo() {} | |
| 29 | |
| 30 scoped_refptr<system::Channel> channel; | |
| 31 | |
| 32 // May be null, in which case |DestroyChannelOnIOThread()| must be used (from | |
| 33 // the IO thread), instead of |DestroyChannel()|. | |
| 34 scoped_refptr<base::TaskRunner> io_thread_task_runner; | |
| 35 }; | |
| 36 | |
| 37 namespace { | 25 namespace { |
| 38 | 26 |
| 39 // Helper for |CreateChannel...()|. (Note: May return null for some failures.) | 27 // Helper for |CreateChannel...()|. (Note: May return null for some failures.) |
| 40 scoped_refptr<system::Channel> MakeChannel( | 28 scoped_refptr<system::Channel> MakeChannel( |
| 41 system::Core* core, | 29 system::Core* core, |
| 42 ScopedPlatformHandle platform_handle, | 30 ScopedPlatformHandle platform_handle, |
| 43 scoped_refptr<system::ChannelEndpoint> channel_endpoint) { | 31 scoped_refptr<system::ChannelEndpoint> channel_endpoint) { |
| 44 DCHECK(platform_handle.is_valid()); | 32 DCHECK(platform_handle.is_valid()); |
| 45 | 33 |
| 46 // Create and initialize a |system::Channel|. | 34 // Create and initialize a |system::Channel|. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 82 |
| 95 scoped_refptr<system::ChannelEndpoint> channel_endpoint; | 83 scoped_refptr<system::ChannelEndpoint> channel_endpoint; |
| 96 scoped_refptr<system::MessagePipeDispatcher> dispatcher = | 84 scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
| 97 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); | 85 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); |
| 98 | 86 |
| 99 system::Core* core = system::entrypoints::GetCore(); | 87 system::Core* core = system::entrypoints::GetCore(); |
| 100 DCHECK(core); | 88 DCHECK(core); |
| 101 ScopedMessagePipeHandle rv( | 89 ScopedMessagePipeHandle rv( |
| 102 MessagePipeHandle(core->AddDispatcher(dispatcher))); | 90 MessagePipeHandle(core->AddDispatcher(dispatcher))); |
| 103 | 91 |
| 104 *channel_info = new ChannelInfo(); | 92 *channel_info = new ChannelInfo( |
| 105 (*channel_info)->channel = | 93 MakeChannel(core, platform_handle.Pass(), channel_endpoint), |
| 106 MakeChannel(core, platform_handle.Pass(), channel_endpoint); | 94 base::MessageLoopProxy::current()); |
| 107 | 95 |
| 108 return rv.Pass(); | 96 return rv.Pass(); |
| 109 } | 97 } |
| 110 | 98 |
| 111 ScopedMessagePipeHandle CreateChannel( | 99 ScopedMessagePipeHandle CreateChannel( |
| 112 ScopedPlatformHandle platform_handle, | 100 ScopedPlatformHandle platform_handle, |
| 113 scoped_refptr<base::TaskRunner> io_thread_task_runner, | 101 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
| 114 DidCreateChannelCallback callback, | 102 DidCreateChannelCallback callback, |
| 115 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { | 103 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { |
| 116 DCHECK(platform_handle.is_valid()); | 104 DCHECK(platform_handle.is_valid()); |
| 105 DCHECK(io_thread_task_runner.get()); |
| 106 DCHECK(!callback.is_null()); |
| 117 | 107 |
| 118 scoped_refptr<system::ChannelEndpoint> channel_endpoint; | 108 scoped_refptr<system::ChannelEndpoint> channel_endpoint; |
| 119 scoped_refptr<system::MessagePipeDispatcher> dispatcher = | 109 scoped_refptr<system::MessagePipeDispatcher> dispatcher = |
| 120 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); | 110 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); |
| 121 | 111 |
| 122 system::Core* core = system::entrypoints::GetCore(); | 112 system::Core* core = system::entrypoints::GetCore(); |
| 123 DCHECK(core); | 113 DCHECK(core); |
| 124 ScopedMessagePipeHandle rv( | 114 ScopedMessagePipeHandle rv( |
| 125 MessagePipeHandle(core->AddDispatcher(dispatcher))); | 115 MessagePipeHandle(core->AddDispatcher(dispatcher))); |
| 126 | 116 |
| 127 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo()); | 117 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo()); |
| 128 channel_info->io_thread_task_runner = io_thread_task_runner; | 118 // We'll have to set |channel_info->channel| on the I/O thread. |
| 119 channel_info->channel_thread_task_runner = io_thread_task_runner; |
| 129 | 120 |
| 130 if (rv.is_valid()) { | 121 if (rv.is_valid()) { |
| 131 io_thread_task_runner->PostTask(FROM_HERE, | 122 io_thread_task_runner->PostTask(FROM_HERE, |
| 132 base::Bind(&CreateChannelHelper, | 123 base::Bind(&CreateChannelHelper, |
| 133 base::Unretained(core), | 124 base::Unretained(core), |
| 134 base::Passed(&platform_handle), | 125 base::Passed(&platform_handle), |
| 135 base::Passed(&channel_info), | 126 base::Passed(&channel_info), |
| 136 channel_endpoint, | 127 channel_endpoint, |
| 137 callback, | 128 callback, |
| 138 callback_thread_task_runner)); | 129 callback_thread_task_runner)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 return; | 143 return; |
| 153 } | 144 } |
| 154 | 145 |
| 155 channel_info->channel->Shutdown(); | 146 channel_info->channel->Shutdown(); |
| 156 delete channel_info; | 147 delete channel_info; |
| 157 } | 148 } |
| 158 | 149 |
| 159 // TODO(vtl): Write tests for this. | 150 // TODO(vtl): Write tests for this. |
| 160 void DestroyChannel(ChannelInfo* channel_info) { | 151 void DestroyChannel(ChannelInfo* channel_info) { |
| 161 DCHECK(channel_info); | 152 DCHECK(channel_info); |
| 162 DCHECK(channel_info->io_thread_task_runner.get()); | 153 DCHECK(channel_info->channel_thread_task_runner.get()); |
| 163 | 154 |
| 164 if (!channel_info->channel.get()) { | 155 if (!channel_info->channel.get()) { |
| 165 // Presumably, |Init()| on the channel failed. | 156 // Presumably, |Init()| on the channel failed. |
| 166 return; | 157 return; |
| 167 } | 158 } |
| 168 | 159 |
| 169 channel_info->channel->WillShutdownSoon(); | 160 channel_info->channel->WillShutdownSoon(); |
| 170 channel_info->io_thread_task_runner->PostTask( | 161 channel_info->channel_thread_task_runner->PostTask( |
| 171 FROM_HERE, base::Bind(&DestroyChannelOnIOThread, channel_info)); | 162 FROM_HERE, base::Bind(&DestroyChannelOnIOThread, channel_info)); |
| 172 } | 163 } |
| 173 | 164 |
| 174 void WillDestroyChannelSoon(ChannelInfo* channel_info) { | 165 void WillDestroyChannelSoon(ChannelInfo* channel_info) { |
| 175 DCHECK(channel_info); | 166 DCHECK(channel_info); |
| 176 channel_info->channel->WillShutdownSoon(); | 167 channel_info->channel->WillShutdownSoon(); |
| 177 } | 168 } |
| 178 | 169 |
| 179 MojoResult CreatePlatformHandleWrapper( | 170 MojoResult CreatePlatformHandleWrapper( |
| 180 ScopedPlatformHandle platform_handle, | 171 ScopedPlatformHandle platform_handle, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 204 |
| 214 *platform_handle = | 205 *platform_handle = |
| 215 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get()) | 206 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get()) |
| 216 ->PassPlatformHandle() | 207 ->PassPlatformHandle() |
| 217 .Pass(); | 208 .Pass(); |
| 218 return MOJO_RESULT_OK; | 209 return MOJO_RESULT_OK; |
| 219 } | 210 } |
| 220 | 211 |
| 221 } // namespace embedder | 212 } // namespace embedder |
| 222 } // namespace mojo | 213 } // namespace mojo |
| OLD | NEW |