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