| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/child/websocket_bridge.h" | 5 #include "content/child/websocket_bridge.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "content/child/child_thread.h" | 14 #include "content/child/child_thread_impl.h" |
| 15 #include "content/child/websocket_dispatcher.h" | 15 #include "content/child/websocket_dispatcher.h" |
| 16 #include "content/common/websocket.h" | 16 #include "content/common/websocket.h" |
| 17 #include "content/common/websocket_messages.h" | 17 #include "content/common/websocket_messages.h" |
| 18 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 19 #include "ipc/ipc_message_macros.h" | 19 #include "ipc/ipc_message_macros.h" |
| 20 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 20 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" |
| 21 #include "third_party/WebKit/public/platform/WebSocketHandle.h" | 21 #include "third_party/WebKit/public/platform/WebSocketHandle.h" |
| 22 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" | 22 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
| 23 #include "third_party/WebKit/public/platform/WebSocketHandshakeRequestInfo.h" | 23 #include "third_party/WebKit/public/platform/WebSocketHandshakeRequestInfo.h" |
| 24 #include "third_party/WebKit/public/platform/WebSocketHandshakeResponseInfo.h" | 24 #include "third_party/WebKit/public/platform/WebSocketHandshakeResponseInfo.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 | 45 |
| 46 WebSocketBridge::WebSocketBridge() | 46 WebSocketBridge::WebSocketBridge() |
| 47 : channel_id_(kInvalidChannelId), | 47 : channel_id_(kInvalidChannelId), |
| 48 render_frame_id_(MSG_ROUTING_NONE), | 48 render_frame_id_(MSG_ROUTING_NONE), |
| 49 client_(NULL) {} | 49 client_(NULL) {} |
| 50 | 50 |
| 51 WebSocketBridge::~WebSocketBridge() { | 51 WebSocketBridge::~WebSocketBridge() { |
| 52 if (channel_id_ != kInvalidChannelId) { | 52 if (channel_id_ != kInvalidChannelId) { |
| 53 // The connection is abruptly disconnected by the renderer without | 53 // The connection is abruptly disconnected by the renderer without |
| 54 // closing handshake. | 54 // closing handshake. |
| 55 ChildThread::current()->Send( | 55 ChildThreadImpl::current()->Send( |
| 56 new WebSocketMsg_DropChannel(channel_id_, | 56 new WebSocketMsg_DropChannel(channel_id_, |
| 57 false, | 57 false, |
| 58 kAbnormalShutdownOpCode, | 58 kAbnormalShutdownOpCode, |
| 59 std::string())); | 59 std::string())); |
| 60 } | 60 } |
| 61 Disconnect(); | 61 Disconnect(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) { | 64 bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) { |
| 65 bool handled = true; | 65 bool handled = true; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // |this| can be deleted here. | 207 // |this| can be deleted here. |
| 208 } | 208 } |
| 209 | 209 |
| 210 void WebSocketBridge::connect( | 210 void WebSocketBridge::connect( |
| 211 const WebURL& url, | 211 const WebURL& url, |
| 212 const WebVector<WebString>& protocols, | 212 const WebVector<WebString>& protocols, |
| 213 const WebSerializedOrigin& origin, | 213 const WebSerializedOrigin& origin, |
| 214 WebSocketHandleClient* client) { | 214 WebSocketHandleClient* client) { |
| 215 DCHECK_EQ(kInvalidChannelId, channel_id_); | 215 DCHECK_EQ(kInvalidChannelId, channel_id_); |
| 216 WebSocketDispatcher* dispatcher = | 216 WebSocketDispatcher* dispatcher = |
| 217 ChildThread::current()->websocket_dispatcher(); | 217 ChildThreadImpl::current()->websocket_dispatcher(); |
| 218 channel_id_ = dispatcher->AddBridge(this); | 218 channel_id_ = dispatcher->AddBridge(this); |
| 219 client_ = client; | 219 client_ = client; |
| 220 | 220 |
| 221 std::vector<std::string> protocols_to_pass; | 221 std::vector<std::string> protocols_to_pass; |
| 222 for (size_t i = 0; i < protocols.size(); ++i) | 222 for (size_t i = 0; i < protocols.size(); ++i) |
| 223 protocols_to_pass.push_back(protocols[i].utf8()); | 223 protocols_to_pass.push_back(protocols[i].utf8()); |
| 224 url::Origin origin_to_pass(origin); | 224 url::Origin origin_to_pass(origin); |
| 225 | 225 |
| 226 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", (" | 226 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", (" |
| 227 << JoinString(protocols_to_pass, ", ") << "), " | 227 << JoinString(protocols_to_pass, ", ") << "), " |
| 228 << origin_to_pass.string() << ")"; | 228 << origin_to_pass.string() << ")"; |
| 229 | 229 |
| 230 ChildThread::current()->Send(new WebSocketHostMsg_AddChannelRequest( | 230 ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest( |
| 231 channel_id_, url, protocols_to_pass, origin_to_pass, render_frame_id_)); | 231 channel_id_, url, protocols_to_pass, origin_to_pass, render_frame_id_)); |
| 232 } | 232 } |
| 233 | 233 |
| 234 void WebSocketBridge::send(bool fin, | 234 void WebSocketBridge::send(bool fin, |
| 235 WebSocketHandle::MessageType type, | 235 WebSocketHandle::MessageType type, |
| 236 const char* data, | 236 const char* data, |
| 237 size_t size) { | 237 size_t size) { |
| 238 if (channel_id_ == kInvalidChannelId) | 238 if (channel_id_ == kInvalidChannelId) |
| 239 return; | 239 return; |
| 240 | 240 |
| 241 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | 241 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; |
| 242 switch (type) { | 242 switch (type) { |
| 243 case WebSocketHandle::MessageTypeContinuation: | 243 case WebSocketHandle::MessageTypeContinuation: |
| 244 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | 244 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; |
| 245 break; | 245 break; |
| 246 case WebSocketHandle::MessageTypeText: | 246 case WebSocketHandle::MessageTypeText: |
| 247 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; | 247 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; |
| 248 break; | 248 break; |
| 249 case WebSocketHandle::MessageTypeBinary: | 249 case WebSocketHandle::MessageTypeBinary: |
| 250 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; | 250 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; |
| 251 break; | 251 break; |
| 252 } | 252 } |
| 253 | 253 |
| 254 DVLOG(1) << "Bridge #" << channel_id_ << " Send(" | 254 DVLOG(1) << "Bridge #" << channel_id_ << " Send(" |
| 255 << fin << ", " << type_to_pass << ", " | 255 << fin << ", " << type_to_pass << ", " |
| 256 << "(data size = " << size << "))"; | 256 << "(data size = " << size << "))"; |
| 257 | 257 |
| 258 ChildThread::current()->Send( | 258 ChildThreadImpl::current()->Send( |
| 259 new WebSocketMsg_SendFrame(channel_id_, | 259 new WebSocketMsg_SendFrame(channel_id_, |
| 260 fin, | 260 fin, |
| 261 type_to_pass, | 261 type_to_pass, |
| 262 std::vector<char>(data, data + size))); | 262 std::vector<char>(data, data + size))); |
| 263 } | 263 } |
| 264 | 264 |
| 265 void WebSocketBridge::flowControl(int64_t quota) { | 265 void WebSocketBridge::flowControl(int64_t quota) { |
| 266 if (channel_id_ == kInvalidChannelId) | 266 if (channel_id_ == kInvalidChannelId) |
| 267 return; | 267 return; |
| 268 | 268 |
| 269 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; | 269 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; |
| 270 | 270 |
| 271 ChildThread::current()->Send( | 271 ChildThreadImpl::current()->Send( |
| 272 new WebSocketMsg_FlowControl(channel_id_, quota)); | 272 new WebSocketMsg_FlowControl(channel_id_, quota)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 void WebSocketBridge::close(unsigned short code, | 275 void WebSocketBridge::close(unsigned short code, |
| 276 const WebString& reason) { | 276 const WebString& reason) { |
| 277 if (channel_id_ == kInvalidChannelId) | 277 if (channel_id_ == kInvalidChannelId) |
| 278 return; | 278 return; |
| 279 | 279 |
| 280 std::string reason_to_pass = reason.utf8(); | 280 std::string reason_to_pass = reason.utf8(); |
| 281 DVLOG(1) << "Bridge #" << channel_id_ << " Close(" | 281 DVLOG(1) << "Bridge #" << channel_id_ << " Close(" |
| 282 << code << ", " << reason_to_pass << ")"; | 282 << code << ", " << reason_to_pass << ")"; |
| 283 // This method is for closing handshake and hence |was_clean| shall be true. | 283 // This method is for closing handshake and hence |was_clean| shall be true. |
| 284 ChildThread::current()->Send( | 284 ChildThreadImpl::current()->Send( |
| 285 new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass)); | 285 new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass)); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void WebSocketBridge::Disconnect() { | 288 void WebSocketBridge::Disconnect() { |
| 289 if (channel_id_ == kInvalidChannelId) | 289 if (channel_id_ == kInvalidChannelId) |
| 290 return; | 290 return; |
| 291 WebSocketDispatcher* dispatcher = | 291 WebSocketDispatcher* dispatcher = |
| 292 ChildThread::current()->websocket_dispatcher(); | 292 ChildThreadImpl::current()->websocket_dispatcher(); |
| 293 dispatcher->RemoveBridge(channel_id_); | 293 dispatcher->RemoveBridge(channel_id_); |
| 294 | 294 |
| 295 channel_id_ = kInvalidChannelId; | 295 channel_id_ = kInvalidChannelId; |
| 296 client_ = NULL; | 296 client_ = NULL; |
| 297 } | 297 } |
| 298 | 298 |
| 299 } // namespace content | 299 } // namespace content |
| OLD | NEW |