[GLIB] Use GUniquePtr instead of GOwnPtr
[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/PassOwnPtr.h>
35 #include <wtf/gobject/GUniquePtr.h>
36 #include <wtf/text/CString.h>
37
38 using namespace WebCore;
39
40 namespace WebKit {
41
42 static gboolean connectionCallback(GSocketService* /*service*/, GSocketConnection* connection, GObject* /*sourceObject*/, WebSocketServer* server)
43 {
44 #if !LOG_DISABLED
45     GRefPtr<GSocketAddress> socketAddress = adoptGRef(g_socket_connection_get_remote_address(connection, 0));
46     GUniquePtr<gchar> addressString(g_inet_address_to_string(g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(socketAddress.get()))));
47     LOG(InspectorServer, "New Connection from %s:%d.", addressString.get(), g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(socketAddress.get())));
48 #endif
49
50     OwnPtr<WebSocketServerConnection> webSocketConnection = adoptPtr(new WebSocketServerConnection(server->client(), server));
51     webSocketConnection->setSocketHandle(SocketStreamHandle::create(connection, webSocketConnection.get()));
52     server->didAcceptConnection(webSocketConnection.release());
53
54     return TRUE;
55 }
56
57 void WebSocketServer::platformInitialize()
58 {
59     m_socketService = adoptGRef(g_socket_service_new());
60     g_signal_connect(m_socketService.get(), "incoming", G_CALLBACK(connectionCallback), this);
61     g_socket_service_start(m_socketService.get());
62 }
63
64 bool WebSocketServer::platformListen(const String& bindAddress, unsigned short port)
65 {
66     LOG(InspectorServer, "Listen to address=%s, port=%d.", bindAddress.utf8().data(), port);
67     GRefPtr<GInetAddress> address = adoptGRef(g_inet_address_new_from_string(bindAddress.utf8().data()));
68     GRefPtr<GSocketAddress> socketAddress = adoptGRef(g_inet_socket_address_new(address.get(), port));
69     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);
70 }
71
72 void WebSocketServer::platformClose()
73 {
74     g_socket_service_stop(m_socketService.get());
75 }
76
77 }
78
79 #endif // ENABLE(INSPECTOR_SERVER)