1 | /* |
2 | * Copyright (C) 2015 Igalia S.L. |
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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "GSocketMonitor.h" |
28 | |
29 | #include <gio/gio.h> |
30 | #include <wtf/glib/RunLoopSourcePriority.h> |
31 | |
32 | namespace IPC { |
33 | |
34 | GSocketMonitor::~GSocketMonitor() |
35 | { |
36 | stop(); |
37 | } |
38 | |
39 | gboolean GSocketMonitor::socketSourceCallback(GSocket*, GIOCondition condition, GSocketMonitor* monitor) |
40 | { |
41 | if (g_cancellable_is_cancelled(monitor->m_cancellable.get())) |
42 | return G_SOURCE_REMOVE; |
43 | return monitor->m_callback(condition); |
44 | } |
45 | |
46 | void GSocketMonitor::start(GSocket* socket, GIOCondition condition, RunLoop& runLoop, Function<gboolean (GIOCondition)>&& callback) |
47 | { |
48 | stop(); |
49 | |
50 | m_cancellable = adoptGRef(g_cancellable_new()); |
51 | m_source = adoptGRef(g_socket_create_source(socket, condition, m_cancellable.get())); |
52 | g_source_set_name(m_source.get(), "[WebKit] Socket monitor" ); |
53 | m_callback = WTFMove(callback); |
54 | g_source_set_callback(m_source.get(), reinterpret_cast<GSourceFunc>(reinterpret_cast<GCallback>(socketSourceCallback)), this, nullptr); |
55 | g_source_set_priority(m_source.get(), RunLoopSourcePriority::RunLoopDispatcher); |
56 | g_source_attach(m_source.get(), runLoop.mainContext()); |
57 | } |
58 | |
59 | void GSocketMonitor::stop() |
60 | { |
61 | if (!m_source) |
62 | return; |
63 | |
64 | g_cancellable_cancel(m_cancellable.get()); |
65 | m_cancellable = nullptr; |
66 | g_source_destroy(m_source.get()); |
67 | m_source = nullptr; |
68 | m_callback = nullptr; |
69 | } |
70 | |
71 | } // namespace IPC |
72 | |