Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / DatabaseTask.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2013 Apple 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  *
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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "modules/webdatabase/DatabaseTask.h"
31
32 #include "platform/Logging.h"
33 #include "modules/webdatabase/Database.h"
34 #include "modules/webdatabase/DatabaseContext.h"
35 #include "modules/webdatabase/DatabaseThread.h"
36
37 namespace WebCore {
38
39 DatabaseTask::DatabaseTask(DatabaseBackend* database, TaskSynchronizer* synchronizer)
40     : m_database(database)
41     , m_synchronizer(synchronizer)
42 #if !LOG_DISABLED
43     , m_complete(false)
44 #endif
45 {
46 }
47
48 DatabaseTask::~DatabaseTask()
49 {
50 #if !LOG_DISABLED
51     ASSERT(m_complete || !m_synchronizer);
52 #endif
53 }
54
55 void DatabaseTask::run()
56 {
57     // Database tasks are meant to be used only once, so make sure this one hasn't been performed before.
58 #if !LOG_DISABLED
59     ASSERT(!m_complete);
60 #endif
61
62     if (!m_synchronizer && !m_database->databaseContext()->databaseThread()->isDatabaseOpen(m_database.get())) {
63         taskCancelled();
64 #if !LOG_DISABLED
65         m_complete = true;
66 #endif
67         return;
68     }
69
70     WTF_LOG(StorageAPI, "Performing %s %p\n", debugTaskName(), this);
71
72     m_database->resetAuthorizer();
73     doPerformTask();
74
75     if (m_synchronizer)
76         m_synchronizer->taskCompleted();
77
78 #if !LOG_DISABLED
79     m_complete = true;
80 #endif
81 }
82
83 // *** DatabaseOpenTask ***
84 // Opens the database file and verifies the version matches the expected version.
85
86 DatabaseBackend::DatabaseOpenTask::DatabaseOpenTask(DatabaseBackend* database, bool setVersionInNewDatabase, TaskSynchronizer* synchronizer, DatabaseError& error, String& errorMessage, bool& success)
87     : DatabaseTask(database, synchronizer)
88     , m_setVersionInNewDatabase(setVersionInNewDatabase)
89     , m_error(error)
90     , m_errorMessage(errorMessage)
91     , m_success(success)
92 {
93     ASSERT(synchronizer); // A task with output parameters is supposed to be synchronous.
94 }
95
96 void DatabaseBackend::DatabaseOpenTask::doPerformTask()
97 {
98     String errorMessage;
99     m_success = database()->performOpenAndVerify(m_setVersionInNewDatabase, m_error, errorMessage);
100     if (!m_success)
101         m_errorMessage = errorMessage.isolatedCopy();
102 }
103
104 #if !LOG_DISABLED
105 const char* DatabaseBackend::DatabaseOpenTask::debugTaskName() const
106 {
107     return "DatabaseOpenTask";
108 }
109 #endif
110
111 // *** DatabaseCloseTask ***
112 // Closes the database.
113
114 DatabaseBackend::DatabaseCloseTask::DatabaseCloseTask(DatabaseBackend* database, TaskSynchronizer* synchronizer)
115     : DatabaseTask(database, synchronizer)
116 {
117 }
118
119 void DatabaseBackend::DatabaseCloseTask::doPerformTask()
120 {
121     Database::from(database())->close();
122 }
123
124 #if !LOG_DISABLED
125 const char* DatabaseBackend::DatabaseCloseTask::debugTaskName() const
126 {
127     return "DatabaseCloseTask";
128 }
129 #endif
130
131 // *** DatabaseTransactionTask ***
132 // Starts a transaction that will report its results via a callback.
133
134 DatabaseBackend::DatabaseTransactionTask::DatabaseTransactionTask(PassRefPtrWillBeRawPtr<SQLTransactionBackend> transaction)
135     : DatabaseTask(Database::from(transaction->database()), 0)
136     , m_transaction(transaction)
137 {
138 }
139
140 DatabaseBackend::DatabaseTransactionTask::~DatabaseTransactionTask()
141 {
142 }
143
144 void DatabaseBackend::DatabaseTransactionTask::doPerformTask()
145 {
146     m_transaction->performNextStep();
147 }
148
149 void DatabaseBackend::DatabaseTransactionTask::taskCancelled()
150 {
151     // If the task is being destructed without the transaction ever being run,
152     // then we must either have an error or an interruption. Give the
153     // transaction a chance to clean up since it may not have been able to
154     // run to its clean up state.
155
156     // Transaction phase 2 cleanup. See comment on "What happens if a
157     // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
158
159     m_transaction->notifyDatabaseThreadIsShuttingDown();
160 }
161
162 #if !LOG_DISABLED
163 const char* DatabaseBackend::DatabaseTransactionTask::debugTaskName() const
164 {
165     return "DatabaseTransactionTask";
166 }
167 #endif
168
169 // *** DatabaseTableNamesTask ***
170 // Retrieves a list of all tables in the database - for WebInspector support.
171
172 DatabaseBackend::DatabaseTableNamesTask::DatabaseTableNamesTask(DatabaseBackend* database, TaskSynchronizer* synchronizer, Vector<String>& names)
173     : DatabaseTask(database, synchronizer)
174     , m_tableNames(names)
175 {
176     ASSERT(synchronizer); // A task with output parameters is supposed to be synchronous.
177 }
178
179 void DatabaseBackend::DatabaseTableNamesTask::doPerformTask()
180 {
181     m_tableNames = Database::from(database())->performGetTableNames();
182 }
183
184 #if !LOG_DISABLED
185 const char* DatabaseBackend::DatabaseTableNamesTask::debugTaskName() const
186 {
187     return "DatabaseTableNamesTask";
188 }
189 #endif
190
191 } // namespace WebCore