Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/places/nsNavBookmarks.h
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
#ifndef nsNavBookmarks_h_
7
#define nsNavBookmarks_h_
8
9
#include "nsINavBookmarksService.h"
10
#include "nsNavHistory.h"
11
#include "nsToolkitCompsCID.h"
12
#include "nsCategoryCache.h"
13
#include "nsTHashtable.h"
14
#include "nsWeakReference.h"
15
#include "mozilla/Attributes.h"
16
#include "prtime.h"
17
18
class nsNavBookmarks;
19
20
namespace mozilla {
21
namespace places {
22
23
  enum BookmarkStatementId {
24
    DB_FIND_REDIRECTED_BOOKMARK = 0
25
  , DB_GET_BOOKMARKS_FOR_URI
26
  };
27
28
  struct BookmarkData {
29
    int64_t id = -1;
30
    nsCString url;
31
    nsCString title;
32
    int32_t position = -1;
33
    int64_t placeId = -1;
34
    int64_t parentId = -1;
35
    int64_t grandParentId = -1;
36
    int32_t type = 0;
37
    int32_t syncStatus = nsINavBookmarksService::SYNC_STATUS_UNKNOWN;
38
    nsCString serviceCID;
39
    PRTime dateAdded = 0;
40
    PRTime lastModified = 0;
41
    nsCString guid;
42
    nsCString parentGuid;
43
  };
44
45
  struct ItemVisitData {
46
    BookmarkData bookmark;
47
    int64_t visitId;
48
    uint32_t transitionType;
49
    PRTime time;
50
  };
51
52
  struct ItemChangeData {
53
    BookmarkData bookmark;
54
    bool isAnnotation = false;
55
    bool updateLastModified = false;
56
    uint16_t source = nsINavBookmarksService::SOURCE_DEFAULT;
57
    nsCString property;
58
    nsCString newValue;
59
    nsCString oldValue;
60
  };
61
62
  struct TombstoneData {
63
    nsCString guid;
64
    PRTime dateRemoved;
65
  };
66
67
  typedef void (nsNavBookmarks::*ItemVisitMethod)(const ItemVisitData&);
68
  typedef void (nsNavBookmarks::*ItemChangeMethod)(const ItemChangeData&);
69
70
  enum BookmarkDate {
71
    LAST_MODIFIED
72
  };
73
74
} // namespace places
75
} // namespace mozilla
76
77
class nsNavBookmarks final : public nsINavBookmarksService
78
                           , public nsINavHistoryObserver
79
                           , public nsIObserver
80
                           , public nsSupportsWeakReference
81
                           , public mozilla::places::INativePlacesEventCallback
82
{
83
public:
84
  NS_DECL_ISUPPORTS
85
  NS_DECL_NSINAVBOOKMARKSSERVICE
86
  NS_DECL_NSINAVHISTORYOBSERVER
87
  NS_DECL_NSIOBSERVER
88
89
  nsNavBookmarks();
90
91
  /**
92
   * Obtains the service's object.
93
   */
94
  static already_AddRefed<nsNavBookmarks> GetSingleton();
95
96
  /**
97
   * Initializes the service's object.  This should only be called once.
98
   */
99
  nsresult Init();
100
101
0
  static nsNavBookmarks* GetBookmarksService() {
102
0
    if (!gBookmarksService) {
103
0
      nsCOMPtr<nsINavBookmarksService> serv =
104
0
        do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID);
105
0
      NS_ENSURE_TRUE(serv, nullptr);
106
0
      NS_ASSERTION(gBookmarksService,
107
0
                   "Should have static instance pointer now");
108
0
    }
109
0
    return gBookmarksService;
110
0
  }
111
112
  typedef mozilla::places::BookmarkData BookmarkData;
113
  typedef mozilla::places::ItemVisitData ItemVisitData;
114
  typedef mozilla::places::ItemChangeData ItemChangeData;
115
  typedef mozilla::places::BookmarkStatementId BookmarkStatementId;
116
117
  nsresult OnVisit(nsIURI* aURI, int64_t aVisitId, PRTime aTime,
118
                   int64_t aSessionId, int64_t aReferringId,
119
                   uint32_t aTransitionType, const nsACString& aGUID,
120
                   bool aHidden, uint32_t aVisitCount,
121
                   uint32_t aTyped, const nsAString& aLastKnownTitle);
122
123
  nsresult GetBookmarkURI(int64_t aItemId, nsIURI** _URI);
124
125
  nsresult ResultNodeForContainer(const nsCString& aGUID,
126
                                  nsNavHistoryQueryOptions* aOptions,
127
                                  nsNavHistoryResultNode** aNode);
128
129
  // Find all the children of a folder, using the given query and options.
130
  // For each child, a ResultNode is created and added to |children|.
131
  // The results are ordered by folder position.
132
  nsresult QueryFolderChildren(int64_t aFolderId,
133
                               nsNavHistoryQueryOptions* aOptions,
134
                               nsCOMArray<nsNavHistoryResultNode>* children);
