| 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 typedef WebSocketDispatcherHost::WebSocketHostState WebSocketHostState; |
| 20 |
| 21 } // namespace |
| 22 |
| 17 WebSocketDispatcherHost::WebSocketDispatcherHost( | 23 WebSocketDispatcherHost::WebSocketDispatcherHost( |
| 18 const GetRequestContextCallback& get_context_callback) | 24 const GetRequestContextCallback& get_context_callback) |
| 19 : get_context_callback_(get_context_callback) {} | 25 : get_context_callback_(get_context_callback) {} |
| 20 | 26 |
| 21 bool WebSocketDispatcherHost::OnMessageReceived(const IPC::Message& message, | 27 bool WebSocketDispatcherHost::OnMessageReceived(const IPC::Message& message, |
| 22 bool* message_was_ok) { | 28 bool* message_was_ok) { |
| 23 switch (message.type()) { | 29 switch (message.type()) { |
| 24 case WebSocketHostMsg_AddChannelRequest::ID: | 30 case WebSocketHostMsg_AddChannelRequest::ID: |
| 25 case WebSocketMsg_SendFrame::ID: | 31 case WebSocketMsg_SendFrame::ID: |
| 26 case WebSocketMsg_FlowControl::ID: | 32 case WebSocketMsg_FlowControl::ID: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 53 return true; // We handled the message (by ignoring it). | 59 return true; // We handled the message (by ignoring it). |
| 54 } | 60 } |
| 55 return host->OnMessageReceived(message, message_was_ok); | 61 return host->OnMessageReceived(message, message_was_ok); |
| 56 } | 62 } |
| 57 | 63 |
| 58 WebSocketHost* WebSocketDispatcherHost::GetHost(int routing_id) const { | 64 WebSocketHost* WebSocketDispatcherHost::GetHost(int routing_id) const { |
| 59 WebSocketHostTable::const_iterator it = hosts_.find(routing_id); | 65 WebSocketHostTable::const_iterator it = hosts_.find(routing_id); |
| 60 return it == hosts_.end() ? NULL : it->second; | 66 return it == hosts_.end() ? NULL : it->second; |
| 61 } | 67 } |
| 62 | 68 |
| 63 void WebSocketDispatcherHost::SendOrDrop(IPC::Message* message) { | 69 WebSocketHostState WebSocketDispatcherHost::SendOrDrop(IPC::Message* message) { |
| 64 if (!Send(message)) { | 70 if (!Send(message)) { |
| 65 DVLOG(1) << "Sending of message type " << message->type() | 71 DVLOG(1) << "Sending of message type " << message->type() |
| 66 << " failed. Dropping channel."; | 72 << " failed. Dropping channel."; |
| 67 DeleteWebSocketHost(message->routing_id()); | 73 DeleteWebSocketHost(message->routing_id()); |
| 74 return WEBSOCKET_HOST_DELETED; |
| 68 } | 75 } |
| 76 return WEBSOCKET_HOST_ALIVE; |
| 69 } | 77 } |
| 70 | 78 |
| 71 void WebSocketDispatcherHost::SendAddChannelResponse( | 79 WebSocketHostState WebSocketDispatcherHost::SendAddChannelResponse( |
| 72 int routing_id, | 80 int routing_id, |
| 73 bool fail, | 81 bool fail, |
| 74 const std::string& selected_protocol, | 82 const std::string& selected_protocol, |
| 75 const std::string& extensions) { | 83 const std::string& extensions) { |
| 76 SendOrDrop(new WebSocketMsg_AddChannelResponse( | 84 if (SendOrDrop(new WebSocketMsg_AddChannelResponse( |
| 77 routing_id, fail, selected_protocol, extensions)); | 85 routing_id, fail, selected_protocol, extensions)) == |
| 78 if (fail) | 86 WEBSOCKET_HOST_DELETED) |
| 87 return WEBSOCKET_HOST_DELETED; |
| 88 if (fail) { |
| 79 DeleteWebSocketHost(routing_id); | 89 DeleteWebSocketHost(routing_id); |
| 90 return WEBSOCKET_HOST_DELETED; |
| 91 } |
| 92 return WEBSOCKET_HOST_ALIVE; |
| 80 } | 93 } |
| 81 | 94 |
| 82 void WebSocketDispatcherHost::SendFrame(int routing_id, | 95 WebSocketHostState WebSocketDispatcherHost::SendFrame( |
| 83 bool fin, | 96 int routing_id, |
| 84 WebSocketMessageType type, | 97 bool fin, |
| 85 const std::vector<char>& data) { | 98 WebSocketMessageType type, |
| 86 SendOrDrop(new WebSocketMsg_SendFrame(routing_id, fin, type, data)); | 99 const std::vector<char>& data) { |
| 100 return SendOrDrop(new WebSocketMsg_SendFrame(routing_id, fin, type, data)); |
| 87 } | 101 } |
| 88 | 102 |
| 89 void WebSocketDispatcherHost::SendFlowControl(int routing_id, int64 quota) { | 103 WebSocketHostState WebSocketDispatcherHost::SendFlowControl(int routing_id, |
| 90 SendOrDrop(new WebSocketMsg_FlowControl(routing_id, quota)); | 104 int64 quota) { |
| 105 return SendOrDrop(new WebSocketMsg_FlowControl(routing_id, quota)); |
| 91 } | 106 } |
| 92 | 107 |
| 93 void WebSocketDispatcherHost::SendClosing(int routing_id) { | 108 WebSocketHostState WebSocketDispatcherHost::SendClosing(int routing_id) { |
| 94 // TODO(ricea): Implement the SendClosing IPC. | 109 // TODO(ricea): Implement the SendClosing IPC. |
| 110 return WEBSOCKET_HOST_ALIVE; |
| 95 } | 111 } |
| 96 | 112 |
| 97 void WebSocketDispatcherHost::DoDropChannel(int routing_id, | 113 WebSocketHostState WebSocketDispatcherHost::DoDropChannel( |
| 98 uint16 code, | 114 int routing_id, |
| 99 const std::string& reason) { | 115 uint16 code, |
| 100 SendOrDrop(new WebSocketMsg_DropChannel(routing_id, code, reason)); | 116 const std::string& reason) { |
| 117 if (SendOrDrop(new WebSocketMsg_DropChannel(routing_id, code, reason)) == |
| 118 WEBSOCKET_HOST_DELETED) |
| 119 return WEBSOCKET_HOST_DELETED; |
| 101 DeleteWebSocketHost(routing_id); | 120 DeleteWebSocketHost(routing_id); |
| 121 return WEBSOCKET_HOST_DELETED; |
| 102 } | 122 } |
| 103 | 123 |
| 104 WebSocketDispatcherHost::~WebSocketDispatcherHost() { | 124 WebSocketDispatcherHost::~WebSocketDispatcherHost() { |
| 105 STLDeleteContainerPairSecondPointers(hosts_.begin(), hosts_.end()); | 125 STLDeleteContainerPairSecondPointers(hosts_.begin(), hosts_.end()); |
| 106 } | 126 } |
| 107 | 127 |
| 108 void WebSocketDispatcherHost::DeleteWebSocketHost(int routing_id) { | 128 void WebSocketDispatcherHost::DeleteWebSocketHost(int routing_id) { |
| 109 WebSocketHostTable::iterator it = hosts_.find(routing_id); | 129 WebSocketHostTable::iterator it = hosts_.find(routing_id); |
| 110 if (it != hosts_.end()) { | 130 DCHECK(it != hosts_.end()); |
| 111 delete it->second; | 131 delete it->second; |
| 112 hosts_.erase(it); | 132 hosts_.erase(it); |
| 113 } | |
| 114 } | 133 } |
| 115 | 134 |
| 116 } // namespace content | 135 } // namespace content |
| OLD | NEW |