initial import
[vuplus_webkit] / Source / WebKit2 / Platform / gtk / RunLoopGtk.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Portions Copyright (c) 2010 Motorola Mobility, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "RunLoop.h"
29
30 #include "WKBase.h"
31 #include "WorkItem.h"
32 #include <glib.h>
33
34 RunLoop::RunLoop()
35 {
36     m_runLoopContext = g_main_context_default();
37     ASSERT(m_runLoopContext);
38     m_runLoopMain = g_main_loop_new(m_runLoopContext, FALSE);
39     ASSERT(m_runLoopMain);
40 }
41
42 RunLoop::~RunLoop()
43 {
44     if (m_runLoopMain) {
45         if (g_main_loop_is_running(m_runLoopMain))
46             g_main_loop_quit(m_runLoopMain);
47         g_main_loop_unref(m_runLoopMain);
48     }
49
50     if (m_runLoopContext)
51         g_main_context_unref(m_runLoopContext);
52 }
53
54 void RunLoop::run()
55 {
56     g_main_loop_run(RunLoop::main()->mainLoop());
57 }
58
59 GMainLoop* RunLoop::mainLoop()
60 {
61     return m_runLoopMain;
62 }
63
64 void RunLoop::stop()
65 {
66     g_main_loop_quit(m_runLoopMain);
67 }
68
69 gboolean RunLoop::queueWork(RunLoop* runLoop)
70 {
71     runLoop->performWork();
72     return FALSE;
73 }
74
75 void RunLoop::wakeUp()
76 {
77     GRefPtr<GSource> source = adoptGRef(g_idle_source_new());
78     g_source_set_priority(source.get(), G_PRIORITY_DEFAULT);
79     g_source_set_callback(source.get(), reinterpret_cast<GSourceFunc>(&RunLoop::queueWork), this, 0);
80     g_source_attach(source.get(), m_runLoopContext);
81
82     g_main_context_wakeup(m_runLoopContext);
83 }
84
85 RunLoop::TimerBase::TimerBase(RunLoop* runLoop)
86     : m_runLoop(runLoop)
87     , m_timerSource(0)
88 {
89 }
90
91 RunLoop::TimerBase::~TimerBase()
92 {
93     stop();
94 }
95
96 void RunLoop::TimerBase::clearTimerSource()
97 {
98     m_timerSource = 0;
99 }
100
101 gboolean RunLoop::TimerBase::timerFiredCallback(RunLoop::TimerBase* timer)
102 {
103     GSource* currentTimerSource = timer->m_timerSource.get();
104     bool isRepeating = timer->isRepeating();
105     // This can change the timerSource by starting a new timer within the callback.
106     timer->fired();
107     if (!isRepeating && currentTimerSource == timer->m_timerSource.get())
108         timer->clearTimerSource();
109     return isRepeating;
110 }
111
112 void RunLoop::TimerBase::start(double fireInterval, bool repeat)
113 {
114     if (m_timerSource)
115         stop();
116
117     m_timerSource = adoptGRef(g_timeout_source_new(static_cast<guint>(fireInterval * 1000)));
118     m_isRepeating = repeat;
119     g_source_set_callback(m_timerSource.get(), reinterpret_cast<GSourceFunc>(&RunLoop::TimerBase::timerFiredCallback), this, 0);
120     g_source_attach(m_timerSource.get(), m_runLoop->m_runLoopContext);
121 }
122
123 void RunLoop::TimerBase::stop()
124 {
125     if (!m_timerSource)
126         return;
127
128     g_source_destroy(m_timerSource.get());
129     clearTimerSource();
130 }
131
132 bool RunLoop::TimerBase::isActive() const
133 {
134     return m_timerSource;
135 }