1/*
2 * Copyright (C) 2009 Torch Mobile, Inc. 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 library is distributed in the hope that i will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *  Library General Public License for more details.
17 *
18 *  You should have received a copy of the GNU Library General Public License
19 *  along with this library; see the file COPYING.LIB.  If not, write to
20 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 *  Boston, MA 02110-1301, USA.
22 */
23
24#include "config.h"
25#include "DatabaseThread.h"
26
27#include "Database.h"
28#include "DatabaseTask.h"
29
30namespace WebCore {
31
32DatabaseThread::DatabaseThread()
33: m_timer(this, &DatabaseThread::timerFired)
34{
35}
36
37DatabaseThread::~DatabaseThread()
38{
39}
40
41void DatabaseThread::requestTermination()
42{
43    m_queue.clear();
44}
45
46bool DatabaseThread::terminationRequested() const
47{
48    return m_queue.isEmpty();
49}
50
51void DatabaseThread::timerFired(Timer<DatabaseThread>*)
52{
53    if (!m_queue.isEmpty()) {
54        RefPtr<DatabaseTask> task = m_queue.first();
55        task->performTask();
56        m_queue.removeFirst();
57        if (!m_queue.isEmpty())
58            m_timer.startOneShot(0);
59    }
60}
61
62void DatabaseThread::scheduleTask(PassRefPtr<DatabaseTask> task)
63{
64    m_queue.append(task);
65    if (!m_timer.isActive())
66        m_timer.startOneShot(0);
67}
68
69void DatabaseThread::scheduleImmediateTask(PassRefPtr<DatabaseTask> task)
70{
71    task->performTask();
72}
73
74void DatabaseThread::unscheduleDatabaseTasks(Database* database)
75{
76    Deque<RefPtr<DatabaseTask> > reservedTasks;
77    for (Deque<RefPtr<DatabaseTask> >::const_iterator i = m_queue.begin(); i != m_queue.end(); ++i) {
78        if ((*i)->database() != database)
79            reservedTasks.append(*i);
80    }
81
82    m_queue.swap(reservedTasks);
83}
84
85void DatabaseThread::recordDatabaseOpen(Database* database)
86{
87    notImplemented();
88}
89
90void DatabaseThread::recordDatabaseClosed(Database* database)
91{
92    notImplemented();
93}
94
95} // namespace WebCore
96