135
136
  /**
137
   * Turns aRow into a node and appends it to aChildren if it is appropriate to
138
   * do so.
139
   *
140
   * @param aRow
141
   *        A Storage statement (in the case of synchronous execution) or row of
142
   *        a result set (in the case of asynchronous execution).
143
   * @param aOptions
144
   *        The options of the parent folder node. These are the options used
145
   *        to fill the parent node.
146
   * @param aChildren
147
   *        The children of the parent folder node.
148
   * @param aCurrentIndex
149
   *        The index of aRow within the results.  When called on the first row,
150
   *        this should be set to -1.
151
   */
152
  nsresult ProcessFolderNodeRow(mozIStorageValueArray* aRow,
153
                                nsNavHistoryQueryOptions* aOptions,
154
                                nsCOMArray<nsNavHistoryResultNode>* aChildren,
155
                                int32_t& aCurrentIndex);
156
157
  /**
158
   * The async version of QueryFolderChildren.
159
   *
160
   * @param aNode
161
   *        The folder node that will receive the children.
162
   * @param _pendingStmt
163
   *        The Storage pending statement that will be used to control async
164
   *        execution.
165
   */
166
  nsresult QueryFolderChildrenAsync(nsNavHistoryFolderResultNode* aNode,
167
                                    mozIStoragePendingStatement** _pendingStmt);
168
169
  /**
170
   * Fetches information about the specified id from the database.
171
   *
172
   * @param aItemId
173
   *        Id of the item to fetch information for.
174
   * @param aBookmark
175
   *        BookmarkData to store the information.
176
   */
177
  nsresult FetchItemInfo(int64_t aItemId,
178
                         BookmarkData& _bookmark);
179
180
  /**
181
   * Fetches information about the specified GUID from the database.
182
   *
183
   * @param aGUID
184
   *        GUID of the item to fetch information for.
185
   * @param aBookmark
186
   *        BookmarkData to store the information.
187
   */
188
  nsresult FetchItemInfo(const nsCString &aGUID,
189
                         BookmarkData& _bookmark);
190
191
/**
192
   * Notifies that a bookmark has been visited.
193
   *
194
   * @param aItemId
195
   *        The visited item id.
196
   * @param aData
197
   *        Details about the new visit.
198
   */
199
  void NotifyItemVisited(const ItemVisitData& aData);
200
201
  /**
202
   * Notifies that a bookmark has changed.
203
   *
204
   * @param aItemId
205
   *        The changed item id.
206
   * @param aData
207
   *        Details about the change.
208
   */
209
  void NotifyItemChanged(const ItemChangeData& aData);
210
211
212
  /**
213
   * Part of INativePlacesEventCallback - handles events from the places
214
   * observer system.
215
   * @param aCx
216
   *        A JSContext for extracting the values from aEvents.
217
   * @param aEvents
218
   *        An array of weakly typed events detailing what changed.
219
   */
220
  void HandlePlacesEvent(const PlacesEventSequence& aEvents) override;
221
  static const int32_t kGetChildrenIndex_Guid;
222
  static const int32_t kGetChildrenIndex_Position;
223
  static const int32_t kGetChildrenIndex_Type;
224
  static const int32_t kGetChildrenIndex_PlaceID;
225
  static const int32_t kGetChildrenIndex_SyncStatus;
226
227
  static mozilla::Atomic<int64_t> sLastInsertedItemId;
228
  static void StoreLastInsertedId(const nsACString& aTable,
229
                                  const int64_t aLastInsertedId);
230
231
  static mozilla::Atomic<int64_t> sTotalSyncChanges;
232
  static void NoteSyncChange();
233
234
private:
235
  static nsNavBookmarks* gBookmarksService;
236
237
  ~nsNavBookmarks();
238
239
  /**
240
   * Checks whether or not aFolderId points to a live bookmark.
241
   *
242
   * @param aFolderId
243
   *        the item-id of the folder to check.
244
   * @return true if aFolderId points to live bookmarks, false otherwise.
245
   */
246
  bool IsLivemark(int64_t aFolderId);
247
248
  nsresult AdjustIndices(int64_t aFolder,
249
                         int32_t aStartIndex,
250
                         int32_t aEndIndex,
251
                         int32_t aDelta);
252
253
  nsresult AdjustSeparatorsSyncCounter(int64_t aFolderId,
254
                                       int32_t aStartIndex,
255
                                       int64_t aSyncChangeDelta);
256
257
  /**
258
   * Fetches properties of a folder.
259
   *
260
   * @param aFolderId
261
   *        Folder to count children for.
262
   * @param _folderCount
263
   *        Number of children in the folder.
264
   * @param _guid
265
   *        Unique id of the folder.
266
   * @param _parentId
267
   *        Id of the parent of the folder.
268
   *
269
   * @throws If folder does not exist.
270
   */
271
  nsresult FetchFolderInfo(int64_t aFolderId,
272
                           int32_t* _folderCount,
273
                           nsACString& _guid,
274
                           int64_t* _parentId);
