| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser/renderer_host/socket_stream_dispatcher_host.h" | 5 #include "chrome/browser/renderer_host/socket_stream_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/renderer_host/socket_stream_host.h" | 8 #include "chrome/browser/renderer_host/socket_stream_host.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/net/socket_stream.h" | 10 #include "chrome/common/net/socket_stream.h" |
| 11 #include "ipc/ipc_message.h" | |
| 12 #include "net/websockets/websocket_job.h" | 11 #include "net/websockets/websocket_job.h" |
| 13 #include "net/websockets/websocket_throttle.h" | 12 #include "net/websockets/websocket_throttle.h" |
| 14 | 13 |
| 15 SocketStreamDispatcherHost::SocketStreamDispatcherHost() : receiver_(NULL) { | 14 SocketStreamDispatcherHost::SocketStreamDispatcherHost() { |
| 16 net::WebSocketJob::EnsureInit(); | 15 net::WebSocketJob::EnsureInit(); |
| 17 } | 16 } |
| 18 | 17 |
| 19 SocketStreamDispatcherHost::~SocketStreamDispatcherHost() { | 18 SocketStreamDispatcherHost::~SocketStreamDispatcherHost() { |
| 20 // TODO(ukai): Implement IDMap::RemoveAll(). | 19 // TODO(ukai): Implement IDMap::RemoveAll(). |
| 21 for (IDMap< IDMap<SocketStreamHost> >::const_iterator iter(&hostmap_); | 20 for (IDMap<SocketStreamHost>::const_iterator iter(&hosts_); |
| 22 !iter.IsAtEnd(); | 21 !iter.IsAtEnd(); |
| 23 iter.Advance()) { | 22 iter.Advance()) { |
| 24 int host_id = iter.GetCurrentKey(); | 23 int socket_id = iter.GetCurrentKey(); |
| 25 CancelRequestsForProcess(host_id); | 24 const SocketStreamHost* socket_stream_host = iter.GetCurrentValue(); |
| 25 delete socket_stream_host; |
| 26 hosts_.Remove(socket_id); |
| 26 } | 27 } |
| 27 } | 28 } |
| 28 | 29 |
| 29 bool SocketStreamDispatcherHost::OnMessageReceived( | 30 bool SocketStreamDispatcherHost::OnMessageReceived(const IPC::Message& message, |
| 30 const IPC::Message& msg, | 31 bool* message_was_ok) { |
| 31 ResourceDispatcherHost::Receiver* receiver, | |
| 32 bool* msg_ok) { | |
| 33 if (!IsSocketStreamDispatcherHostMessage(msg)) | |
| 34 return false; | |
| 35 | |
| 36 *msg_ok = true; | |
| 37 bool handled = true; | 32 bool handled = true; |
| 38 receiver_ = receiver; | 33 IPC_BEGIN_MESSAGE_MAP_EX(SocketStreamDispatcherHost, message, *message_was_ok) |
| 39 IPC_BEGIN_MESSAGE_MAP_EX(SocketStreamDispatcherHost, msg, *msg_ok) | |
| 40 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Connect, OnConnect) | 34 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Connect, OnConnect) |
| 41 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_SendData, OnSendData) | 35 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_SendData, OnSendData) |
| 42 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Close, OnCloseReq) | 36 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Close, OnCloseReq) |
| 43 IPC_MESSAGE_UNHANDLED(handled = false) | 37 IPC_MESSAGE_UNHANDLED(handled = false) |
| 44 IPC_END_MESSAGE_MAP_EX() | 38 IPC_END_MESSAGE_MAP_EX() |
| 45 receiver_ = NULL; | |
| 46 return handled; | 39 return handled; |
| 47 } | 40 } |
| 48 | 41 |
| 49 void SocketStreamDispatcherHost::CancelRequestsForProcess(int host_id) { | |
| 50 IDMap<SocketStreamHost>* hosts = hostmap_.Lookup(host_id); | |
| 51 if (hosts == NULL) | |
| 52 return; | |
| 53 for (IDMap<SocketStreamHost>::const_iterator hosts_iter(hosts); | |
| 54 !hosts_iter.IsAtEnd(); | |
| 55 hosts_iter.Advance()) { | |
| 56 const SocketStreamHost* socket_stream_host = hosts_iter.GetCurrentValue(); | |
| 57 delete socket_stream_host; | |
| 58 int socket_id = hosts_iter.GetCurrentKey(); | |
| 59 hosts->Remove(socket_id); | |
| 60 } | |
| 61 hostmap_.Remove(host_id); | |
| 62 delete hosts; | |
| 63 } | |
| 64 | |
| 65 // SocketStream::Delegate methods implementations. | 42 // SocketStream::Delegate methods implementations. |
| 66 void SocketStreamDispatcherHost::OnConnected(net::SocketStream* socket, | 43 void SocketStreamDispatcherHost::OnConnected(net::SocketStream* socket, |
| 67 int max_pending_send_allowed) { | 44 int max_pending_send_allowed) { |
| 68 SocketStreamHost* socket_stream_host = | 45 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 69 SocketStreamHost::GetSocketStreamHost(socket); | |
| 70 DCHECK(socket_stream_host); | |
| 71 int socket_id = socket_stream_host->socket_id(); | |
| 72 DVLOG(1) << "SocketStreamDispatcherHost::OnConnected socket_id=" << socket_id | 46 DVLOG(1) << "SocketStreamDispatcherHost::OnConnected socket_id=" << socket_id |
| 73 << " max_pending_send_allowed=" << max_pending_send_allowed; | 47 << " max_pending_send_allowed=" << max_pending_send_allowed; |
| 74 if (socket_id == chrome_common_net::kNoSocketId) { | 48 if (socket_id == chrome_common_net::kNoSocketId) { |
| 75 LOG(ERROR) << "NoSocketId in OnConnected"; | 49 LOG(ERROR) << "NoSocketId in OnConnected"; |
| 76 return; | 50 return; |
| 77 } | 51 } |
| 78 if (!socket_stream_host->Connected(max_pending_send_allowed)) { | 52 if (!Send(new ViewMsg_SocketStream_Connected( |
| 53 socket_id, max_pending_send_allowed))) { |
| 79 LOG(ERROR) << "ViewMsg_SocketStream_Connected failed."; | 54 LOG(ERROR) << "ViewMsg_SocketStream_Connected failed."; |
| 80 DeleteSocketStreamHost(socket_stream_host->receiver()->id(), socket_id); | 55 DeleteSocketStreamHost(socket_id); |
| 81 } | 56 } |
| 82 } | 57 } |
| 83 | 58 |
| 84 void SocketStreamDispatcherHost::OnSentData(net::SocketStream* socket, | 59 void SocketStreamDispatcherHost::OnSentData(net::SocketStream* socket, |
| 85 int amount_sent) { | 60 int amount_sent) { |
| 86 SocketStreamHost* socket_stream_host = | 61 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 87 SocketStreamHost::GetSocketStreamHost(socket); | |
| 88 DCHECK(socket_stream_host); | |
| 89 int socket_id = socket_stream_host->socket_id(); | |
| 90 DVLOG(1) << "SocketStreamDispatcherHost::OnSentData socket_id=" << socket_id | 62 DVLOG(1) << "SocketStreamDispatcherHost::OnSentData socket_id=" << socket_id |
| 91 << " amount_sent=" << amount_sent; | 63 << " amount_sent=" << amount_sent; |
| 92 if (socket_id == chrome_common_net::kNoSocketId) { | 64 if (socket_id == chrome_common_net::kNoSocketId) { |
| 93 LOG(ERROR) << "NoSocketId in OnReceivedData"; | 65 LOG(ERROR) << "NoSocketId in OnReceivedData"; |
| 94 return; | 66 return; |
| 95 } | 67 } |
| 96 if (!socket_stream_host->SentData(amount_sent)) { | 68 if (!Send(new ViewMsg_SocketStream_SentData(socket_id, amount_sent))) { |
| 97 LOG(ERROR) << "ViewMsg_SocketStream_SentData failed."; | 69 LOG(ERROR) << "ViewMsg_SocketStream_SentData failed."; |
| 98 DeleteSocketStreamHost(socket_stream_host->receiver()->id(), socket_id); | 70 DeleteSocketStreamHost(socket_id); |
| 99 } | 71 } |
| 100 } | 72 } |
| 101 | 73 |
| 102 void SocketStreamDispatcherHost::OnReceivedData( | 74 void SocketStreamDispatcherHost::OnReceivedData( |
| 103 net::SocketStream* socket, const char* data, int len) { | 75 net::SocketStream* socket, const char* data, int len) { |
| 104 SocketStreamHost* socket_stream_host = | 76 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 105 SocketStreamHost::GetSocketStreamHost(socket); | |
| 106 DCHECK(socket_stream_host); | |
| 107 int socket_id = socket_stream_host->socket_id(); | |
| 108 DVLOG(1) << "SocketStreamDispatcherHost::OnReceiveData socket_id=" | 77 DVLOG(1) << "SocketStreamDispatcherHost::OnReceiveData socket_id=" |
| 109 << socket_id; | 78 << socket_id; |
| 110 if (socket_id == chrome_common_net::kNoSocketId) { | 79 if (socket_id == chrome_common_net::kNoSocketId) { |
| 111 LOG(ERROR) << "NoSocketId in OnReceivedData"; | 80 LOG(ERROR) << "NoSocketId in OnReceivedData"; |
| 112 return; | 81 return; |
| 113 } | 82 } |
| 114 if (!socket_stream_host->ReceivedData(data, len)) { | 83 if (!Send(new ViewMsg_SocketStream_ReceivedData( |
| 84 socket_id, std::vector<char>(data, data + len)))) { |
| 115 LOG(ERROR) << "ViewMsg_SocketStream_ReceivedData failed."; | 85 LOG(ERROR) << "ViewMsg_SocketStream_ReceivedData failed."; |
| 116 DeleteSocketStreamHost(socket_stream_host->receiver()->id(), socket_id); | 86 DeleteSocketStreamHost(socket_id); |
| 117 } | 87 } |
| 118 } | 88 } |
| 119 | 89 |
| 120 void SocketStreamDispatcherHost::OnClose(net::SocketStream* socket) { | 90 void SocketStreamDispatcherHost::OnClose(net::SocketStream* socket) { |
| 121 SocketStreamHost* socket_stream_host = | 91 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 122 SocketStreamHost::GetSocketStreamHost(socket); | |
| 123 DCHECK(socket_stream_host); | |
| 124 int socket_id = socket_stream_host->socket_id(); | |
| 125 DVLOG(1) << "SocketStreamDispatcherHost::OnClosed socket_id=" << socket_id; | 92 DVLOG(1) << "SocketStreamDispatcherHost::OnClosed socket_id=" << socket_id; |
| 126 if (socket_id == chrome_common_net::kNoSocketId) { | 93 if (socket_id == chrome_common_net::kNoSocketId) { |
| 127 LOG(ERROR) << "NoSocketId in OnClose"; | 94 LOG(ERROR) << "NoSocketId in OnClose"; |
| 128 return; | 95 return; |
| 129 } | 96 } |
| 130 DeleteSocketStreamHost(socket_stream_host->receiver()->id(), socket_id); | 97 DeleteSocketStreamHost(socket_id); |
| 131 } | 98 } |
| 132 | 99 |
| 133 // Message handlers called by OnMessageReceived. | 100 // Message handlers called by OnMessageReceived. |
| 134 void SocketStreamDispatcherHost::OnConnect(const GURL& url, int socket_id) { | 101 void SocketStreamDispatcherHost::OnConnect(const GURL& url, int socket_id) { |
| 135 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect url=" << url | 102 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect url=" << url |
| 136 << " socket_id=" << socket_id; | 103 << " socket_id=" << socket_id; |
| 137 DCHECK_NE(chrome_common_net::kNoSocketId, socket_id); | 104 DCHECK_NE(chrome_common_net::kNoSocketId, socket_id); |
| 138 DCHECK(receiver_); | 105 if (hosts_.Lookup(socket_id)) { |
| 139 if (LookupHostMap(receiver_->id(), socket_id)) { | 106 LOG(ERROR) << "socket_id=" << socket_id << " already registered."; |
| 140 LOG(ERROR) << "host_id=" << receiver_->id() | |
| 141 << " socket_id=" << socket_id << " already registered."; | |
| 142 return; | 107 return; |
| 143 } | 108 } |
| 144 SocketStreamHost* socket_stream_host = | 109 SocketStreamHost* socket_stream_host = new SocketStreamHost(this, socket_id); |
| 145 new SocketStreamHost(this, receiver_, socket_id); | 110 hosts_.AddWithID(socket_stream_host, socket_id); |
| 146 AddHostMap(receiver_->id(), socket_id, socket_stream_host); | |
| 147 socket_stream_host->Connect(url); | 111 socket_stream_host->Connect(url); |
| 148 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect -> " << socket_id; | 112 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect -> " << socket_id; |
| 149 } | 113 } |
| 150 | 114 |
| 151 void SocketStreamDispatcherHost::OnSendData( | 115 void SocketStreamDispatcherHost::OnSendData( |
| 152 int socket_id, const std::vector<char>& data) { | 116 int socket_id, const std::vector<char>& data) { |
| 153 DVLOG(1) << "SocketStreamDispatcherHost::OnSendData socket_id=" << socket_id; | 117 DVLOG(1) << "SocketStreamDispatcherHost::OnSendData socket_id=" << socket_id; |
| 154 DCHECK(receiver_); | 118 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 155 SocketStreamHost* socket_stream_host = | |
| 156 LookupHostMap(receiver_->id(), socket_id); | |
| 157 if (!socket_stream_host) { | 119 if (!socket_stream_host) { |
| 158 LOG(ERROR) << "host_id=" << receiver_->id() | 120 LOG(ERROR) << "socket_id=" << socket_id << " already closed."; |
| 159 << " socket_id=" << socket_id << " already closed."; | |
| 160 return; | 121 return; |
| 161 } | 122 } |
| 162 if (!socket_stream_host->SendData(data)) { | 123 if (!socket_stream_host->SendData(data)) { |
| 163 // Cannot accept more data to send. | 124 // Cannot accept more data to send. |
| 164 socket_stream_host->Close(); | 125 socket_stream_host->Close(); |
| 165 } | 126 } |
| 166 } | 127 } |
| 167 | 128 |
| 168 void SocketStreamDispatcherHost::OnCloseReq(int socket_id) { | 129 void SocketStreamDispatcherHost::OnCloseReq(int socket_id) { |
| 169 DVLOG(1) << "SocketStreamDispatcherHost::OnCloseReq socket_id=" << socket_id; | 130 DVLOG(1) << "SocketStreamDispatcherHost::OnCloseReq socket_id=" << socket_id; |
| 170 DCHECK(receiver_); | 131 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 171 SocketStreamHost* socket_stream_host = | |
| 172 LookupHostMap(receiver_->id(), socket_id); | |
| 173 if (!socket_stream_host) | 132 if (!socket_stream_host) |
| 174 return; | 133 return; |
| 175 socket_stream_host->Close(); | 134 socket_stream_host->Close(); |
| 176 } | 135 } |
| 177 | 136 |
| 178 void SocketStreamDispatcherHost::DeleteSocketStreamHost( | 137 void SocketStreamDispatcherHost::DeleteSocketStreamHost(int socket_id) { |
| 179 int host_id, int socket_id) { | 138 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 180 SocketStreamHost* socket_stream_host = LookupHostMap(host_id, socket_id); | |
| 181 DCHECK(socket_stream_host); | 139 DCHECK(socket_stream_host); |
| 182 delete socket_stream_host; | 140 delete socket_stream_host; |
| 183 IDMap<SocketStreamHost>* hosts = hostmap_.Lookup(host_id); | 141 hosts_.Remove(socket_id); |
| 184 DCHECK(hosts); | 142 if (!Send(new ViewMsg_SocketStream_Closed(socket_id))) { |
| 185 hosts->Remove(socket_id); | 143 LOG(ERROR) << "ViewMsg_SocketStream_Closed failed."; |
| 186 if (hosts->IsEmpty()) { | |
| 187 hostmap_.Remove(host_id); | |
| 188 delete hosts; | |
| 189 } | 144 } |
| 190 } | 145 } |
| 191 | |
| 192 void SocketStreamDispatcherHost::AddHostMap( | |
| 193 int host_id, int socket_id, SocketStreamHost* socket_stream_host) { | |
| 194 IDMap<SocketStreamHost>* hosts = hostmap_.Lookup(host_id); | |
| 195 if (!hosts) { | |
| 196 hosts = new IDMap<SocketStreamHost>; | |
| 197 hostmap_.AddWithID(hosts, host_id); | |
| 198 } | |
| 199 hosts->AddWithID(socket_stream_host, socket_id); | |
| 200 } | |
| 201 | |
| 202 SocketStreamHost* SocketStreamDispatcherHost::LookupHostMap( | |
| 203 int host_id, int socket_id) { | |
| 204 IDMap<SocketStreamHost>* hosts = hostmap_.Lookup(host_id); | |
| 205 if (!hosts) | |
| 206 return NULL; | |
| 207 return hosts->Lookup(socket_id); | |
| 208 } | |
| 209 | |
| 210 /* static */ | |
| 211 bool SocketStreamDispatcherHost::IsSocketStreamDispatcherHostMessage( | |
| 212 const IPC::Message& message) { | |
| 213 switch (message.type()) { | |
| 214 case ViewHostMsg_SocketStream_Connect::ID: | |
| 215 case ViewHostMsg_SocketStream_SendData::ID: | |
| 216 case ViewHostMsg_SocketStream_Close::ID: | |
| 217 return true; | |
| 218 | |
| 219 default: | |
| 220 break; | |
| 221 } | |
| 222 return false; | |
| 223 } | |
| OLD | NEW |