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
|
//* -*- 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 QueryStringToQuery and QueryToQueryString).
*/
#include "mozilla/DebugOnly.h"
#include "nsNavHistory.h"
#include "nsNavBookmarks.h"
#include "nsEscape.h"
#include "nsCOMArray.h"
#include "nsNetUtil.h"
#include "nsTArray.h"
#include "nsQueryObject.h"
#include "prprf.h"
#include "nsVariant.h"
using namespace mozilla;
using namespace mozilla::places;
static nsresult ParseQueryBooleanString(const nsCString& aString, bool* aValue);
// query getters
typedef decltype(&nsINavHistoryQuery::GetOnlyBookmarked) BoolQueryGetter;
typedef decltype(&nsINavHistoryQuery::GetBeginTimeReference) Uint32QueryGetter;
typedef decltype(&nsINavHistoryQuery::GetBeginTime) Int64QueryGetter;
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 decltype(&nsINavHistoryQuery::SetOnlyBookmarked) BoolQuerySetter;
typedef decltype(&nsINavHistoryQuery::SetBeginTimeReference) Uint32QuerySetter;
typedef decltype(&nsINavHistoryQuery::SetBeginTime) Int64QuerySetter;
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 decltype(&nsINavHistoryQueryOptions::SetExpandQueries)
BoolOptionsSetter;
typedef decltype(&nsINavHistoryQueryOptions::SetMaxResults) Uint32OptionsSetter;
typedef decltype(&nsINavHistoryQueryOptions::SetResultType) Uint16OptionsSetter;
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_PARENT "parent"
#define QUERYKEY_NOTANNOTATION "!annotation"
#define QUERYKEY_ANNOTATION "annotation"
#define QUERYKEY_URI "uri"
#define QUERYKEY_GROUP "group"
#define QUERYKEY_SORT "sort"
#define QUERYKEY_RESULT_TYPE "type"
#define QUERYKEY_EXCLUDE_ITEMS "excludeItems"
#define QUERYKEY_EXCLUDE_QUERIES "excludeQueries"
#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);
}
NS_IMETHODIMP
nsNavHistory::QueryStringToQuery(const nsACString& aQueryString,
nsINavHistoryQuery** _query,
nsINavHistoryQueryOptions** _options) {
NS_ENSURE_ARG_POINTER(_query);
NS_ENSURE_ARG_POINTER(_options);
nsTArray<QueryKeyValuePair> tokens;
nsresult rv = TokenizeQueryString(aQueryString, &tokens);
NS_ENSURE_SUCCESS(rv, rv);
RefPtr<nsNavHistoryQueryOptions> options = new nsNavHistoryQueryOptions();
RefPtr<nsNavHistoryQuery> query = new nsNavHistoryQuery();
rv = TokensToQuery(tokens, query, options);
MOZ_ASSERT(NS_SUCCEEDED(rv), "The query string should be valid");
if (NS_FAILED(rv)) {
NS_WARNING("Unable to parse the query string: ");
NS_WARNING(PromiseFlatCString(aQueryString).get());
}
options.forget(_options);
query.forget(_query);
return NS_OK;
}
NS_IMETHODIMP
nsNavHistory::QueryToQueryString(nsINavHistoryQuery* aQuery,
nsINavHistoryQueryOptions* aOptions,
nsACString& aQueryString) {
NS_ENSURE_ARG(aQuery);
NS_ENSURE_ARG(aOptions);
RefPtr<nsNavHistoryQuery> query = do_QueryObject(aQuery);
NS_ENSURE_STATE(query);
RefPtr<nsNavHistoryQueryOptions> options = do_QueryObject(aOptions);
NS_ENSURE_STATE(options);
nsAutoCString queryString;
bool hasIt;
// begin time
query->GetHasBeginTime(&hasIt);
if (hasIt) {
AppendInt64KeyValueIfNonzero(queryString,
nsLiteralCString(QUERYKEY_BEGIN_TIME), query,
&nsINavHistoryQuery::GetBeginTime);
AppendUint32KeyValueIfNonzero(
queryString, nsLiteralCString(QUERYKEY_BEGIN_TIME_REFERENCE), query,
&nsINavHistoryQuery::GetBeginTimeReference);
}
// end time
query->GetHasEndTime(&hasIt);
if (hasIt) {
AppendInt64KeyValueIfNonzero(queryString,
nsLiteralCString(QUERYKEY_END_TIME), query,
&nsINavHistoryQuery::GetEndTime);
AppendUint32KeyValueIfNonzero(
queryString, nsLiteralCString(QUERYKEY_END_TIME_REFERENCE), query,
&nsINavHistoryQuery::GetEndTimeReference);
}
// search terms
if (!query->SearchTerms().IsEmpty()) {
const nsString& searchTerms = query->SearchTerms();
nsCString escapedTerms;
if (!NS_Escape(NS_ConvertUTF16toUTF8(searchTerms), escapedTerms,
url_XAlphas))
return NS_ERROR_OUT_OF_MEMORY;
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(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,
nsLiteralCString(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
if (!query->Domain().IsVoid()) {
AppendBoolKeyValueIfTrue(queryString,
nsLiteralCString(QUERYKEY_DOMAIN_IS_HOST), query,
&nsINavHistoryQuery::GetDomainIsHost);
const nsCString& domain = query->Domain();
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
if (query->Uri()) {
nsCOMPtr<nsIURI> uri = query->Uri();
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
if (!query->Annotation().IsEmpty()) {
AppendAmpersandIfNonempty(queryString);
if (query->AnnotationIsNot()) {
queryString.AppendLiteral(QUERYKEY_NOTANNOTATION "=");
} else {
queryString.AppendLiteral(QUERYKEY_ANNOTATION "=");
}
const nsCString& annot = query->Annotation();
nsAutoCString escaped;
bool success = NS_Escape(annot, escaped, url_XAlphas);
NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
queryString.Append(escaped);
}
// parents
const nsTArray<nsCString>& parents = query->Parents();
for (uint32_t i = 0; i < parents.Length(); ++i) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_PARENT "=");
queryString += parents[i];
}
// 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 += nsLiteralCString(QUERYKEY_TAG "=");
queryString += escapedTag;
}
AppendBoolKeyValueIfTrue(queryString, nsLiteralCString(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 += nsLiteralCString(QUERYKEY_TRANSITION "=");
AppendInt64(queryString, transitions[i]);
}
// sorting
if (options->SortingMode() != nsINavHistoryQueryOptions::SORT_BY_NONE) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_SORT "=");
AppendInt16(queryString, options->SortingMode());
}
// result type
if (options->ResultType() != nsINavHistoryQueryOptions::RESULTS_AS_URI) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_RESULT_TYPE "=");
AppendInt16(queryString, options->ResultType());
}
// exclude items
if (options->ExcludeItems()) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_EXCLUDE_ITEMS "=1");
}
// exclude queries
if (options->ExcludeQueries()) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_EXCLUDE_QUERIES "=1");
}
// expand queries
if (!options->ExpandQueries()) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_EXPAND_QUERIES "=0");
}
// include hidden
if (options->IncludeHidden()) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_INCLUDE_HIDDEN "=1");
}
// max results
if (options->MaxResults()) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_MAX_RESULTS "=");
AppendInt32(queryString, options->MaxResults());
}
// queryType
if (options->QueryType() != nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_QUERY_TYPE "=");
AppendInt16(queryString, options->QueryType());
}
// async enabled
if (options->AsyncEnabled()) {
AppendAmpersandIfNonempty(queryString);
queryString += nsLiteralCString(QUERYKEY_ASYNC_ENABLED "=1");
}
aQueryString.AssignLiteral("place:");
aQueryString.Append(queryString);
return NS_OK;
}
nsresult nsNavHistory::TokensToQuery(const nsTArray<QueryKeyValuePair>& aTokens,
nsNavHistoryQuery* aQuery,
nsNavHistoryQueryOptions* aOptions) {
nsresult rv;
if (aTokens.Length() == 0) return NS_OK;
nsTArray<nsCString> parents;
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, aQuery, &nsINavHistoryQuery::SetBeginTime);
// begin time reference
} else if (kvp.key.EqualsLiteral(QUERYKEY_BEGIN_TIME_REFERENCE)) {
SetQueryKeyUint32(kvp.value, aQuery,
&nsINavHistoryQuery::SetBeginTimeReference);
// end time
} else if (kvp.key.EqualsLiteral(QUERYKEY_END_TIME)) {
SetQueryKeyInt64(kvp.value, aQuery, &nsINavHistoryQuery::SetEndTime);
// end time reference
} else if (kvp.key.EqualsLiteral(QUERYKEY_END_TIME_REFERENCE)) {
SetQueryKeyUint32(kvp.value, aQuery,
&nsINavHistoryQuery::SetEndTimeReference);
// search terms
} else if (kvp.key.EqualsLiteral(QUERYKEY_SEARCH_TERMS)) {
nsCString unescapedTerms = kvp.value;
NS_UnescapeURL(unescapedTerms); // modifies input
rv = aQuery->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))
aQuery->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))
aQuery->SetMaxVisits(visits);
else
NS_WARNING("Bad number for maxVisits in query");
// onlyBookmarked flag
} else if (kvp.key.EqualsLiteral(QUERYKEY_ONLY_BOOKMARKED)) {
SetQueryKeyBool(kvp.value, aQuery,
&nsINavHistoryQuery::SetOnlyBookmarked);
// domainIsHost flag
} else if (kvp.key.EqualsLiteral(QUERYKEY_DOMAIN_IS_HOST)) {
SetQueryKeyBool(kvp.value, aQuery, &nsINavHistoryQuery::SetDomainIsHost);
// domain string
} else if (kvp.key.EqualsLiteral(QUERYKEY_DOMAIN)) {
nsAutoCString unescapedDomain(kvp.value);
NS_UnescapeURL(unescapedDomain); // modifies input
rv = aQuery->SetDomain(unescapedDomain);
NS_ENSURE_SUCCESS(rv, rv);
// parent folders (guids)
} else if (kvp.key.EqualsLiteral(QUERYKEY_PARENT)) {
parents.AppendElement(kvp.value);
// 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 = aQuery->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
aQuery->SetAnnotationIsNot(true);
aQuery->SetAnnotation(unescaped);
// annotation
} else if (kvp.key.EqualsLiteral(QUERYKEY_ANNOTATION)) {
nsAutoCString unescaped(kvp.value);
NS_UnescapeURL(unescaped); // modifies input
aQuery->SetAnnotationIsNot(false);
aQuery->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)) {
tags.AppendElement(tag);
}
// not tags
} else if (kvp.key.EqualsLiteral(QUERYKEY_NOTTAGS)) {
SetQueryKeyBool(kvp.value, aQuery, &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))
transitions.AppendElement(transition);
} else {
NS_WARNING("Invalid Int32 transition value.");
}
// sorting mode
} else if (kvp.key.EqualsLiteral(QUERYKEY_SORT)) {
SetOptionsKeyUint16(kvp.value, aOptions,
&nsINavHistoryQueryOptions::SetSortingMode);
// 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);
// 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 (parents.Length() != 0) {
rv = aQuery->SetParents(parents);
NS_ENSURE_SUCCESS(rv, rv);
}
if (tags.Length() > 0) {
aQuery->SetTags(std::move(tags));
}
if (transitions.Length() > 0) {
rv = aQuery->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;
}
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),
mParents(aOther.mParents.Clone()),
mTags(aOther.mTags.Clone()),
mTagsAreNot(aOther.mTagsAreNot),
mTransitions(aOther.mTransitions.Clone()) {}
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*)));
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();
// 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)) {
// XXX(Bug 1631371) Check if this should use a fallible operation as it
// pretended earlier.
mTags.AppendElement(tag);
}
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::GetParents(nsTArray<nsCString>& aGuids) {
aGuids = mParents.Clone();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetParentCount(uint32_t* aGuidCount) {
*aGuidCount = mParents.Length();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetParents(const nsTArray<nsCString>& aGuids) {
mParents.Clear();
if (!mParents.Assign(aGuids, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetTransitions(
nsTArray<uint32_t>& aTransitions) {
aTransitions = mTransitions.Clone();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::GetTransitionCount(uint32_t* aCount) {
*aCount = mTransitions.Length();
return NS_OK;
}
NS_IMETHODIMP nsNavHistoryQuery::SetTransitions(
const nsTArray<uint32_t>& aTransitions) {
if (!mTransitions.Assign(aTransitions, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQuery::Clone(nsINavHistoryQuery** _clone) {
nsNavHistoryQuery* clone = nullptr;
Unused << Clone(&clone);
*_clone = clone;
return NS_OK;
}
nsresult nsNavHistoryQuery::Clone(nsNavHistoryQuery** _clone) {
*_clone = nullptr;
RefPtr<nsNavHistoryQuery> clone = new nsNavHistoryQuery(*this);
clone.forget(_clone);
return NS_OK;
}
// nsNavHistoryQueryOptions
NS_IMPL_ISUPPORTS(nsNavHistoryQueryOptions, nsNavHistoryQueryOptions,
nsINavHistoryQueryOptions)
nsNavHistoryQueryOptions::nsNavHistoryQueryOptions()
: mSort(0),
mResultType(0),
mExcludeItems(false),
mExcludeQueries(false),
mExpandQueries(true),
mIncludeHidden(false),
mMaxResults(0),
mQueryType(nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY),
mAsyncEnabled(false) {}
nsNavHistoryQueryOptions::nsNavHistoryQueryOptions(
const nsNavHistoryQueryOptions& other)
: mSort(other.mSort),
mResultType(other.mResultType),
mExcludeItems(other.mExcludeItems),
mExcludeQueries(other.mExcludeQueries),
mExpandQueries(other.mExpandQueries),
mIncludeHidden(other.mIncludeHidden),
mMaxResults(other.mMaxResults),
mQueryType(other.mQueryType),
mAsyncEnabled(other.mAsyncEnabled) {}
// 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;
}
// resultType
NS_IMETHODIMP
nsNavHistoryQueryOptions::GetResultType(uint16_t* aType) {
*aType = mResultType;
return NS_OK;
}
NS_IMETHODIMP
nsNavHistoryQueryOptions::SetResultType(uint16_t aType) {
if (aType > RESULTS_AS_LEFT_PANE_QUERY) return NS_ERROR_INVALID_ARG;
// Tag queries, containers and the roots query are bookmarks related, so we
// set the QueryType accordingly.
if (aType == RESULTS_AS_TAGS_ROOT || aType == RESULTS_AS_ROOTS_QUERY ||
aType == RESULTS_AS_LEFT_PANE_QUERY) {
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;
}
// 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_TAGS_ROOT ||
mResultType == RESULTS_AS_LEFT_PANE_QUERY ||
mResultType == RESULTS_AS_ROOTS_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** _clone) {
nsNavHistoryQueryOptions* clone = nullptr;
Unused << Clone(&clone);
*_clone = clone;
return NS_OK;
}
nsresult nsNavHistoryQueryOptions::Clone(nsNavHistoryQueryOptions** _clone) {
*_clone = nullptr;
RefPtr<nsNavHistoryQueryOptions> clone = new nsNavHistoryQueryOptions(*this);
clone.forget(_clone);
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 = false;
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.");
}
}
|