toolkit/components/places/nsNavBookmarks.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsNavBookmarks_h_
michael@0 7 #define nsNavBookmarks_h_
michael@0 8
michael@0 9 #include "nsINavBookmarksService.h"
michael@0 10 #include "nsIAnnotationService.h"
michael@0 11 #include "nsITransaction.h"
michael@0 12 #include "nsNavHistory.h"
michael@0 13 #include "nsToolkitCompsCID.h"
michael@0 14 #include "nsCategoryCache.h"
michael@0 15 #include "nsTHashtable.h"
michael@0 16 #include "nsWeakReference.h"
michael@0 17 #include "mozilla/Attributes.h"
michael@0 18 #include "prtime.h"
michael@0 19
michael@0 20 class nsNavBookmarks;
michael@0 21 class nsIOutputStream;
michael@0 22
michael@0 23 namespace mozilla {
michael@0 24 namespace places {
michael@0 25
michael@0 26 enum BookmarkStatementId {
michael@0 27 DB_FIND_REDIRECTED_BOOKMARK = 0
michael@0 28 , DB_GET_BOOKMARKS_FOR_URI
michael@0 29 };
michael@0 30
michael@0 31 struct BookmarkData {
michael@0 32 int64_t id;
michael@0 33 nsCString url;
michael@0 34 nsCString title;
michael@0 35 int32_t position;
michael@0 36 int64_t placeId;
michael@0 37 int64_t parentId;
michael@0 38 int64_t grandParentId;
michael@0 39 int32_t type;
michael@0 40 nsCString serviceCID;
michael@0 41 PRTime dateAdded;
michael@0 42 PRTime lastModified;
michael@0 43 nsCString guid;
michael@0 44 nsCString parentGuid;
michael@0 45 };
michael@0 46
michael@0 47 struct ItemVisitData {
michael@0 48 BookmarkData bookmark;
michael@0 49 int64_t visitId;
michael@0 50 uint32_t transitionType;
michael@0 51 PRTime time;
michael@0 52 };
michael@0 53
michael@0 54 struct ItemChangeData {
michael@0 55 BookmarkData bookmark;
michael@0 56 nsCString property;
michael@0 57 bool isAnnotation;
michael@0 58 nsCString newValue;
michael@0 59 };
michael@0 60
michael@0 61 typedef void (nsNavBookmarks::*ItemVisitMethod)(const ItemVisitData&);
michael@0 62 typedef void (nsNavBookmarks::*ItemChangeMethod)(const ItemChangeData&);
michael@0 63
michael@0 64 class BookmarkKeyClass : public nsTrimInt64HashKey
michael@0 65 {
michael@0 66 public:
michael@0 67 BookmarkKeyClass(const int64_t* aItemId)
michael@0 68 : nsTrimInt64HashKey(aItemId)
michael@0 69 , creationTime(PR_Now())
michael@0 70 {
michael@0 71 }
michael@0 72 BookmarkKeyClass(const BookmarkKeyClass& aOther)
michael@0 73 : nsTrimInt64HashKey(aOther)
michael@0 74 , creationTime(PR_Now())
michael@0 75 {
michael@0 76 NS_NOTREACHED("Do not call me!");
michael@0 77 }
michael@0 78 BookmarkData bookmark;
michael@0 79 PRTime creationTime;
michael@0 80 };
michael@0 81
michael@0 82 enum BookmarkDate {
michael@0 83 DATE_ADDED = 0
michael@0 84 , LAST_MODIFIED
michael@0 85 };
michael@0 86
michael@0 87 } // namespace places
michael@0 88 } // namespace mozilla
michael@0 89
michael@0 90 class nsNavBookmarks MOZ_FINAL : public nsINavBookmarksService
michael@0 91 , public nsINavHistoryObserver
michael@0 92 , public nsIAnnotationObserver
michael@0 93 , public nsIObserver
michael@0 94 , public nsSupportsWeakReference
michael@0 95 {
michael@0 96 public:
michael@0 97 NS_DECL_ISUPPORTS
michael@0 98 NS_DECL_NSINAVBOOKMARKSSERVICE
michael@0 99 NS_DECL_NSINAVHISTORYOBSERVER
michael@0 100 NS_DECL_NSIANNOTATIONOBSERVER
michael@0 101 NS_DECL_NSIOBSERVER
michael@0 102
michael@0 103 nsNavBookmarks();
michael@0 104
michael@0 105 /**
michael@0 106 * Obtains the service's object.
michael@0 107 */
michael@0 108 static already_AddRefed<nsNavBookmarks> GetSingleton();
michael@0 109
michael@0 110 /**
michael@0 111 * Initializes the service's object. This should only be called once.
michael@0 112 */
michael@0 113 nsresult Init();
michael@0 114
michael@0 115 static nsNavBookmarks* GetBookmarksService() {
michael@0 116 if (!gBookmarksService) {
michael@0 117 nsCOMPtr<nsINavBookmarksService> serv =
michael@0 118 do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID);
michael@0 119 NS_ENSURE_TRUE(serv, nullptr);
michael@0 120 NS_ASSERTION(gBookmarksService,
michael@0 121 "Should have static instance pointer now");
michael@0 122 }
michael@0 123 return gBookmarksService;
michael@0 124 }
michael@0 125
michael@0 126 typedef mozilla::places::BookmarkData BookmarkData;
michael@0 127 typedef mozilla::places::BookmarkKeyClass BookmarkKeyClass;
michael@0 128 typedef mozilla::places::ItemVisitData ItemVisitData;
michael@0 129 typedef mozilla::places::ItemChangeData ItemChangeData;
michael@0 130 typedef mozilla::places::BookmarkStatementId BookmarkStatementId;
michael@0 131
michael@0 132 nsresult ResultNodeForContainer(int64_t aID,
michael@0 133 nsNavHistoryQueryOptions* aOptions,
michael@0 134 nsNavHistoryResultNode** aNode);
michael@0 135
michael@0 136 // Find all the children of a folder, using the given query and options.
michael@0 137 // For each child, a ResultNode is created and added to |children|.
michael@0 138 // The results are ordered by folder position.
michael@0 139 nsresult QueryFolderChildren(int64_t aFolderId,
michael@0 140 nsNavHistoryQueryOptions* aOptions,
michael@0 141 nsCOMArray<nsNavHistoryResultNode>* children);
michael@0 142
michael@0 143 /**
michael@0 144 * Turns aRow into a node and appends it to aChildren if it is appropriate to
michael@0 145 * do so.
michael@0 146 *
michael@0 147 * @param aRow
michael@0 148 * A Storage statement (in the case of synchronous execution) or row of
michael@0 149 * a result set (in the case of asynchronous execution).
michael@0 150 * @param aOptions
michael@0 151 * The options of the parent folder node.
michael@0 152 * @param aChildren
michael@0 153 * The children of the parent folder node.
michael@0 154 * @param aCurrentIndex
michael@0 155 * The index of aRow within the results. When called on the first row,
michael@0 156 * this should be set to -1.
michael@0 157 */
michael@0 158 nsresult ProcessFolderNodeRow(mozIStorageValueArray* aRow,
michael@0 159 nsNavHistoryQueryOptions* aOptions,
michael@0 160 nsCOMArray<nsNavHistoryResultNode>* aChildren,
michael@0 161 int32_t& aCurrentIndex);
michael@0 162
michael@0 163 /**
michael@0 164 * The async version of QueryFolderChildren.
michael@0 165 *
michael@0 166 * @param aNode
michael@0 167 * The folder node that will receive the children.
michael@0 168 * @param _pendingStmt
michael@0 169 * The Storage pending statement that will be used to control async
michael@0 170 * execution.
michael@0 171 */
michael@0 172 nsresult QueryFolderChildrenAsync(nsNavHistoryFolderResultNode* aNode,
michael@0 173 int64_t aFolderId,
michael@0 174 mozIStoragePendingStatement** _pendingStmt);
michael@0 175
michael@0 176 /**
michael@0 177 * @return index of the new folder in aIndex, whether it was passed in or
michael@0 178 * generated by autoincrement.
michael@0 179 *
michael@0 180 * @note If aFolder is -1, uses the autoincrement id for folder index.
michael@0 181 * @note aTitle will be truncated to TITLE_LENGTH_MAX
michael@0 182 */
michael@0 183 nsresult CreateContainerWithID(int64_t aId, int64_t aParent,
michael@0 184 const nsACString& aTitle,
michael@0 185 bool aIsBookmarkFolder,
michael@0 186 int32_t* aIndex,
michael@0 187 const nsACString& aGUID,
michael@0 188 int64_t* aNewFolder);
michael@0 189
michael@0 190 /**
michael@0 191 * Fetches information about the specified id from the database.
michael@0 192 *
michael@0 193 * @param aItemId
michael@0 194 * Id of the item to fetch information for.
michael@0 195 * @param aBookmark
michael@0 196 * BookmarkData to store the information.
michael@0 197 */
michael@0 198 nsresult FetchItemInfo(int64_t aItemId,
michael@0 199 BookmarkData& _bookmark);
michael@0 200
michael@0 201 /**
michael@0 202 * Notifies that a bookmark has been visited.
michael@0 203 *
michael@0 204 * @param aItemId
michael@0 205 * The visited item id.
michael@0 206 * @param aData
michael@0 207 * Details about the new visit.
michael@0 208 */
michael@0 209 void NotifyItemVisited(const ItemVisitData& aData);
michael@0 210
michael@0 211 /**
michael@0 212 * Notifies that a bookmark has changed.
michael@0 213 *
michael@0 214 * @param aItemId
michael@0 215 * The changed item id.
michael@0 216 * @param aData
michael@0 217 * Details about the change.
michael@0 218 */
michael@0 219 void NotifyItemChanged(const ItemChangeData& aData);
michael@0 220
michael@0 221 /**
michael@0 222 * Recursively builds an array of descendant folders inside a given folder.
michael@0 223 *
michael@0 224 * @param aFolderId
michael@0 225 * The folder to fetch descendants from.
michael@0 226 * @param aDescendantFoldersArray
michael@0 227 * Output array to put descendant folders id.
michael@0 228 */
michael@0 229 nsresult GetDescendantFolders(int64_t aFolderId,
michael@0 230 nsTArray<int64_t>& aDescendantFoldersArray);
michael@0 231
michael@0 232 private:
michael@0 233 static nsNavBookmarks* gBookmarksService;
michael@0 234
michael@0 235 ~nsNavBookmarks();
michael@0 236
michael@0 237 /**
michael@0 238 * Locates the root items in the bookmarks folder hierarchy assigning folder
michael@0 239 * ids to the root properties that are exposed through the service interface.
michael@0 240 */
michael@0 241 nsresult ReadRoots();
michael@0 242
michael@0 243 nsresult AdjustIndices(int64_t aFolder,
michael@0 244 int32_t aStartIndex,
michael@0 245 int32_t aEndIndex,
michael@0 246 int32_t aDelta);
michael@0 247
michael@0 248 /**
michael@0 249 * Fetches properties of a folder.
michael@0 250 *
michael@0 251 * @param aFolderId
michael@0 252 * Folder to count children for.
michael@0 253 * @param _folderCount
michael@0 254 * Number of children in the folder.
michael@0 255 * @param _guid
michael@0 256 * Unique id of the folder.
michael@0 257 * @param _parentId
michael@0 258 * Id of the parent of the folder.
michael@0 259 *
michael@0 260 * @throws If folder does not exist.
michael@0 261 */
michael@0 262 nsresult FetchFolderInfo(int64_t aFolderId,
michael@0 263 int32_t* _folderCount,
michael@0 264 nsACString& _guid,
michael@0 265 int64_t* _parentId);
michael@0 266
michael@0 267 nsresult GetLastChildId(int64_t aFolder, int64_t* aItemId);
michael@0 268
michael@0 269 /**
michael@0 270 * This is an handle to the Places database.
michael@0 271 */
michael@0 272 nsRefPtr<mozilla::places::Database> mDB;
michael@0 273
michael@0 274 int32_t mItemCount;
michael@0 275
michael@0 276 nsMaybeWeakPtrArray<nsINavBookmarkObserver> mObservers;
michael@0 277
michael@0 278 int64_t mRoot;
michael@0 279 int64_t mMenuRoot;
michael@0 280 int64_t mTagsRoot;
michael@0 281 int64_t mUnfiledRoot;
michael@0 282 int64_t mToolbarRoot;
michael@0 283
michael@0 284 inline bool IsRoot(int64_t aFolderId) {
michael@0 285 return aFolderId == mRoot || aFolderId == mMenuRoot ||
michael@0 286 aFolderId == mTagsRoot || aFolderId == mUnfiledRoot ||
michael@0 287 aFolderId == mToolbarRoot;
michael@0 288 }
michael@0 289
michael@0 290 nsresult IsBookmarkedInDatabase(int64_t aBookmarkID, bool* aIsBookmarked);
michael@0 291
michael@0 292 nsresult SetItemDateInternal(enum mozilla::places::BookmarkDate aDateType,
michael@0 293 int64_t aItemId,
michael@0 294 PRTime aValue);
michael@0 295
michael@0 296 // Recursive method to build an array of folder's children
michael@0 297 nsresult GetDescendantChildren(int64_t aFolderId,
michael@0 298 const nsACString& aFolderGuid,
michael@0 299 int64_t aGrandParentId,
michael@0 300 nsTArray<BookmarkData>& aFolderChildrenArray);
michael@0 301
michael@0 302 enum ItemType {
michael@0 303 BOOKMARK = TYPE_BOOKMARK,
michael@0 304 FOLDER = TYPE_FOLDER,
michael@0 305 SEPARATOR = TYPE_SEPARATOR,
michael@0 306 };
michael@0 307
michael@0 308 /**
michael@0 309 * Helper to insert a bookmark in the database.
michael@0 310 *
michael@0 311 * @param aItemId
michael@0 312 * The itemId to insert, pass -1 to generate a new one.
michael@0 313 * @param aPlaceId
michael@0 314 * The placeId to which this bookmark refers to, pass nullptr for
michael@0 315 * items that don't refer to an URI (eg. folders, separators, ...).
michael@0 316 * @param aItemType
michael@0 317 * The type of the new bookmark, see TYPE_* constants.
michael@0 318 * @param aParentId
michael@0 319 * The itemId of the parent folder.
michael@0 320 * @param aIndex
michael@0 321 * The position inside the parent folder.
michael@0 322 * @param aTitle
michael@0 323 * The title for the new bookmark.
michael@0 324 * Pass a void string to set a NULL title.
michael@0 325 * @param aDateAdded
michael@0 326 * The date for the insertion.
michael@0 327 * @param [optional] aLastModified
michael@0 328 * The last modified date for the insertion.
michael@0 329 * It defaults to aDateAdded.
michael@0 330 *
michael@0 331 * @return The new item id that has been inserted.
michael@0 332 *
michael@0 333 * @note This will also update last modified date of the parent folder.
michael@0 334 */
michael@0 335 nsresult InsertBookmarkInDB(int64_t aPlaceId,
michael@0 336 enum ItemType aItemType,
michael@0 337 int64_t aParentId,
michael@0 338 int32_t aIndex,
michael@0 339 const nsACString& aTitle,
michael@0 340 PRTime aDateAdded,
michael@0 341 PRTime aLastModified,
michael@0 342 const nsACString& aParentGuid,
michael@0 343 int64_t aGrandParentId,
michael@0 344 nsIURI* aURI,
michael@0 345 int64_t* _itemId,
michael@0 346 nsACString& _guid);
michael@0 347
michael@0 348 /**
michael@0 349 * TArray version of getBookmarksIdForURI for ease of use in C++ code.
michael@0 350 * Pass in a reference to a TArray; it will get filled with the
michael@0 351 * resulting list of bookmark IDs.
michael@0 352 *
michael@0 353 * @param aURI
michael@0 354 * URI to get bookmarks for.
michael@0 355 * @param aResult
michael@0 356 * Array of bookmark ids.
michael@0 357 * @param aSkipTags
michael@0 358 * If true ids of tags-as-bookmarks entries will be excluded.
michael@0 359 */
michael@0 360 nsresult GetBookmarkIdsForURITArray(nsIURI* aURI,
michael@0 361 nsTArray<int64_t>& aResult,
michael@0 362 bool aSkipTags);
michael@0 363
michael@0 364 nsresult GetBookmarksForURI(nsIURI* aURI,
michael@0 365 nsTArray<BookmarkData>& _bookmarks);
michael@0 366
michael@0 367 int64_t RecursiveFindRedirectedBookmark(int64_t aPlaceId);
michael@0 368
michael@0 369 static const int32_t kGetChildrenIndex_Position;
michael@0 370 static const int32_t kGetChildrenIndex_Type;
michael@0 371 static const int32_t kGetChildrenIndex_PlaceID;
michael@0 372 static const int32_t kGetChildrenIndex_Guid;
michael@0 373
michael@0 374 class RemoveFolderTransaction MOZ_FINAL : public nsITransaction {
michael@0 375 public:
michael@0 376 RemoveFolderTransaction(int64_t aID) : mID(aID) {}
michael@0 377
michael@0 378 NS_DECL_ISUPPORTS
michael@0 379
michael@0 380 NS_IMETHOD DoTransaction() {
michael@0 381 nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService();
michael@0 382 NS_ENSURE_TRUE(bookmarks, NS_ERROR_OUT_OF_MEMORY);
michael@0 383 BookmarkData folder;
michael@0 384 nsresult rv = bookmarks->FetchItemInfo(mID, folder);
michael@0 385 // TODO (Bug 656935): store the BookmarkData struct instead.
michael@0 386 mParent = folder.parentId;
michael@0 387 mIndex = folder.position;
michael@0 388
michael@0 389 rv = bookmarks->GetItemTitle(mID, mTitle);
michael@0 390 NS_ENSURE_SUCCESS(rv, rv);
michael@0 391
michael@0 392 return bookmarks->RemoveItem(mID);
michael@0 393 }
michael@0 394
michael@0 395 NS_IMETHOD UndoTransaction() {
michael@0 396 nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService();
michael@0 397 NS_ENSURE_TRUE(bookmarks, NS_ERROR_OUT_OF_MEMORY);
michael@0 398 int64_t newFolder;
michael@0 399 return bookmarks->CreateContainerWithID(mID, mParent, mTitle, true,
michael@0 400 &mIndex, EmptyCString(), &newFolder);
michael@0 401 }
michael@0 402
michael@0 403 NS_IMETHOD RedoTransaction() {
michael@0 404 return DoTransaction();
michael@0 405 }
michael@0 406
michael@0 407 NS_IMETHOD GetIsTransient(bool* aResult) {
michael@0 408 *aResult = false;
michael@0 409 return NS_OK;
michael@0 410 }
michael@0 411
michael@0 412 NS_IMETHOD Merge(nsITransaction* aTransaction, bool* aResult) {
michael@0 413 *aResult = false;
michael@0 414 return NS_OK;
michael@0 415 }
michael@0 416
michael@0 417 private:
michael@0 418 int64_t mID;
michael@0 419 int64_t mParent;
michael@0 420 nsCString mTitle;
michael@0 421 int32_t mIndex;
michael@0 422 };
michael@0 423
michael@0 424 // Used to enable and disable the observer notifications.
michael@0 425 bool mCanNotify;
michael@0 426 nsCategoryCache<nsINavBookmarkObserver> mCacheObservers;
michael@0 427
michael@0 428 // Tracks whether we are in batch mode.
michael@0 429 // Note: this is only tracking bookmarks batches, not history ones.
michael@0 430 bool mBatching;
michael@0 431
michael@0 432 /**
michael@0 433 * Always call EnsureKeywordsHash() and check it for errors before actually
michael@0 434 * using the hash. Internal keyword methods are already doing that.
michael@0 435 */
michael@0 436 nsresult EnsureKeywordsHash();
michael@0 437 nsDataHashtable<nsTrimInt64HashKey, nsString> mBookmarkToKeywordHash;
michael@0 438 bool mBookmarkToKeywordHashInitialized;
michael@0 439
michael@0 440 /**
michael@0 441 * This function must be called every time a bookmark is removed.
michael@0 442 *
michael@0 443 * @param aURI
michael@0 444 * Uri to test.
michael@0 445 */
michael@0 446 nsresult UpdateKeywordsHashForRemovedBookmark(int64_t aItemId);
michael@0 447
michael@0 448 /**
michael@0 449 * Cache for the last fetched BookmarkData entries.
michael@0 450 * This is used to speed up repeated requests to the same item id.
michael@0 451 */
michael@0 452 nsTHashtable<BookmarkKeyClass> mRecentBookmarksCache;
michael@0 453
michael@0 454 /**
michael@0 455 * Tracks bookmarks in the cache critical path. Items should not be
michael@0 456 * added to the cache till they are removed from this hash.
michael@0 457 */
michael@0 458 nsTHashtable<nsTrimInt64HashKey> mUncachableBookmarks;
michael@0 459 };
michael@0 460
michael@0 461 #endif // nsNavBookmarks_h_

mercurial