1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/renderer/socket_stream_dispatcher.h"
#include <vector>
#include "base/id_map.h"
#include "base/ref_counted.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/net/socket_stream.h"
#include "chrome/renderer/render_thread.h"
#include "googleurl/src/gurl.h"
#include "webkit/glue/websocketstreamhandle_bridge.h"
#include "webkit/glue/websocketstreamhandle_delegate.h"
// IPCWebSocketStreamHandleBridge is owned by each SocketStreamHandle.
// It communicates with the main browser process via SocketStreamDispatcher.
class IPCWebSocketStreamHandleBridge
: public webkit_glue::WebSocketStreamHandleBridge {
public:
IPCWebSocketStreamHandleBridge(
IPC::Message::Sender* sender,
WebKit::WebSocketStreamHandle* handle,
webkit_glue::WebSocketStreamHandleDelegate* delegate)
: socket_id_(chrome_common_net::kNoSocketId),
sender_(sender),
handle_(handle),
delegate_(delegate) {}
// Returns the handle having given id or NULL if there is no such handle.
static IPCWebSocketStreamHandleBridge* FromSocketId(int id);
// webkit_glue::WebSocketStreamHandleBridge methods.
virtual void Connect(const GURL& url);
virtual bool Send(const std::vector<char>& data);
virtual void Close();
// Called by SocketStreamDispatcher.
void OnConnected(int max_amount_send_allowed);
void OnSentData(int amount_sent);
void OnReceivedData(const std::vector<char>& data);
void OnClosed();
private:
virtual ~IPCWebSocketStreamHandleBridge();
void DoConnect(const GURL& url);
int socket_id_;
IPC::Message::Sender* sender_;
WebKit::WebSocketStreamHandle* handle_;
webkit_glue::WebSocketStreamHandleDelegate* delegate_;
static IDMap<IPCWebSocketStreamHandleBridge> all_bridges;
};
IDMap<IPCWebSocketStreamHandleBridge>
IPCWebSocketStreamHandleBridge::all_bridges;
/* static */
IPCWebSocketStreamHandleBridge* IPCWebSocketStreamHandleBridge::FromSocketId(
int id) {
return all_bridges.Lookup(id);
}
IPCWebSocketStreamHandleBridge::~IPCWebSocketStreamHandleBridge() {
DLOG(INFO) << "IPCWebSocketStreamHandleBridge destructor socket_id="
<< socket_id_;
if (socket_id_ != chrome_common_net::kNoSocketId) {
sender_->Send(new ViewHostMsg_Close(socket_id_));
socket_id_ = chrome_common_net::kNoSocketId;
}
}
void IPCWebSocketStreamHandleBridge::Connect(const GURL& url) {
DCHECK(sender_);
DLOG(INFO) << "Connect url=" << url;
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &IPCWebSocketStreamHandleBridge::DoConnect,
url));
}
bool IPCWebSocketStreamHandleBridge::Send(
const std::vector<char>& data) {
DLOG(INFO) << "Send data.size=" << data.size();
if (sender_->Send(new ViewHostMsg_SocketStream_SendData(socket_id_, data))) {
if (delegate_)
delegate_->WillSendData(handle_, &data[0], data.size());
return true;
}
return false;
}
void IPCWebSocketStreamHandleBridge::Close() {
DLOG(INFO) << "Close socket_id" << socket_id_;
sender_->Send(new ViewHostMsg_SocketStream_Close(socket_id_));
}
void IPCWebSocketStreamHandleBridge::OnConnected(int max_pending_send_allowed) {
DLOG(INFO) << "IPCWebSocketStreamHandleBridge::OnConnected socket_id="
<< socket_id_;
if (delegate_)
delegate_->DidOpenStream(handle_, max_pending_send_allowed);
}
void IPCWebSocketStreamHandleBridge::OnSentData(int amount_sent) {
if (delegate_)
delegate_->DidSendData(handle_, amount_sent);
}
void IPCWebSocketStreamHandleBridge::OnReceivedData(
const std::vector<char>& data) {
if (delegate_)
delegate_->DidReceiveData(handle_, &data[0], data.size());
}
void IPCWebSocketStreamHandleBridge::OnClosed() {
DLOG(INFO) << "IPCWebSocketStreamHandleBridge::OnClosed";
if (socket_id_ != chrome_common_net::kNoSocketId) {
all_bridges.Remove(socket_id_);
socket_id_ = chrome_common_net::kNoSocketId;
}
if (delegate_) {
delegate_->DidClose(handle_);
}
delegate_ = NULL;
Release();
}
void IPCWebSocketStreamHandleBridge::DoConnect(const GURL& url) {
DCHECK(sender_);
DCHECK_EQ(socket_id_, chrome_common_net::kNoSocketId);
if (delegate_)
delegate_->WillOpenStream(handle_, url);
socket_id_ = all_bridges.Add(this);
DCHECK_NE(socket_id_, chrome_common_net::kNoSocketId);
if (sender_->Send(new ViewHostMsg_SocketStream_Connect(url, socket_id_))) {
DLOG(INFO) << "Connect socket_id=" << socket_id_;
AddRef(); // Released in OnClosed().
// TODO(ukai): timeout to OnConnected.
} else {
LOG(ERROR) << "IPC SocketStream_Connect failed.";
OnClosed();
}
}
SocketStreamDispatcher::SocketStreamDispatcher() {
}
/* static */
webkit_glue::WebSocketStreamHandleBridge*
SocketStreamDispatcher::CreateBridge(
WebKit::WebSocketStreamHandle* handle,
webkit_glue::WebSocketStreamHandleDelegate* delegate) {
return new IPCWebSocketStreamHandleBridge(
ChildThread::current(), handle, delegate);
}
bool SocketStreamDispatcher::OnMessageReceived(const IPC::Message& msg) {
DLOG(INFO) << "SocketStreamDispatcher::OnMessageReceived";
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(SocketStreamDispatcher, msg)
IPC_MESSAGE_HANDLER(ViewMsg_SocketStream_Connected, OnConnected)
IPC_MESSAGE_HANDLER(ViewMsg_SocketStream_SentData, OnSentData)
IPC_MESSAGE_HANDLER(ViewMsg_SocketStream_ReceivedData, OnReceivedData)
IPC_MESSAGE_HANDLER(ViewMsg_SocketStream_Closed, OnClosed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void SocketStreamDispatcher::OnConnected(int socket_id,
int max_pending_send_allowed) {
DLOG(INFO) << "SocketStreamDispatcher::OnConnected socket_id=" << socket_id
<< " max_pending_send_allowed=" << max_pending_send_allowed;
IPCWebSocketStreamHandleBridge* bridge =
IPCWebSocketStreamHandleBridge::FromSocketId(socket_id);
if (bridge)
bridge->OnConnected(max_pending_send_allowed);
else
DLOG(ERROR) << "No SocketStreamHandleBridge for socket_id=" << socket_id;
}
void SocketStreamDispatcher::OnSentData(int socket_id, int amount_sent) {
IPCWebSocketStreamHandleBridge* bridge =
IPCWebSocketStreamHandleBridge::FromSocketId(socket_id);
if (bridge)
bridge->OnSentData(amount_sent);
else
DLOG(ERROR) << "No SocketStreamHandleBridge for socket_id=" << socket_id;
}
void SocketStreamDispatcher::OnReceivedData(
int socket_id, const std::vector<char>& data) {
IPCWebSocketStreamHandleBridge* bridge =
IPCWebSocketStreamHandleBridge::FromSocketId(socket_id);
if (bridge)
bridge->OnReceivedData(data);
else
DLOG(ERROR) << "No SocketStreamHandleBridge for socket_id=" << socket_id;
}
void SocketStreamDispatcher::OnClosed(int socket_id) {
IPCWebSocketStreamHandleBridge* bridge =
IPCWebSocketStreamHandleBridge::FromSocketId(socket_id);
if (bridge)
bridge->OnClosed();
else
DLOG(ERROR) << "No SocketStreamHandleBridge for socket_id=" << socket_id;
}
|