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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
|
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/**
* This file contains the definitions of nsNavHistoryQuery,
* nsNavHistoryQueryOptions, and those functions in nsINavHistory that directly
* support queries (specifically QueryStringToQueries and QueriesToQueryString).
*/
#include "mozilla/DebugOnly.h"
#include "nsNavHistory.h"
#include "nsNavBookmarks.h"
#include "nsEscape.h"
#include "nsCOMArray.h"
#include "nsNetUtil.h"
#include "nsTArray.h"
#include "prprf.h"
#include "nsVariant.h"
using namespace mozilla;
class QueryKeyValuePair
{
public:
// QueryKeyValuePair
//
// 01234567890
// input : qwerty&key=value&qwerty
// ^ ^ ^
// aKeyBegin | aPastEnd (may point to null terminator)
// aEquals
//
// Special case: if aKeyBegin == aEquals, then there is only one string
// and no equal sign, so we treat the entire thing as a key with no value
QueryKeyValuePair(const nsCSubstring& aSource, int32_t aKeyBegin,
int32_t aEquals, int32_t aPastEnd)
{
if (aEquals == aKeyBegin)
aEquals = aPastEnd;
key = Substring(aSource, aKeyBegin, aEquals - aKeyBegin);
if (aPastEnd - aEquals > 0)
value = Substring(aSource, aEquals + 1, aPastEnd - aEquals - 1);
}
nsCString key;
nsCString value;
};
static nsresult TokenizeQueryString(const nsACString& aQuery,
nsTArray<QueryKeyValuePair>* aTokens);
static nsresult ParseQueryBooleanString(const nsCString& aString,
bool* aValue);
// query getters
typedef NS_STDCALL_FUNCPROTO(nsresult, BoolQueryGetter, nsINavHistoryQuery,
GetOnlyBookmarked, (bool*));
typedef NS_STDCALL_FUNCPROTO(nsresult, Uint32QueryGetter, nsINavHistoryQuery,
GetBeginTimeReference, (uint32_t*));
typedef NS_STDCALL_FUNCPROTO(nsresult, Int64QueryGetter, nsINavHistoryQuery,
GetBeginTime, (int64_t*));
static void AppendBoolKeyValueIfTrue(nsACString& aString,
const nsCString& aName,
nsINavHistoryQuery* aQuery,
BoolQueryGetter getter);
static void AppendUint32KeyValueIfNonzero(nsACString& aString,
const nsCString& aName,
nsINavHistoryQuery* aQuery,
Uint32QueryGetter getter);
static void AppendInt64KeyValueIfNonzero(nsACString& aString,
const nsCString& aName,
nsINavHistoryQuery* aQuery,
Int64QueryGetter getter);
// query setters
typedef NS_STDCALL_FUNCPROTO(nsresult, BoolQuerySetter, nsINavHistoryQuery,
SetOnlyBookmarked, (bool));
typedef NS_STDCALL_FUNCPROTO(nsresult, Uint32QuerySetter, nsINavHistoryQuery,
SetBeginTimeReference, (uint32_t));
typedef NS_STDCALL_FUNCPROTO(nsresult, Int64QuerySetter, nsINavHistoryQuery,
SetBeginTime, (int64_t));
static void SetQueryKeyBool(const nsCString& aValue, nsINavHistoryQuery* aQuery,
BoolQuerySetter setter);
static void SetQueryKeyUint32(const nsCString& aValue, nsINavHistoryQuery* aQuery,
Uint32QuerySetter setter);
static void SetQueryKeyInt64(const nsCString& aValue, nsINavHistoryQuery* aQuery,
Int64QuerySetter setter);
// options setters
typedef NS_STDCALL_FUNCPROTO(nsresult, BoolOptionsSetter,
nsINavHistoryQueryOptions,
SetExpandQueries, (bool));
typedef NS_STDCALL_FUNCPROTO(nsresult, Uint32OptionsSetter,
nsINavHistoryQueryOptions,
SetMaxResults, (uint32_t));
typedef NS_STDCALL_FUNCPROTO(nsresult, Uint16OptionsSetter,
nsINavHistoryQueryOptions,
SetResultType, (uint16_t));
static void SetOptionsKeyBool(const nsCString& aValue,
nsINavHistoryQueryOptions* aOptions,
BoolOptionsSetter setter);
static void SetOptionsKeyUint16(const nsCString& aValue,
nsINavHistoryQueryOptions* aOptions,
Uint16OptionsSetter setter);
static void SetOptionsKeyUint32(const nsCString& aValue,
nsINavHistoryQueryOptions* aOptions,
Uint32OptionsSetter setter);
// Components of a query string.
// Note that query strings are also generated in nsNavBookmarks::GetFolderURI
// for performance reasons, so if you change these values, change that, too.
#define QUERYKEY_BEGIN_TIME "beginTime"
#define QUERYKEY_BEGIN_TIME_REFERENCE "beginTimeRef"
#define QUERYKEY_END_TIME "endTime"
#define QUERYKEY_END_TIME_REFERENCE "endTimeRef"
#define QUERYKEY_SEARCH_TERMS "terms"
#define QUERYKEY_MIN_VISITS "minVisits"
#define QUERYKEY_MAX_VISITS "maxVisits"
#define QUERYKEY_ONLY_BOOKMARKED "onlyBookmarked"
#define QUERYKEY_DOMAIN_IS_HOST "domainIsHost"
#define QUERYKEY_DOMAIN "domain"
#define QUERYKEY_FOLDER "folder"
#define QUERYKEY_NOTANNOTATION "!annotation"
#define QUERYKEY_ANNOTATION "annotation"
#define QUERYKEY_URI "uri"
#define QUERYKEY_SEPARATOR "OR"
#define QUERYKEY_GROUP "group"
#define QUERYKEY_SORT "sort"
#define QUERYKEY_SORTING_ANNOTATION "sortingAnnotation"
#define QUERYKEY_RESULT_TYPE "type"
#define QUERYKEY_EXCLUDE_ITEMS "excludeItems"
#define QUERYKEY_EXCLUDE_QUERIES "excludeQueries"
#define QUERYKEY_EXCLUDE_READ_ONLY_FOLDERS "excludeReadOnlyFolders"
#define QUERYKEY_EXPAND_QUERIES "expandQueries"
#define QUERYKEY_FORCE_ORIGINAL_TITLE "originalTitle"
#define QUERYKEY_INCLUDE_HIDDEN "includeHidden"
#define QUERYKEY_MAX_RESULTS "maxResults"
#define QUERYKEY_QUERY_TYPE "queryType"
#define QUERYKEY_TAG "tag"
#define QUERYKEY_NOTTAGS "!tags"
#define QUERYKEY_ASYNC_ENABLED "asyncEnabled"
#define QUERYKEY_TRANSITION "transition"
inline void AppendAmpersandIfNonempty(nsACString& aString)
{
if (! aString.IsEmpty())
aString.Append('&');
}
inline void AppendInt16(nsACString& str, int16_t i)
{
nsAutoCString tmp;
tmp.AppendInt(i);
str.Append(tmp);
}
inline void AppendInt32(nsACString& str, int32_t i)
{
nsAutoCString tmp;
tmp.AppendInt(i);
str.Append(tmp);
}
inline void AppendInt64(nsACString& str, int64_t i)
{
nsCString tmp;
tmp.AppendInt(i);
str.Append(tmp);
}
namespace PlacesFolderConversion {
#define PLACES_ROOT_FOLDER "PLACES_ROOT"
#define BOOKMARKS_MENU_FOLDER "BOOKMARKS_MENU"
#define TAGS_FOLDER "TAGS"
#define UNFILED_BOOKMARKS_FOLDER "UNFILED_BOOKMARKS"
#define TOOLBAR_FOLDER "TOOLBAR"
#define MOBILE_BOOKMARKS_FOLDER "MOBILE_BOOKMARKS"
/**
* Converts a folder name to a folder id.
*
* @param aName
* The name of the folder to convert to a folder id.
* @returns the folder id if aName is a recognizable name, -1 otherwise.
*/
inline int64_t DecodeFolder(const nsCString &aName)
{
nsNavBookmarks *bs = nsNavBookmarks::GetBookmarksService();
NS_ENSURE_TRUE(bs, false);
int64_t folderID = -1;
if (aName.EqualsLiteral(PLACES_ROOT_FOLDER))
(void)bs->GetPlacesRoot(&folderID);
else if (aName.EqualsLiteral(BOOKMARKS_MENU_FOLDER))
(void)bs->GetBookmarksMenuFolder(&folderID);
else if (aName.EqualsLiteral(TAGS_FOLDER))
(void)bs->GetTagsFolder(&folderID);
else if (aName.EqualsLiteral(UNFILED_BOOKMARKS_FOLDER))
(void)bs->GetUnfiledBookmarksFolder(&folderID);
else if (aName.EqualsLiteral(TOOLBAR_FOLDER))
(void)bs->GetToolbarFolder(&folderID);
else if (aName.EqualsLiteral(MOBILE_BOOKMARKS_FOLDER))
(void)bs->GetMobileFolder(&folderID);
return folderID;
}
/**
* Converts a folder id to a named constant, or a string representation of the
* folder id if there is no named constant for the folder, and appends it to
* aQuery.
*
* @param aQuery
* The string to append the folder string to. This is generally a
* query string, but could really be anything.
* @param aFolderID
* The folder ID to convert to the proper named constant.
*/
inline nsresult AppendFolder(nsCString &aQuery, int64_t aFolderID)
{
nsNavBookmarks *bs = nsNavBookmarks::GetBookmarksService();
NS_ENSURE_STATE(bs);
int64_t folderID;
if (NS_SUCCEEDED(bs->GetPlacesRoot(&folderID)) &&
aFolderID == folderID) {
aQuery.AppendLiteral(PLACES_ROOT_FOLDER);
}
else if (NS_SUCCEEDED(bs->GetBookmarksMenuFolder(&folderID)) &&
aFolderID == folderID) {
aQuery.AppendLiteral(BOOKMARKS_MENU_FOLDER);
}
else if (NS_SUCCEEDED(bs->GetTagsFolder(&folderID)) &&
aFolderID == folderID) {
aQuery.AppendLiteral(TAGS_FOLDER);
}
else if (NS_SUCCEEDED(bs->GetUnfiledBookmarksFolder(&folderID)) &&
aFolderID == folderID) {
aQuery.AppendLiteral(UNFILED_BOOKMARKS_FOLDER);
}
else if (NS_SUCCEEDED(bs->GetToolbarFolder(&folderID)) &&
aFolderID == folderID) {
aQuery.AppendLiteral(TOOLBAR_FOLDER);
}
else if (NS_SUCCEEDED(bs->GetMobileFolder(&folderID)) &&
aFolderID == folderID) {
aQuery.AppendLiteral(MOBILE_BOOKMARKS_FOLDER);
}
else {
// It wasn't one of our named constants, so just convert it to a string.
aQuery.AppendInt(aFolderID);
}
return NS_OK;
}
} // namespace PlacesFolderConversion
// nsNavHistory::QueryStringToQueries
//
// From C++ places code, you should use QueryStringToQueryArray, this is
// the harder-to-use XPCOM version.
NS_IMETHODIMP
nsNavHistory::QueryStringToQueries(const nsACString& aQueryString,
nsINavHistoryQuery*** aQueries,
uint32_t* aResultCount,
nsINavHistoryQueryOptions** aOptions)
{
NS_ENSURE_ARG_POINTER(aQueries);
NS_ENSURE_ARG_POINTER(aResultCount);
NS_ENSURE_ARG_POINTER(aOptions);
*aQueries = nullptr;
*aResultCount = 0;
nsCOMPtr<nsNavHistoryQueryOptions> options;
nsCOMArray<nsNavHistoryQuery> queries;
nsresult rv = QueryStringToQueryArray(aQueryString, &queries,
getter_AddRefs(options));
NS_ENSURE_SUCCESS(rv, rv);
*aResultCount = queries.Count();
if (queries.Count() > 0) {
// convert COM array to raw
*aQueries = static_cast<nsINavHistoryQuery**>
(moz_xmalloc(sizeof(nsINavHistoryQuery*) * queries.Count()));
NS_ENSURE_TRUE(*aQueries, NS_ERROR_OUT_OF_MEMORY);
for (int32_t i = 0; i < queries.Count(); i ++) {
(*aQueries)[i] = queries[i];
NS_ADDREF((*aQueries)[i]);
}
}
options.forget(aOptions);
return NS_OK;
}
// nsNavHistory::QueryStringToQueryArray
//
// An internal version of QueryStringToQueries that fills a COM array for
// ease-of-use.
nsresult
nsNavHistory::QueryStringToQueryArray(const nsACString& aQueryString,
nsCOMArray<nsNavHistoryQuery>* aQueries,
nsNavHistoryQueryOptions** aOptions)
{
nsresult rv;
aQueries->Clear();
*aOptions = nullptr;
RefPtr<nsNavHistoryQueryOptions> options(new nsNavHistoryQueryOptions());
if (! options)
return NS_ERROR_OUT_OF_MEMORY;
nsTArray<QueryKeyValuePair> tokens;
rv = TokenizeQueryString(aQueryString, &tokens);
NS_ENSURE_SUCCESS(rv, rv);
rv = TokensToQueries(tokens, aQueries, options);
if (NS_FAILED(rv)) {
NS_WARNING("Unable to parse the query string: ");
NS_WARNING(PromiseFlatCString(aQueryString).get());
return rv;
}
options.forget(aOptions);
return NS_OK;
}
// nsNavHistory::QueriesToQueryString
NS_IMETHODIMP
nsNavHistory::QueriesToQueryString(nsINavHistoryQuery **aQueries,
uint32_t aQueryCount,
nsINavHistoryQueryOptions* aOptions,
nsACString& aQueryString)
{
NS_ENSURE_ARG(aQueries);
NS_ENSURE_ARG(aOptions);
nsCOMPtr<nsNavHistoryQueryOptions> options = do_QueryInterface(aOptions);
NS_ENSURE_TRUE(options, NS_ERROR_INVALID_ARG);
nsAutoCString queryString;
for (uint32_t queryIndex = 0; queryIndex < aQueryCount; queryIndex ++) {
nsCOMPtr<nsNavHistoryQuery> query = do_QueryInterface(aQueries[queryIndex]);
if (queryIndex > 0) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_SEPARATOR);
}
bool hasIt;
// begin time
query->GetHasBeginTime(&hasIt);
if (hasIt) {
AppendInt64KeyValueIfNonzero(queryString,
NS_LITERAL_CSTRING(QUERYKEY_BEGIN_TIME),
query, &nsINavHistoryQuery::GetBeginTime);
AppendUint32KeyValueIfNonzero(queryString,
NS_LITERAL_CSTRING(QUERYKEY_BEGIN_TIME_REFERENCE),
query, &nsINavHistoryQuery::GetBeginTimeReference);
}
// end time
query->GetHasEndTime(&hasIt);
if (hasIt) {
AppendInt64KeyValueIfNonzero(queryString,
NS_LITERAL_CSTRING(QUERYKEY_END_TIME),
query, &nsINavHistoryQuery::GetEndTime);
AppendUint32KeyValueIfNonzero(queryString,
NS_LITERAL_CSTRING(QUERYKEY_END_TIME_REFERENCE),
query, &nsINavHistoryQuery::GetEndTimeReference);
}
// search terms
query->GetHasSearchTerms(&hasIt);
if (hasIt) {
nsAutoString searchTerms;
query->GetSearchTerms(searchTerms);
nsCString escapedTerms;
if (! NS_Escape(NS_ConvertUTF16toUTF8(searchTerms), escapedTerms,
url_XAlphas))
return NS_ERROR_OUT_OF_MEMORY;
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_SEARCH_TERMS "=");
queryString += escapedTerms;
}
// min and max visits
int32_t minVisits;
if (NS_SUCCEEDED(query->GetMinVisits(&minVisits)) && minVisits >= 0) {
AppendAmpersandIfNonempty(queryString);
queryString.AppendLiteral(QUERYKEY_MIN_VISITS "=");
AppendInt32(queryString, minVisits);
}
int32_t maxVisits;
if (NS_SUCCEEDED(query->GetMaxVisits(&maxVisits)) && maxVisits >= 0) {
AppendAmpersandIfNonempty(queryString);
queryString.AppendLiteral(QUERYKEY_MAX_VISITS "=");
AppendInt32(queryString, maxVisits);
}
// only bookmarked
AppendBoolKeyValueIfTrue(queryString,
NS_LITERAL_CSTRING(QUERYKEY_ONLY_BOOKMARKED),
query, &nsINavHistoryQuery::GetOnlyBookmarked);
// domain (+ is host), only call if hasDomain, which means non-IsVoid
// this means we may get an empty string for the domain in the result,
// which is valid
query->GetHasDomain(&hasIt);
if (hasIt) {
AppendBoolKeyValueIfTrue(queryString,
NS_LITERAL_CSTRING(QUERYKEY_DOMAIN_IS_HOST),
query, &nsINavHistoryQuery::GetDomainIsHost);
nsAutoCString domain;
nsresult rv = query->GetDomain(domain);
NS_ENSURE_SUCCESS(rv, rv);
nsCString escapedDomain;
bool success = NS_Escape(domain, escapedDomain, url_XAlphas);
NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
AppendAmpersandIfNonempty(queryString);
queryString.AppendLiteral(QUERYKEY_DOMAIN "=");
queryString.Append(escapedDomain);
}
// uri
query->GetHasUri(&hasIt);
if (hasIt) {
nsCOMPtr<nsIURI> uri;
query->GetUri(getter_AddRefs(uri));
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE); // hasURI should tell is if invalid
nsAutoCString uriSpec;
nsresult rv = uri->GetSpec(uriSpec);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString escaped;
bool success = NS_Escape(uriSpec, escaped, url_XAlphas);
NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
AppendAmpersandIfNonempty(queryString);
queryString.AppendLiteral(QUERYKEY_URI "=");
queryString.Append(escaped);
}
// annotation
query->GetHasAnnotation(&hasIt);
if (hasIt) {
AppendAmpersandIfNonempty(queryString);
bool annotationIsNot;
query->GetAnnotationIsNot(&annotationIsNot);
if (annotationIsNot)
queryString.AppendLiteral(QUERYKEY_NOTANNOTATION "=");
else
queryString.AppendLiteral(QUERYKEY_ANNOTATION "=");
nsAutoCString annot;
query->GetAnnotation(annot);
nsAutoCString escaped;
bool success = NS_Escape(annot, escaped, url_XAlphas);
NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
queryString.Append(escaped);
}
// folders
int64_t *folders = nullptr;
uint32_t folderCount = 0;
query->GetFolders(&folderCount, &folders);
for (uint32_t i = 0; i < folderCount; ++i) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_FOLDER "=");
nsresult rv = PlacesFolderConversion::AppendFolder(queryString, folders[i]);
NS_ENSURE_SUCCESS(rv, rv);
}
free(folders);
// tags
const nsTArray<nsString> &tags = query->Tags();
for (uint32_t i = 0; i < tags.Length(); ++i) {
nsAutoCString escapedTag;
if (!NS_Escape(NS_ConvertUTF16toUTF8(tags[i]), escapedTag, url_XAlphas))
return NS_ERROR_OUT_OF_MEMORY;
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_TAG "=");
queryString += escapedTag;
}
AppendBoolKeyValueIfTrue(queryString,
NS_LITERAL_CSTRING(QUERYKEY_NOTTAGS),
query,
&nsINavHistoryQuery::GetTagsAreNot);
// transitions
const nsTArray<uint32_t>& transitions = query->Transitions();
for (uint32_t i = 0; i < transitions.Length(); ++i) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_TRANSITION "=");
AppendInt64(queryString, transitions[i]);
}
}
// sorting
if (options->SortingMode() != nsINavHistoryQueryOptions::SORT_BY_NONE) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_SORT "=");
AppendInt16(queryString, options->SortingMode());
if (options->SortingMode() == nsINavHistoryQueryOptions::SORT_BY_ANNOTATION_DESCENDING ||
options->SortingMode() == nsINavHistoryQueryOptions::SORT_BY_ANNOTATION_ASCENDING) {
// sortingAnnotation
nsAutoCString sortingAnnotation;
if (NS_SUCCEEDED(options->GetSortingAnnotation(sortingAnnotation))) {
nsCString escaped;
if (!NS_Escape(sortingAnnotation, escaped, url_XAlphas))
return NS_ERROR_OUT_OF_MEMORY;
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_SORTING_ANNOTATION "=");
queryString.Append(escaped);
}
}
}
// result type
if (options->ResultType() != nsINavHistoryQueryOptions::RESULTS_AS_URI) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_RESULT_TYPE "=");
AppendInt16(queryString, options->ResultType());
}
// exclude items
if (options->ExcludeItems()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_EXCLUDE_ITEMS "=1");
}
// exclude queries
if (options->ExcludeQueries()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_EXCLUDE_QUERIES "=1");
}
// exclude read only folders
if (options->ExcludeReadOnlyFolders()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_EXCLUDE_READ_ONLY_FOLDERS "=1");
}
// expand queries
if (!options->ExpandQueries()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_EXPAND_QUERIES "=0");
}
// include hidden
if (options->IncludeHidden()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_INCLUDE_HIDDEN "=1");
}
// max results
if (options->MaxResults()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_MAX_RESULTS "=");
AppendInt32(queryString, options->MaxResults());
}
// queryType
if (options->QueryType() != nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_QUERY_TYPE "=");
AppendInt16(queryString, options->QueryType());
}
// async enabled
if (options->AsyncEnabled()) {
AppendAmpersandIfNonempty(queryString);
queryString += NS_LITERAL_CSTRING(QUERYKEY_ASYNC_ENABLED "=1");
}
aQueryString.AssignLiteral("place:");
aQueryString.Append(queryString);
return NS_OK;
}
// TokenizeQueryString
nsresult
TokenizeQueryString(const nsACString& aQuery,
nsTArray<QueryKeyValuePair>* aTokens)
{
// Strip off the "place:" prefix
const uint32_t prefixlen = 6; // = strlen("place:");
nsCString query;
if (aQuery.Length() >= prefixlen &&
Substring(aQuery, 0, prefixlen).EqualsLiteral("place:"))
query = Substring(aQuery, prefixlen);
else
query = aQuery;
int32_t keyFirstIndex = 0;
int32_t equalsIndex = 0;
for (uint32_t i = 0; i < query.Length(); i ++) {
if (query[i] == '&') {
// new clause, save last one
if (i - keyFirstIndex > 1) {
if (! aTokens->AppendElement(QueryKeyValuePair(query, keyFirstIndex,
equalsIndex, i)))
return NS_ERROR_OUT_OF_MEMORY;
}
keyFirstIndex = equalsIndex = i + 1;
} else if (query[i] == '=') {
equalsIndex = i;
}
}
// handle last pair, if any
if (query.Length() - keyFirstIndex > 1) {
if (! aTokens->AppendElement(QueryKeyValuePair(query, keyFirstIndex,
equalsIndex, query.Length())))
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
// nsNavHistory::TokensToQueries
nsresult
nsNavHistory::TokensToQueries(const nsTArray<QueryKeyValuePair>& aTokens,
nsCOMArray<nsNavHistoryQuery>* aQueries,
nsNavHistoryQueryOptions* aOptions)
{
nsresult rv;
nsCOMPtr<nsNavHistoryQuery> query(new nsNavHistoryQuery());
if (! query)
return NS_ERROR_OUT_OF_MEMORY;
if (! aQueries->AppendObject(query))
return NS_ERROR_OUT_OF_MEMORY;
if (aTokens.Length() == 0)
return NS_OK; // nothing to do
nsTArray<int64_t> folders;
nsTArray<nsString> tags;
nsTArray<uint32_t> transitions;
for (uint32_t i = 0; i < aTokens.Length(); i ++) {
const QueryKeyValuePair& kvp = aTokens[i];
// begin time
if (kvp.key.EqualsLiteral(QUERYKEY_BEGIN_TIME)) {
SetQueryKeyInt64(kvp.value, query, &nsINavHistoryQuery::SetBeginTime);
// begin time reference
} else if (kvp.key.EqualsLiteral(QUERYKEY_BEGIN_TIME_REFERENCE)) {
SetQueryKeyUint32(kvp.value, query, &nsINavHistoryQuery::SetBeginTimeReference);
// end time
} else if (kvp.key.EqualsLiteral(QUERYKEY_END_TIME)) {
SetQueryKeyInt64(kvp.value, query, &nsINavHistoryQuery::SetEndTime);
// end time reference
} else if (kvp.key.EqualsLiteral(QUERYKEY_END_TIME_REFERENCE)) {
SetQueryKeyUint32(kvp.value, query, &nsINavHistoryQuery::SetEndTimeReference);
// search terms
} else if (kvp.key.EqualsLiteral(QUERYKEY_SEARCH_TERMS)) {
nsCString unescapedTerms = kvp.value;
NS_UnescapeURL(unescapedTerms); // modifies input
rv = query->SetSearchTerms(NS_ConvertUTF8toUTF16(unescapedTerms));
NS_ENSURE_SUCCESS(rv, rv);
// min visits
} else if (kvp.key.EqualsLiteral(QUERYKEY_MIN_VISITS)) {
int32_t visits = kvp.value.ToInteger(&rv);
if (NS_SUCCEEDED(rv))
query->SetMinVisits(visits);
else
NS_WARNING("Bad number for minVisits in query");
// max visits
} else if (kvp.key.EqualsLiteral(QUERYKEY_MAX_VISITS)) {
int32_t visits = kvp.value.ToInteger(&rv);
if (NS_SUCCEEDED(rv))
query->SetMaxVisits(visits);
else
NS_WARNING("Bad number for maxVisits in query");
// onlyBookmarked flag
} else if (kvp.key.EqualsLiteral(QUERYKEY_ONLY_BOOKMARKED)) {
SetQueryKeyBool(kvp.value, query, &nsINavHistoryQuery::SetOnlyBookmarked);
// domainIsHost flag
} else if (kvp.key.EqualsLiteral(QUERYKEY_DOMAIN_IS_HOST)) {
SetQueryKeyBool(kvp.value, query, &nsINavHistoryQuery::SetDomainIsHost);
// domain string
} else if (kvp.key.EqualsLiteral(QUERYKEY_DOMAIN)) {
nsAutoCString unescapedDomain(kvp.value);
NS_UnescapeURL(unescapedDomain); // modifies input
rv = query->SetDomain(unescapedDomain);
NS_ENSURE_SUCCESS(rv, rv);
// folders
} else if (kvp.key.EqualsLiteral(QUERYKEY_FOLDER)) {
int64_t folder;
if (PR_sscanf(kvp.value.get(), "%lld", &folder) == 1) {
NS_ENSURE_TRUE(folders.AppendElement(folder), NS_ERROR_OUT_OF_MEMORY);
} else {
folder = PlacesFolderConversion::DecodeFolder(kvp.value);
if (folder != -1)
NS_ENSURE_TRUE(folders.AppendElement(folder), NS_ERROR_OUT_OF_MEMORY);
else
NS_WARNING("folders value in query is invalid, ignoring");
}
// uri
} else if (kvp.key.EqualsLiteral(QUERYKEY_URI)) {
nsAutoCString unescapedUri(kvp.value);
NS_UnescapeURL(unescapedUri); // modifies input
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), unescapedUri);
if (NS_FAILED(rv)) {
NS_WARNING("Unable to parse URI");
}
rv = query->SetUri(uri);
NS_ENSURE_SUCCESS(rv, rv);
// not annotation
} else if (kvp.key.EqualsLiteral(QUERYKEY_NOTANNOTATION)) {
nsAutoCString unescaped(kvp.value);
NS_UnescapeURL(unescaped); // modifies input
query->SetAnnotationIsNot(true);
query->SetAnnotation(unescaped);
// annotation
} else if (kvp.key.EqualsLiteral(QUERYKEY_ANNOTATION)) {
nsAutoCString unescaped(kvp.value);
NS_UnescapeURL(unescaped); // modifies input
query->SetAnnotationIsNot(false);
query->SetAnnotation(unescaped);
// tag
} else if (kvp.key.EqualsLiteral(QUERYKEY_TAG)) {
nsAutoCString unescaped(kvp.value);
NS_UnescapeURL(unescaped); // modifies input
NS_ConvertUTF8toUTF16 tag(unescaped);
if (!tags.Contains(tag)) {
NS_ENSURE_TRUE(tags.AppendElement(tag), NS_ERROR_OUT_OF_MEMORY);
}
// not tags
} else if (kvp.key.EqualsLiteral(QUERYKEY_NOTTAGS)) {
SetQueryKeyBool(kvp.value, query, &nsINavHistoryQuery::SetTagsAreNot);
// transition
} else if (kvp.key.EqualsLiteral(QUERYKEY_TRANSITION)) {
uint32_t transition = kvp.value.ToInteger(&rv);
if (NS_SUCCEEDED(rv)) {
if (!transitions.Contains(transition))
NS_ENSURE_TRUE(transitions.AppendElement(transition),
NS_ERROR_OUT_OF_MEMORY);
}
else {
NS_WARNING("Invalid Int32 transition value.");
}
// new query component
} else if (kvp.key.EqualsLiteral(QUERYKEY_SEPARATOR)) {
if (folders.Length() != 0) {
query->SetFolders(folders.Elements(), folders.Length());
folders.Clear();
}
if (tags.Length() > 0) {
rv = query->SetTags(tags);
NS_ENSURE_SUCCESS(rv, rv);
tags.Clear();
}
if (transitions.Length() > 0) {
rv = query->SetTransitions(transitions);
NS_ENSURE_SUCCESS(rv, rv);
transitions.Clear();
}
query = new nsNavHistoryQuery();
if (! query)
return NS_ERROR_OUT_OF_MEMORY;
if (! aQueries->AppendObject(query))
return NS_ERROR_OUT_OF_MEMORY;
// sorting mode
} else if (kvp.key.EqualsLiteral(QUERYKEY_SORT)) {
SetOptionsKeyUint16(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetSortingMode);
// sorting annotation
} else if (kvp.key.EqualsLiteral(QUERYKEY_SORTING_ANNOTATION)) {
nsCString sortingAnnotation = kvp.value;
NS_UnescapeURL(sortingAnnotation);
rv = aOptions->SetSortingAnnotation(sortingAnnotation);
NS_ENSURE_SUCCESS(rv, rv);
// result type
} else if (kvp.key.EqualsLiteral(QUERYKEY_RESULT_TYPE)) {
SetOptionsKeyUint16(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetResultType);
// exclude items
} else if (kvp.key.EqualsLiteral(QUERYKEY_EXCLUDE_ITEMS)) {
SetOptionsKeyBool(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetExcludeItems);
// exclude queries
} else if (kvp.key.EqualsLiteral(QUERYKEY_EXCLUDE_QUERIES)) {
SetOptionsKeyBool(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetExcludeQueries);
// exclude read only folders
} else if (kvp.key.EqualsLiteral(QUERYKEY_EXCLUDE_READ_ONLY_FOLDERS)) {
SetOptionsKeyBool(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetExcludeReadOnlyFolders);
// expand queries
} else if (kvp.key.EqualsLiteral(QUERYKEY_EXPAND_QUERIES)) {
SetOptionsKeyBool(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetExpandQueries);
// include hidden
} else if (kvp.key.EqualsLiteral(QUERYKEY_INCLUDE_HIDDEN)) {
SetOptionsKeyBool(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetIncludeHidden);
// max results
} else if (kvp.key.EqualsLiteral(QUERYKEY_MAX_RESULTS)) {
SetOptionsKeyUint32(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetMaxResults);
// query type
} else if (kvp.key.EqualsLiteral(QUERYKEY_QUERY_TYPE)) {
SetOptionsKeyUint16(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetQueryType);
// async enabled
} else if (kvp.key.EqualsLiteral(QUERYKEY_ASYNC_ENABLED)) {
SetOptionsKeyBool(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetAsyncEnabled);
// unknown key
} else {
NS_WARNING("TokensToQueries(), ignoring unknown key: ");
NS_WARNING(kvp.key.get());
}
}
if (folders.Length() != 0)
query->SetFolders(folders.Elements(), folders.Length());
if (tags.Length() > 0) {
rv = query->SetTags(tags);
NS_ENSURE_SUCCESS(rv, rv);
}
if (transitions.Length() > 0) {
rv = query->SetTransitions(transitions);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
// ParseQueryBooleanString
//
// Converts a 0/1 or true/false string into a bool
nsresult
ParseQueryBooleanString(const nsCString& aString, bool* aValue)
{
if (aString.EqualsLiteral("1") || aString.EqualsLiteral("true")) {
*aValue = true;
return NS_OK;
} else if (aString.EqualsLiteral("0") || aString.EqualsLiteral("false")) {
*aValue = false;
return NS_OK;
}
return NS_ERROR_INVALID_ARG;
}
// nsINavHistoryQuery **********************************************************
NS_IMPL_ISUPPORTS(nsNavHistoryQuery, nsNavHistoryQuery, nsINavHistoryQuery)
// nsINavHistoryQuery::nsNavHistoryQuery
//
// This must initialize the object such that the default values will cause
// all history to be returned if this query is used. Then the caller can
// just set the things it's interested in.
nsNavHistoryQuery::nsNavHistoryQuery()
: mMinVisits(-1), mMaxVisits(-1), mBeginTime(0),
mBeginTimeReference(TIME_RELATIVE_EPOCH),
mEndTime(0), mEndTimeReference(TIME_RELATIVE_EPOCH),
mOnlyBookmarked(false),
mDomainIsHost(false),
mAnnotationIsNot(false),
mTagsAreNot(false)
{
// differentiate not set (IsVoid) from empty string (local files)
mDomain.SetIsVoid(true);
}
nsNavHistoryQuery::nsNavHistoryQuery(const nsNavHistoryQuery& aOther)
: mMinVisits(aOther.mMinVisits), mMaxVisits(aOther.mMaxVisits),
mBeginTime(aOther.mBeginTime),
mBeginTimeReference(aOther.mBeginTimeReference),
mEndTime(aOther.mEndTime), mEndTimeReference(aOther.mEndTimeReference),
mSearchTerms(aOther.mSearchTerms), mOnlyBookmarked(aOther.mOnlyBookmarked),
mDomainIsHost(aOther.mDomainIsHost), mDomain(aOther.mDomain),
mUri(aOther.mUri),
mAnnotationIsNot(aOther.mAnnotationIsNot),
mAnnotation(aOther.mAnnotation), mTags(aOther.mTags),
mTagsAreNot(aOther.mTagsAreNot), mTransitions(aOther.mTransitions)
{}
NS_IMETHODIMP nsNavHistoryQuery::GetBeginTime(PRTime *aBeginTime)
{
*aBeginTime = mBeginTime;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetBeginTime(PRTime aBeginTime)
{
mBeginTime = aBeginTime;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetBeginTimeReference(uint32_t* _retval)
{
*_retval = mBeginTimeReference;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetBeginTimeReference(uint32_t aReference)
{
if (aReference > TIME_RELATIVE_NOW)
return NS_ERROR_INVALID_ARG;
mBeginTimeReference = aReference;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetHasBeginTime(bool* _retval)
{
*_retval = ! (mBeginTimeReference == TIME_RELATIVE_EPOCH && mBeginTime == 0);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetAbsoluteBeginTime(PRTime* _retval)
{
*_retval = nsNavHistory::NormalizeTime(mBeginTimeReference, mBeginTime);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetEndTime(PRTime *aEndTime)
{
*aEndTime = mEndTime;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetEndTime(PRTime aEndTime)
{
mEndTime = aEndTime;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetEndTimeReference(uint32_t* _retval)
{
*_retval = mEndTimeReference;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetEndTimeReference(uint32_t aReference)
{
if (aReference > TIME_RELATIVE_NOW)
return NS_ERROR_INVALID_ARG;
mEndTimeReference = aReference;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetHasEndTime(bool* _retval)
{
*_retval = ! (mEndTimeReference == TIME_RELATIVE_EPOCH && mEndTime == 0);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetAbsoluteEndTime(PRTime* _retval)
{
*_retval = nsNavHistory::NormalizeTime(mEndTimeReference, mEndTime);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetSearchTerms(nsAString& aSearchTerms)
{
aSearchTerms = mSearchTerms;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetSearchTerms(const nsAString& aSearchTerms)
{
mSearchTerms = aSearchTerms;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetHasSearchTerms(bool* _retval)
{
*_retval = (! mSearchTerms.IsEmpty());
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetMinVisits(int32_t* _retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = mMinVisits;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetMinVisits(int32_t aVisits)
{
mMinVisits = aVisits;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetMaxVisits(int32_t* _retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = mMaxVisits;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetMaxVisits(int32_t aVisits)
{
mMaxVisits = aVisits;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetOnlyBookmarked(bool *aOnlyBookmarked)
{
*aOnlyBookmarked = mOnlyBookmarked;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetOnlyBookmarked(bool aOnlyBookmarked)
{
mOnlyBookmarked = aOnlyBookmarked;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetDomainIsHost(bool *aDomainIsHost)
{
*aDomainIsHost = mDomainIsHost;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetDomainIsHost(bool aDomainIsHost)
{
mDomainIsHost = aDomainIsHost;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetDomain(nsACString& aDomain)
{
aDomain = mDomain;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetDomain(const nsACString& aDomain)
{
mDomain = aDomain;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetHasDomain(bool* _retval)
{
// note that empty but not void is still a valid query (local files)
*_retval = (! mDomain.IsVoid());
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetUri(nsIURI** aUri)
{
NS_IF_ADDREF(*aUri = mUri);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetUri(nsIURI* aUri)
{
mUri = aUri;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetHasUri(bool* aHasUri)
{
*aHasUri = (mUri != nullptr);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetAnnotationIsNot(bool* aIsNot)
{
*aIsNot = mAnnotationIsNot;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetAnnotationIsNot(bool aIsNot)
{
mAnnotationIsNot = aIsNot;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetAnnotation(nsACString& aAnnotation)
{
aAnnotation = mAnnotation;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetAnnotation(const nsACString& aAnnotation)
{
mAnnotation = aAnnotation;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetHasAnnotation(bool* aHasIt)
{
*aHasIt = ! mAnnotation.IsEmpty();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetTags(nsIVariant **aTags)
{
NS_ENSURE_ARG_POINTER(aTags);
RefPtr<nsVariant> out = new nsVariant();
uint32_t arrayLen = mTags.Length();
nsresult rv;
if (arrayLen == 0)
rv = out->SetAsEmptyArray();
else {
// Note: The resulting nsIVariant dupes both the array and its elements.
const char16_t **array = reinterpret_cast<const char16_t **>
(moz_xmalloc(arrayLen * sizeof(char16_t *)));
NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
for (uint32_t i = 0; i < arrayLen; ++i) {
array[i] = mTags[i].get();
}
rv = out->SetAsArray(nsIDataType::VTYPE_WCHAR_STR,
nullptr,
arrayLen,
reinterpret_cast<void *>(array));
free(array);
}
NS_ENSURE_SUCCESS(rv, rv);
out.forget(aTags);
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetTags(nsIVariant *aTags)
{
NS_ENSURE_ARG(aTags);
uint16_t dataType;
aTags->GetDataType(&dataType);
// Caller passed in empty array. Easy -- clear our mTags array and return.
if (dataType == nsIDataType::VTYPE_EMPTY_ARRAY) {
mTags.Clear();
return NS_OK;
}
// Before we go any further, make sure caller passed in an array.
NS_ENSURE_TRUE(dataType == nsIDataType::VTYPE_ARRAY, NS_ERROR_ILLEGAL_VALUE);
uint16_t eltType;
nsIID eltIID;
uint32_t arrayLen;
void *array;
// Convert the nsIVariant to an array. We own the resulting buffer and its
// elements.
nsresult rv = aTags->GetAsArray(&eltType, &eltIID, &arrayLen, &array);
NS_ENSURE_SUCCESS(rv, rv);
// If element type is not wstring, thanks a lot. Your memory die now.
if (eltType != nsIDataType::VTYPE_WCHAR_STR) {
switch (eltType) {
case nsIDataType::VTYPE_ID:
case nsIDataType::VTYPE_CHAR_STR:
{
char **charArray = reinterpret_cast<char **>(array);
for (uint32_t i = 0; i < arrayLen; ++i) {
if (charArray[i])
free(charArray[i]);
}
}
break;
case nsIDataType::VTYPE_INTERFACE:
case nsIDataType::VTYPE_INTERFACE_IS:
{
nsISupports **supportsArray = reinterpret_cast<nsISupports **>(array);
for (uint32_t i = 0; i < arrayLen; ++i) {
NS_IF_RELEASE(supportsArray[i]);
}
}
break;
// The other types are primitives that do not need to be freed.
}
free(array);
return NS_ERROR_ILLEGAL_VALUE;
}
char16_t **tags = reinterpret_cast<char16_t **>(array);
mTags.Clear();
// Finally, add each passed-in tag to our mTags array and then sort it.
for (uint32_t i = 0; i < arrayLen; ++i) {
// Don't allow nulls.
if (!tags[i]) {
free(tags);
return NS_ERROR_ILLEGAL_VALUE;
}
nsDependentString tag(tags[i]);
// Don't store duplicate tags. This isn't just to save memory or to be
// fancy; the SQL that's built from the tags relies on no dupes.
if (!mTags.Contains(tag)) {
if (!mTags.AppendElement(tag)) {
free(tags[i]);
free(tags);
return NS_ERROR_OUT_OF_MEMORY;
}
}
free(tags[i]);
}
free(tags);
mTags.Sort();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetTagsAreNot(bool *aTagsAreNot)
{
NS_ENSURE_ARG_POINTER(aTagsAreNot);
*aTagsAreNot = mTagsAreNot;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetTagsAreNot(bool aTagsAreNot)
{
mTagsAreNot = aTagsAreNot;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetFolders(uint32_t *aCount,
int64_t **aFolders)
{
uint32_t count = mFolders.Length();
int64_t *folders = nullptr;
if (count > 0) {
folders = static_cast<int64_t*>
(moz_xmalloc(count * sizeof(int64_t)));
NS_ENSURE_TRUE(folders, NS_ERROR_OUT_OF_MEMORY);
for (uint32_t i = 0; i < count; ++i) {
folders[i] = mFolders[i];
}
}
*aCount = count;
*aFolders = folders;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetFolderCount(uint32_t *aCount)
{
*aCount = mFolders.Length();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetFolders(const int64_t *aFolders,
uint32_t aFolderCount)
{
if (!mFolders.ReplaceElementsAt(0, mFolders.Length(),
aFolders, aFolderCount)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetTransitions(uint32_t* aCount,
uint32_t** aTransitions)
{
uint32_t count = mTransitions.Length();
uint32_t* transitions = nullptr;
if (count > 0) {
transitions = reinterpret_cast<uint32_t*>
(moz_xmalloc(count * sizeof(uint32_t)));
NS_ENSURE_TRUE(transitions, NS_ERROR_OUT_OF_MEMORY);
for (uint32_t i = 0; i < count; ++i) {
transitions[i] = mTransitions[i];
}
}
*aCount = count;
*aTransitions = transitions;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetTransitionCount(uint32_t* aCount)
{
*aCount = mTransitions.Length();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetTransitions(const uint32_t* aTransitions,
uint32_t aCount)
{
if (!mTransitions.ReplaceElementsAt(0, mTransitions.Length(), aTransitions,
aCount))
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::Clone(nsINavHistoryQuery** _retval)
{
*_retval = nullptr;
RefPtr<nsNavHistoryQuery> clone = new nsNavHistoryQuery(*this);
NS_ENSURE_TRUE(clone, NS_ERROR_OUT_OF_MEMORY);
clone.forget(_retval);
return NS_OK;
}
// nsNavHistoryQueryOptions
NS_IMPL_ISUPPORTS(nsNavHistoryQueryOptions, nsNavHistoryQueryOptions, nsINavHistoryQueryOptions)
// sortingMode
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetSortingMode(uint16_t* aMode)
{
*aMode = mSort;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetSortingMode(uint16_t aMode)
{
if (aMode > SORT_BY_FRECENCY_DESCENDING)
return NS_ERROR_INVALID_ARG;
mSort = aMode;
return NS_OK;
}
// sortingAnnotation
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetSortingAnnotation(nsACString& _result) {
_result.Assign(mSortingAnnotation);
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetSortingAnnotation(const nsACString& aSortingAnnotation) {
mSortingAnnotation.Assign(aSortingAnnotation);
return NS_OK;
}
// resultType
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetResultType(uint16_t* aType)
{
*aType = mResultType;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetResultType(uint16_t aType)
{
if (aType > RESULTS_AS_TAG_CONTENTS)
return NS_ERROR_INVALID_ARG;
// Tag queries and containers are bookmarks related, so we set the QueryType
// accordingly.
if (aType == RESULTS_AS_TAG_QUERY || aType == RESULTS_AS_TAG_CONTENTS)
mQueryType = QUERY_TYPE_BOOKMARKS;
mResultType = aType;
return NS_OK;
}
// excludeItems
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetExcludeItems(bool* aExclude)
{
*aExclude = mExcludeItems;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetExcludeItems(bool aExclude)
{
mExcludeItems = aExclude;
return NS_OK;
}
// excludeQueries
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetExcludeQueries(bool* aExclude)
{
*aExclude = mExcludeQueries;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetExcludeQueries(bool aExclude)
{
mExcludeQueries = aExclude;
return NS_OK;
}
// excludeReadOnlyFolders
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetExcludeReadOnlyFolders(bool* aExclude)
{
*aExclude = mExcludeReadOnlyFolders;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetExcludeReadOnlyFolders(bool aExclude)
{
mExcludeReadOnlyFolders = aExclude;
return NS_OK;
}
// expandQueries
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetExpandQueries(bool* aExpand)
{
*aExpand = mExpandQueries;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetExpandQueries(bool aExpand)
{
mExpandQueries = aExpand;
return NS_OK;
}
// includeHidden
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetIncludeHidden(bool* aIncludeHidden)
{
*aIncludeHidden = mIncludeHidden;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetIncludeHidden(bool aIncludeHidden)
{
mIncludeHidden = aIncludeHidden;
return NS_OK;
}
// maxResults
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetMaxResults(uint32_t* aMaxResults)
{
*aMaxResults = mMaxResults;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetMaxResults(uint32_t aMaxResults)
{
mMaxResults = aMaxResults;
return NS_OK;
}
// queryType
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetQueryType(uint16_t* _retval)
{
*_retval = mQueryType;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetQueryType(uint16_t aQueryType)
{
// Tag query and containers are forced to QUERY_TYPE_BOOKMARKS when the
// resultType is set.
if (mResultType == RESULTS_AS_TAG_CONTENTS ||
mResultType == RESULTS_AS_TAG_QUERY)
return NS_OK;
mQueryType = aQueryType;
return NS_OK;
}
// asyncEnabled
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetAsyncEnabled(bool* _asyncEnabled)
{
*_asyncEnabled = mAsyncEnabled;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetAsyncEnabled(bool aAsyncEnabled)
{
mAsyncEnabled = aAsyncEnabled;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::Clone(nsINavHistoryQueryOptions** aResult)
{
nsNavHistoryQueryOptions *clone = nullptr;
nsresult rv = Clone(&clone);
*aResult = clone;
return rv;
}
nsresult
nsNavHistoryQueryOptions::Clone(nsNavHistoryQueryOptions **aResult)
{
*aResult = nullptr;
nsNavHistoryQueryOptions *result = new nsNavHistoryQueryOptions();
RefPtr<nsNavHistoryQueryOptions> resultHolder(result);
result->mSort = mSort;
result->mResultType = mResultType;
result->mExcludeItems = mExcludeItems;
result->mExcludeQueries = mExcludeQueries;
result->mExpandQueries = mExpandQueries;
result->mMaxResults = mMaxResults;
result->mQueryType = mQueryType;
result->mParentAnnotationToExclude = mParentAnnotationToExclude;
result->mAsyncEnabled = mAsyncEnabled;
resultHolder.forget(aResult);
return NS_OK;
}
// AppendBoolKeyValueIfTrue
void // static
AppendBoolKeyValueIfTrue(nsACString& aString, const nsCString& aName,
nsINavHistoryQuery* aQuery,
BoolQueryGetter getter)
{
bool value;
DebugOnly<nsresult> rv = (aQuery->*getter)(&value);
NS_ASSERTION(NS_SUCCEEDED(rv), "Failure getting boolean value");
if (value) {
AppendAmpersandIfNonempty(aString);
aString += aName;
aString.AppendLiteral("=1");
}
}
// AppendUint32KeyValueIfNonzero
void // static
AppendUint32KeyValueIfNonzero(nsACString& aString,
const nsCString& aName,
nsINavHistoryQuery* aQuery,
Uint32QueryGetter getter)
{
uint32_t value;
DebugOnly<nsresult> rv = (aQuery->*getter)(&value);
NS_ASSERTION(NS_SUCCEEDED(rv), "Failure getting value");
if (value) {
AppendAmpersandIfNonempty(aString);
aString += aName;
// AppendInt requires a concrete string
nsAutoCString appendMe("=");
appendMe.AppendInt(value);
aString.Append(appendMe);
}
}
// AppendInt64KeyValueIfNonzero
void // static
AppendInt64KeyValueIfNonzero(nsACString& aString,
const nsCString& aName,
nsINavHistoryQuery* aQuery,
Int64QueryGetter getter)
{
PRTime value;
DebugOnly<nsresult> rv = (aQuery->*getter)(&value);
NS_ASSERTION(NS_SUCCEEDED(rv), "Failure getting value");
if (value) {
AppendAmpersandIfNonempty(aString);
aString += aName;
nsAutoCString appendMe("=");
appendMe.AppendInt(static_cast<int64_t>(value));
aString.Append(appendMe);
}
}
// SetQuery/OptionsKeyBool
void // static
SetQueryKeyBool(const nsCString& aValue, nsINavHistoryQuery* aQuery,
BoolQuerySetter setter)
{
bool value;
nsresult rv = ParseQueryBooleanString(aValue, &value);
if (NS_SUCCEEDED(rv)) {
rv = (aQuery->*setter)(value);
if (NS_FAILED(rv)) {
NS_WARNING("Error setting boolean key value");
}
} else {
NS_WARNING("Invalid boolean key value in query string.");
}
}
void // static
SetOptionsKeyBool(const nsCString& aValue, nsINavHistoryQueryOptions* aOptions,
BoolOptionsSetter setter)
{
bool value;
nsresult rv = ParseQueryBooleanString(aValue, &value);
if (NS_SUCCEEDED(rv)) {
rv = (aOptions->*setter)(value);
if (NS_FAILED(rv)) {
NS_WARNING("Error setting boolean key value");
}
} else {
NS_WARNING("Invalid boolean key value in query string.");
}
}
// SetQuery/OptionsKeyUint32
void // static
SetQueryKeyUint32(const nsCString& aValue, nsINavHistoryQuery* aQuery,
Uint32QuerySetter setter)
{
nsresult rv;
uint32_t value = aValue.ToInteger(&rv);
if (NS_SUCCEEDED(rv)) {
rv = (aQuery->*setter)(value);
if (NS_FAILED(rv)) {
NS_WARNING("Error setting Int32 key value");
}
} else {
NS_WARNING("Invalid Int32 key value in query string.");
}
}
void // static
SetOptionsKeyUint32(const nsCString& aValue, nsINavHistoryQueryOptions* aOptions,
Uint32OptionsSetter setter)
{
nsresult rv;
uint32_t value = aValue.ToInteger(&rv);
if (NS_SUCCEEDED(rv)) {
rv = (aOptions->*setter)(value);
if (NS_FAILED(rv)) {
NS_WARNING("Error setting Int32 key value");
}
} else {
NS_WARNING("Invalid Int32 key value in query string.");
}
}
void // static
SetOptionsKeyUint16(const nsCString& aValue, nsINavHistoryQueryOptions* aOptions,
Uint16OptionsSetter setter)
{
nsresult rv;
uint16_t value = static_cast<uint16_t>(aValue.ToInteger(&rv));
if (NS_SUCCEEDED(rv)) {
rv = (aOptions->*setter)(value);
if (NS_FAILED(rv)) {
NS_WARNING("Error setting Int16 key value");
}
} else {
NS_WARNING("Invalid Int16 key value in query string.");
}
}
// SetQueryKeyInt64
void SetQueryKeyInt64(const nsCString& aValue, nsINavHistoryQuery* aQuery,
Int64QuerySetter setter)
{
nsresult rv;
int64_t value;
if (PR_sscanf(aValue.get(), "%lld", &value) == 1) {
rv = (aQuery->*setter)(value);
if (NS_FAILED(rv)) {
NS_WARNING("Error setting Int64 key value");
}
} else {
NS_WARNING("Invalid Int64 value in query string.");
}
}
|