Chromium Code Reviews| 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_bridge.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "content/child/child_thread.h" | |
| 13 #include "content/child/websocket_dispatcher.h" | |
| 14 #include "content/common/websocket_messages.h" | |
| 15 #include "ipc/ipc_message.h" | |
| 16 #include "ipc/ipc_message_macros.h" | |
| 17 #include "url/gurl.h" | |
| 18 #include "third_party/WebKit/public/platform/WebSocketHandle.h" | |
| 19 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" | |
| 20 #include "third_party/WebKit/public/platform/WebString.h" | |
| 21 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 22 #include "third_party/WebKit/public/platform/WebVector.h" | |
| 23 | |
| 24 using WebKit::WebSocketHandle; | |
| 25 using WebKit::WebSocketHandleClient; | |
| 26 using WebKit::WebString; | |
| 27 using WebKit::WebURL; | |
| 28 using WebKit::WebVector; | |
| 29 | |
| 30 namespace content { | |
| 31 | |
| 32 WebSocketBridge::WebSocketBridge() | |
| 33 : channel_id_(kInvalidChannelId), client_(NULL) {} | |
| 34 | |
| 35 WebSocketBridge::~WebSocketBridge() { | |
| 36 Disconnect(); | |
| 37 } | |
| 38 | |
| 39 void WebSocketBridge::DidConnect(bool fail, | |
| 40 const WebString& selected_protocol, | |
| 41 const WebString& extensions) { | |
| 42 WebSocketHandleClient* client = client_; | |
| 43 if (fail) | |
| 44 Disconnect(); | |
| 45 if (client) | |
| 46 client->didConnect(this, fail, selected_protocol, extensions); | |
| 47 // |this| can be deleted here. | |
| 48 } | |
| 49 | |
| 50 void WebSocketBridge::DidReceiveData( | |
| 51 bool fin, | |
| 52 WebSocketHandle::MessageType type, | |
| 53 const char* data, | |
| 54 size_t size) { | |
| 55 if (client_) | |
| 56 client_->didReceiveData(this, fin, type, data, size); | |
| 57 // |this| can be deleted here. | |
| 58 } | |
| 59 | |
| 60 void WebSocketBridge::DidReceiveFlowControl(int64_t quota) { | |
| 61 if (client_) | |
| 62 client_->didReceiveFlowControl(this, quota); | |
| 63 // |this| can be deleted here. | |
| 64 } | |
| 65 | |
| 66 void WebSocketBridge::DidClose(unsigned short code, | |
| 67 const WebString& reason) { | |
| 68 WebSocketHandleClient* client = client_; | |
| 69 Disconnect(); | |
| 70 if (client) | |
| 71 client->didClose(this, code, reason); | |
| 72 // |this| can be deleted here. | |
| 73 } | |
| 74 | |
| 75 void WebSocketBridge::connect( | |
| 76 const WebURL& url, | |
| 77 const WebVector<WebString>& protocols, | |
| 78 const WebString& origin, | |
| 79 WebSocketHandleClient* client) { | |
| 80 | |
|
piman
2013/09/20 03:39:06
nit: no need for blank line
yhirano
2013/09/20 08:42:28
Done.
| |
| 81 DCHECK_EQ(kInvalidChannelId, channel_id_); | |
| 82 WebSocketDispatcher* dispatcher = | |
| 83 ChildThread::current()->websocket_dispatcher(); | |
| 84 channel_id_ = dispatcher->AddBridge(this); | |
| 85 client_ = client; | |
| 86 | |
| 87 std::vector<std::string> protocols_to_pass; | |
| 88 for (size_t i = 0; i < protocols.size(); ++i) | |
| 89 protocols_to_pass.push_back(protocols[i].utf8()); | |
| 90 GURL origin_to_pass(origin.utf8()); | |
| 91 | |
| 92 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" | |
| 93 << url << ", (" << JoinString(protocols_to_pass, ", ") << "), " | |
| 94 << origin_to_pass << ")"; | |
| 95 | |
| 96 ChildThread::current()->Send( | |
| 97 new WebSocketHostMsg_AddChannelRequest(channel_id_, | |
| 98 url, | |
| 99 protocols_to_pass, | |
| 100 origin_to_pass)); | |
| 101 } | |
| 102 | |
| 103 void WebSocketBridge::send(bool fin, | |
| 104 WebSocketHandle::MessageType type, | |
| 105 const char* data, | |
| 106 size_t size) { | |
| 107 if (channel_id_ == kInvalidChannelId) | |
| 108 return; | |
| 109 | |
| 110 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
| 111 switch (type) { | |
| 112 case WebSocketHandle::MessageTypeContinuation: | |
| 113 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
| 114 break; | |
| 115 case WebSocketHandle::MessageTypeText: | |
| 116 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; | |
| 117 break; | |
| 118 case WebSocketHandle::MessageTypeBinary: | |
| 119 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; | |
| 120 break; | |
| 121 } | |
| 122 | |
| 123 DVLOG(1) << "Bridge #" << channel_id_ << " Send(" | |
| 124 << fin << ", " << type_to_pass << ", " | |
| 125 << "(data size = " << size << "))"; | |
| 126 | |
| 127 ChildThread::current()->Send( | |
| 128 new WebSocketMsg_SendFrame(channel_id_, | |
| 129 fin, | |
| 130 type_to_pass, | |
| 131 std::vector<char>(data, data + size))); | |
| 132 } | |
| 133 | |
| 134 void WebSocketBridge::flowControl(int64_t quota) { | |
| 135 if (channel_id_ == kInvalidChannelId) | |
| 136 return; | |
| 137 | |
| 138 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; | |
| 139 | |
| 140 ChildThread::current()->Send( | |
| 141 new WebSocketMsg_FlowControl(channel_id_, quota)); | |
| 142 } | |
| 143 | |
| 144 void WebSocketBridge::close(unsigned short code, | |
| 145 const WebString& reason) { | |
| 146 if (channel_id_ == kInvalidChannelId) | |
| 147 return; | |
| 148 | |
| 149 std::string reason_to_pass = reason.utf8(); | |
| 150 DVLOG(1) << "Bridge #" << channel_id_ << " Close(" | |
| 151 << code << ", " << reason_to_pass << ")"; | |
| 152 ChildThread::current()->Send( | |
| 153 new WebSocketMsg_DropChannel(channel_id_, code, reason_to_pass)); | |
| 154 } | |
| 155 | |
| 156 void WebSocketBridge::Disconnect() { | |
| 157 if (channel_id_ == kInvalidChannelId) | |
| 158 return; | |
| 159 WebSocketDispatcher* dispatcher = | |
| 160 ChildThread::current()->websocket_dispatcher(); | |
| 161 dispatcher->RemoveBridge(channel_id_); | |
| 162 | |
| 163 channel_id_ = kInvalidChannelId; | |
| 164 client_ = NULL; | |
| 165 } | |
| 166 | |
| 167 } // namespace content | |
| OLD | NEW |