OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/websockethandle_impl.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <string.h> |
| 9 |
| 10 #include <string> |
| 11 #include <utility> |
| 12 |
| 13 #include "base/logging.h" |
| 14 #include "base/strings/string_util.h" |
| 15 #include "third_party/WebKit/public/platform/InterfaceProvider.h" |
| 16 #include "third_party/WebKit/public/platform/Platform.h" |
| 17 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
| 18 #include "third_party/WebKit/public/platform/WebString.h" |
| 19 #include "third_party/WebKit/public/platform/WebURL.h" |
| 20 #include "third_party/WebKit/public/platform/WebVector.h" |
| 21 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandle.
h" |
| 22 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandleC
lient.h" |
| 23 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandsha
keRequestInfo.h" |
| 24 #include "third_party/WebKit/public/platform/modules/websockets/WebSocketHandsha
keResponseInfo.h" |
| 25 #include "url/gurl.h" |
| 26 #include "url/origin.h" |
| 27 |
| 28 using blink::WebSecurityOrigin; |
| 29 using blink::WebSocketHandle; |
| 30 using blink::WebSocketHandleClient; |
| 31 using blink::WebString; |
| 32 using blink::WebURL; |
| 33 using blink::WebVector; |
| 34 |
| 35 namespace content { |
| 36 namespace { |
| 37 |
| 38 const uint16_t kAbnormalShutdownOpCode = 1006; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 WebSocketHandleImpl::WebSocketHandleImpl( |
| 43 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 44 : client_(nullptr), |
| 45 client_binding_(this), |
| 46 task_runner_(std::move(task_runner)), |
| 47 did_initialize_(false) { |
| 48 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 49 << " created"; |
| 50 } |
| 51 |
| 52 void WebSocketHandleImpl::Initialize( |
| 53 blink::InterfaceProvider* interface_provider) { |
| 54 DCHECK(!websocket_); |
| 55 DCHECK(!did_initialize_); |
| 56 |
| 57 interface_provider->getInterface(mojo::GetProxy(&websocket_)); |
| 58 |
| 59 websocket_.set_connection_error_handler( |
| 60 base::Bind(&WebSocketHandleImpl::OnConnectionError, |
| 61 base::Unretained(this))); |
| 62 did_initialize_ = true; |
| 63 } |
| 64 |
| 65 void WebSocketHandleImpl::connect(const WebURL& url, |
| 66 const WebVector<WebString>& protocols, |
| 67 const WebSecurityOrigin& origin, |
| 68 const WebURL& first_party_for_cookies, |
| 69 const WebString& user_agent_override, |
| 70 WebSocketHandleClient* client) { |
| 71 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 72 << " Connect(" << url.string().utf8() << ", " |
| 73 << origin.toString().utf8() << ")"; |
| 74 |
| 75 // It is insufficient to test if websocket_ is non-null as Disconnect() sets |
| 76 // websocket_ to null. |
| 77 if (!did_initialize_) |
| 78 Initialize(blink::Platform::current()->interfaceProvider()); |
| 79 |
| 80 DCHECK(websocket_); |
| 81 |
| 82 DCHECK(!client_); |
| 83 DCHECK(client); |
| 84 client_ = client; |
| 85 |
| 86 mojo::Array<mojo::String> protocols_to_pass(protocols.size()); |
| 87 for (size_t i = 0; i < protocols.size(); ++i) |
| 88 protocols_to_pass[i] = protocols[i].utf8(); |
| 89 |
| 90 websocket_->AddChannelRequest( |
| 91 url, |
| 92 std::move(protocols_to_pass), |
| 93 origin, |
| 94 first_party_for_cookies, |
| 95 user_agent_override.latin1(), |
| 96 client_binding_.CreateInterfacePtrAndBind(task_runner_)); |
| 97 } |
| 98 |
| 99 void WebSocketHandleImpl::send(bool fin, |
| 100 WebSocketHandle::MessageType type, |
| 101 const char* data, |
| 102 size_t size) { |
| 103 DCHECK(websocket_); |
| 104 |
| 105 mojom::WebSocketMessageType type_to_pass; |
| 106 switch (type) { |
| 107 case WebSocketHandle::MessageTypeContinuation: |
| 108 type_to_pass = mojom::WebSocketMessageType::CONTINUATION; |
| 109 break; |
| 110 case WebSocketHandle::MessageTypeText: |
| 111 type_to_pass = mojom::WebSocketMessageType::TEXT; |
| 112 break; |
| 113 case WebSocketHandle::MessageTypeBinary: |
| 114 type_to_pass = mojom::WebSocketMessageType::BINARY; |
| 115 break; |
| 116 default: |
| 117 NOTREACHED(); |
| 118 return; |
| 119 } |
| 120 |
| 121 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 122 << " Send(" << fin << ", " << type_to_pass << ", " |
| 123 << "(data size = " << size << "))"; |
| 124 |
| 125 mojo::Array<uint8_t> data_to_pass(size); |
| 126 std::copy(data, data + size, data_to_pass.begin()); |
| 127 |
| 128 websocket_->SendFrame(fin, type_to_pass, std::move(data_to_pass)); |
| 129 } |
| 130 |
| 131 void WebSocketHandleImpl::flowControl(int64_t quota) { |
| 132 DCHECK(websocket_); |
| 133 |
| 134 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 135 << " FlowControl(" << quota << ")"; |
| 136 |
| 137 websocket_->SendFlowControl(quota); |
| 138 } |
| 139 |
| 140 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { |
| 141 DCHECK(websocket_); |
| 142 |
| 143 std::string reason_to_pass = reason.utf8(); |
| 144 |
| 145 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 146 << " Close(" << code << ", " << reason_to_pass << ")"; |
| 147 |
| 148 websocket_->StartClosingHandshake(code, reason_to_pass); |
| 149 } |
| 150 |
| 151 WebSocketHandleImpl::~WebSocketHandleImpl() { |
| 152 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 153 << " deleted"; |
| 154 |
| 155 if (websocket_) |
| 156 websocket_->StartClosingHandshake(kAbnormalShutdownOpCode, std::string()); |
| 157 } |
| 158 |
| 159 void WebSocketHandleImpl::Disconnect() { |
| 160 websocket_.reset(); |
| 161 client_ = nullptr; |
| 162 } |
| 163 |
| 164 void WebSocketHandleImpl::OnConnectionError() { |
| 165 // Our connection to the WebSocket was dropped. This could be due to |
| 166 // exceeding the maximum number of concurrent websockets from this process. |
| 167 |
| 168 // TODO(darin): This error message is overly specific. We don't know for sure |
| 169 // that this is the only reason we'd get here. This should be more generic or |
| 170 // we should figure out how to make it more specific. |
| 171 OnFailChannel("Error in connection establishment: " |
| 172 "net::ERR_INSUFFICIENT_RESOURCES"); |
| 173 } |
| 174 |
| 175 void WebSocketHandleImpl::OnFailChannel(const mojo::String& message) { |
| 176 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 177 << " OnFailChannel(" << message << ")"; |
| 178 |
| 179 WebSocketHandleClient* client = client_; |
| 180 Disconnect(); |
| 181 if (!client) |
| 182 return; |
| 183 |
| 184 client->didFail(this, WebString::fromUTF8(message)); |
| 185 // |this| can be deleted here. |
| 186 } |
| 187 |
| 188 void WebSocketHandleImpl::OnStartOpeningHandshake( |
| 189 mojom::WebSocketHandshakeRequestPtr request) { |
| 190 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 191 << " OnStartOpeningHandshake(" << request->url << ")"; |
| 192 // All strings are already encoded to ASCII in the browser. |
| 193 blink::WebSocketHandshakeRequestInfo request_to_pass; |
| 194 request_to_pass.setURL(WebURL(request->url)); |
| 195 for (size_t i = 0; i < request->headers.size(); ++i) { |
| 196 const mojom::HttpHeaderPtr& header = request->headers[i]; |
| 197 request_to_pass.addHeaderField(WebString::fromLatin1(header->name), |
| 198 WebString::fromLatin1(header->value)); |
| 199 } |
| 200 request_to_pass.setHeadersText(WebString::fromLatin1(request->headers_text)); |
| 201 client_->didStartOpeningHandshake(this, request_to_pass); |
| 202 } |
| 203 |
| 204 void WebSocketHandleImpl::OnFinishOpeningHandshake( |
| 205 mojom::WebSocketHandshakeResponsePtr response) { |
| 206 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 207 << " OnFinishOpeningHandshake(" << response->url << ")"; |
| 208 |
| 209 // All strings are already encoded to ASCII in the browser. |
| 210 blink::WebSocketHandshakeResponseInfo response_to_pass; |
| 211 response_to_pass.setStatusCode(response->status_code); |
| 212 response_to_pass.setStatusText(WebString::fromLatin1(response->status_text)); |
| 213 for (size_t i = 0; i < response->headers.size(); ++i) { |
| 214 const mojom::HttpHeaderPtr& header = response->headers[i]; |
| 215 response_to_pass.addHeaderField(WebString::fromLatin1(header->name), |
| 216 WebString::fromLatin1(header->value)); |
| 217 } |
| 218 response_to_pass.setHeadersText( |
| 219 WebString::fromLatin1(response->headers_text)); |
| 220 client_->didFinishOpeningHandshake(this, response_to_pass); |
| 221 } |
| 222 |
| 223 void WebSocketHandleImpl::OnAddChannelResponse( |
| 224 const mojo::String& protocol, |
| 225 const mojo::String& extensions) { |
| 226 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 227 << " OnAddChannelResponse(" |
| 228 << protocol << ", " << extensions << ")"; |
| 229 |
| 230 if (!client_) |
| 231 return; |
| 232 |
| 233 client_->didConnect(this, |
| 234 WebString::fromUTF8(protocol), |
| 235 WebString::fromUTF8(extensions)); |
| 236 // |this| can be deleted here. |
| 237 } |
| 238 |
| 239 void WebSocketHandleImpl::OnDataFrame( |
| 240 bool fin, mojom::WebSocketMessageType type, |
| 241 mojo::Array<uint8_t> data) { |
| 242 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 243 << " OnDataFrame(" << fin << ", " << type << ", " |
| 244 << "(data size = " << data.size() << "))"; |
| 245 if (!client_) |
| 246 return; |
| 247 |
| 248 WebSocketHandle::MessageType type_to_pass = |
| 249 WebSocketHandle::MessageTypeContinuation; |
| 250 switch (type) { |
| 251 case mojom::WebSocketMessageType::CONTINUATION: |
| 252 type_to_pass = WebSocketHandle::MessageTypeContinuation; |
| 253 break; |
| 254 case mojom::WebSocketMessageType::TEXT: |
| 255 type_to_pass = WebSocketHandle::MessageTypeText; |
| 256 break; |
| 257 case mojom::WebSocketMessageType::BINARY: |
| 258 type_to_pass = WebSocketHandle::MessageTypeBinary; |
| 259 break; |
| 260 } |
| 261 const char* data_to_pass = |
| 262 reinterpret_cast<const char*>(data.empty() ? nullptr : &data[0]); |
| 263 client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size()); |
| 264 // |this| can be deleted here. |
| 265 } |
| 266 |
| 267 void WebSocketHandleImpl::OnFlowControl(int64_t quota) { |
| 268 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 269 << " OnFlowControl(" << quota << ")"; |
| 270 if (!client_) |
| 271 return; |
| 272 |
| 273 client_->didReceiveFlowControl(this, quota); |
| 274 // |this| can be deleted here. |
| 275 } |
| 276 |
| 277 void WebSocketHandleImpl::OnDropChannel(bool was_clean, uint16_t code, |
| 278 const mojo::String& reason) { |
| 279 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 280 << " OnDropChannel(" << was_clean << ", " << code << ", " |
| 281 << reason << ")"; |
| 282 |
| 283 WebSocketHandleClient* client = client_; |
| 284 Disconnect(); |
| 285 if (!client) |
| 286 return; |
| 287 |
| 288 client->didClose(this, was_clean, code, WebString::fromUTF8(reason)); |
| 289 // |this| can be deleted here. |
| 290 } |
| 291 |
| 292 void WebSocketHandleImpl::OnClosingHandshake() { |
| 293 DVLOG(1) << "WebSocketHandleImpl @" << reinterpret_cast<void*>(this) |
| 294 << " OnClosingHandshake()"; |
| 295 if (!client_) |
| 296 return; |
| 297 |
| 298 client_->didStartClosingHandshake(this); |
| 299 // |this| can be deleted here. |
| 300 } |
| 301 |
| 302 } // namespace content |
OLD | NEW |