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/edk/embedder/embedder.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "mojo/edk/embedder/platform_support.h"
13 #include "mojo/edk/system/channel.h"
14 #include "mojo/edk/system/channel_endpoint.h"
15 #include "mojo/edk/system/channel_info.h"
16 #include "mojo/edk/system/core.h"
17 #include "mojo/edk/system/entrypoints.h"
18 #include "mojo/edk/system/message_pipe_dispatcher.h"
19 #include "mojo/edk/system/platform_handle_dispatcher.h"
20 #include "mojo/edk/system/raw_channel.h"
27 // Helper for |CreateChannel...()|. (Note: May return null for some failures.)
28 scoped_refptr<system::Channel> MakeChannel(
30 ScopedPlatformHandle platform_handle,
31 scoped_refptr<system::ChannelEndpoint> channel_endpoint) {
32 DCHECK(platform_handle.is_valid());
34 // Create and initialize a |system::Channel|.
35 scoped_refptr<system::Channel> channel =
36 new system::Channel(core->platform_support());
37 if (!channel->Init(system::RawChannel::Create(platform_handle.Pass()))) {
38 // This is very unusual (e.g., maybe |platform_handle| was invalid or we
39 // reached some system resource limit).
40 LOG(ERROR) << "Channel::Init() failed";
41 // Return null, since |Shutdown()| shouldn't be called in this case.
42 return scoped_refptr<system::Channel>();
44 // Once |Init()| has succeeded, we have to return |channel| (since
45 // |Shutdown()| will have to be called on it).
47 channel->AttachAndRunEndpoint(channel_endpoint, true);
51 void CreateChannelHelper(
53 ScopedPlatformHandle platform_handle,
54 scoped_ptr<ChannelInfo> channel_info,
55 scoped_refptr<system::ChannelEndpoint> channel_endpoint,
56 DidCreateChannelCallback callback,
57 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
58 channel_info->channel =
59 MakeChannel(core, platform_handle.Pass(), channel_endpoint);
61 // Hand the channel back to the embedder.
62 if (callback_thread_task_runner.get()) {
63 callback_thread_task_runner->PostTask(
64 FROM_HERE, base::Bind(callback, channel_info.release()));
66 callback.Run(channel_info.release());
72 void Init(scoped_ptr<PlatformSupport> platform_support) {
73 system::entrypoints::SetCore(new system::Core(platform_support.Pass()));
76 // TODO(vtl): Write tests for this.
77 ScopedMessagePipeHandle CreateChannelOnIOThread(
78 ScopedPlatformHandle platform_handle,
79 ChannelInfo** channel_info) {
80 DCHECK(platform_handle.is_valid());
83 scoped_refptr<system::ChannelEndpoint> channel_endpoint;
84 scoped_refptr<system::MessagePipeDispatcher> dispatcher =
85 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
87 system::Core* core = system::entrypoints::GetCore();
89 ScopedMessagePipeHandle rv(
90 MessagePipeHandle(core->AddDispatcher(dispatcher)));
92 *channel_info = new ChannelInfo(
93 MakeChannel(core, platform_handle.Pass(), channel_endpoint),
94 base::MessageLoopProxy::current());
99 ScopedMessagePipeHandle CreateChannel(
100 ScopedPlatformHandle platform_handle,
101 scoped_refptr<base::TaskRunner> io_thread_task_runner,
102 DidCreateChannelCallback callback,
103 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
104 DCHECK(platform_handle.is_valid());
105 DCHECK(io_thread_task_runner.get());
106 DCHECK(!callback.is_null());
108 scoped_refptr<system::ChannelEndpoint> channel_endpoint;
109 scoped_refptr<system::MessagePipeDispatcher> dispatcher =
110 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
112 system::Core* core = system::entrypoints::GetCore();
114 ScopedMessagePipeHandle rv(
115 MessagePipeHandle(core->AddDispatcher(dispatcher)));
117 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo());
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;
122 io_thread_task_runner->PostTask(FROM_HERE,
123 base::Bind(&CreateChannelHelper,
124 base::Unretained(core),
125 base::Passed(&platform_handle),
126 base::Passed(&channel_info),
129 callback_thread_task_runner));
131 (callback_thread_task_runner.get() ? callback_thread_task_runner
132 : io_thread_task_runner)
133 ->PostTask(FROM_HERE, base::Bind(callback, channel_info.release()));
139 void DestroyChannelOnIOThread(ChannelInfo* channel_info) {
140 DCHECK(channel_info);
141 if (!channel_info->channel.get()) {
142 // Presumably, |Init()| on the channel failed.
146 channel_info->channel->Shutdown();
150 // TODO(vtl): Write tests for this.
151 void DestroyChannel(ChannelInfo* channel_info) {
152 DCHECK(channel_info);
153 DCHECK(channel_info->channel_thread_task_runner.get());
155 if (!channel_info->channel.get()) {
156 // Presumably, |Init()| on the channel failed.
160 channel_info->channel->WillShutdownSoon();
161 channel_info->channel_thread_task_runner->PostTask(
162 FROM_HERE, base::Bind(&DestroyChannelOnIOThread, channel_info));
165 void WillDestroyChannelSoon(ChannelInfo* channel_info) {
166 DCHECK(channel_info);
167 channel_info->channel->WillShutdownSoon();
170 MojoResult CreatePlatformHandleWrapper(
171 ScopedPlatformHandle platform_handle,
172 MojoHandle* platform_handle_wrapper_handle) {
173 DCHECK(platform_handle_wrapper_handle);
175 scoped_refptr<system::Dispatcher> dispatcher(
176 new system::PlatformHandleDispatcher(platform_handle.Pass()));
178 system::Core* core = system::entrypoints::GetCore();
180 MojoHandle h = core->AddDispatcher(dispatcher);
181 if (h == MOJO_HANDLE_INVALID) {
182 LOG(ERROR) << "Handle table full";
184 return MOJO_RESULT_RESOURCE_EXHAUSTED;
187 *platform_handle_wrapper_handle = h;
188 return MOJO_RESULT_OK;
191 MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,
192 ScopedPlatformHandle* platform_handle) {
193 DCHECK(platform_handle);
195 system::Core* core = system::entrypoints::GetCore();
197 scoped_refptr<system::Dispatcher> dispatcher(
198 core->GetDispatcher(platform_handle_wrapper_handle));
199 if (!dispatcher.get())
200 return MOJO_RESULT_INVALID_ARGUMENT;
202 if (dispatcher->GetType() != system::Dispatcher::kTypePlatformHandle)
203 return MOJO_RESULT_INVALID_ARGUMENT;
206 static_cast<system::PlatformHandleDispatcher*>(dispatcher.get())
207 ->PassPlatformHandle()
209 return MOJO_RESULT_OK;
212 } // namespace embedder