OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/html_viewer/websockethandle_impl.h" | 5 #include "mojo/services/html_viewer/websockethandle_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "mojo/services/html_viewer/blink_basic_type_converters.h" | 12 #include "mojo/services/html_viewer/blink_basic_type_converters.h" |
13 #include "mojo/services/network/public/cpp/web_socket_read_queue.h" | 13 #include "mojo/services/network/public/cpp/web_socket_read_queue.h" |
14 #include "mojo/services/network/public/cpp/web_socket_write_queue.h" | 14 #include "mojo/services/network/public/cpp/web_socket_write_queue.h" |
15 #include "mojo/services/network/public/interfaces/network_service.mojom.h" | 15 #include "mojo/services/network/public/interfaces/network_service.mojom.h" |
16 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 16 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" |
17 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" | 17 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
18 #include "third_party/WebKit/public/platform/WebString.h" | 18 #include "third_party/WebKit/public/platform/WebString.h" |
19 #include "third_party/WebKit/public/platform/WebURL.h" | 19 #include "third_party/WebKit/public/platform/WebURL.h" |
20 #include "third_party/WebKit/public/platform/WebVector.h" | 20 #include "third_party/WebKit/public/platform/WebVector.h" |
21 | 21 |
22 using blink::WebSerializedOrigin; | 22 using blink::WebSerializedOrigin; |
23 using blink::WebSocketHandle; | 23 using blink::WebSocketHandle; |
24 using blink::WebSocketHandleClient; | 24 using blink::WebSocketHandleClient; |
25 using blink::WebString; | 25 using blink::WebString; |
26 using blink::WebURL; | 26 using blink::WebURL; |
27 using blink::WebVector; | 27 using blink::WebVector; |
28 | 28 |
| 29 using mojo::ConvertTo; |
| 30 using mojo::String; |
| 31 using mojo::WebSocket; |
| 32 using mojo::WebSocketReadQueue; |
| 33 |
29 namespace mojo { | 34 namespace mojo { |
30 | 35 |
31 template<> | 36 template<> |
32 struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> { | 37 struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> { |
33 static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) { | 38 static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) { |
34 DCHECK(type == WebSocketHandle::MessageTypeContinuation || | 39 DCHECK(type == WebSocketHandle::MessageTypeContinuation || |
35 type == WebSocketHandle::MessageTypeText || | 40 type == WebSocketHandle::MessageTypeText || |
36 type == WebSocketHandle::MessageTypeBinary); | 41 type == WebSocketHandle::MessageTypeBinary); |
37 typedef WebSocket::MessageType MessageType; | 42 typedef WebSocket::MessageType MessageType; |
38 COMPILE_ASSERT( | 43 COMPILE_ASSERT( |
(...skipping 15 matching lines...) Expand all Loading... |
54 template<> | 59 template<> |
55 struct TypeConverter<WebSocketHandle::MessageType, WebSocket::MessageType> { | 60 struct TypeConverter<WebSocketHandle::MessageType, WebSocket::MessageType> { |
56 static WebSocketHandle::MessageType Convert(WebSocket::MessageType type) { | 61 static WebSocketHandle::MessageType Convert(WebSocket::MessageType type) { |
57 DCHECK(type == WebSocket::MESSAGE_TYPE_CONTINUATION || | 62 DCHECK(type == WebSocket::MESSAGE_TYPE_CONTINUATION || |
58 type == WebSocket::MESSAGE_TYPE_TEXT || | 63 type == WebSocket::MESSAGE_TYPE_TEXT || |
59 type == WebSocket::MESSAGE_TYPE_BINARY); | 64 type == WebSocket::MESSAGE_TYPE_BINARY); |
60 return static_cast<WebSocketHandle::MessageType>(type); | 65 return static_cast<WebSocketHandle::MessageType>(type); |
61 } | 66 } |
62 }; | 67 }; |
63 | 68 |
| 69 } // namespace mojo |
| 70 |
| 71 namespace html_viewer { |
| 72 |
64 // This class forms a bridge from the mojo WebSocketClient interface and the | 73 // This class forms a bridge from the mojo WebSocketClient interface and the |
65 // Blink WebSocketHandleClient interface. | 74 // Blink WebSocketHandleClient interface. |
66 class WebSocketClientImpl : public InterfaceImpl<WebSocketClient> { | 75 class WebSocketClientImpl : public mojo::InterfaceImpl<mojo::WebSocketClient> { |
67 public: | 76 public: |
68 explicit WebSocketClientImpl(WebSocketHandleImpl* handle, | 77 explicit WebSocketClientImpl(WebSocketHandleImpl* handle, |
69 blink::WebSocketHandleClient* client) | 78 blink::WebSocketHandleClient* client) |
70 : handle_(handle), client_(client) {} | 79 : handle_(handle), client_(client) {} |
71 ~WebSocketClientImpl() override {} | 80 ~WebSocketClientImpl() override {} |
72 | 81 |
73 private: | 82 private: |
74 // WebSocketClient methods: | 83 // WebSocketClient methods: |
75 void DidConnect(bool fail, | 84 void DidConnect(bool fail, |
76 const String& selected_subprotocol, | 85 const String& selected_subprotocol, |
77 const String& extensions, | 86 const String& extensions, |
78 ScopedDataPipeConsumerHandle receive_stream) override { | 87 mojo::ScopedDataPipeConsumerHandle receive_stream) override { |
79 blink::WebSocketHandleClient* client = client_; | 88 blink::WebSocketHandleClient* client = client_; |
80 WebSocketHandleImpl* handle = handle_; | 89 WebSocketHandleImpl* handle = handle_; |
81 receive_stream_ = receive_stream.Pass(); | 90 receive_stream_ = receive_stream.Pass(); |
82 read_queue_.reset(new WebSocketReadQueue(receive_stream_.get())); | 91 read_queue_.reset(new WebSocketReadQueue(receive_stream_.get())); |
83 if (fail) | 92 if (fail) |
84 handle->Disconnect(); // deletes |this| | 93 handle->Disconnect(); // deletes |this| |
85 client->didConnect(handle, | 94 client->didConnect(handle, |
86 fail, | 95 fail, |
87 selected_subprotocol.To<WebString>(), | 96 selected_subprotocol.To<WebString>(), |
88 extensions.To<WebString>()); | 97 extensions.To<WebString>()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 fin, | 136 fin, |
128 ConvertTo<WebSocketHandle::MessageType>(type), | 137 ConvertTo<WebSocketHandle::MessageType>(type), |
129 data, | 138 data, |
130 num_bytes); | 139 num_bytes); |
131 // |handle_| can be deleted here. | 140 // |handle_| can be deleted here. |
132 } | 141 } |
133 | 142 |
134 // |handle_| owns this object, so it is guaranteed to outlive us. | 143 // |handle_| owns this object, so it is guaranteed to outlive us. |
135 WebSocketHandleImpl* handle_; | 144 WebSocketHandleImpl* handle_; |
136 blink::WebSocketHandleClient* client_; | 145 blink::WebSocketHandleClient* client_; |
137 ScopedDataPipeConsumerHandle receive_stream_; | 146 mojo::ScopedDataPipeConsumerHandle receive_stream_; |
138 scoped_ptr<WebSocketReadQueue> read_queue_; | 147 scoped_ptr<WebSocketReadQueue> read_queue_; |
139 | 148 |
140 DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl); | 149 DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl); |
141 }; | 150 }; |
142 | 151 |
143 WebSocketHandleImpl::WebSocketHandleImpl(NetworkService* network_service) | 152 WebSocketHandleImpl::WebSocketHandleImpl(mojo::NetworkService* network_service) |
144 : did_close_(false) { | 153 : did_close_(false) { |
145 network_service->CreateWebSocket(GetProxy(&web_socket_)); | 154 network_service->CreateWebSocket(GetProxy(&web_socket_)); |
146 } | 155 } |
147 | 156 |
148 WebSocketHandleImpl::~WebSocketHandleImpl() { | 157 WebSocketHandleImpl::~WebSocketHandleImpl() { |
149 if (!did_close_) { | 158 if (!did_close_) { |
150 // The connection is abruptly disconnected by the renderer without | 159 // The connection is abruptly disconnected by the renderer without |
151 // closing handshake. | 160 // closing handshake. |
152 web_socket_->Close(WebSocket::kAbnormalCloseCode, String()); | 161 web_socket_->Close(WebSocket::kAbnormalCloseCode, String()); |
153 } | 162 } |
154 } | 163 } |
155 | 164 |
156 void WebSocketHandleImpl::connect(const WebURL& url, | 165 void WebSocketHandleImpl::connect(const WebURL& url, |
157 const WebVector<WebString>& protocols, | 166 const WebVector<WebString>& protocols, |
158 const WebSerializedOrigin& origin, | 167 const WebSerializedOrigin& origin, |
159 WebSocketHandleClient* client) { | 168 WebSocketHandleClient* client) { |
160 client_.reset(new WebSocketClientImpl(this, client)); | 169 client_.reset(new WebSocketClientImpl(this, client)); |
161 WebSocketClientPtr client_ptr; | 170 mojo::WebSocketClientPtr client_ptr; |
162 // TODO(mpcomplete): Is this the right ownership model? Or should mojo own | 171 // TODO(mpcomplete): Is this the right ownership model? Or should mojo own |
163 // |client_|? | 172 // |client_|? |
164 WeakBindToProxy(client_.get(), &client_ptr); | 173 WeakBindToProxy(client_.get(), &client_ptr); |
165 | 174 |
166 DataPipe data_pipe; | 175 mojo::DataPipe data_pipe; |
167 send_stream_ = data_pipe.producer_handle.Pass(); | 176 send_stream_ = data_pipe.producer_handle.Pass(); |
168 write_queue_.reset(new WebSocketWriteQueue(send_stream_.get())); | 177 write_queue_.reset(new mojo::WebSocketWriteQueue(send_stream_.get())); |
169 web_socket_->Connect(url.string().utf8(), | 178 web_socket_->Connect(url.string().utf8(), |
170 Array<String>::From(protocols), | 179 mojo::Array<String>::From(protocols), |
171 origin.string().utf8(), | 180 origin.string().utf8(), data_pipe.consumer_handle.Pass(), |
172 data_pipe.consumer_handle.Pass(), | |
173 client_ptr.Pass()); | 181 client_ptr.Pass()); |
174 } | 182 } |
175 | 183 |
176 void WebSocketHandleImpl::send(bool fin, | 184 void WebSocketHandleImpl::send(bool fin, |
177 WebSocketHandle::MessageType type, | 185 WebSocketHandle::MessageType type, |
178 const char* data, | 186 const char* data, |
179 size_t size) { | 187 size_t size) { |
180 if (!client_) | 188 if (!client_) |
181 return; | 189 return; |
182 | 190 |
(...skipping 22 matching lines...) Expand all Loading... |
205 uint32_t num_bytes, | 213 uint32_t num_bytes, |
206 const char* data) { | 214 const char* data) { |
207 web_socket_->Send(fin, ConvertTo<WebSocket::MessageType>(type), num_bytes); | 215 web_socket_->Send(fin, ConvertTo<WebSocket::MessageType>(type), num_bytes); |
208 } | 216 } |
209 | 217 |
210 void WebSocketHandleImpl::Disconnect() { | 218 void WebSocketHandleImpl::Disconnect() { |
211 did_close_ = true; | 219 did_close_ = true; |
212 client_.reset(); | 220 client_.reset(); |
213 } | 221 } |
214 | 222 |
215 } // namespace mojo | 223 } // namespace html_viewer |
OLD | NEW |