Source code

Revision control

Other Tools

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