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_dispatcher_host.h" | 5 #include "content/browser/renderer_host/websocket_dispatcher_host.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "content/browser/renderer_host/websocket_host.h" | 12 #include "content/browser/renderer_host/websocket_host.h" |
13 #include "content/common/websocket_messages.h" | 13 #include "content/common/websocket_messages.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
| 17 namespace { |
| 18 |
| 19 // Many methods defined in this file return a WebSocketHostState enum |
| 20 // value. Make WebSocketHostState visible at file scope so it doesn't have to be |
| 21 // fully-qualified every time. |
| 22 typedef WebSocketDispatcherHost::WebSocketHostState WebSocketHostState; |
| 23 |
| 24 } // namespace |
| 25 |
17 WebSocketDispatcherHost::WebSocketDispatcherHost( | 26 WebSocketDispatcherHost::WebSocketDispatcherHost( |
18 const GetRequestContextCallback& get_context_callback) | 27 const GetRequestContextCallback& get_context_callback) |
19 : get_context_callback_(get_context_callback), | 28 : get_context_callback_(get_context_callback), |
20 websocket_host_factory_( | 29 websocket_host_factory_( |
21 base::Bind(&WebSocketDispatcherHost::CreateWebSocketHost, | 30 base::Bind(&WebSocketDispatcherHost::CreateWebSocketHost, |
22 base::Unretained(this))) {} | 31 base::Unretained(this))) {} |
23 | 32 |
24 WebSocketDispatcherHost::WebSocketDispatcherHost( | 33 WebSocketDispatcherHost::WebSocketDispatcherHost( |
25 const GetRequestContextCallback& get_context_callback, | 34 const GetRequestContextCallback& get_context_callback, |
26 const WebSocketHostFactory& websocket_host_factory) | 35 const WebSocketHostFactory& websocket_host_factory) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 return true; // We handled the message (by ignoring it). | 75 return true; // We handled the message (by ignoring it). |
67 } | 76 } |
68 return host->OnMessageReceived(message, message_was_ok); | 77 return host->OnMessageReceived(message, message_was_ok); |
69 } | 78 } |
70 | 79 |
71 WebSocketHost* WebSocketDispatcherHost::GetHost(int routing_id) const { | 80 WebSocketHost* WebSocketDispatcherHost::GetHost(int routing_id) const { |
72 WebSocketHostTable::const_iterator it = hosts_.find(routing_id); | 81 WebSocketHostTable::const_iterator it = hosts_.find(routing_id); |
73 return it == hosts_.end() ? NULL : it->second; | 82 return it == hosts_.end() ? NULL : it->second; |
74 } | 83 } |
75 | 84 |
76 void WebSocketDispatcherHost::SendOrDrop(IPC::Message* message) { | 85 WebSocketHostState WebSocketDispatcherHost::SendOrDrop(IPC::Message* message) { |
77 if (!Send(message)) { | 86 if (!Send(message)) { |
78 DVLOG(1) << "Sending of message type " << message->type() | 87 DVLOG(1) << "Sending of message type " << message->type() |
79 << " failed. Dropping channel."; | 88 << " failed. Dropping channel."; |
80 DeleteWebSocketHost(message->routing_id()); | 89 DeleteWebSocketHost(message->routing_id()); |
| 90 return WEBSOCKET_HOST_DELETED; |
81 } | 91 } |
| 92 return WEBSOCKET_HOST_ALIVE; |
82 } | 93 } |
83 | 94 |
84 void WebSocketDispatcherHost::SendAddChannelResponse( | 95 WebSocketHostState WebSocketDispatcherHost::SendAddChannelResponse( |
85 int routing_id, | 96 int routing_id, |
86 bool fail, | 97 bool fail, |
87 const std::string& selected_protocol, | 98 const std::string& selected_protocol, |
88 const std::string& extensions) { | 99 const std::string& extensions) { |
89 SendOrDrop(new WebSocketMsg_AddChannelResponse( | 100 if (SendOrDrop(new WebSocketMsg_AddChannelResponse( |
90 routing_id, fail, selected_protocol, extensions)); | 101 routing_id, fail, selected_protocol, extensions)) == |
91 if (fail) | 102 WEBSOCKET_HOST_DELETED) |
| 103 return WEBSOCKET_HOST_DELETED; |
| 104 if (fail) { |
92 DeleteWebSocketHost(routing_id); | 105 DeleteWebSocketHost(routing_id); |
| 106 return WEBSOCKET_HOST_DELETED; |
| 107 } |
| 108 return WEBSOCKET_HOST_ALIVE; |
93 } | 109 } |
94 | 110 |
95 void WebSocketDispatcherHost::SendFrame(int routing_id, | 111 WebSocketHostState WebSocketDispatcherHost::SendFrame( |
96 bool fin, | 112 int routing_id, |
97 WebSocketMessageType type, | 113 bool fin, |
98 const std::vector<char>& data) { | 114 WebSocketMessageType type, |
99 SendOrDrop(new WebSocketMsg_SendFrame(routing_id, fin, type, data)); | 115 const std::vector<char>& data) { |
| 116 return SendOrDrop(new WebSocketMsg_SendFrame(routing_id, fin, type, data)); |
100 } | 117 } |
101 | 118 |
102 void WebSocketDispatcherHost::SendFlowControl(int routing_id, int64 quota) { | 119 WebSocketHostState WebSocketDispatcherHost::SendFlowControl(int routing_id, |
103 SendOrDrop(new WebSocketMsg_FlowControl(routing_id, quota)); | 120 int64 quota) { |
| 121 return SendOrDrop(new WebSocketMsg_FlowControl(routing_id, quota)); |
104 } | 122 } |
105 | 123 |
106 void WebSocketDispatcherHost::SendClosing(int routing_id) { | 124 WebSocketHostState WebSocketDispatcherHost::SendClosing(int routing_id) { |
107 // TODO(ricea): Implement the SendClosing IPC. | 125 // TODO(ricea): Implement the SendClosing IPC. |
| 126 return WEBSOCKET_HOST_ALIVE; |
108 } | 127 } |
109 | 128 |
110 void WebSocketDispatcherHost::DoDropChannel(int routing_id, | 129 WebSocketHostState WebSocketDispatcherHost::DoDropChannel( |
111 uint16 code, | 130 int routing_id, |
112 const std::string& reason) { | 131 uint16 code, |
113 SendOrDrop(new WebSocketMsg_DropChannel(routing_id, code, reason)); | 132 const std::string& reason) { |
| 133 if (SendOrDrop(new WebSocketMsg_DropChannel(routing_id, code, reason)) == |
| 134 WEBSOCKET_HOST_DELETED) |
| 135 return WEBSOCKET_HOST_DELETED; |
114 DeleteWebSocketHost(routing_id); | 136 DeleteWebSocketHost(routing_id); |
| 137 return WEBSOCKET_HOST_DELETED; |
115 } | 138 } |
116 | 139 |
117 WebSocketDispatcherHost::~WebSocketDispatcherHost() { | 140 WebSocketDispatcherHost::~WebSocketDispatcherHost() { |
118 STLDeleteContainerPairSecondPointers(hosts_.begin(), hosts_.end()); | 141 STLDeleteContainerPairSecondPointers(hosts_.begin(), hosts_.end()); |
119 } | 142 } |
120 | 143 |
121 void WebSocketDispatcherHost::DeleteWebSocketHost(int routing_id) { | 144 void WebSocketDispatcherHost::DeleteWebSocketHost(int routing_id) { |
122 WebSocketHostTable::iterator it = hosts_.find(routing_id); | 145 WebSocketHostTable::iterator it = hosts_.find(routing_id); |
123 if (it != hosts_.end()) { | 146 DCHECK(it != hosts_.end()); |
124 delete it->second; | 147 delete it->second; |
125 hosts_.erase(it); | 148 hosts_.erase(it); |
126 } | |
127 } | 149 } |
128 | 150 |
129 } // namespace content | 151 } // namespace content |
OLD | NEW |