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