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" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "mojo/system/channel.h" |
7 #include "mojo/system/core_impl.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" |
8 | 17 |
9 namespace mojo { | 18 namespace mojo { |
| 19 |
| 20 namespace embedder { |
| 21 |
| 22 struct ChannelInfo { |
| 23 scoped_refptr<system::Channel> channel; |
| 24 }; |
| 25 |
| 26 } // namespace embedder |
| 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, |
| 35 scoped_refptr<MessagePipe> message_pipe, |
| 36 embedder::DidCreateChannelOnIOThreadCallback callback) { |
| 37 CHECK(platform_handle.is_valid()); |
| 38 |
| 39 scoped_ptr<embedder::ChannelInfo> channel_info(new embedder::ChannelInfo); |
| 40 |
| 41 // Create and initialize |Channel|. |
| 42 channel_info->channel = new Channel(); |
| 43 bool success = channel_info->channel->Init(platform_handle.Pass()); |
| 44 DCHECK(success); |
| 45 |
| 46 // Attach the message pipe endpoint. |
| 47 MessageInTransit::EndpointId endpoint_id = |
| 48 channel_info->channel->AttachMessagePipeEndpoint(message_pipe, 1); |
| 49 DCHECK_EQ(endpoint_id, Channel::kBootstrapEndpointId); |
| 50 channel_info->channel->RunMessagePipeEndpoint(Channel::kBootstrapEndpointId, |
| 51 Channel::kBootstrapEndpointId); |
| 52 |
| 53 // Hand the channel back to the embedder. |
| 54 callback.Run(channel_info.release()); |
| 55 } |
| 56 |
| 57 MojoHandle CreateChannelHelper( |
| 58 ScopedPlatformHandle platform_handle, |
| 59 scoped_refptr<base::TaskRunner> io_thread_task_runner, |
| 60 embedder::DidCreateChannelOnIOThreadCallback callback) { |
| 61 DCHECK(platform_handle.is_valid()); |
| 62 |
| 63 scoped_refptr<MessagePipe> message_pipe(new MessagePipe( |
| 64 scoped_ptr<MessagePipeEndpoint>(new LocalMessagePipeEndpoint()), |
| 65 scoped_ptr<MessagePipeEndpoint>(new ProxyMessagePipeEndpoint()))); |
| 66 scoped_refptr<MessagePipeDispatcher> dispatcher(new MessagePipeDispatcher()); |
| 67 dispatcher->Init(message_pipe, 0); |
| 68 |
| 69 CoreImpl* core_impl = static_cast<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 } // namespace |
| 84 } // namespace system |
| 85 |
10 namespace embedder { | 86 namespace embedder { |
11 | 87 |
12 void Init() { | 88 void Init() { |
13 Core::Init(new system::CoreImpl()); | 89 Core::Init(new system::CoreImpl()); |
14 } | 90 } |
15 | 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) { |
| 102 DCHECK(channel_info); |
| 103 DCHECK(channel_info->channel.get()); |
| 104 channel_info->channel->Shutdown(); |
| 105 delete channel_info; |
| 106 } |
| 107 |
16 } // namespace embedder | 108 } // namespace embedder |
17 } // namespace mojo | 109 } // namespace mojo |
OLD | NEW |