Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / embedder / embedder.cc
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/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.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"
18
19 namespace mojo {
20 namespace embedder {
21
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
24 // that use it.
25 struct ChannelInfo {
26   explicit ChannelInfo(scoped_refptr<system::Channel> channel)
27       : channel(channel) {}
28   ~ChannelInfo() {}
29
30   scoped_refptr<system::Channel> channel;
31 };
32
33 namespace {
34
35 // Helper for |CreateChannelOnIOThread()|. (Note: May return null for some
36 // failures.)
37 scoped_refptr<system::Channel> MakeChannel(
38     ScopedPlatformHandle platform_handle,
39     scoped_refptr<system::MessagePipe> message_pipe) {
40   DCHECK(platform_handle.is_valid());
41
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>();
50   }
51   // Once |Init()| has succeeded, we have to return |channel| (since
52   // |Shutdown()| will have to be called on it).
53
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";
61     return channel;
62   }
63   CHECK_EQ(endpoint_id, system::Channel::kBootstrapEndpointId);
64
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";
69     return channel;
70   }
71
72   return channel;
73 }
74
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)));
82
83   // Hand the channel back to the embedder.
84   if (callback_thread_task_runner) {
85     callback_thread_task_runner->PostTask(FROM_HERE,
86                                           base::Bind(callback,
87                                                      channel_info.release()));
88   } else {
89     callback.Run(channel_info.release());
90   }
91 }
92
93 }  // namespace
94
95 void Init() {
96   system::entrypoints::SetCore(new system::Core());
97 }
98
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());
105
106   std::pair<scoped_refptr<system::MessagePipeDispatcher>,
107             scoped_refptr<system::MessagePipe> > remote_message_pipe =
108       system::MessagePipeDispatcher::CreateRemoteMessagePipe();
109
110   system::Core* core = system::entrypoints::GetCore();
111   DCHECK(core);
112   ScopedMessagePipeHandle rv(
113       MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first)));
114   // TODO(vtl): Do we properly handle the failure case here?
115   if (rv.is_valid()) {
116     io_thread_task_runner->PostTask(FROM_HERE,
117                                     base::Bind(&CreateChannelOnIOThread,
118                                                base::Passed(&platform_handle),
119                                                remote_message_pipe.second,
120                                                callback,
121                                                callback_thread_task_runner));
122   }
123   return rv.Pass();
124 }
125
126 void DestroyChannelOnIOThread(ChannelInfo* channel_info) {
127   DCHECK(channel_info);
128   if (!channel_info->channel) {
129     // Presumably, |Init()| on the channel failed.
130     return;
131   }
132
133   channel_info->channel->Shutdown();
134   delete channel_info;
135 }
136
137 }  // namespace embedder
138 }  // namespace mojo