| 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/browser/renderer_host/websocket_host.h" | 5 #include "content/browser/renderer_host/websocket_host.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "content/browser/renderer_host/websocket_dispatcher_host.h" | 9 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
| 10 #include "content/common/websocket_messages.h" | 10 #include "content/common/websocket_messages.h" |
| 11 #include "ipc/ipc_message_macros.h" | 11 #include "ipc/ipc_message_macros.h" |
| 12 #include "net/websockets/websocket_channel.h" | 12 #include "net/websockets/websocket_channel.h" |
| 13 #include "net/websockets/websocket_event_interface.h" | 13 #include "net/websockets/websocket_event_interface.h" |
| 14 #include "net/websockets/websocket_frame.h" // for WebSocketFrameHeader::OpCode | 14 #include "net/websockets/websocket_frame.h" // for WebSocketFrameHeader::OpCode |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 typedef net::WebSocketEventInterface::ChannelState ChannelState; |
| 21 typedef WebSocketDispatcherHost::WebSocketHostState WebSocketHostState; |
| 22 |
| 20 // Convert a content::WebSocketMessageType to a | 23 // Convert a content::WebSocketMessageType to a |
| 21 // net::WebSocketFrameHeader::OpCode | 24 // net::WebSocketFrameHeader::OpCode |
| 22 net::WebSocketFrameHeader::OpCode MessageTypeToOpCode( | 25 net::WebSocketFrameHeader::OpCode MessageTypeToOpCode( |
| 23 WebSocketMessageType type) { | 26 WebSocketMessageType type) { |
| 24 DCHECK(type == WEB_SOCKET_MESSAGE_TYPE_CONTINUATION || | 27 DCHECK(type == WEB_SOCKET_MESSAGE_TYPE_CONTINUATION || |
| 25 type == WEB_SOCKET_MESSAGE_TYPE_TEXT || | 28 type == WEB_SOCKET_MESSAGE_TYPE_TEXT || |
| 26 type == WEB_SOCKET_MESSAGE_TYPE_BINARY); | 29 type == WEB_SOCKET_MESSAGE_TYPE_BINARY); |
| 27 typedef net::WebSocketFrameHeader::OpCode OpCode; | 30 typedef net::WebSocketFrameHeader::OpCode OpCode; |
| 28 // These compile asserts verify that the same underlying values are used for | 31 // These compile asserts verify that the same underlying values are used for |
| 29 // both types, so we can simply cast between them. | 32 // both types, so we can simply cast between them. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 41 | 44 |
| 42 WebSocketMessageType OpCodeToMessageType( | 45 WebSocketMessageType OpCodeToMessageType( |
| 43 net::WebSocketFrameHeader::OpCode opCode) { | 46 net::WebSocketFrameHeader::OpCode opCode) { |
| 44 DCHECK(opCode == net::WebSocketFrameHeader::kOpCodeContinuation || | 47 DCHECK(opCode == net::WebSocketFrameHeader::kOpCodeContinuation || |
| 45 opCode == net::WebSocketFrameHeader::kOpCodeText || | 48 opCode == net::WebSocketFrameHeader::kOpCodeText || |
| 46 opCode == net::WebSocketFrameHeader::kOpCodeBinary); | 49 opCode == net::WebSocketFrameHeader::kOpCodeBinary); |
| 47 // This cast is guaranteed valid by the COMPILE_ASSERT() statements above. | 50 // This cast is guaranteed valid by the COMPILE_ASSERT() statements above. |
| 48 return static_cast<WebSocketMessageType>(opCode); | 51 return static_cast<WebSocketMessageType>(opCode); |
| 49 } | 52 } |
| 50 | 53 |
| 54 ChannelState StateCast(WebSocketHostState host_state) { |
| 55 const WebSocketHostState WEBSOCKET_HOST_ALIVE = |
| 56 WebSocketDispatcherHost::WEBSOCKET_HOST_ALIVE; |
| 57 const WebSocketHostState WEBSOCKET_HOST_DELETED = |
| 58 WebSocketDispatcherHost::WEBSOCKET_HOST_DELETED; |
| 59 |
| 60 DCHECK(host_state == WEBSOCKET_HOST_ALIVE || |
| 61 host_state == WEBSOCKET_HOST_DELETED); |
| 62 // These compile asserts verify that we can get away with using static_cast<> |
| 63 // for the conversion. |
| 64 COMPILE_ASSERT(static_cast<ChannelState>(WEBSOCKET_HOST_ALIVE) == |
| 65 net::WebSocketEventInterface::CHANNEL_ALIVE, |
| 66 enum_values_must_match_for_state_alive); |
| 67 COMPILE_ASSERT(static_cast<ChannelState>(WEBSOCKET_HOST_DELETED) == |
| 68 net::WebSocketEventInterface::CHANNEL_DELETED, |
| 69 enum_values_must_match_for_state_deleted); |
| 70 return static_cast<ChannelState>(host_state); |
| 71 } |
| 72 |
| 51 // Implementation of net::WebSocketEventInterface. Receives events from our | 73 // Implementation of net::WebSocketEventInterface. Receives events from our |
| 52 // WebSocketChannel object. Each event is translated to an IPC and sent to the | 74 // WebSocketChannel object. Each event is translated to an IPC and sent to the |
| 53 // renderer or child process via WebSocketDispatcherHost. | 75 // renderer or child process via WebSocketDispatcherHost. |
| 54 class WebSocketEventHandler : public net::WebSocketEventInterface { | 76 class WebSocketEventHandler : public net::WebSocketEventInterface { |
| 55 public: | 77 public: |
| 56 WebSocketEventHandler(WebSocketDispatcherHost* dispatcher, int routing_id); | 78 WebSocketEventHandler(WebSocketDispatcherHost* dispatcher, int routing_id); |
| 57 virtual ~WebSocketEventHandler(); | 79 virtual ~WebSocketEventHandler(); |
| 58 | 80 |
| 59 // net::WebSocketEventInterface implementation | 81 // net::WebSocketEventInterface implementation |
| 60 | 82 |
| 61 // TODO(ricea): Add |extensions| parameter to pass the list of enabled | 83 // TODO(ricea): Add |extensions| parameter to pass the list of enabled |
| 62 // WebSocket extensions through to the renderer to make it visible to | 84 // WebSocket extensions through to the renderer to make it visible to |
| 63 // Javascript. | 85 // Javascript. |
| 64 virtual void OnAddChannelResponse( | 86 virtual ChannelState OnAddChannelResponse( |
| 65 bool fail, | 87 bool fail, |
| 66 const std::string& selected_subprotocol) OVERRIDE; | 88 const std::string& selected_subprotocol) OVERRIDE; |
| 67 virtual void OnDataFrame(bool fin, | 89 virtual ChannelState OnDataFrame(bool fin, |
| 68 WebSocketMessageType type, | 90 WebSocketMessageType type, |
| 69 const std::vector<char>& data) OVERRIDE; | 91 const std::vector<char>& data) OVERRIDE; |
| 70 virtual void OnClosingHandshake() OVERRIDE; | 92 virtual ChannelState OnClosingHandshake() OVERRIDE; |
| 71 virtual void OnFlowControl(int64 quota) OVERRIDE; | 93 virtual ChannelState OnFlowControl(int64 quota) OVERRIDE; |
| 72 virtual void OnDropChannel(uint16 code, | 94 virtual ChannelState OnDropChannel(uint16 code, |
| 73 const std::string& reason) OVERRIDE; | 95 const std::string& reason) OVERRIDE; |
| 74 | 96 |
| 75 private: | 97 private: |
| 76 WebSocketDispatcherHost* const dispatcher_; | 98 WebSocketDispatcherHost* const dispatcher_; |
| 77 const int routing_id_; | 99 const int routing_id_; |
| 78 | 100 |
| 79 DISALLOW_COPY_AND_ASSIGN(WebSocketEventHandler); | 101 DISALLOW_COPY_AND_ASSIGN(WebSocketEventHandler); |
| 80 }; | 102 }; |
| 81 | 103 |
| 82 WebSocketEventHandler::WebSocketEventHandler( | 104 WebSocketEventHandler::WebSocketEventHandler( |
| 83 WebSocketDispatcherHost* dispatcher, | 105 WebSocketDispatcherHost* dispatcher, |
| 84 int routing_id) | 106 int routing_id) |
| 85 : dispatcher_(dispatcher), routing_id_(routing_id) {} | 107 : dispatcher_(dispatcher), routing_id_(routing_id) {} |
| 86 | 108 |
| 87 WebSocketEventHandler::~WebSocketEventHandler() { | 109 WebSocketEventHandler::~WebSocketEventHandler() { |
| 88 DVLOG(1) << "WebSocketEventHandler destroyed routing_id= " << routing_id_; | 110 DVLOG(1) << "WebSocketEventHandler destroyed routing_id= " << routing_id_; |
| 89 } | 111 } |
| 90 | 112 |
| 91 void WebSocketEventHandler::OnAddChannelResponse( | 113 ChannelState WebSocketEventHandler::OnAddChannelResponse( |
| 92 bool fail, | 114 bool fail, |
| 93 const std::string& selected_protocol) { | 115 const std::string& selected_protocol) { |
| 94 dispatcher_->SendAddChannelResponse( | 116 return StateCast(dispatcher_->SendAddChannelResponse( |
| 95 routing_id_, fail, selected_protocol, std::string()); | 117 routing_id_, fail, selected_protocol, std::string())); |
| 96 // |this| may have been deleted here. | |
| 97 } | 118 } |
| 98 | 119 |
| 99 void WebSocketEventHandler::OnDataFrame(bool fin, | 120 ChannelState WebSocketEventHandler::OnDataFrame( |
| 100 net::WebSocketFrameHeader::OpCode type, | 121 bool fin, |
| 101 const std::vector<char>& data) { | 122 net::WebSocketFrameHeader::OpCode type, |
| 102 dispatcher_->SendFrame(routing_id_, fin, OpCodeToMessageType(type), data); | 123 const std::vector<char>& data) { |
| 103 // |this| may have been deleted here. | 124 return StateCast(dispatcher_->SendFrame( |
| 125 routing_id_, fin, OpCodeToMessageType(type), data)); |
| 104 } | 126 } |
| 105 | 127 |
| 106 void WebSocketEventHandler::OnClosingHandshake() { | 128 ChannelState WebSocketEventHandler::OnClosingHandshake() { |
| 107 dispatcher_->SendClosing(routing_id_); | 129 return StateCast(dispatcher_->SendClosing(routing_id_)); |
| 108 // |this| may have been deleted here. | |
| 109 } | 130 } |
| 110 | 131 |
| 111 void WebSocketEventHandler::OnFlowControl(int64 quota) { | 132 ChannelState WebSocketEventHandler::OnFlowControl(int64 quota) { |
| 112 dispatcher_->SendFlowControl(routing_id_, quota); | 133 return StateCast(dispatcher_->SendFlowControl(routing_id_, quota)); |
| 113 // |this| may have been deleted here. | |
| 114 } | 134 } |
| 115 | 135 |
| 116 void WebSocketEventHandler::OnDropChannel(uint16 code, | 136 ChannelState WebSocketEventHandler::OnDropChannel(uint16 code, |
| 117 const std::string& reason) { | 137 const std::string& reason) { |
| 118 dispatcher_->DoDropChannel(routing_id_, code, reason); | 138 return StateCast(dispatcher_->DoDropChannel(routing_id_, code, reason)); |
| 119 // |this| has been deleted here. | |
| 120 } | 139 } |
| 121 | 140 |
| 122 } // namespace | 141 } // namespace |
| 123 | 142 |
| 124 WebSocketHost::WebSocketHost(int routing_id, | 143 WebSocketHost::WebSocketHost(int routing_id, |
| 125 WebSocketDispatcherHost* dispatcher, | 144 WebSocketDispatcherHost* dispatcher, |
| 126 net::URLRequestContext* url_request_context) { | 145 net::URLRequestContext* url_request_context) { |
| 127 DVLOG(1) << "WebSocketHost: created routing_id= " << routing_id; | 146 DVLOG(1) << "WebSocketHost: created routing_id= " << routing_id; |
| 128 scoped_ptr<net::WebSocketEventInterface> event_interface( | 147 scoped_ptr<net::WebSocketEventInterface> event_interface( |
| 129 new WebSocketEventHandler(dispatcher, routing_id)); | 148 new WebSocketEventHandler(dispatcher, routing_id)); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 void WebSocketHost::OnDropChannel(uint16 code, const std::string& reason) { | 197 void WebSocketHost::OnDropChannel(uint16 code, const std::string& reason) { |
| 179 DVLOG(3) << "WebSocketDispatcherHost::OnDropChannel" | 198 DVLOG(3) << "WebSocketDispatcherHost::OnDropChannel" |
| 180 << " routing_id= " << routing_id_ << " code= " << code | 199 << " routing_id= " << routing_id_ << " code= " << code |
| 181 << " reason= " << reason; | 200 << " reason= " << reason; |
| 182 | 201 |
| 183 channel_->StartClosingHandshake(code, reason); | 202 channel_->StartClosingHandshake(code, reason); |
| 184 } | 203 } |
| 185 | 204 |
| 186 | 205 |
| 187 } // namespace content | 206 } // namespace content |
| OLD | NEW |