2009-08-20 22:56:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-08-20 22:56:10 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_places_History_h_
|
|
|
|
#define mozilla_places_History_h_
|
|
|
|
|
|
|
|
#include "mozilla/IHistory.h"
|
2012-02-01 14:59:27 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2011-01-14 00:37:04 +03:00
|
|
|
#include "mozIAsyncHistory.h"
|
2012-01-20 16:48:20 +04:00
|
|
|
#include "nsIDownloadHistory.h"
|
2011-10-27 13:11:34 +04:00
|
|
|
#include "Database.h"
|
|
|
|
|
2009-08-20 22:56:10 +04:00
|
|
|
#include "mozilla/dom/Link.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsURIHashKey.h"
|
2011-07-14 01:28:37 +04:00
|
|
|
#include "nsTObserverArray.h"
|
2010-07-14 05:00:33 +04:00
|
|
|
#include "nsDeque.h"
|
|
|
|
#include "nsIObserver.h"
|
2010-10-06 19:21:31 +04:00
|
|
|
#include "mozIStorageConnection.h"
|
2009-08-20 22:56:10 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace places {
|
|
|
|
|
2011-01-11 21:48:14 +03:00
|
|
|
struct VisitData;
|
|
|
|
|
2009-08-20 22:56:10 +04:00
|
|
|
#define NS_HISTORYSERVICE_CID \
|
2010-07-14 05:00:41 +04:00
|
|
|
{0x0937a705, 0x91a6, 0x417a, {0x82, 0x92, 0xb2, 0x2e, 0xb1, 0x0d, 0xa8, 0x6c}}
|
2009-08-20 22:56:10 +04:00
|
|
|
|
2012-03-28 23:50:59 +04:00
|
|
|
// Max size of History::mRecentlyVisitedURIs
|
|
|
|
#define RECENTLY_VISITED_URI_SIZE 8
|
|
|
|
|
2009-08-20 22:56:10 +04:00
|
|
|
class History : public IHistory
|
2012-01-20 16:48:20 +04:00
|
|
|
, public nsIDownloadHistory
|
2011-01-14 00:37:04 +03:00
|
|
|
, public mozIAsyncHistory
|
2010-07-14 05:00:33 +04:00
|
|
|
, public nsIObserver
|
2009-08-20 22:56:10 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_IHISTORY
|
2012-01-20 16:48:20 +04:00
|
|
|
NS_DECL_NSIDOWNLOADHISTORY
|
2011-01-14 00:37:04 +03:00
|
|
|
NS_DECL_MOZIASYNCHISTORY
|
2010-07-14 05:00:33 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
2009-08-20 22:56:10 +04:00
|
|
|
|
|
|
|
History();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies about the visited status of a given URI.
|
|
|
|
*
|
|
|
|
* @param aURI
|
|
|
|
* The URI to notify about.
|
|
|
|
*/
|
2010-10-06 19:21:31 +04:00
|
|
|
void NotifyVisited(nsIURI* aURI);
|
2009-08-20 22:56:10 +04:00
|
|
|
|
2010-10-06 19:21:31 +04:00
|
|
|
/**
|
|
|
|
* Obtains the statement to use to check if a URI is visited or not.
|
|
|
|
*/
|
|
|
|
mozIStorageAsyncStatement* GetIsVisitedStatement();
|
|
|
|
|
2011-01-11 21:48:14 +03:00
|
|
|
/**
|
|
|
|
* Adds an entry in moz_places with the data in aVisitData.
|
|
|
|
*
|
|
|
|
* @param aVisitData
|
|
|
|
* The visit data to use to populate a new row in moz_places.
|
|
|
|
*/
|
|
|
|
nsresult InsertPlace(const VisitData& aVisitData);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an entry in moz_places with the data in aVisitData.
|
|
|
|
*
|
|
|
|
* @param aVisitData
|
|
|
|
* The visit data to use to update the existing row in moz_places.
|
|
|
|
*/
|
|
|
|
nsresult UpdatePlace(const VisitData& aVisitData);
|
|
|
|
|
2011-01-11 21:48:53 +03:00
|
|
|
/**
|
|
|
|
* Loads information about the page into _place from moz_places.
|
|
|
|
*
|
|
|
|
* @param _place
|
|
|
|
* The VisitData for the place we need to know information about.
|
|
|
|
* @return true if the page was recorded in moz_places, false otherwise.
|
|
|
|
*/
|
|
|
|
bool FetchPageInfo(VisitData& _place);
|
|
|
|
|
2011-09-15 04:37:45 +04:00
|
|
|
/**
|
2011-11-28 07:03:14 +04:00
|
|
|
* Get the number of bytes of memory this History object is using,
|
|
|
|
* including sizeof(*this))
|
2011-09-15 04:37:45 +04:00
|
|
|
*/
|
2011-12-16 02:59:53 +04:00
|
|
|
size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf);
|
2011-09-15 04:37:45 +04:00
|
|
|
|
2009-08-20 22:56:10 +04:00
|
|
|
/**
|
|
|
|
* Obtains a pointer to this service.
|
|
|
|
*/
|
2010-10-06 19:21:31 +04:00
|
|
|
static History* GetService();
|
2009-08-20 22:56:10 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains a pointer that has had AddRef called on it. Used by the service
|
|
|
|
* manager only.
|
|
|
|
*/
|
2010-10-06 19:21:31 +04:00
|
|
|
static History* GetSingleton();
|
2009-08-20 22:56:10 +04:00
|
|
|
|
2011-10-27 13:11:34 +04:00
|
|
|
template<int N>
|
|
|
|
already_AddRefed<mozIStorageStatement>
|
|
|
|
GetStatement(const char (&aQuery)[N])
|
|
|
|
{
|
|
|
|
mozIStorageConnection* dbConn = GetDBConn();
|
|
|
|
NS_ENSURE_TRUE(dbConn, nsnull);
|
|
|
|
return mDB->GetStatement(aQuery);
|
|
|
|
}
|
2010-11-08 22:43:46 +03:00
|
|
|
|
2012-02-01 14:59:27 +04:00
|
|
|
bool IsShuttingDown() const {
|
|
|
|
return mShuttingDown;
|
|
|
|
}
|
|
|
|
Mutex& GetShutdownMutex() {
|
|
|
|
return mShutdownMutex;
|
|
|
|
}
|
|
|
|
|
2012-03-28 23:50:59 +04:00
|
|
|
/**
|
|
|
|
* Helper function to append a new URI to mRecentlyVisitedURIs. See
|
|
|
|
* mRecentlyVisitedURIs.
|
|
|
|
*/
|
|
|
|
void AppendToRecentlyVisitedURIs(nsIURI* aURI);
|
|
|
|
|
2009-08-20 22:56:10 +04:00
|
|
|
private:
|
2010-10-06 19:21:31 +04:00
|
|
|
virtual ~History();
|
|
|
|
|
2010-11-08 22:43:46 +03:00
|
|
|
/**
|
|
|
|
* Obtains a read-write database connection.
|
|
|
|
*/
|
|
|
|
mozIStorageConnection* GetDBConn();
|
|
|
|
|
|
|
|
/**
|
2011-10-27 13:11:34 +04:00
|
|
|
* The database handle. This is initialized lazily by the first call to
|
|
|
|
* GetDBConn(), so never use it directly, or, if you really need, always
|
|
|
|
* invoke GetDBConn() before.
|
2010-11-08 22:43:46 +03:00
|
|
|
*/
|
2011-10-27 13:11:34 +04:00
|
|
|
nsRefPtr<mozilla::places::Database> mDB;
|
2010-11-08 22:43:46 +03:00
|
|
|
|
2010-10-06 19:21:31 +04:00
|
|
|
/**
|
|
|
|
* A read-only database connection used for checking if a URI is visited.
|
|
|
|
*
|
|
|
|
* @note this should only be accessed by GetIsVisistedStatement and Shutdown.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<mozIStorageConnection> mReadOnlyDBConn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An asynchronous statement to query if a URI is visited or not.
|
|
|
|
*
|
2010-10-13 23:02:22 +04:00
|
|
|
* @note this should only be accessed by GetIsVisistedStatement and Shutdown.
|
2010-10-06 19:21:31 +04:00
|
|
|
*/
|
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> mIsVisitedStatement;
|
2009-08-20 22:56:10 +04:00
|
|
|
|
2010-07-14 05:00:33 +04:00
|
|
|
/**
|
|
|
|
* Remove any memory references to tasks and do not take on any more.
|
|
|
|
*/
|
|
|
|
void Shutdown();
|
|
|
|
|
2010-10-06 19:21:31 +04:00
|
|
|
static History* gService;
|
2009-08-20 22:56:10 +04:00
|
|
|
|
2010-07-14 05:00:33 +04:00
|
|
|
// Ensures new tasks aren't started on destruction.
|
|
|
|
bool mShuttingDown;
|
2012-02-01 14:59:27 +04:00
|
|
|
// This mutex guards mShuttingDown. Code running in other threads that might
|
|
|
|
// schedule tasks that use the database should grab it and check the value of
|
|
|
|
// mShuttingDown. If we are already shutting down, the code must gracefully
|
|
|
|
// avoid using the db. If we are not, the lock will prevent shutdown from
|
|
|
|
// starting in an unexpected moment.
|
|
|
|
Mutex mShutdownMutex;
|
2010-07-14 05:00:33 +04:00
|
|
|
|
2011-07-14 01:28:37 +04:00
|
|
|
typedef nsTObserverArray<mozilla::dom::Link* > ObserverArray;
|
2009-08-20 22:56:10 +04:00
|
|
|
|
|
|
|
class KeyClass : public nsURIHashKey
|
|
|
|
{
|
|
|
|
public:
|
2010-10-06 19:21:31 +04:00
|
|
|
KeyClass(const nsIURI* aURI)
|
2009-08-20 22:56:10 +04:00
|
|
|
: nsURIHashKey(aURI)
|
|
|
|
{
|
|
|
|
}
|
2010-10-06 19:21:31 +04:00
|
|
|
KeyClass(const KeyClass& aOther)
|
2009-08-20 22:56:10 +04:00
|
|
|
: nsURIHashKey(aOther)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Do not call me!");
|
|
|
|
}
|
|
|
|
ObserverArray array;
|
|
|
|
};
|
|
|
|
|
2011-09-15 04:37:45 +04:00
|
|
|
/**
|
2011-12-16 02:59:53 +04:00
|
|
|
* Helper function for nsTHashtable::SizeOfExcludingThis call in
|
|
|
|
* SizeOfIncludingThis().
|
2011-09-15 04:37:45 +04:00
|
|
|
*/
|
2011-12-16 02:59:53 +04:00
|
|
|
static size_t SizeOfEntryExcludingThis(KeyClass* aEntry,
|
|
|
|
nsMallocSizeOfFun aMallocSizeOf,
|
|
|
|
void*);
|
2011-09-15 04:37:45 +04:00
|
|
|
|
2009-08-20 22:56:10 +04:00
|
|
|
nsTHashtable<KeyClass> mObservers;
|
2012-03-28 23:50:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mRecentlyVisitedURIs remembers URIs which are recently added to the DB,
|
|
|
|
* to avoid saving these locations repeatedly in a short period.
|
|
|
|
*/
|
|
|
|
typedef nsAutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE>
|
|
|
|
RecentlyVisitedArray;
|
|
|
|
RecentlyVisitedArray mRecentlyVisitedURIs;
|
|
|
|
RecentlyVisitedArray::index_type mRecentlyVisitedURIsNextIndex;
|
|
|
|
|
|
|
|
bool IsRecentlyVisitedURI(nsIURI* aURI);
|
2009-08-20 22:56:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace places
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_places_History_h_
|