InspectorDatabaseAgent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include "config.h"
  29. #if ENABLE(INSPECTOR) && ENABLE(SQL_DATABASE)
  30. #include "InspectorDatabaseAgent.h"
  31. #include "Database.h"
  32. #include "ExceptionCode.h"
  33. #include "ExceptionCodePlaceholder.h"
  34. #include "InspectorDatabaseResource.h"
  35. #include "InspectorFrontend.h"
  36. #include "InspectorState.h"
  37. #include "InspectorValues.h"
  38. #include "InstrumentingAgents.h"
  39. #include "SQLError.h"
  40. #include "SQLResultSet.h"
  41. #include "SQLResultSetRowList.h"
  42. #include "SQLStatementCallback.h"
  43. #include "SQLStatementErrorCallback.h"
  44. #include "SQLTransaction.h"
  45. #include "SQLTransactionCallback.h"
  46. #include "SQLTransactionErrorCallback.h"
  47. #include "SQLValue.h"
  48. #include "VoidCallback.h"
  49. #include <wtf/Vector.h>
  50. typedef WebCore::InspectorBackendDispatcher::DatabaseCommandHandler::ExecuteSQLCallback ExecuteSQLCallback;
  51. namespace WebCore {
  52. namespace DatabaseAgentState {
  53. static const char databaseAgentEnabled[] = "databaseAgentEnabled";
  54. };
  55. namespace {
  56. void reportTransactionFailed(ExecuteSQLCallback* requestCallback, SQLError* error)
  57. {
  58. RefPtr<TypeBuilder::Database::Error> errorObject = TypeBuilder::Database::Error::create()
  59. .setMessage(error->message())
  60. .setCode(error->code());
  61. requestCallback->sendSuccess(0, 0, errorObject.release());
  62. }
  63. class StatementCallback : public SQLStatementCallback {
  64. public:
  65. static PassRefPtr<StatementCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
  66. {
  67. return adoptRef(new StatementCallback(requestCallback));
  68. }
  69. virtual ~StatementCallback() { }
  70. virtual bool handleEvent(SQLTransaction*, SQLResultSet* resultSet)
  71. {
  72. SQLResultSetRowList* rowList = resultSet->rows();
  73. RefPtr<TypeBuilder::Array<String> > columnNames = TypeBuilder::Array<String>::create();
  74. const Vector<String>& columns = rowList->columnNames();
  75. for (size_t i = 0; i < columns.size(); ++i)
  76. columnNames->addItem(columns[i]);
  77. RefPtr<TypeBuilder::Array<InspectorValue> > values = TypeBuilder::Array<InspectorValue>::create();
  78. const Vector<SQLValue>& data = rowList->values();
  79. for (size_t i = 0; i < data.size(); ++i) {
  80. const SQLValue& value = rowList->values()[i];
  81. switch (value.type()) {
  82. case SQLValue::StringValue: values->addItem(InspectorString::create(value.string())); break;
  83. case SQLValue::NumberValue: values->addItem(InspectorBasicValue::create(value.number())); break;
  84. case SQLValue::NullValue: values->addItem(InspectorValue::null()); break;
  85. }
  86. }
  87. m_requestCallback->sendSuccess(columnNames.release(), values.release(), 0);
  88. return true;
  89. }
  90. private:
  91. StatementCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
  92. : m_requestCallback(requestCallback) { }
  93. RefPtr<ExecuteSQLCallback> m_requestCallback;
  94. };
  95. class StatementErrorCallback : public SQLStatementErrorCallback {
  96. public:
  97. static PassRefPtr<StatementErrorCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
  98. {
  99. return adoptRef(new StatementErrorCallback(requestCallback));
  100. }
  101. virtual ~StatementErrorCallback() { }
  102. virtual bool handleEvent(SQLTransaction*, SQLError* error)
  103. {
  104. reportTransactionFailed(m_requestCallback.get(), error);
  105. return true;
  106. }
  107. private:
  108. StatementErrorCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
  109. : m_requestCallback(requestCallback) { }
  110. RefPtr<ExecuteSQLCallback> m_requestCallback;
  111. };
  112. class TransactionCallback : public SQLTransactionCallback {
  113. public:
  114. static PassRefPtr<TransactionCallback> create(const String& sqlStatement, PassRefPtr<ExecuteSQLCallback> requestCallback)
  115. {
  116. return adoptRef(new TransactionCallback(sqlStatement, requestCallback));
  117. }
  118. virtual ~TransactionCallback() { }
  119. virtual bool handleEvent(SQLTransaction* transaction)
  120. {
  121. if (!m_requestCallback->isActive())
  122. return true;
  123. Vector<SQLValue> sqlValues;
  124. RefPtr<SQLStatementCallback> callback(StatementCallback::create(m_requestCallback.get()));
  125. RefPtr<SQLStatementErrorCallback> errorCallback(StatementErrorCallback::create(m_requestCallback.get()));
  126. transaction->executeSQL(m_sqlStatement, sqlValues, callback.release(), errorCallback.release(), IGNORE_EXCEPTION);
  127. return true;
  128. }
  129. private:
  130. TransactionCallback(const String& sqlStatement, PassRefPtr<ExecuteSQLCallback> requestCallback)
  131. : m_sqlStatement(sqlStatement)
  132. , m_requestCallback(requestCallback) { }
  133. String m_sqlStatement;
  134. RefPtr<ExecuteSQLCallback> m_requestCallback;
  135. };
  136. class TransactionErrorCallback : public SQLTransactionErrorCallback {
  137. public:
  138. static PassRefPtr<TransactionErrorCallback> create(PassRefPtr<ExecuteSQLCallback> requestCallback)
  139. {
  140. return adoptRef(new TransactionErrorCallback(requestCallback));
  141. }
  142. virtual ~TransactionErrorCallback() { }
  143. virtual bool handleEvent(SQLError* error)
  144. {
  145. reportTransactionFailed(m_requestCallback.get(), error);
  146. return true;
  147. }
  148. private:
  149. TransactionErrorCallback(PassRefPtr<ExecuteSQLCallback> requestCallback)
  150. : m_requestCallback(requestCallback) { }
  151. RefPtr<ExecuteSQLCallback> m_requestCallback;
  152. };
  153. class TransactionSuccessCallback : public VoidCallback {
  154. public:
  155. static PassRefPtr<TransactionSuccessCallback> create()
  156. {
  157. return adoptRef(new TransactionSuccessCallback());
  158. }
  159. virtual ~TransactionSuccessCallback() { }
  160. virtual bool handleEvent() { return false; }
  161. private:
  162. TransactionSuccessCallback() { }
  163. };
  164. } // namespace
  165. void InspectorDatabaseAgent::didOpenDatabase(PassRefPtr<Database> database, const String& domain, const String& name, const String& version)
  166. {
  167. if (InspectorDatabaseResource* resource = findByFileName(database->fileName())) {
  168. resource->setDatabase(database);
  169. return;
  170. }
  171. RefPtr<InspectorDatabaseResource> resource = InspectorDatabaseResource::create(database, domain, name, version);
  172. m_resources.set(resource->id(), resource);
  173. // Resources are only bound while visible.
  174. if (m_frontend && m_enabled)
  175. resource->bind(m_frontend);
  176. }
  177. void InspectorDatabaseAgent::clearResources()
  178. {
  179. m_resources.clear();
  180. }
  181. InspectorDatabaseAgent::InspectorDatabaseAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state)
  182. : InspectorBaseAgent<InspectorDatabaseAgent>("Database", instrumentingAgents, state)
  183. , m_enabled(false)
  184. {
  185. m_instrumentingAgents->setInspectorDatabaseAgent(this);
  186. }
  187. InspectorDatabaseAgent::~InspectorDatabaseAgent()
  188. {
  189. m_instrumentingAgents->setInspectorDatabaseAgent(0);
  190. }
  191. void InspectorDatabaseAgent::setFrontend(InspectorFrontend* frontend)
  192. {
  193. m_frontend = frontend->database();
  194. }
  195. void InspectorDatabaseAgent::clearFrontend()
  196. {
  197. m_frontend = 0;
  198. disable(0);
  199. }
  200. void InspectorDatabaseAgent::enable(ErrorString*)
  201. {
  202. if (m_enabled)
  203. return;
  204. m_enabled = true;
  205. m_state->setBoolean(DatabaseAgentState::databaseAgentEnabled, m_enabled);
  206. DatabaseResourcesMap::iterator databasesEnd = m_resources.end();
  207. for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != databasesEnd; ++it)
  208. it->value->bind(m_frontend);
  209. }
  210. void InspectorDatabaseAgent::disable(ErrorString*)
  211. {
  212. if (!m_enabled)
  213. return;
  214. m_enabled = false;
  215. m_state->setBoolean(DatabaseAgentState::databaseAgentEnabled, m_enabled);
  216. }
  217. void InspectorDatabaseAgent::restore()
  218. {
  219. m_enabled = m_state->getBoolean(DatabaseAgentState::databaseAgentEnabled);
  220. }
  221. void InspectorDatabaseAgent::getDatabaseTableNames(ErrorString* error, const String& databaseId, RefPtr<TypeBuilder::Array<String> >& names)
  222. {
  223. if (!m_enabled) {
  224. *error = "Database agent is not enabled";
  225. return;
  226. }
  227. names = TypeBuilder::Array<String>::create();
  228. Database* database = databaseForId(databaseId);
  229. if (database) {
  230. Vector<String> tableNames = database->tableNames();
  231. unsigned length = tableNames.size();
  232. for (unsigned i = 0; i < length; ++i)
  233. names->addItem(tableNames[i]);
  234. }
  235. }
  236. void InspectorDatabaseAgent::executeSQL(ErrorString*, const String& databaseId, const String& query, PassRefPtr<ExecuteSQLCallback> prpRequestCallback)
  237. {
  238. RefPtr<ExecuteSQLCallback> requestCallback = prpRequestCallback;
  239. if (!m_enabled) {
  240. requestCallback->sendFailure("Database agent is not enabled");
  241. return;
  242. }
  243. Database* database = databaseForId(databaseId);
  244. if (!database) {
  245. requestCallback->sendFailure("Database not found");
  246. return;
  247. }
  248. RefPtr<SQLTransactionCallback> callback(TransactionCallback::create(query, requestCallback.get()));
  249. RefPtr<SQLTransactionErrorCallback> errorCallback(TransactionErrorCallback::create(requestCallback.get()));
  250. RefPtr<VoidCallback> successCallback(TransactionSuccessCallback::create());
  251. database->transaction(callback.release(), errorCallback.release(), successCallback.release());
  252. }
  253. String InspectorDatabaseAgent::databaseId(Database* database)
  254. {
  255. for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
  256. if (it->value->database() == database)
  257. return it->key;
  258. }
  259. return String();
  260. }
  261. InspectorDatabaseResource* InspectorDatabaseAgent::findByFileName(const String& fileName)
  262. {
  263. for (DatabaseResourcesMap::iterator it = m_resources.begin(); it != m_resources.end(); ++it) {
  264. if (it->value->database()->fileName() == fileName)
  265. return it->value.get();
  266. }
  267. return 0;
  268. }
  269. Database* InspectorDatabaseAgent::databaseForId(const String& databaseId)
  270. {
  271. DatabaseResourcesMap::iterator it = m_resources.find(databaseId);
  272. if (it == m_resources.end())
  273. return 0;
  274. return it->value->database();
  275. }
  276. } // namespace WebCore
  277. #endif // ENABLE(INSPECTOR) && ENABLE(SQL_DATABASE)