OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "components/html_viewer/web_socket_handle_impl.h" | |
6 | |
7 #include <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/macros.h" | |
12 #include "components/html_viewer/blink_basic_type_converters.h" | |
13 #include "mojo/public/cpp/bindings/binding.h" | |
14 #include "mojo/services/network/public/cpp/web_socket_read_queue.h" | |
15 #include "mojo/services/network/public/cpp/web_socket_write_queue.h" | |
16 #include "mojo/services/network/public/interfaces/web_socket_factory.mojom.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/WebSocketHandleC
lient.h" | |
22 | |
23 using blink::WebSecurityOrigin; | |
24 using blink::WebSocketHandle; | |
25 using blink::WebSocketHandleClient; | |
26 using blink::WebString; | |
27 using blink::WebURL; | |
28 using blink::WebVector; | |
29 | |
30 using mojo::ConvertTo; | |
31 using mojo::String; | |
32 using mojo::WebSocket; | |
33 using mojo::WebSocketReadQueue; | |
34 | |
35 namespace mojo { | |
36 | |
37 template<> | |
38 struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> { | |
39 static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) { | |
40 DCHECK(type == WebSocketHandle::MessageTypeContinuation || | |
41 type == WebSocketHandle::MessageTypeText || | |
42 type == WebSocketHandle::MessageTypeBinary); | |
43 typedef WebSocket::MessageType MessageType; | |
44 static_assert( | |
45 static_cast<MessageType>(WebSocketHandle::MessageTypeContinuation) == | |
46 WebSocket::MessageType::CONTINUATION, | |
47 "WebSocket and WebSocketHandle enum values must match"); | |
48 static_assert(static_cast<MessageType>(WebSocketHandle::MessageTypeText) == | |
49 WebSocket::MessageType::TEXT, | |
50 "WebSocket and WebSocketHandle enum values must match"); | |
51 static_assert( | |
52 static_cast<MessageType>(WebSocketHandle::MessageTypeBinary) == | |
53 WebSocket::MessageType::BINARY, | |
54 "WebSocket and WebSocketHandle enum values must match"); | |
55 return static_cast<WebSocket::MessageType>(type); | |
56 } | |
57 }; | |
58 | |
59 template<> | |
60 struct TypeConverter<WebSocketHandle::MessageType, WebSocket::MessageType> { | |
61 static WebSocketHandle::MessageType Convert(WebSocket::MessageType type) { | |
62 DCHECK(type == WebSocket::MessageType::CONTINUATION || | |
63 type == WebSocket::MessageType::TEXT || | |
64 type == WebSocket::MessageType::BINARY); | |
65 return static_cast<WebSocketHandle::MessageType>(type); | |
66 } | |
67 }; | |
68 | |
69 } // namespace mojo | |
70 | |
71 namespace html_viewer { | |
72 | |
73 // This class forms a bridge from the mojo WebSocketClient interface and the | |
74 // Blink WebSocketHandleClient interface. | |
75 class WebSocketClientImpl : public mojo::WebSocketClient { | |
76 public: | |
77 WebSocketClientImpl(WebSocketHandleImpl* handle, | |
78 blink::WebSocketHandleClient* client, | |
79 mojo::InterfaceRequest<mojo::WebSocketClient> request) | |
80 : handle_(handle), client_(client), binding_(this, std::move(request)) {} | |
81 ~WebSocketClientImpl() override {} | |
82 | |
83 private: | |
84 // WebSocketClient methods: | |
85 void DidConnect(const String& selected_subprotocol, | |
86 const String& extensions, | |
87 mojo::ScopedDataPipeConsumerHandle receive_stream) override { | |
88 blink::WebSocketHandleClient* client = client_; | |
89 WebSocketHandleImpl* handle = handle_; | |
90 receive_stream_ = std::move(receive_stream); | |
91 read_queue_.reset(new WebSocketReadQueue(receive_stream_.get())); | |
92 client->didConnect(handle, | |
93 selected_subprotocol.To<WebString>(), | |
94 extensions.To<WebString>()); | |
95 // |handle| can be deleted here. | |
96 } | |
97 | |
98 void DidReceiveData(bool fin, | |
99 WebSocket::MessageType type, | |
100 uint32_t num_bytes) override { | |
101 read_queue_->Read(num_bytes, | |
102 base::Bind(&WebSocketClientImpl::DidReadFromReceiveStream, | |
103 base::Unretained(this), | |
104 fin, type, num_bytes)); | |
105 } | |
106 | |
107 void DidReceiveFlowControl(int64_t quota) override { | |
108 client_->didReceiveFlowControl(handle_, quota); | |
109 // |handle| can be deleted here. | |
110 } | |
111 | |
112 void DidFail(const String& message) override { | |
113 blink::WebSocketHandleClient* client = client_; | |
114 WebSocketHandleImpl* handle = handle_; | |
115 handle->Disconnect(); // deletes |this| | |
116 client->didFail(handle, message.To<WebString>()); | |
117 // |handle| can be deleted here. | |
118 } | |
119 | |
120 void DidClose(bool was_clean, uint16_t code, const String& reason) override { | |
121 blink::WebSocketHandleClient* client = client_; | |
122 WebSocketHandleImpl* handle = handle_; | |
123 handle->Disconnect(); // deletes |this| | |
124 client->didClose(handle, was_clean, code, reason.To<WebString>()); | |
125 // |handle| can be deleted here. | |
126 } | |
127 | |
128 void DidReadFromReceiveStream(bool fin, | |
129 WebSocket::MessageType type, | |
130 uint32_t num_bytes, | |
131 const char* data) { | |
132 client_->didReceiveData(handle_, | |
133 fin, | |
134 ConvertTo<WebSocketHandle::MessageType>(type), | |
135 data, | |
136 num_bytes); | |
137 // |handle_| can be deleted here. | |
138 } | |
139 | |
140 // |handle_| owns this object, so it is guaranteed to outlive us. | |
141 WebSocketHandleImpl* handle_; | |
142 blink::WebSocketHandleClient* client_; | |
143 mojo::ScopedDataPipeConsumerHandle receive_stream_; | |
144 scoped_ptr<WebSocketReadQueue> read_queue_; | |
145 mojo::Binding<mojo::WebSocketClient> binding_; | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl); | |
148 }; | |
149 | |
150 WebSocketHandleImpl::WebSocketHandleImpl(mojo::WebSocketFactory* factory) | |
151 : did_close_(false) { | |
152 factory->CreateWebSocket(GetProxy(&web_socket_)); | |
153 } | |
154 | |
155 WebSocketHandleImpl::~WebSocketHandleImpl() { | |
156 if (!did_close_) { | |
157 // The connection is abruptly disconnected by the renderer without | |
158 // closing handshake. | |
159 web_socket_->Close(WebSocket::kAbnormalCloseCode, String()); | |
160 } | |
161 } | |
162 | |
163 void WebSocketHandleImpl::connect(const WebURL& url, | |
164 const WebVector<WebString>& protocols, | |
165 const WebSecurityOrigin& origin, | |
166 WebSocketHandleClient* client) { | |
167 // TODO(mpcomplete): Is this the right ownership model? Or should mojo own | |
168 // |client_|? | |
169 mojo::WebSocketClientPtr client_ptr; | |
170 mojo::MessagePipe pipe; | |
171 client_ptr.Bind(mojo::InterfacePtrInfo<mojo::WebSocketClient>( | |
172 std::move(pipe.handle0), 0u)); | |
173 mojo::InterfaceRequest<mojo::WebSocketClient> request; | |
174 request.Bind(std::move(pipe.handle1)); | |
175 client_.reset(new WebSocketClientImpl(this, client, std::move(request))); | |
176 | |
177 mojo::DataPipe data_pipe; | |
178 send_stream_ = std::move(data_pipe.producer_handle); | |
179 write_queue_.reset(new mojo::WebSocketWriteQueue(send_stream_.get())); | |
180 web_socket_->Connect( | |
181 url.string().utf8(), mojo::Array<String>::From(protocols), | |
182 origin.toString().utf8(), std::move(data_pipe.consumer_handle), | |
183 std::move(client_ptr)); | |
184 } | |
185 | |
186 void WebSocketHandleImpl::send(bool fin, | |
187 WebSocketHandle::MessageType type, | |
188 const char* data, | |
189 size_t size) { | |
190 if (!client_) | |
191 return; | |
192 | |
193 uint32_t size32 = static_cast<uint32_t>(size); | |
194 write_queue_->Write( | |
195 data, size32, | |
196 base::Bind(&WebSocketHandleImpl::DidWriteToSendStream, | |
197 base::Unretained(this), | |
198 fin, type, size32)); | |
199 } | |
200 | |
201 void WebSocketHandleImpl::flowControl(int64_t quota) { | |
202 if (!client_) | |
203 return; | |
204 | |
205 web_socket_->FlowControl(quota); | |
206 } | |
207 | |
208 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { | |
209 web_socket_->Close(code, reason.utf8()); | |
210 } | |
211 | |
212 void WebSocketHandleImpl::DidWriteToSendStream( | |
213 bool fin, | |
214 WebSocketHandle::MessageType type, | |
215 uint32_t num_bytes, | |
216 const char* data) { | |
217 web_socket_->Send(fin, ConvertTo<WebSocket::MessageType>(type), num_bytes); | |
218 } | |
219 | |
220 void WebSocketHandleImpl::Disconnect() { | |
221 did_close_ = true; | |
222 client_.reset(); | |
223 } | |
224 | |
225 } // namespace html_viewer | |
OLD | NEW |