OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/browser/renderer_host/socket_stream_dispatcher_host.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/renderer_host/socket_stream_host.h" |
| 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/net/socket_stream.h" |
| 11 #include "ipc/ipc_message.h" |
| 12 |
| 13 SocketStreamDispatcherHost::SocketStreamDispatcherHost() |
| 14 : sender_(NULL) { |
| 15 } |
| 16 |
| 17 SocketStreamDispatcherHost::~SocketStreamDispatcherHost() { |
| 18 // TODO(ukai): Implement IDMap::RemoveAll(). |
| 19 for (IDMap<SocketStreamHost>::const_iterator iter(&hosts_); |
| 20 !iter.IsAtEnd(); |
| 21 iter.Advance()) { |
| 22 int socket_id = iter.GetCurrentKey(); |
| 23 const SocketStreamHost* socket_stream_host = iter.GetCurrentValue(); |
| 24 delete socket_stream_host; |
| 25 hosts_.Remove(socket_id); |
| 26 } |
| 27 } |
| 28 |
| 29 void SocketStreamDispatcherHost::Initialize( |
| 30 IPC::Message::Sender* sender, int process_id) { |
| 31 DLOG(INFO) << "Initialize: SocketStreamDispatcherHost process_id=" |
| 32 << process_id_; |
| 33 DCHECK(sender); |
| 34 sender_ = sender; |
| 35 process_id_ = process_id; |
| 36 } |
| 37 |
| 38 bool SocketStreamDispatcherHost::OnMessageReceived(const IPC::Message& msg, |
| 39 bool* msg_ok) { |
| 40 DCHECK(sender_); |
| 41 *msg_ok = true; |
| 42 bool handled = true; |
| 43 IPC_BEGIN_MESSAGE_MAP_EX(SocketStreamDispatcherHost, msg, *msg_ok) |
| 44 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Connect, OnConnect) |
| 45 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_SendData, OnSendData) |
| 46 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Close, OnCloseReq) |
| 47 IPC_MESSAGE_UNHANDLED(handled = false) |
| 48 IPC_END_MESSAGE_MAP_EX() |
| 49 return handled; |
| 50 } |
| 51 |
| 52 // SocketStream::Delegate methods implementations. |
| 53 void SocketStreamDispatcherHost::OnConnected(net::SocketStream* socket, |
| 54 int max_pending_send_allowed) { |
| 55 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 56 DLOG(INFO) << "SocketStreamDispatcherHost::OnConnected socket_id=" |
| 57 << socket_id |
| 58 << " max_pending_send_allowed=" << max_pending_send_allowed; |
| 59 if (socket_id == chrome_common_net::kNoSocketId) { |
| 60 LOG(ERROR) << "NoSocketId in OnConnected"; |
| 61 return; |
| 62 } |
| 63 if (!sender_->Send(new ViewMsg_SocketStream_Connected( |
| 64 socket_id, max_pending_send_allowed))) { |
| 65 LOG(ERROR) << "ViewMsg_SocketStream_Connected failed."; |
| 66 DeleteSocketStreamHost(socket_id); |
| 67 } |
| 68 } |
| 69 |
| 70 void SocketStreamDispatcherHost::OnSentData(net::SocketStream* socket, |
| 71 int amount_sent) { |
| 72 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 73 DLOG(INFO) << "SocketStreamDispatcherHost::OnSentData socket_id=" |
| 74 << socket_id |
| 75 << " amount_sent=" << amount_sent; |
| 76 if (socket_id == chrome_common_net::kNoSocketId) { |
| 77 LOG(ERROR) << "NoSocketId in OnReceivedData"; |
| 78 return; |
| 79 } |
| 80 if (!sender_->Send( |
| 81 new ViewMsg_SocketStream_SentData(socket_id, amount_sent))) { |
| 82 LOG(ERROR) << "ViewMsg_SocketStream_SentData failed."; |
| 83 DeleteSocketStreamHost(socket_id); |
| 84 } |
| 85 } |
| 86 |
| 87 void SocketStreamDispatcherHost::OnReceivedData( |
| 88 net::SocketStream* socket, const char* data, int len) { |
| 89 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 90 DLOG(INFO) << "SocketStreamDispatcherHost::OnReceiveData socket_id=" |
| 91 << socket_id; |
| 92 if (socket_id == chrome_common_net::kNoSocketId) { |
| 93 LOG(ERROR) << "NoSocketId in OnReceivedData"; |
| 94 return; |
| 95 } |
| 96 if (!sender_->Send(new ViewMsg_SocketStream_ReceivedData( |
| 97 socket_id, std::vector<char>(data, data + len)))) { |
| 98 LOG(ERROR) << "ViewMsg_SocketStream_ReceivedData failed."; |
| 99 DeleteSocketStreamHost(socket_id); |
| 100 } |
| 101 } |
| 102 |
| 103 void SocketStreamDispatcherHost::OnClose(net::SocketStream* socket) { |
| 104 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 105 DLOG(INFO) << "SocketStreamDispatcherHost::OnClosed socket_id=" |
| 106 << socket_id; |
| 107 if (socket_id == chrome_common_net::kNoSocketId) { |
| 108 LOG(ERROR) << "NoSocketId in OnClose"; |
| 109 return; |
| 110 } |
| 111 DeleteSocketStreamHost(socket_id); |
| 112 } |
| 113 |
| 114 // Message handlers called by OnMessageReceived. |
| 115 void SocketStreamDispatcherHost::OnConnect(const GURL& url, int socket_id) { |
| 116 DLOG(INFO) << "SocketStreamDispatcherHost::OnConnect url=" << url |
| 117 << " socket_id=" << socket_id; |
| 118 DCHECK_NE(chrome_common_net::kNoSocketId, socket_id); |
| 119 if (hosts_.Lookup(socket_id)) { |
| 120 LOG(ERROR) << "socket_id=" << socket_id << " already registered."; |
| 121 return; |
| 122 } |
| 123 SocketStreamHost* socket_stream_host = new SocketStreamHost(this, socket_id); |
| 124 hosts_.AddWithID(socket_stream_host, socket_id); |
| 125 socket_stream_host->Connect(url); |
| 126 DLOG(INFO) << "SocketStreamDispatcherHost::OnConnect -> " << socket_id; |
| 127 } |
| 128 |
| 129 void SocketStreamDispatcherHost::OnSendData( |
| 130 int socket_id, const std::vector<char>& data) { |
| 131 DLOG(INFO) << "SocketStreamDispatcherHost::OnSendData socket_id=" |
| 132 << socket_id; |
| 133 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 134 if (!socket_stream_host) { |
| 135 LOG(ERROR) << "socket_id=" << socket_id << " already closed."; |
| 136 return; |
| 137 } |
| 138 if (!socket_stream_host->SendData(data)) { |
| 139 // Cannot accept more data to send. |
| 140 socket_stream_host->Close(); |
| 141 } |
| 142 } |
| 143 |
| 144 void SocketStreamDispatcherHost::OnCloseReq(int socket_id) { |
| 145 DLOG(INFO) << "SocketStreamDispatcherHost::OnCloseReq socket_id=" |
| 146 << socket_id; |
| 147 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 148 if (!socket_stream_host) |
| 149 return; |
| 150 socket_stream_host->Close(); |
| 151 } |
| 152 |
| 153 void SocketStreamDispatcherHost::DeleteSocketStreamHost(int socket_id) { |
| 154 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 155 DCHECK(socket_stream_host); |
| 156 delete socket_stream_host; |
| 157 hosts_.Remove(socket_id); |
| 158 if (!sender_->Send(new ViewMsg_SocketStream_Closed(socket_id))) { |
| 159 LOG(ERROR) << "ViewMsg_SocketStream_Closed failed."; |
| 160 } |
| 161 } |
OLD | NEW |