1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/History.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,221 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_places_History_h_ 1.11 +#define mozilla_places_History_h_ 1.12 + 1.13 +#include "mozilla/IHistory.h" 1.14 +#include "mozilla/MemoryReporting.h" 1.15 +#include "mozilla/Mutex.h" 1.16 +#include "mozIAsyncHistory.h" 1.17 +#include "nsIDownloadHistory.h" 1.18 +#include "Database.h" 1.19 + 1.20 +#include "mozilla/dom/Link.h" 1.21 +#include "nsTHashtable.h" 1.22 +#include "nsString.h" 1.23 +#include "nsURIHashKey.h" 1.24 +#include "nsTObserverArray.h" 1.25 +#include "nsDeque.h" 1.26 +#include "nsIMemoryReporter.h" 1.27 +#include "nsIObserver.h" 1.28 +#include "mozIStorageConnection.h" 1.29 + 1.30 +namespace mozilla { 1.31 +namespace places { 1.32 + 1.33 +struct VisitData; 1.34 + 1.35 +#define NS_HISTORYSERVICE_CID \ 1.36 + {0x0937a705, 0x91a6, 0x417a, {0x82, 0x92, 0xb2, 0x2e, 0xb1, 0x0d, 0xa8, 0x6c}} 1.37 + 1.38 +// Max size of History::mRecentlyVisitedURIs 1.39 +#define RECENTLY_VISITED_URI_SIZE 8 1.40 + 1.41 +class History : public IHistory 1.42 + , public nsIDownloadHistory 1.43 + , public mozIAsyncHistory 1.44 + , public nsIObserver 1.45 + , public nsIMemoryReporter 1.46 +{ 1.47 +public: 1.48 + NS_DECL_THREADSAFE_ISUPPORTS 1.49 + NS_DECL_IHISTORY 1.50 + NS_DECL_NSIDOWNLOADHISTORY 1.51 + NS_DECL_MOZIASYNCHISTORY 1.52 + NS_DECL_NSIOBSERVER 1.53 + NS_DECL_NSIMEMORYREPORTER 1.54 + 1.55 + History(); 1.56 + 1.57 + /** 1.58 + * Obtains the statement to use to check if a URI is visited or not. 1.59 + */ 1.60 + mozIStorageAsyncStatement* GetIsVisitedStatement(); 1.61 + 1.62 + /** 1.63 + * Adds an entry in moz_places with the data in aVisitData. 1.64 + * 1.65 + * @param aVisitData 1.66 + * The visit data to use to populate a new row in moz_places. 1.67 + */ 1.68 + nsresult InsertPlace(const VisitData& aVisitData); 1.69 + 1.70 + /** 1.71 + * Updates an entry in moz_places with the data in aVisitData. 1.72 + * 1.73 + * @param aVisitData 1.74 + * The visit data to use to update the existing row in moz_places. 1.75 + */ 1.76 + nsresult UpdatePlace(const VisitData& aVisitData); 1.77 + 1.78 + /** 1.79 + * Loads information about the page into _place from moz_places. 1.80 + * 1.81 + * @param _place 1.82 + * The VisitData for the place we need to know information about. 1.83 + * @param [out] _exists 1.84 + * Whether or the page was recorded in moz_places, false otherwise. 1.85 + */ 1.86 + nsresult FetchPageInfo(VisitData& _place, bool* _exists); 1.87 + 1.88 + /** 1.89 + * Get the number of bytes of memory this History object is using, 1.90 + * including sizeof(*this)) 1.91 + */ 1.92 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); 1.93 + 1.94 + /** 1.95 + * Obtains a pointer to this service. 1.96 + */ 1.97 + static History* GetService(); 1.98 + 1.99 + /** 1.100 + * Obtains a pointer that has had AddRef called on it. Used by the service 1.101 + * manager only. 1.102 + */ 1.103 + static History* GetSingleton(); 1.104 + 1.105 + template<int N> 1.106 + already_AddRefed<mozIStorageStatement> 1.107 + GetStatement(const char (&aQuery)[N]) 1.108 + { 1.109 + mozIStorageConnection* dbConn = GetDBConn(); 1.110 + NS_ENSURE_TRUE(dbConn, nullptr); 1.111 + return mDB->GetStatement(aQuery); 1.112 + } 1.113 + 1.114 + already_AddRefed<mozIStorageStatement> 1.115 + GetStatement(const nsACString& aQuery) 1.116 + { 1.117 + mozIStorageConnection* dbConn = GetDBConn(); 1.118 + NS_ENSURE_TRUE(dbConn, nullptr); 1.119 + return mDB->GetStatement(aQuery); 1.120 + } 1.121 + 1.122 + bool IsShuttingDown() const { 1.123 + return mShuttingDown; 1.124 + } 1.125 + Mutex& GetShutdownMutex() { 1.126 + return mShutdownMutex; 1.127 + } 1.128 + 1.129 + /** 1.130 + * Helper function to append a new URI to mRecentlyVisitedURIs. See 1.131 + * mRecentlyVisitedURIs. 1.132 + */ 1.133 + void AppendToRecentlyVisitedURIs(nsIURI* aURI); 1.134 + 1.135 +private: 1.136 + virtual ~History(); 1.137 + 1.138 + void InitMemoryReporter(); 1.139 + 1.140 + /** 1.141 + * Obtains a read-write database connection. 1.142 + */ 1.143 + mozIStorageConnection* GetDBConn(); 1.144 + 1.145 + /** 1.146 + * The database handle. This is initialized lazily by the first call to 1.147 + * GetDBConn(), so never use it directly, or, if you really need, always 1.148 + * invoke GetDBConn() before. 1.149 + */ 1.150 + nsRefPtr<mozilla::places::Database> mDB; 1.151 + 1.152 + /** 1.153 + * A read-only database connection used for checking if a URI is visited. 1.154 + * 1.155 + * @note this should only be accessed by GetIsVisistedStatement and Shutdown. 1.156 + */ 1.157 + nsCOMPtr<mozIStorageConnection> mReadOnlyDBConn; 1.158 + 1.159 + /** 1.160 + * An asynchronous statement to query if a URI is visited or not. 1.161 + * 1.162 + * @note this should only be accessed by GetIsVisistedStatement and Shutdown. 1.163 + */ 1.164 + nsCOMPtr<mozIStorageAsyncStatement> mIsVisitedStatement; 1.165 + 1.166 + /** 1.167 + * Remove any memory references to tasks and do not take on any more. 1.168 + */ 1.169 + void Shutdown(); 1.170 + 1.171 + static History* gService; 1.172 + 1.173 + // Ensures new tasks aren't started on destruction. 1.174 + bool mShuttingDown; 1.175 + // This mutex guards mShuttingDown. Code running in other threads that might 1.176 + // schedule tasks that use the database should grab it and check the value of 1.177 + // mShuttingDown. If we are already shutting down, the code must gracefully 1.178 + // avoid using the db. If we are not, the lock will prevent shutdown from 1.179 + // starting in an unexpected moment. 1.180 + Mutex mShutdownMutex; 1.181 + 1.182 + typedef nsTObserverArray<mozilla::dom::Link* > ObserverArray; 1.183 + 1.184 + class KeyClass : public nsURIHashKey 1.185 + { 1.186 + public: 1.187 + KeyClass(const nsIURI* aURI) 1.188 + : nsURIHashKey(aURI) 1.189 + { 1.190 + } 1.191 + KeyClass(const KeyClass& aOther) 1.192 + : nsURIHashKey(aOther) 1.193 + { 1.194 + NS_NOTREACHED("Do not call me!"); 1.195 + } 1.196 + ObserverArray array; 1.197 + }; 1.198 + 1.199 + /** 1.200 + * Helper function for nsTHashtable::SizeOfExcludingThis call in 1.201 + * SizeOfIncludingThis(). 1.202 + */ 1.203 + static size_t SizeOfEntryExcludingThis(KeyClass* aEntry, 1.204 + mozilla::MallocSizeOf aMallocSizeOf, 1.205 + void*); 1.206 + 1.207 + nsTHashtable<KeyClass> mObservers; 1.208 + 1.209 + /** 1.210 + * mRecentlyVisitedURIs remembers URIs which are recently added to the DB, 1.211 + * to avoid saving these locations repeatedly in a short period. 1.212 + */ 1.213 + typedef nsAutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE> 1.214 + RecentlyVisitedArray; 1.215 + RecentlyVisitedArray mRecentlyVisitedURIs; 1.216 + RecentlyVisitedArray::index_type mRecentlyVisitedURIsNextIndex; 1.217 + 1.218 + bool IsRecentlyVisitedURI(nsIURI* aURI); 1.219 +}; 1.220 + 1.221 +} // namespace places 1.222 +} // namespace mozilla 1.223 + 1.224 +#endif // mozilla_places_History_h_