Source code

Revision control

Other Tools

1
/* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
2
* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "Helpers.h"
7
#include "mozIStorageError.h"
8
#include "prio.h"
9
#include "nsString.h"
10
#include "nsNavHistory.h"
11
#include "mozilla/Base64.h"
12
#include "mozilla/HashFunctions.h"
13
#include <algorithm>
14
#include "mozilla/Services.h"
15
16
// The length of guids that are used by history and bookmarks.
17
#define GUID_LENGTH 12
18
19
// Maximum number of chars to use for calculating hashes. This value has been
20
// picked to ensure low hash collisions on a real world common places.sqlite.
21
// While collisions are not a big deal for functionality, a low ratio allows
22
// for slightly more efficient SELECTs.
23
#define MAX_CHARS_TO_HASH 1500U
24
25
extern "C" {
26
27
// Generates a new Places GUID. This function uses C linkage because it's
28
// called from the Rust synced bookmarks mirror, on the storage thread.
29
nsresult NS_GeneratePlacesGUID(nsACString* _guid) {
30
return mozilla::places::GenerateGUID(*_guid);
31
}
32
33
} // extern "C"
34
35
namespace mozilla {
36
namespace places {
37
38
////////////////////////////////////////////////////////////////////////////////
39
//// AsyncStatementCallback
40
41
NS_IMPL_ISUPPORTS(AsyncStatementCallback, mozIStorageStatementCallback)
42
43
NS_IMETHODIMP
44
WeakAsyncStatementCallback::HandleResult(mozIStorageResultSet* aResultSet) {
45
MOZ_ASSERT(false, "Was not expecting a resultset, but got it.");
46
return NS_OK;
47
}
48
49
NS_IMETHODIMP
50
WeakAsyncStatementCallback::HandleCompletion(uint16_t aReason) { return NS_OK; }
51
52
NS_IMETHODIMP
53
WeakAsyncStatementCallback::HandleError(mozIStorageError* aError) {
54
#ifdef DEBUG
55
int32_t result;
56
nsresult rv = aError->GetResult(&result);
57
NS_ENSURE_SUCCESS(rv, rv);
58
nsAutoCString message;
59
rv = aError->GetMessage(message);
60
NS_ENSURE_SUCCESS(rv, rv);
61
62
nsAutoCString warnMsg;
63
warnMsg.AppendLiteral(
64
"An error occurred while executing an async statement: ");
65
warnMsg.AppendInt(result);
66
warnMsg.Append(' ');
67
warnMsg.Append(message);
68
NS_WARNING(warnMsg.get());
69
#endif
70
71
return NS_OK;
72
}
73
74
#define URI_TO_URLCSTRING(uri, spec) \
75
nsAutoCString spec; \
76
if (NS_FAILED(aURI->GetSpec(spec))) { \
77
return NS_ERROR_UNEXPECTED; \
78
}
79
80
// Bind URI to statement by index.
81
nsresult // static
82
URIBinder::Bind(mozIStorageStatement* aStatement, int32_t aIndex,
83
nsIURI* aURI) {
84
NS_ASSERTION(aStatement, "Must have non-null statement");
85
NS_ASSERTION(aURI, "Must have non-null uri");
86
87
URI_TO_URLCSTRING(aURI, spec);
88
return URIBinder::Bind(aStatement, aIndex, spec);
89
}
90
91
// Statement URLCString to statement by index.
92
nsresult // static
93
URIBinder::Bind(mozIStorageStatement* aStatement, int32_t index,
94
const nsACString& aURLString) {
95
NS_ASSERTION(aStatement, "Must have non-null statement");
96
return aStatement->BindUTF8StringByIndex(
97
index, StringHead(aURLString, URI_LENGTH_MAX));
98
}
99
100
// Bind URI to statement by name.
101
nsresult // static
102
URIBinder::Bind(mozIStorageStatement* aStatement, const nsACString& aName,
103
nsIURI* aURI) {
104
NS_ASSERTION(aStatement, "Must have non-null statement");
105
NS_ASSERTION(aURI, "Must have non-null uri");
106
107
URI_TO_URLCSTRING(aURI, spec);
108
return URIBinder::Bind(aStatement, aName, spec);
109
}
110
111
// Bind URLCString to statement by name.
112
nsresult // static
113
URIBinder::Bind(mozIStorageStatement* aStatement, const nsACString& aName,
114
const nsACString& aURLString) {
115
NS_ASSERTION(aStatement, "Must have non-null statement");
116
return aStatement->BindUTF8StringByName(
117
aName, StringHead(aURLString, URI_LENGTH_MAX));
118
}
119
120
// Bind URI to params by index.
121
nsresult // static
122
URIBinder::Bind(mozIStorageBindingParams* aParams, int32_t aIndex,
123
nsIURI* aURI) {
124
NS_ASSERTION(aParams, "Must have non-null statement");
125
NS_ASSERTION(aURI, "Must have non-null uri");
126
127
URI_TO_URLCSTRING(aURI, spec);
128
return URIBinder::Bind(aParams, aIndex, spec);
129
}
130
131
// Bind URLCString to params by index.
132
nsresult // static
133
URIBinder::Bind(mozIStorageBindingParams* aParams, int32_t index,
134
const nsACString& aURLString) {
135
NS_ASSERTION(aParams, "Must have non-null statement");
136
return aParams->BindUTF8StringByIndex(index,
137
StringHead(aURLString, URI_LENGTH_MAX));
138
}
139
140
// Bind URI to params by name.
141
nsresult // static
142
URIBinder::Bind(mozIStorageBindingParams* aParams, const nsACString& aName,
143
nsIURI* aURI) {
144
NS_ASSERTION(aParams, "Must have non-null params array");
145
NS_ASSERTION(aURI, "Must have non-null uri");
146
147
URI_TO_URLCSTRING(aURI, spec);
148
return URIBinder::Bind(aParams, aName, spec);
149
}
150
151
// Bind URLCString to params by name.
152
nsresult // static
153
URIBinder::Bind(mozIStorageBindingParams* aParams, const nsACString& aName,
154
const nsACString& aURLString) {
155
NS_ASSERTION(aParams, "Must have non-null params array");
156
157
nsresult rv = aParams->BindUTF8StringByName(
158
aName, StringHead(aURLString, URI_LENGTH_MAX));
159
NS_ENSURE_SUCCESS(rv, rv);
160
return NS_OK;
161
}
162
163
#undef URI_TO_URLCSTRING
164
165
nsresult GetReversedHostname(nsIURI* aURI, nsString& aRevHost) {
166
nsAutoCString forward8;
167
nsresult rv = aURI->GetHost(forward8);
168
// Not all URIs have a host.
169
if (NS_FAILED(rv)) return rv;
170
171
// can't do reversing in UTF8, better use 16-bit chars
172
GetReversedHostname(NS_ConvertUTF8toUTF16(forward8), aRevHost);
173
return NS_OK;
174
}
175
176
void GetReversedHostname(const nsString& aForward, nsString& aRevHost) {
177
ReverseString(aForward, aRevHost);
178
aRevHost.Append(char16_t('.'));
179
}
180
181
void ReverseString(const nsString& aInput, nsString& aReversed) {
182
aReversed.Truncate(0);
183
for (int32_t i = aInput.Length() - 1; i >= 0; i--) {
184
aReversed.Append(aInput[i]);
185
}
186
}
187
188
static nsresult GenerateRandomBytes(uint32_t aSize, uint8_t* _buffer) {
189
// On Windows, we'll use its built-in cryptographic API.
190
#if defined(XP_WIN)
191
const nsNavHistory* history = nsNavHistory::GetConstHistoryService();
192
HCRYPTPROV cryptoProvider;
193
nsresult rv = history->GetCryptoProvider(cryptoProvider);
194
NS_ENSURE_SUCCESS(rv, rv);
195
BOOL rc = CryptGenRandom(cryptoProvider, aSize, _buffer);
196
return rc ? NS_OK : NS_ERROR_FAILURE;
197
198
// On Unix, we'll just read in from /dev/urandom.
199
#elif defined(XP_UNIX)
200
NS_ENSURE_ARG_MAX(aSize, INT32_MAX);
201
PRFileDesc* urandom = PR_Open("/dev/urandom", PR_RDONLY, 0);
202
nsresult rv = NS_ERROR_FAILURE;
203
if (urandom) {
204
int32_t bytesRead = PR_Read(urandom, _buffer, aSize);
205
if (bytesRead == static_cast<int32_t>(aSize)) {
206
rv = NS_OK;
207
}
208
(void)PR_Close(urandom);
209
}
210
return rv;
211
#endif
212
}
213
214
nsresult GenerateGUID(nsACString& _guid) {
215
_guid.Truncate();
216
217
// Request raw random bytes and base64url encode them. For each set of three
218
// bytes, we get one character.
219
const uint32_t kRequiredBytesLength =
220
static_cast<uint32_t>(GUID_LENGTH / 4 * 3);
221
222
uint8_t buffer[kRequiredBytesLength];
223
nsresult rv = GenerateRandomBytes(kRequiredBytesLength, buffer);
224
NS_ENSURE_SUCCESS(rv, rv);
225
226
rv = Base64URLEncode(kRequiredBytesLength, buffer,
227
Base64URLEncodePaddingPolicy::Omit, _guid);
228
NS_ENSURE_SUCCESS(rv, rv);
229
230
NS_ASSERTION(_guid.Length() == GUID_LENGTH, "GUID is not the right size!");
231
return NS_OK;
232
}
233
234
bool IsValidGUID(const nsACString& aGUID) {
235
nsCString::size_type len = aGUID.Length();
236
if (len != GUID_LENGTH) {
237
return false;
238
}
239
240
for (nsCString::size_type i = 0; i < len; i++) {
241
char c = aGUID[i];
242
if ((c >= 'a' && c <= 'z') || // a-z
243
(c >= 'A' && c <= 'Z') || // A-Z
244
(c >= '0' && c <= '9') || // 0-9
245
c == '-' || c == '_') { // - or _
246
continue;
247
}
248
return false;
249
}
250
return true;
251
}
252
253
void TruncateTitle(const nsACString& aTitle, nsACString& aTrimmed) {
254
if (aTitle.IsVoid()) {
255
return;
256
}
257
aTrimmed = aTitle;
258
if (aTitle.Length() > TITLE_LENGTH_MAX) {
259
aTrimmed = StringHead(aTitle, TITLE_LENGTH_MAX);
260
}
261
}
262
263
PRTime RoundToMilliseconds(PRTime aTime) {
264
return aTime - (aTime % PR_USEC_PER_MSEC);
265
}
266
267
PRTime RoundedPRNow() { return RoundToMilliseconds(PR_Now()); }
268
269
nsresult HashURL(const nsACString& aSpec, const nsACString& aMode,
270
uint64_t* _hash) {
271
NS_ENSURE_ARG_POINTER(_hash);
272
273
// HashString doesn't stop at the string boundaries if a length is passed to
274
// it, so ensure to pass a proper value.
275
const uint32_t maxLenToHash =
276
std::min(static_cast<uint32_t>(aSpec.Length()), MAX_CHARS_TO_HASH);
277
278
if (aMode.IsEmpty()) {
279
// URI-like strings (having a prefix before a colon), are handled specially,
280
// as a 48 bit hash, where first 16 bits are the prefix hash, while the
281
// other 32 are the string hash.
282
// The 16 bits have been decided based on the fact hashing all of the IANA
283
// known schemes, plus "places", does not generate collisions.
284
// Since we only care about schemes, we just search in the first 50 chars.
285
// The longest known IANA scheme, at this time, is 30 chars.
286
const nsDependentCSubstring& strHead = StringHead(aSpec, 50);
287
nsACString::const_iterator start, tip, end;
288
strHead.BeginReading(tip);
289
start = tip;
290
strHead.EndReading(end);
291
uint32_t strHash = HashString(aSpec.BeginReading(), maxLenToHash);
292
if (FindCharInReadable(':', tip, end)) {
293
const nsDependentCSubstring& prefix = Substring(start, tip);
294
uint64_t prefixHash =
295
static_cast<uint64_t>(HashString(prefix) & 0x0000FFFF);
296
// The second half of the url is more likely to be unique, so we add it.
297
*_hash = (prefixHash << 32) + strHash;
298
} else {
299
*_hash = strHash;
300
}
301
} else if (aMode.EqualsLiteral("prefix_lo")) {
302
// Keep only 16 bits.
303
*_hash = static_cast<uint64_t>(
304
HashString(aSpec.BeginReading(), maxLenToHash) & 0x0000FFFF)
305
<< 32;
306
} else if (aMode.EqualsLiteral("prefix_hi")) {
307
// Keep only 16 bits.
308
*_hash = static_cast<uint64_t>(
309
HashString(aSpec.BeginReading(), maxLenToHash) & 0x0000FFFF)
310
<< 32;
311
// Make this a prefix upper bound by filling the lowest 32 bits.
312
*_hash += 0xFFFFFFFF;
313
} else {
314
return NS_ERROR_FAILURE;
315
}
316
317
return NS_OK;
318
}
319
320
bool GetHiddenState(bool aIsRedirect, uint32_t aTransitionType) {
321
return aTransitionType == nsINavHistoryService::TRANSITION_FRAMED_LINK ||
322
aTransitionType == nsINavHistoryService::TRANSITION_EMBED ||
323
aIsRedirect;
324
}
325
326
nsresult TokenizeQueryString(const nsACString& aQuery,
327
nsTArray<QueryKeyValuePair>* aTokens) {
328
// Strip off the "place:" prefix
329
const uint32_t prefixlen = 6; // = strlen("place:");
330
nsCString query;
331
if (aQuery.Length() >= prefixlen &&
332
Substring(aQuery, 0, prefixlen).EqualsLiteral("place:"))
333
query = Substring(aQuery, prefixlen);
334
else
335
query = aQuery;
336
337
int32_t keyFirstIndex = 0;
338
int32_t equalsIndex = 0;
339
for (uint32_t i = 0; i < query.Length(); i++) {
340
if (query[i] == '&') {
341
// new clause, save last one
342
if (i - keyFirstIndex > 1) {
343
if (!aTokens->AppendElement(
344
QueryKeyValuePair(query, keyFirstIndex, equalsIndex, i)))
345
return NS_ERROR_OUT_OF_MEMORY;
346
}
347
keyFirstIndex = equalsIndex = i + 1;
348
} else if (query[i] == '=') {
349
equalsIndex = i;
350
}
351
}
352
353
// handle last pair, if any
354
if (query.Length() - keyFirstIndex > 1) {
355
if (!aTokens->AppendElement(QueryKeyValuePair(query, keyFirstIndex,
356
equalsIndex, query.Length())))
357
return NS_ERROR_OUT_OF_MEMORY;
358
}
359
return NS_OK;
360
}
361
362
void TokensToQueryString(const nsTArray<QueryKeyValuePair>& aTokens,
363
nsACString& aQuery) {
364
aQuery = NS_LITERAL_CSTRING("place:");
365
for (uint32_t i = 0; i < aTokens.Length(); i++) {
366
if (i > 0) {
367
aQuery.Append("&");
368
}
369
aQuery.Append(aTokens[i].key);
370
aQuery.AppendLiteral("=");
371
aQuery.Append(aTokens[i].value);
372
}
373
}
374
375
} // namespace places
376
} // namespace mozilla