/src/mozilla-central/toolkit/components/places/nsAnnotationService.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
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 "mozilla/ArrayUtils.h" |
7 | | |
8 | | #include "nsAnnotationService.h" |
9 | | #include "nsNavHistory.h" |
10 | | #include "nsPlacesTables.h" |
11 | | #include "nsPlacesIndexes.h" |
12 | | #include "nsPlacesMacros.h" |
13 | | #include "Helpers.h" |
14 | | |
15 | | #include "nsNetUtil.h" |
16 | | #include "nsIVariant.h" |
17 | | #include "nsString.h" |
18 | | #include "nsVariant.h" |
19 | | #include "mozilla/storage.h" |
20 | | |
21 | | #include "GeckoProfiler.h" |
22 | | |
23 | | #include "nsNetCID.h" |
24 | | |
25 | | using namespace mozilla; |
26 | | using namespace mozilla::places; |
27 | | |
28 | | const int32_t nsAnnotationService::kAnnoIndex_ID = 0; |
29 | | const int32_t nsAnnotationService::kAnnoIndex_PageOrItem = 1; |
30 | | const int32_t nsAnnotationService::kAnnoIndex_NameID = 2; |
31 | | const int32_t nsAnnotationService::kAnnoIndex_Content = 3; |
32 | | const int32_t nsAnnotationService::kAnnoIndex_Flags = 4; |
33 | | const int32_t nsAnnotationService::kAnnoIndex_Expiration = 5; |
34 | | const int32_t nsAnnotationService::kAnnoIndex_Type = 6; |
35 | | const int32_t nsAnnotationService::kAnnoIndex_DateAdded = 7; |
36 | | const int32_t nsAnnotationService::kAnnoIndex_LastModified = 8; |
37 | | |
38 | | namespace { |
39 | | |
40 | | // Fires `onItemChanged` notifications for bookmark annotation changes. |
41 | | void |
42 | | NotifyItemChanged(const BookmarkData& aBookmark, const nsACString& aName, |
43 | | uint16_t aSource, bool aDontUpdateLastModified) |
44 | 0 | { |
45 | 0 | if (aBookmark.id < 0) { |
46 | 0 | return; |
47 | 0 | } |
48 | 0 | |
49 | 0 | ItemChangeData changeData; |
50 | 0 | changeData.bookmark = aBookmark; |
51 | 0 | changeData.isAnnotation = true; |
52 | 0 | changeData.updateLastModified = !aDontUpdateLastModified; |
53 | 0 | changeData.source = aSource; |
54 | 0 | changeData.property = aName; |
55 | 0 |
|
56 | 0 | nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService(); |
57 | 0 | if (bookmarks) { |
58 | 0 | bookmarks->NotifyItemChanged(changeData); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | } |
63 | | |
64 | | PLACES_FACTORY_SINGLETON_IMPLEMENTATION(nsAnnotationService, gAnnotationService) |
65 | | |
66 | | NS_IMPL_ISUPPORTS(nsAnnotationService |
67 | | , nsIAnnotationService |
68 | | , nsISupportsWeakReference |
69 | | ) |
70 | | |
71 | | |
72 | | nsAnnotationService::nsAnnotationService() |
73 | 0 | { |
74 | 0 | NS_ASSERTION(!gAnnotationService, |
75 | 0 | "Attempting to create two instances of the service!"); |
76 | 0 | gAnnotationService = this; |
77 | 0 | } |
78 | | |
79 | | |
80 | | nsAnnotationService::~nsAnnotationService() |
81 | 0 | { |
82 | 0 | NS_ASSERTION(gAnnotationService == this, |
83 | 0 | "Deleting a non-singleton instance of the service"); |
84 | 0 | if (gAnnotationService == this) |
85 | 0 | gAnnotationService = nullptr; |
86 | 0 | } |
87 | | |
88 | | |
89 | | nsresult |
90 | | nsAnnotationService::Init() |
91 | 0 | { |
92 | 0 | mDB = Database::GetDatabase(); |
93 | 0 | NS_ENSURE_STATE(mDB); |
94 | 0 |
|
95 | 0 | return NS_OK; |
96 | 0 | } |
97 | | |
98 | | nsresult |
99 | | nsAnnotationService::SetAnnotationStringInternal(int64_t aItemId, |
100 | | BookmarkData* aBookmark, |
101 | | const nsACString& aName, |
102 | | const nsAString& aValue, |
103 | | int32_t aFlags, |
104 | | uint16_t aExpiration) |
105 | 0 | { |
106 | 0 | mozStorageTransaction transaction(mDB->MainConn(), false); |
107 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
108 | 0 |
|
109 | 0 | nsresult rv = StartSetAnnotation(aItemId, aBookmark, aName, aFlags, |
110 | 0 | aExpiration, |
111 | 0 | nsIAnnotationService::TYPE_STRING, |
112 | 0 | statement); |
113 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
114 | 0 |
|
115 | 0 | mozStorageStatementScoper scoper(statement); |
116 | 0 |
|
117 | 0 | rv = statement->BindStringByName(NS_LITERAL_CSTRING("content"), aValue); |
118 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
119 | 0 |
|
120 | 0 | rv = statement->Execute(); |
121 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
122 | 0 |
|
123 | 0 | rv = transaction.Commit(); |
124 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
125 | 0 |
|
126 | 0 | return NS_OK; |
127 | 0 | } |
128 | | |
129 | | |
130 | | NS_IMETHODIMP |
131 | | nsAnnotationService::SetItemAnnotation(int64_t aItemId, |
132 | | const nsACString& aName, |
133 | | nsIVariant* aValue, |
134 | | int32_t aFlags, |
135 | | uint16_t aExpiration, |
136 | | uint16_t aSource, |
137 | | bool aDontUpdateLastModified) |
138 | 0 | { |
139 | 0 | AUTO_PROFILER_LABEL("nsAnnotationService::SetItemAnnotation", OTHER); |
140 | 0 |
|
141 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
142 | 0 | NS_ENSURE_ARG(aValue); |
143 | 0 |
|
144 | 0 | uint16_t dataType; |
145 | 0 | nsresult rv = aValue->GetDataType(&dataType); |
146 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
147 | 0 | BookmarkData bookmark; |
148 | 0 |
|
149 | 0 | switch (dataType) { |
150 | 0 | case nsIDataType::VTYPE_INT8: |
151 | 0 | case nsIDataType::VTYPE_UINT8: |
152 | 0 | case nsIDataType::VTYPE_INT16: |
153 | 0 | case nsIDataType::VTYPE_UINT16: |
154 | 0 | case nsIDataType::VTYPE_INT32: |
155 | 0 | case nsIDataType::VTYPE_UINT32: |
156 | 0 | case nsIDataType::VTYPE_BOOL: { |
157 | 0 | int32_t valueInt; |
158 | 0 | rv = aValue->GetAsInt32(&valueInt); |
159 | 0 | if (NS_SUCCEEDED(rv)) { |
160 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
161 | 0 | rv = SetAnnotationInt32Internal(aItemId, &bookmark, aName, |
162 | 0 | valueInt, aFlags, aExpiration); |
163 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
164 | 0 | break; |
165 | 0 | } |
166 | 0 | // Fall through int64_t case otherwise. |
167 | 0 | MOZ_FALLTHROUGH; |
168 | 0 | } |
169 | 0 | case nsIDataType::VTYPE_INT64: |
170 | 0 | case nsIDataType::VTYPE_UINT64: { |
171 | 0 | int64_t valueLong; |
172 | 0 | rv = aValue->GetAsInt64(&valueLong); |
173 | 0 | if (NS_SUCCEEDED(rv)) { |
174 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
175 | 0 | rv = SetAnnotationInt64Internal(aItemId, &bookmark, aName, |
176 | 0 | valueLong, aFlags, aExpiration); |
177 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
178 | 0 | break; |
179 | 0 | } |
180 | 0 | // Fall through double case otherwise. |
181 | 0 | MOZ_FALLTHROUGH; |
182 | 0 | } |
183 | 0 | case nsIDataType::VTYPE_FLOAT: |
184 | 0 | case nsIDataType::VTYPE_DOUBLE: { |
185 | 0 | double valueDouble; |
186 | 0 | rv = aValue->GetAsDouble(&valueDouble); |
187 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
188 | 0 | rv = SetAnnotationDoubleInternal(aItemId, &bookmark, |
189 | 0 | aName, valueDouble, aFlags, aExpiration); |
190 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
191 | 0 | break; |
192 | 0 | } |
193 | 0 | case nsIDataType::VTYPE_CHAR: |
194 | 0 | case nsIDataType::VTYPE_WCHAR: |
195 | 0 | case nsIDataType::VTYPE_DOMSTRING: |
196 | 0 | case nsIDataType::VTYPE_CHAR_STR: |
197 | 0 | case nsIDataType::VTYPE_WCHAR_STR: |
198 | 0 | case nsIDataType::VTYPE_STRING_SIZE_IS: |
199 | 0 | case nsIDataType::VTYPE_WSTRING_SIZE_IS: |
200 | 0 | case nsIDataType::VTYPE_UTF8STRING: |
201 | 0 | case nsIDataType::VTYPE_CSTRING: |
202 | 0 | case nsIDataType::VTYPE_ASTRING: { |
203 | 0 | nsAutoString stringValue; |
204 | 0 | rv = aValue->GetAsAString(stringValue); |
205 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
206 | 0 | rv = SetAnnotationStringInternal(aItemId, &bookmark, aName, |
207 | 0 | stringValue, aFlags, aExpiration); |
208 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
209 | 0 | break; |
210 | 0 | } |
211 | 0 | default: |
212 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
213 | 0 | } |
214 | 0 | |
215 | 0 | NotifyItemChanged(bookmark, aName, aSource, aDontUpdateLastModified); |
216 | 0 |
|
217 | 0 | return NS_OK; |
218 | 0 | } |
219 | | |
220 | | |
221 | | nsresult |
222 | | nsAnnotationService::SetAnnotationInt32Internal(int64_t aItemId, |
223 | | BookmarkData* aBookmark, |
224 | | const nsACString& aName, |
225 | | int32_t aValue, |
226 | | int32_t aFlags, |
227 | | uint16_t aExpiration) |
228 | 0 | { |
229 | 0 | mozStorageTransaction transaction(mDB->MainConn(), false); |
230 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
231 | 0 | nsresult rv = StartSetAnnotation(aItemId, aBookmark, aName, aFlags, |
232 | 0 | aExpiration, |
233 | 0 | nsIAnnotationService::TYPE_INT32, |
234 | 0 | statement); |
235 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
236 | 0 |
|
237 | 0 | mozStorageStatementScoper scoper(statement); |
238 | 0 |
|
239 | 0 | rv = statement->BindInt32ByName(NS_LITERAL_CSTRING("content"), aValue); |
240 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
241 | 0 |
|
242 | 0 | rv = statement->Execute(); |
243 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
244 | 0 |
|
245 | 0 | rv = transaction.Commit(); |
246 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
247 | 0 |
|
248 | 0 | return NS_OK; |
249 | 0 | } |
250 | | |
251 | | |
252 | | nsresult |
253 | | nsAnnotationService::SetAnnotationInt64Internal(int64_t aItemId, |
254 | | BookmarkData* aBookmark, |
255 | | const nsACString& aName, |
256 | | int64_t aValue, |
257 | | int32_t aFlags, |
258 | | uint16_t aExpiration) |
259 | 0 | { |
260 | 0 | mozStorageTransaction transaction(mDB->MainConn(), false); |
261 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
262 | 0 | nsresult rv = StartSetAnnotation(aItemId, aBookmark, aName, aFlags, |
263 | 0 | aExpiration, |
264 | 0 | nsIAnnotationService::TYPE_INT64, |
265 | 0 | statement); |
266 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
267 | 0 |
|
268 | 0 | mozStorageStatementScoper scoper(statement); |
269 | 0 |
|
270 | 0 | rv = statement->BindInt64ByName(NS_LITERAL_CSTRING("content"), aValue); |
271 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
272 | 0 |
|
273 | 0 | rv = statement->Execute(); |
274 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
275 | 0 |
|
276 | 0 | rv = transaction.Commit(); |
277 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
278 | 0 |
|
279 | 0 | return NS_OK; |
280 | 0 | } |
281 | | |
282 | | |
283 | | nsresult |
284 | | nsAnnotationService::SetAnnotationDoubleInternal(int64_t aItemId, |
285 | | BookmarkData* aBookmark, |
286 | | const nsACString& aName, |
287 | | double aValue, |
288 | | int32_t aFlags, |
289 | | uint16_t aExpiration) |
290 | 0 | { |
291 | 0 | mozStorageTransaction transaction(mDB->MainConn(), false); |
292 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
293 | 0 | nsresult rv = StartSetAnnotation(aItemId, aBookmark, aName, aFlags, |
294 | 0 | aExpiration, |
295 | 0 | nsIAnnotationService::TYPE_DOUBLE, |
296 | 0 | statement); |
297 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
298 | 0 |
|
299 | 0 | mozStorageStatementScoper scoper(statement); |
300 | 0 |
|
301 | 0 | rv = statement->BindDoubleByName(NS_LITERAL_CSTRING("content"), aValue); |
302 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
303 | 0 |
|
304 | 0 | rv = statement->Execute(); |
305 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
306 | 0 |
|
307 | 0 | rv = transaction.Commit(); |
308 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
309 | 0 |
|
310 | 0 | return NS_OK; |
311 | 0 | } |
312 | | |
313 | | |
314 | | nsresult |
315 | | nsAnnotationService::GetValueFromStatement(nsCOMPtr<mozIStorageStatement>& aStatement, |
316 | | nsIVariant** _retval) |
317 | 0 | { |
318 | 0 | nsresult rv; |
319 | 0 |
|
320 | 0 | nsCOMPtr<nsIWritableVariant> value = new nsVariant(); |
321 | 0 | int32_t type = aStatement->AsInt32(kAnnoIndex_Type); |
322 | 0 | switch (type) { |
323 | 0 | case nsIAnnotationService::TYPE_INT32: |
324 | 0 | case nsIAnnotationService::TYPE_INT64: |
325 | 0 | case nsIAnnotationService::TYPE_DOUBLE: { |
326 | 0 | rv = value->SetAsDouble(aStatement->AsDouble(kAnnoIndex_Content)); |
327 | 0 | break; |
328 | 0 | } |
329 | 0 | case nsIAnnotationService::TYPE_STRING: { |
330 | 0 | nsAutoString valueString; |
331 | 0 | rv = aStatement->GetString(kAnnoIndex_Content, valueString); |
332 | 0 | if (NS_SUCCEEDED(rv)) |
333 | 0 | rv = value->SetAsAString(valueString); |
334 | 0 | break; |
335 | 0 | } |
336 | 0 | default: { |
337 | 0 | rv = NS_ERROR_UNEXPECTED; |
338 | 0 | break; |
339 | 0 | } |
340 | 0 | } |
341 | 0 | if (NS_SUCCEEDED(rv)) { |
342 | 0 | value.forget(_retval); |
343 | 0 | } |
344 | 0 | return rv; |
345 | 0 | } |
346 | | |
347 | | |
348 | | NS_IMETHODIMP |
349 | | nsAnnotationService::GetItemAnnotation(int64_t aItemId, |
350 | | const nsACString& aName, |
351 | | nsIVariant** _retval) |
352 | 0 | { |
353 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
354 | 0 | NS_ENSURE_ARG_POINTER(_retval); |
355 | 0 |
|
356 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
357 | 0 | nsresult rv = StartGetAnnotation(aItemId, aName, statement); |
358 | 0 | if (NS_FAILED(rv)) |
359 | 0 | return rv; |
360 | 0 | |
361 | 0 | mozStorageStatementScoper scoper(statement); |
362 | 0 |
|
363 | 0 | return GetValueFromStatement(statement, _retval); |
364 | 0 | } |
365 | | |
366 | | |
367 | | NS_IMETHODIMP |
368 | | nsAnnotationService::GetItemAnnotationInfo(int64_t aItemId, |
369 | | const nsACString& aName, |
370 | | nsIVariant** _value, |
371 | | int32_t* _flags, |
372 | | uint16_t* _expiration, |
373 | | uint16_t* _storageType) |
374 | 0 | { |
375 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
376 | 0 | NS_ENSURE_ARG_POINTER(_value); |
377 | 0 | NS_ENSURE_ARG_POINTER(_flags); |
378 | 0 | NS_ENSURE_ARG_POINTER(_expiration); |
379 | 0 | NS_ENSURE_ARG_POINTER(_storageType); |
380 | 0 |
|
381 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
382 | 0 | nsresult rv = StartGetAnnotation(aItemId, aName, statement); |
383 | 0 | if (NS_FAILED(rv)) |
384 | 0 | return rv; |
385 | 0 | |
386 | 0 | mozStorageStatementScoper scoper(statement); |
387 | 0 | *_flags = statement->AsInt32(kAnnoIndex_Flags); |
388 | 0 | *_expiration = (uint16_t)statement->AsInt32(kAnnoIndex_Expiration); |
389 | 0 | int32_t type = (uint16_t)statement->AsInt32(kAnnoIndex_Type); |
390 | 0 | if (type == 0) { |
391 | 0 | // For annotations created before explicit typing, |
392 | 0 | // we can't determine type, just return as string type. |
393 | 0 | *_storageType = nsIAnnotationService::TYPE_STRING; |
394 | 0 | } |
395 | 0 | else { |
396 | 0 | *_storageType = type; |
397 | 0 | } |
398 | 0 |
|
399 | 0 | return GetValueFromStatement(statement, _value); |
400 | 0 | } |
401 | | |
402 | | |
403 | | nsresult |
404 | | nsAnnotationService::GetItemAnnotationNamesTArray(int64_t aItemId, |
405 | | nsTArray<nsCString>* _result) |
406 | 0 | { |
407 | 0 | _result->Clear(); |
408 | 0 |
|
409 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
410 | 0 | statement = mDB->GetStatement( |
411 | 0 | "SELECT n.name " |
412 | 0 | "FROM moz_anno_attributes n " |
413 | 0 | "JOIN moz_items_annos a ON a.anno_attribute_id = n.id " |
414 | 0 | "WHERE a.item_id = :item_id" |
415 | 0 | ); |
416 | 0 | NS_ENSURE_STATE(statement); |
417 | 0 | mozStorageStatementScoper scoper(statement); |
418 | 0 |
|
419 | 0 | nsresult rv = statement->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), aItemId); |
420 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
421 | 0 |
|
422 | 0 | bool hasResult = false; |
423 | 0 | while (NS_SUCCEEDED(statement->ExecuteStep(&hasResult)) && |
424 | 0 | hasResult) { |
425 | 0 | nsAutoCString name; |
426 | 0 | rv = statement->GetUTF8String(0, name); |
427 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
428 | 0 | if (!_result->AppendElement(name)) |
429 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
430 | 0 | } |
431 | 0 |
|
432 | 0 | return NS_OK; |
433 | 0 | } |
434 | | |
435 | | |
436 | | NS_IMETHODIMP |
437 | | nsAnnotationService::GetItemAnnotationNames(int64_t aItemId, |
438 | | uint32_t* _count, |
439 | | nsIVariant*** _result) |
440 | 0 | { |
441 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
442 | 0 | NS_ENSURE_ARG_POINTER(_count); |
443 | 0 | NS_ENSURE_ARG_POINTER(_result); |
444 | 0 |
|
445 | 0 | *_count = 0; |
446 | 0 | *_result = nullptr; |
447 | 0 |
|
448 | 0 | nsTArray<nsCString> names; |
449 | 0 | nsresult rv = GetItemAnnotationNamesTArray(aItemId, &names); |
450 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
451 | 0 |
|
452 | 0 | if (names.Length() == 0) |
453 | 0 | return NS_OK; |
454 | 0 | |
455 | 0 | *_result = static_cast<nsIVariant**> |
456 | 0 | (moz_xmalloc(sizeof(nsIVariant*) * names.Length())); |
457 | 0 |
|
458 | 0 | for (uint32_t i = 0; i < names.Length(); i ++) { |
459 | 0 | nsCOMPtr<nsIWritableVariant> var = new nsVariant(); |
460 | 0 | if (!var) { |
461 | 0 | // need to release all the variants we've already created |
462 | 0 | for (uint32_t j = 0; j < i; j ++) |
463 | 0 | NS_RELEASE((*_result)[j]); |
464 | 0 | free(*_result); |
465 | 0 | *_result = nullptr; |
466 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
467 | 0 | } |
468 | 0 | var->SetAsAUTF8String(names[i]); |
469 | 0 | NS_ADDREF((*_result)[i] = var); |
470 | 0 | } |
471 | 0 | *_count = names.Length(); |
472 | 0 |
|
473 | 0 | return NS_OK; |
474 | 0 | } |
475 | | |
476 | | |
477 | | /** |
478 | | * @note We don't remove anything from the moz_anno_attributes table. If we |
479 | | * delete the last item of a given name, that item really should go away. |
480 | | * It will be cleaned up by expiration. |
481 | | */ |
482 | | nsresult |
483 | | nsAnnotationService::RemoveAnnotationInternal(int64_t aItemId, |
484 | | BookmarkData* aBookmark, |
485 | | const nsACString& aName) |
486 | 0 | { |
487 | 0 | nsCOMPtr<mozIStorageStatement> statement; |
488 | 0 | statement = mDB->GetStatement( |
489 | 0 | "DELETE FROM moz_items_annos " |
490 | 0 | "WHERE item_id = :item_id " |
491 | 0 | "AND anno_attribute_id = " |
492 | 0 | "(SELECT id FROM moz_anno_attributes WHERE name = :anno_name)" |
493 | 0 | ); |
494 | 0 | NS_ENSURE_STATE(statement); |
495 | 0 | mozStorageStatementScoper scoper(statement); |
496 | 0 |
|
497 | 0 | nsresult rv; |
498 | 0 | rv = statement->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), aItemId); |
499 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
500 | 0 |
|
501 | 0 | rv = statement->BindUTF8StringByName(NS_LITERAL_CSTRING("anno_name"), aName); |
502 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
503 | 0 |
|
504 | 0 | rv = statement->Execute(); |
505 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
506 | 0 |
|
507 | 0 | nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService(); |
508 | 0 | if (bookmarks) { |
509 | 0 | MOZ_ASSERT(aBookmark); |
510 | 0 | if (NS_FAILED(bookmarks->FetchItemInfo(aItemId, *aBookmark))) { |
511 | 0 | aBookmark->id = -1; |
512 | 0 | } |
513 | 0 | } |
514 | 0 |
|
515 | 0 | return NS_OK; |
516 | 0 | } |
517 | | |
518 | | |
519 | | NS_IMETHODIMP |
520 | | nsAnnotationService::RemoveItemAnnotation(int64_t aItemId, |
521 | | const nsACString& aName, |
522 | | uint16_t aSource) |
523 | 0 | { |
524 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
525 | 0 |
|
526 | 0 | BookmarkData bookmark; |
527 | 0 | nsresult rv = RemoveAnnotationInternal(aItemId, &bookmark, aName); |
528 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
529 | 0 |
|
530 | 0 | NotifyItemChanged(bookmark, aName, aSource, false); |
531 | 0 |
|
532 | 0 | return NS_OK; |
533 | 0 | } |
534 | | |
535 | | |
536 | | nsresult |
537 | | nsAnnotationService::RemoveItemAnnotations(int64_t aItemId) |
538 | 0 | { |
539 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
540 | 0 |
|
541 | 0 | // Should this be precompiled or a getter? |
542 | 0 | nsCOMPtr<mozIStorageStatement> statement = mDB->GetStatement( |
543 | 0 | "DELETE FROM moz_items_annos WHERE item_id = :item_id" |
544 | 0 | ); |
545 | 0 | NS_ENSURE_STATE(statement); |
546 | 0 | mozStorageStatementScoper scoper(statement); |
547 | 0 |
|
548 | 0 | nsresult rv = statement->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), aItemId); |
549 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
550 | 0 |
|
551 | 0 | rv = statement->Execute(); |
552 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
553 | 0 |
|
554 | 0 | return NS_OK; |
555 | 0 | } |
556 | | |
557 | | |
558 | | NS_IMETHODIMP |
559 | | nsAnnotationService::ItemHasAnnotation(int64_t aItemId, |
560 | | const nsACString& aName, |
561 | | bool* _hasAnno) |
562 | 0 | { |
563 | 0 | NS_ENSURE_ARG_MIN(aItemId, 1); |
564 | 0 | NS_ENSURE_ARG_POINTER(_hasAnno); |
565 | 0 |
|
566 | 0 | nsCOMPtr<mozIStorageStatement> stmt = mDB->GetStatement( |
567 | 0 | "SELECT b.id, " |
568 | 0 | "(SELECT id FROM moz_anno_attributes WHERE name = :anno_name) AS nameid, " |
569 | 0 | "a.id, a.dateAdded " |
570 | 0 | "FROM moz_bookmarks b " |
571 | 0 | "LEFT JOIN moz_items_annos a ON a.item_id = b.id " |
572 | 0 | "AND a.anno_attribute_id = nameid " |
573 | 0 | "WHERE b.id = :item_id" |
574 | 0 | ); |
575 | 0 | NS_ENSURE_STATE(stmt); |
576 | 0 | mozStorageStatementScoper checkAnnoScoper(stmt); |
577 | 0 |
|
578 | 0 | nsresult rv = stmt->BindUTF8StringByName(NS_LITERAL_CSTRING("anno_name"), aName); |
579 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
580 | 0 | rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), aItemId); |
581 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
582 | 0 |
|
583 | 0 | bool hasResult; |
584 | 0 | rv = stmt->ExecuteStep(&hasResult); |
585 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
586 | 0 | if (!hasResult) { |
587 | 0 | // We are trying to get an annotation on an invalid bookmarks or |
588 | 0 | // history entry. |
589 | 0 | // Here we preserve the old behavior, returning that we don't have the |
590 | 0 | // annotation, ignoring the fact itemId is invalid. |
591 | 0 | // Otherwise we should return NS_ERROR_INVALID_ARG, but this will somehow |
592 | 0 | // break the API. In future we could want to be pickier. |
593 | 0 | *_hasAnno = false; |
594 | 0 | } |
595 | 0 | else { |
596 | 0 | int64_t annotationId = stmt->AsInt64(2); |
597 | 0 | *_hasAnno = (annotationId > 0); |
598 | 0 | } |
599 | 0 |
|
600 | 0 | return NS_OK; |
601 | 0 | } |
602 | | |
603 | | |
604 | | /** |
605 | | * This loads the statement and steps it once so you can get data out of it. |
606 | | * |
607 | | * @note You have to reset the statement when you're done if this succeeds. |
608 | | * @throws NS_ERROR_NOT_AVAILABLE if the annotation is not found. |
609 | | */ |
610 | | |
611 | | nsresult |
612 | | nsAnnotationService::StartGetAnnotation(int64_t aItemId, |
613 | | const nsACString& aName, |
614 | | nsCOMPtr<mozIStorageStatement>& aStatement) |
615 | 0 | { |
616 | 0 | aStatement = mDB->GetStatement( |
617 | 0 | "SELECT a.id, a.item_id, :anno_name, a.content, a.flags, " |
618 | 0 | "a.expiration, a.type " |
619 | 0 | "FROM moz_anno_attributes n " |
620 | 0 | "JOIN moz_items_annos a ON a.anno_attribute_id = n.id " |
621 | 0 | "WHERE a.item_id = :item_id " |
622 | 0 | "AND n.name = :anno_name" |
623 | 0 | ); |
624 | 0 | NS_ENSURE_STATE(aStatement); |
625 | 0 | mozStorageStatementScoper getAnnoScoper(aStatement); |
626 | 0 |
|
627 | 0 | nsresult rv; |
628 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), aItemId); |
629 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
630 | 0 |
|
631 | 0 | rv = aStatement->BindUTF8StringByName(NS_LITERAL_CSTRING("anno_name"), aName); |
632 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
633 | 0 |
|
634 | 0 | bool hasResult = false; |
635 | 0 | rv = aStatement->ExecuteStep(&hasResult); |
636 | 0 | if (NS_FAILED(rv) || !hasResult) |
637 | 0 | return NS_ERROR_NOT_AVAILABLE; |
638 | 0 | |
639 | 0 | // on success, DON'T reset the statement, the caller needs to read from it, |
640 | 0 | // and it is the caller's job to reset it. |
641 | 0 | getAnnoScoper.Abandon(); |
642 | 0 |
|
643 | 0 | return NS_OK; |
644 | 0 | } |
645 | | |
646 | | |
647 | | /** |
648 | | * This does most of the setup work needed to set an annotation, except for |
649 | | * binding the the actual value and executing the statement. |
650 | | * It will either update an existing annotation or insert a new one. |
651 | | * |
652 | | * @note The aStatement RESULT IS NOT ADDREFED. This is just one of the class |
653 | | * vars, which control its scope. DO NOT RELEASE. |
654 | | * The caller must take care of resetting the statement if this succeeds. |
655 | | */ |
656 | | nsresult |
657 | | nsAnnotationService::StartSetAnnotation(int64_t aItemId, |
658 | | BookmarkData* aBookmark, |
659 | | const nsACString& aName, |
660 | | int32_t aFlags, |
661 | | uint16_t aExpiration, |
662 | | uint16_t aType, |
663 | | nsCOMPtr<mozIStorageStatement>& aStatement) |
664 | 0 | { |
665 | 0 | MOZ_ASSERT(aExpiration == EXPIRE_NEVER, "Only EXPIRE_NEVER is supported"); |
666 | 0 | NS_ENSURE_ARG(aExpiration == EXPIRE_NEVER); |
667 | 0 |
|
668 | 0 | // Ensure the annotation name exists. |
669 | 0 | nsCOMPtr<mozIStorageStatement> addNameStmt = mDB->GetStatement( |
670 | 0 | "INSERT OR IGNORE INTO moz_anno_attributes (name) VALUES (:anno_name)" |
671 | 0 | ); |
672 | 0 | NS_ENSURE_STATE(addNameStmt); |
673 | 0 | mozStorageStatementScoper scoper(addNameStmt); |
674 | 0 |
|
675 | 0 | nsresult rv = addNameStmt->BindUTF8StringByName(NS_LITERAL_CSTRING("anno_name"), aName); |
676 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
677 | 0 | rv = addNameStmt->Execute(); |
678 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
679 | 0 |
|
680 | 0 | // We have to check 2 things: |
681 | 0 | // - if the annotation already exists we should update it. |
682 | 0 | // - we should not allow setting annotations on invalid URIs or itemIds. |
683 | 0 | // This query will tell us: |
684 | 0 | // - whether the item or page exists. |
685 | 0 | // - whether the annotation already exists. |
686 | 0 | // - the nameID associated with the annotation name. |
687 | 0 | // - the id and dateAdded of the old annotation, if it exists. |
688 | 0 | nsCOMPtr<mozIStorageStatement> stmt = mDB->GetStatement( |
689 | 0 | "SELECT b.id, " |
690 | 0 | "(SELECT id FROM moz_anno_attributes WHERE name = :anno_name) AS nameid, " |
691 | 0 | "a.id, a.dateAdded, b.parent, b.type, b.lastModified, b.guid, p.guid " |
692 | 0 | "FROM moz_bookmarks b " |
693 | 0 | "JOIN moz_bookmarks p ON p.id = b.parent " |
694 | 0 | "LEFT JOIN moz_items_annos a ON a.item_id = b.id " |
695 | 0 | "AND a.anno_attribute_id = nameid " |
696 | 0 | "WHERE b.id = :item_id" |
697 | 0 | ); |
698 | 0 | NS_ENSURE_STATE(stmt); |
699 | 0 | mozStorageStatementScoper checkAnnoScoper(stmt); |
700 | 0 |
|
701 | 0 | rv = stmt->BindUTF8StringByName(NS_LITERAL_CSTRING("anno_name"), aName); |
702 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
703 | 0 | rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), aItemId); |
704 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
705 | 0 |
|
706 | 0 | bool hasResult; |
707 | 0 | rv = stmt->ExecuteStep(&hasResult); |
708 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
709 | 0 | if (!hasResult) { |
710 | 0 | // We are trying to create an annotation on an invalid bookmark |
711 | 0 | // or history entry. |
712 | 0 | return NS_ERROR_INVALID_ARG; |
713 | 0 | } |
714 | 0 | |
715 | 0 | int64_t fkId = stmt->AsInt64(0); |
716 | 0 | int64_t nameID = stmt->AsInt64(1); |
717 | 0 | int64_t oldAnnoId = stmt->AsInt64(2); |
718 | 0 | int64_t oldAnnoDate = stmt->AsInt64(3); |
719 | 0 |
|
720 | 0 | aStatement = mDB->GetStatement( |
721 | 0 | "INSERT OR REPLACE INTO moz_items_annos " |
722 | 0 | "(id, item_id, anno_attribute_id, content, flags, " |
723 | 0 | "expiration, type, dateAdded, lastModified) " |
724 | 0 | "VALUES (:id, :fk, :name_id, :content, :flags, " |
725 | 0 | ":expiration, :type, :date_added, :last_modified)" |
726 | 0 | ); |
727 | 0 |
|
728 | 0 | // Since we're already querying `moz_bookmarks`, we fetch the changed |
729 | 0 | // bookmark's info here, instead of using `FetchItemInfo`. |
730 | 0 | MOZ_ASSERT(aBookmark); |
731 | 0 | aBookmark->id = fkId; |
732 | 0 | aBookmark->parentId = stmt->AsInt64(4); |
733 | 0 | aBookmark->type = stmt->AsInt64(5); |
734 | 0 |
|
735 | 0 | aBookmark->lastModified = static_cast<PRTime>(stmt->AsInt64(6)); |
736 | 0 | if (NS_FAILED(stmt->GetUTF8String(7, aBookmark->guid)) || |
737 | 0 | NS_FAILED(stmt->GetUTF8String(8, aBookmark->parentGuid))) { |
738 | 0 | aBookmark->id = -1; |
739 | 0 | } |
740 | 0 | NS_ENSURE_STATE(aStatement); |
741 | 0 | mozStorageStatementScoper setAnnoScoper(aStatement); |
742 | 0 |
|
743 | 0 | // Don't replace existing annotations. |
744 | 0 | if (oldAnnoId > 0) { |
745 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("id"), oldAnnoId); |
746 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
747 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("date_added"), oldAnnoDate); |
748 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
749 | 0 | } |
750 | 0 | else { |
751 | 0 | rv = aStatement->BindNullByName(NS_LITERAL_CSTRING("id")); |
752 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
753 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("date_added"), RoundedPRNow()); |
754 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
755 | 0 | } |
756 | 0 |
|
757 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("fk"), fkId); |
758 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
759 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("name_id"), nameID); |
760 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
761 | 0 |
|
762 | 0 | rv = aStatement->BindInt32ByName(NS_LITERAL_CSTRING("flags"), aFlags); |
763 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
764 | 0 | rv = aStatement->BindInt32ByName(NS_LITERAL_CSTRING("expiration"), aExpiration); |
765 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
766 | 0 | rv = aStatement->BindInt32ByName(NS_LITERAL_CSTRING("type"), aType); |
767 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
768 | 0 | rv = aStatement->BindInt64ByName(NS_LITERAL_CSTRING("last_modified"), RoundedPRNow()); |
769 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
770 | 0 |
|
771 | 0 | // On success, leave the statement open, the caller will set the value |
772 | 0 | // and execute the statement. |
773 | 0 | setAnnoScoper.Abandon(); |
774 | 0 |
|
775 | 0 | return NS_OK; |
776 | 0 | } |