| 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/network/web_socket_impl.h" | 5 #include "mojo/services/network/web_socket_impl.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 9 #include "mojo/message_pump/handle_watcher.h" | 11 #include "mojo/message_pump/handle_watcher.h" |
| 10 #include "mojo/services/network/network_context.h" | 12 #include "mojo/services/network/network_context.h" |
| 11 #include "mojo/services/network/public/cpp/web_socket_read_queue.h" | 13 #include "mojo/services/network/public/cpp/web_socket_read_queue.h" |
| 12 #include "mojo/services/network/public/cpp/web_socket_write_queue.h" | 14 #include "mojo/services/network/public/cpp/web_socket_write_queue.h" |
| 13 #include "net/websockets/websocket_channel.h" | 15 #include "net/websockets/websocket_channel.h" |
| 14 #include "net/websockets/websocket_errors.h" | 16 #include "net/websockets/websocket_errors.h" |
| 15 #include "net/websockets/websocket_event_interface.h" | 17 #include "net/websockets/websocket_event_interface.h" |
| 16 #include "net/websockets/websocket_frame.h" // for WebSocketFrameHeader::OpCode | 18 #include "net/websockets/websocket_frame.h" // for WebSocketFrameHeader::OpCode |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 } | 58 } |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 namespace { | 61 namespace { |
| 60 | 62 |
| 61 typedef net::WebSocketEventInterface::ChannelState ChannelState; | 63 typedef net::WebSocketEventInterface::ChannelState ChannelState; |
| 62 | 64 |
| 63 struct WebSocketEventHandler : public net::WebSocketEventInterface { | 65 struct WebSocketEventHandler : public net::WebSocketEventInterface { |
| 64 public: | 66 public: |
| 65 WebSocketEventHandler(WebSocketClientPtr client) | 67 WebSocketEventHandler(WebSocketClientPtr client) |
| 66 : client_(client.Pass()) { | 68 : client_(std::move(client)) {} |
| 67 } | |
| 68 ~WebSocketEventHandler() override {} | 69 ~WebSocketEventHandler() override {} |
| 69 | 70 |
| 70 private: | 71 private: |
| 71 // net::WebSocketEventInterface methods: | 72 // net::WebSocketEventInterface methods: |
| 72 ChannelState OnAddChannelResponse(const std::string& selected_subprotocol, | 73 ChannelState OnAddChannelResponse(const std::string& selected_subprotocol, |
| 73 const std::string& extensions) override; | 74 const std::string& extensions) override; |
| 74 ChannelState OnDataFrame(bool fin, | 75 ChannelState OnDataFrame(bool fin, |
| 75 WebSocketMessageType type, | 76 WebSocketMessageType type, |
| 76 const std::vector<char>& data) override; | 77 const std::vector<char>& data) override; |
| 77 ChannelState OnClosingHandshake() override; | 78 ChannelState OnClosingHandshake() override; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 99 ScopedDataPipeProducerHandle receive_stream_; | 100 ScopedDataPipeProducerHandle receive_stream_; |
| 100 scoped_ptr<WebSocketWriteQueue> write_queue_; | 101 scoped_ptr<WebSocketWriteQueue> write_queue_; |
| 101 | 102 |
| 102 DISALLOW_COPY_AND_ASSIGN(WebSocketEventHandler); | 103 DISALLOW_COPY_AND_ASSIGN(WebSocketEventHandler); |
| 103 }; | 104 }; |
| 104 | 105 |
| 105 ChannelState WebSocketEventHandler::OnAddChannelResponse( | 106 ChannelState WebSocketEventHandler::OnAddChannelResponse( |
| 106 const std::string& selected_protocol, | 107 const std::string& selected_protocol, |
| 107 const std::string& extensions) { | 108 const std::string& extensions) { |
| 108 DataPipe data_pipe; | 109 DataPipe data_pipe; |
| 109 receive_stream_ = data_pipe.producer_handle.Pass(); | 110 receive_stream_ = std::move(data_pipe.producer_handle); |
| 110 write_queue_.reset(new WebSocketWriteQueue(receive_stream_.get())); | 111 write_queue_.reset(new WebSocketWriteQueue(receive_stream_.get())); |
| 111 client_->DidConnect( | 112 client_->DidConnect(selected_protocol, extensions, |
| 112 selected_protocol, extensions, data_pipe.consumer_handle.Pass()); | 113 std::move(data_pipe.consumer_handle)); |
| 113 return WebSocketEventInterface::CHANNEL_ALIVE; | 114 return WebSocketEventInterface::CHANNEL_ALIVE; |
| 114 } | 115 } |
| 115 | 116 |
| 116 ChannelState WebSocketEventHandler::OnDataFrame( | 117 ChannelState WebSocketEventHandler::OnDataFrame( |
| 117 bool fin, | 118 bool fin, |
| 118 net::WebSocketFrameHeader::OpCode type, | 119 net::WebSocketFrameHeader::OpCode type, |
| 119 const std::vector<char>& data) { | 120 const std::vector<char>& data) { |
| 120 uint32_t size = static_cast<uint32_t>(data.size()); | 121 uint32_t size = static_cast<uint32_t>(data.size()); |
| 121 write_queue_->Write( | 122 write_queue_->Write( |
| 122 &data[0], size, | 123 &data[0], size, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 bool fin, | 171 bool fin, |
| 171 net::WebSocketFrameHeader::OpCode type, | 172 net::WebSocketFrameHeader::OpCode type, |
| 172 uint32_t num_bytes, | 173 uint32_t num_bytes, |
| 173 const char* buffer) { | 174 const char* buffer) { |
| 174 client_->DidReceiveData( | 175 client_->DidReceiveData( |
| 175 fin, ConvertTo<WebSocket::MessageType>(type), num_bytes); | 176 fin, ConvertTo<WebSocket::MessageType>(type), num_bytes); |
| 176 } | 177 } |
| 177 | 178 |
| 178 } // namespace mojo | 179 } // namespace mojo |
| 179 | 180 |
| 180 WebSocketImpl::WebSocketImpl( | 181 WebSocketImpl::WebSocketImpl(NetworkContext* context, |
| 181 NetworkContext* context, | 182 scoped_ptr<mojo::AppRefCount> app_refcount, |
| 182 scoped_ptr<mojo::AppRefCount> app_refcount, | 183 InterfaceRequest<WebSocket> request) |
| 183 InterfaceRequest<WebSocket> request) | 184 : context_(context), |
| 184 : context_(context), app_refcount_(app_refcount.Pass()), | 185 app_refcount_(std::move(app_refcount)), |
| 185 binding_(this, request.Pass()) { | 186 binding_(this, std::move(request)) {} |
| 186 } | |
| 187 | 187 |
| 188 WebSocketImpl::~WebSocketImpl() { | 188 WebSocketImpl::~WebSocketImpl() { |
| 189 } | 189 } |
| 190 | 190 |
| 191 void WebSocketImpl::Connect(const String& url, | 191 void WebSocketImpl::Connect(const String& url, |
| 192 Array<String> protocols, | 192 Array<String> protocols, |
| 193 const String& origin, | 193 const String& origin, |
| 194 ScopedDataPipeConsumerHandle send_stream, | 194 ScopedDataPipeConsumerHandle send_stream, |
| 195 WebSocketClientPtr client) { | 195 WebSocketClientPtr client) { |
| 196 DCHECK(!channel_); | 196 DCHECK(!channel_); |
| 197 send_stream_ = send_stream.Pass(); | 197 send_stream_ = std::move(send_stream); |
| 198 read_queue_.reset(new WebSocketReadQueue(send_stream_.get())); | 198 read_queue_.reset(new WebSocketReadQueue(send_stream_.get())); |
| 199 scoped_ptr<net::WebSocketEventInterface> event_interface( | 199 scoped_ptr<net::WebSocketEventInterface> event_interface( |
| 200 new WebSocketEventHandler(client.Pass())); | 200 new WebSocketEventHandler(std::move(client))); |
| 201 channel_.reset(new net::WebSocketChannel(event_interface.Pass(), | 201 channel_.reset(new net::WebSocketChannel(std::move(event_interface), |
| 202 context_->url_request_context())); | 202 context_->url_request_context())); |
| 203 channel_->SendAddChannelRequest(GURL(url.get()), | 203 channel_->SendAddChannelRequest(GURL(url.get()), |
| 204 protocols.To<std::vector<std::string>>(), | 204 protocols.To<std::vector<std::string>>(), |
| 205 url::Origin(GURL(origin.get()))); | 205 url::Origin(GURL(origin.get()))); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void WebSocketImpl::Send(bool fin, | 208 void WebSocketImpl::Send(bool fin, |
| 209 WebSocket::MessageType type, | 209 WebSocket::MessageType type, |
| 210 uint32_t num_bytes) { | 210 uint32_t num_bytes) { |
| 211 DCHECK(channel_); | 211 DCHECK(channel_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 230 uint32_t num_bytes, | 230 uint32_t num_bytes, |
| 231 const char* data) { | 231 const char* data) { |
| 232 std::vector<char> buffer(num_bytes); | 232 std::vector<char> buffer(num_bytes); |
| 233 memcpy(&buffer[0], data, num_bytes); | 233 memcpy(&buffer[0], data, num_bytes); |
| 234 DCHECK(channel_); | 234 DCHECK(channel_); |
| 235 channel_->SendFrame( | 235 channel_->SendFrame( |
| 236 fin, ConvertTo<net::WebSocketFrameHeader::OpCode>(type), buffer); | 236 fin, ConvertTo<net::WebSocketFrameHeader::OpCode>(type), buffer); |
| 237 } | 237 } |
| 238 | 238 |
| 239 } // namespace mojo | 239 } // namespace mojo |
| OLD | NEW |