OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/child/websocket_dispatcher.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "content/child/websocket_bridge.h" |
| 13 #include "content/common/websocket_messages.h" |
| 14 #include "ipc/ipc_message.h" |
| 15 #include "ipc/ipc_message_macros.h" |
| 16 #include "url/gurl.h" |
| 17 #include "third_party/WebKit/public/platform/WebSocketHandle.h" |
| 18 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
| 19 |
| 20 using WebKit::WebSocketHandle; |
| 21 using WebKit::WebSocketHandleClient; |
| 22 using WebKit::WebString; |
| 23 using WebKit::WebURL; |
| 24 using WebKit::WebVector; |
| 25 |
| 26 namespace content { |
| 27 |
| 28 WebSocketDispatcher::WebSocketDispatcher() : channel_id_max_(0) {} |
| 29 |
| 30 WebSocketDispatcher::~WebSocketDispatcher() {} |
| 31 |
| 32 int WebSocketDispatcher::AddBridge(WebSocketBridge* bridge) { |
| 33 ++channel_id_max_; |
| 34 bridges_.insert(std::make_pair(channel_id_max_, bridge)); |
| 35 return channel_id_max_; |
| 36 } |
| 37 |
| 38 void WebSocketDispatcher::RemoveBridge(int channel_id) { |
| 39 std::map<int, WebSocketBridge*>::iterator i = bridges_.find(channel_id); |
| 40 if (i == bridges_.end()) { |
| 41 DVLOG(1) << "Remove a non-existent bridge(" << channel_id << ")"; |
| 42 return; |
| 43 } |
| 44 bridges_.erase(i); |
| 45 } |
| 46 |
| 47 bool WebSocketDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 48 bool handled = true; |
| 49 IPC_BEGIN_MESSAGE_MAP(WebSocketDispatcher, msg) |
| 50 IPC_MESSAGE_HANDLER(WebSocketMsg_AddChannelResponse, OnConnected) |
| 51 IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, OnReceivedData) |
| 52 IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, OnReceivedFlowControl) |
| 53 IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, OnClosed) |
| 54 IPC_MESSAGE_UNHANDLED(handled = false) |
| 55 IPC_END_MESSAGE_MAP() |
| 56 return handled; |
| 57 } |
| 58 |
| 59 void WebSocketDispatcher::OnConnected(int channel_id, |
| 60 bool fail, |
| 61 const std::string& selected_protocol, |
| 62 const std::string& extensions) { |
| 63 DVLOG(1) << "WebSocketDispatcher::OnConnected(" |
| 64 << channel_id << ", " |
| 65 << fail << ", " |
| 66 << selected_protocol << ", " |
| 67 << extensions << ")"; |
| 68 WebSocketBridge* bridge = GetBridge(channel_id); |
| 69 if (!bridge) return; |
| 70 WebString protocol_to_pass = WebString::fromUTF8(selected_protocol); |
| 71 WebString extensions_to_pass = WebString::fromUTF8(extensions); |
| 72 bridge->DidConnect(fail, protocol_to_pass, extensions_to_pass); |
| 73 } |
| 74 |
| 75 void WebSocketDispatcher::OnReceivedData(int channel_id, |
| 76 bool fin, |
| 77 WebSocketMessageType type, |
| 78 const std::vector<char>& data) { |
| 79 DVLOG(1) << "WebSocketDispatcher::OnReceivedData(" |
| 80 << channel_id << ", " |
| 81 << fin << ", " |
| 82 << type << ", " |
| 83 << "(data size = " << data.size() << "))"; |
| 84 WebSocketBridge* bridge = GetBridge(channel_id); |
| 85 if (!bridge) return; |
| 86 WebSocketHandle::MessageType type_to_pass = |
| 87 WebSocketHandle::MessageTypeContinuation; |
| 88 switch (type) { |
| 89 case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION: |
| 90 type_to_pass = WebSocketHandle::MessageTypeContinuation; |
| 91 break; |
| 92 case WEB_SOCKET_MESSAGE_TYPE_TEXT: |
| 93 type_to_pass = WebSocketHandle::MessageTypeText; |
| 94 break; |
| 95 case WEB_SOCKET_MESSAGE_TYPE_BINARY: |
| 96 type_to_pass = WebSocketHandle::MessageTypeBinary; |
| 97 break; |
| 98 } |
| 99 bridge->DidReceiveData(fin, type_to_pass, data.data(), data.size()); |
| 100 } |
| 101 |
| 102 void WebSocketDispatcher::OnReceivedFlowControl(int channel_id, int64 quota) { |
| 103 DVLOG(1) << "WebSocketDispatcher::OnReceivedFlowControl(" |
| 104 << channel_id << ", " |
| 105 << quota << ")"; |
| 106 WebSocketBridge* bridge = GetBridge(channel_id); |
| 107 if (!bridge) return; |
| 108 bridge->DidReceiveFlowControl(quota); |
| 109 } |
| 110 |
| 111 void WebSocketDispatcher::OnClosed(int channel_id, |
| 112 unsigned short code, |
| 113 const std::string& reason) { |
| 114 DVLOG(1) << "WebSocketDispatcher::OnClosed(" |
| 115 << channel_id << ", " |
| 116 << code << ", " |
| 117 << reason << ")"; |
| 118 WebSocketBridge* bridge = GetBridge(channel_id); |
| 119 if (!bridge) return; |
| 120 WebString reason_to_pass = WebString::fromUTF8(reason); |
| 121 bridge->DidClose(code, reason_to_pass); |
| 122 } |
| 123 |
| 124 WebSocketBridge* WebSocketDispatcher::GetBridge(int channel_id) { |
| 125 std::map<int, WebSocketBridge*>::iterator i = bridges_.find(channel_id); |
| 126 if (i == bridges_.end()) { |
| 127 DVLOG(1) << "No bridge for channel_id=" << channel_id; |
| 128 return NULL; |
| 129 } |
| 130 return i->second; |
| 131 } |
| 132 |
| 133 } // namespace content |
OLD | NEW |