2009-11-11 14:04:14 +03:00
|
|
|
/* 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-11-11 14:04:14 +03:00
|
|
|
|
|
|
|
#ifndef mozilla_places_Helpers_h_
|
|
|
|
#define mozilla_places_Helpers_h_
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file contains helper classes used by various bits of Places code.
|
|
|
|
*/
|
|
|
|
|
2010-04-27 15:24:54 +04:00
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "nsIURI.h"
|
2010-11-16 04:22:01 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2010-11-18 04:23:29 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2012-02-04 18:12:42 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2009-11-11 14:04:14 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace places {
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Asynchronous Statement Callback Helper
|
|
|
|
|
|
|
|
class AsyncStatementCallback : public mozIStorageStatementCallback
|
|
|
|
{
|
|
|
|
public:
|
2010-10-11 16:53:42 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_MOZISTORAGESTATEMENTCALLBACK
|
|
|
|
AsyncStatementCallback() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~AsyncStatementCallback() {}
|
2009-11-11 14:04:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-10-11 16:53:42 +04:00
|
|
|
* Macros to use in place of NS_DECL_MOZISTORAGESTATEMENTCALLBACK to declare the
|
|
|
|
* methods this class assumes silent or notreached.
|
2009-11-11 14:04:14 +03:00
|
|
|
*/
|
|
|
|
#define NS_DECL_ASYNCSTATEMENTCALLBACK \
|
|
|
|
NS_IMETHOD HandleResult(mozIStorageResultSet *); \
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t);
|
2009-11-11 14:04:14 +03:00
|
|
|
|
2010-04-27 15:24:54 +04:00
|
|
|
/**
|
|
|
|
* Utils to bind a specified URI (or URL) to a statement or binding params, at
|
|
|
|
* the specified index or name.
|
|
|
|
* @note URIs are always bound as UTF8.
|
|
|
|
*/
|
|
|
|
class URIBinder // static
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Bind URI to statement by index.
|
|
|
|
static nsresult Bind(mozIStorageStatement* statement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index,
|
2010-04-27 15:24:54 +04:00
|
|
|
nsIURI* aURI);
|
|
|
|
// Statement URLCString to statement by index.
|
|
|
|
static nsresult Bind(mozIStorageStatement* statement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index,
|
2010-04-27 15:24:54 +04:00
|
|
|
const nsACString& aURLString);
|
|
|
|
// Bind URI to statement by name.
|
|
|
|
static nsresult Bind(mozIStorageStatement* statement,
|
|
|
|
const nsACString& aName,
|
|
|
|
nsIURI* aURI);
|
|
|
|
// Bind URLCString to statement by name.
|
|
|
|
static nsresult Bind(mozIStorageStatement* statement,
|
|
|
|
const nsACString& aName,
|
|
|
|
const nsACString& aURLString);
|
|
|
|
// Bind URI to params by index.
|
|
|
|
static nsresult Bind(mozIStorageBindingParams* aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index,
|
2010-04-27 15:24:54 +04:00
|
|
|
nsIURI* aURI);
|
|
|
|
// Bind URLCString to params by index.
|
|
|
|
static nsresult Bind(mozIStorageBindingParams* aParams,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index,
|
2010-04-27 15:24:54 +04:00
|
|
|
const nsACString& aURLString);
|
|
|
|
// Bind URI to params by name.
|
|
|
|
static nsresult Bind(mozIStorageBindingParams* aParams,
|
|
|
|
const nsACString& aName,
|
|
|
|
nsIURI* aURI);
|
|
|
|
// Bind URLCString to params by name.
|
|
|
|
static nsresult Bind(mozIStorageBindingParams* aParams,
|
|
|
|
const nsACString& aName,
|
|
|
|
const nsACString& aURLString);
|
|
|
|
};
|
|
|
|
|
2010-07-14 05:00:33 +04:00
|
|
|
/**
|
|
|
|
* This extracts the hostname from the URI and reverses it in the
|
|
|
|
* form that we use (always ending with a "."). So
|
|
|
|
* "http://microsoft.com/" becomes "moc.tfosorcim."
|
|
|
|
*
|
|
|
|
* The idea behind this is that we can create an index over the items in
|
|
|
|
* the reversed host name column, and then query for as much or as little
|
|
|
|
* of the host name as we feel like.
|
|
|
|
*
|
|
|
|
* For example, the query "host >= 'gro.allizom.' AND host < 'gro.allizom/'
|
|
|
|
* Matches all host names ending in '.mozilla.org', including
|
|
|
|
* 'developer.mozilla.org' and just 'mozilla.org' (since we define all
|
|
|
|
* reversed host names to end in a period, even 'mozilla.org' matches).
|
|
|
|
* The important thing is that this operation uses the index. Any substring
|
|
|
|
* calls in a select statement (even if it's for the beginning of a string)
|
|
|
|
* will bypass any indices and will be slow).
|
|
|
|
*
|
|
|
|
* @param aURI
|
|
|
|
* URI that contains spec to reverse
|
|
|
|
* @param aRevHost
|
|
|
|
* Out parameter
|
|
|
|
*/
|
|
|
|
nsresult GetReversedHostname(nsIURI* aURI, nsString& aRevHost);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar method to GetReversedHostName but for strings
|
|
|
|
*/
|
|
|
|
void GetReversedHostname(const nsString& aForward, nsString& aRevHost);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverses a string.
|
|
|
|
*
|
|
|
|
* @param aInput
|
|
|
|
* The string to be reversed
|
|
|
|
* @param aReversed
|
2011-10-29 07:37:13 +04:00
|
|
|
* Output parameter will contain the reversed string
|
2010-07-14 05:00:33 +04:00
|
|
|
*/
|
|
|
|
void ReverseString(const nsString& aInput, nsString& aReversed);
|
2010-04-27 15:24:54 +04:00
|
|
|
|
2010-11-24 03:04:28 +03:00
|
|
|
/**
|
|
|
|
* Generates an 12 character guid to be used by bookmark and history entries.
|
|
|
|
*
|
|
|
|
* @note This guid uses the characters a-z, A-Z, 0-9, '-', and '_'.
|
|
|
|
*/
|
|
|
|
nsresult GenerateGUID(nsCString& _guid);
|
|
|
|
|
2010-12-02 20:40:49 +03:00
|
|
|
/**
|
|
|
|
* Determines if the string is a valid guid or not.
|
|
|
|
*
|
|
|
|
* @param aGUID
|
|
|
|
* The guid to test.
|
|
|
|
* @return true if it is a valid guid, false otherwise.
|
|
|
|
*/
|
2013-06-19 11:18:38 +04:00
|
|
|
bool IsValidGUID(const nsACString& aGUID);
|
2010-12-02 20:40:49 +03:00
|
|
|
|
2011-10-29 07:37:13 +04:00
|
|
|
/**
|
|
|
|
* Truncates the title if it's longer than TITLE_LENGTH_MAX.
|
|
|
|
*
|
|
|
|
* @param aTitle
|
|
|
|
* The title to truncate (if necessary)
|
|
|
|
* @param aTrimmed
|
|
|
|
* Output parameter to return the trimmed string
|
|
|
|
*/
|
|
|
|
void TruncateTitle(const nsACString& aTitle, nsACString& aTrimmed);
|
|
|
|
|
2010-11-16 04:22:01 +03:00
|
|
|
/**
|
|
|
|
* Used to finalize a statementCache on a specified thread.
|
|
|
|
*/
|
|
|
|
template<typename StatementType>
|
|
|
|
class FinalizeStatementCacheProxy : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param aStatementCache
|
|
|
|
* The statementCache that should be finalized.
|
2010-11-18 04:23:29 +03:00
|
|
|
* @param aOwner
|
|
|
|
* The object that owns the statement cache. This runnable will hold
|
|
|
|
* a strong reference to it so aStatementCache will not disappear from
|
|
|
|
* under us.
|
2010-11-16 04:22:01 +03:00
|
|
|
*/
|
|
|
|
FinalizeStatementCacheProxy(
|
2010-11-18 04:23:29 +03:00
|
|
|
mozilla::storage::StatementCache<StatementType>& aStatementCache,
|
|
|
|
nsISupports* aOwner
|
2010-11-16 04:22:01 +03:00
|
|
|
)
|
|
|
|
: mStatementCache(aStatementCache)
|
2010-11-18 04:23:29 +03:00
|
|
|
, mOwner(aOwner)
|
|
|
|
, mCallingThread(do_GetCurrentThread())
|
2010-11-16 04:22:01 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-18 04:23:29 +03:00
|
|
|
NS_IMETHOD Run()
|
2010-11-16 04:22:01 +03:00
|
|
|
{
|
|
|
|
mStatementCache.FinalizeStatements();
|
2010-11-18 04:23:29 +03:00
|
|
|
// Release the owner back on the calling thread.
|
|
|
|
(void)NS_ProxyRelease(mCallingThread, mOwner);
|
2010-11-16 04:22:01 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
mozilla::storage::StatementCache<StatementType>& mStatementCache;
|
2010-11-18 04:23:29 +03:00
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
nsCOMPtr<nsIThread> mCallingThread;
|
2010-11-16 04:22:01 +03:00
|
|
|
};
|
|
|
|
|
2010-12-03 04:37:53 +03:00
|
|
|
/**
|
|
|
|
* Forces a WAL checkpoint. This will cause all transactions stored in the
|
|
|
|
* journal file to be committed to the main database.
|
|
|
|
*
|
|
|
|
* @note The checkpoint will force a fsync/flush.
|
|
|
|
*/
|
2011-10-27 13:11:34 +04:00
|
|
|
void ForceWALCheckpoint();
|
2010-12-03 04:37:53 +03:00
|
|
|
|
2011-01-11 21:26:44 +03:00
|
|
|
/**
|
|
|
|
* Determines if a visit should be marked as hidden given its transition type
|
|
|
|
* and whether or not it was a redirect.
|
|
|
|
*
|
|
|
|
* @param aIsRedirect
|
|
|
|
* True if this visit was a redirect, false otherwise.
|
|
|
|
* @param aTransitionType
|
|
|
|
* The transition type of the visit.
|
|
|
|
* @return true if this visit should be hidden.
|
|
|
|
*/
|
|
|
|
bool GetHiddenState(bool aIsRedirect,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aTransitionType);
|
2011-01-11 21:26:44 +03:00
|
|
|
|
2011-01-25 20:41:42 +03:00
|
|
|
/**
|
|
|
|
* Notifies a specified topic via the observer service.
|
|
|
|
*/
|
|
|
|
class PlacesEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-07-14 23:21:34 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2011-01-25 20:41:42 +03:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2014-09-04 02:25:36 +04:00
|
|
|
explicit PlacesEvent(const char* aTopic);
|
2011-01-25 20:41:42 +03:00
|
|
|
protected:
|
2014-06-24 02:40:02 +04:00
|
|
|
~PlacesEvent() {}
|
2011-01-25 20:41:42 +03:00
|
|
|
void Notify();
|
|
|
|
|
|
|
|
const char* const mTopic;
|
|
|
|
};
|
|
|
|
|
2012-02-04 18:12:42 +04:00
|
|
|
/**
|
|
|
|
* Used to notify a topic to system observers on async execute completion.
|
|
|
|
*/
|
|
|
|
class AsyncStatementCallbackNotifier : public AsyncStatementCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-09-04 02:25:36 +04:00
|
|
|
explicit AsyncStatementCallbackNotifier(const char* aTopic)
|
2012-02-04 18:12:42 +04:00
|
|
|
: mTopic(aTopic)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t aReason);
|
2012-02-04 18:12:42 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* mTopic;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to notify a topic to system observers on async execute completion.
|
|
|
|
*/
|
|
|
|
class AsyncStatementTelemetryTimer : public AsyncStatementCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-09-04 02:25:36 +04:00
|
|
|
explicit AsyncStatementTelemetryTimer(Telemetry::ID aHistogramId,
|
|
|
|
TimeStamp aStart = TimeStamp::Now())
|
2012-02-04 18:12:42 +04:00
|
|
|
: mHistogramId(aHistogramId)
|
|
|
|
, mStart(aStart)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHOD HandleCompletion(uint16_t aReason);
|
2012-02-04 18:12:42 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const Telemetry::ID mHistogramId;
|
|
|
|
const TimeStamp mStart;
|
|
|
|
};
|
2012-02-24 16:42:24 +04:00
|
|
|
|
2009-11-11 14:04:14 +03:00
|
|
|
} // namespace places
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_places_Helpers_h_
|