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