2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "core/inspector/InspectorDatabaseAgent.h"
32 #include "bindings/v8/ExceptionStatePlaceholder.h"
33 #include "core/inspector/InspectorDatabaseResource.h"
34 #include "core/inspector/InspectorState.h"
35 #include "core/inspector/InstrumentingAgents.h"
36 #include "core/loader/DocumentLoader.h"
37 #include "core/frame/Frame.h"
38 #include "core/html/VoidCallback.h"
39 #include "core/page/Page.h"
40 #include "modules/webdatabase/Database.h"
41 #include "modules/webdatabase/SQLError.h"
42 #include "modules/webdatabase/SQLResultSet.h"
43 #include "modules/webdatabase/SQLResultSetRowList.h"
44 #include "modules/webdatabase/SQLStatementCallback.h"
45 #include "modules/webdatabase/SQLStatementErrorCallback.h"
46 #include "modules/webdatabase/SQLTransaction.h"
47 #include "modules/webdatabase/SQLTransactionCallback.h"
48 #include "modules/webdatabase/SQLTransactionErrorCallback.h"
49 #include "modules/webdatabase/sqlite/SQLValue.h"
50 #include "platform/JSONValues.h"
51 #include "wtf/Vector.h"
53 typedef WebCore::InspectorBackendDispatcher::DatabaseCommandHandler::ExecuteSQLCallback ExecuteSQLCallback;
57 namespace DatabaseAgentState {
58 static const char databaseAgentEnabled[] = "databaseAgentEnabled";
63 void reportTransactionFailed(ExecuteSQLCallback* requestCallback, SQLError* error)
65 RefPtr<TypeBuilder::Database::Error> errorObject = TypeBuilder::Database::Error::create()
66 .setMessage(error->message())
67 .setCode(error->code());
68 requestCallback->sendSuccess(0, 0, errorObject.release());
71 class StatementCallback FINAL : public SQLStatementCallback {
73 static PassOwnPtr<StatementCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
75 return adoptPtr(new StatementCallback(requestCallback));
78 virtual ~StatementCallback() { }
80 virtual bool handleEvent(SQLTransaction*, SQLResultSet* resultSet) OVERRIDE
82 SQLResultSetRowList* rowList = resultSet->rows();
84 RefPtr<TypeBuilder::Array<String> > columnNames = TypeBuilder::Array<String>::create();
85 const Vector<String>& columns = rowList->columnNames();
86 for (size_t i = 0; i < columns.size(); ++i)
87 columnNames->addItem(columns[i]);
89 RefPtr<TypeBuilder::Array<JSONValue> > values = TypeBuilder::Array<JSONValue>::create();
90 const Vector<SQLValue>& data = rowList->values();
91 for (size_t i = 0; i < data.size(); ++i) {
92 const SQLValue& value = rowList->values()[i];
93 switch (value.type()) {
94 case SQLValue::StringValue: values->addItem(JSONString::create(value.string())); break;
95 case SQLValue::NumberValue: values->addItem(JSONBasicValue::create(value.number())); break;
96 case SQLValue::NullValue: values->addItem(JSONValue::null()); break;
99 m_requestCallback->sendSuccess(columnNames.release(), values.release(), 0);
104 StatementCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
105 : m_requestCallback(requestCallback) { }
106 RefPtr<ExecuteSQLCallback> m_requestCallback;
109 class StatementErrorCallback FINAL : public SQLStatementErrorCallback {
111 static PassOwnPtr<StatementErrorCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
113 return adoptPtr(new StatementErrorCallback(requestCallback));
116 virtual ~StatementErrorCallback() { }
118 virtual bool handleEvent(SQLTransaction*, SQLError* error) OVERRIDE
120 reportTransactionFailed(m_requestCallback.get(), error);
125 StatementErrorCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
126 : m_requestCallback(requestCallback) { }
127 RefPtr<ExecuteSQLCallback> m_requestCallback;
130 class TransactionCallback FINAL : public SQLTransactionCallback {
132 static PassOwnPtr<TransactionCallback> create(const String& sqlStatement, PassRefPtr<ExecuteSQLCallback> requestCallback)
134 return adoptPtr(new TransactionCallback(sqlStatement, requestCallback));
137 virtual ~TransactionCallback() { }
139 virtual bool handleEvent(SQLTransaction* transaction) OVERRIDE
141 if (!m_requestCallback->isActive())
144 Vector<SQLValue> sqlValues;
145 OwnPtr<SQLStatementCallback> callback(StatementCallback::create(m_requestCallback.get()));
146 OwnPtr<SQLStatementErrorCallback> errorCallback(StatementErrorCallback::create(m_requestCallback.get()));
147 transaction->executeSQL(m_sqlStatement, sqlValues, callback.release(), errorCallback.release(), IGNORE_EXCEPTION);
151 TransactionCallback(const String& sqlStatement, PassRefPtr<ExecuteSQLCallback> requestCallback)
152 : m_sqlStatement(sqlStatement)
153 , m_requestCallback(requestCallback) { }
154 String m_sqlStatement;
155 RefPtr<ExecuteSQLCallback> m_requestCallback;
158 class TransactionErrorCallback FINAL : public SQLTransactionErrorCallback {
160 static PassOwnPtr<TransactionErrorCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
162 return adoptPtr(new TransactionErrorCallback(requestCallback));
165 virtual ~TransactionErrorCallback() { }
167 virtual bool handleEvent(SQLError* error) OVERRIDE
169 reportTransactionFailed(m_requestCallback.get(), error);
173 TransactionErrorCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
174 : m_requestCallback(requestCallback) { }
175 RefPtr<ExecuteSQLCallback> m_requestCallback;
178 class TransactionSuccessCallback FINAL : public VoidCallback {
180 static PassOwnPtr<TransactionSuccessCallback> create()
182 return adoptPtr(new TransactionSuccessCallback());
185 virtual ~TransactionSuccessCallback() { }
187 virtual void handleEvent() OVERRIDE { }
190 TransactionSuccessCallback() { }
195 void InspectorDatabaseAgent::didOpenDatabase(PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
197 if (InspectorDatabaseResource* resource = findByFileName(database->fileName())) {
198 resource->setDatabase(database);
202 RefPtr<InspectorDatabaseResource> resource = InspectorDatabaseResource::create(database, domain, name, version);
203 m_resources.set(resource->id(), resource);
204 // Resources are only bound while visible.
205 if (m_frontend && m_enabled)
206 resource->bind(m_frontend);
209 void InspectorDatabaseAgent::didCommitLoad(Frame* frame, DocumentLoader* loader)
211 // FIXME: If "frame" is always guarenteed to be in the same Page as loader->frame()
212 // then all we need to check here is loader->frame()->isMainFrame()
213 // and we don't need "frame" at all.
214 if (loader->frame() != frame->page()->mainFrame())
220 InspectorDatabaseAgent::InspectorDatabaseAgent()
221 : InspectorBaseAgent<InspectorDatabaseAgent>("Database")
227 void InspectorDatabaseAgent::init()
229 m_instrumentingAgents->setInspectorDatabaseAgent(this);
232 InspectorDatabaseAgent::~InspectorDatabaseAgent()
234 m_instrumentingAgents->setInspectorDatabaseAgent(0);
237 void InspectorDatabaseAgent::setFrontend(InspectorFrontend* frontend)
239 m_frontend = frontend->database();
242 void InspectorDatabaseAgent::clearFrontend()
248 void InspectorDatabaseAgent::enable(ErrorString*)
253 m_state->setBoolean(DatabaseAgentState::databaseAgentEnabled, m_enabled);
255 DatabaseResourcesMap::iterator databasesEnd = m_resources.end();
256 for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != databasesEnd; ++it)
257 it->value->bind(m_frontend);
260 void InspectorDatabaseAgent::disable(ErrorString*)
265 m_state->setBoolean(DatabaseAgentState::databaseAgentEnabled, m_enabled);
268 void InspectorDatabaseAgent::restore()
270 m_enabled = m_state->getBoolean(DatabaseAgentState::databaseAgentEnabled);
273 void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString* error, const String& databaseId, RefPtr<TypeBuilder::Array<String> >& names)
276 *error = "Database agent is not enabled";
280 names = TypeBuilder::Array<String>::create();
282 Database* database = databaseForId(databaseId);
284 Vector<String> tableNames = database->tableNames();
285 unsigned length = tableNames.size();
286 for (unsigned i = 0; i < length; ++i)
287 names->addItem(tableNames[i]);
291 void InspectorDatabaseAgent::executeSQL(ErrorString*, const String& databaseId, const String& query, PassRefPtr<ExecuteSQLCallback> prpRequestCallback)
293 RefPtr<ExecuteSQLCallback> requestCallback = prpRequestCallback;
296 requestCallback->sendFailure("Database agent is not enabled");
300 Database* database = databaseForId(databaseId);
302 requestCallback->sendFailure("Database not found");
306 OwnPtr<SQLTransactionCallback> callback(TransactionCallback::create(query, requestCallback.get()));
307 OwnPtr<SQLTransactionErrorCallback> errorCallback(TransactionErrorCallback::create(requestCallback.get()));
308 OwnPtr<VoidCallback> successCallback(TransactionSuccessCallback::create());
309 database->transaction(callback.release(), errorCallback.release(), successCallback.release());
312 String InspectorDatabaseAgent::databaseId(Database* database)
314 for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
315 if (it->value->database() == database)
321 InspectorDatabaseResource* InspectorDatabaseAgent::findByFileName(const String& fileName)
323 for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
324 if (it->value->database()->fileName() == fileName)
325 return it->value.get();
330 Database* InspectorDatabaseAgent::databaseForId(const String& databaseId)
332 DatabaseResourcesMap::iterator it = m_resources.find(databaseId);
333 if (it == m_resources.end())
335 return it->value->database();
338 } // namespace WebCore