| 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/network_service_impl.h" | 5 #include "mojo/services/network/network_service_impl.h" |
| 6 | 6 |
| 7 #include "mojo/public/cpp/application/application_connection.h" | 7 #include "mojo/public/cpp/application/application_connection.h" |
| 8 #include "mojo/services/network/cookie_store_impl.h" | 8 #include "mojo/services/network/cookie_store_impl.h" |
| 9 #include "mojo/services/network/net_adapters.h" |
| 10 #include "mojo/services/network/tcp_bound_socket_impl.h" |
| 9 #include "mojo/services/network/url_loader_impl.h" | 11 #include "mojo/services/network/url_loader_impl.h" |
| 10 #include "mojo/services/network/web_socket_impl.h" | 12 #include "mojo/services/network/web_socket_impl.h" |
| 11 | 13 |
| 12 namespace mojo { | 14 namespace mojo { |
| 13 | 15 |
| 16 namespace { |
| 17 |
| 18 // Allows a Callback<NetworkErrorPtr, NetAddressPtr> to be called by a |
| 19 // Callback<NetworkErrorPtr> when the address is already known. |
| 20 void BoundAddressCallbackAdapter( |
| 21 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback, |
| 22 NetAddressPtr address, |
| 23 NetworkErrorPtr err) { |
| 24 callback.Run(err.Pass(), address.Pass()); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 14 NetworkServiceImpl::NetworkServiceImpl(ApplicationConnection* connection, | 29 NetworkServiceImpl::NetworkServiceImpl(ApplicationConnection* connection, |
| 15 NetworkContext* context) | 30 NetworkContext* context) |
| 16 : context_(context), | 31 : context_(context), |
| 17 origin_(GURL(connection->GetRemoteApplicationURL()).GetOrigin()) { | 32 origin_(GURL(connection->GetRemoteApplicationURL()).GetOrigin()) { |
| 18 } | 33 } |
| 19 | 34 |
| 20 NetworkServiceImpl::~NetworkServiceImpl() { | 35 NetworkServiceImpl::~NetworkServiceImpl() { |
| 21 } | 36 } |
| 22 | 37 |
| 23 void NetworkServiceImpl::CreateURLLoader(InterfaceRequest<URLLoader> loader) { | 38 void NetworkServiceImpl::CreateURLLoader(InterfaceRequest<URLLoader> loader) { |
| 24 // TODO(darin): Plumb origin_. Use for CORS. | 39 // TODO(darin): Plumb origin_. Use for CORS. |
| 25 BindToRequest(new URLLoaderImpl(context_), &loader); | 40 BindToRequest(new URLLoaderImpl(context_), &loader); |
| 26 } | 41 } |
| 27 | 42 |
| 28 void NetworkServiceImpl::GetCookieStore(InterfaceRequest<CookieStore> store) { | 43 void NetworkServiceImpl::GetCookieStore(InterfaceRequest<CookieStore> store) { |
| 29 BindToRequest(new CookieStoreImpl(context_, origin_), &store); | 44 BindToRequest(new CookieStoreImpl(context_, origin_), &store); |
| 30 } | 45 } |
| 31 | 46 |
| 32 void NetworkServiceImpl::CreateWebSocket(InterfaceRequest<WebSocket> socket) { | 47 void NetworkServiceImpl::CreateWebSocket(InterfaceRequest<WebSocket> socket) { |
| 33 BindToRequest(new WebSocketImpl(context_), &socket); | 48 BindToRequest(new WebSocketImpl(context_), &socket); |
| 34 } | 49 } |
| 35 | 50 |
| 36 void NetworkServiceImpl::CreateTCPBoundSocket( | 51 void NetworkServiceImpl::CreateTCPBoundSocket( |
| 37 NetAddressPtr local_address, | 52 NetAddressPtr local_address, |
| 38 InterfaceRequest<TCPBoundSocket> bound_socket, | 53 InterfaceRequest<TCPBoundSocket> bound_socket, |
| 39 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { | 54 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { |
| 40 // TODO(brettw) implement this. | 55 scoped_ptr<TCPBoundSocketImpl> bound(new TCPBoundSocketImpl); |
| 41 callback.Run(NetworkErrorPtr(), NetAddressPtr()); | 56 int net_error = bound->Bind(local_address.Pass()); |
| 57 if (net_error != net::OK) { |
| 58 callback.Run(MakeNetworkError(net_error), NetAddressPtr()); |
| 59 return; |
| 60 } |
| 61 NetAddressPtr resulting_local_address(bound->GetLocalAddress()); |
| 62 BindToRequest(bound.release(), &bound_socket); |
| 63 callback.Run(MakeNetworkError(net::OK), resulting_local_address.Pass()); |
| 42 } | 64 } |
| 43 | 65 |
| 44 void NetworkServiceImpl::CreateTCPClientSocket( | 66 void NetworkServiceImpl::CreateTCPConnectedSocket( |
| 45 NetAddressPtr remote_address, | 67 NetAddressPtr remote_address, |
| 46 ScopedDataPipeConsumerHandle send_stream, | 68 ScopedDataPipeConsumerHandle send_stream, |
| 47 ScopedDataPipeProducerHandle receive_stream, | 69 ScopedDataPipeProducerHandle receive_stream, |
| 48 InterfaceRequest<TCPClientSocket> client_socket, | 70 InterfaceRequest<TCPConnectedSocket> client_socket, |
| 49 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { | 71 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) { |
| 50 // TODO(brettw) implement this. | 72 TCPBoundSocketImpl bound_socket; |
| 51 callback.Run(NetworkErrorPtr(), NetAddressPtr()); | 73 int net_error = bound_socket.Bind(NetAddressPtr()); |
| 74 if (net_error != net::OK) { |
| 75 callback.Run(MakeNetworkError(net_error), NetAddressPtr()); |
| 76 return; |
| 77 } |
| 78 |
| 79 bound_socket.Connect( |
| 80 remote_address.Pass(), send_stream.Pass(), receive_stream.Pass(), |
| 81 client_socket.Pass(), |
| 82 base::Bind(&BoundAddressCallbackAdapter, |
| 83 callback, |
| 84 base::Passed(bound_socket.GetLocalAddress().Pass()))); |
| 52 } | 85 } |
| 53 | 86 |
| 54 } // namespace mojo | 87 } // namespace mojo |
| OLD | NEW |