Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: content/child/websocket_bridge.cc

Issue 22815034: Introduce webkit_glue bridges for the new WebSocket Implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "content/child/websocket_bridge.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "content/child/child_thread.h"
13 #include "content/child/websocket_dispatcher.h"
14 #include "content/common/websocket.h"
15 #include "content/common/websocket_messages.h"
16 #include "ipc/ipc_message.h"
17 #include "ipc/ipc_message_macros.h"
18 #include "url/gurl.h"
19 #include "third_party/WebKit/public/platform/WebSocketHandle.h"
20 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h"
21 #include "third_party/WebKit/public/platform/WebString.h"
22 #include "third_party/WebKit/public/platform/WebURL.h"
23 #include "third_party/WebKit/public/platform/WebVector.h"
24
25 using WebKit::WebSocketHandle;
26 using WebKit::WebSocketHandleClient;
27 using WebKit::WebString;
28 using WebKit::WebURL;
29 using WebKit::WebVector;
30
31 namespace content {
32
33 WebSocketBridge::WebSocketBridge()
34 : channel_id_(kInvalidChannelId), client_(NULL) {}
35
36 WebSocketBridge::~WebSocketBridge() {
37 Disconnect();
38 }
39
40 bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) {
41 bool handled = true;
42 IPC_BEGIN_MESSAGE_MAP(WebSocketBridge, msg)
43 IPC_MESSAGE_HANDLER(WebSocketMsg_AddChannelResponse, DidConnect)
44 IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, DidReceiveData)
45 IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, DidReceiveFlowControl)
46 IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, DidClose)
47 IPC_MESSAGE_UNHANDLED(handled = false)
48 IPC_END_MESSAGE_MAP()
49 return handled;
50 }
51
52 void WebSocketBridge::DidConnect(bool fail,
53 const std::string& selected_protocol,
54 const std::string& extensions) {
55 WebSocketHandleClient* client = client_;
56 DVLOG(1) << "WebSocketBridge::DidConnect("
57 << fail << ", "
58 << selected_protocol << ", "
59 << extensions << ")";
60 if (fail)
61 Disconnect();
62 if (!client)
63 return;
64
65 WebString protocol_to_pass = WebString::fromUTF8(selected_protocol);
66 WebString extensions_to_pass = WebString::fromUTF8(extensions);
67 client->didConnect(this, fail, protocol_to_pass, extensions_to_pass);
68 // |this| can be deleted here.
69 }
70
71 void WebSocketBridge::DidReceiveData(bool fin,
72 WebSocketMessageType type,
73 const std::vector<char>& data) {
74 DVLOG(1) << "WebSocketBridge::DidReceiveData("
75 << fin << ", "
76 << type << ", "
77 << "(data size = " << data.size() << "))";
78 if (!client_)
79 return;
80
81 WebSocketHandle::MessageType type_to_pass =
82 WebSocketHandle::MessageTypeContinuation;
83 switch (type) {
84 case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION:
85 type_to_pass = WebSocketHandle::MessageTypeContinuation;
86 break;
87 case WEB_SOCKET_MESSAGE_TYPE_TEXT:
88 type_to_pass = WebSocketHandle::MessageTypeText;
89 break;
90 case WEB_SOCKET_MESSAGE_TYPE_BINARY:
91 type_to_pass = WebSocketHandle::MessageTypeBinary;
92 break;
93 }
94 const char* data_to_pass = data.empty() ? NULL : &data[0];
95 client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size());
96 // |this| can be deleted here.
97 }
98
99 void WebSocketBridge::DidReceiveFlowControl(int64_t quota) {
100 DVLOG(1) << "WebSocketBridge::DidReceiveFlowControl(" << quota << ")";
101 if (!client_)
102 return;
103
104 client_->didReceiveFlowControl(this, quota);
105 // |this| can be deleted here.
106 }
107
108 void WebSocketBridge::DidClose(unsigned short code,
109 const std::string& reason) {
110 DVLOG(1) << "WebSocketBridge::DidClose("
111 << code << ", "
112 << reason << ")";
113 WebSocketHandleClient* client = client_;
114 Disconnect();
115 if (!client)
116 return;
117
118 WebString reason_to_pass = WebString::fromUTF8(reason);
119 client->didClose(this, code, reason_to_pass);
120 // |this| can be deleted here.
121 }
122
123 void WebSocketBridge::connect(
124 const WebURL& url,
125 const WebVector<WebString>& protocols,
126 const WebString& origin,
127 WebSocketHandleClient* client) {
128 DCHECK_EQ(kInvalidChannelId, channel_id_);
129 WebSocketDispatcher* dispatcher =
130 ChildThread::current()->websocket_dispatcher();
131 channel_id_ = dispatcher->AddBridge(this);
132 client_ = client;
133
134 std::vector<std::string> protocols_to_pass;
135 for (size_t i = 0; i < protocols.size(); ++i)
136 protocols_to_pass.push_back(protocols[i].utf8());
137 GURL origin_to_pass(origin.utf8());
138
139 DVLOG(1) << "Bridge#" << channel_id_ << " Connect("
140 << url << ", (" << JoinString(protocols_to_pass, ", ") << "), "
141 << origin_to_pass << ")";
142
143 ChildThread::current()->Send(
144 new WebSocketHostMsg_AddChannelRequest(channel_id_,
145 url,
146 protocols_to_pass,
147 origin_to_pass));
148 }
149
150 void WebSocketBridge::send(bool fin,
151 WebSocketHandle::MessageType type,
152 const char* data,
153 size_t size) {
154 if (channel_id_ == kInvalidChannelId)
155 return;
156
157 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION;
158 switch (type) {
159 case WebSocketHandle::MessageTypeContinuation:
160 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION;
161 break;
162 case WebSocketHandle::MessageTypeText:
163 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT;
164 break;
165 case WebSocketHandle::MessageTypeBinary:
166 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY;
167 break;
168 }
169
170 DVLOG(1) << "Bridge #" << channel_id_ << " Send("
171 << fin << ", " << type_to_pass << ", "
172 << "(data size = " << size << "))";
173
174 ChildThread::current()->Send(
175 new WebSocketMsg_SendFrame(channel_id_,
176 fin,
177 type_to_pass,
178 std::vector<char>(data, data + size)));
179 }
180
181 void WebSocketBridge::flowControl(int64_t quota) {
182 if (channel_id_ == kInvalidChannelId)
183 return;
184
185 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")";
186
187 ChildThread::current()->Send(
188 new WebSocketMsg_FlowControl(channel_id_, quota));
189 }
190
191 void WebSocketBridge::close(unsigned short code,
192 const WebString& reason) {
193 if (channel_id_ == kInvalidChannelId)
194 return;
195
196 std::string reason_to_pass = reason.utf8();
197 DVLOG(1) << "Bridge #" << channel_id_ << " Close("
198 << code << ", " << reason_to_pass << ")";
199 ChildThread::current()->Send(
200 new WebSocketMsg_DropChannel(channel_id_, code, reason_to_pass));
201 }
202
203 void WebSocketBridge::Disconnect() {
204 if (channel_id_ == kInvalidChannelId)
205 return;
206 WebSocketDispatcher* dispatcher =
207 ChildThread::current()->websocket_dispatcher();
208 dispatcher->RemoveBridge(channel_id_);
209
210 channel_id_ = kInvalidChannelId;
211 client_ = NULL;
212 }
213
214 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698