OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/http_server_impl.h" | 5 #include "mojo/services/network/http_server_impl.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "mojo/services/network/http_connection_impl.h" | 10 #include "mojo/services/network/http_connection_impl.h" |
9 #include "mojo/services/network/net_adapters.h" | 11 #include "mojo/services/network/net_adapters.h" |
10 #include "mojo/services/network/net_address_type_converters.h" | 12 #include "mojo/services/network/net_address_type_converters.h" |
11 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
12 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
13 #include "net/log/net_log.h" | 15 #include "net/log/net_log.h" |
14 #include "net/socket/tcp_server_socket.h" | 16 #include "net/socket/tcp_server_socket.h" |
15 | 17 |
16 namespace mojo { | 18 namespace mojo { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 const int kBackLog = 10; | 22 const int kBackLog = 10; |
21 | 23 |
22 } // namespace | 24 } // namespace |
23 | 25 |
24 // static | 26 // static |
25 void HttpServerImpl::Create( | 27 void HttpServerImpl::Create( |
26 NetAddressPtr local_address, | 28 NetAddressPtr local_address, |
27 HttpServerDelegatePtr delegate, | 29 HttpServerDelegatePtr delegate, |
28 scoped_ptr<mojo::AppRefCount> app_refcount, | 30 scoped_ptr<mojo::AppRefCount> app_refcount, |
29 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { | 31 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { |
30 HttpServerImpl* http_server = new HttpServerImpl( | 32 HttpServerImpl* http_server = |
31 delegate.Pass(), app_refcount.Pass()); | 33 new HttpServerImpl(std::move(delegate), std::move(app_refcount)); |
32 | 34 |
33 int net_error = http_server->Start(local_address.Pass()); | 35 int net_error = http_server->Start(std::move(local_address)); |
34 if (net_error != net::OK) { | 36 if (net_error != net::OK) { |
35 callback.Run(MakeNetworkError(net_error), nullptr); | 37 callback.Run(MakeNetworkError(net_error), nullptr); |
36 delete http_server; | 38 delete http_server; |
37 return; | 39 return; |
38 } | 40 } |
39 callback.Run(MakeNetworkError(net::OK), http_server->GetLocalAddress()); | 41 callback.Run(MakeNetworkError(net::OK), http_server->GetLocalAddress()); |
40 } | 42 } |
41 | 43 |
42 HttpServerImpl::HttpServerImpl( | 44 HttpServerImpl::HttpServerImpl(HttpServerDelegatePtr delegate, |
43 HttpServerDelegatePtr delegate, | 45 scoped_ptr<mojo::AppRefCount> app_refcount) |
44 scoped_ptr<mojo::AppRefCount> app_refcount) | 46 : delegate_(std::move(delegate)), app_refcount_(std::move(app_refcount)) { |
45 : delegate_(delegate.Pass()), app_refcount_(app_refcount.Pass()) { | |
46 DCHECK(delegate_); | 47 DCHECK(delegate_); |
47 delegate_.set_connection_error_handler([this]() { delete this; }); | 48 delegate_.set_connection_error_handler([this]() { delete this; }); |
48 } | 49 } |
49 | 50 |
50 HttpServerImpl::~HttpServerImpl() {} | 51 HttpServerImpl::~HttpServerImpl() {} |
51 | 52 |
52 int HttpServerImpl::Start(NetAddressPtr local_address) { | 53 int HttpServerImpl::Start(NetAddressPtr local_address) { |
53 DCHECK(local_address); | 54 DCHECK(local_address); |
54 | 55 |
55 scoped_ptr<net::ServerSocket> socket( | 56 scoped_ptr<net::ServerSocket> socket( |
56 new net::TCPServerSocket(nullptr, net::NetLog::Source())); | 57 new net::TCPServerSocket(nullptr, net::NetLog::Source())); |
57 int net_result = socket->Listen(local_address.To<net::IPEndPoint>(), | 58 int net_result = socket->Listen(local_address.To<net::IPEndPoint>(), |
58 kBackLog); | 59 kBackLog); |
59 if (net_result != net::OK) | 60 if (net_result != net::OK) |
60 return net_result; | 61 return net_result; |
61 | 62 |
62 server_.reset(new net::HttpServer(socket.Pass(), this)); | 63 server_.reset(new net::HttpServer(std::move(socket), this)); |
63 | 64 |
64 return net::OK; | 65 return net::OK; |
65 } | 66 } |
66 | 67 |
67 NetAddressPtr HttpServerImpl::GetLocalAddress() const { | 68 NetAddressPtr HttpServerImpl::GetLocalAddress() const { |
68 if (!server_) | 69 if (!server_) |
69 return nullptr; | 70 return nullptr; |
70 | 71 |
71 net::IPEndPoint address; | 72 net::IPEndPoint address; |
72 int net_result = server_->GetLocalAddress(&address); | 73 int net_result = server_->GetLocalAddress(&address); |
73 if (net_result != net::OK) | 74 if (net_result != net::OK) |
74 return nullptr; | 75 return nullptr; |
75 | 76 |
76 return NetAddress::From(address); | 77 return NetAddress::From(address); |
77 } | 78 } |
78 | 79 |
79 void HttpServerImpl::OnConnect(int connection_id) { | 80 void HttpServerImpl::OnConnect(int connection_id) { |
80 DCHECK(connections_.find(connection_id) == connections_.end()); | 81 DCHECK(connections_.find(connection_id) == connections_.end()); |
81 | 82 |
82 HttpConnectionPtr connection; | 83 HttpConnectionPtr connection; |
83 HttpConnectionDelegatePtr connection_delegate; | 84 HttpConnectionDelegatePtr connection_delegate; |
84 InterfaceRequest<HttpConnectionDelegate> delegate_request = | 85 InterfaceRequest<HttpConnectionDelegate> delegate_request = |
85 GetProxy(&connection_delegate); | 86 GetProxy(&connection_delegate); |
86 linked_ptr<HttpConnectionImpl> connection_impl( | 87 linked_ptr<HttpConnectionImpl> connection_impl(new HttpConnectionImpl( |
87 new HttpConnectionImpl(connection_id, this, connection_delegate.Pass(), | 88 connection_id, this, std::move(connection_delegate), &connection)); |
88 &connection)); | |
89 | 89 |
90 connections_[connection_id] = connection_impl; | 90 connections_[connection_id] = connection_impl; |
91 | 91 |
92 delegate_->OnConnected(connection.Pass(), delegate_request.Pass()); | 92 delegate_->OnConnected(std::move(connection), std::move(delegate_request)); |
93 } | 93 } |
94 | 94 |
95 void HttpServerImpl::OnHttpRequest(int connection_id, | 95 void HttpServerImpl::OnHttpRequest(int connection_id, |
96 const net::HttpServerRequestInfo& info) { | 96 const net::HttpServerRequestInfo& info) { |
97 DCHECK(connections_.find(connection_id) != connections_.end()); | 97 DCHECK(connections_.find(connection_id) != connections_.end()); |
98 connections_[connection_id]->OnReceivedHttpRequest(info); | 98 connections_[connection_id]->OnReceivedHttpRequest(info); |
99 } | 99 } |
100 | 100 |
101 void HttpServerImpl::OnWebSocketRequest( | 101 void HttpServerImpl::OnWebSocketRequest( |
102 int connection_id, | 102 int connection_id, |
103 const net::HttpServerRequestInfo& info) { | 103 const net::HttpServerRequestInfo& info) { |
104 DCHECK(connections_.find(connection_id) != connections_.end()); | 104 DCHECK(connections_.find(connection_id) != connections_.end()); |
105 connections_[connection_id]->OnReceivedWebSocketRequest(info); | 105 connections_[connection_id]->OnReceivedWebSocketRequest(info); |
106 } | 106 } |
107 | 107 |
108 void HttpServerImpl::OnWebSocketMessage(int connection_id, | 108 void HttpServerImpl::OnWebSocketMessage(int connection_id, |
109 const std::string& data) { | 109 const std::string& data) { |
110 DCHECK(connections_.find(connection_id) != connections_.end()); | 110 DCHECK(connections_.find(connection_id) != connections_.end()); |
111 connections_[connection_id]->OnReceivedWebSocketMessage(data); | 111 connections_[connection_id]->OnReceivedWebSocketMessage(data); |
112 } | 112 } |
113 | 113 |
114 void HttpServerImpl::OnClose(int connection_id) { | 114 void HttpServerImpl::OnClose(int connection_id) { |
115 DCHECK(connections_.find(connection_id) != connections_.end()); | 115 DCHECK(connections_.find(connection_id) != connections_.end()); |
116 connections_.erase(connection_id); | 116 connections_.erase(connection_id); |
117 } | 117 } |
118 | 118 |
119 } // namespace mojo | 119 } // namespace mojo |
OLD | NEW |