275
276
  nsresult AddSyncChangesForBookmarksWithURL(const nsACString& aURL,
277
                                             int64_t aSyncChangeDelta);
278
279
  // Bumps the change counter for all bookmarks with |aURI|. This is used to
280
  // update tagged bookmarks when adding or changing a tag entry.
281
  nsresult AddSyncChangesForBookmarksWithURI(nsIURI* aURI,
282
                                             int64_t aSyncChangeDelta);
283
284
  // Bumps the change counter for all bookmarked URLs within |aFolderId|. This
285
  // is used to update tagged bookmarks when changing or removing a tag folder.
286
  nsresult AddSyncChangesForBookmarksInFolder(int64_t aFolderId,
287
                                              int64_t aSyncChangeDelta);
288
289
  // Inserts a tombstone for a removed synced item.
290
  nsresult InsertTombstone(const BookmarkData& aBookmark);
291
292
  // Inserts tombstones for removed synced items.
293
  nsresult InsertTombstones(const nsTArray<mozilla::places::TombstoneData>& aTombstones);
294
295
  // Removes a stale synced bookmark tombstone.
296
  nsresult RemoveTombstone(const nsACString& aGUID);
297
298
  nsresult SetItemTitleInternal(BookmarkData& aBookmark,
299
                                const nsACString& aTitle,
300
                                int64_t aSyncChangeDelta);
301
302
  /**
303
   * This is an handle to the Places database.
304
   */
305
  RefPtr<mozilla::places::Database> mDB;
306
307
  nsMaybeWeakPtrArray<nsINavBookmarkObserver> mObservers;
308
309
0
  int64_t TagsRootId() {
310
0
    return mDB->GetTagsFolderId();
311
0
  }
312
313
0
  inline bool IsRoot(int64_t aFolderId) {
314
0
    return aFolderId == mDB->GetRootFolderId() ||
315
0
           aFolderId == mDB->GetMenuFolderId() ||
316
0
           aFolderId == mDB->GetTagsFolderId() ||
317
0
           aFolderId == mDB->GetUnfiledFolderId() ||
318
0
           aFolderId == mDB->GetToolbarFolderId() ||
319
0
           aFolderId == mDB->GetMobileFolderId();
320
0
  }
321
322
  nsresult SetItemDateInternal(enum mozilla::places::BookmarkDate aDateType,
323
                               int64_t aSyncChangeDelta,
324
                               int64_t aItemId,
325
                               PRTime aValue);
326
327
  nsresult RemoveFolderChildren(int64_t aFolderId, uint16_t aSource);
328
329
  // Recursive method to build an array of folder's children
330
  nsresult GetDescendantChildren(int64_t aFolderId,
331
                                 const nsACString& aFolderGuid,
332
                                 int64_t aGrandParentId,
333
                                 nsTArray<BookmarkData>& aFolderChildrenArray);
334
335
  enum ItemType {
336
    BOOKMARK = TYPE_BOOKMARK,
337
    FOLDER = TYPE_FOLDER,
338
    SEPARATOR = TYPE_SEPARATOR,
339
  };
340
341
  /**
342
   * Helper to insert a bookmark in the database.
343
   *
344
   *  @param aItemId
345
   *         The itemId to insert, pass -1 to generate a new one.
346
   *  @param aPlaceId
347
   *         The placeId to which this bookmark refers to, pass nullptr for
348
   *         items that don't refer to an URI (eg. folders, separators, ...).
349
   *  @param aItemType
350
   *         The type of the new bookmark, see TYPE_* constants.
351
   *  @param aParentId
352
   *         The itemId of the parent folder.
353
   *  @param aIndex
354
   *         The position inside the parent folder.
355
   *  @param aTitle
356
   *         The title for the new bookmark.
357
   *         Pass a void string to set a NULL title.
358
   *  @param aDateAdded
359
   *         The date for the insertion.
360
   *  @param [optional] aLastModified
361
   *         The last modified date for the insertion.
362
   *         It defaults to aDateAdded.
363
   *
364
   *  @return The new item id that has been inserted.
365
   *
366
   *  @note This will also update last modified date of the parent folder.
367
   */
368
  nsresult InsertBookmarkInDB(int64_t aPlaceId,
369
                              enum ItemType aItemType,
370
                              int64_t aParentId,
371
                              int32_t aIndex,
372
                              const nsACString& aTitle,
373
                              PRTime aDateAdded,
374
                              PRTime aLastModified,
375
                              const nsACString& aParentGuid,
376
                              int64_t aGrandParentId,
377
                              nsIURI* aURI,
378
                              uint16_t aSource,
379
                              int64_t* _itemId,
380
                              nsACString& _guid);
381
382
  nsresult GetBookmarksForURI(nsIURI* aURI,
383
                              nsTArray<BookmarkData>& _bookmarks);
384
385
  // Used to enable and disable the observer notifications.
386
  bool mCanNotify;
387
};
388
389
#endif // nsNavBookmarks_h_