Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: ipc/mojo/ipc_channel_mojo.cc

Issue 553283002: IPC::ChannelMojo: Introduce IPC::MojoBootstrap for Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed build error Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_host.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ipc/mojo/ipc_channel_mojo.h" 5 #include "ipc/mojo/ipc_channel_mojo.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "ipc/ipc_listener.h" 10 #include "ipc/ipc_listener.h"
11 #include "ipc/mojo/ipc_channel_mojo_host.h"
11 #include "ipc/mojo/ipc_channel_mojo_readers.h" 12 #include "ipc/mojo/ipc_channel_mojo_readers.h"
13 #include "ipc/mojo/ipc_mojo_bootstrap.h"
12 #include "mojo/embedder/embedder.h" 14 #include "mojo/embedder/embedder.h"
13 15
14 #if defined(OS_POSIX) && !defined(OS_NACL) 16 #if defined(OS_POSIX) && !defined(OS_NACL)
15 #include "ipc/file_descriptor_set_posix.h" 17 #include "ipc/file_descriptor_set_posix.h"
16 #endif 18 #endif
17 19
18 namespace IPC { 20 namespace IPC {
19 21
20 namespace { 22 namespace {
21 23
22 // IPC::Listener for bootstrap channels.
23 // It should never receive any message.
24 class NullListener : public Listener {
25 public:
26 virtual bool OnMessageReceived(const Message&) OVERRIDE {
27 NOTREACHED();
28 return false;
29 }
30
31 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
32 NOTREACHED();
33 }
34
35 virtual void OnChannelError() OVERRIDE {
36 NOTREACHED();
37 }
38
39 virtual void OnBadMessageReceived(const Message& message) OVERRIDE {
40 NOTREACHED();
41 }
42 };
43
44 base::LazyInstance<NullListener> g_null_listener = LAZY_INSTANCE_INITIALIZER;
45
46 class MojoChannelFactory : public ChannelFactory { 24 class MojoChannelFactory : public ChannelFactory {
47 public: 25 public:
48 MojoChannelFactory( 26 MojoChannelFactory(ChannelMojoHost* host,
49 ChannelHandle channel_handle, 27 ChannelHandle channel_handle,
50 Channel::Mode mode, 28 Channel::Mode mode)
51 scoped_refptr<base::TaskRunner> io_thread_task_runner) 29 : host_(host), channel_handle_(channel_handle), mode_(mode) {}
52 : channel_handle_(channel_handle),
53 mode_(mode),
54 io_thread_task_runner_(io_thread_task_runner) {
55 }
56 30
57 virtual std::string GetName() const OVERRIDE { 31 virtual std::string GetName() const OVERRIDE {
58 return channel_handle_.name; 32 return channel_handle_.name;
59 } 33 }
60 34
61 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE { 35 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE {
62 return ChannelMojo::Create( 36 return ChannelMojo::Create(host_, channel_handle_, mode_, listener)
63 channel_handle_, 37 .PassAs<Channel>();
64 mode_,
65 listener,
66 io_thread_task_runner_).PassAs<Channel>();
67 } 38 }
68 39
69 private: 40 private:
41 ChannelMojoHost* host_;
70 ChannelHandle channel_handle_; 42 ChannelHandle channel_handle_;
71 Channel::Mode mode_; 43 Channel::Mode mode_;
72 scoped_refptr<base::TaskRunner> io_thread_task_runner_;
73 }; 44 };
74 45
75 mojo::embedder::PlatformHandle ToPlatformHandle(
76 const ChannelHandle& handle) {
77 #if defined(OS_POSIX) && !defined(OS_NACL)
78 return mojo::embedder::PlatformHandle(handle.socket.fd);
79 #elif defined(OS_WIN)
80 return mojo::embedder::PlatformHandle(handle.pipe.handle);
81 #else
82 #error "Unsupported Platform!"
83 #endif
84 }
85
86 } // namespace 46 } // namespace
87 47
88 //------------------------------------------------------------------------------ 48 //------------------------------------------------------------------------------
89 49
90 void ChannelMojo::ChannelInfoDeleter::operator()( 50 void ChannelMojo::ChannelInfoDeleter::operator()(
91 mojo::embedder::ChannelInfo* ptr) const { 51 mojo::embedder::ChannelInfo* ptr) const {
92 mojo::embedder::DestroyChannelOnIOThread(ptr); 52 mojo::embedder::DestroyChannelOnIOThread(ptr);
93 } 53 }
94 54
95 //------------------------------------------------------------------------------ 55 //------------------------------------------------------------------------------
96 56
97 // static 57 // static
98 scoped_ptr<ChannelMojo> ChannelMojo::Create( 58 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojoHost* host,
99 const ChannelHandle &channel_handle, Mode mode, Listener* listener, 59 const ChannelHandle& channel_handle,
100 scoped_refptr<base::TaskRunner> io_thread_task_runner) { 60 Mode mode,
101 return make_scoped_ptr( 61 Listener* listener) {
102 new ChannelMojo(channel_handle, mode, listener, io_thread_task_runner)); 62 return make_scoped_ptr(new ChannelMojo(host, channel_handle, mode, listener));
103 } 63 }
104 64
105 // static 65 // static
106 scoped_ptr<ChannelFactory> ChannelMojo::CreateFactory( 66 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory(
107 const ChannelHandle &channel_handle, Mode mode, 67 ChannelMojoHost* host,
108 scoped_refptr<base::TaskRunner> io_thread_task_runner) { 68 const ChannelHandle& channel_handle) {
109 return make_scoped_ptr( 69 return make_scoped_ptr(
110 new MojoChannelFactory( 70 new MojoChannelFactory(host, channel_handle, Channel::MODE_SERVER))
111 channel_handle, mode, 71 .PassAs<ChannelFactory>();
112 io_thread_task_runner)).PassAs<ChannelFactory>();
113 } 72 }
114 73
115 ChannelMojo::ChannelMojo(const ChannelHandle& channel_handle, 74 // static
75 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory(
76 const ChannelHandle& channel_handle) {
77 return make_scoped_ptr(
78 new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT))
79 .PassAs<ChannelFactory>();
80 }
81
82 ChannelMojo::ChannelMojo(ChannelMojoHost* host,
83 const ChannelHandle& handle,
116 Mode mode, 84 Mode mode,
117 Listener* listener, 85 Listener* listener)
118 scoped_refptr<base::TaskRunner> io_thread_task_runner) 86 : host_(host),
119 : bootstrap_(
120 Channel::Create(channel_handle, mode, g_null_listener.Pointer())),
121 mode_(mode), 87 mode_(mode),
122 listener_(listener), 88 listener_(listener),
123 peer_pid_(base::kNullProcessId), 89 peer_pid_(base::kNullProcessId),
124 weak_factory_(this) { 90 weak_factory_(this) {
125 if (base::MessageLoopProxy::current() == io_thread_task_runner.get()) { 91 // Create MojoBootstrap after all members are set as it touches
126 InitOnIOThread(); 92 // ChannelMojo from a different thread.
127 } else { 93 bootstrap_ = MojoBootstrap::Create(handle, mode, this);
128 io_thread_task_runner->PostTask(FROM_HERE, 94 if (host_)
129 base::Bind(&ChannelMojo::InitOnIOThread, 95 host_->OnChannelCreated(this);
130 weak_factory_.GetWeakPtr()));
131 }
132 } 96 }
133 97
134 ChannelMojo::~ChannelMojo() { 98 ChannelMojo::~ChannelMojo() {
135 Close(); 99 Close();
100
101 if (host_)
102 host_->OnChannelDestroyed();
136 } 103 }
137 104
138 void ChannelMojo::InitOnIOThread() { 105 void ChannelMojo::InitControlReader(
106 mojo::embedder::ScopedPlatformHandle handle) {
107 DCHECK(base::MessageLoopForIO::IsCurrent());
139 mojo::embedder::ChannelInfo* channel_info; 108 mojo::embedder::ChannelInfo* channel_info;
140 mojo::ScopedMessagePipeHandle control_pipe = 109 mojo::ScopedMessagePipeHandle control_pipe =
141 mojo::embedder::CreateChannelOnIOThread( 110 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info);
142 mojo::embedder::ScopedPlatformHandle(
143 ToPlatformHandle(bootstrap_->TakePipeHandle())),
144 &channel_info);
145 channel_info_.reset(channel_info); 111 channel_info_.reset(channel_info);
146 112
147 switch (mode_) { 113 switch (mode_) {
148 case MODE_SERVER: 114 case MODE_SERVER:
149 control_reader_.reset( 115 control_reader_.reset(
150 new internal::ServerControlReader(control_pipe.Pass(), this)); 116 new internal::ServerControlReader(control_pipe.Pass(), this));
151 break; 117 break;
152 case MODE_CLIENT: 118 case MODE_CLIENT:
153 control_reader_.reset( 119 control_reader_.reset(
154 new internal::ClientControlReader(control_pipe.Pass(), this)); 120 new internal::ClientControlReader(control_pipe.Pass(), this));
155 break; 121 break;
156 default: 122 default:
157 NOTREACHED(); 123 NOTREACHED();
158 break; 124 break;
159 } 125 }
160 } 126 }
161 127
162 bool ChannelMojo::Connect() { 128 bool ChannelMojo::Connect() {
163 DCHECK(!message_reader_); 129 DCHECK(!message_reader_);
164 return control_reader_->Connect(); 130 DCHECK(!control_reader_);
131 return bootstrap_->Connect();
165 } 132 }
166 133
167 void ChannelMojo::Close() { 134 void ChannelMojo::Close() {
168 control_reader_.reset(); 135 control_reader_.reset();
169 message_reader_.reset(); 136 message_reader_.reset();
170 channel_info_.reset(); 137 channel_info_.reset();
171 } 138 }
172 139
140 void ChannelMojo::OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) {
141 InitControlReader(handle.Pass());
142 control_reader_->Connect();
143 }
144
145 void ChannelMojo::OnBootstrapError() {
146 listener_->OnChannelError();
147 }
148
173 void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) { 149 void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) {
174 message_reader_ = 150 message_reader_ =
175 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); 151 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this));
176 152
177 for (size_t i = 0; i < pending_messages_.size(); ++i) { 153 for (size_t i = 0; i < pending_messages_.size(); ++i) {
178 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); 154 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i]));
179 pending_messages_[i] = NULL; 155 pending_messages_[i] = NULL;
180 if (!sent) { 156 if (!sent) {
181 pending_messages_.clear(); 157 pending_messages_.clear();
182 listener_->OnChannelError(); 158 listener_->OnChannelError();
(...skipping 22 matching lines...) Expand all
205 } 181 }
206 182
207 return message_reader_->Send(make_scoped_ptr(message)); 183 return message_reader_->Send(make_scoped_ptr(message));
208 } 184 }
209 185
210 base::ProcessId ChannelMojo::GetPeerPID() const { 186 base::ProcessId ChannelMojo::GetPeerPID() const {
211 return peer_pid_; 187 return peer_pid_;
212 } 188 }
213 189
214 base::ProcessId ChannelMojo::GetSelfPID() const { 190 base::ProcessId ChannelMojo::GetSelfPID() const {
215 return bootstrap_->GetSelfPID(); 191 return base::GetCurrentProcId();
216 } 192 }
217 193
218 ChannelHandle ChannelMojo::TakePipeHandle() { 194 ChannelHandle ChannelMojo::TakePipeHandle() {
219 return bootstrap_->TakePipeHandle(); 195 NOTREACHED();
196 return ChannelHandle();
197 }
198
199 void ChannelMojo::OnClientLaunched(base::ProcessHandle handle) {
200 bootstrap_->OnClientLaunched(handle);
220 } 201 }
221 202
222 void ChannelMojo::OnMessageReceived(Message& message) { 203 void ChannelMojo::OnMessageReceived(Message& message) {
223 listener_->OnMessageReceived(message); 204 listener_->OnMessageReceived(message);
224 if (message.dispatch_error()) 205 if (message.dispatch_error())
225 listener_->OnBadMessageReceived(message); 206 listener_->OnBadMessageReceived(message);
226 } 207 }
227 208
228 #if defined(OS_POSIX) && !defined(OS_NACL) 209 #if defined(OS_POSIX) && !defined(OS_NACL)
229 int ChannelMojo::GetClientFileDescriptor() const { 210 int ChannelMojo::GetClientFileDescriptor() const {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 handles->push_back(wrapped_handle); 266 handles->push_back(wrapped_handle);
286 } 267 }
287 } 268 }
288 269
289 return MOJO_RESULT_OK; 270 return MOJO_RESULT_OK;
290 } 271 }
291 272
292 #endif // defined(OS_POSIX) && !defined(OS_NACL) 273 #endif // defined(OS_POSIX) && !defined(OS_NACL)
293 274
294 } // namespace IPC 275 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698