OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/system/embedder.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "mojo/system/channel.h" | |
12 #include "mojo/system/core_impl.h" | |
13 #include "mojo/system/local_message_pipe_endpoint.h" | |
14 #include "mojo/system/message_pipe.h" | |
15 #include "mojo/system/message_pipe_dispatcher.h" | |
16 #include "mojo/system/proxy_message_pipe_endpoint.h" | |
17 | |
18 namespace mojo { | |
19 namespace embedder { | |
20 | |
21 struct ChannelInfo { | |
22 scoped_refptr<system::Channel> channel; | |
23 }; | |
24 | |
25 static void CreateChannelOnIOThread( | |
26 ScopedPlatformHandle platform_handle, | |
27 scoped_refptr<system::MessagePipe> message_pipe, | |
28 DidCreateChannelOnIOThreadCallback callback) { | |
29 CHECK(platform_handle.is_valid()); | |
30 | |
31 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo); | |
32 | |
33 // Create and initialize a |system::Channel|. | |
34 channel_info->channel = new system::Channel(); | |
35 bool success = channel_info->channel->Init(platform_handle.Pass()); | |
36 DCHECK(success); | |
37 | |
38 // Attach the message pipe endpoint. | |
39 system::MessageInTransit::EndpointId endpoint_id = | |
40 channel_info->channel->AttachMessagePipeEndpoint(message_pipe, 1); | |
41 DCHECK_EQ(endpoint_id, system::Channel::kBootstrapEndpointId); | |
42 channel_info->channel->RunMessagePipeEndpoint( | |
43 system::Channel::kBootstrapEndpointId, | |
44 system::Channel::kBootstrapEndpointId); | |
45 | |
46 // Hand the channel back to the embedder. | |
47 callback.Run(channel_info.release()); | |
48 } | |
49 | |
50 void Init() { | |
51 Core::Init(new system::CoreImpl()); | |
52 } | |
53 | |
54 MojoHandle CreateChannel( | |
55 ScopedPlatformHandle platform_handle, | |
56 scoped_refptr<base::TaskRunner> io_thread_task_runner, | |
57 DidCreateChannelOnIOThreadCallback callback) { | |
58 DCHECK(platform_handle.is_valid()); | |
59 | |
60 scoped_refptr<system::MessagePipe> message_pipe( | |
61 new system::MessagePipe(scoped_ptr<system::MessagePipeEndpoint>( | |
62 new system::LocalMessagePipeEndpoint()), | |
63 scoped_ptr<system::MessagePipeEndpoint>( | |
64 new system::ProxyMessagePipeEndpoint()))); | |
65 scoped_refptr<system::MessagePipeDispatcher> dispatcher( | |
66 new system::MessagePipeDispatcher()); | |
67 dispatcher->Init(message_pipe, 0); | |
68 | |
69 system::CoreImpl* core_impl = static_cast<system::CoreImpl*>(Core::Get()); | |
70 DCHECK(core_impl); | |
71 MojoHandle rv = core_impl->AddDispatcher(dispatcher); | |
72 // TODO(vtl): Do we properly handle the failure case here? | |
73 if (rv != MOJO_HANDLE_INVALID) { | |
74 io_thread_task_runner->PostTask(FROM_HERE, | |
75 base::Bind(&CreateChannelOnIOThread, | |
76 base::Passed(&platform_handle), | |
77 message_pipe, | |
78 callback)); | |
79 } | |
80 return rv; | |
81 } | |
82 | |
83 void DestroyChannelOnIOThread(ChannelInfo* channel_info) { | |
84 DCHECK(channel_info); | |
85 DCHECK(channel_info->channel.get()); | |
86 channel_info->channel->Shutdown(); | |
87 delete channel_info; | |
88 } | |
89 | |
90 } // namespace embedder | |
91 } // namespace mojo | |
OLD | NEW |