nsXULTemplateQueryProcessorStorage.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "prprf.h"
  6. #include "nsIDOMNodeList.h"
  7. #include "nsUnicharUtils.h"
  8. #include "nsArrayUtils.h"
  9. #include "nsVariant.h"
  10. #include "nsAppDirectoryServiceDefs.h"
  11. #include "nsIURI.h"
  12. #include "nsIFileChannel.h"
  13. #include "nsIFile.h"
  14. #include "nsGkAtoms.h"
  15. #include "nsContentUtils.h"
  16. #include "nsXULTemplateBuilder.h"
  17. #include "nsXULTemplateResultStorage.h"
  18. #include "nsXULContentUtils.h"
  19. #include "nsXULSortService.h"
  20. #include "mozIStorageService.h"
  21. #include "nsIChannel.h"
  22. #include "nsIDocument.h"
  23. #include "nsNetUtil.h"
  24. using mozilla::fallible;
  25. //----------------------------------------------------------------------
  26. //
  27. // nsXULTemplateResultSetStorage
  28. //
  29. NS_IMPL_ISUPPORTS(nsXULTemplateResultSetStorage, nsISimpleEnumerator)
  30. nsXULTemplateResultSetStorage::nsXULTemplateResultSetStorage(mozIStorageStatement* aStatement)
  31. : mStatement(aStatement)
  32. {
  33. uint32_t count;
  34. nsresult rv = aStatement->GetColumnCount(&count);
  35. if (NS_FAILED(rv)) {
  36. mStatement = nullptr;
  37. return;
  38. }
  39. for (uint32_t c = 0; c < count; c++) {
  40. nsAutoCString name;
  41. rv = aStatement->GetColumnName(c, name);
  42. if (NS_SUCCEEDED(rv)) {
  43. nsCOMPtr<nsIAtom> columnName = NS_Atomize(NS_LITERAL_CSTRING("?") + name);
  44. mColumnNames.AppendObject(columnName);
  45. }
  46. }
  47. }
  48. NS_IMETHODIMP
  49. nsXULTemplateResultSetStorage::HasMoreElements(bool *aResult)
  50. {
  51. if (!mStatement) {
  52. *aResult = false;
  53. return NS_OK;
  54. }
  55. nsresult rv = mStatement->ExecuteStep(aResult);
  56. NS_ENSURE_SUCCESS(rv, rv);
  57. // Because the nsXULTemplateResultSetStorage is owned by many nsXULTemplateResultStorage objects,
  58. // it could live longer than it needed to get results.
  59. // So we destroy the statement to free resources when all results are fetched
  60. if (!*aResult) {
  61. mStatement = nullptr;
  62. }
  63. return NS_OK;
  64. }
  65. NS_IMETHODIMP
  66. nsXULTemplateResultSetStorage::GetNext(nsISupports **aResult)
  67. {
  68. nsXULTemplateResultStorage* result =
  69. new nsXULTemplateResultStorage(this);
  70. *aResult = result;
  71. NS_ADDREF(result);
  72. return NS_OK;
  73. }
  74. int32_t
  75. nsXULTemplateResultSetStorage::GetColumnIndex(nsIAtom* aColumnName)
  76. {
  77. int32_t count = mColumnNames.Count();
  78. for (int32_t c = 0; c < count; c++) {
  79. if (mColumnNames[c] == aColumnName)
  80. return c;
  81. }
  82. return -1;
  83. }
  84. void
  85. nsXULTemplateResultSetStorage::FillColumnValues(nsCOMArray<nsIVariant>& aArray)
  86. {
  87. if (!mStatement)
  88. return;
  89. int32_t count = mColumnNames.Count();
  90. for (int32_t c = 0; c < count; c++) {
  91. RefPtr<nsVariant> value = new nsVariant();
  92. int32_t type;
  93. mStatement->GetTypeOfIndex(c, &type);
  94. if (type == mStatement->VALUE_TYPE_INTEGER) {
  95. int64_t val = mStatement->AsInt64(c);
  96. value->SetAsInt64(val);
  97. }
  98. else if (type == mStatement->VALUE_TYPE_FLOAT) {
  99. double val = mStatement->AsDouble(c);
  100. value->SetAsDouble(val);
  101. }
  102. else {
  103. nsAutoString val;
  104. nsresult rv = mStatement->GetString(c, val);
  105. if (NS_FAILED(rv))
  106. value->SetAsAString(EmptyString());
  107. else
  108. value->SetAsAString(val);
  109. }
  110. aArray.AppendObject(value);
  111. }
  112. }
  113. //----------------------------------------------------------------------
  114. //
  115. // nsXULTemplateQueryProcessorStorage
  116. //
  117. NS_IMPL_ISUPPORTS(nsXULTemplateQueryProcessorStorage,
  118. nsIXULTemplateQueryProcessor)
  119. nsXULTemplateQueryProcessorStorage::nsXULTemplateQueryProcessorStorage()
  120. : mGenerationStarted(false)
  121. {
  122. }
  123. NS_IMETHODIMP
  124. nsXULTemplateQueryProcessorStorage::GetDatasource(nsIArray* aDataSources,
  125. nsIDOMNode* aRootNode,
  126. bool aIsTrusted,
  127. nsIXULTemplateBuilder* aBuilder,
  128. bool* aShouldDelayBuilding,
  129. nsISupports** aReturn)
  130. {
  131. *aReturn = nullptr;
  132. *aShouldDelayBuilding = false;
  133. if (!aIsTrusted) {
  134. return NS_OK;
  135. }
  136. uint32_t length;
  137. nsresult rv = aDataSources->GetLength(&length);
  138. NS_ENSURE_SUCCESS(rv, rv);
  139. if (length == 0) {
  140. return NS_OK;
  141. }
  142. // We get only the first uri. This query processor supports
  143. // only one database at a time.
  144. nsCOMPtr<nsIURI> uri;
  145. uri = do_QueryElementAt(aDataSources, 0);
  146. if (!uri) {
  147. // No uri in the list of datasources
  148. return NS_OK;
  149. }
  150. nsCOMPtr<mozIStorageService> storage =
  151. do_GetService("@mozilla.org/storage/service;1", &rv);
  152. NS_ENSURE_SUCCESS(rv, rv);
  153. nsCOMPtr<nsIFile> databaseFile;
  154. nsAutoCString scheme;
  155. rv = uri->GetScheme(scheme);
  156. NS_ENSURE_SUCCESS(rv, rv);
  157. if (scheme.EqualsLiteral("profile")) {
  158. nsAutoCString path;
  159. rv = uri->GetPath(path);
  160. NS_ENSURE_SUCCESS(rv, rv);
  161. if (path.IsEmpty()) {
  162. return NS_ERROR_FAILURE;
  163. }
  164. rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
  165. getter_AddRefs(databaseFile));
  166. NS_ENSURE_SUCCESS(rv, rv);
  167. rv = databaseFile->AppendNative(path);
  168. NS_ENSURE_SUCCESS(rv, rv);
  169. }
  170. else {
  171. nsCOMPtr<nsIChannel> channel;
  172. nsCOMPtr<nsINode> node = do_QueryInterface(aRootNode);
  173. // The following channel is never openend, so it does not matter what
  174. // securityFlags we pass; let's follow the principle of least privilege.
  175. rv = NS_NewChannel(getter_AddRefs(channel),
  176. uri,
  177. node,
  178. nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
  179. nsIContentPolicy::TYPE_OTHER);
  180. NS_ENSURE_SUCCESS(rv, rv);
  181. nsCOMPtr<nsIFileChannel> fileChannel = do_QueryInterface(channel, &rv);
  182. if (NS_FAILED(rv)) { // if it fails, not a file url
  183. nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_BAD_URI);
  184. return rv;
  185. }
  186. rv = fileChannel->GetFile(getter_AddRefs(databaseFile));
  187. NS_ENSURE_SUCCESS(rv, rv);
  188. }
  189. // ok now we have an URI of a sqlite file
  190. nsCOMPtr<mozIStorageConnection> connection;
  191. rv = storage->OpenDatabase(databaseFile, getter_AddRefs(connection));
  192. if (NS_FAILED(rv)) {
  193. nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_CANNOT_OPEN_DATABASE);
  194. return rv;
  195. }
  196. connection.forget(aReturn);
  197. return NS_OK;
  198. }
  199. NS_IMETHODIMP
  200. nsXULTemplateQueryProcessorStorage::InitializeForBuilding(nsISupports* aDatasource,
  201. nsIXULTemplateBuilder* aBuilder,
  202. nsIDOMNode* aRootNode)
  203. {
  204. NS_ENSURE_STATE(!mGenerationStarted);
  205. mStorageConnection = do_QueryInterface(aDatasource);
  206. if (!mStorageConnection)
  207. return NS_ERROR_INVALID_ARG;
  208. bool ready;
  209. mStorageConnection->GetConnectionReady(&ready);
  210. if (!ready)
  211. return NS_ERROR_UNEXPECTED;
  212. return NS_OK;
  213. }
  214. NS_IMETHODIMP
  215. nsXULTemplateQueryProcessorStorage::Done()
  216. {
  217. mGenerationStarted = false;
  218. return NS_OK;
  219. }
  220. NS_IMETHODIMP
  221. nsXULTemplateQueryProcessorStorage::CompileQuery(nsIXULTemplateBuilder* aBuilder,
  222. nsIDOMNode* aQueryNode,
  223. nsIAtom* aRefVariable,
  224. nsIAtom* aMemberVariable,
  225. nsISupports** aReturn)
  226. {
  227. nsCOMPtr<nsIDOMNodeList> childNodes;
  228. aQueryNode->GetChildNodes(getter_AddRefs(childNodes));
  229. uint32_t length;
  230. childNodes->GetLength(&length);
  231. nsCOMPtr<mozIStorageStatement> statement;
  232. nsCOMPtr<nsIContent> queryContent = do_QueryInterface(aQueryNode);
  233. nsAutoString sqlQuery;
  234. // Let's get all text nodes (which should be the query)
  235. if (!nsContentUtils::GetNodeTextContent(queryContent, false, sqlQuery, fallible)) {
  236. return NS_ERROR_OUT_OF_MEMORY;
  237. }
  238. nsresult rv = mStorageConnection->CreateStatement(NS_ConvertUTF16toUTF8(sqlQuery),
  239. getter_AddRefs(statement));
  240. if (NS_FAILED(rv)) {
  241. nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_BAD_QUERY);
  242. return rv;
  243. }
  244. uint32_t parameterCount = 0;
  245. for (nsIContent* child = queryContent->GetFirstChild();
  246. child;
  247. child = child->GetNextSibling()) {
  248. if (child->NodeInfo()->Equals(nsGkAtoms::param, kNameSpaceID_XUL)) {
  249. nsAutoString value;
  250. if (!nsContentUtils::GetNodeTextContent(child, false, value, fallible)) {
  251. return NS_ERROR_OUT_OF_MEMORY;
  252. }
  253. uint32_t index = parameterCount;
  254. nsAutoString name, indexValue;
  255. if (child->GetAttr(kNameSpaceID_None, nsGkAtoms::name, name)) {
  256. rv = statement->GetParameterIndex(NS_ConvertUTF16toUTF8(name),
  257. &index);
  258. if (NS_FAILED(rv)) {
  259. nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_UNKNOWN_QUERY_PARAMETER);
  260. return rv;
  261. }
  262. parameterCount++;
  263. }
  264. else if (child->GetAttr(kNameSpaceID_None, nsGkAtoms::index, indexValue)) {
  265. PR_sscanf(NS_ConvertUTF16toUTF8(indexValue).get(),"%d",&index);
  266. if (index > 0)
  267. index--;
  268. }
  269. else {
  270. parameterCount++;
  271. }
  272. static nsIContent::AttrValuesArray sTypeValues[] =
  273. { &nsGkAtoms::int32, &nsGkAtoms::integer, &nsGkAtoms::int64,
  274. &nsGkAtoms::null, &nsGkAtoms::double_, &nsGkAtoms::string, nullptr };
  275. int32_t typeError = 1;
  276. int32_t typeValue = child->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type,
  277. sTypeValues, eCaseMatters);
  278. rv = NS_ERROR_ILLEGAL_VALUE;
  279. int32_t valInt32 = 0;
  280. int64_t valInt64 = 0;
  281. double valFloat = 0;
  282. switch (typeValue) {
  283. case 0:
  284. case 1:
  285. typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%d",&valInt32);
  286. if (typeError > 0)
  287. rv = statement->BindInt32ByIndex(index, valInt32);
  288. break;
  289. case 2:
  290. typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%lld",&valInt64);
  291. if (typeError > 0)
  292. rv = statement->BindInt64ByIndex(index, valInt64);
  293. break;
  294. case 3:
  295. rv = statement->BindNullByIndex(index);
  296. break;
  297. case 4:
  298. typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%lf",&valFloat);
  299. if (typeError > 0)
  300. rv = statement->BindDoubleByIndex(index, valFloat);
  301. break;
  302. case 5:
  303. case nsIContent::ATTR_MISSING:
  304. rv = statement->BindStringByIndex(index, value);
  305. break;
  306. default:
  307. typeError = 0;
  308. }
  309. if (typeError <= 0) {
  310. nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_WRONG_TYPE_QUERY_PARAMETER);
  311. return rv;
  312. }
  313. if (NS_FAILED(rv)) {
  314. nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_QUERY_PARAMETER_NOT_BOUND);
  315. return rv;
  316. }
  317. }
  318. }
  319. *aReturn = statement;
  320. NS_IF_ADDREF(*aReturn);
  321. return NS_OK;
  322. }
  323. NS_IMETHODIMP
  324. nsXULTemplateQueryProcessorStorage::GenerateResults(nsISupports* aDatasource,
  325. nsIXULTemplateResult* aRef,
  326. nsISupports* aQuery,
  327. nsISimpleEnumerator** aResults)
  328. {
  329. mGenerationStarted = true;
  330. nsCOMPtr<mozIStorageStatement> statement = do_QueryInterface(aQuery);
  331. if (!statement)
  332. return NS_ERROR_FAILURE;
  333. nsXULTemplateResultSetStorage* results =
  334. new nsXULTemplateResultSetStorage(statement);
  335. *aResults = results;
  336. NS_ADDREF(*aResults);
  337. return NS_OK;
  338. }
  339. NS_IMETHODIMP
  340. nsXULTemplateQueryProcessorStorage::AddBinding(nsIDOMNode* aRuleNode,
  341. nsIAtom* aVar,
  342. nsIAtom* aRef,
  343. const nsAString& aExpr)
  344. {
  345. return NS_OK;
  346. }
  347. NS_IMETHODIMP
  348. nsXULTemplateQueryProcessorStorage::TranslateRef(nsISupports* aDatasource,
  349. const nsAString& aRefString,
  350. nsIXULTemplateResult** aRef)
  351. {
  352. nsXULTemplateResultStorage* result =
  353. new nsXULTemplateResultStorage(nullptr);
  354. *aRef = result;
  355. NS_ADDREF(*aRef);
  356. return NS_OK;
  357. }
  358. NS_IMETHODIMP
  359. nsXULTemplateQueryProcessorStorage::CompareResults(nsIXULTemplateResult* aLeft,
  360. nsIXULTemplateResult* aRight,
  361. nsIAtom* aVar,
  362. uint32_t aSortHints,
  363. int32_t* aResult)
  364. {
  365. *aResult = 0;
  366. if (!aVar)
  367. return NS_OK;
  368. // We're going to see if values are integers or float, to perform
  369. // a suitable comparison
  370. nsCOMPtr<nsISupports> leftValue, rightValue;
  371. if (aLeft)
  372. aLeft->GetBindingObjectFor(aVar, getter_AddRefs(leftValue));
  373. if (aRight)
  374. aRight->GetBindingObjectFor(aVar, getter_AddRefs(rightValue));
  375. if (leftValue && rightValue) {
  376. nsCOMPtr<nsIVariant> vLeftValue = do_QueryInterface(leftValue);
  377. nsCOMPtr<nsIVariant> vRightValue = do_QueryInterface(rightValue);
  378. if (vLeftValue && vRightValue) {
  379. nsresult rv1, rv2;
  380. uint16_t vtypeL, vtypeR;
  381. vLeftValue->GetDataType(&vtypeL);
  382. vRightValue->GetDataType(&vtypeR);
  383. if (vtypeL == vtypeR) {
  384. if (vtypeL == nsIDataType::VTYPE_INT64) {
  385. int64_t leftValue, rightValue;
  386. rv1 = vLeftValue->GetAsInt64(&leftValue);
  387. rv2 = vRightValue->GetAsInt64(&rightValue);
  388. if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2)) {
  389. if (leftValue > rightValue)
  390. *aResult = 1;
  391. else if (leftValue < rightValue)
  392. *aResult = -1;
  393. return NS_OK;
  394. }
  395. }
  396. else if (vtypeL == nsIDataType::VTYPE_DOUBLE) {
  397. double leftValue, rightValue;
  398. rv1 = vLeftValue->GetAsDouble(&leftValue);
  399. rv2 = vRightValue->GetAsDouble(&rightValue);
  400. if (NS_SUCCEEDED(rv1) && NS_SUCCEEDED(rv2)) {
  401. if (leftValue > rightValue)
  402. *aResult = 1;
  403. else if (leftValue < rightValue)
  404. *aResult = -1;
  405. return NS_OK;
  406. }
  407. }
  408. }
  409. }
  410. }
  411. // Values are not integers or floats, so we just compare them as simple strings
  412. nsAutoString leftVal;
  413. if (aLeft)
  414. aLeft->GetBindingFor(aVar, leftVal);
  415. nsAutoString rightVal;
  416. if (aRight)
  417. aRight->GetBindingFor(aVar, rightVal);
  418. *aResult = XULSortServiceImpl::CompareValues(leftVal, rightVal, aSortHints);
  419. return NS_OK;
  420. }