| 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 "chrome/plugin/plugin_channel_base.h" | 5 #include "chrome/plugin/plugin_channel_base.h" |
| 6 | 6 |
| 7 #include <stack> |
| 8 |
| 7 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
| 10 #include "base/lazy_instance.h" |
| 8 #include "chrome/common/child_process.h" | 11 #include "chrome/common/child_process.h" |
| 9 #include "ipc/ipc_sync_message.h" | 12 #include "ipc/ipc_sync_message.h" |
| 10 | 13 |
| 11 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
| 12 #include "ipc/ipc_channel_posix.h" | 15 #include "ipc/ipc_channel_posix.h" |
| 13 #endif | 16 #endif |
| 14 | 17 |
| 15 typedef base::hash_map<std::string, scoped_refptr<PluginChannelBase> > | 18 typedef base::hash_map<std::string, scoped_refptr<PluginChannelBase> > |
| 16 PluginChannelMap; | 19 PluginChannelMap; |
| 17 | 20 |
| 18 static PluginChannelMap g_plugin_channels_; | 21 static PluginChannelMap g_plugin_channels_; |
| 19 | 22 |
| 23 static base::LazyInstance<std::stack<scoped_refptr<PluginChannelBase> > > |
| 24 lazy_plugin_channel_stack_(base::LINKER_INITIALIZED); |
| 20 | 25 |
| 21 PluginChannelBase* PluginChannelBase::GetChannel( | 26 PluginChannelBase* PluginChannelBase::GetChannel( |
| 22 const std::string& channel_name, IPC::Channel::Mode mode, | 27 const std::string& channel_name, IPC::Channel::Mode mode, |
| 23 PluginChannelFactory factory, MessageLoop* ipc_message_loop, | 28 PluginChannelFactory factory, MessageLoop* ipc_message_loop, |
| 24 bool create_pipe_now) { | 29 bool create_pipe_now) { |
| 25 scoped_refptr<PluginChannelBase> channel; | 30 scoped_refptr<PluginChannelBase> channel; |
| 26 | 31 |
| 27 PluginChannelMap::const_iterator iter = g_plugin_channels_.find(channel_name); | 32 PluginChannelMap::const_iterator iter = g_plugin_channels_.find(channel_name); |
| 28 if (iter == g_plugin_channels_.end()) { | 33 if (iter == g_plugin_channels_.end()) { |
| 29 channel = factory(); | 34 channel = factory(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 60 peer_pid_(0), | 65 peer_pid_(0), |
| 61 in_remove_route_(false), | 66 in_remove_route_(false), |
| 62 channel_valid_(false), | 67 channel_valid_(false), |
| 63 in_dispatch_(0), | 68 in_dispatch_(0), |
| 64 send_unblocking_only_during_dispatch_(false) { | 69 send_unblocking_only_during_dispatch_(false) { |
| 65 } | 70 } |
| 66 | 71 |
| 67 PluginChannelBase::~PluginChannelBase() { | 72 PluginChannelBase::~PluginChannelBase() { |
| 68 } | 73 } |
| 69 | 74 |
| 75 PluginChannelBase* PluginChannelBase::GetCurrentChannel() { |
| 76 return lazy_plugin_channel_stack_.Pointer()->top(); |
| 77 } |
| 78 |
| 70 void PluginChannelBase::CleanupChannels() { | 79 void PluginChannelBase::CleanupChannels() { |
| 71 // Make a copy of the references as we can't iterate the map since items will | 80 // Make a copy of the references as we can't iterate the map since items will |
| 72 // be removed from it as we clean them up. | 81 // be removed from it as we clean them up. |
| 73 std::vector<scoped_refptr<PluginChannelBase> > channels; | 82 std::vector<scoped_refptr<PluginChannelBase> > channels; |
| 74 for (PluginChannelMap::const_iterator iter = g_plugin_channels_.begin(); | 83 for (PluginChannelMap::const_iterator iter = g_plugin_channels_.begin(); |
| 75 iter != g_plugin_channels_.end(); | 84 iter != g_plugin_channels_.end(); |
| 76 ++iter) { | 85 ++iter) { |
| 77 channels.push_back(iter->second); | 86 channels.push_back(iter->second); |
| 78 } | 87 } |
| 79 | 88 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 108 return channel_->Send(message); | 117 return channel_->Send(message); |
| 109 } | 118 } |
| 110 | 119 |
| 111 int PluginChannelBase::Count() { | 120 int PluginChannelBase::Count() { |
| 112 return static_cast<int>(g_plugin_channels_.size()); | 121 return static_cast<int>(g_plugin_channels_.size()); |
| 113 } | 122 } |
| 114 | 123 |
| 115 void PluginChannelBase::OnMessageReceived(const IPC::Message& message) { | 124 void PluginChannelBase::OnMessageReceived(const IPC::Message& message) { |
| 116 // This call might cause us to be deleted, so keep an extra reference to | 125 // This call might cause us to be deleted, so keep an extra reference to |
| 117 // ourself so that we can send the reply and decrement back in_dispatch_. | 126 // ourself so that we can send the reply and decrement back in_dispatch_. |
| 118 scoped_refptr<PluginChannelBase> me(this); | 127 lazy_plugin_channel_stack_.Pointer()->push( |
| 128 scoped_refptr<PluginChannelBase>(this)); |
| 119 | 129 |
| 120 in_dispatch_++; | 130 in_dispatch_++; |
| 121 if (message.routing_id() == MSG_ROUTING_CONTROL) { | 131 if (message.routing_id() == MSG_ROUTING_CONTROL) { |
| 122 OnControlMessageReceived(message); | 132 OnControlMessageReceived(message); |
| 123 } else { | 133 } else { |
| 124 bool routed = router_.RouteMessage(message); | 134 bool routed = router_.RouteMessage(message); |
| 125 if (!routed && message.is_sync()) { | 135 if (!routed && message.is_sync()) { |
| 126 // The listener has gone away, so we must respond or else the caller will | 136 // The listener has gone away, so we must respond or else the caller will |
| 127 // hang waiting for a reply. | 137 // hang waiting for a reply. |
| 128 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); | 138 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&message); |
| 129 reply->set_reply_error(); | 139 reply->set_reply_error(); |
| 130 Send(reply); | 140 Send(reply); |
| 131 } | 141 } |
| 132 } | 142 } |
| 133 in_dispatch_--; | 143 in_dispatch_--; |
| 144 |
| 145 lazy_plugin_channel_stack_.Pointer()->pop(); |
| 134 } | 146 } |
| 135 | 147 |
| 136 void PluginChannelBase::OnChannelConnected(int32 peer_pid) { | 148 void PluginChannelBase::OnChannelConnected(int32 peer_pid) { |
| 137 peer_pid_ = peer_pid; | 149 peer_pid_ = peer_pid; |
| 138 } | 150 } |
| 139 | 151 |
| 140 void PluginChannelBase::AddRoute(int route_id, | 152 void PluginChannelBase::AddRoute(int route_id, |
| 141 IPC::Channel::Listener* listener, | 153 IPC::Channel::Listener* listener, |
| 142 bool npobject) { | 154 bool npobject) { |
| 143 if (npobject) { | 155 if (npobject) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 if (channel_valid()) { | 222 if (channel_valid()) { |
| 211 IPC::RemoveAndCloseChannelSocket(channel_name()); | 223 IPC::RemoveAndCloseChannelSocket(channel_name()); |
| 212 } | 224 } |
| 213 #endif | 225 #endif |
| 214 channel_valid_ = false; | 226 channel_valid_ = false; |
| 215 } | 227 } |
| 216 | 228 |
| 217 void PluginChannelBase::SendUnblockingOnlyDuringDispatch() { | 229 void PluginChannelBase::SendUnblockingOnlyDuringDispatch() { |
| 218 send_unblocking_only_during_dispatch_ = true; | 230 send_unblocking_only_during_dispatch_ = true; |
| 219 } | 231 } |
| OLD | NEW |