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 "ipc/mojo/ipc_mojo_bootstrap.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/process/process_handle.h" | |
9 #include "ipc/ipc_message_utils.h" | |
10 #include "ipc/ipc_platform_file.h" | |
11 #include "mojo/embedder/platform_channel_pair.h" | |
12 | |
13 namespace IPC { | |
14 | |
15 // MojoBootstrap | |
16 | |
17 // static | |
18 scoped_ptr<MojoBootstrap> MojoBootstrap::Create(ChannelHandle handle, | |
19 Channel::Mode mode, | |
20 Delegate* delegate) { | |
21 CHECK(mode == Channel::MODE_CLIENT || mode == Channel::MODE_SERVER); | |
22 scoped_ptr<MojoBootstrap> self = | |
23 mode == Channel::MODE_CLIENT | |
24 ? scoped_ptr<MojoBootstrap>(new MojoClientBootstrap()) | |
25 : scoped_ptr<MojoBootstrap>(new MojoServerBootstrap()); | |
26 scoped_ptr<Channel> bootstrap_channel = | |
27 Channel::Create(handle, mode, self.get()); | |
28 self->Init(bootstrap_channel.Pass(), delegate); | |
29 return self.Pass(); | |
30 } | |
31 | |
32 MojoBootstrap::MojoBootstrap() : delegate_(NULL), state_(STATE_INITIALIZED) { | |
33 } | |
34 | |
35 MojoBootstrap::~MojoBootstrap() { | |
36 } | |
37 | |
38 void MojoBootstrap::Init(scoped_ptr<Channel> channel, Delegate* delegate) { | |
39 channel_ = channel.Pass(); | |
40 delegate_ = delegate; | |
41 } | |
42 | |
43 bool MojoBootstrap::Connect() { | |
44 return channel_->Connect(); | |
45 } | |
46 | |
47 void MojoBootstrap::OnBadMessageReceived(const Message& message) { | |
48 delegate_->OnBootstrapError(); | |
49 } | |
50 | |
51 void MojoBootstrap::OnChannelError() { | |
52 if (state_ == STATE_READY) | |
53 return; | |
54 DLOG(WARNING) << "Detected error on Mojo bootstrap channel."; | |
55 delegate()->OnBootstrapError(); | |
56 } | |
57 | |
58 bool MojoBootstrap::Send(Message* message) { | |
59 return channel_->Send(message); | |
60 } | |
61 | |
62 #if defined(OS_POSIX) && !defined(OS_NACL) | |
63 int MojoBootstrap::GetClientFileDescriptor() const { | |
64 return channel_->GetClientFileDescriptor(); | |
65 } | |
66 | |
67 int MojoBootstrap::TakeClientFileDescriptor() { | |
68 return channel_->TakeClientFileDescriptor(); | |
69 } | |
70 #endif // defined(OS_POSIX) && !defined(OS_NACL) | |
71 | |
72 // MojoServerBootstrap | |
73 | |
74 MojoServerBootstrap::MojoServerBootstrap() | |
75 : client_process_(base::kNullProcessHandle), connected_(false) { | |
76 } | |
77 | |
78 void MojoServerBootstrap::SendClientPipe() { | |
79 DCHECK(state() == STATE_INITIALIZED); | |
viettrungluu
2014/09/15 20:19:44
DCHECK_EQ
| |
80 DCHECK(client_process_ != base::kNullProcessHandle); | |
viettrungluu
2014/09/15 20:19:43
DCHECK_NE
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
81 | |
viettrungluu
2014/09/15 20:19:43
DCHECK(connected_) also?
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
82 mojo::embedder::PlatformChannelPair channel_pair; | |
83 server_pipe_ = channel_pair.PassServerHandle(); | |
84 PlatformFileForTransit client_pipe = GetFileHandleForProcess( | |
85 channel_pair.PassClientHandle().release().ToPlaformFile(), | |
86 client_process_, | |
87 true); | |
88 CHECK(client_pipe != IPC::InvalidPlatformFileForTransit()); | |
89 scoped_ptr<Message> message(new Message()); | |
90 ParamTraits<PlatformFileForTransit>::Write(message.get(), client_pipe); | |
91 Send(message.release()); | |
92 | |
93 set_state(STATE_WAITING_ACK); | |
94 } | |
95 | |
96 void MojoServerBootstrap::SendClientPipeIfReady() { | |
97 // Is the client launched? | |
98 if (client_process_ == base::kNullProcessHandle) | |
99 return; | |
100 // Has the bootstrap channel made? | |
viettrungluu
2014/09/15 20:19:43
"made" -> "been made"
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
101 if (!connected_) | |
102 return; | |
103 | |
104 SendClientPipe(); | |
105 } | |
106 | |
107 void MojoServerBootstrap::OnClientLaunched(base::ProcessHandle process) { | |
108 DCHECK(state() == STATE_INITIALIZED); | |
viettrungluu
2014/09/15 20:19:44
DCHECK_EQ
(etc.)
Hajime Morrita
2014/09/15 22:01:37
Done.
| |
109 DCHECK(process != base::kNullProcessHandle); | |
110 client_process_ = process; | |
111 SendClientPipeIfReady(); | |
112 } | |
113 | |
114 void MojoServerBootstrap::OnChannelConnected(int32 peer_pid) { | |
115 DCHECK(state() == STATE_INITIALIZED); | |
116 connected_ = true; | |
117 SendClientPipeIfReady(); | |
viettrungluu
2014/09/15 20:19:43
Is the order of OnClientLaunched and OnChannelConn
Hajime Morrita
2014/09/15 22:01:37
Unfortunately not. RenderHost launches the child p
| |
118 } | |
119 | |
120 bool MojoServerBootstrap::OnMessageReceived(const Message&) { | |
121 DCHECK(state() == STATE_WAITING_ACK); | |
122 set_state(STATE_READY); | |
123 | |
124 delegate()->OnPipeAvailable( | |
125 mojo::embedder::ScopedPlatformHandle(server_pipe_.release())); | |
126 | |
127 return true; | |
128 } | |
129 | |
130 // MojoClientBootstrap | |
131 | |
132 MojoClientBootstrap::MojoClientBootstrap() { | |
133 } | |
134 | |
135 bool MojoClientBootstrap::OnMessageReceived(const Message& message) { | |
136 PlatformFileForTransit pipe; | |
137 PickleIterator iter(message); | |
138 if (!ParamTraits<PlatformFileForTransit>::Read(&message, &iter, &pipe)) { | |
139 DLOG(WARNING) << "Failed to read a file handle from bootstrap channel."; | |
140 message.set_dispatch_error(); | |
141 return false; | |
142 } | |
143 | |
144 // Sends ACK back. | |
145 Send(new Message()); | |
146 set_state(STATE_READY); | |
147 delegate()->OnPipeAvailable( | |
148 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle( | |
149 PlatformFileForTransitToPlatformFile(pipe)))); | |
150 | |
151 return true; | |
152 } | |
153 | |
154 void MojoClientBootstrap::OnClientLaunched(base::ProcessHandle process) { | |
155 // This notification should happen only on server processes. | |
156 NOTREACHED(); | |
157 } | |
158 | |
159 void MojoClientBootstrap::OnChannelConnected(int32 peer_pid) { | |
160 } | |
161 | |
162 } // namespace IPC | |
OLD | NEW |