2011-04-14 Ilya Tikhonovsky <loislo@chromium.org>
[WebKit-https.git] / Source / WebCore / inspector / InspectorDatabaseAgent.cpp
1 /*
2  * Copyright (C) 2010 Google 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
31 #include "InspectorDatabaseAgent.h"
32
33 #if ENABLE(INSPECTOR) && ENABLE(DATABASE)
34
35 #include "Database.h"
36 #include "ExceptionCode.h"
37 #include "InspectorDatabaseResource.h"
38 #include "InspectorFrontend.h"
39 #include "InspectorValues.h"
40 #include "InstrumentingAgents.h"
41 #include "SQLError.h"
42 #include "SQLStatementCallback.h"
43 #include "SQLStatementErrorCallback.h"
44 #include "SQLResultSetRowList.h"
45 #include "SQLTransaction.h"
46 #include "SQLTransactionCallback.h"
47 #include "SQLTransactionErrorCallback.h"
48 #include "SQLValue.h"
49 #include "VoidCallback.h"
50
51 #include <wtf/Vector.h>
52
53 namespace WebCore {
54
55 class InspectorDatabaseAgent::FrontendProvider : public RefCounted<InspectorDatabaseAgent::FrontendProvider> {
56 public:
57     static PassRefPtr<FrontendProvider> create(InspectorFrontend* inspectorFrontend)
58     {
59         return adoptRef(new FrontendProvider(inspectorFrontend));
60     }
61
62     virtual ~FrontendProvider() { }
63
64     InspectorFrontend::Database* frontend() { return m_inspectorFrontend; }
65     void clearFrontend() { m_inspectorFrontend = 0; }
66 private:
67     FrontendProvider(InspectorFrontend* inspectorFrontend) : m_inspectorFrontend(inspectorFrontend->database()) { }
68     InspectorFrontend::Database* m_inspectorFrontend;
69 };
70
71 namespace {
72
73 int lastTransactionId = 0;
74
75 void reportTransactionFailed(InspectorFrontend::Database* frontend, int transactionId, SQLError* error)
76 {
77     if (!frontend)
78         return;
79     RefPtr<InspectorObject> errorObject = InspectorObject::create();
80     errorObject->setString("message", error->message());
81     errorObject->setNumber("code", error->code());
82     frontend->sqlTransactionFailed(transactionId, errorObject);
83 }
84
85 class StatementCallback : public SQLStatementCallback {
86 public:
87     static PassRefPtr<StatementCallback> create(int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
88     {
89         return adoptRef(new StatementCallback(transactionId, frontendProvider));
90     }
91
92     virtual ~StatementCallback() { }
93
94     virtual bool handleEvent(SQLTransaction*, SQLResultSet* resultSet)
95     {
96         if (!m_frontendProvider->frontend())
97             return true;
98
99         SQLResultSetRowList* rowList = resultSet->rows();
100
101         RefPtr<InspectorArray> columnNames = InspectorArray::create();
102         const Vector<String>& columns = rowList->columnNames();
103         for (size_t i = 0; i < columns.size(); ++i)
104             columnNames->pushString(columns[i]);
105
106         RefPtr<InspectorArray> values = InspectorArray::create();
107         const Vector<SQLValue>& data = rowList->values();
108         for (size_t i = 0; i < data.size(); ++i) {
109             const SQLValue& value = rowList->values()[i];
110             switch (value.type()) {
111                 case SQLValue::StringValue: values->pushString(value.string()); break;
112                 case SQLValue::NumberValue: values->pushNumber(value.number()); break;
113                 case SQLValue::NullValue: values->pushValue(InspectorValue::null()); break;
114             }
115         }
116         m_frontendProvider->frontend()->sqlTransactionSucceeded(m_transactionId, columnNames, values);
117         return true;
118     }
119
120 private:
121     StatementCallback(int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
122         : m_transactionId(transactionId)
123         , m_frontendProvider(frontendProvider) { }
124     int m_transactionId;
125     RefPtr<InspectorDatabaseAgent::FrontendProvider> m_frontendProvider;
126 };
127
128 class StatementErrorCallback : public SQLStatementErrorCallback {
129 public:
130     static PassRefPtr<StatementErrorCallback> create(int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
131     {
132         return adoptRef(new StatementErrorCallback(transactionId, frontendProvider));
133     }
134
135     virtual ~StatementErrorCallback() { }
136
137     virtual bool handleEvent(SQLTransaction*, SQLError* error)
138     {
139         reportTransactionFailed(m_frontendProvider->frontend(), m_transactionId, error);
140         return true;  
141     }
142
143 private:
144     StatementErrorCallback(int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
145         : m_transactionId(transactionId)
146         , m_frontendProvider(frontendProvider) { }
147     int m_transactionId;
148     RefPtr<InspectorDatabaseAgent::FrontendProvider> m_frontendProvider;
149 };
150
151 class TransactionCallback : public SQLTransactionCallback {
152 public:
153     static PassRefPtr<TransactionCallback> create(const String& sqlStatement, int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
154     {
155         return adoptRef(new TransactionCallback(sqlStatement, transactionId, frontendProvider));
156     }
157
158     virtual ~TransactionCallback() { }
159
160     virtual bool handleEvent(SQLTransaction* transaction)
161     {
162         if (!m_frontendProvider->frontend())
163             return true;
164
165         Vector<SQLValue> sqlValues;
166         RefPtr<SQLStatementCallback> callback(StatementCallback::create(m_transactionId, m_frontendProvider));
167         RefPtr<SQLStatementErrorCallback> errorCallback(StatementErrorCallback::create(m_transactionId, m_frontendProvider));
168         ExceptionCode ec = 0;
169         transaction->executeSQL(m_sqlStatement, sqlValues, callback.release(), errorCallback.release(), ec);
170         return true;
171     }
172 private:
173     TransactionCallback(const String& sqlStatement, int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
174         : m_sqlStatement(sqlStatement)
175         , m_transactionId(transactionId)
176         , m_frontendProvider(frontendProvider) { }
177     String m_sqlStatement;
178     int m_transactionId;
179     RefPtr<InspectorDatabaseAgent::FrontendProvider> m_frontendProvider;
180 };
181
182 class TransactionErrorCallback : public SQLTransactionErrorCallback {
183 public:
184     static PassRefPtr<TransactionErrorCallback> create(int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
185     {
186         return adoptRef(new TransactionErrorCallback(transactionId, frontendProvider));
187     }
188
189     virtual ~TransactionErrorCallback() { }
190
191     virtual bool handleEvent(SQLError* error)
192     {
193         reportTransactionFailed(m_frontendProvider->frontend(), m_transactionId, error);
194         return true;
195     }
196 private:
197     TransactionErrorCallback(int transactionId, PassRefPtr<InspectorDatabaseAgent::FrontendProvider> frontendProvider)
198         : m_transactionId(transactionId)
199         , m_frontendProvider(frontendProvider) { }
200     int m_transactionId;
201     RefPtr<InspectorDatabaseAgent::FrontendProvider> m_frontendProvider;
202 };
203
204 class TransactionSuccessCallback : public VoidCallback {
205 public:
206     static PassRefPtr<TransactionSuccessCallback> create()
207     {
208         return adoptRef(new TransactionSuccessCallback());
209     }
210
211     virtual ~TransactionSuccessCallback() { }
212
213     virtual void handleEvent() { }
214
215 private:
216     TransactionSuccessCallback() { }
217 };
218
219 } // namespace
220
221 void InspectorDatabaseAgent::didOpenDatabase(PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
222 {
223     if (InspectorDatabaseResource* resource = findByFileName(database->fileName())) {
224         resource->setDatabase(database);
225         return;
226     }
227
228     RefPtr<InspectorDatabaseResource> resource = InspectorDatabaseResource::create(database, domain, name, version);
229     m_resources.set(resource->id(), resource);
230     // Resources are only bound while visible.
231     if (m_frontendProvider)
232         resource->bind(m_frontendProvider->frontend());
233 }
234
235 void InspectorDatabaseAgent::clearResources()
236 {
237     m_resources.clear();
238 }
239
240 InspectorDatabaseAgent::InspectorDatabaseAgent(InstrumentingAgents* instrumentingAgents)
241     : m_instrumentingAgents(instrumentingAgents)
242 {
243     m_instrumentingAgents->setInspectorDatabaseAgent(this);
244 }
245
246 InspectorDatabaseAgent::~InspectorDatabaseAgent()
247 {
248     m_instrumentingAgents->setInspectorDatabaseAgent(0);
249 }
250
251 void InspectorDatabaseAgent::setFrontend(InspectorFrontend* frontend)
252 {
253     m_frontendProvider = FrontendProvider::create(frontend);
254     DatabaseResourcesMap::iterator databasesEnd = m_resources.end();
255     for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != databasesEnd; ++it)
256         it->second->bind(m_frontendProvider->frontend());
257 }
258
259 void InspectorDatabaseAgent::clearFrontend()
260 {
261     m_frontendProvider->clearFrontend();
262     m_frontendProvider.clear();
263 }
264
265 void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString*, int databaseId, RefPtr<InspectorArray>* names)
266 {
267     Database* database = databaseForId(databaseId);
268     if (database) {
269         Vector<String> tableNames = database->tableNames();
270         unsigned length = tableNames.size();
271         for (unsigned i = 0; i < length; ++i)
272             (*names)->pushString(tableNames[i]);
273     }
274 }
275
276 void InspectorDatabaseAgent::executeSQL(ErrorString*, int databaseId, const String& query, bool* success, int* transactionId)
277 {
278     Database* database = databaseForId(databaseId);
279     if (!database) {
280         *success = false;
281         return;
282     }
283
284     *transactionId = ++lastTransactionId;
285     RefPtr<SQLTransactionCallback> callback(TransactionCallback::create(query, *transactionId, m_frontendProvider));
286     RefPtr<SQLTransactionErrorCallback> errorCallback(TransactionErrorCallback::create(*transactionId, m_frontendProvider));
287     RefPtr<VoidCallback> successCallback(TransactionSuccessCallback::create());
288     database->transaction(callback.release(), errorCallback.release(), successCallback.release());
289     *success = true;
290 }
291
292 int InspectorDatabaseAgent::databaseId(Database* database)
293 {
294     for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
295         if (it->second->database() == database)
296             return it->first;
297     }
298     return 0;
299 }
300
301 InspectorDatabaseResource* InspectorDatabaseAgent::findByFileName(const String& fileName)
302 {
303     for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
304         if (it->second->database()->fileName() == fileName)
305             return it->second.get();
306     }
307     return 0;
308 }
309
310 Database* InspectorDatabaseAgent::databaseForId(int databaseId)
311 {
312     DatabaseResourcesMap::iterator it = m_resources.find(databaseId);
313     if (it == m_resources.end())
314         return 0;
315     return it->second->database();
316 }
317
318 } // namespace WebCore
319
320 #endif // ENABLE(INSPECTOR) && ENABLE(DATABASE)