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/embedder/embedder.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.h"
13 #include "mojo/system/entrypoints.h"
14 #include "mojo/system/message_in_transit.h"
15 #include "mojo/system/message_pipe.h"
16 #include "mojo/system/message_pipe_dispatcher.h"
17 #include "mojo/system/raw_channel.h"
22 // This is defined here (instead of a header file), since it's opaque to the
23 // outside world. But we need to define it before our (internal-only) functions
26 explicit ChannelInfo(scoped_refptr
<system::Channel
> channel
)
30 scoped_refptr
<system::Channel
> channel
;
35 // Helper for |CreateChannelOnIOThread()|. (Note: May return null for some
37 scoped_refptr
<system::Channel
> MakeChannel(
38 ScopedPlatformHandle platform_handle
,
39 scoped_refptr
<system::MessagePipe
> message_pipe
) {
40 DCHECK(platform_handle
.is_valid());
42 // Create and initialize a |system::Channel|.
43 scoped_refptr
<system::Channel
> channel
= new system::Channel();
44 if (!channel
->Init(system::RawChannel::Create(platform_handle
.Pass()))) {
45 // This is very unusual (e.g., maybe |platform_handle| was invalid or we
46 // reached some system resource limit).
47 LOG(ERROR
) << "Channel::Init() failed";
48 // Return null, since |Shutdown()| shouldn't be called in this case.
49 return scoped_refptr
<system::Channel
>();
51 // Once |Init()| has succeeded, we have to return |channel| (since
52 // |Shutdown()| will have to be called on it).
54 // Attach the message pipe endpoint.
55 system::MessageInTransit::EndpointId endpoint_id
=
56 channel
->AttachMessagePipeEndpoint(message_pipe
, 1);
57 if (endpoint_id
== system::MessageInTransit::kInvalidEndpointId
) {
58 // This means that, e.g., the other endpoint of the message pipe was closed
59 // first. But it's not necessarily an error per se.
60 DVLOG(2) << "Channel::AttachMessagePipeEndpoint() failed";
63 CHECK_EQ(endpoint_id
, system::Channel::kBootstrapEndpointId
);
65 if (!channel
->RunMessagePipeEndpoint(system::Channel::kBootstrapEndpointId
,
66 system::Channel::kBootstrapEndpointId
)) {
67 // Currently, there's no reason for this to fail.
68 NOTREACHED() << "Channel::RunMessagePipeEndpoint() failed";
75 void CreateChannelOnIOThread(
76 ScopedPlatformHandle platform_handle
,
77 scoped_refptr
<system::MessagePipe
> message_pipe
,
78 DidCreateChannelCallback callback
,
79 scoped_refptr
<base::TaskRunner
> callback_thread_task_runner
) {
80 scoped_ptr
<ChannelInfo
> channel_info(
81 new ChannelInfo(MakeChannel(platform_handle
.Pass(), message_pipe
)));
83 // Hand the channel back to the embedder.
84 if (callback_thread_task_runner
) {
85 callback_thread_task_runner
->PostTask(FROM_HERE
,
87 channel_info
.release()));
89 callback
.Run(channel_info
.release());
96 system::entrypoints::SetCore(new system::Core());
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());
106 std::pair
<scoped_refptr
<system::MessagePipeDispatcher
>,
107 scoped_refptr
<system::MessagePipe
> > remote_message_pipe
=
108 system::MessagePipeDispatcher::CreateRemoteMessagePipe();
110 system::Core
* core
= system::entrypoints::GetCore();
112 ScopedMessagePipeHandle
rv(
113 MessagePipeHandle(core
->AddDispatcher(remote_message_pipe
.first
)));
114 // TODO(vtl): Do we properly handle the failure case here?
116 io_thread_task_runner
->PostTask(FROM_HERE
,
117 base::Bind(&CreateChannelOnIOThread
,
118 base::Passed(&platform_handle
),
119 remote_message_pipe
.second
,
121 callback_thread_task_runner
));
126 void DestroyChannelOnIOThread(ChannelInfo
* channel_info
) {
127 DCHECK(channel_info
);
128 if (!channel_info
->channel
) {
129 // Presumably, |Init()| on the channel failed.
133 channel_info
->channel
->Shutdown();
137 } // namespace embedder