| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/renderer_host/socket_stream_dispatcher_host.h" | 5 #include "content/browser/renderer_host/socket_stream_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/net/url_request_context_getter.h" |
| 10 #include "chrome/common/render_messages_params.h" | 10 #include "chrome/common/render_messages_params.h" |
| 11 #include "chrome/common/net/socket_stream.h" | |
| 12 #include "chrome/common/net/url_request_context_getter.h" | |
| 13 #include "content/browser/renderer_host/socket_stream_host.h" | 11 #include "content/browser/renderer_host/socket_stream_host.h" |
| 12 #include "content/common/socket_stream.h" |
| 13 #include "content/common/socket_stream_messages.h" |
| 14 #include "net/websockets/websocket_job.h" | 14 #include "net/websockets/websocket_job.h" |
| 15 #include "net/websockets/websocket_throttle.h" | 15 #include "net/websockets/websocket_throttle.h" |
| 16 | 16 |
| 17 SocketStreamDispatcherHost::SocketStreamDispatcherHost() { | 17 SocketStreamDispatcherHost::SocketStreamDispatcherHost() { |
| 18 net::WebSocketJob::EnsureInit(); | 18 net::WebSocketJob::EnsureInit(); |
| 19 } | 19 } |
| 20 | 20 |
| 21 SocketStreamDispatcherHost::~SocketStreamDispatcherHost() { | 21 SocketStreamDispatcherHost::~SocketStreamDispatcherHost() { |
| 22 // TODO(ukai): Implement IDMap::RemoveAll(). | 22 // TODO(ukai): Implement IDMap::RemoveAll(). |
| 23 for (IDMap<SocketStreamHost>::const_iterator iter(&hosts_); | 23 for (IDMap<SocketStreamHost>::const_iterator iter(&hosts_); |
| 24 !iter.IsAtEnd(); | 24 !iter.IsAtEnd(); |
| 25 iter.Advance()) { | 25 iter.Advance()) { |
| 26 int socket_id = iter.GetCurrentKey(); | 26 int socket_id = iter.GetCurrentKey(); |
| 27 const SocketStreamHost* socket_stream_host = iter.GetCurrentValue(); | 27 const SocketStreamHost* socket_stream_host = iter.GetCurrentValue(); |
| 28 delete socket_stream_host; | 28 delete socket_stream_host; |
| 29 hosts_.Remove(socket_id); | 29 hosts_.Remove(socket_id); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 bool SocketStreamDispatcherHost::OnMessageReceived(const IPC::Message& message, | 33 bool SocketStreamDispatcherHost::OnMessageReceived(const IPC::Message& message, |
| 34 bool* message_was_ok) { | 34 bool* message_was_ok) { |
| 35 bool handled = true; | 35 bool handled = true; |
| 36 IPC_BEGIN_MESSAGE_MAP_EX(SocketStreamDispatcherHost, message, *message_was_ok) | 36 IPC_BEGIN_MESSAGE_MAP_EX(SocketStreamDispatcherHost, message, *message_was_ok) |
| 37 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Connect, OnConnect) | 37 IPC_MESSAGE_HANDLER(SocketStreamHostMsg_Connect, OnConnect) |
| 38 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_SendData, OnSendData) | 38 IPC_MESSAGE_HANDLER(SocketStreamHostMsg_SendData, OnSendData) |
| 39 IPC_MESSAGE_HANDLER(ViewHostMsg_SocketStream_Close, OnCloseReq) | 39 IPC_MESSAGE_HANDLER(SocketStreamHostMsg_Close, OnCloseReq) |
| 40 IPC_MESSAGE_UNHANDLED(handled = false) | 40 IPC_MESSAGE_UNHANDLED(handled = false) |
| 41 IPC_END_MESSAGE_MAP_EX() | 41 IPC_END_MESSAGE_MAP_EX() |
| 42 return handled; | 42 return handled; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // SocketStream::Delegate methods implementations. | 45 // SocketStream::Delegate methods implementations. |
| 46 void SocketStreamDispatcherHost::OnConnected(net::SocketStream* socket, | 46 void SocketStreamDispatcherHost::OnConnected(net::SocketStream* socket, |
| 47 int max_pending_send_allowed) { | 47 int max_pending_send_allowed) { |
| 48 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); | 48 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 49 DVLOG(1) << "SocketStreamDispatcherHost::OnConnected socket_id=" << socket_id | 49 DVLOG(1) << "SocketStreamDispatcherHost::OnConnected socket_id=" << socket_id |
| 50 << " max_pending_send_allowed=" << max_pending_send_allowed; | 50 << " max_pending_send_allowed=" << max_pending_send_allowed; |
| 51 if (socket_id == chrome_common_net::kNoSocketId) { | 51 if (socket_id == content_common::kNoSocketId) { |
| 52 LOG(ERROR) << "NoSocketId in OnConnected"; | 52 LOG(ERROR) << "NoSocketId in OnConnected"; |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 if (!Send(new ViewMsg_SocketStream_Connected( | 55 if (!Send(new SocketStreamMsg_Connected( |
| 56 socket_id, max_pending_send_allowed))) { | 56 socket_id, max_pending_send_allowed))) { |
| 57 LOG(ERROR) << "ViewMsg_SocketStream_Connected failed."; | 57 LOG(ERROR) << "SocketStreamMsg_Connected failed."; |
| 58 DeleteSocketStreamHost(socket_id); | 58 DeleteSocketStreamHost(socket_id); |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 void SocketStreamDispatcherHost::OnSentData(net::SocketStream* socket, | 62 void SocketStreamDispatcherHost::OnSentData(net::SocketStream* socket, |
| 63 int amount_sent) { | 63 int amount_sent) { |
| 64 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); | 64 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 65 DVLOG(1) << "SocketStreamDispatcherHost::OnSentData socket_id=" << socket_id | 65 DVLOG(1) << "SocketStreamDispatcherHost::OnSentData socket_id=" << socket_id |
| 66 << " amount_sent=" << amount_sent; | 66 << " amount_sent=" << amount_sent; |
| 67 if (socket_id == chrome_common_net::kNoSocketId) { | 67 if (socket_id == content_common::kNoSocketId) { |
| 68 LOG(ERROR) << "NoSocketId in OnReceivedData"; | 68 LOG(ERROR) << "NoSocketId in OnReceivedData"; |
| 69 return; | 69 return; |
| 70 } | 70 } |
| 71 if (!Send(new ViewMsg_SocketStream_SentData(socket_id, amount_sent))) { | 71 if (!Send(new SocketStreamMsg_SentData(socket_id, amount_sent))) { |
| 72 LOG(ERROR) << "ViewMsg_SocketStream_SentData failed."; | 72 LOG(ERROR) << "SocketStreamMsg_SentData failed."; |
| 73 DeleteSocketStreamHost(socket_id); | 73 DeleteSocketStreamHost(socket_id); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 void SocketStreamDispatcherHost::OnReceivedData( | 77 void SocketStreamDispatcherHost::OnReceivedData( |
| 78 net::SocketStream* socket, const char* data, int len) { | 78 net::SocketStream* socket, const char* data, int len) { |
| 79 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); | 79 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 80 DVLOG(1) << "SocketStreamDispatcherHost::OnReceiveData socket_id=" | 80 DVLOG(1) << "SocketStreamDispatcherHost::OnReceiveData socket_id=" |
| 81 << socket_id; | 81 << socket_id; |
| 82 if (socket_id == chrome_common_net::kNoSocketId) { | 82 if (socket_id == content_common::kNoSocketId) { |
| 83 LOG(ERROR) << "NoSocketId in OnReceivedData"; | 83 LOG(ERROR) << "NoSocketId in OnReceivedData"; |
| 84 return; | 84 return; |
| 85 } | 85 } |
| 86 if (!Send(new ViewMsg_SocketStream_ReceivedData( | 86 if (!Send(new SocketStreamMsg_ReceivedData( |
| 87 socket_id, std::vector<char>(data, data + len)))) { | 87 socket_id, std::vector<char>(data, data + len)))) { |
| 88 LOG(ERROR) << "ViewMsg_SocketStream_ReceivedData failed."; | 88 LOG(ERROR) << "SocketStreamMsg_ReceivedData failed."; |
| 89 DeleteSocketStreamHost(socket_id); | 89 DeleteSocketStreamHost(socket_id); |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 void SocketStreamDispatcherHost::OnClose(net::SocketStream* socket) { | 93 void SocketStreamDispatcherHost::OnClose(net::SocketStream* socket) { |
| 94 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); | 94 int socket_id = SocketStreamHost::SocketIdFromSocketStream(socket); |
| 95 DVLOG(1) << "SocketStreamDispatcherHost::OnClosed socket_id=" << socket_id; | 95 DVLOG(1) << "SocketStreamDispatcherHost::OnClosed socket_id=" << socket_id; |
| 96 if (socket_id == chrome_common_net::kNoSocketId) { | 96 if (socket_id == content_common::kNoSocketId) { |
| 97 LOG(ERROR) << "NoSocketId in OnClose"; | 97 LOG(ERROR) << "NoSocketId in OnClose"; |
| 98 return; | 98 return; |
| 99 } | 99 } |
| 100 DeleteSocketStreamHost(socket_id); | 100 DeleteSocketStreamHost(socket_id); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Message handlers called by OnMessageReceived. | 103 // Message handlers called by OnMessageReceived. |
| 104 void SocketStreamDispatcherHost::OnConnect(const GURL& url, int socket_id) { | 104 void SocketStreamDispatcherHost::OnConnect(const GURL& url, int socket_id) { |
| 105 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect url=" << url | 105 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect url=" << url |
| 106 << " socket_id=" << socket_id; | 106 << " socket_id=" << socket_id; |
| 107 DCHECK_NE(chrome_common_net::kNoSocketId, socket_id); | 107 DCHECK_NE(content_common::kNoSocketId, socket_id); |
| 108 if (hosts_.Lookup(socket_id)) { | 108 if (hosts_.Lookup(socket_id)) { |
| 109 LOG(ERROR) << "socket_id=" << socket_id << " already registered."; | 109 LOG(ERROR) << "socket_id=" << socket_id << " already registered."; |
| 110 return; | 110 return; |
| 111 } | 111 } |
| 112 SocketStreamHost* socket_stream_host = new SocketStreamHost(this, socket_id); | 112 SocketStreamHost* socket_stream_host = new SocketStreamHost(this, socket_id); |
| 113 hosts_.AddWithID(socket_stream_host, socket_id); | 113 hosts_.AddWithID(socket_stream_host, socket_id); |
| 114 socket_stream_host->Connect(url, GetURLRequestContext()); | 114 socket_stream_host->Connect(url, GetURLRequestContext()); |
| 115 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect -> " << socket_id; | 115 DVLOG(1) << "SocketStreamDispatcherHost::OnConnect -> " << socket_id; |
| 116 } | 116 } |
| 117 | 117 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 135 if (!socket_stream_host) | 135 if (!socket_stream_host) |
| 136 return; | 136 return; |
| 137 socket_stream_host->Close(); | 137 socket_stream_host->Close(); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void SocketStreamDispatcherHost::DeleteSocketStreamHost(int socket_id) { | 140 void SocketStreamDispatcherHost::DeleteSocketStreamHost(int socket_id) { |
| 141 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); | 141 SocketStreamHost* socket_stream_host = hosts_.Lookup(socket_id); |
| 142 DCHECK(socket_stream_host); | 142 DCHECK(socket_stream_host); |
| 143 delete socket_stream_host; | 143 delete socket_stream_host; |
| 144 hosts_.Remove(socket_id); | 144 hosts_.Remove(socket_id); |
| 145 if (!Send(new ViewMsg_SocketStream_Closed(socket_id))) { | 145 if (!Send(new SocketStreamMsg_Closed(socket_id))) { |
| 146 LOG(ERROR) << "ViewMsg_SocketStream_Closed failed."; | 146 LOG(ERROR) << "SocketStreamMsg_Closed failed."; |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 net::URLRequestContext* SocketStreamDispatcherHost::GetURLRequestContext() { | 150 net::URLRequestContext* SocketStreamDispatcherHost::GetURLRequestContext() { |
| 151 net::URLRequestContext* rv = NULL; | 151 net::URLRequestContext* rv = NULL; |
| 152 if (url_request_context_override_.get()) { | 152 if (url_request_context_override_.get()) { |
| 153 // TODO(jam): temporary code until Gears is taken out, then |
| 154 // GetRequestContext will take a different parameter and we can take out |
| 155 // this struct and the #include "chrome/common/render_messages_params.h" |
| 156 // above. |
| 153 ViewHostMsg_Resource_Request request; | 157 ViewHostMsg_Resource_Request request; |
| 154 rv = url_request_context_override_->GetRequestContext(request); | 158 rv = url_request_context_override_->GetRequestContext(request); |
| 155 } | 159 } |
| 156 if (!rv) { | 160 if (!rv) { |
| 157 URLRequestContextGetter* context_getter = | 161 URLRequestContextGetter* context_getter = |
| 158 Profile::GetDefaultRequestContext(); | 162 Profile::GetDefaultRequestContext(); |
| 159 if (context_getter) | 163 if (context_getter) |
| 160 rv = context_getter->GetURLRequestContext(); | 164 rv = context_getter->GetURLRequestContext(); |
| 161 } | 165 } |
| 162 | 166 |
| 163 return rv; | 167 return rv; |
| 164 } | 168 } |
| OLD | NEW |