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 "modules/websockets/WebSocketHandle.h" |
| 6 |
| 7 #include "modules/websockets/WebSocketHandleClient.h" |
| 8 #include "platform/network/WebSocketHandshakeRequest.h" |
| 9 #include "platform/network/WebSocketHandshakeResponse.h" |
| 10 #include "platform/weborigin/KURL.h" |
| 11 #include "platform/weborigin/SecurityOrigin.h" |
| 12 #include "public/platform/InterfaceProvider.h" |
| 13 #include "public/platform/Platform.h" |
| 14 #include "public/platform/WebScheduler.h" |
| 15 #include "wtf/Functional.h" |
| 16 #include "wtf/text/WTFString.h" |
| 17 |
| 18 namespace blink { |
| 19 namespace { |
| 20 |
| 21 const uint16_t kAbnormalShutdownOpCode = 1006; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 WebSocketHandle::WebSocketHandle() |
| 26 : m_client(nullptr) |
| 27 , m_clientBinding(this) |
| 28 { |
| 29 DVLOG(1) << "WebSocketHandle @" << this << " created"; |
| 30 } |
| 31 |
| 32 WebSocketHandle::~WebSocketHandle() |
| 33 { |
| 34 DVLOG(1) << "WebSocketHandle @" << this << " deleted"; |
| 35 |
| 36 if (m_websocket) |
| 37 m_websocket->StartClosingHandshake(kAbnormalShutdownOpCode, emptyString(
)); |
| 38 } |
| 39 |
| 40 void WebSocketHandle::initialize(InterfaceProvider* interfaceProvider) |
| 41 { |
| 42 DVLOG(1) << "WebSocketHandle @" << this << " initialize(...)"; |
| 43 |
| 44 DCHECK(!m_websocket); |
| 45 interfaceProvider->getInterface(mojo::GetProxy(&m_websocket)); |
| 46 |
| 47 m_websocket.set_connection_error_handler( |
| 48 convertToBaseCallback(bind(&WebSocketHandle::onConnectionError, unretain
ed(this)))); |
| 49 } |
| 50 |
| 51 void WebSocketHandle::connect(const KURL& url, const Vector<String>& protocols,
SecurityOrigin* origin, const KURL& firstPartyForCookies, const String& userAgen
tOverride, WebSocketHandleClient* client) |
| 52 { |
| 53 DCHECK(m_websocket); |
| 54 |
| 55 DVLOG(1) << "WebSocketHandle @" << this << " connect(" << url.getString() <<
", " << origin->toString() << ")"; |
| 56 |
| 57 DCHECK(!m_client); |
| 58 DCHECK(client); |
| 59 m_client = client; |
| 60 |
| 61 m_websocket->AddChannelRequest( |
| 62 url, |
| 63 protocols, |
| 64 origin, |
| 65 firstPartyForCookies, |
| 66 userAgentOverride.isNull() ? emptyString() : userAgentOverride, |
| 67 m_clientBinding.CreateInterfacePtrAndBind( |
| 68 Platform::current()->currentThread()->scheduler()->loadingTaskRunner
()->taskRunner())); |
| 69 } |
| 70 |
| 71 void WebSocketHandle::send(bool fin, WebSocketHandle::MessageType type, const ch
ar* data, size_t size) |
| 72 { |
| 73 DCHECK(m_websocket); |
| 74 |
| 75 mojom::blink::WebSocketMessageType typeToPass; |
| 76 switch (type) { |
| 77 case WebSocketHandle::MessageTypeContinuation: |
| 78 typeToPass = mojom::blink::WebSocketMessageType::CONTINUATION; |
| 79 break; |
| 80 case WebSocketHandle::MessageTypeText: |
| 81 typeToPass = mojom::blink::WebSocketMessageType::TEXT; |
| 82 break; |
| 83 case WebSocketHandle::MessageTypeBinary: |
| 84 typeToPass = mojom::blink::WebSocketMessageType::BINARY; |
| 85 break; |
| 86 default: |
| 87 NOTREACHED(); |
| 88 return; |
| 89 } |
| 90 |
| 91 DVLOG(1) << "WebSocketHandle @" << this << " send(" << fin << ", " << typeTo
Pass << ", " << "(data size = " << size << "))"; |
| 92 |
| 93 // TODO(darin): Avoid this copy. |
| 94 Vector<uint8_t> dataToPass(size); |
| 95 std::copy(data, data + size, dataToPass.begin()); |
| 96 |
| 97 m_websocket->SendFrame(fin, typeToPass, dataToPass); |
| 98 } |
| 99 |
| 100 void WebSocketHandle::flowControl(int64_t quota) |
| 101 { |
| 102 DCHECK(m_websocket); |
| 103 |
| 104 DVLOG(1) << "WebSocketHandle @" << this << " flowControl(" << quota << ")"; |
| 105 |
| 106 m_websocket->SendFlowControl(quota); |
| 107 } |
| 108 |
| 109 void WebSocketHandle::close(unsigned short code, const String& reason) |
| 110 { |
| 111 DCHECK(m_websocket); |
| 112 |
| 113 DVLOG(1) << "WebSocketHandle @" << this << " close(" << code << ", " << reas
on << ")"; |
| 114 |
| 115 m_websocket->StartClosingHandshake(code, reason.isNull() ? emptyString() : r
eason); |
| 116 } |
| 117 |
| 118 void WebSocketHandle::disconnect() |
| 119 { |
| 120 m_websocket.reset(); |
| 121 m_client = nullptr; |
| 122 } |
| 123 |
| 124 void WebSocketHandle::onConnectionError() |
| 125 { |
| 126 if (!Platform::current()) { |
| 127 // In the renderer shutdown sequence, mojo channels are destructed and |
| 128 // this function is called. On the other hand, blink objects became |
| 129 // invalid *silently*, which means we must not touch |*client_| any |
| 130 // more. |
| 131 // TODO(yhirano): Remove this code once the shutdown sequence is fixed. |
| 132 disconnect(); |
| 133 return; |
| 134 } |
| 135 |
| 136 // Our connection to the WebSocket was dropped. This could be due to |
| 137 // exceeding the maximum number of concurrent websockets from this process. |
| 138 |
| 139 // TODO(darin): Communicate a more specific error here (see crbug/634502). |
| 140 OnFailChannel( |
| 141 "Error in connection establishment: net:" |
| 142 ":ERR_INSUFFICIENT_RESOURCES"); |
| 143 } |
| 144 |
| 145 void WebSocketHandle::OnFailChannel(const String& message) |
| 146 { |
| 147 DVLOG(1) << "WebSocketHandle @" << this << " OnFailChannel(" << message << "
)"; |
| 148 |
| 149 WebSocketHandleClient* client = m_client; |
| 150 disconnect(); |
| 151 if (!client) |
| 152 return; |
| 153 |
| 154 client->didFail(this, message); |
| 155 // |this| can be deleted here. |
| 156 } |
| 157 |
| 158 void WebSocketHandle::OnStartOpeningHandshake(mojom::blink::WebSocketHandshakeRe
questPtr request) |
| 159 { |
| 160 DVLOG(1) << "WebSocketHandle @" << this << " OnStartOpeningHandshake(" << re
quest->url.getString() << ")"; |
| 161 |
| 162 RefPtr<WebSocketHandshakeRequest> requestToPass = WebSocketHandshakeRequest:
:create(request->url); |
| 163 for (size_t i = 0; i < request->headers.size(); ++i) { |
| 164 const mojom::blink::HttpHeaderPtr& header = request->headers[i]; |
| 165 requestToPass->addHeaderField(AtomicString(header->name), AtomicString(h
eader->value)); |
| 166 } |
| 167 requestToPass->setHeadersText(request->headers_text); |
| 168 m_client->didStartOpeningHandshake(this, requestToPass); |
| 169 } |
| 170 |
| 171 void WebSocketHandle::OnFinishOpeningHandshake(mojom::blink::WebSocketHandshakeR
esponsePtr response) |
| 172 { |
| 173 DVLOG(1) << "WebSocketHandle @" << this << " OnFinishOpeningHandshake(" << r
esponse->url.getString() << ")"; |
| 174 |
| 175 WebSocketHandshakeResponse responseToPass; |
| 176 responseToPass.setStatusCode(response->status_code); |
| 177 responseToPass.setStatusText(response->status_text); |
| 178 for (size_t i = 0; i < response->headers.size(); ++i) { |
| 179 const mojom::blink::HttpHeaderPtr& header = response->headers[i]; |
| 180 responseToPass.addHeaderField(AtomicString(header->name), AtomicString(h
eader->value)); |
| 181 } |
| 182 responseToPass.setHeadersText(response->headers_text); |
| 183 m_client->didFinishOpeningHandshake(this, &responseToPass); |
| 184 } |
| 185 |
| 186 void WebSocketHandle::OnAddChannelResponse(const String& protocol, const String&
extensions) |
| 187 { |
| 188 DVLOG(1) << "WebSocketHandle @" << this << " OnAddChannelResponse(" << proto
col << ", " << extensions << ")"; |
| 189 |
| 190 if (!m_client) |
| 191 return; |
| 192 |
| 193 m_client->didConnect(this, protocol, extensions); |
| 194 // |this| can be deleted here. |
| 195 } |
| 196 |
| 197 void WebSocketHandle::OnDataFrame(bool fin, mojom::blink::WebSocketMessageType t
ype, const Vector<uint8_t>& data) |
| 198 { |
| 199 DVLOG(1) << "WebSocketHandle @" << this << " OnDataFrame(" << fin << ", " <<
type << ", " << "(data size = " << data.size() << "))"; |
| 200 if (!m_client) |
| 201 return; |
| 202 |
| 203 WebSocketHandle::MessageType typeToPass = WebSocketHandle::MessageTypeContin
uation; |
| 204 switch (type) { |
| 205 case mojom::blink::WebSocketMessageType::CONTINUATION: |
| 206 typeToPass = WebSocketHandle::MessageTypeContinuation; |
| 207 break; |
| 208 case mojom::blink::WebSocketMessageType::TEXT: |
| 209 typeToPass = WebSocketHandle::MessageTypeText; |
| 210 break; |
| 211 case mojom::blink::WebSocketMessageType::BINARY: |
| 212 typeToPass = WebSocketHandle::MessageTypeBinary; |
| 213 break; |
| 214 } |
| 215 const char* dataToPass = reinterpret_cast<const char*>(data.isEmpty() ? null
ptr : &data[0]); |
| 216 m_client->didReceiveData(this, fin, typeToPass, dataToPass, data.size()); |
| 217 // |this| can be deleted here. |
| 218 } |
| 219 |
| 220 void WebSocketHandle::OnFlowControl(int64_t quota) |
| 221 { |
| 222 DVLOG(1) << "WebSocketHandle @" << this << " OnFlowControl(" << quota << ")"
; |
| 223 if (!m_client) |
| 224 return; |
| 225 |
| 226 m_client->didReceiveFlowControl(this, quota); |
| 227 // |this| can be deleted here. |
| 228 } |
| 229 |
| 230 void WebSocketHandle::OnDropChannel(bool wasClean, uint16_t code, const String&
reason) |
| 231 { |
| 232 DVLOG(1) << "WebSocketHandle @" << this << " OnDropChannel(" << wasClean <<
", " << code << ", " << reason << ")"; |
| 233 |
| 234 WebSocketHandleClient* client = m_client; |
| 235 disconnect(); |
| 236 if (!client) |
| 237 return; |
| 238 |
| 239 client->didClose(this, wasClean, code, reason); |
| 240 // |this| can be deleted here. |
| 241 } |
| 242 |
| 243 void WebSocketHandle::OnClosingHandshake() |
| 244 { |
| 245 DVLOG(1) << "WebSocketHandle @" << this << " OnClosingHandshake()"; |
| 246 if (!m_client) |
| 247 return; |
| 248 |
| 249 m_client->didStartClosingHandshake(this); |
| 250 // |this| can be deleted here. |
| 251 } |
| 252 |
| 253 } // namespace blink |
OLD | NEW |