Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / services / network / http_server_impl.cc
blobada77f396cd987dd1bcdb6a404ab9c270fc7c854
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
3 // found in the LICENSE file.
5 #include "mojo/services/network/http_server_impl.h"
7 #include "base/logging.h"
8 #include "mojo/services/network/http_connection_impl.h"
9 #include "mojo/services/network/net_adapters.h"
10 #include "mojo/services/network/net_address_type_converters.h"
11 #include "net/base/ip_endpoint.h"
12 #include "net/base/net_errors.h"
13 #include "net/log/net_log.h"
14 #include "net/socket/tcp_server_socket.h"
16 namespace mojo {
18 namespace {
20 const int kBackLog = 10;
22 } // namespace
24 // static
25 void HttpServerImpl::Create(
26 NetAddressPtr local_address,
27 HttpServerDelegatePtr delegate,
28 scoped_ptr<mojo::AppRefCount> app_refcount,
29 const Callback<void(NetworkErrorPtr, NetAddressPtr)>& callback) {
30 HttpServerImpl* http_server = new HttpServerImpl(
31 delegate.Pass(), app_refcount.Pass());
33 int net_error = http_server->Start(local_address.Pass());
34 if (net_error != net::OK) {
35 callback.Run(MakeNetworkError(net_error), nullptr);
36 delete http_server;
37 return;
39 callback.Run(MakeNetworkError(net::OK), http_server->GetLocalAddress());
42 HttpServerImpl::HttpServerImpl(
43 HttpServerDelegatePtr delegate,
44 scoped_ptr<mojo::AppRefCount> app_refcount)
45 : delegate_(delegate.Pass()), app_refcount_(app_refcount.Pass()) {
46 DCHECK(delegate_);
47 delegate_.set_connection_error_handler([this]() { delete this; });
50 HttpServerImpl::~HttpServerImpl() {}
52 int HttpServerImpl::Start(NetAddressPtr local_address) {
53 DCHECK(local_address);
55 scoped_ptr<net::ServerSocket> socket(
56 new net::TCPServerSocket(nullptr, net::NetLog::Source()));
57 int net_result = socket->Listen(local_address.To<net::IPEndPoint>(),
58 kBackLog);
59 if (net_result != net::OK)
60 return net_result;
62 server_.reset(new net::HttpServer(socket.Pass(), this));
64 return net::OK;
67 NetAddressPtr HttpServerImpl::GetLocalAddress() const {
68 if (!server_)
69 return nullptr;
71 net::IPEndPoint address;
72 int net_result = server_->GetLocalAddress(&address);
73 if (net_result != net::OK)
74 return nullptr;
76 return NetAddress::From(address);
79 void HttpServerImpl::OnConnect(int connection_id) {
80 DCHECK(connections_.find(connection_id) == connections_.end());
82 HttpConnectionPtr connection;
83 HttpConnectionDelegatePtr connection_delegate;
84 InterfaceRequest<HttpConnectionDelegate> delegate_request =
85 GetProxy(&connection_delegate);
86 linked_ptr<HttpConnectionImpl> connection_impl(
87 new HttpConnectionImpl(connection_id, this, connection_delegate.Pass(),
88 &connection));
90 connections_[connection_id] = connection_impl;
92 delegate_->OnConnected(connection.Pass(), delegate_request.Pass());
95 void HttpServerImpl::OnHttpRequest(int connection_id,
96 const net::HttpServerRequestInfo& info) {
97 DCHECK(connections_.find(connection_id) != connections_.end());
98 connections_[connection_id]->OnReceivedHttpRequest(info);
101 void HttpServerImpl::OnWebSocketRequest(
102 int connection_id,
103 const net::HttpServerRequestInfo& info) {
104 DCHECK(connections_.find(connection_id) != connections_.end());
105 connections_[connection_id]->OnReceivedWebSocketRequest(info);
108 void HttpServerImpl::OnWebSocketMessage(int connection_id,
109 const std::string& data) {
110 DCHECK(connections_.find(connection_id) != connections_.end());
111 connections_[connection_id]->OnReceivedWebSocketMessage(data);
114 void HttpServerImpl::OnClose(int connection_id) {
115 DCHECK(connections_.find(connection_id) != connections_.end());
116 connections_.erase(connection_id);
119 } // namespace mojo