toolkit/components/places/History.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_places_History_h_
michael@0 8 #define mozilla_places_History_h_
michael@0 9
michael@0 10 #include "mozilla/IHistory.h"
michael@0 11 #include "mozilla/MemoryReporting.h"
michael@0 12 #include "mozilla/Mutex.h"
michael@0 13 #include "mozIAsyncHistory.h"
michael@0 14 #include "nsIDownloadHistory.h"
michael@0 15 #include "Database.h"
michael@0 16
michael@0 17 #include "mozilla/dom/Link.h"
michael@0 18 #include "nsTHashtable.h"
michael@0 19 #include "nsString.h"
michael@0 20 #include "nsURIHashKey.h"
michael@0 21 #include "nsTObserverArray.h"
michael@0 22 #include "nsDeque.h"
michael@0 23 #include "nsIMemoryReporter.h"
michael@0 24 #include "nsIObserver.h"
michael@0 25 #include "mozIStorageConnection.h"
michael@0 26
michael@0 27 namespace mozilla {
michael@0 28 namespace places {
michael@0 29
michael@0 30 struct VisitData;
michael@0 31
michael@0 32 #define NS_HISTORYSERVICE_CID \
michael@0 33 {0x0937a705, 0x91a6, 0x417a, {0x82, 0x92, 0xb2, 0x2e, 0xb1, 0x0d, 0xa8, 0x6c}}
michael@0 34
michael@0 35 // Max size of History::mRecentlyVisitedURIs
michael@0 36 #define RECENTLY_VISITED_URI_SIZE 8
michael@0 37
michael@0 38 class History : public IHistory
michael@0 39 , public nsIDownloadHistory
michael@0 40 , public mozIAsyncHistory
michael@0 41 , public nsIObserver
michael@0 42 , public nsIMemoryReporter
michael@0 43 {
michael@0 44 public:
michael@0 45 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 46 NS_DECL_IHISTORY
michael@0 47 NS_DECL_NSIDOWNLOADHISTORY
michael@0 48 NS_DECL_MOZIASYNCHISTORY
michael@0 49 NS_DECL_NSIOBSERVER
michael@0 50 NS_DECL_NSIMEMORYREPORTER
michael@0 51
michael@0 52 History();
michael@0 53
michael@0 54 /**
michael@0 55 * Obtains the statement to use to check if a URI is visited or not.
michael@0 56 */
michael@0 57 mozIStorageAsyncStatement* GetIsVisitedStatement();
michael@0 58
michael@0 59 /**
michael@0 60 * Adds an entry in moz_places with the data in aVisitData.
michael@0 61 *
michael@0 62 * @param aVisitData
michael@0 63 * The visit data to use to populate a new row in moz_places.
michael@0 64 */
michael@0 65 nsresult InsertPlace(const VisitData& aVisitData);
michael@0 66
michael@0 67 /**
michael@0 68 * Updates an entry in moz_places with the data in aVisitData.
michael@0 69 *
michael@0 70 * @param aVisitData
michael@0 71 * The visit data to use to update the existing row in moz_places.
michael@0 72 */
michael@0 73 nsresult UpdatePlace(const VisitData& aVisitData);
michael@0 74
michael@0 75 /**
michael@0 76 * Loads information about the page into _place from moz_places.
michael@0 77 *
michael@0 78 * @param _place
michael@0 79 * The VisitData for the place we need to know information about.
michael@0 80 * @param [out] _exists
michael@0 81 * Whether or the page was recorded in moz_places, false otherwise.
michael@0 82 */
michael@0 83 nsresult FetchPageInfo(VisitData& _place, bool* _exists);
michael@0 84
michael@0 85 /**
michael@0 86 * Get the number of bytes of memory this History object is using,
michael@0 87 * including sizeof(*this))
michael@0 88 */
michael@0 89 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
michael@0 90
michael@0 91 /**
michael@0 92 * Obtains a pointer to this service.
michael@0 93 */
michael@0 94 static History* GetService();
michael@0 95
michael@0 96 /**
michael@0 97 * Obtains a pointer that has had AddRef called on it. Used by the service
michael@0 98 * manager only.
michael@0 99 */
michael@0 100 static History* GetSingleton();
michael@0 101
michael@0 102 template<int N>
michael@0 103 already_AddRefed<mozIStorageStatement>
michael@0 104 GetStatement(const char (&aQuery)[N])
michael@0 105 {
michael@0 106 mozIStorageConnection* dbConn = GetDBConn();
michael@0 107 NS_ENSURE_TRUE(dbConn, nullptr);
michael@0 108 return mDB->GetStatement(aQuery);
michael@0 109 }
michael@0 110
michael@0 111 already_AddRefed<mozIStorageStatement>
michael@0 112 GetStatement(const nsACString& aQuery)
michael@0 113 {
michael@0 114 mozIStorageConnection* dbConn = GetDBConn();
michael@0 115 NS_ENSURE_TRUE(dbConn, nullptr);
michael@0 116 return mDB->GetStatement(aQuery);
michael@0 117 }
michael@0 118
michael@0 119 bool IsShuttingDown() const {
michael@0 120 return mShuttingDown;
michael@0 121 }
michael@0 122 Mutex& GetShutdownMutex() {
michael@0 123 return mShutdownMutex;
michael@0 124 }
michael@0 125
michael@0 126 /**
michael@0 127 * Helper function to append a new URI to mRecentlyVisitedURIs. See
michael@0 128 * mRecentlyVisitedURIs.
michael@0 129 */
michael@0 130 void AppendToRecentlyVisitedURIs(nsIURI* aURI);
michael@0 131
michael@0 132 private:
michael@0 133 virtual ~History();
michael@0 134
michael@0 135 void InitMemoryReporter();
michael@0 136
michael@0 137 /**
michael@0 138 * Obtains a read-write database connection.
michael@0 139 */
michael@0 140 mozIStorageConnection* GetDBConn();
michael@0 141
michael@0 142 /**
michael@0 143 * The database handle. This is initialized lazily by the first call to
michael@0 144 * GetDBConn(), so never use it directly, or, if you really need, always
michael@0 145 * invoke GetDBConn() before.
michael@0 146 */
michael@0 147 nsRefPtr<mozilla::places::Database> mDB;
michael@0 148
michael@0 149 /**
michael@0 150 * A read-only database connection used for checking if a URI is visited.
michael@0 151 *
michael@0 152 * @note this should only be accessed by GetIsVisistedStatement and Shutdown.
michael@0 153 */
michael@0 154 nsCOMPtr<mozIStorageConnection> mReadOnlyDBConn;
michael@0 155
michael@0 156 /**
michael@0 157 * An asynchronous statement to query if a URI is visited or not.
michael@0 158 *
michael@0 159 * @note this should only be accessed by GetIsVisistedStatement and Shutdown.
michael@0 160 */
michael@0 161 nsCOMPtr<mozIStorageAsyncStatement> mIsVisitedStatement;
michael@0 162
michael@0 163 /**
michael@0 164 * Remove any memory references to tasks and do not take on any more.
michael@0 165 */
michael@0 166 void Shutdown();
michael@0 167
michael@0 168 static History* gService;
michael@0 169
michael@0 170 // Ensures new tasks aren't started on destruction.
michael@0 171 bool mShuttingDown;
michael@0 172 // This mutex guards mShuttingDown. Code running in other threads that might
michael@0 173 // schedule tasks that use the database should grab it and check the value of
michael@0 174 // mShuttingDown. If we are already shutting down, the code must gracefully
michael@0 175 // avoid using the db. If we are not, the lock will prevent shutdown from
michael@0 176 // starting in an unexpected moment.
michael@0 177 Mutex mShutdownMutex;
michael@0 178
michael@0 179 typedef nsTObserverArray<mozilla::dom::Link* > ObserverArray;
michael@0 180
michael@0 181 class KeyClass : public nsURIHashKey
michael@0 182 {
michael@0 183 public:
michael@0 184 KeyClass(const nsIURI* aURI)
michael@0 185 : nsURIHashKey(aURI)
michael@0 186 {
michael@0 187 }
michael@0 188 KeyClass(const KeyClass& aOther)
michael@0 189 : nsURIHashKey(aOther)
michael@0 190 {
michael@0 191 NS_NOTREACHED("Do not call me!");
michael@0 192 }
michael@0 193 ObserverArray array;
michael@0 194 };
michael@0 195
michael@0 196 /**
michael@0 197 * Helper function for nsTHashtable::SizeOfExcludingThis call in
michael@0 198 * SizeOfIncludingThis().
michael@0 199 */
michael@0 200 static size_t SizeOfEntryExcludingThis(KeyClass* aEntry,
michael@0 201 mozilla::MallocSizeOf aMallocSizeOf,
michael@0 202 void*);
michael@0 203
michael@0 204 nsTHashtable<KeyClass> mObservers;
michael@0 205
michael@0 206 /**
michael@0 207 * mRecentlyVisitedURIs remembers URIs which are recently added to the DB,
michael@0 208 * to avoid saving these locations repeatedly in a short period.
michael@0 209 */
michael@0 210 typedef nsAutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE>
michael@0 211 RecentlyVisitedArray;
michael@0 212 RecentlyVisitedArray mRecentlyVisitedURIs;
michael@0 213 RecentlyVisitedArray::index_type mRecentlyVisitedURIsNextIndex;
michael@0 214
michael@0 215 bool IsRecentlyVisitedURI(nsIURI* aURI);
michael@0 216 };
michael@0 217
michael@0 218 } // namespace places
michael@0 219 } // namespace mozilla
michael@0 220
michael@0 221 #endif // mozilla_places_History_h_

mercurial