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