Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / 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 #include "modules/webdatabase/InspectorDatabaseAgent.h"
31
32 #include "bindings/v8/ExceptionStatePlaceholder.h"
33 #include "core/frame/Frame.h"
34 #include "core/html/VoidCallback.h"
35 #include "core/inspector/InspectorState.h"
36 #include "core/loader/DocumentLoader.h"
37 #include "core/page/Page.h"
38 #include "modules/webdatabase/Database.h"
39 #include "modules/webdatabase/InspectorDatabaseResource.h"
40 #include "modules/webdatabase/SQLError.h"
41 #include "modules/webdatabase/SQLResultSet.h"
42 #include "modules/webdatabase/SQLResultSetRowList.h"
43 #include "modules/webdatabase/SQLStatementCallback.h"
44 #include "modules/webdatabase/SQLStatementErrorCallback.h"
45 #include "modules/webdatabase/SQLTransaction.h"
46 #include "modules/webdatabase/SQLTransactionCallback.h"
47 #include "modules/webdatabase/SQLTransactionErrorCallback.h"
48 #include "modules/webdatabase/sqlite/SQLValue.h"
49 #include "platform/JSONValues.h"
50 #include "wtf/Vector.h"
51
52 typedef WebCore::InspectorBackendDispatcher::DatabaseCommandHandler::ExecuteSQLCallback ExecuteSQLCallback;
53
54 namespace WebCore {
55
56 namespace DatabaseAgentState {
57 static const char databaseAgentEnabled[] = "databaseAgentEnabled";
58 };
59
60 namespace {
61
62 void reportTransactionFailed(ExecuteSQLCallback* requestCallback, SQLError* error)
63 {
64     RefPtr<TypeBuilder::Database::Error> errorObject = TypeBuilder::Database::Error::create()
65         .setMessage(error->message())
66         .setCode(error->code());
67     requestCallback->sendSuccess(0, 0, errorObject.release());
68 }
69
70 class StatementCallback FINAL : public SQLStatementCallback {
71 public:
72     static PassOwnPtr<StatementCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
73     {
74         return adoptPtr(new StatementCallback(requestCallback));
75     }
76
77     virtual ~StatementCallback() { }
78
79     virtual bool handleEvent(SQLTransaction*, SQLResultSet* resultSet) OVERRIDE
80     {
81         SQLResultSetRowList* rowList = resultSet->rows();
82
83         RefPtr<TypeBuilder::Array<String> > columnNames = TypeBuilder::Array<String>::create();
84         const Vector<String>& columns = rowList->columnNames();
85         for (size_t i = 0; i < columns.size(); ++i)
86             columnNames->addItem(columns[i]);
87
88         RefPtr<TypeBuilder::Array<JSONValue> > values = TypeBuilder::Array<JSONValue>::create();
89         const Vector<SQLValue>& data = rowList->values();
90         for (size_t i = 0; i < data.size(); ++i) {
91             const SQLValue& value = rowList->values()[i];
92             switch (value.type()) {
93             case SQLValue::StringValue: values->addItem(JSONString::create(value.string())); break;
94             case SQLValue::NumberValue: values->addItem(JSONBasicValue::create(value.number())); break;
95             case SQLValue::NullValue: values->addItem(JSONValue::null()); break;
96             }
97         }
98         m_requestCallback->sendSuccess(columnNames.release(), values.release(), 0);
99         return true;
100     }
101
102 private:
103     StatementCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
104         : m_requestCallback(requestCallback) { }
105     RefPtr<ExecuteSQLCallback> m_requestCallback;
106 };
107
108 class StatementErrorCallback FINAL : public SQLStatementErrorCallback {
109 public:
110     static PassOwnPtr<StatementErrorCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
111     {
112         return adoptPtr(new StatementErrorCallback(requestCallback));
113     }
114
115     virtual ~StatementErrorCallback() { }
116
117     virtual bool handleEvent(SQLTransaction*, SQLError* error) OVERRIDE
118     {
119         reportTransactionFailed(m_requestCallback.get(), error);
120         return true;
121     }
122
123 private:
124     StatementErrorCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
125         : m_requestCallback(requestCallback) { }
126     RefPtr<ExecuteSQLCallback> m_requestCallback;
127 };
128
129 class TransactionCallback FINAL : public SQLTransactionCallback {
130 public:
131     static PassOwnPtr<TransactionCallback> create(const String& sqlStatement, PassRefPtr<ExecuteSQLCallback> requestCallback)
132     {
133         return adoptPtr(new TransactionCallback(sqlStatement, requestCallback));
134     }
135
136     virtual ~TransactionCallback() { }
137
138     virtual bool handleEvent(SQLTransaction* transaction) OVERRIDE
139     {
140         if (!m_requestCallback->isActive())
141             return true;
142
143         Vector<SQLValue> sqlValues;
144         OwnPtr<SQLStatementCallback> callback(StatementCallback::create(m_requestCallback.get()));
145         OwnPtr<SQLStatementErrorCallback> errorCallback(StatementErrorCallback::create(m_requestCallback.get()));
146         transaction->executeSQL(m_sqlStatement, sqlValues, callback.release(), errorCallback.release(), IGNORE_EXCEPTION);
147         return true;
148     }
149 private:
150     TransactionCallback(const String& sqlStatement, PassRefPtr<ExecuteSQLCallback> requestCallback)
151         : m_sqlStatement(sqlStatement)
152         , m_requestCallback(requestCallback) { }
153     String m_sqlStatement;
154     RefPtr<ExecuteSQLCallback> m_requestCallback;
155 };
156
157 class TransactionErrorCallback FINAL : public SQLTransactionErrorCallback {
158 public:
159     static PassOwnPtr<TransactionErrorCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
160     {
161         return adoptPtr(new TransactionErrorCallback(requestCallback));
162     }
163
164     virtual ~TransactionErrorCallback() { }
165
166     virtual bool handleEvent(SQLError* error) OVERRIDE
167     {
168         reportTransactionFailed(m_requestCallback.get(), error);
169         return true;
170     }
171 private:
172     TransactionErrorCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
173         : m_requestCallback(requestCallback) { }
174     RefPtr<ExecuteSQLCallback> m_requestCallback;
175 };
176
177 class TransactionSuccessCallback FINAL : public VoidCallback {
178 public:
179     static PassOwnPtr<TransactionSuccessCallback> create()
180     {
181         return adoptPtr(new TransactionSuccessCallback());
182     }
183
184     virtual ~TransactionSuccessCallback() { }
185
186     virtual void handleEvent() OVERRIDE { }
187
188 private:
189     TransactionSuccessCallback() { }
190 };
191
192 } // namespace
193
194 void InspectorDatabaseAgent::didOpenDatabase(PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
195 {
196     if (InspectorDatabaseResource* resource = findByFileName(database->fileName())) {
197         resource->setDatabase(database);
198         return;
199     }
200
201     RefPtr<InspectorDatabaseResource> resource = InspectorDatabaseResource::create(database, domain, name, version);
202     m_resources.set(resource->id(), resource);
203     // Resources are only bound while visible.
204     if (m_frontend && m_enabled)
205         resource->bind(m_frontend);
206 }
207
208 void InspectorDatabaseAgent::didCommitLoad(Frame* frame, DocumentLoader* loader)
209 {
210     // FIXME: If "frame" is always guarenteed to be in the same Page as loader->frame()
211     // then all we need to check here is loader->frame()->isMainFrame()
212     // and we don't need "frame" at all.
213     if (loader->frame() != frame->page()->mainFrame())
214         return;
215
216     m_resources.clear();
217 }
218
219 InspectorDatabaseAgent::InspectorDatabaseAgent()
220     : InspectorBaseAgent<InspectorDatabaseAgent>("Database")
221     , m_frontend(0)
222     , m_enabled(false)
223 {
224 }
225
226 InspectorDatabaseAgent::~InspectorDatabaseAgent()
227 {
228 }
229
230 void InspectorDatabaseAgent::setFrontend(InspectorFrontend* frontend)
231 {
232     m_frontend = frontend->database();
233 }
234
235 void InspectorDatabaseAgent::clearFrontend()
236 {
237     m_frontend = 0;
238     disable(0);
239 }
240
241 void InspectorDatabaseAgent::enable(ErrorString*)
242 {
243     if (m_enabled)
244         return;
245     m_enabled = true;
246     m_state->setBoolean(DatabaseAgentState::databaseAgentEnabled, m_enabled);
247
248     DatabaseResourcesMap::iterator databasesEnd = m_resources.end();
249     for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != databasesEnd; ++it)
250         it->value->bind(m_frontend);
251 }
252
253 void InspectorDatabaseAgent::disable(ErrorString*)
254 {
255     if (!m_enabled)
256         return;
257     m_enabled = false;
258     m_state->setBoolean(DatabaseAgentState::databaseAgentEnabled, m_enabled);
259 }
260
261 void InspectorDatabaseAgent::restore()
262 {
263     m_enabled = m_state->getBoolean(DatabaseAgentState::databaseAgentEnabled);
264 }
265
266 void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString* error, const String& databaseId, RefPtr<TypeBuilder::Array<String> >& names)
267 {
268     if (!m_enabled) {
269         *error = "Database agent is not enabled";
270         return;
271     }
272
273     names = TypeBuilder::Array<String>::create();
274
275     Database* database = databaseForId(databaseId);
276     if (database) {
277         Vector<String> tableNames = database->tableNames();
278         unsigned length = tableNames.size();
279         for (unsigned i = 0; i < length; ++i)
280             names->addItem(tableNames[i]);
281     }
282 }
283
284 void InspectorDatabaseAgent::executeSQL(ErrorString*, const String& databaseId, const String& query, PassRefPtr<ExecuteSQLCallback> prpRequestCallback)
285 {
286     RefPtr<ExecuteSQLCallback> requestCallback = prpRequestCallback;
287
288     if (!m_enabled) {
289         requestCallback->sendFailure("Database agent is not enabled");
290         return;
291     }
292
293     Database* database = databaseForId(databaseId);
294     if (!database) {
295         requestCallback->sendFailure("Database not found");
296         return;
297     }
298
299     OwnPtr<SQLTransactionCallback> callback(TransactionCallback::create(query, requestCallback.get()));
300     OwnPtr<SQLTransactionErrorCallback> errorCallback(TransactionErrorCallback::create(requestCallback.get()));
301     OwnPtr<VoidCallback> successCallback(TransactionSuccessCallback::create());
302     database->transaction(callback.release(), errorCallback.release(), successCallback.release());
303 }
304
305 InspectorDatabaseResource* InspectorDatabaseAgent::findByFileName(const String& fileName)
306 {
307     for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
308         if (it->value->database()->fileName() == fileName)
309             return it->value.get();
310     }
311     return 0;
312 }
313
314 Database* InspectorDatabaseAgent::databaseForId(const String& databaseId)
315 {
316     DatabaseResourcesMap::iterator it = m_resources.find(databaseId);
317     if (it == m_resources.end())
318         return 0;
319     return it->value->database();
320 }
321
322 } // namespace WebCore