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