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