| 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/embedder/embedder.h" | 5 #include "mojo/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 "mojo/system/channel.h" | 11 #include "mojo/system/channel.h" |
| 12 #include "mojo/system/core.h" | 12 #include "mojo/system/core.h" |
| 13 #include "mojo/system/entrypoints.h" | 13 #include "mojo/system/entrypoints.h" |
| 14 #include "mojo/system/message_in_transit.h" | 14 #include "mojo/system/message_in_transit.h" |
| 15 #include "mojo/system/message_pipe.h" | 15 #include "mojo/system/message_pipe.h" |
| 16 #include "mojo/system/message_pipe_dispatcher.h" | 16 #include "mojo/system/message_pipe_dispatcher.h" |
| 17 #include "mojo/system/platform_handle_dispatcher.h" | 17 #include "mojo/system/platform_handle_dispatcher.h" |
| 18 #include "mojo/system/raw_channel.h" | 18 #include "mojo/system/raw_channel.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 namespace embedder { | 21 namespace embedder { |
| 22 | 22 |
| 23 // This is defined here (instead of a header file), since it's opaque to the | 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 | 24 // outside world. But we need to define it before our (internal-only) functions |
| 25 // that use it. | 25 // that use it. |
| 26 struct ChannelInfo { | 26 struct ChannelInfo { |
| 27 explicit ChannelInfo(scoped_refptr<system::Channel> channel) | 27 ChannelInfo() {} |
| 28 : channel(channel) {} | |
| 29 ~ChannelInfo() {} | 28 ~ChannelInfo() {} |
| 30 | 29 |
| 31 scoped_refptr<system::Channel> channel; | 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; |
| 32 }; | 35 }; |
| 33 | 36 |
| 34 namespace { | 37 namespace { |
| 35 | 38 |
| 36 // Helper for |CreateChannelOnIOThread()|. (Note: May return null for some | 39 // Helper for |CreateChannel...()|. (Note: May return null for some failures.) |
| 37 // failures.) | |
| 38 scoped_refptr<system::Channel> MakeChannel( | 40 scoped_refptr<system::Channel> MakeChannel( |
| 39 ScopedPlatformHandle platform_handle, | 41 ScopedPlatformHandle platform_handle, |
| 40 scoped_refptr<system::MessagePipe> message_pipe) { | 42 scoped_refptr<system::MessagePipe> message_pipe) { |
| 41 DCHECK(platform_handle.is_valid()); | 43 DCHECK(platform_handle.is_valid()); |
| 42 | 44 |
| 43 // Create and initialize a |system::Channel|. | 45 // Create and initialize a |system::Channel|. |
| 44 scoped_refptr<system::Channel> channel = new system::Channel(); | 46 scoped_refptr<system::Channel> channel = new system::Channel(); |
| 45 if (!channel->Init(system::RawChannel::Create(platform_handle.Pass()))) { | 47 if (!channel->Init(system::RawChannel::Create(platform_handle.Pass()))) { |
| 46 // This is very unusual (e.g., maybe |platform_handle| was invalid or we | 48 // This is very unusual (e.g., maybe |platform_handle| was invalid or we |
| 47 // reached some system resource limit). | 49 // reached some system resource limit). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 66 if (!channel->RunMessagePipeEndpoint(system::Channel::kBootstrapEndpointId, | 68 if (!channel->RunMessagePipeEndpoint(system::Channel::kBootstrapEndpointId, |
| 67 system::Channel::kBootstrapEndpointId)) { | 69 system::Channel::kBootstrapEndpointId)) { |
| 68 // Currently, there's no reason for this to fail. | 70 // Currently, there's no reason for this to fail. |
| 69 NOTREACHED() << "Channel::RunMessagePipeEndpoint() failed"; | 71 NOTREACHED() << "Channel::RunMessagePipeEndpoint() failed"; |
| 70 return channel; | 72 return channel; |
| 71 } | 73 } |
| 72 | 74 |
| 73 return channel; | 75 return channel; |
| 74 } | 76 } |
| 75 | 77 |
| 76 void CreateChannelOnIOThread( | 78 void CreateChannelHelper( |
| 77 ScopedPlatformHandle platform_handle, | 79 ScopedPlatformHandle platform_handle, |
| 80 scoped_ptr<ChannelInfo> channel_info, |
| 78 scoped_refptr<system::MessagePipe> message_pipe, | 81 scoped_refptr<system::MessagePipe> message_pipe, |
| 79 DidCreateChannelCallback callback, | 82 DidCreateChannelCallback callback, |
| 80 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { | 83 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { |
| 81 scoped_ptr<ChannelInfo> channel_info( | 84 channel_info->channel = MakeChannel(platform_handle.Pass(), message_pipe); |
| 82 new ChannelInfo(MakeChannel(platform_handle.Pass(), message_pipe))); | |
| 83 | 85 |
| 84 // Hand the channel back to the embedder. | 86 // Hand the channel back to the embedder. |
| 85 if (callback_thread_task_runner) { | 87 if (callback_thread_task_runner) { |
| 86 callback_thread_task_runner->PostTask( | 88 callback_thread_task_runner->PostTask( |
| 87 FROM_HERE, base::Bind(callback, channel_info.release())); | 89 FROM_HERE, base::Bind(callback, channel_info.release())); |
| 88 } else { | 90 } else { |
| 89 callback.Run(channel_info.release()); | 91 callback.Run(channel_info.release()); |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 } // namespace | 95 } // namespace |
| 94 | 96 |
| 95 void Init() { | 97 void Init() { |
| 96 system::entrypoints::SetCore(new system::Core()); | 98 system::entrypoints::SetCore(new system::Core()); |
| 97 } | 99 } |
| 98 | 100 |
| 101 // TODO(vtl): Write tests for this. |
| 102 ScopedMessagePipeHandle CreateChannelOnIOThread( |
| 103 ScopedPlatformHandle platform_handle, |
| 104 ChannelInfo** channel_info) { |
| 105 DCHECK(platform_handle.is_valid()); |
| 106 DCHECK(channel_info); |
| 107 |
| 108 std::pair<scoped_refptr<system::MessagePipeDispatcher>, |
| 109 scoped_refptr<system::MessagePipe> > remote_message_pipe = |
| 110 system::MessagePipeDispatcher::CreateRemoteMessagePipe(); |
| 111 |
| 112 system::Core* core = system::entrypoints::GetCore(); |
| 113 DCHECK(core); |
| 114 ScopedMessagePipeHandle rv( |
| 115 MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first))); |
| 116 |
| 117 *channel_info = new ChannelInfo(); |
| 118 (*channel_info)->channel = |
| 119 MakeChannel(platform_handle.Pass(), remote_message_pipe.second); |
| 120 |
| 121 return rv.Pass(); |
| 122 } |
| 123 |
| 99 ScopedMessagePipeHandle CreateChannel( | 124 ScopedMessagePipeHandle CreateChannel( |
| 100 ScopedPlatformHandle platform_handle, | 125 ScopedPlatformHandle platform_handle, |
| 101 scoped_refptr<base::TaskRunner> io_thread_task_runner, | 126 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
| 102 DidCreateChannelCallback callback, | 127 DidCreateChannelCallback callback, |
| 103 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { | 128 scoped_refptr<base::TaskRunner> callback_thread_task_runner) { |
| 104 DCHECK(platform_handle.is_valid()); | 129 DCHECK(platform_handle.is_valid()); |
| 105 | 130 |
| 106 std::pair<scoped_refptr<system::MessagePipeDispatcher>, | 131 std::pair<scoped_refptr<system::MessagePipeDispatcher>, |
| 107 scoped_refptr<system::MessagePipe> > remote_message_pipe = | 132 scoped_refptr<system::MessagePipe> > remote_message_pipe = |
| 108 system::MessagePipeDispatcher::CreateRemoteMessagePipe(); | 133 system::MessagePipeDispatcher::CreateRemoteMessagePipe(); |
| 109 | 134 |
| 110 system::Core* core = system::entrypoints::GetCore(); | 135 system::Core* core = system::entrypoints::GetCore(); |
| 111 DCHECK(core); | 136 DCHECK(core); |
| 112 ScopedMessagePipeHandle rv( | 137 ScopedMessagePipeHandle rv( |
| 113 MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first))); | 138 MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first))); |
| 114 // TODO(vtl): Do we properly handle the failure case here? | 139 |
| 140 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo()); |
| 141 channel_info->io_thread_task_runner = io_thread_task_runner; |
| 142 |
| 115 if (rv.is_valid()) { | 143 if (rv.is_valid()) { |
| 116 io_thread_task_runner->PostTask(FROM_HERE, | 144 io_thread_task_runner->PostTask(FROM_HERE, |
| 117 base::Bind(&CreateChannelOnIOThread, | 145 base::Bind(&CreateChannelHelper, |
| 118 base::Passed(&platform_handle), | 146 base::Passed(&platform_handle), |
| 147 base::Passed(&channel_info), |
| 119 remote_message_pipe.second, | 148 remote_message_pipe.second, |
| 120 callback, | 149 callback, |
| 121 callback_thread_task_runner)); | 150 callback_thread_task_runner)); |
| 151 } else { |
| 152 (callback_thread_task_runner ? callback_thread_task_runner |
| 153 : io_thread_task_runner) |
| 154 ->PostTask(FROM_HERE, base::Bind(callback, channel_info.release())); |
| 122 } | 155 } |
| 156 |
| 123 return rv.Pass(); | 157 return rv.Pass(); |
| 124 } | 158 } |
| 125 | 159 |
| 126 void DestroyChannelOnIOThread(ChannelInfo* channel_info) { | 160 void DestroyChannelOnIOThread(ChannelInfo* channel_info) { |
| 127 DCHECK(channel_info); | 161 DCHECK(channel_info); |
| 128 if (!channel_info->channel) { | 162 if (!channel_info->channel) { |
| 129 // Presumably, |Init()| on the channel failed. | 163 // Presumably, |Init()| on the channel failed. |
| 130 return; | 164 return; |
| 131 } | 165 } |
| 132 | 166 |
| 133 channel_info->channel->Shutdown(); | 167 channel_info->channel->Shutdown(); |
| 134 delete channel_info; | 168 delete channel_info; |
| 135 } | 169 } |
| 136 | 170 |
| 171 // TODO(vtl): Write tests for this. |
| 172 void DestroyChannel(ChannelInfo* channel_info) { |
| 173 DCHECK(channel_info); |
| 174 DCHECK(channel_info->io_thread_task_runner); |
| 175 |
| 176 channel_info->io_thread_task_runner->PostTask( |
| 177 FROM_HERE, base::Bind(&DestroyChannelOnIOThread, channel_info)); |
| 178 } |
| 179 |
| 137 MojoResult CreatePlatformHandleWrapper( | 180 MojoResult CreatePlatformHandleWrapper( |
| 138 ScopedPlatformHandle platform_handle, | 181 ScopedPlatformHandle platform_handle, |
| 139 MojoHandle* platform_handle_wrapper_handle) { | 182 MojoHandle* platform_handle_wrapper_handle) { |
| 140 DCHECK(platform_handle_wrapper_handle); | 183 DCHECK(platform_handle_wrapper_handle); |
| 141 | 184 |
| 142 scoped_refptr<system::Dispatcher> dispatcher( | 185 scoped_refptr<system::Dispatcher> dispatcher( |
| 143 new system::PlatformHandleDispatcher(platform_handle.Pass())); | 186 new system::PlatformHandleDispatcher(platform_handle.Pass())); |
| 144 | 187 |
| 145 system::Core* core = system::entrypoints::GetCore(); | 188 system::Core* core = system::entrypoints::GetCore(); |
| 146 DCHECK(core); | 189 DCHECK(core); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 171 | 214 |
| 172 *platform_handle = | 215 *platform_handle = |
| 173 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get()) | 216 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get()) |
| 174 ->PassPlatformHandle() | 217 ->PassPlatformHandle() |
| 175 .Pass(); | 218 .Pass(); |
| 176 return MOJO_RESULT_OK; | 219 return MOJO_RESULT_OK; |
| 177 } | 220 } |
| 178 | 221 |
| 179 } // namespace embedder | 222 } // namespace embedder |
| 180 } // namespace mojo | 223 } // namespace mojo |
| OLD | NEW |