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
3 // found in the LICENSE file.
5 #include "mojo/embedder/embedder.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "mojo/embedder/platform_support.h"
12 #include "mojo/system/channel.h"
13 #include "mojo/system/channel_endpoint.h"
14 #include "mojo/system/core.h"
15 #include "mojo/system/entrypoints.h"
16 #include "mojo/system/message_in_transit.h"
17 #include "mojo/system/message_pipe_dispatcher.h"
18 #include "mojo/system/platform_handle_dispatcher.h"
19 #include "mojo/system/raw_channel.h"
24 // This is defined here (instead of a header file), since it's opaque to the
25 // outside world. But we need to define it before our (internal-only) functions
31 scoped_refptr<system::Channel> channel;
33 // May be null, in which case |DestroyChannelOnIOThread()| must be used (from
34 // the IO thread), instead of |DestroyChannel()|.
35 scoped_refptr<base::TaskRunner> io_thread_task_runner;
40 // Helper for |CreateChannel...()|. (Note: May return null for some failures.)
41 scoped_refptr<system::Channel> MakeChannel(
43 ScopedPlatformHandle platform_handle,
44 scoped_refptr<system::ChannelEndpoint> channel_endpoint) {
45 DCHECK(platform_handle.is_valid());
47 // Create and initialize a |system::Channel|.
48 scoped_refptr<system::Channel> channel =
49 new system::Channel(core->platform_support());
50 if (!channel->Init(system::RawChannel::Create(platform_handle.Pass()))) {
51 // This is very unusual (e.g., maybe |platform_handle| was invalid or we
52 // reached some system resource limit).
53 LOG(ERROR) << "Channel::Init() failed";
54 // Return null, since |Shutdown()| shouldn't be called in this case.
55 return scoped_refptr<system::Channel>();
57 // Once |Init()| has succeeded, we have to return |channel| (since
58 // |Shutdown()| will have to be called on it).
60 // Attach the endpoint.
61 system::MessageInTransit::EndpointId endpoint_id =
62 channel->AttachEndpoint(channel_endpoint);
63 if (endpoint_id == system::MessageInTransit::kInvalidEndpointId) {
64 // This means that, e.g., the other endpoint of the message pipe was closed
65 // first. But it's not necessarily an error per se.
66 DVLOG(2) << "Channel::AttachEndpoint() failed";
69 CHECK_EQ(endpoint_id, system::Channel::kBootstrapEndpointId);
71 if (!channel->RunMessagePipeEndpoint(system::Channel::kBootstrapEndpointId,
72 system::Channel::kBootstrapEndpointId)) {
73 // Currently, there's no reason for this to fail.
74 NOTREACHED() << "Channel::RunMessagePipeEndpoint() failed";
81 void CreateChannelHelper(
83 ScopedPlatformHandle platform_handle,
84 scoped_ptr<ChannelInfo> channel_info,
85 scoped_refptr<system::ChannelEndpoint> channel_endpoint,
86 DidCreateChannelCallback callback,
87 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
88 channel_info->channel =
89 MakeChannel(core, platform_handle.Pass(), channel_endpoint);
91 // Hand the channel back to the embedder.
92 if (callback_thread_task_runner.get()) {
93 callback_thread_task_runner->PostTask(
94 FROM_HERE, base::Bind(callback, channel_info.release()));
96 callback.Run(channel_info.release());
102 void Init(scoped_ptr<PlatformSupport> platform_support) {
103 system::entrypoints::SetCore(new system::Core(platform_support.Pass()));
106 // TODO(vtl): Write tests for this.
107 ScopedMessagePipeHandle CreateChannelOnIOThread(
108 ScopedPlatformHandle platform_handle,
109 ChannelInfo** channel_info) {
110 DCHECK(platform_handle.is_valid());
111 DCHECK(channel_info);
113 scoped_refptr<system::ChannelEndpoint> channel_endpoint;
114 scoped_refptr<system::MessagePipeDispatcher> dispatcher =
115 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
117 system::Core* core = system::entrypoints::GetCore();
119 ScopedMessagePipeHandle rv(
120 MessagePipeHandle(core->AddDispatcher(dispatcher)));
122 *channel_info = new ChannelInfo();
123 (*channel_info)->channel =
124 MakeChannel(core, platform_handle.Pass(), channel_endpoint);
129 ScopedMessagePipeHandle CreateChannel(
130 ScopedPlatformHandle platform_handle,
131 scoped_refptr<base::TaskRunner> io_thread_task_runner,
132 DidCreateChannelCallback callback,
133 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
134 DCHECK(platform_handle.is_valid());
136 scoped_refptr<system::ChannelEndpoint> channel_endpoint;
137 scoped_refptr<system::MessagePipeDispatcher> dispatcher =
138 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
140 system::Core* core = system::entrypoints::GetCore();
142 ScopedMessagePipeHandle rv(
143 MessagePipeHandle(core->AddDispatcher(dispatcher)));
145 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo());
146 channel_info->io_thread_task_runner = io_thread_task_runner;
149 io_thread_task_runner->PostTask(FROM_HERE,
150 base::Bind(&CreateChannelHelper,
151 base::Unretained(core),
152 base::Passed(&platform_handle),
153 base::Passed(&channel_info),
156 callback_thread_task_runner));
158 (callback_thread_task_runner.get() ? callback_thread_task_runner
159 : io_thread_task_runner)
160 ->PostTask(FROM_HERE, base::Bind(callback, channel_info.release()));
166 void DestroyChannelOnIOThread(ChannelInfo* channel_info) {
167 DCHECK(channel_info);
168 if (!channel_info->channel.get()) {
169 // Presumably, |Init()| on the channel failed.
173 channel_info->channel->Shutdown();
177 // TODO(vtl): Write tests for this.
178 void DestroyChannel(ChannelInfo* channel_info) {
179 DCHECK(channel_info);
180 DCHECK(channel_info->io_thread_task_runner.get());
182 if (!channel_info->channel.get()) {
183 // Presumably, |Init()| on the channel failed.
187 channel_info->channel->WillShutdownSoon();
188 channel_info->io_thread_task_runner->PostTask(
189 FROM_HERE, base::Bind(&DestroyChannelOnIOThread, channel_info));
192 void WillDestroyChannelSoon(ChannelInfo* channel_info) {
193 DCHECK(channel_info);
194 channel_info->channel->WillShutdownSoon();
197 MojoResult CreatePlatformHandleWrapper(
198 ScopedPlatformHandle platform_handle,
199 MojoHandle* platform_handle_wrapper_handle) {
200 DCHECK(platform_handle_wrapper_handle);
202 scoped_refptr<system::Dispatcher> dispatcher(
203 new system::PlatformHandleDispatcher(platform_handle.Pass()));
205 system::Core* core = system::entrypoints::GetCore();
207 MojoHandle h = core->AddDispatcher(dispatcher);
208 if (h == MOJO_HANDLE_INVALID) {
209 LOG(ERROR) << "Handle table full";
211 return MOJO_RESULT_RESOURCE_EXHAUSTED;
214 *platform_handle_wrapper_handle = h;
215 return MOJO_RESULT_OK;
218 MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,
219 ScopedPlatformHandle* platform_handle) {
220 DCHECK(platform_handle);
222 system::Core* core = system::entrypoints::GetCore();
224 scoped_refptr<system::Dispatcher> dispatcher(
225 core->GetDispatcher(platform_handle_wrapper_handle));
226 if (!dispatcher.get())
227 return MOJO_RESULT_INVALID_ARGUMENT;
229 if (dispatcher->GetType() != system::Dispatcher::kTypePlatformHandle)
230 return MOJO_RESULT_INVALID_ARGUMENT;
233 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get())
234 ->PassPlatformHandle()
236 return MOJO_RESULT_OK;
239 } // namespace embedder