Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/ref_counted.h" | 6 #include "base/ref_counted.h" |
| 7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 8 #include "base/thread.h" | 8 #include "base/thread.h" |
| 9 #include "ipc/ipc_channel_proxy.h" | 9 #include "ipc/ipc_channel_proxy.h" |
| 10 #include "ipc/ipc_logging.h" | 10 #include "ipc/ipc_logging.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 delete this; | 58 delete this; |
| 59 } | 59 } |
| 60 | 60 |
| 61 //------------------------------------------------------------------------------ | 61 //------------------------------------------------------------------------------ |
| 62 | 62 |
| 63 ChannelProxy::Context::Context(Channel::Listener* listener, | 63 ChannelProxy::Context::Context(Channel::Listener* listener, |
| 64 MessageLoop* ipc_message_loop) | 64 MessageLoop* ipc_message_loop) |
| 65 : listener_message_loop_(MessageLoop::current()), | 65 : listener_message_loop_(MessageLoop::current()), |
| 66 listener_(listener), | 66 listener_(listener), |
| 67 ipc_message_loop_(ipc_message_loop), | 67 ipc_message_loop_(ipc_message_loop), |
| 68 channel_(NULL), | |
| 69 peer_pid_(0), | 68 peer_pid_(0), |
| 70 channel_connected_called_(false) { | 69 channel_connected_called_(false) { |
| 71 } | 70 } |
| 72 | 71 |
| 73 void ChannelProxy::Context::CreateChannel(const std::string& id, | 72 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, |
| 74 const Channel::Mode& mode) { | 73 const Channel::Mode& mode) { |
| 75 DCHECK(channel_ == NULL); | 74 DCHECK(channel_.get() == NULL); |
| 76 channel_id_ = id; | 75 channel_id_ = handle.name; |
| 77 channel_ = new Channel(id, mode, this); | 76 channel_.reset(new Channel(handle, mode, this)); |
| 78 } | 77 } |
| 79 | 78 |
| 80 bool ChannelProxy::Context::TryFilters(const Message& message) { | 79 bool ChannelProxy::Context::TryFilters(const Message& message) { |
| 81 #ifdef IPC_MESSAGE_LOG_ENABLED | 80 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 82 Logging* logger = Logging::current(); | 81 Logging* logger = Logging::current(); |
| 83 if (logger->Enabled()) | 82 if (logger->Enabled()) |
| 84 logger->OnPreDispatchMessage(message); | 83 logger->OnPreDispatchMessage(message); |
| 85 #endif | 84 #endif |
| 86 | 85 |
| 87 for (size_t i = 0; i < filters_.size(); ++i) { | 86 for (size_t i = 0; i < filters_.size(); ++i) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 // Assume a reference to ourselves on behalf of this thread. This reference | 146 // Assume a reference to ourselves on behalf of this thread. This reference |
| 148 // will be released when we are closed. | 147 // will be released when we are closed. |
| 149 AddRef(); | 148 AddRef(); |
| 150 | 149 |
| 151 if (!channel_->Connect()) { | 150 if (!channel_->Connect()) { |
| 152 OnChannelError(); | 151 OnChannelError(); |
| 153 return; | 152 return; |
| 154 } | 153 } |
| 155 | 154 |
| 156 for (size_t i = 0; i < filters_.size(); ++i) | 155 for (size_t i = 0; i < filters_.size(); ++i) |
| 157 filters_[i]->OnFilterAdded(channel_); | 156 filters_[i]->OnFilterAdded(channel_.get()); |
| 158 } | 157 } |
| 159 | 158 |
| 160 // Called on the IPC::Channel thread | 159 // Called on the IPC::Channel thread |
| 161 void ChannelProxy::Context::OnChannelClosed() { | 160 void ChannelProxy::Context::OnChannelClosed() { |
| 162 // It's okay for IPC::ChannelProxy::Close to be called more than once, which | 161 // It's okay for IPC::ChannelProxy::Close to be called more than once, which |
| 163 // would result in this branch being taken. | 162 // would result in this branch being taken. |
| 164 if (!channel_) | 163 if (!channel_.get()) |
| 165 return; | 164 return; |
| 166 | 165 |
| 167 for (size_t i = 0; i < filters_.size(); ++i) { | 166 for (size_t i = 0; i < filters_.size(); ++i) { |
| 168 filters_[i]->OnChannelClosing(); | 167 filters_[i]->OnChannelClosing(); |
| 169 filters_[i]->OnFilterRemoved(); | 168 filters_[i]->OnFilterRemoved(); |
| 170 } | 169 } |
| 171 | 170 |
| 172 // We don't need the filters anymore. | 171 // We don't need the filters anymore. |
| 173 filters_.clear(); | 172 filters_.clear(); |
| 174 | 173 |
| 175 delete channel_; | 174 channel_.reset(); |
| 176 channel_ = NULL; | |
| 177 | 175 |
| 178 // Balance with the reference taken during startup. This may result in | 176 // Balance with the reference taken during startup. This may result in |
| 179 // self-destruction. | 177 // self-destruction. |
| 180 Release(); | 178 Release(); |
| 181 } | 179 } |
| 182 | 180 |
| 183 // Called on the IPC::Channel thread | 181 // Called on the IPC::Channel thread |
| 184 void ChannelProxy::Context::OnSendMessage(Message* message) { | 182 void ChannelProxy::Context::OnSendMessage(Message* message) { |
| 185 if (!channel_) { | 183 if (!channel_.get()) { |
| 186 delete message; | 184 delete message; |
| 187 OnChannelClosed(); | 185 OnChannelClosed(); |
| 188 return; | 186 return; |
| 189 } | 187 } |
| 190 if (!channel_->Send(message)) | 188 if (!channel_->Send(message)) |
| 191 OnChannelError(); | 189 OnChannelError(); |
| 192 } | 190 } |
| 193 | 191 |
| 194 // Called on the IPC::Channel thread | 192 // Called on the IPC::Channel thread |
| 195 void ChannelProxy::Context::OnAddFilter() { | 193 void ChannelProxy::Context::OnAddFilter() { |
| 196 std::vector<scoped_refptr<MessageFilter> > filters; | 194 std::vector<scoped_refptr<MessageFilter> > filters; |
| 197 { | 195 { |
| 198 AutoLock auto_lock(pending_filters_lock_); | 196 AutoLock auto_lock(pending_filters_lock_); |
| 199 filters.swap(pending_filters_); | 197 filters.swap(pending_filters_); |
| 200 } | 198 } |
| 201 | 199 |
| 202 for (size_t i = 0; i < filters.size(); ++i) { | 200 for (size_t i = 0; i < filters.size(); ++i) { |
| 203 filters_.push_back(filters[i]); | 201 filters_.push_back(filters[i]); |
| 204 | 202 |
| 205 // If the channel has already been created, then we need to send this | 203 // If the channel has already been created, then we need to send this |
| 206 // message so that the filter gets access to the Channel. | 204 // message so that the filter gets access to the Channel. |
| 207 if (channel_) | 205 if (channel_.get()) |
| 208 filters[i]->OnFilterAdded(channel_); | 206 filters[i]->OnFilterAdded(channel_.get()); |
| 209 // Ditto for the peer process id. | 207 // Ditto for the peer process id. |
| 210 if (peer_pid_) | 208 if (peer_pid_) |
| 211 filters[i]->OnChannelConnected(peer_pid_); | 209 filters[i]->OnChannelConnected(peer_pid_); |
| 212 } | 210 } |
| 213 } | 211 } |
| 214 | 212 |
| 215 // Called on the IPC::Channel thread | 213 // Called on the IPC::Channel thread |
| 216 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { | 214 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { |
| 217 for (size_t i = 0; i < filters_.size(); ++i) { | 215 for (size_t i = 0; i < filters_.size(); ++i) { |
| 218 if (filters_[i].get() == filter) { | 216 if (filters_[i].get() == filter) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 } | 269 } |
| 272 | 270 |
| 273 // Called on the listener's thread | 271 // Called on the listener's thread |
| 274 void ChannelProxy::Context::OnDispatchError() { | 272 void ChannelProxy::Context::OnDispatchError() { |
| 275 if (listener_) | 273 if (listener_) |
| 276 listener_->OnChannelError(); | 274 listener_->OnChannelError(); |
| 277 } | 275 } |
| 278 | 276 |
| 279 //----------------------------------------------------------------------------- | 277 //----------------------------------------------------------------------------- |
| 280 | 278 |
| 281 ChannelProxy::ChannelProxy(const std::string& channel_id, | 279 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, |
| 282 Channel::Mode mode, | 280 Channel::Mode mode, |
| 283 Channel::Listener* listener, | 281 Channel::Listener* listener, |
| 284 MessageLoop* ipc_thread) | 282 MessageLoop* ipc_thread) |
| 285 : context_(new Context(listener, ipc_thread)) { | 283 : context_(new Context(listener, ipc_thread)) { |
| 286 Init(channel_id, mode, ipc_thread, true); | 284 Init(channel_handle, mode, ipc_thread, true); |
| 287 } | 285 } |
| 288 | 286 |
| 289 ChannelProxy::ChannelProxy(const std::string& channel_id, | 287 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, |
| 290 Channel::Mode mode, | 288 Channel::Mode mode, |
| 291 MessageLoop* ipc_thread, | 289 MessageLoop* ipc_thread, |
| 292 Context* context, | 290 Context* context, |
| 293 bool create_pipe_now) | 291 bool create_pipe_now) |
| 294 : context_(context) { | 292 : context_(context) { |
| 295 Init(channel_id, mode, ipc_thread, create_pipe_now); | 293 Init(channel_handle, mode, ipc_thread, create_pipe_now); |
| 296 } | 294 } |
| 297 | 295 |
| 298 ChannelProxy::~ChannelProxy() { | 296 ChannelProxy::~ChannelProxy() { |
| 299 Close(); | 297 Close(); |
| 300 } | 298 } |
| 301 | 299 |
| 302 void ChannelProxy::Init(const std::string& channel_id, Channel::Mode mode, | 300 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
| 303 MessageLoop* ipc_thread_loop, bool create_pipe_now) { | 301 Channel::Mode mode, MessageLoop* ipc_thread_loop, |
| 302 bool create_pipe_now) { | |
| 303 #if defined(OS_POSIX) | |
| 304 // When we are creating a server on POSIX, we need it's file descriptor | |
|
agl
2010/12/08 15:38:57
s/it's/its/
| |
| 305 // to be created immediately so that it can be accessed and passed | |
| 306 // to other processes. Forcing it to be made immediately avoids | |
|
agl
2010/12/08 15:38:57
s/made/created/
| |
| 307 // race conditions that may otherwise arise. | |
| 308 if (mode == Channel::MODE_SERVER) { | |
| 309 create_pipe_now = true; | |
| 310 } | |
| 311 #endif // defined(OS_POSIX) | |
| 312 | |
| 304 if (create_pipe_now) { | 313 if (create_pipe_now) { |
| 305 // Create the channel immediately. This effectively sets up the | 314 // Create the channel immediately. This effectively sets up the |
| 306 // low-level pipe so that the client can connect. Without creating | 315 // low-level pipe so that the client can connect. Without creating |
| 307 // the pipe immediately, it is possible for a listener to attempt | 316 // the pipe immediately, it is possible for a listener to attempt |
| 308 // to connect and get an error since the pipe doesn't exist yet. | 317 // to connect and get an error since the pipe doesn't exist yet. |
| 309 context_->CreateChannel(channel_id, mode); | 318 context_->CreateChannel(channel_handle, mode); |
| 310 } else { | 319 } else { |
| 311 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 320 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 312 context_.get(), &Context::CreateChannel, channel_id, mode)); | 321 context_.get(), &Context::CreateChannel, channel_handle, mode)); |
| 313 } | 322 } |
| 314 | 323 |
| 315 // complete initialization on the background thread | 324 // complete initialization on the background thread |
| 316 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 325 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 317 context_.get(), &Context::OnChannelOpened)); | 326 context_.get(), &Context::OnChannelOpened)); |
| 318 } | 327 } |
| 319 | 328 |
| 320 void ChannelProxy::Close() { | 329 void ChannelProxy::Close() { |
| 321 // Clear the backpointer to the listener so that any pending calls to | 330 // Clear the backpointer to the listener so that any pending calls to |
| 322 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 331 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 353 | 362 |
| 354 void ChannelProxy::ClearIPCMessageLoop() { | 363 void ChannelProxy::ClearIPCMessageLoop() { |
| 355 context()->ClearIPCMessageLoop(); | 364 context()->ClearIPCMessageLoop(); |
| 356 } | 365 } |
| 357 | 366 |
| 358 #if defined(OS_POSIX) && !defined(OS_NACL) | 367 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 359 // See the TODO regarding lazy initialization of the channel in | 368 // See the TODO regarding lazy initialization of the channel in |
| 360 // ChannelProxy::Init(). | 369 // ChannelProxy::Init(). |
| 361 // We assume that IPC::Channel::GetClientFileDescriptorMapping() is thread-safe. | 370 // We assume that IPC::Channel::GetClientFileDescriptorMapping() is thread-safe. |
| 362 int ChannelProxy::GetClientFileDescriptor() const { | 371 int ChannelProxy::GetClientFileDescriptor() const { |
| 363 Channel *channel = context_.get()->channel_; | 372 Channel *channel = context_.get()->channel_.get(); |
| 364 DCHECK(channel); // Channel must have been created first. | 373 // Channel must have been created first. |
| 374 DCHECK(channel) << context_.get()->channel_id_; | |
| 365 return channel->GetClientFileDescriptor(); | 375 return channel->GetClientFileDescriptor(); |
| 366 } | 376 } |
| 367 #endif | 377 #endif |
| 368 | 378 |
| 369 //----------------------------------------------------------------------------- | 379 //----------------------------------------------------------------------------- |
| 370 | 380 |
| 371 } // namespace IPC | 381 } // namespace IPC |
| OLD | NEW |