Add WTF::move()
[WebKit-https.git] / Source / WebKit2 / UIProcess / InspectorServer / soup / WebSocketServerSoup.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics Ltd. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26 #if ENABLE(INSPECTOR_SERVER)
27 #include "WebSocketServer.h"
28
29 #include "Logging.h"
30 #include "WebSocketServerConnection.h"
31 #include <WebCore/SocketStreamHandle.h>
32 #include <gio/gio.h>
33 #include <glib.h>
34 #include <wtf/gobject/GUniquePtr.h>
35 #include <wtf/text/CString.h>
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 static gboolean connectionCallback(GSocketService* /*service*/, GSocketConnection* connection, GObject* /*sourceObject*/, WebSocketServer* server)
42 {
43 #if !LOG_DISABLED
44     GRefPtr<GSocketAddress> socketAddress = adoptGRef(g_socket_connection_get_remote_address(connection, 0));
45     GUniquePtr<gchar> addressString(g_inet_address_to_string(g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(socketAddress.get()))));
46     LOG(InspectorServer, "New Connection from %s:%d.", addressString.get(), g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(socketAddress.get())));
47 #endif
48
49     auto webSocketConnection = std::make_unique<WebSocketServerConnection>(server->client(), server);
50     webSocketConnection->setSocketHandle(SocketStreamHandle::create(connection, webSocketConnection.get()));
51     server->didAcceptConnection(WTF::move(webSocketConnection));
52
53     return TRUE;
54 }
55
56 void WebSocketServer::platformInitialize()
57 {
58     m_socketService = adoptGRef(g_socket_service_new());
59     g_signal_connect(m_socketService.get(), "incoming", G_CALLBACK(connectionCallback), this);
60     g_socket_service_start(m_socketService.get());
61 }
62
63 bool WebSocketServer::platformListen(const String& bindAddress, unsigned short port)
64 {
65     LOG(InspectorServer, "Listen to address=%s, port=%d.", bindAddress.utf8().data(), port);
66     GRefPtr<GInetAddress> address = adoptGRef(g_inet_address_new_from_string(bindAddress.utf8().data()));
67     GRefPtr<GSocketAddress> socketAddress = adoptGRef(g_inet_socket_address_new(address.get(), port));
68     return g_socket_listener_add_address(G_SOCKET_LISTENER(m_socketService.get()), socketAddress.get(), G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, 0, 0, 0);
69 }
70
71 void WebSocketServer::platformClose()
72 {
73     g_socket_service_stop(m_socketService.get());
74 }
75
76 }
77
78 #endif // ENABLE(INSPECTOR_SERVER)