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