Move more string_util functions to base namespace.
[chromium-blink-merge.git] / content / child / websocket_bridge.cc
blob2af6e8ece84e35f3451851b1b67aeb1e6e92be8d
1 // Copyright 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.
5 #include "content/child/websocket_bridge.h"
7 #include <stdint.h>
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "content/child/child_thread_impl.h"
15 #include "content/child/websocket_dispatcher.h"
16 #include "content/common/websocket.h"
17 #include "content/common/websocket_messages.h"
18 #include "ipc/ipc_message.h"
19 #include "ipc/ipc_message_macros.h"
20 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h"
21 #include "third_party/WebKit/public/platform/WebSocketHandle.h"
22 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h"
23 #include "third_party/WebKit/public/platform/WebSocketHandshakeRequestInfo.h"
24 #include "third_party/WebKit/public/platform/WebSocketHandshakeResponseInfo.h"
25 #include "third_party/WebKit/public/platform/WebString.h"
26 #include "third_party/WebKit/public/platform/WebURL.h"
27 #include "third_party/WebKit/public/platform/WebVector.h"
28 #include "url/gurl.h"
29 #include "url/origin.h"
31 using blink::WebSerializedOrigin;
32 using blink::WebSocketHandle;
33 using blink::WebSocketHandleClient;
34 using blink::WebString;
35 using blink::WebURL;
36 using blink::WebVector;
38 namespace content {
40 namespace {
42 const unsigned short kAbnormalShutdownOpCode = 1006;
44 } // namespace
46 WebSocketBridge::WebSocketBridge()
47 : channel_id_(kInvalidChannelId),
48 render_frame_id_(MSG_ROUTING_NONE),
49 client_(NULL) {}
51 WebSocketBridge::~WebSocketBridge() {
52 if (channel_id_ != kInvalidChannelId) {
53 // The connection is abruptly disconnected by the renderer without
54 // closing handshake.
55 ChildThreadImpl::current()->Send(
56 new WebSocketMsg_DropChannel(channel_id_,
57 false,
58 kAbnormalShutdownOpCode,
59 std::string()));
61 Disconnect();
64 bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) {
65 bool handled = true;
66 IPC_BEGIN_MESSAGE_MAP(WebSocketBridge, msg)
67 IPC_MESSAGE_HANDLER(WebSocketMsg_AddChannelResponse, DidConnect)
68 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyStartOpeningHandshake,
69 DidStartOpeningHandshake)
70 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFinishOpeningHandshake,
71 DidFinishOpeningHandshake)
72 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFailure, DidFail)
73 IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, DidReceiveData)
74 IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, DidReceiveFlowControl)
75 IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, DidClose)
76 IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyClosing,
77 DidStartClosingHandshake)
78 IPC_MESSAGE_UNHANDLED(handled = false)
79 IPC_END_MESSAGE_MAP()
80 return handled;
83 void WebSocketBridge::DidConnect(const std::string& selected_protocol,
84 const std::string& extensions) {
85 WebSocketHandleClient* client = client_;
86 DVLOG(1) << "WebSocketBridge::DidConnect("
87 << selected_protocol << ", "
88 << extensions << ")";
89 if (!client)
90 return;
92 WebString protocol_to_pass = WebString::fromUTF8(selected_protocol);
93 WebString extensions_to_pass = WebString::fromUTF8(extensions);
94 client->didConnect(this, protocol_to_pass, extensions_to_pass);
95 // |this| can be deleted here.
98 void WebSocketBridge::DidStartOpeningHandshake(
99 const WebSocketHandshakeRequest& request) {
100 DVLOG(1) << "WebSocketBridge::DidStartOpeningHandshake("
101 << request.url << ")";
102 // All strings are already encoded to ASCII in the browser.
103 blink::WebSocketHandshakeRequestInfo request_to_pass;
104 request_to_pass.setURL(WebURL(request.url));
105 for (size_t i = 0; i < request.headers.size(); ++i) {
106 const std::pair<std::string, std::string>& header = request.headers[i];
107 request_to_pass.addHeaderField(WebString::fromLatin1(header.first),
108 WebString::fromLatin1(header.second));
110 request_to_pass.setHeadersText(WebString::fromLatin1(request.headers_text));
111 client_->didStartOpeningHandshake(this, request_to_pass);
114 void WebSocketBridge::DidFinishOpeningHandshake(
115 const WebSocketHandshakeResponse& response) {
116 DVLOG(1) << "WebSocketBridge::DidFinishOpeningHandshake("
117 << response.url << ")";
118 // All strings are already encoded to ASCII in the browser.
119 blink::WebSocketHandshakeResponseInfo response_to_pass;
120 response_to_pass.setStatusCode(response.status_code);
121 response_to_pass.setStatusText(WebString::fromLatin1(response.status_text));
122 for (size_t i = 0; i < response.headers.size(); ++i) {
123 const std::pair<std::string, std::string>& header = response.headers[i];
124 response_to_pass.addHeaderField(WebString::fromLatin1(header.first),
125 WebString::fromLatin1(header.second));
127 response_to_pass.setHeadersText(WebString::fromLatin1(response.headers_text));
128 client_->didFinishOpeningHandshake(this, response_to_pass);
131 void WebSocketBridge::DidFail(const std::string& message) {
132 DVLOG(1) << "WebSocketBridge::DidFail(" << message << ")";
133 WebSocketHandleClient* client = client_;
134 Disconnect();
135 if (!client)
136 return;
138 WebString message_to_pass = WebString::fromUTF8(message);
139 client->didFail(this, message_to_pass);
140 // |this| can be deleted here.
143 void WebSocketBridge::DidReceiveData(bool fin,
144 WebSocketMessageType type,
145 const std::vector<char>& data) {
146 DVLOG(1) << "WebSocketBridge::DidReceiveData("
147 << fin << ", "
148 << type << ", "
149 << "(data size = " << data.size() << "))";
150 if (!client_)
151 return;
153 WebSocketHandle::MessageType type_to_pass =
154 WebSocketHandle::MessageTypeContinuation;
155 switch (type) {
156 case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION:
157 type_to_pass = WebSocketHandle::MessageTypeContinuation;
158 break;
159 case WEB_SOCKET_MESSAGE_TYPE_TEXT:
160 type_to_pass = WebSocketHandle::MessageTypeText;
161 break;
162 case WEB_SOCKET_MESSAGE_TYPE_BINARY:
163 type_to_pass = WebSocketHandle::MessageTypeBinary;
164 break;
166 const char* data_to_pass = data.empty() ? NULL : &data[0];
167 client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size());
168 // |this| can be deleted here.
171 void WebSocketBridge::DidReceiveFlowControl(int64_t quota) {
172 DVLOG(1) << "WebSocketBridge::DidReceiveFlowControl(" << quota << ")";
173 if (!client_)
174 return;
176 client_->didReceiveFlowControl(this, quota);
177 // |this| can be deleted here.
180 void WebSocketBridge::DidClose(bool was_clean,
181 unsigned short code,
182 const std::string& reason) {
183 DVLOG(1) << "WebSocketBridge::DidClose("
184 << was_clean << ", "
185 << code << ", "
186 << reason << ")";
187 WebSocketHandleClient* client = client_;
188 Disconnect();
189 if (!client)
190 return;
192 WebString reason_to_pass = WebString::fromUTF8(reason);
193 client->didClose(this, was_clean, code, reason_to_pass);
194 // |this| can be deleted here.
197 void WebSocketBridge::DidStartClosingHandshake() {
198 DVLOG(1) << "WebSocketBridge::DidStartClosingHandshake()";
199 if (!client_)
200 return;
202 client_->didStartClosingHandshake(this);
203 // |this| can be deleted here.
206 void WebSocketBridge::connect(
207 const WebURL& url,
208 const WebVector<WebString>& protocols,
209 const WebSerializedOrigin& origin,
210 WebSocketHandleClient* client) {
211 DCHECK_EQ(kInvalidChannelId, channel_id_);
212 WebSocketDispatcher* dispatcher =
213 ChildThreadImpl::current()->websocket_dispatcher();
214 channel_id_ = dispatcher->AddBridge(this);
215 client_ = client;
217 std::vector<std::string> protocols_to_pass;
218 for (size_t i = 0; i < protocols.size(); ++i)
219 protocols_to_pass.push_back(protocols[i].utf8());
220 url::Origin origin_to_pass(origin);
222 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", ("
223 << JoinString(protocols_to_pass, ", ") << "), "
224 << origin_to_pass.string() << ")";
226 ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest(
227 channel_id_, url, protocols_to_pass, origin_to_pass, render_frame_id_));
230 void WebSocketBridge::send(bool fin,
231 WebSocketHandle::MessageType type,
232 const char* data,
233 size_t size) {
234 if (channel_id_ == kInvalidChannelId)
235 return;
237 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION;
238 switch (type) {
239 case WebSocketHandle::MessageTypeContinuation:
240 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION;
241 break;
242 case WebSocketHandle::MessageTypeText:
243 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT;
244 break;
245 case WebSocketHandle::MessageTypeBinary:
246 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY;
247 break;
250 DVLOG(1) << "Bridge #" << channel_id_ << " Send("
251 << fin << ", " << type_to_pass << ", "
252 << "(data size = " << size << "))";
254 ChildThreadImpl::current()->Send(
255 new WebSocketMsg_SendFrame(channel_id_,
256 fin,
257 type_to_pass,
258 std::vector<char>(data, data + size)));
261 void WebSocketBridge::flowControl(int64_t quota) {
262 if (channel_id_ == kInvalidChannelId)
263 return;
265 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")";
267 ChildThreadImpl::current()->Send(
268 new WebSocketMsg_FlowControl(channel_id_, quota));
271 void WebSocketBridge::close(unsigned short code,
272 const WebString& reason) {
273 if (channel_id_ == kInvalidChannelId)
274 return;
276 std::string reason_to_pass = reason.utf8();
277 DVLOG(1) << "Bridge #" << channel_id_ << " Close("
278 << code << ", " << reason_to_pass << ")";
279 // This method is for closing handshake and hence |was_clean| shall be true.
280 ChildThreadImpl::current()->Send(
281 new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass));
284 void WebSocketBridge::Disconnect() {
285 if (channel_id_ == kInvalidChannelId)
286 return;
287 WebSocketDispatcher* dispatcher =
288 ChildThreadImpl::current()->websocket_dispatcher();
289 dispatcher->RemoveBridge(channel_id_);
291 channel_id_ = kInvalidChannelId;
292 client_ = NULL;
295 } // namespace content