2006-07-18 15:56:56 +00:00
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version : MPL 1.1 / GPL 2.0 / LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 ( the " License " ) ; you may not use this file except in compliance with
* the License . You may obtain a copy of the License at
* http : //www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an " AS IS " basis ,
* WITHOUT WARRANTY OF ANY KIND , either express or implied . See the License
* for the specific language governing rights and limitations under the
* License .
*
2007-09-15 20:07:02 +00:00
* The Original Code is Mozilla History System .
2006-07-18 15:56:56 +00:00
*
2007-09-15 20:07:02 +00:00
* The Initial Developer of the Original Code is Google Inc .
2006-07-18 15:56:56 +00:00
* Portions created by the Initial Developer are Copyright ( C ) 2005
* the Initial Developer . All Rights Reserved .
*
* Contributor ( s ) :
* Brett Wilson < brettw @ gmail . com > ( original author )
2007-09-15 20:07:02 +00:00
* Dietrich Ayala < dietrich @ mozilla . com >
* Seth Spitzer < sspitzer @ mozilla . com >
* Asaf Romano < mano @ mozilla . com >
2007-12-11 09:22:50 +00:00
* Marco Bonardo < mak77 @ supereva . it >
2006-07-18 15:56:56 +00:00
*
* Alternatively , the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later ( the " GPL " ) , or
* the GNU Lesser General Public License Version 2.1 or later ( the " LGPL " ) ,
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above . If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL , and not to allow others to
* use your version of this file under the terms of the MPL , indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL . If you do not delete
* the provisions above , a recipient may use your version of this file under
* the terms of any one of the MPL , the GPL or the LGPL .
*
* * * * * * END LICENSE BLOCK * * * * * */
# include <stdio.h>
# include "nsNavHistory.h"
2006-07-18 15:59:28 +00:00
# include "nsNavBookmarks.h"
2006-07-18 16:31:18 +00:00
# include "nsAnnotationService.h"
2006-07-18 15:56:56 +00:00
2006-07-18 16:37:13 +00:00
# include "nsIArray.h"
2006-07-18 16:01:37 +00:00
# include "nsArrayEnumerator.h"
2006-07-18 16:19:04 +00:00
# include "nsCollationCID.h"
2006-07-18 15:56:56 +00:00
# include "nsCOMPtr.h"
# include "nsCRT.h"
2006-07-18 16:19:04 +00:00
# include "nsDateTimeFormatCID.h"
2006-07-18 16:01:37 +00:00
# include "nsDebug.h"
# include "nsEnumeratorUtils.h"
2006-07-18 16:10:01 +00:00
# include "nsFaviconService.h"
2006-07-18 16:23:51 +00:00
# include "nsIChannelEventSink.h"
2006-07-18 16:01:37 +00:00
# include "nsIComponentManager.h"
2006-07-18 16:19:04 +00:00
# include "nsILocaleService.h"
2006-07-18 15:56:56 +00:00
# include "nsILocalFile.h"
2006-07-18 16:01:37 +00:00
# include "nsIPrefBranch2.h"
# include "nsIServiceManager.h"
# include "nsISimpleEnumerator.h"
2006-07-18 15:56:56 +00:00
# include "nsISupportsPrimitives.h"
# include "nsIURI.h"
# include "nsIURL.h"
# include "nsNetUtil.h"
2006-07-18 16:01:37 +00:00
# include "nsPrintfCString.h"
2006-07-18 15:56:56 +00:00
# include "nsPromiseFlatString.h"
2006-07-18 16:01:37 +00:00
# include "nsString.h"
2006-07-18 15:56:56 +00:00
# include "nsUnicharUtils.h"
2006-07-18 16:36:23 +00:00
# include "prsystem.h"
2006-07-18 16:01:37 +00:00
# include "prtime.h"
# include "prprf.h"
2007-06-20 05:19:06 +00:00
# include "nsEscape.h"
2007-09-15 19:53:43 +00:00
# include "nsIVariant.h"
# include "nsVariant.h"
2006-07-18 15:56:56 +00:00
# include "mozIStorageService.h"
# include "mozIStorageConnection.h"
# include "mozIStorageValueArray.h"
# include "mozIStorageStatement.h"
# include "mozIStorageFunction.h"
# include "mozStorageCID.h"
# include "mozStorageHelper.h"
# include "nsAppDirectoryServiceDefs.h"
2007-08-14 14:20:42 +00:00
# include "nsAutoLock.h"
2007-08-28 07:19:57 +00:00
# include "nsIIdleService.h"
2008-01-25 17:11:14 +00:00
# include "nsILivemarkService.h"
# include "nsMathUtils.h" // for NS_ceilf()
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
// Microsecond timeout for "recent" events such as typed and bookmark following.
// If you typed it more than this time ago, it's not recent.
// This is 15 minutes m s/m us/s
# define RECENT_EVENT_THRESHOLD (15 * 60 * 1000000)
2006-07-18 16:27:59 +00:00
// Microseconds ago to look for redirects when updating bookmarks. Used to
// compute the threshold for nsNavBookmarks::AddBookmarkToHash
# define BOOKMARK_REDIRECT_TIME_THRESHOLD (2 * 60 * 100000)
2006-07-18 16:14:12 +00:00
// The maximum number of things that we will store in the recent events list
// before calling ExpireNonrecentEvents. This number should be big enough so it
// is very difficult to get that many unconsumed events (for example, typed but
// never visited) in the RECENT_EVENT_THRESHOLD. Otherwise, we'll start
// checking each one for every page visit, which will be somewhat slower.
# define RECENT_EVENT_QUEUE_MAX_LENGTH 128
2006-07-18 15:56:56 +00:00
// preference ID strings
# define PREF_BRANCH_BASE "browser."
2007-12-05 08:30:07 +00:00
# define PREF_BROWSER_HISTORY_EXPIRE_DAYS_MIN "history_expire_days_min"
# define PREF_BROWSER_HISTORY_EXPIRE_DAYS_MAX "history_expire_days"
# define PREF_BROWSER_HISTORY_EXPIRE_SITES "history_expire_sites"
2006-07-18 15:56:56 +00:00
# define PREF_AUTOCOMPLETE_ONLY_TYPED "urlbar.matchOnlyTyped"
# define PREF_AUTOCOMPLETE_ENABLED "urlbar.autocomplete.enabled"
2008-01-27 22:52:27 +00:00
# define PREF_AUTOCOMPLETE_MAX_RICH_RESULTS "urlbar.maxRichResults"
2008-01-29 06:53:19 +00:00
# define PREF_AUTOCOMPLETE_SEARCH_CHUNK_SIZE "urlbar.search.chunkSize"
# define PREF_AUTOCOMPLETE_SEARCH_TIMEOUT "urlbar.search.timeout"
2006-07-18 16:36:23 +00:00
# define PREF_DB_CACHE_PERCENTAGE "history_cache_percentage"
2008-01-25 17:11:14 +00:00
# define PREF_FRECENCY_NUM_VISITS "places.frecency.numVisits"
# define PREF_FRECENCY_UPDATE_IDLE_TIME "places.frecency.updateIdleTime"
# define PREF_FRECENCY_FIRST_BUCKET_CUTOFF "places.frecency.firstBucketCutoff"
# define PREF_FRECENCY_SECOND_BUCKET_CUTOFF "places.frecency.secondBucketCutoff"
# define PREF_FRECENCY_THIRD_BUCKET_CUTOFF "places.frecency.thirdBucketCutoff"
# define PREF_FRECENCY_FOURTH_BUCKET_CUTOFF "places.frecency.fourthBucketCutoff"
# define PREF_FRECENCY_FIRST_BUCKET_WEIGHT "places.frecency.firstBucketWeight"
# define PREF_FRECENCY_SECOND_BUCKET_WEIGHT "places.frecency.secondBucketWeight"
# define PREF_FRECENCY_THIRD_BUCKET_WEIGHT "places.frecency.thirdBucketWeight"
# define PREF_FRECENCY_FOURTH_BUCKET_WEIGHT "places.frecency.fourthBucketWeight"
# define PREF_FRECENCY_DEFAULT_BUCKET_WEIGHT "places.frecency.defaultBucketWeight"
# define PREF_FRECENCY_EMBED_VISIT_BONUS "places.frecency.embedVisitBonus"
# define PREF_FRECENCY_LINK_VISIT_BONUS "places.frecency.linkVisitBonus"
# define PREF_FRECENCY_TYPED_VISIT_BONUS "places.frecency.typedVisitBonus"
# define PREF_FRECENCY_BOOKMARK_VISIT_BONUS "places.frecency.bookmarkVisitBonus"
# define PREF_FRECENCY_DOWNLOAD_VISIT_BONUS "places.frecency.downloadVisitBonus"
# define PREF_FRECENCY_PERM_REDIRECT_VISIT_BONUS "places.frecency.permRedirectVisitBonus"
# define PREF_FRECENCY_TEMP_REDIRECT_VISIT_BONUS "places.frecency.tempRedirectVisitBonus"
# define PREF_FRECENCY_DEFAULT_VISIT_BONUS "places.frecency.defaultVisitBonus"
# define PREF_FRECENCY_UNVISITED_BOOKMARK_BONUS "places.frecency.unvisitedBookmarkBonus"
# define PREF_FRECENCY_UNVISITED_TYPED_BONUS "places.frecency.unvisitedTypedBonus"
2007-05-09 01:19:12 +00:00
# define PREF_BROWSER_IMPORT_BOOKMARKS "browser.places.importBookmarksHTML"
2007-12-04 22:04:51 +00:00
# define PREF_BROWSER_IMPORT_DEFAULTS "browser.places.importDefaults"
# define PREF_BROWSER_CREATEDSMARTBOOKMARKS "browser.places.createdSmartBookmarks"
# define PREF_BROWSER_LEFTPANEFOLDERID "browser.places.leftPaneFolderId"
2006-07-18 16:36:23 +00:00
// Default (integer) value of PREF_DB_CACHE_PERCENTAGE from 0-100
// This is 6% of machine memory, giving 15MB for a user with 256MB of memory.
// The most that will be used is the size of the DB file. Normal history sizes
// look like 10MB would be a high average for a typical user, so the maximum
// should not normally be required.
# define DEFAULT_DB_CACHE_PERCENTAGE 6
2007-11-19 20:48:56 +00:00
// We set the default database page size to be larger. sqlite's default is 1K.
// This gives good performance when many small parts of the file have to be
// loaded for each statement. Because we try to keep large chunks of the file
// in memory, a larger page size should give better I/O performance. 32K is
// sqlite's default max page size.
# define DEFAULT_DB_PAGE_SIZE 4096
2006-07-18 15:56:56 +00:00
// the value of mLastNow expires every 3 seconds
# define HISTORY_EXPIRE_NOW_TIMEOUT (3 * PR_MSEC_PER_SEC)
2006-07-18 16:29:14 +00:00
// see bug #319004 -- clamp title and URL to generously-large but not too large
// length
# define HISTORY_URI_LENGTH_MAX 65536
# define HISTORY_TITLE_LENGTH_MAX 4096
2007-05-31 06:06:38 +00:00
// db file name
# define DB_FILENAME NS_LITERAL_STRING("places.sqlite")
2007-06-27 20:57:56 +00:00
// db backup file name
# define DB_CORRUPT_FILENAME NS_LITERAL_STRING("places.sqlite.corrupt")
2006-07-18 16:34:13 +00:00
// Lazy adding
# ifdef LAZY_ADD
// time that we'll wait before committing messages
# define LAZY_MESSAGE_TIMEOUT (3 * PR_MSEC_PER_SEC)
// the maximum number of times we'll postpone a lazy timer before committing
// See StartLazyTimer()
# define MAX_LAZY_TIMER_DEFERMENTS 2
# endif // LAZY_ADD
2008-01-25 17:11:14 +00:00
// Perform "long idle" tasks after 15 minutes of idle time, repeating.
2007-08-28 07:19:57 +00:00
// 15 minutes = 900 seconds = 900000 milliseconds
2007-11-20 23:27:41 +00:00
# define LONG_IDLE_TIME_IN_MSECS (900000)
2007-08-28 07:19:57 +00:00
2007-11-05 01:10:37 +00:00
// Perform expiration after 5 minutes of idle time, repeating.
// 5 minutes = 300 seconds = 300000 milliseconds
# define EXPIRE_IDLE_TIME_IN_MSECS (300000)
2008-01-25 17:11:14 +00:00
// Number of records to update frecency for when idle.
// Amount is low, since we're doing work often (see mFrecencyUpdateIdleTime),
// the idea being to constantly keep the first few chunks up to date.
# define COUNT_TO_RECALCULATE_FRECENCY_ON_IDLE 10
2007-11-05 01:10:37 +00:00
// Amount of items to expire at idle time.
# define MAX_EXPIRE_RECORDS_ON_IDLE 200
2007-11-20 23:45:34 +00:00
// Limit the number of items in the history for performance reasons
2007-12-05 08:30:07 +00:00
# define EXPIRATION_CAP_SITES 40000
2007-11-20 23:45:34 +00:00
2008-01-25 17:11:14 +00:00
// DB migration types
# define DB_MIGRATION_NONE 0
# define DB_MIGRATION_CREATED 1
# define DB_MIGRATION_UPDATED 2
2006-07-18 16:23:51 +00:00
NS_IMPL_ADDREF ( nsNavHistory )
NS_IMPL_RELEASE ( nsNavHistory )
NS_INTERFACE_MAP_BEGIN ( nsNavHistory )
NS_INTERFACE_MAP_ENTRY ( nsINavHistoryService )
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS ( nsIGlobalHistory2 , nsIGlobalHistory3 )
NS_INTERFACE_MAP_ENTRY ( nsIGlobalHistory3 )
2007-12-22 02:27:11 +00:00
NS_INTERFACE_MAP_ENTRY ( nsIDownloadHistory )
2006-07-18 16:23:51 +00:00
NS_INTERFACE_MAP_ENTRY ( nsIBrowserHistory )
NS_INTERFACE_MAP_ENTRY ( nsIObserver )
NS_INTERFACE_MAP_ENTRY ( nsISupportsWeakReference )
2008-01-19 06:58:21 +00:00
# ifdef MOZ_XUL
2006-07-18 16:23:51 +00:00
NS_INTERFACE_MAP_ENTRY ( nsIAutoCompleteSearch )
2007-05-27 17:45:20 +00:00
NS_INTERFACE_MAP_ENTRY ( nsIAutoCompleteSimpleResultListener )
2008-01-19 06:58:21 +00:00
# endif
2006-07-18 16:23:51 +00:00
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS ( nsISupports , nsINavHistoryService )
NS_INTERFACE_MAP_END
2006-07-18 15:56:56 +00:00
static nsresult GetReversedHostname ( nsIURI * aURI , nsAString & host ) ;
static void GetReversedHostname ( const nsString & aForward , nsAString & aReversed ) ;
2006-07-18 16:19:04 +00:00
static void GetSubstringFromNthDot ( const nsCString & aInput , PRInt32 aStartingSpot ,
2006-07-18 15:59:03 +00:00
PRInt32 aN , PRBool aIncludeDot ,
2006-07-18 16:19:04 +00:00
nsACString & aSubstr ) ;
2006-07-18 16:29:14 +00:00
static nsresult GenerateTitleFromURI ( nsIURI * aURI , nsAString & aTitle ) ;
2006-07-18 16:19:04 +00:00
static PRInt32 GetTLDCharCount ( const nsCString & aHost ) ;
static PRInt32 GetTLDType ( const nsCString & aHostTail ) ;
static PRBool IsNumericHostName ( const nsCString & aHost ) ;
2006-07-18 16:20:08 +00:00
static PRInt64 GetSimpleBookmarksQueryFolder (
2007-04-25 21:03:29 +00:00
const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsNavHistoryQueryOptions * aOptions ) ;
2007-09-15 20:07:02 +00:00
static void ParseSearchTermsFromQueries ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsTArray < nsStringArray * > * aTerms ) ;
2006-07-18 15:56:56 +00:00
inline void ReverseString ( const nsString & aInput , nsAString & aReversed )
{
aReversed . Truncate ( 0 ) ;
2006-07-18 15:59:03 +00:00
for ( PRInt32 i = aInput . Length ( ) - 1 ; i > = 0 ; i - - )
2006-07-18 15:56:56 +00:00
aReversed . Append ( aInput [ i ] ) ;
}
2006-07-18 15:59:03 +00:00
inline void parameterString ( PRInt32 paramIndex , nsACString & aParamString )
2006-07-18 15:56:56 +00:00
{
aParamString = nsPrintfCString ( " ?%d " , paramIndex + 1 ) ;
}
// UpdateBatchScoper
//
// This just sets begin/end of batch updates to correspond to C++ scopes so
// we can be sure end always gets called.
class UpdateBatchScoper
{
public :
UpdateBatchScoper ( nsNavHistory & aNavHistory ) : mNavHistory ( aNavHistory )
{
mNavHistory . BeginUpdateBatch ( ) ;
}
~ UpdateBatchScoper ( )
{
mNavHistory . EndUpdateBatch ( ) ;
}
protected :
nsNavHistory & mNavHistory ;
} ;
2006-07-18 16:10:01 +00:00
// if adding a new one, be sure to update nsNavBookmarks statements and
// its kGetChildrenIndex_* constants
2006-07-18 15:59:03 +00:00
const PRInt32 nsNavHistory : : kGetInfoIndex_PageID = 0 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_URL = 1 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_Title = 2 ;
2007-04-25 21:03:29 +00:00
const PRInt32 nsNavHistory : : kGetInfoIndex_RevHost = 3 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_VisitCount = 4 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_VisitDate = 5 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_FaviconURL = 6 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_SessionId = 7 ;
2007-05-10 08:05:19 +00:00
const PRInt32 nsNavHistory : : kGetInfoIndex_ItemId = 8 ;
2007-05-19 00:40:55 +00:00
const PRInt32 nsNavHistory : : kGetInfoIndex_ItemDateAdded = 9 ;
const PRInt32 nsNavHistory : : kGetInfoIndex_ItemLastModified = 10 ;
2006-07-18 15:56:56 +00:00
2006-07-18 15:59:03 +00:00
const PRInt32 nsNavHistory : : kAutoCompleteIndex_URL = 0 ;
const PRInt32 nsNavHistory : : kAutoCompleteIndex_Title = 1 ;
2007-08-22 00:05:03 +00:00
const PRInt32 nsNavHistory : : kAutoCompleteIndex_FaviconURL = 2 ;
const PRInt32 nsNavHistory : : kAutoCompleteIndex_ItemId = 3 ;
2007-09-01 00:58:10 +00:00
const PRInt32 nsNavHistory : : kAutoCompleteIndex_ParentId = 4 ;
2008-01-25 17:11:14 +00:00
const PRInt32 nsNavHistory : : kAutoCompleteIndex_BookmarkTitle = 5 ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:19:04 +00:00
static nsDataHashtable < nsCStringHashKey , int > * gTldTypes ;
2006-07-18 15:56:56 +00:00
static const char * gQuitApplicationMessage = " quit-application " ;
2006-07-18 16:27:09 +00:00
static const char * gXpcomShutdown = " xpcom-shutdown " ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:07:34 +00:00
// annotation names
const char nsNavHistory : : kAnnotationPreviousEncoding [ ] = " history/encoding " ;
2006-07-18 16:03:54 +00:00
2006-07-18 15:58:11 +00:00
nsNavHistory * nsNavHistory : : gHistoryService ;
2006-07-18 15:56:56 +00:00
// nsNavHistory::nsNavHistory
nsNavHistory : : nsNavHistory ( ) : mNowValid ( PR_FALSE ) ,
mExpireNowTimer ( nsnull ) ,
2006-07-18 16:35:58 +00:00
mExpire ( this ) ,
2007-12-05 08:30:07 +00:00
mExpireDaysMin ( 0 ) ,
mExpireDaysMax ( 0 ) ,
mExpireSites ( 0 ) ,
2007-08-14 14:20:42 +00:00
mAutoCompleteOnlyTyped ( PR_FALSE ) ,
2008-01-27 22:52:27 +00:00
mAutoCompleteMaxResults ( 25 ) ,
2008-01-29 06:53:19 +00:00
mAutoCompleteSearchChunkSize ( 100 ) ,
mAutoCompleteSearchTimeout ( 100 ) ,
2007-08-14 14:20:42 +00:00
mBatchLevel ( 0 ) ,
mLock ( nsnull ) ,
2007-10-11 06:42:38 +00:00
mBatchHasTransaction ( PR_FALSE ) ,
2007-11-20 02:01:53 +00:00
mTagsFolder ( - 1 )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:34:13 +00:00
# ifdef LAZY_ADD
mLazyTimerSet = PR_TRUE ;
mLazyTimerDeferments = 0 ;
# endif
2006-07-18 15:58:11 +00:00
NS_ASSERTION ( ! gHistoryService , " YOU ARE CREATING 2 COPIES OF THE HISTORY SERVICE. Everything will break. " ) ;
gHistoryService = this ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::~nsNavHistory
nsNavHistory : : ~ nsNavHistory ( )
{
2006-07-18 15:58:11 +00:00
// remove the static reference to the service. Check to make sure its us
// in case somebody creates an extra instance of the service.
2006-07-18 15:58:38 +00:00
NS_ASSERTION ( gHistoryService = = this , " YOU CREATED 2 COPIES OF THE HISTORY SERVICE. " ) ;
gHistoryService = nsnull ;
2007-08-14 14:20:42 +00:00
if ( mLock )
PR_DestroyLock ( mLock ) ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::Init
nsresult
nsNavHistory : : Init ( )
{
nsresult rv ;
2006-07-18 16:36:23 +00:00
// prefs (must be before DB init, which uses the pref service)
nsCOMPtr < nsIPrefService > prefService =
do_GetService ( NS_PREFSERVICE_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = prefService - > GetBranch ( PREF_BRANCH_BASE , getter_AddRefs ( mPrefBranch ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-08-14 14:20:42 +00:00
mLock = PR_NewLock ( ) ;
NS_ENSURE_TRUE ( mLock , NS_ERROR_OUT_OF_MEMORY ) ;
2008-01-25 17:11:14 +00:00
// prefs
LoadPrefs ( PR_TRUE ) ;
2007-06-27 20:57:56 +00:00
// init db file
rv = InitDBFile ( PR_FALSE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// init db and statements
2008-01-25 17:11:14 +00:00
PRInt16 migrationType ;
rv = InitDB ( & migrationType ) ;
2007-06-27 20:57:56 +00:00
if ( NS_FAILED ( rv ) ) {
// if unable to initialize the db, force-re-initialize it:
// InitDBFile will backup the old db and create a new one.
rv = InitDBFile ( PR_TRUE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
rv = InitDB ( & migrationType ) ;
2007-06-27 20:57:56 +00:00
}
2006-07-18 16:12:59 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
2006-07-18 16:25:06 +00:00
# ifdef IN_MEMORY_LINKS
rv = InitMemDB ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
# endif
2006-07-18 15:56:56 +00:00
2008-01-19 06:58:21 +00:00
# ifdef MOZ_XUL
2006-07-18 16:32:08 +00:00
rv = InitAutoComplete ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-19 06:58:21 +00:00
# endif
2006-07-18 15:56:56 +00:00
2006-07-18 16:23:51 +00:00
// extract the last session ID so we know where to pick up. There is no index
// over sessions so the naive statement "SELECT MAX(session) FROM
2006-12-08 19:57:12 +00:00
// moz_historyvisits" won't have good performance. Instead we select the
2006-07-18 16:23:51 +00:00
// session of the last visited page because we do have indices over dates.
// We still do MAX(session) in case there are duplicate sessions for the same
// date, but there will generally be very few (1) of these.
2006-07-18 16:14:12 +00:00
{
nsCOMPtr < mozIStorageStatement > selectSession ;
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-11-09 21:05:49 +00:00
" SELECT MAX(session) FROM moz_historyvisits "
" WHERE visit_date = "
" (SELECT MAX(visit_date) from moz_historyvisits) " ) ,
2006-07-18 16:14:12 +00:00
getter_AddRefs ( selectSession ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasSession ;
if ( NS_SUCCEEDED ( selectSession - > ExecuteStep ( & hasSession ) ) & & hasSession )
mLastSessionID = selectSession - > AsInt64 ( 0 ) ;
else
mLastSessionID = 1 ;
}
2006-07-18 15:56:56 +00:00
// string bundle for localization
nsCOMPtr < nsIStringBundleService > bundleService =
do_GetService ( NS_STRINGBUNDLE_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = bundleService - > CreateBundle (
2006-07-18 16:03:05 +00:00
" chrome://browser/locale/places/places.properties " ,
2006-07-18 15:56:56 +00:00
getter_AddRefs ( mBundle ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
// locale
nsCOMPtr < nsILocaleService > ls = do_GetService ( NS_LOCALESERVICE_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = ls - > GetApplicationLocale ( getter_AddRefs ( mLocale ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// collation
nsCOMPtr < nsICollationFactory > cfact = do_CreateInstance (
2006-07-18 16:38:28 +00:00
NS_COLLATIONFACTORY_CONTRACTID , & rv ) ;
2006-07-18 16:19:04 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = cfact - > CreateCollation ( mLocale , getter_AddRefs ( mCollation ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// date formatter
2006-07-18 16:38:28 +00:00
mDateFormatter = do_CreateInstance ( NS_DATETIMEFORMAT_CONTRACTID , & rv ) ;
2006-07-18 16:19:04 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
// initialize idle timer
InitializeIdleTimer ( ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
// recent events hash tables
NS_ENSURE_TRUE ( mRecentTyped . Init ( 128 ) , NS_ERROR_OUT_OF_MEMORY ) ;
NS_ENSURE_TRUE ( mRecentBookmark . Init ( 128 ) , NS_ERROR_OUT_OF_MEMORY ) ;
2006-07-18 16:23:51 +00:00
NS_ENSURE_TRUE ( mRecentRedirects . Init ( 128 ) , NS_ERROR_OUT_OF_MEMORY ) ;
2006-07-18 16:14:12 +00:00
2006-07-18 15:56:56 +00:00
// The AddObserver calls must be the last lines in this function, because
// this function may fail, and thus, this object would be not completely
// initialized), but the observerservice would still keep a reference to us
// and notify us about shutdown, which may cause crashes.
2006-07-18 16:26:20 +00:00
nsCOMPtr < nsIObserverService > observerService =
do_GetService ( " @mozilla.org/observer-service;1 " , & rv ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:26:20 +00:00
nsCOMPtr < nsIPrefBranch2 > pbi = do_QueryInterface ( mPrefBranch ) ;
2006-07-18 15:56:56 +00:00
if ( pbi ) {
pbi - > AddObserver ( PREF_AUTOCOMPLETE_ONLY_TYPED , this , PR_FALSE ) ;
2008-01-27 22:52:27 +00:00
pbi - > AddObserver ( PREF_AUTOCOMPLETE_MAX_RICH_RESULTS , this , PR_FALSE ) ;
2008-01-29 06:53:19 +00:00
pbi - > AddObserver ( PREF_AUTOCOMPLETE_SEARCH_CHUNK_SIZE , this , PR_FALSE ) ;
pbi - > AddObserver ( PREF_AUTOCOMPLETE_SEARCH_TIMEOUT , this , PR_FALSE ) ;
2007-12-05 08:30:07 +00:00
pbi - > AddObserver ( PREF_BROWSER_HISTORY_EXPIRE_DAYS_MAX , this , PR_FALSE ) ;
pbi - > AddObserver ( PREF_BROWSER_HISTORY_EXPIRE_DAYS_MIN , this , PR_FALSE ) ;
pbi - > AddObserver ( PREF_BROWSER_HISTORY_EXPIRE_SITES , this , PR_FALSE ) ;
2006-07-18 15:56:56 +00:00
}
2006-07-18 16:26:20 +00:00
observerService - > AddObserver ( this , gQuitApplicationMessage , PR_FALSE ) ;
2006-07-18 16:27:09 +00:00
observerService - > AddObserver ( this , gXpcomShutdown , PR_FALSE ) ;
2006-07-18 15:56:56 +00:00
2008-01-25 17:11:14 +00:00
if ( migrationType = = DB_MIGRATION_CREATED ) {
2006-07-18 16:24:41 +00:00
nsCOMPtr < nsIFile > historyFile ;
rv = NS_GetSpecialDirectory ( NS_APP_HISTORY_50_FILE ,
getter_AddRefs ( historyFile ) ) ;
if ( NS_SUCCEEDED ( rv ) & & historyFile ) {
2007-08-15 15:50:22 +00:00
ImportHistory ( historyFile ) ;
2006-07-18 16:24:41 +00:00
}
}
2008-01-25 17:11:14 +00:00
// In case we've either imported or done a migration from a pre-frecency build,
// calculate the first cutoff period's frecencies now.
// Swallow errors here to not block initialization.
if ( migrationType ! = DB_MIGRATION_NONE )
( void ) RecalculateFrecencies ( ) ;
2006-07-18 16:25:55 +00:00
// Don't add code that can fail here! Do it up above, before we add our
// observers.
return NS_OK ;
2006-07-18 15:56:56 +00:00
}
2007-06-27 20:57:56 +00:00
// nsNavHistory::BackupDBFile
//
// backup a corrupted db file
2006-07-18 16:26:44 +00:00
//
2006-07-18 15:56:56 +00:00
nsresult
2007-06-27 20:57:56 +00:00
nsNavHistory : : BackupDBFile ( )
2006-07-18 15:56:56 +00:00
{
2007-06-27 20:57:56 +00:00
// move the database file to a uniquely named backup
2007-05-31 06:06:38 +00:00
nsCOMPtr < nsIFile > profDir ;
2007-06-27 20:57:56 +00:00
nsresult rv = NS_GetSpecialDirectory ( NS_APP_USER_PROFILE_50_DIR ,
2007-05-31 06:06:38 +00:00
getter_AddRefs ( profDir ) ) ;
2008-01-07 06:28:24 +00:00
// create unique file
2007-06-27 20:57:56 +00:00
nsCOMPtr < nsIFile > corruptBackup ;
rv = profDir - > Clone ( getter_AddRefs ( corruptBackup ) ) ;
2007-05-31 06:06:38 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
rv = corruptBackup - > Append ( DB_CORRUPT_FILENAME ) ;
2006-07-18 16:24:16 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
rv = corruptBackup - > CreateUnique ( nsIFile : : NORMAL_FILE_TYPE , 0600 ) ;
2006-07-18 16:24:16 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-07 06:28:24 +00:00
// get unique name, and remove tmp file
nsAutoString backupName ;
rv = corruptBackup - > GetLeafName ( backupName ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = corruptBackup - > Remove ( PR_FALSE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// copy contents of db file to new uniquely-named backup file
rv = mDBFile - > CopyTo ( profDir , backupName ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
2007-06-27 20:57:56 +00:00
}
// nsNavHistory::InitDBFile
nsresult
nsNavHistory : : InitDBFile ( PRBool aForceInit )
{
// get profile dir, file
nsCOMPtr < nsIFile > profDir ;
nsresult rv = NS_GetSpecialDirectory ( NS_APP_USER_PROFILE_50_DIR ,
getter_AddRefs ( profDir ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = profDir - > Clone ( getter_AddRefs ( mDBFile ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBFile - > Append ( DB_FILENAME ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// if forcing, backup and remove the old file
2008-01-07 06:28:24 +00:00
PRBool dbExists ;
2007-06-27 20:57:56 +00:00
if ( aForceInit ) {
2008-01-07 06:28:24 +00:00
if ( mDBConn ) {
// close db connection if open
rv = mDBConn - > Close ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-06-27 20:57:56 +00:00
rv = BackupDBFile ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-07 06:28:24 +00:00
rv = mDBFile - > Remove ( PR_FALSE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
dbExists = PR_FALSE ;
}
else {
// file exists?
rv = mDBFile - > Exists ( & dbExists ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
}
2007-05-31 06:06:38 +00:00
// open the database
2006-07-18 15:56:56 +00:00
mDBService = do_GetService ( MOZ_STORAGE_SERVICE_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
rv = mDBService - > OpenDatabase ( mDBFile , getter_AddRefs ( mDBConn ) ) ;
2006-07-26 18:54:12 +00:00
if ( rv = = NS_ERROR_FILE_CORRUPTED ) {
2007-05-31 06:06:38 +00:00
dbExists = PR_FALSE ;
2007-06-27 20:57:56 +00:00
// backup file
rv = BackupDBFile ( ) ;
2007-05-31 06:06:38 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
// create new db file, and try to open again
rv = profDir - > Clone ( getter_AddRefs ( mDBFile ) ) ;
2007-05-31 06:06:38 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
rv = mDBFile - > Append ( DB_FILENAME ) ;
2006-07-26 18:54:12 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
rv = mDBService - > OpenDatabase ( mDBFile , getter_AddRefs ( mDBConn ) ) ;
2006-07-26 18:54:12 +00:00
}
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-27 20:57:56 +00:00
2007-05-31 06:06:38 +00:00
// if the db didn't previously exist, or was corrupted, re-import bookmarks.
if ( ! dbExists ) {
nsCOMPtr < nsIPrefBranch > prefs ( do_GetService ( " @mozilla.org/preferences-service;1 " ) ) ;
if ( prefs ) {
rv = prefs - > SetBoolPref ( PREF_BROWSER_IMPORT_BOOKMARKS , PR_TRUE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-12-04 22:04:51 +00:00
rv = prefs - > SetBoolPref ( PREF_BROWSER_IMPORT_DEFAULTS , PR_TRUE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-12-05 17:14:05 +00:00
// if the places.sqlite gets deleted/corrupted the queries should be created again
rv = prefs - > SetBoolPref ( PREF_BROWSER_CREATEDSMARTBOOKMARKS , PR_FALSE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-12-04 22:04:51 +00:00
2007-12-05 17:14:05 +00:00
// we must create a new Organizer left pane folder root, the old will not be valid anymore
2007-12-04 22:04:51 +00:00
rv = prefs - > SetIntPref ( PREF_BROWSER_LEFTPANEFOLDERID , - 1 ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-05-31 06:06:38 +00:00
}
}
2007-07-26 21:39:06 +00:00
2007-06-27 20:57:56 +00:00
return NS_OK ;
}
// nsNavHistory::InitDB
//
2007-07-26 16:23:11 +00:00
# define PLACES_SCHEMA_VERSION 6
2007-06-27 20:57:56 +00:00
nsresult
2008-01-25 17:11:14 +00:00
nsNavHistory : : InitDB ( PRInt16 * aMadeChanges )
2007-06-27 20:57:56 +00:00
{
nsresult rv ;
PRBool tableExists ;
2008-01-25 17:11:14 +00:00
* aMadeChanges = DB_MIGRATION_NONE ;
2007-05-31 06:06:38 +00:00
2007-11-01 17:07:11 +00:00
// IMPORTANT NOTE:
// setting page_size must happen first, see bug #401985 for details
//
// Set the database page size. This will only have any effect on empty files,
// so must be done before anything else. If the file already exists, we'll
// get that file's page size and this will have no effect.
nsCAutoString pageSizePragma ( " PRAGMA page_size= " ) ;
pageSizePragma . AppendInt ( DEFAULT_DB_PAGE_SIZE ) ;
rv = mDBConn - > ExecuteSimpleSQL ( pageSizePragma ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:37:38 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
2007-06-23 16:11:33 +00:00
// Initialize the other places services' database tables. We do this before
// creating our statements. Some of our statements depend on these external
// tables, such as the bookmarks or favicon tables.
2006-07-18 16:31:18 +00:00
rv = nsNavBookmarks : : InitTables ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:10:26 +00:00
rv = nsFaviconService : : InitTables ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:31:18 +00:00
rv = nsAnnotationService : : InitTables ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:10:26 +00:00
2007-05-02 19:07:57 +00:00
// Initialize the places schema version if this is first run.
rv = mDBConn - > TableExists ( NS_LITERAL_CSTRING ( " moz_places " ) , & tableExists ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! tableExists ) {
rv = UpdateSchemaVersion ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-04-01 02:38:42 +00:00
// Get the places schema version, which we store in the user_version PRAGMA
2007-04-22 21:48:52 +00:00
PRInt32 DBSchemaVersion ;
2007-07-02 17:25:41 +00:00
rv = mDBConn - > GetSchemaVersion ( & DBSchemaVersion ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-04-01 02:38:42 +00:00
2007-04-22 21:48:52 +00:00
if ( PLACES_SCHEMA_VERSION ! = DBSchemaVersion ) {
// Migration How-to:
2007-04-04 17:21:03 +00:00
//
2007-04-22 21:48:52 +00:00
// 1. increment PLACES_SCHEMA_VERSION.
// 2. implement a method that performs up/sidegrade to your version
// from the current version.
//
// NOTE: We don't support downgrading back to History-only Places.
// If you want to go from newer schema version back to V0, you'll need to
// blow away your sqlite file. Subsequent up/downgrades have backwards and
// forward migration code.
//
// XXX Backup places.sqlite to places-{version}.sqlite when doing db migration?
if ( DBSchemaVersion < PLACES_SCHEMA_VERSION ) {
// Upgrading
2007-07-26 16:23:11 +00:00
// Migrate anno tables up to V3
2007-04-22 21:48:52 +00:00
if ( DBSchemaVersion < 3 ) {
rv = MigrateV3Up ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-05-19 00:40:55 +00:00
// Migrate bookmarks tables up to V5
if ( DBSchemaVersion < 5 ) {
2007-05-09 01:19:12 +00:00
rv = ForceMigrateBookmarksDB ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-07-26 16:23:11 +00:00
// Migrate anno tables up to V6
if ( DBSchemaVersion < 6 ) {
rv = MigrateV6Up ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
// XXX Upgrades >V6 must add migration code here.
2007-04-22 21:48:52 +00:00
2007-04-04 17:21:03 +00:00
} else {
2007-04-22 21:48:52 +00:00
// Downgrading
// XXX Need to prompt user or otherwise notify of
// potential dataloss when downgrading.
2007-07-26 16:23:11 +00:00
// XXX Downgrades from >V6 must add migration code here.
2007-04-22 21:48:52 +00:00
2007-05-19 00:40:55 +00:00
// Downgrade v1,2,4,5
2007-07-26 16:23:11 +00:00
// v3,6 have no backwards incompatible changes.
2008-01-14 02:47:21 +00:00
if ( DBSchemaVersion > 2 & & DBSchemaVersion < 6 ) {
2007-04-22 21:48:52 +00:00
// perform downgrade to v2
rv = ForceMigrateBookmarksDB ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-04-01 02:38:42 +00:00
}
2007-04-04 17:21:03 +00:00
// update schema version in the db
2007-05-02 19:07:57 +00:00
rv = UpdateSchemaVersion ( ) ;
2007-04-04 17:21:03 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-04-01 02:38:42 +00:00
}
2006-07-18 16:36:23 +00:00
// Get the page size. This may be different than was set above if the database
// file already existed and has a different page size.
PRInt32 pageSize ;
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:36:23 +00:00
nsCOMPtr < mozIStorageStatement > statement ;
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING ( " PRAGMA page_size " ) ,
getter_AddRefs ( statement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasResult ;
rv = statement - > ExecuteStep ( & hasResult ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
NS_ENSURE_TRUE ( hasResult , NS_ERROR_FAILURE ) ;
pageSize = statement - > AsInt32 ( 0 ) ;
2006-07-18 15:56:56 +00:00
}
2006-07-18 16:36:23 +00:00
// compute the size of the database cache
PRInt32 cachePercentage ;
if ( NS_FAILED ( mPrefBranch - > GetIntPref ( PREF_DB_CACHE_PERCENTAGE ,
& cachePercentage ) ) )
cachePercentage = DEFAULT_DB_CACHE_PERCENTAGE ;
if ( cachePercentage > 50 )
cachePercentage = 50 ; // sanity check, don't take too much
if ( cachePercentage < 0 )
cachePercentage = 0 ;
PRInt64 cacheSize = PR_GetPhysicalMemorySize ( ) * cachePercentage / 100 ;
PRInt64 cachePages = cacheSize / pageSize ;
// set the cache size
nsCAutoString cacheSizePragma ( " PRAGMA cache_size= " ) ;
cacheSizePragma . AppendInt ( cachePages ) ;
rv = mDBConn - > ExecuteSimpleSQL ( cacheSizePragma ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-06-23 16:11:33 +00:00
// lock the db file
// http://www.sqlite.org/pragma.html#pragma_locking_mode
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " PRAGMA locking_mode = EXCLUSIVE " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-12-08 19:57:12 +00:00
// moz_places
2007-06-27 20:57:56 +00:00
if ( ! tableExists ) {
2008-01-25 17:11:14 +00:00
* aMadeChanges = DB_MIGRATION_CREATED ;
2006-12-08 19:57:12 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " CREATE TABLE moz_places ( "
2006-07-18 16:10:01 +00:00
" id INTEGER PRIMARY KEY, "
" url LONGVARCHAR, "
" title LONGVARCHAR, "
" rev_host LONGVARCHAR, "
" visit_count INTEGER DEFAULT 0, "
" hidden INTEGER DEFAULT 0 NOT NULL, "
" typed INTEGER DEFAULT 0 NOT NULL, "
2008-01-25 17:11:14 +00:00
" favicon_id INTEGER, "
" frecency INTEGER DEFAULT -1 NOT NULL) " ) ) ;
2006-07-18 16:10:01 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2007-11-20 18:23:38 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE UNIQUE INDEX moz_places_url_uniqueindex ON moz_places (url) " ) ) ;
2006-07-18 16:10:01 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2007-11-20 18:23:38 +00:00
// This index is used for favicon expiration, see nsNavHistoryExpire::ExpireItems.
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX moz_places_faviconindex ON moz_places (favicon_id) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX moz_places_hostindex ON moz_places (rev_host) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX moz_places_visitcount ON moz_places (visit_count) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX moz_places_frecencyindex ON moz_places (frecency) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
}
2006-07-18 16:35:58 +00:00
2006-12-08 19:57:12 +00:00
// moz_historyvisits
rv = mDBConn - > TableExists ( NS_LITERAL_CSTRING ( " moz_historyvisits " ) , & tableExists ) ;
2006-07-18 16:10:01 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! tableExists ) {
2006-12-08 19:57:12 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " CREATE TABLE moz_historyvisits ( "
" id INTEGER PRIMARY KEY, "
2006-07-18 16:10:01 +00:00
" from_visit INTEGER, "
2006-12-08 19:57:12 +00:00
" place_id INTEGER, "
2006-07-18 16:10:01 +00:00
" visit_date INTEGER, "
" visit_type INTEGER, "
" session INTEGER) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2007-11-20 18:23:38 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
2008-01-05 05:16:27 +00:00
" CREATE INDEX moz_historyvisits_placedateindex "
" ON moz_historyvisits (place_id, visit_date) " ) ) ;
2006-07-18 16:10:01 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2007-11-20 18:23:38 +00:00
// This makes a big difference in startup time for large profiles because of
// finding bookmark redirects using the referring page.
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX moz_historyvisits_fromindex ON moz_historyvisits (from_visit) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX moz_historyvisits_dateindex ON moz_historyvisits (visit_date) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2006-07-18 16:38:03 +00:00
2008-01-25 17:11:14 +00:00
PRBool migrated = PR_FALSE ;
rv = EnsureCurrentSchema ( mDBConn , & migrated ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( migrated & & * aMadeChanges ! = DB_MIGRATION_CREATED )
* aMadeChanges = DB_MIGRATION_UPDATED ;
2006-07-18 16:37:38 +00:00
rv = transaction . Commit ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:31:18 +00:00
// --- PUT SCHEMA-MODIFYING THINGS (like create table) ABOVE THIS LINE ---
2006-07-18 16:36:23 +00:00
// DO NOT PUT ANY SCHEMA-MODIFYING THINGS HERE
2007-06-27 20:57:56 +00:00
rv = InitStatements ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:36:23 +00:00
return NS_OK ;
}
2008-01-25 17:11:14 +00:00
nsresult
nsNavHistory : : InitializeIdleTimer ( )
{
if ( mIdleTimer ) {
mIdleTimer - > Cancel ( ) ;
mIdleTimer = nsnull ;
}
nsresult rv ;
mIdleTimer = do_CreateInstance ( " @mozilla.org/timer;1 " , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 idleTimerTimeout = PR_MIN ( LONG_IDLE_TIME_IN_MSECS ,
EXPIRE_IDLE_TIME_IN_MSECS ) ;
if ( mFrecencyUpdateIdleTime )
idleTimerTimeout = PR_MIN ( idleTimerTimeout , mFrecencyUpdateIdleTime ) ;
rv = mIdleTimer - > InitWithFuncCallback ( IdleTimerCallback , this ,
idleTimerTimeout ,
nsITimer : : TYPE_REPEATING_SLACK ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
2007-05-02 19:07:57 +00:00
// nsNavHistory::UpdateSchemaVersion
//
// Called by the individual services' InitTables()
nsresult
nsNavHistory : : UpdateSchemaVersion ( )
{
2007-07-02 17:25:41 +00:00
return mDBConn - > SetSchemaVersion ( PLACES_SCHEMA_VERSION ) ;
2007-05-02 19:07:57 +00:00
}
2006-07-18 16:36:23 +00:00
// nsNavHistory::InitStatements
//
// Called after InitDB, this creates our stored statements
nsresult
nsNavHistory : : InitStatements ( )
{
nsresult rv ;
2006-07-18 16:10:01 +00:00
// mDBGetURLPageInfo
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-04-25 21:03:29 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count "
2006-12-08 19:57:12 +00:00
" FROM moz_places h "
2006-07-18 16:10:01 +00:00
" WHERE h.url = ?1 " ) ,
getter_AddRefs ( mDBGetURLPageInfo ) ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:10:01 +00:00
2006-07-18 16:23:02 +00:00
// mDBGetIdPageInfo
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-04-25 21:03:29 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count "
2006-12-08 19:57:12 +00:00
" FROM moz_places h WHERE h.id = ?1 " ) ,
2006-07-18 16:23:02 +00:00
getter_AddRefs ( mDBGetIdPageInfo ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:11:44 +00:00
// mDBRecentVisitOfURL
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2006-12-08 19:57:12 +00:00
" SELECT v.id, v.session "
" FROM moz_places h JOIN moz_historyvisits v ON h.id = v.place_id "
2006-07-18 16:11:44 +00:00
" WHERE h.url = ?1 "
" ORDER BY v.visit_date DESC "
" LIMIT 1 " ) ,
getter_AddRefs ( mDBRecentVisitOfURL ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBInsertVisit
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2006-12-08 19:57:12 +00:00
" INSERT INTO moz_historyvisits "
" (from_visit, place_id, visit_date, visit_type, session) "
2006-07-18 16:11:44 +00:00
" VALUES (?1, ?2, ?3, ?4, ?5) " ) ,
getter_AddRefs ( mDBInsertVisit ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:18:15 +00:00
2006-07-18 16:23:51 +00:00
// mDBGetPageVisitStats (see InternalAdd)
2006-07-18 16:21:22 +00:00
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2006-07-18 16:23:51 +00:00
" SELECT id, visit_count, typed, hidden "
2006-12-08 19:57:12 +00:00
" FROM moz_places "
2006-07-18 16:23:51 +00:00
" WHERE url = ?1 " ) ,
getter_AddRefs ( mDBGetPageVisitStats ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBUpdatePageVisitStats (see InternalAdd)
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2006-12-08 19:57:12 +00:00
" UPDATE moz_places "
2006-07-18 16:32:33 +00:00
" SET visit_count = ?2, hidden = ?3, typed = ?4 "
2006-07-18 16:21:22 +00:00
" WHERE id = ?1 " ) ,
2006-07-18 16:23:51 +00:00
getter_AddRefs ( mDBUpdatePageVisitStats ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBAddNewPage (see InternalAddNewPage)
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-11-20 18:23:38 +00:00
" INSERT OR REPLACE INTO moz_places "
2008-01-25 17:11:14 +00:00
" (url, title, rev_host, hidden, typed, visit_count, frecency) "
" VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) " ) ,
2006-07-18 16:23:51 +00:00
getter_AddRefs ( mDBAddNewPage ) ) ;
2006-07-18 16:21:22 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
// mDBVisitToURLResult, should match kGetInfoIndex_* (see GetQueryResults)
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-04-25 21:03:29 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, "
2008-01-05 05:16:27 +00:00
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " h.id " )
" , f.url, null, null "
2006-12-08 19:57:12 +00:00
" FROM moz_places h "
" JOIN moz_historyvisits v ON h.id = v.place_id "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
" WHERE v.id = ?1 " ) ,
2006-07-18 16:19:04 +00:00
getter_AddRefs ( mDBVisitToURLResult ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBVisitToVisitResult, should match kGetInfoIndex_* (see GetQueryResults)
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-04-25 21:03:29 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, "
" v.visit_date, f.url, v.session, null "
2006-12-08 19:57:12 +00:00
" FROM moz_places h "
" JOIN moz_historyvisits v ON h.id = v.place_id "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
" WHERE v.id = ?1 " ) ,
2006-07-18 16:19:04 +00:00
getter_AddRefs ( mDBVisitToVisitResult ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBUrlToURLResult, should match kGetInfoIndex_*
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-04-25 21:03:29 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, "
2008-01-05 05:16:27 +00:00
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " h.id " )
" , f.url, null, null "
2006-12-08 19:57:12 +00:00
" FROM moz_places h "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
2006-07-18 16:19:04 +00:00
" WHERE h.url = ?1 " ) ,
getter_AddRefs ( mDBUrlToUrlResult ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-04-25 21:03:29 +00:00
// mDBBookmarkToUrlResult, should match kGetInfoIndex_*
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT b.fk, h.url, b.title, h.rev_host, h.visit_count, "
2008-01-05 05:16:27 +00:00
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " b.fk " )
" , f.url, null, null, b.dateAdded, b.lastModified "
2007-04-25 21:03:29 +00:00
" FROM moz_bookmarks b "
" JOIN moz_places h ON b.fk = h.id "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
" WHERE b.id = ?1 " ) ,
getter_AddRefs ( mDBBookmarkToUrlResult ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-10-09 19:14:48 +00:00
// mDBURIHasTag
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT b.id FROM moz_bookmarks b "
" JOIN moz_places p ON b.fk = p.id "
" WHERE p.url = ?1 "
" AND (SELECT b1.parent FROM moz_bookmarks b1 WHERE "
" b1.id = b.parent AND LOWER(b1.title) = LOWER(?2)) = ?3 "
" LIMIT 1 " ) ,
getter_AddRefs ( mDBURIHasTag ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-10-11 06:42:38 +00:00
// mFoldersWithAnnotationQuery
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT annos.item_id, annos.content FROM moz_anno_attributes attrs "
" JOIN moz_items_annos annos ON attrs.id = annos.anno_attribute_id "
" WHERE attrs.name = ?1 " ) ,
getter_AddRefs ( mFoldersWithAnnotationQuery ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
// mDBVisitsForFrecency
// NOTE: we are not limiting to visits with "visit_type NOT IN (0,4)"
// because if we do that, mDBVisitsForFrecency would return no visits
// for places with only embed (or undefined) visits. That would
// cause use to estimate a frecency based on what information we do have,
// see CalculateFrecencyInternal(). that would result in a
// non-zero frecency for a place with only
// embedded visits, instead of a frecency of 0.
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT visit_date, visit_type FROM moz_historyvisits "
" WHERE place_id = ?1 ORDER BY visit_date DESC LIMIT " ) +
nsPrintfCString ( " %d " , mNumVisitsForFrecency ) ,
getter_AddRefs ( mDBVisitsForFrecency ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// find places with invalid frecencies (frecency = -1)
// invalid frecencies can happen in these scenarios:
// 1) we've done "clear private data"
// 2) we've expired or deleted visits
// 3) we've migrated from an older version, before global frecency
//
// from older versions, unmigrated bookmarks might be hidden,
// so we can't exclude hidden places (by doing "WHERE hidden <> 1")
// from our query, as we want to calculate the frecency for those
// places and unhide them (if they are not livemark items and not
// place: queries.)
//
// Note, we are not limiting ourselves to places with visits
// because we may not have any if the place is a bookmark and
// we expired or deleted all the visits.
// we sort by visit count (even though it might not be correct
// as those visits could have been removed or expired.)
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT id, visit_count, hidden, typed, frecency, url "
" FROM moz_places WHERE frecency = -1 "
" ORDER BY visit_count DESC LIMIT " ) +
nsPrintfCString ( " %d " , COUNT_TO_RECALCULATE_FRECENCY_ON_IDLE ) ,
getter_AddRefs ( mDBInvalidFrecencies ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// this query is designed to find places with high frecency and
// an "old" max visit_date.
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT h.id, h.visit_count, h.hidden, h.typed, h.frecency, h.url "
" FROM moz_places h WHERE "
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " h.id " )
" < ?1 ORDER BY h.frecency DESC LIMIT " ) +
nsPrintfCString ( " %d " , COUNT_TO_RECALCULATE_FRECENCY_ON_IDLE ) ,
getter_AddRefs ( mDBOldFrecencies ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBUpdateFrecencyAndHidden
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" UPDATE moz_places SET frecency = ?2, hidden = ?3 WHERE id = ?1 " ) ,
getter_AddRefs ( mDBUpdateFrecencyAndHidden ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBGetPlaceVisitStats
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT typed, hidden, frecency FROM moz_places WHERE id = ?1 " ) ,
getter_AddRefs ( mDBGetPlaceVisitStats ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT b.parent FROM moz_places h JOIN moz_bookmarks b "
" on b.fk = h.id WHERE b.type = 1 and h.id = ?1 " ) ,
getter_AddRefs ( mDBGetBookmarkParentsForPlace ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// when calculating frecency, we want the visit count to be
// all the visits.
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " SELECT COUNT(*) FROM moz_historyvisits "
" WHERE place_id = ?1 " ) ,
getter_AddRefs ( mDBVisitCountForFrecency ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// this query is used to calculate the visit_count column
// which we use in the UI. to the end user, we should not count
// embedded (or undefined) visits.
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " SELECT COUNT(*) FROM moz_historyvisits "
" WHERE visit_type NOT IN(0,4) AND place_id = ?1 " ) ,
getter_AddRefs ( mDBTrueVisitCount ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:10:26 +00:00
return NS_OK ;
2006-07-18 15:56:56 +00:00
}
2007-04-04 17:21:03 +00:00
// nsNavHistory::ForceMigrateBookmarksDB
2007-04-01 02:38:42 +00:00
//
// This dumps all bookmarks-related tables, and recreates them,
// forcing a re-import of bookmarks.html.
2007-04-04 17:21:03 +00:00
//
// NOTE: This may cause data-loss if downgrading!
// Only use this for migration if you're sure that bookmarks.html
// and the target version support all bookmarks fields.
2007-04-01 02:38:42 +00:00
nsresult
2007-04-04 17:21:03 +00:00
nsNavHistory : : ForceMigrateBookmarksDB ( mozIStorageConnection * aDBConn )
2007-04-01 02:38:42 +00:00
{
// drop bookmarks tables
2007-05-17 06:44:02 +00:00
nsresult rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " DROP TABLE IF EXISTS moz_bookmarks " ) ) ;
2007-04-01 02:38:42 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-05-17 06:44:02 +00:00
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " DROP TABLE IF EXISTS moz_bookmarks_folders " ) ) ;
2007-04-01 02:38:42 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-05-17 06:44:02 +00:00
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " DROP TABLE IF EXISTS moz_bookmarks_roots " ) ) ;
2007-04-01 02:38:42 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-05-17 06:44:02 +00:00
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " DROP TABLE IF EXISTS moz_keywords " ) ) ;
2007-04-01 02:38:42 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// initialize bookmarks tables
rv = nsNavBookmarks : : InitTables ( aDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-05-09 01:19:12 +00:00
// set pref indicating bookmarks.html should be imported.
nsCOMPtr < nsIPrefBranch > prefs ( do_GetService ( " @mozilla.org/preferences-service;1 " ) ) ;
if ( prefs ) {
2007-05-17 04:29:27 +00:00
prefs - > SetBoolPref ( PREF_BROWSER_IMPORT_BOOKMARKS , PR_TRUE ) ;
2007-05-09 01:19:12 +00:00
}
2007-04-01 02:38:42 +00:00
return rv ;
}
2007-04-22 21:48:52 +00:00
// nsNavHistory::MigrateV3Up
nsresult
nsNavHistory : : MigrateV3Up ( mozIStorageConnection * aDBConn )
{
2007-04-26 00:52:50 +00:00
// if type col is already there, then a partial update occurred.
// return, making no changes, and allowing db version to be updated.
nsCOMPtr < mozIStorageStatement > statement ;
nsresult rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING ( " SELECT type from moz_annos " ) ,
getter_AddRefs ( statement ) ) ;
if ( NS_SUCCEEDED ( rv ) )
return NS_OK ;
2007-04-22 21:48:52 +00:00
// add type column to moz_annos
2007-04-26 00:52:50 +00:00
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
2007-04-22 21:48:52 +00:00
" ALTER TABLE moz_annos ADD type INTEGER DEFAULT 0 " ) ) ;
2007-04-26 00:52:50 +00:00
if ( NS_FAILED ( rv ) ) {
// if the alteration failed, force-migrate
2007-05-17 06:44:02 +00:00
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " DROP TABLE IF EXISTS moz_annos " ) ) ;
2007-04-26 00:52:50 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = nsAnnotationService : : InitTables ( mDBConn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-04-22 21:48:52 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
2007-07-26 16:23:11 +00:00
// nsNavHistory::MigrateV6Up
nsresult
nsNavHistory : : MigrateV6Up ( mozIStorageConnection * aDBConn )
{
2007-12-18 00:20:45 +00:00
// if dateAdded & lastModified cols are already there, then a partial update occurred,
// and so we should not attempt to add these cols.
2007-07-26 16:23:11 +00:00
nsCOMPtr < mozIStorageStatement > statement ;
nsresult rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-12-18 00:20:45 +00:00
" SELECT a.dateAdded, a.lastModified FROM moz_annos a " ) ,
getter_AddRefs ( statement ) ) ;
2007-07-26 16:23:11 +00:00
if ( NS_FAILED ( rv ) ) {
// add dateAdded and lastModified columns to moz_annos
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" ALTER TABLE moz_annos ADD dateAdded INTEGER DEFAULT 0 " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" ALTER TABLE moz_annos ADD lastModified INTEGER DEFAULT 0 " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-12-18 00:20:45 +00:00
}
2007-07-26 16:23:11 +00:00
2007-12-18 00:20:45 +00:00
// if dateAdded & lastModified cols are already there, then a partial update occurred,
// and so we should not attempt to add these cols. see bug #408443 for details.
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT b.dateAdded, b.lastModified FROM moz_items_annos b " ) ,
getter_AddRefs ( statement ) ) ;
if ( NS_FAILED ( rv ) ) {
2007-07-26 16:23:11 +00:00
// add dateAdded and lastModified columns to moz_items_annos
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" ALTER TABLE moz_items_annos ADD dateAdded INTEGER DEFAULT 0 " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" ALTER TABLE moz_items_annos ADD lastModified INTEGER DEFAULT 0 " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
// we used to create an indexes on moz_favicons.url and
// moz_anno_attributes.name, but those indexes are not needed
// because those columns are UNIQUE, so remove them.
// see bug #386303 for more details
rv = aDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_favicons_url " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = aDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_anno_attributes_nameindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-31 02:16:02 +00:00
return NS_OK ;
}
2008-01-05 05:16:27 +00:00
nsresult
2008-01-25 17:11:14 +00:00
nsNavHistory : : EnsureCurrentSchema ( mozIStorageConnection * aDBConn , PRBool * aDidMigrate )
2008-01-05 05:16:27 +00:00
{
// We need to do a one-time change of the moz_historyvisits.pageindex
// to speed up finding last visit date when joinin with moz_places.
// See bug 392399 for more details.
PRBool oldIndexExists = PR_FALSE ;
nsresult rv = aDBConn - > IndexExists (
NS_LITERAL_CSTRING ( " moz_historyvisits_pageindex " ) , & oldIndexExists ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( oldIndexExists ) {
2008-01-25 17:11:14 +00:00
* aDidMigrate = PR_TRUE ;
2008-01-05 05:16:27 +00:00
// wrap in a transaction for safety and performance
mozStorageTransaction pageindexTransaction ( aDBConn , PR_FALSE ) ;
// drop old index
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" DROP INDEX IF EXISTS moz_historyvisits_pageindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// create the new multi-column index
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX IF NOT EXISTS moz_historyvisits_placedateindex "
" ON moz_historyvisits (place_id, visit_date) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = pageindexTransaction . Commit ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2008-01-25 17:11:14 +00:00
// for existing profiles, we may not have a frecency column
nsCOMPtr < mozIStorageStatement > statement ;
rv = aDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT frecency FROM moz_places " ) , getter_AddRefs ( statement ) ) ;
if ( NS_FAILED ( rv ) ) {
* aDidMigrate = PR_TRUE ;
// wrap in a transaction for safety and performance
mozStorageTransaction frecencyTransaction ( aDBConn , PR_FALSE ) ;
// add frecency column to moz_places, default to -1
// so that all the frecencies are invalid and we'll
// recalculate them on idle.
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" ALTER TABLE moz_places ADD frecency INTEGER DEFAULT -1 NOT NULL " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// create index for the frecency column
// XXX multi column index with typed, and visit_count?
rv = aDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" CREATE INDEX IF NOT EXISTS "
" moz_places_frecencyindex ON moz_places (frecency) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// XXX todo
// forcibly call the "on idle" timer here to do a little work
// but the rest will happen on idle.
// for place: items and unvisited livemark items, we need to set
// the frecency to 0 so that they don't show up in url bar autocomplete
rv = FixInvalidFrecenciesForExcludedPlaces ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = frecencyTransaction . Commit ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2008-01-05 05:16:27 +00:00
return NS_OK ;
}
2007-07-31 02:16:02 +00:00
nsresult
nsNavHistory : : CleanUpOnQuit ( )
{
2007-07-26 16:23:11 +00:00
// bug #371800 - remove moz_places.user_title
// test for moz_places.user_title
nsCOMPtr < mozIStorageStatement > statement2 ;
2007-07-31 02:16:02 +00:00
nsresult rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-07-26 16:23:11 +00:00
" SELECT user_title FROM moz_places " ) , getter_AddRefs ( statement2 ) ) ;
if ( NS_SUCCEEDED ( rv ) ) {
2007-07-31 04:55:51 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
2007-07-31 02:16:02 +00:00
// 1. Indexes are moved along with the renamed table. Since we're dropping
// that table, we're also dropping it's indexes, and later re-creating them
// for the new table.
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_urlindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_titleindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_faviconindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_hostindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_visitcount " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_frecencyindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-31 02:16:02 +00:00
2007-11-20 18:23:38 +00:00
// 2. remove any duplicate URIs
rv = RemoveDuplicateURIs ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// 3. rename moz_places to moz_places_backup
2007-07-31 02:16:02 +00:00
rv = mDBConn - > ExecuteSimpleSQL (
2007-07-26 16:23:11 +00:00
NS_LITERAL_CSTRING ( " ALTER TABLE moz_places RENAME TO moz_places_backup " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
// 4. create moz_places w/o user_title
2007-07-26 16:23:11 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " CREATE TABLE moz_places ( "
" id INTEGER PRIMARY KEY, "
" url LONGVARCHAR, "
" title LONGVARCHAR, "
" rev_host LONGVARCHAR, "
" visit_count INTEGER DEFAULT 0, "
" hidden INTEGER DEFAULT 0 NOT NULL, "
" typed INTEGER DEFAULT 0 NOT NULL, "
2008-01-25 17:11:14 +00:00
" favicon_id INTEGER, "
" frecency INTEGER DEFAULT -1 NOT NULL) " ) ) ;
2007-07-26 16:23:11 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
// 5. recreate the indexes
2007-07-31 02:16:02 +00:00
// NOTE: tests showed that it's faster to create the indexes prior to filling
// the table than it is to add them afterwards.
2007-07-26 16:23:11 +00:00
rv = mDBConn - > ExecuteSimpleSQL (
2007-11-20 18:23:38 +00:00
NS_LITERAL_CSTRING ( " CREATE UNIQUE INDEX moz_places_url_uniqueindex ON moz_places (url) " ) ) ;
2007-07-31 02:16:02 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " CREATE INDEX moz_places_faviconindex ON moz_places (favicon_id) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " CREATE INDEX moz_places_hostindex ON moz_places (rev_host) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBConn - > ExecuteSimpleSQL (
2007-11-02 03:24:03 +00:00
NS_LITERAL_CSTRING ( " CREATE INDEX moz_places_visitcount ON moz_places (visit_count) " ) ) ;
2007-07-31 02:16:02 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " CREATE INDEX moz_places_frecencyindex ON moz_places (frecency) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
2007-11-20 18:23:38 +00:00
// 6. copy all data into moz_places
2007-07-26 16:23:11 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" INSERT INTO moz_places "
2008-01-25 17:11:14 +00:00
" SELECT id, url, title, rev_host, visit_count, hidden, typed, favicon_id, frecency "
2007-07-26 16:23:11 +00:00
" FROM moz_places_backup " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
// 7. drop moz_places_backup
2007-07-26 16:23:11 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" DROP TABLE moz_places_backup " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-31 04:55:51 +00:00
transaction . Commit ( ) ;
2007-07-26 16:23:11 +00:00
}
2007-11-02 03:24:03 +00:00
// bug #381795 - remove unused indexes
mozStorageTransaction idxTransaction ( mDBConn , PR_FALSE ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_titleindex " ) ) ;
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_annos_item_idindex " ) ) ;
idxTransaction . Commit ( ) ;
2007-07-26 16:23:11 +00:00
return NS_OK ;
}
2007-07-31 02:16:02 +00:00
2006-07-18 16:25:06 +00:00
# ifdef IN_MEMORY_LINKS
2006-07-18 15:56:56 +00:00
// nsNavHistory::InitMemDB
//
// Should be called after InitDB
nsresult
nsNavHistory : : InitMemDB ( )
{
2006-07-18 15:57:46 +00:00
nsresult rv = mDBService - > OpenSpecialDatabase ( " memory " , getter_AddRefs ( mMemDBConn ) ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// create our table and index
rv = mMemDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " CREATE TABLE moz_memhistory (url LONGVARCHAR UNIQUE) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
// In-memory links indexes
rv = mMemDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " CREATE INDEX moz_memhistory_index ON moz_memhistory (url) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
// prepackaged statements
rv = mMemDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " SELECT url FROM moz_memhistory WHERE url = ?1 " ) ,
getter_AddRefs ( mMemDBGetPage ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mMemDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " INSERT OR IGNORE INTO moz_memhistory VALUES (?1) " ) ,
getter_AddRefs ( mMemDBAddPage ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// Copy the URLs over: sort by URL because the original table already has
// and index. We can therefor not spend time sorting the whole thing another
// time by inserting in order.
nsCOMPtr < mozIStorageStatement > selectStatement ;
2006-12-08 19:57:12 +00:00
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING ( " SELECT url FROM moz_places WHERE visit_count > 0 ORDER BY url " ) ,
2006-07-18 15:56:56 +00:00
getter_AddRefs ( selectStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasMore = PR_FALSE ;
//rv = mMemDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("BEGIN TRANSACTION"));
mozStorageTransaction transaction ( mMemDBConn , PR_FALSE ) ;
nsCString url ;
while ( NS_SUCCEEDED ( rv = selectStatement - > ExecuteStep ( & hasMore ) ) & & hasMore ) {
2006-07-18 16:24:41 +00:00
rv = selectStatement - > GetUTF8String ( 0 , url ) ;
2006-07-18 15:56:56 +00:00
if ( NS_SUCCEEDED ( rv ) & & ! url . IsEmpty ( ) ) {
rv = mMemDBAddPage - > BindUTF8StringParameter ( 0 , url ) ;
if ( NS_SUCCEEDED ( rv ) )
mMemDBAddPage - > Execute ( ) ;
}
}
transaction . Commit ( ) ;
return NS_OK ;
}
2006-07-18 16:25:06 +00:00
# endif
2006-07-18 15:56:56 +00:00
2006-07-18 15:58:11 +00:00
// nsNavHistory::GetUrlIdFor
//
2006-07-18 15:58:38 +00:00
// Called by the bookmarks and annotation services, this function returns the
2006-07-18 15:58:11 +00:00
// ID of the row for the given URL, optionally creating one if it doesn't
// exist. A newly created entry will have no visits.
//
// If aAutoCreate is false and the item doesn't exist, the entry ID will be
// zero.
//
// This DOES NOT check for bad URLs other than that they're nonempty.
nsresult
nsNavHistory : : GetUrlIdFor ( nsIURI * aURI , PRInt64 * aEntryID ,
PRBool aAutoCreate )
{
* aEntryID = 0 ;
mozStorageStatementScoper statementResetter ( mDBGetURLPageInfo ) ;
2006-07-18 16:23:51 +00:00
nsresult rv = BindStatementURI ( mDBGetURLPageInfo , 0 , aURI ) ;
2006-07-18 15:58:11 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasEntry = PR_FALSE ;
rv = mDBGetURLPageInfo - > ExecuteStep ( & hasEntry ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( hasEntry ) {
return mDBGetURLPageInfo - > GetInt64 ( kGetInfoIndex_PageID , aEntryID ) ;
} else if ( aAutoCreate ) {
2006-07-18 16:23:51 +00:00
// create a new hidden, untyped, unvisited entry
2006-07-18 15:58:11 +00:00
mDBGetURLPageInfo - > Reset ( ) ;
statementResetter . Abandon ( ) ;
2006-07-18 16:28:24 +00:00
nsString voidString ;
voidString . SetIsVoid ( PR_TRUE ) ;
2008-01-25 17:11:14 +00:00
return InternalAddNewPage ( aURI , voidString , PR_TRUE , PR_FALSE , 0 , PR_TRUE , aEntryID ) ;
2006-07-18 15:58:11 +00:00
} else {
// Doesn't exist: don't do anything, entry ID was already set to 0 above
return NS_OK ;
}
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::InternalAddNewPage
//
2007-11-20 18:23:38 +00:00
// Adds a new page to the DB.
// THIS SHOULD BE THE ONLY PLACE NEW moz_places ROWS ARE
2006-07-18 15:56:56 +00:00
// CREATED. This allows us to maintain better consistency.
//
// If non-null, the new page ID will be placed into aPageID.
nsresult
2006-07-18 16:28:24 +00:00
nsNavHistory : : InternalAddNewPage ( nsIURI * aURI , const nsAString & aTitle ,
PRBool aHidden , PRBool aTyped ,
2008-01-25 17:11:14 +00:00
PRInt32 aVisitCount ,
PRBool aCalculateFrecency ,
PRInt64 * aPageID )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:23:51 +00:00
mozStorageStatementScoper scoper ( mDBAddNewPage ) ;
nsresult rv = BindStatementURI ( mDBAddNewPage , 0 , aURI ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:28:24 +00:00
// title
if ( aTitle . IsVoid ( ) ) {
2006-07-18 16:29:14 +00:00
// if no title is specified, make up a title based on the filename
nsAutoString title ;
GenerateTitleFromURI ( aURI , title ) ;
rv = mDBAddNewPage - > BindStringParameter ( 1 ,
StringHead ( title , HISTORY_TITLE_LENGTH_MAX ) ) ;
2006-07-18 16:28:24 +00:00
} else {
2006-07-18 16:29:14 +00:00
rv = mDBAddNewPage - > BindStringParameter ( 1 ,
StringHead ( aTitle , HISTORY_TITLE_LENGTH_MAX ) ) ;
2006-07-18 16:28:24 +00:00
}
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
// host (reversed with trailing period)
nsAutoString revHost ;
rv = GetReversedHostname ( aURI , revHost ) ;
2006-07-18 16:02:41 +00:00
// Not all URI types have hostnames, so this is optional.
if ( NS_SUCCEEDED ( rv ) ) {
2006-07-18 16:28:24 +00:00
rv = mDBAddNewPage - > BindStringParameter ( 2 , revHost ) ;
2006-07-18 16:02:41 +00:00
} else {
2006-07-18 16:28:24 +00:00
rv = mDBAddNewPage - > BindNullParameter ( 2 ) ;
2006-07-18 16:02:41 +00:00
}
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// hidden
2006-07-18 16:28:24 +00:00
rv = mDBAddNewPage - > BindInt32Parameter ( 3 , aHidden ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// typed
2006-07-18 16:28:24 +00:00
rv = mDBAddNewPage - > BindInt32Parameter ( 4 , aTyped ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// visit count
2006-07-18 16:28:24 +00:00
rv = mDBAddNewPage - > BindInt32Parameter ( 5 , aVisitCount ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
nsCAutoString url ;
rv = aURI - > GetSpec ( url ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// frecency
PRInt32 frecency = - 1 ;
if ( aCalculateFrecency ) {
rv = CalculateFrecency ( - 1 /* no page id, since this page doesn't exist */ ,
aTyped , aVisitCount , url ,
& frecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
rv = mDBAddNewPage - > BindInt32Parameter ( 6 , frecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:23:51 +00:00
rv = mDBAddNewPage - > Execute ( ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// If the caller wants the page ID, go get it
if ( aPageID ) {
rv = mDBConn - > GetLastInsertRowID ( aPageID ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
return NS_OK ;
}
2006-07-18 16:16:13 +00:00
// nsNavHistory::InternalAddVisit
2006-07-18 15:56:56 +00:00
//
// Just a wrapper for inserting a new visit in the DB.
2006-07-18 16:11:44 +00:00
2006-07-18 16:23:51 +00:00
nsresult
nsNavHistory : : InternalAddVisit ( PRInt64 aPageID , PRInt64 aReferringVisit ,
PRInt64 aSessionID , PRTime aTime ,
PRInt32 aTransitionType , PRInt64 * visitID )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:11:44 +00:00
nsresult rv ;
mozStorageStatementScoper scoper ( mDBInsertVisit ) ;
2006-07-18 16:23:51 +00:00
rv = mDBInsertVisit - > BindInt64Parameter ( 0 , aReferringVisit ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:11:44 +00:00
rv = mDBInsertVisit - > BindInt64Parameter ( 1 , aPageID ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:11:44 +00:00
rv = mDBInsertVisit - > BindInt64Parameter ( 2 , aTime ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-08-02 17:34:55 +00:00
2006-07-18 16:11:44 +00:00
rv = mDBInsertVisit - > BindInt32Parameter ( 3 , aTransitionType ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:23:51 +00:00
rv = mDBInsertVisit - > BindInt64Parameter ( 4 , aSessionID ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:11:44 +00:00
rv = mDBInsertVisit - > Execute ( ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:14:36 +00:00
return mDBConn - > GetLastInsertRowID ( visitID ) ;
2006-07-18 15:56:56 +00:00
}
2006-07-18 16:23:51 +00:00
// nsNavHistory::FindLastVisit
//
// This finds the most recent visit to the given URL. If found, it will put
// that visit's ID and session into the respective out parameters and return
// true. Returns false if no visit is found.
//
// This is used to compute the referring visit.
PRBool
nsNavHistory : : FindLastVisit ( nsIURI * aURI , PRInt64 * aVisitID ,
PRInt64 * aSessionID )
{
mozStorageStatementScoper scoper ( mDBRecentVisitOfURL ) ;
nsresult rv = BindStatementURI ( mDBRecentVisitOfURL , 0 , aURI ) ;
2008-01-02 17:25:27 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2006-07-18 16:23:51 +00:00
PRBool hasMore ;
rv = mDBRecentVisitOfURL - > ExecuteStep ( & hasMore ) ;
2008-01-02 17:25:27 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2006-07-18 16:23:51 +00:00
if ( hasMore ) {
* aVisitID = mDBRecentVisitOfURL - > AsInt64 ( 0 ) ;
* aSessionID = mDBRecentVisitOfURL - > AsInt64 ( 1 ) ;
return PR_TRUE ;
}
return PR_FALSE ;
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::IsURIStringVisited
//
// Takes a URL as a string and returns true if we've visited it.
//
// Be careful to always reset the statement since it will be reused.
PRBool nsNavHistory : : IsURIStringVisited ( const nsACString & aURIString )
{
# ifdef IN_MEMORY_LINKS
// check the memory DB
nsresult rv = mMemDBGetPage - > BindUTF8StringParameter ( 0 , aURIString ) ;
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
PRBool hasPage = PR_FALSE ;
mMemDBGetPage - > ExecuteStep ( & hasPage ) ;
mMemDBGetPage - > Reset ( ) ;
return hasPage ;
# else
2006-07-18 16:34:13 +00:00
# ifdef LAZY_ADD
// check the lazy list to see if this has recently been added
for ( PRUint32 i = 0 ; i < mLazyMessages . Length ( ) ; i + + ) {
if ( mLazyMessages [ i ] . type = = LazyMessage : : Type_AddURI ) {
if ( aURIString . Equals ( mLazyMessages [ i ] . uriSpec ) )
return PR_TRUE ;
}
}
# endif
2006-07-18 15:56:56 +00:00
// check the main DB
2007-12-18 07:28:14 +00:00
mozStorageStatementScoper scoper ( mDBGetPageVisitStats ) ;
nsresult rv = mDBGetPageVisitStats - > BindUTF8StringParameter ( 0 , aURIString ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
PRBool hasMore = PR_FALSE ;
2007-12-18 07:28:14 +00:00
rv = mDBGetPageVisitStats - > ExecuteStep ( & hasMore ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-12-18 07:28:14 +00:00
if ( ! hasMore )
2006-07-18 16:26:44 +00:00
return PR_FALSE ;
2006-07-18 15:56:56 +00:00
// Actually get the result to make sure the visit count > 0. there are
// several ways that we can get pages with visit counts of 0, and those
// should not count.
PRInt32 visitCount ;
2007-12-18 07:28:14 +00:00
rv = mDBGetPageVisitStats - > GetInt32 ( 1 , & visitCount ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
return visitCount > 0 ;
# endif
}
// nsNavHistory::LoadPrefs
nsresult
2008-01-25 17:11:14 +00:00
nsNavHistory : : LoadPrefs ( PRBool aInitializing )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:26:20 +00:00
if ( ! mPrefBranch )
return NS_OK ;
2007-12-05 08:30:07 +00:00
mPrefBranch - > GetIntPref ( PREF_BROWSER_HISTORY_EXPIRE_DAYS_MAX , & mExpireDaysMax ) ;
mPrefBranch - > GetIntPref ( PREF_BROWSER_HISTORY_EXPIRE_DAYS_MIN , & mExpireDaysMin ) ;
if ( NS_FAILED ( mPrefBranch - > GetIntPref ( PREF_BROWSER_HISTORY_EXPIRE_SITES ,
& mExpireSites ) ) )
mExpireSites = EXPIRATION_CAP_SITES ;
2007-11-20 23:45:34 +00:00
2008-01-19 06:58:21 +00:00
# ifdef MOZ_XUL
2006-07-18 16:32:08 +00:00
PRBool oldCompleteOnlyTyped = mAutoCompleteOnlyTyped ;
2006-07-18 16:26:20 +00:00
mPrefBranch - > GetBoolPref ( PREF_AUTOCOMPLETE_ONLY_TYPED ,
2006-07-18 15:56:56 +00:00
& mAutoCompleteOnlyTyped ) ;
2008-01-27 22:52:27 +00:00
mPrefBranch - > GetBoolPref ( PREF_AUTOCOMPLETE_MAX_RICH_RESULTS ,
& mAutoCompleteMaxResults ) ;
2008-01-29 06:53:19 +00:00
mPrefBranch - > GetBoolPref ( PREF_AUTOCOMPLETE_SEARCH_CHUNK_SIZE ,
& mAutoCompleteSearchChunkSize ) ;
mPrefBranch - > GetBoolPref ( PREF_AUTOCOMPLETE_SEARCH_TIMEOUT ,
& mAutoCompleteSearchTimeout ) ;
2008-01-25 17:11:14 +00:00
if ( ! aInitializing & & oldCompleteOnlyTyped ! = mAutoCompleteOnlyTyped ) {
2007-09-07 05:35:10 +00:00
// update the autocomplete statements if the option has changed.
nsresult rv = CreateAutoCompleteQueries ( ) ;
2006-07-18 16:32:08 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
}
2008-01-19 06:58:21 +00:00
# endif
2008-01-25 17:11:14 +00:00
// get the frecency prefs
nsCOMPtr < nsIPrefBranch > prefs ( do_GetService ( " @mozilla.org/preferences-service;1 " ) ) ;
if ( prefs ) {
prefs - > GetIntPref ( PREF_FRECENCY_NUM_VISITS ,
& mNumVisitsForFrecency ) ;
prefs - > GetIntPref ( PREF_FRECENCY_UPDATE_IDLE_TIME ,
& mFrecencyUpdateIdleTime ) ;
prefs - > GetIntPref ( PREF_FRECENCY_FIRST_BUCKET_CUTOFF ,
& mFirstBucketCutoffInDays ) ;
prefs - > GetIntPref ( PREF_FRECENCY_SECOND_BUCKET_CUTOFF ,
& mSecondBucketCutoffInDays ) ;
prefs - > GetIntPref ( PREF_FRECENCY_THIRD_BUCKET_CUTOFF ,
& mThirdBucketCutoffInDays ) ;
prefs - > GetIntPref ( PREF_FRECENCY_FOURTH_BUCKET_CUTOFF ,
& mFourthBucketCutoffInDays ) ;
prefs - > GetIntPref ( PREF_FRECENCY_EMBED_VISIT_BONUS ,
& mEmbedVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_LINK_VISIT_BONUS ,
& mLinkVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_TYPED_VISIT_BONUS ,
& mTypedVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_BOOKMARK_VISIT_BONUS ,
& mBookmarkVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_DOWNLOAD_VISIT_BONUS ,
& mDownloadVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_PERM_REDIRECT_VISIT_BONUS ,
& mPermRedirectVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_TEMP_REDIRECT_VISIT_BONUS ,
& mTempRedirectVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_DEFAULT_VISIT_BONUS ,
& mDefaultVisitBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_UNVISITED_BOOKMARK_BONUS ,
& mUnvisitedBookmarkBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_UNVISITED_TYPED_BONUS ,
& mUnvisitedTypedBonus ) ;
prefs - > GetIntPref ( PREF_FRECENCY_FIRST_BUCKET_WEIGHT ,
& mFirstBucketWeight ) ;
prefs - > GetIntPref ( PREF_FRECENCY_SECOND_BUCKET_WEIGHT ,
& mSecondBucketWeight ) ;
prefs - > GetIntPref ( PREF_FRECENCY_THIRD_BUCKET_WEIGHT ,
& mThirdBucketWeight ) ;
prefs - > GetIntPref ( PREF_FRECENCY_FOURTH_BUCKET_WEIGHT ,
& mFourthBucketWeight ) ;
prefs - > GetIntPref ( PREF_FRECENCY_DEFAULT_BUCKET_WEIGHT ,
& mDefaultWeight ) ;
}
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::GetNow
//
// This is a hack to avoid calling PR_Now() too often, as is the case when
// we're asked the ageindays of many history entries in a row. A timer is
// set which will clear our valid flag after a short timeout.
PRTime
nsNavHistory : : GetNow ( )
{
if ( ! mNowValid ) {
mLastNow = PR_Now ( ) ;
mNowValid = PR_TRUE ;
if ( ! mExpireNowTimer )
mExpireNowTimer = do_CreateInstance ( " @mozilla.org/timer;1 " ) ;
if ( mExpireNowTimer )
mExpireNowTimer - > InitWithFuncCallback ( expireNowTimerCallback , this ,
HISTORY_EXPIRE_NOW_TIMEOUT ,
nsITimer : : TYPE_ONE_SHOT ) ;
}
return mLastNow ;
}
// nsNavHistory::expireNowTimerCallback
2006-07-18 16:34:13 +00:00
void nsNavHistory : : expireNowTimerCallback ( nsITimer * aTimer , void * aClosure )
2006-07-18 15:56:56 +00:00
{
2007-07-08 07:08:04 +00:00
nsNavHistory * history = static_cast < nsNavHistory * > ( aClosure ) ;
2006-07-18 15:56:56 +00:00
history - > mNowValid = PR_FALSE ;
history - > mExpireNowTimer = nsnull ;
}
2006-11-21 23:02:16 +00:00
static PRTime
NormalizeTimeRelativeToday ( PRTime aTime )
{
// round to midnight this morning
PRExplodedTime explodedTime ;
PR_ExplodeTime ( aTime , PR_LocalTimeParameters , & explodedTime ) ;
// set to midnight (0:00)
explodedTime . tm_min =
explodedTime . tm_hour =
explodedTime . tm_sec =
explodedTime . tm_usec = 0 ;
return PR_ImplodeTime ( & explodedTime ) ;
}
2006-07-18 15:56:56 +00:00
2006-07-18 16:03:30 +00:00
// nsNavHistory::NormalizeTime
//
// Converts a nsINavHistoryQuery reference+offset time into a PRTime
// relative to the epoch.
2006-07-18 16:19:04 +00:00
//
// It is important that this function NOT use the current time optimization.
// It is called to update queries, and we really need to know what right
// now is because those incoming values will also have current times that
// we will have to compare against.
2006-07-18 16:03:30 +00:00
2006-07-18 16:19:04 +00:00
PRTime // static
2006-07-18 16:03:30 +00:00
nsNavHistory : : NormalizeTime ( PRUint32 aRelative , PRTime aOffset )
{
PRTime ref ;
switch ( aRelative )
{
case nsINavHistoryQuery : : TIME_RELATIVE_EPOCH :
return aOffset ;
2006-11-21 23:02:16 +00:00
case nsINavHistoryQuery : : TIME_RELATIVE_TODAY :
ref = NormalizeTimeRelativeToday ( PR_Now ( ) ) ;
2006-07-18 16:03:30 +00:00
break ;
case nsINavHistoryQuery : : TIME_RELATIVE_NOW :
2006-07-18 16:19:04 +00:00
ref = PR_Now ( ) ;
2006-07-18 16:03:30 +00:00
break ;
default :
NS_NOTREACHED ( " Invalid relative time " ) ;
return 0 ;
}
return ref + aOffset ;
}
2006-07-18 16:19:04 +00:00
// nsNavHistory::CanLiveUpdateQuery
//
// Returns true if this set of queries/options can be live-updated. That is,
// we can look at a node and compare its attributes to the query and easily
// tell whether it belongs in the result set or not.
//
// QUERYUPDATE_TIME:
// This query is only limited by an inclusive time range on the first
// query object. The caller can quickly evaluate the time itself if it
// chooses. This is even simpler than "simple" below.
// QUERYUPDATE_SIMPLE:
// This query is evaluatable using EvaluateQueryForNode to do live
// updating.
// QUERYUPDATE_COMPLEX:
// This query is not evaluatable using EvaluateQueryForNode. When something
// happens that this query updates, you will need to re-run the query.
// QUERYUPDATE_COMPLEX_WITH_BOOKMARKS:
// A complex query that additionally has dependence on bookmarks. All
// bookmark-dependent queries fall under this category.
//
// aHasSearchTerms will be set to true if the query has any dependence on
// keywords. When there is no dependence on keywords, we can handle title
// change operations as simple instead of complex.
PRUint32
2006-07-18 16:20:08 +00:00
nsNavHistory : : GetUpdateRequirements ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
2006-07-18 16:19:04 +00:00
nsNavHistoryQueryOptions * aOptions ,
PRBool * aHasSearchTerms )
{
2006-07-18 16:20:08 +00:00
NS_ASSERTION ( aQueries . Count ( ) > 0 , " Must have at least one query " ) ;
2006-07-18 16:19:04 +00:00
// first check if there are search terms
* aHasSearchTerms = PR_FALSE ;
2006-07-18 16:20:33 +00:00
PRInt32 i ;
for ( i = 0 ; i < aQueries . Count ( ) ; i + + ) {
2006-07-18 16:20:08 +00:00
aQueries [ i ] - > GetHasSearchTerms ( aHasSearchTerms ) ;
2006-07-18 16:19:04 +00:00
if ( * aHasSearchTerms )
break ;
}
PRBool nonTimeBasedItems = PR_FALSE ;
2006-07-18 16:20:33 +00:00
for ( i = 0 ; i < aQueries . Count ( ) ; i + + ) {
2006-07-18 16:20:08 +00:00
nsNavHistoryQuery * query = aQueries [ i ] ;
2006-07-18 16:19:04 +00:00
if ( query - > Folders ( ) . Length ( ) > 0 | | query - > OnlyBookmarked ( ) ) {
return QUERYUPDATE_COMPLEX_WITH_BOOKMARKS ;
}
// Note: we don't currently have any complex non-bookmarked items, but these
// are expected to be added. Put detection of these items here.
if ( ! query - > SearchTerms ( ) . IsEmpty ( ) | |
! query - > Domain ( ) . IsVoid ( ) | |
query - > Uri ( ) ! = nsnull )
nonTimeBasedItems = PR_TRUE ;
}
2007-07-17 01:13:50 +00:00
// Whenever there is a maximum number of results,
// and we are not a bookmark query we must requery. This
// is because we can't generally know if any given addition/change causes
// the item to be in the top N items in the database.
if ( aOptions - > MaxResults ( ) > 0 )
return QUERYUPDATE_COMPLEX ;
2006-07-18 16:20:08 +00:00
if ( aQueries . Count ( ) = = 1 & & ! nonTimeBasedItems )
2006-07-18 16:19:04 +00:00
return QUERYUPDATE_TIME ;
return QUERYUPDATE_SIMPLE ;
}
// nsNavHistory::EvaluateQueryForNode
//
// This runs the node through the given queries to see if satisfies the
// query conditions. Not every query parameters are handled by this code,
// but we handle the most common ones so that performance is better.
//
// We assume that the time on the node is the time that we want to compare.
// This is not necessarily true because URL nodes have the last access time,
// which is not necessarily the same. However, since this is being called
// to update the list, we assume that the last access time is the current
// access time that we are being asked to compare so it works out.
//
// Returns true if node matches the query, false if not.
PRBool
2006-07-18 16:20:08 +00:00
nsNavHistory : : EvaluateQueryForNode ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
2006-07-18 16:19:04 +00:00
nsNavHistoryQueryOptions * aOptions ,
2006-07-18 16:22:38 +00:00
nsNavHistoryResultNode * aNode )
2006-07-18 16:19:04 +00:00
{
// lazily created from the node's string when we need to match URIs
nsCOMPtr < nsIURI > nodeUri ;
2006-07-18 16:20:08 +00:00
for ( PRInt32 i = 0 ; i < aQueries . Count ( ) ; i + + ) {
2006-07-18 16:19:04 +00:00
PRBool hasIt ;
2006-07-18 16:20:08 +00:00
nsCOMPtr < nsNavHistoryQuery > query = aQueries [ i ] ;
2006-07-18 16:19:04 +00:00
// --- begin time ---
query - > GetHasBeginTime ( & hasIt ) ;
if ( hasIt ) {
PRTime beginTime = NormalizeTime ( query - > BeginTimeReference ( ) ,
query - > BeginTime ( ) ) ;
if ( aNode - > mTime < beginTime )
continue ; // before our time range
}
// --- end time ---
query - > GetHasEndTime ( & hasIt ) ;
if ( hasIt ) {
PRTime endTime = NormalizeTime ( query - > EndTimeReference ( ) ,
query - > EndTime ( ) ) ;
if ( aNode - > mTime > endTime )
continue ; // after our time range
}
// --- search terms ---
if ( ! query - > SearchTerms ( ) . IsEmpty ( ) ) {
// we can use the existing filtering code, just give it our one object in
// an array.
nsCOMArray < nsNavHistoryResultNode > inputSet ;
inputSet . AppendObject ( aNode ) ;
2007-09-15 20:07:02 +00:00
nsCOMArray < nsNavHistoryQuery > queries ;
queries . AppendObject ( query ) ;
2006-07-18 16:19:04 +00:00
nsCOMArray < nsNavHistoryResultNode > filteredSet ;
2007-10-01 17:36:18 +00:00
nsresult rv = FilterResultSet ( nsnull , inputSet , & filteredSet , queries , aOptions ) ;
2006-07-18 16:19:04 +00:00
if ( NS_FAILED ( rv ) )
continue ;
if ( ! filteredSet . Count ( ) )
continue ; // did not make it through the filter, doesn't match
}
// --- domain/host matching ---
query - > GetHasDomain ( & hasIt ) ;
if ( hasIt ) {
if ( ! nodeUri ) {
// lazy creation of nodeUri, which might be checked for multiple queries
if ( NS_FAILED ( NS_NewURI ( getter_AddRefs ( nodeUri ) , aNode - > mURI ) ) )
continue ;
}
nsCAutoString host ;
if ( NS_FAILED ( nodeUri - > GetAsciiHost ( host ) ) )
continue ;
nsCAutoString asciiRequest ;
if ( NS_FAILED ( AsciiHostNameFromHostString ( query - > Domain ( ) , asciiRequest ) ) )
continue ;
if ( query - > DomainIsHost ( ) ) {
if ( ! asciiRequest . Equals ( host ) )
continue ; // host names don't match
}
// check domain names
nsCAutoString domain ;
DomainNameFromHostName ( host , domain ) ;
if ( ! asciiRequest . Equals ( domain ) )
continue ; // domain names don't match
}
// --- URI matching ---
if ( query - > Uri ( ) ) {
if ( ! nodeUri ) { // lazy creation of nodeUri
if ( NS_FAILED ( NS_NewURI ( getter_AddRefs ( nodeUri ) , aNode - > mURI ) ) )
continue ;
}
if ( ! query - > UriIsPrefix ( ) ) {
// easy case: the URI is an exact match
PRBool equals ;
2006-07-18 16:20:08 +00:00
nsresult rv = query - > Uri ( ) - > Equals ( nodeUri , & equals ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2006-07-18 16:19:04 +00:00
if ( ! equals )
continue ;
} else {
// harder case: match prefix, note that we need to get the ASCII string
// from the node's parsed URI instead of using the node's mUrl string,
// because that might not be normalized
nsCAutoString nodeUriString ;
nodeUri - > GetAsciiSpec ( nodeUriString ) ;
nsCAutoString queryUriString ;
query - > Uri ( ) - > GetAsciiSpec ( queryUriString ) ;
if ( queryUriString . Length ( ) > nodeUriString . Length ( ) )
continue ; // not long enough to match as prefix
nodeUriString . SetLength ( queryUriString . Length ( ) ) ;
if ( ! nodeUriString . Equals ( queryUriString ) )
continue ; // prefixes don't match
}
}
// If we ever make it to the bottom of this loop, that means it passed all
// tests for the given query. Since queries are ORed together, that means
// it passed everything and we are done.
return PR_TRUE ;
}
// didn't match any query
return PR_FALSE ;
}
// nsNavHistory::AsciiHostNameFromHostString
//
// We might have interesting encodings and different case in the host name.
// This will convert that host name into an ASCII host name by sending it
// through the URI canonicalization. The result can be used for comparison
// with other ASCII host name strings.
nsresult // static
nsNavHistory : : AsciiHostNameFromHostString ( const nsACString & aHostName ,
nsACString & aAscii )
{
nsCOMPtr < nsIURI > uri ;
nsresult rv = NS_NewURI ( getter_AddRefs ( uri ) , aHostName ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return uri - > GetAsciiHost ( aAscii ) ;
}
// nsNavHistory::DomainNameFromHostName
//
// This does the www.mozilla.org -> mozilla.org and
// foo.theregister.co.uk -> theregister.co.uk conversion
void // static
nsNavHistory : : DomainNameFromHostName ( const nsCString & aHostName ,
nsACString & aDomainName )
{
if ( IsNumericHostName ( aHostName ) ) {
// easy case
aDomainName = aHostName ;
} else {
// compute the toplevel domain name
PRInt32 tldLength = GetTLDCharCount ( aHostName ) ;
if ( tldLength < PRInt32 ( aHostName . Length ( ) ) ) {
// bugzilla.mozilla.org : tldLength = 3, topDomain = mozilla.org
GetSubstringFromNthDot ( aHostName ,
aHostName . Length ( ) - tldLength - 2 ,
1 , PR_FALSE , aDomainName ) ;
}
}
}
2006-07-18 15:56:56 +00:00
// Nav history *****************************************************************
// nsNavHistory::GetHasHistoryEntries
NS_IMETHODIMP
nsNavHistory : : GetHasHistoryEntries ( PRBool * aHasEntries )
{
nsCOMPtr < mozIStorageStatement > dbSelectStatement ;
nsresult rv = mDBConn - > CreateStatement (
2006-12-08 19:57:12 +00:00
NS_LITERAL_CSTRING ( " SELECT id FROM moz_historyvisits LIMIT 1 " ) ,
2006-07-18 15:56:56 +00:00
getter_AddRefs ( dbSelectStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return dbSelectStatement - > ExecuteStep ( aHasEntries ) ;
}
2008-01-25 17:11:14 +00:00
nsresult
nsNavHistory : : FixInvalidFrecenciesForExcludedPlaces ( )
{
// for every moz_place that has an invalid frecency (-1) and
// begins with "place:" or is an unvisited child of a livemark feed,
// set frecency to 0 so that it is excluded from url bar autocomplete.
nsCOMPtr < mozIStorageStatement > dbUpdateStatement ;
nsresult rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " UPDATE moz_places SET frecency = 0 WHERE id IN ( "
" SELECT h.id FROM moz_places h JOIN moz_bookmarks b ON h.id = b.fk "
" WHERE frecency = -1 "
// place is not a livemark feed item
" AND (b.parent IN ( "
" SELECT annos.item_id FROM moz_anno_attributes attrs "
" JOIN moz_items_annos annos ON attrs.id = annos.anno_attribute_id "
" WHERE attrs.name = ?1) "
// place has no visits (that are not invalid or embedded)
" AND (SELECT visit_date FROM moz_historyvisits "
" WHERE place_id = h.id AND visit_type NOT IN (0,4) LIMIT 1) is null) "
" OR SUBSTR(h.url,0,6) = 'place:') " ) ,
getter_AddRefs ( dbUpdateStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = dbUpdateStatement - > BindUTF8StringParameter ( 0 , NS_LITERAL_CSTRING ( LMANNO_FEEDURI ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = dbUpdateStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
nsresult
nsNavHistory : : CalculateVisitCount ( PRInt64 aPlaceId , PRBool aForFrecency , PRInt32 * aVisitCount )
{
nsCOMPtr < mozIStorageStatement > dbSelectStatement =
aForFrecency ? mDBVisitCountForFrecency : mDBTrueVisitCount ;
mozStorageStatementScoper scope ( dbSelectStatement ) ;
nsresult rv = dbSelectStatement - > BindInt64Parameter ( 0 , aPlaceId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasVisits = PR_TRUE ;
rv = dbSelectStatement - > ExecuteStep ( & hasVisits ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( hasVisits ) {
rv = dbSelectStatement - > GetInt32 ( 0 , aVisitCount ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
else
* aVisitCount = 0 ;
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
// nsNavHistory::MarkPageAsFollowedBookmark
//
2007-12-28 01:53:59 +00:00
// We call MarkPageAsFollowedBookmark() before visiting a URL in order to
// help determine the transition type of the visit.
// We keep track of the URL so that later, in AddVisitChain()
// we can use TRANSITION_BOOKMARK as the transition.
// Note, AddVisitChain() is not called immediately when we are doing LAZY_ADDs
//
// @see MarkPageAsTyped
2006-07-18 16:14:12 +00:00
NS_IMETHODIMP
nsNavHistory : : MarkPageAsFollowedBookmark ( nsIURI * aURI )
{
2006-07-18 16:36:48 +00:00
// don't add when history is disabled
if ( IsHistoryDisabled ( ) )
2006-07-18 16:32:58 +00:00
return NS_OK ;
2006-07-18 16:14:12 +00:00
nsCAutoString uriString ;
2008-01-25 17:11:14 +00:00
nsresult rv = aURI - > GetSpec ( uriString ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:14:12 +00:00
2008-01-25 17:11:14 +00:00
// if URL is already in the bookmark queue, then we need to remove the old one
2006-07-18 16:14:12 +00:00
PRInt64 unusedEventTime ;
if ( mRecentBookmark . Get ( uriString , & unusedEventTime ) )
mRecentBookmark . Remove ( uriString ) ;
if ( mRecentBookmark . Count ( ) > RECENT_EVENT_QUEUE_MAX_LENGTH )
ExpireNonrecentEvents ( & mRecentBookmark ) ;
2007-12-28 01:53:59 +00:00
mRecentBookmark . Put ( uriString , GetNow ( ) ) ;
2006-07-18 16:14:12 +00:00
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::CanAddURI
//
// Filter out unwanted URIs such as "chrome:", "mailbox:", etc.
//
// The model is if we don't know differently then add which basically means
// we are suppose to try all the things we know not to allow in and then if
// we don't bail go on and allow it in.
nsresult
nsNavHistory : : CanAddURI ( nsIURI * aURI , PRBool * canAdd )
{
2008-01-25 17:11:14 +00:00
nsCAutoString scheme ;
nsresult rv = aURI - > GetScheme ( scheme ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// first check the most common cases (HTTP, HTTPS) to allow in to avoid most
// of the work
if ( scheme . EqualsLiteral ( " http " ) ) {
* canAdd = PR_TRUE ;
return NS_OK ;
}
if ( scheme . EqualsLiteral ( " https " ) ) {
* canAdd = PR_TRUE ;
return NS_OK ;
}
// now check for all bad things
if ( scheme . EqualsLiteral ( " about " ) | |
scheme . EqualsLiteral ( " imap " ) | |
scheme . EqualsLiteral ( " news " ) | |
scheme . EqualsLiteral ( " mailbox " ) | |
2006-07-18 16:07:59 +00:00
scheme . EqualsLiteral ( " moz-anno " ) | |
2006-07-18 15:56:56 +00:00
scheme . EqualsLiteral ( " view-source " ) | |
scheme . EqualsLiteral ( " chrome " ) | |
scheme . EqualsLiteral ( " data " ) ) {
* canAdd = PR_FALSE ;
return NS_OK ;
}
* canAdd = PR_TRUE ;
return NS_OK ;
}
2006-07-18 16:16:13 +00:00
// nsNavHistory::SetPageDetails
NS_IMETHODIMP
nsNavHistory : : SetPageDetails ( nsIURI * aURI , const nsAString & aTitle ,
2007-07-26 16:23:11 +00:00
PRUint32 aVisitCount , PRBool aHidden ,
PRBool aTyped )
2006-07-18 16:16:13 +00:00
{
// look up the page ID, creating a new one if necessary
PRInt64 pageID ;
nsresult rv = GetUrlIdFor ( aURI , & pageID , PR_TRUE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCOMPtr < mozIStorageStatement > statement ;
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2006-12-08 19:57:12 +00:00
" UPDATE moz_places "
2006-07-18 16:16:13 +00:00
" SET title = ?2, "
" visit_count = ?4, "
" hidden = ?5, "
" typed = ?6 "
" WHERE id = ?1 " ) ,
getter_AddRefs ( statement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > BindInt64Parameter ( 0 , pageID ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:27:34 +00:00
2008-01-25 17:11:14 +00:00
// XXX should we be calculating / setting frecency here?
2006-07-18 16:27:34 +00:00
// for the titles, be careful to interpret isVoid as NULL SQL command so that
// we can tell the difference between "set to empty" and "unset"
if ( aTitle . IsVoid ( ) )
statement - > BindNullParameter ( 1 ) ;
else
2006-07-18 16:29:14 +00:00
statement - > BindStringParameter ( 1 , StringHead ( aTitle , HISTORY_TITLE_LENGTH_MAX ) ) ;
2006-07-18 16:16:13 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:27:34 +00:00
2006-07-18 16:16:13 +00:00
statement - > BindInt32Parameter ( 3 , aVisitCount ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
statement - > BindInt32Parameter ( 4 , aHidden ? 1 : 0 ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
statement - > BindInt32Parameter ( 5 , aTyped ? 1 : 0 ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
// nsNavHistory::AddVisit
2006-07-18 16:23:51 +00:00
//
// Adds or updates a page with the given URI. The ID of the new visit will
// be put into aVisitID.
//
// THE RETURNED NEW VISIT ID MAY BE 0 indicating that this page should not be
// added to the history.
2006-07-18 16:16:13 +00:00
NS_IMETHODIMP
2007-12-21 19:37:18 +00:00
nsNavHistory : : AddVisit ( nsIURI * aURI , PRTime aTime , nsIURI * aReferringURI ,
2006-07-18 16:23:51 +00:00
PRInt32 aTransitionType , PRBool aIsRedirect ,
PRInt64 aSessionID , PRInt64 * aVisitID )
2006-07-18 16:16:13 +00:00
{
2006-07-18 16:23:51 +00:00
// Filter out unwanted URIs, silently failing
PRBool canAdd = PR_FALSE ;
2008-01-25 17:11:14 +00:00
nsresult rv = CanAddURI ( aURI , & canAdd ) ;
2006-07-18 16:16:13 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:23:51 +00:00
if ( ! canAdd ) {
* aVisitID = 0 ;
return NS_OK ;
}
2006-07-18 16:16:13 +00:00
2006-07-18 16:23:51 +00:00
// in-memory version
# ifdef IN_MEMORY_LINKS
rv = BindStatementURI ( mMemDBAddPage , 0 , aURI ) ;
2006-07-18 16:21:22 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:23:51 +00:00
mMemDBAddPage - > Execute ( ) ;
# endif
2006-07-18 16:21:22 +00:00
2006-07-18 16:23:51 +00:00
// This will prevent corruption since we have to do a two-phase add.
// Generally this won't do anything because AddURI has its own transaction.
2007-11-20 23:27:41 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
2006-07-18 16:23:51 +00:00
// see if this is an update (revisit) or a new page
mozStorageStatementScoper scoper ( mDBGetPageVisitStats ) ;
rv = BindStatementURI ( mDBGetPageVisitStats , 0 , aURI ) ;
2006-07-18 16:21:22 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:23:51 +00:00
PRBool alreadyVisited = PR_TRUE ;
rv = mDBGetPageVisitStats - > ExecuteStep ( & alreadyVisited ) ;
PRInt64 pageID = 0 ;
2008-01-25 17:11:14 +00:00
PRBool hidden ; // XXX fix me, this should not be a PRBool, as we later do BindInt32Parameter()
PRBool typed ; // XXX fix me, this should not be a PRBool, as we later do BindInt32Parameter()
2006-07-18 16:23:51 +00:00
PRBool newItem = PR_FALSE ; // used to send out notifications at the end
if ( alreadyVisited ) {
// Update the existing entry...
rv = mDBGetPageVisitStats - > GetInt64 ( 0 , & pageID ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 oldVisitCount = 0 ;
rv = mDBGetPageVisitStats - > GetInt32 ( 1 , & oldVisitCount ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 oldTypedState = 0 ;
rv = mDBGetPageVisitStats - > GetInt32 ( 2 , & oldTypedState ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-30 05:04:55 +00:00
PRBool oldHiddenState = 0 ;
2006-07-18 16:23:51 +00:00
rv = mDBGetPageVisitStats - > GetInt32 ( 3 , & oldHiddenState ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// free the previous statement before we make a new one
mDBGetPageVisitStats - > Reset ( ) ;
scoper . Abandon ( ) ;
// embedded links and redirects will be hidden, but don't hide pages that
// are already unhidden.
//
// Note that we test the redirect flag and not for the redirect transition
// type. The transition type refers to how we got here, and whether a page
// is shown does not depend on whether you got to it through a redirect.
// Rather, we want to hide pages that do not themselves redirect somewhere
// else, which is what the redirect flag means.
2007-08-20 22:43:28 +00:00
//
// note, we want to unhide any hidden pages that the user explicitly types
// (aTransitionType == TRANSITION_TYPED) so that they will appear in
// the history UI (sidebar, history menu, url bar autocomplete, etc)
2006-07-18 16:23:51 +00:00
hidden = oldHiddenState ;
2007-08-20 22:43:28 +00:00
if ( hidden & & ( ! aIsRedirect | | aTransitionType = = TRANSITION_TYPED ) & &
2008-01-25 17:11:14 +00:00
aTransitionType ! = TRANSITION_EMBED )
2006-07-18 16:23:51 +00:00
hidden = PR_FALSE ; // unhide
2006-07-18 16:32:33 +00:00
typed = oldTypedState | | ( aTransitionType = = TRANSITION_TYPED ) ;
2008-01-25 17:11:14 +00:00
PRInt32 trueVisitCount = 0 ;
// we can't trust the visit_count in the moz_places, because...
rv = CalculateVisitCount ( pageID , PR_FALSE /* not for frecency */ ,
& trueVisitCount ) ;
// some items may have a true visit count of 0 which will not count for link
2006-07-18 16:23:51 +00:00
// visiting, so be sure to note this transition
2008-01-25 17:11:14 +00:00
if ( trueVisitCount = = 0 )
2006-07-18 16:23:51 +00:00
newItem = PR_TRUE ;
// update with new stats
mozStorageStatementScoper updateScoper ( mDBUpdatePageVisitStats ) ;
rv = mDBUpdatePageVisitStats - > BindInt64Parameter ( 0 , pageID ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
// only increment the visit_count if the transition was not EMBED
// XXX what about TRANSITION_DOWNLOAD? (bug 412217)
if ( aTransitionType ! = TRANSITION_EMBED )
trueVisitCount + + ;
rv = mDBUpdatePageVisitStats - > BindInt32Parameter ( 1 , trueVisitCount ) ;
2006-07-18 16:23:51 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBUpdatePageVisitStats - > BindInt32Parameter ( 2 , hidden ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:32:33 +00:00
rv = mDBUpdatePageVisitStats - > BindInt32Parameter ( 3 , typed ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
2006-07-18 16:23:51 +00:00
rv = mDBUpdatePageVisitStats - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
} else {
// New page
newItem = PR_TRUE ;
// free the previous statement before we make a new one
mDBGetPageVisitStats - > Reset ( ) ;
scoper . Abandon ( ) ;
2007-11-13 02:54:32 +00:00
// Hide only embedded links, redirects, and downloads
2006-07-18 16:23:51 +00:00
// See the hidden computation code above for a little more explanation.
2007-11-13 02:54:32 +00:00
hidden = ( aTransitionType = = TRANSITION_EMBED | | aIsRedirect | |
aTransitionType = = TRANSITION_DOWNLOAD ) ;
2006-07-18 16:23:51 +00:00
2006-07-18 16:32:33 +00:00
typed = ( aTransitionType = = TRANSITION_TYPED ) ;
// set as visited once, no title
2006-07-18 16:28:24 +00:00
nsString voidString ;
voidString . SetIsVoid ( PR_TRUE ) ;
2008-01-25 17:11:14 +00:00
rv = InternalAddNewPage ( aURI , voidString , hidden , typed , 1 , PR_TRUE , & pageID ) ;
2006-07-18 16:23:51 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-12-21 19:37:18 +00:00
// Get the place id for the referrer, if we have one
PRInt64 referringVisitID = 0 ;
PRInt64 referringSessionID ;
if ( aReferringURI & &
! FindLastVisit ( aReferringURI , & referringVisitID , & referringSessionID ) ) {
// Add the referrer
rv = AddVisit ( aReferringURI , aTime - 1 , nsnull , TRANSITION_LINK , PR_FALSE ,
aSessionID , & referringVisitID ) ;
if ( NS_FAILED ( rv ) )
referringVisitID = 0 ;
}
rv = InternalAddVisit ( pageID , referringVisitID , aSessionID , aTime ,
2006-07-18 16:23:51 +00:00
aTransitionType , aVisitID ) ;
2008-01-25 17:11:14 +00:00
transaction . Commit ( ) ;
// Update frecency (*after* the visit info is in the db)
// Swallow errors here, since if we've gotten this far, it's more
// important to notify the observers below.
( void ) UpdateFrecency ( pageID , PR_FALSE ) ;
2006-07-18 16:23:51 +00:00
2006-07-18 16:34:38 +00:00
// Notify observers: The hidden detection code must match that in
// GetQueryResults to maintain consistency.
2006-07-18 16:23:51 +00:00
// FIXME bug 325241: make a way to observe hidden URLs
2006-07-18 16:34:38 +00:00
if ( ! hidden & & aTransitionType ! = TRANSITION_EMBED ) {
2006-07-18 16:23:51 +00:00
ENUMERATE_WEAKARRAY ( mObservers , nsINavHistoryObserver ,
OnVisit ( aURI , * aVisitID , aTime , aSessionID ,
2007-12-21 19:37:18 +00:00
referringVisitID , aTransitionType ) ) ;
2006-07-18 16:23:51 +00:00
}
2008-01-25 17:11:14 +00:00
// Normally docshell sends the link visited observer notification for us (this
2006-07-18 16:23:51 +00:00
// will tell all the documents to update their visited link coloring).
2007-12-27 01:48:19 +00:00
// However, for redirects (since we implement nsIGlobalHistory3) and downloads
// (since we implement nsIDownloadHistory) this will not happen and we need to
// send it ourselves.
if ( newItem & & ( aIsRedirect | | aTransitionType = = TRANSITION_DOWNLOAD ) ) {
2006-07-18 16:23:51 +00:00
nsCOMPtr < nsIObserverService > obsService =
do_GetService ( " @mozilla.org/observer-service;1 " ) ;
if ( obsService )
obsService - > NotifyObservers ( aURI , NS_LINK_VISITED_EVENT_TOPIC , nsnull ) ;
}
return NS_OK ;
2006-07-18 16:16:13 +00:00
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::GetNewQuery
NS_IMETHODIMP nsNavHistory : : GetNewQuery ( nsINavHistoryQuery * * _retval )
{
* _retval = new nsNavHistoryQuery ( ) ;
if ( ! * _retval )
return NS_ERROR_OUT_OF_MEMORY ;
2006-07-18 15:59:28 +00:00
NS_ADDREF ( * _retval ) ;
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
2006-07-18 16:12:34 +00:00
// nsNavHistory::GetNewQueryOptions
2006-07-18 15:59:28 +00:00
NS_IMETHODIMP nsNavHistory : : GetNewQueryOptions ( nsINavHistoryQueryOptions * * _retval )
{
* _retval = new nsNavHistoryQueryOptions ( ) ;
NS_ENSURE_TRUE ( * _retval , NS_ERROR_OUT_OF_MEMORY ) ;
NS_ADDREF ( * _retval ) ;
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::ExecuteQuery
//
NS_IMETHODIMP
2006-07-18 15:59:28 +00:00
nsNavHistory : : ExecuteQuery ( nsINavHistoryQuery * aQuery , nsINavHistoryQueryOptions * aOptions ,
2006-07-18 15:56:56 +00:00
nsINavHistoryResult * * _retval )
{
2006-07-18 16:02:41 +00:00
return ExecuteQueries ( & aQuery , 1 , aOptions , _retval ) ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::ExecuteQueries
//
2006-07-18 16:19:04 +00:00
// This function is actually very simple, we just create the proper root node (either
// a bookmark folder or a complex query node) and assign it to the result. The node
// will then populate itself accordingly.
//
// Quick overview of query operation: When you call this function, we will construct
// the correct container node and set the options you give it. This node will then
// fill itself. Folder nodes will call nsNavBookmarks::QueryFolderChildren, and
// all other queries will call GetQueryResults. If these results contain other
// queries, those will be populated when the container is opened.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
2006-07-18 16:02:41 +00:00
nsNavHistory : : ExecuteQueries ( nsINavHistoryQuery * * aQueries , PRUint32 aQueryCount ,
2006-07-18 15:59:28 +00:00
nsINavHistoryQueryOptions * aOptions ,
2006-07-18 15:56:56 +00:00
nsINavHistoryResult * * _retval )
{
2006-07-18 16:19:04 +00:00
nsresult rv ;
2006-07-18 16:04:43 +00:00
NS_ENSURE_ARG_POINTER ( aQueries ) ;
NS_ENSURE_ARG_POINTER ( aOptions ) ;
2006-07-18 16:19:04 +00:00
if ( ! aQueryCount )
return NS_ERROR_INVALID_ARG ;
2006-07-18 16:04:43 +00:00
2006-07-18 16:19:04 +00:00
// concrete options
2006-07-18 15:59:28 +00:00
nsCOMPtr < nsNavHistoryQueryOptions > options = do_QueryInterface ( aOptions ) ;
NS_ENSURE_TRUE ( options , NS_ERROR_INVALID_ARG ) ;
2006-07-18 16:20:08 +00:00
// concrete queries array
nsCOMArray < nsNavHistoryQuery > queries ;
for ( PRUint32 i = 0 ; i < aQueryCount ; i + + ) {
nsCOMPtr < nsNavHistoryQuery > query = do_QueryInterface ( aQueries [ i ] , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
queries . AppendObject ( query ) ;
}
2006-07-18 16:19:04 +00:00
// root node
nsRefPtr < nsNavHistoryContainerResultNode > rootNode ;
2007-04-25 21:03:29 +00:00
PRInt64 folderId = GetSimpleBookmarksQueryFolder ( queries , options ) ;
2006-07-18 16:19:04 +00:00
if ( folderId ) {
// In the simple case where we're just querying children of a single bookmark
// folder, we can more efficiently generate results.
2006-07-18 16:20:08 +00:00
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
NS_ENSURE_TRUE ( bookmarks , NS_ERROR_OUT_OF_MEMORY ) ;
2007-10-19 06:26:34 +00:00
nsRefPtr < nsNavHistoryResultNode > tempRootNode ;
2007-08-02 20:19:44 +00:00
rv = bookmarks - > ResultNodeForContainer ( folderId , options ,
getter_AddRefs ( tempRootNode ) ) ;
2006-07-18 16:20:08 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rootNode = tempRootNode - > GetAsContainer ( ) ;
2006-07-18 16:19:04 +00:00
} else {
// complex query
2006-07-18 16:20:08 +00:00
rootNode = new nsNavHistoryQueryResultNode ( EmptyCString ( ) , EmptyCString ( ) ,
queries , options ) ;
NS_ENSURE_TRUE ( rootNode , NS_ERROR_OUT_OF_MEMORY ) ;
2006-07-18 16:19:04 +00:00
}
// result object
nsRefPtr < nsNavHistoryResult > result ;
rv = nsNavHistoryResult : : NewHistoryResult ( aQueries , aQueryCount , options , rootNode ,
getter_AddRefs ( result ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
NS_ADDREF ( * _retval = result ) ;
return NS_OK ;
}
2007-07-26 21:39:06 +00:00
// determine from our nsNavHistoryQuery array and nsNavHistoryQueryOptions
// if this is the place query from the history menu.
// from browser-menubar.inc, our history menu query is:
// place:type=0&sort=4&maxResults=10
// note, any maxResult > 0 will still be considered a history menu query
2007-12-06 07:38:52 +00:00
// or if this is the place query from the "Most Visited" item in the "Smart Bookmarks" folder:
// place:queryType=0&sort=8&maxResults=10
// note, any maxResult > 0 will still be considered a Most Visited menu query
2007-07-26 21:39:06 +00:00
static
2007-12-06 07:38:52 +00:00
PRBool IsHistoryMenuQuery ( const nsCOMArray < nsNavHistoryQuery > & aQueries , nsNavHistoryQueryOptions * aOptions , PRUint16 aSortMode )
2007-07-26 21:39:06 +00:00
{
if ( aQueries . Count ( ) ! = 1 )
return PR_FALSE ;
2006-07-18 16:19:04 +00:00
2007-07-26 21:39:06 +00:00
nsNavHistoryQuery * aQuery = aQueries [ 0 ] ;
if ( aOptions - > QueryType ( ) ! = nsINavHistoryQueryOptions : : QUERY_TYPE_HISTORY )
return PR_FALSE ;
2006-07-18 16:19:04 +00:00
2007-07-26 21:39:06 +00:00
if ( aOptions - > ResultType ( ) ! = nsINavHistoryQueryOptions : : RESULTS_AS_URI )
return PR_FALSE ;
2006-07-18 16:19:04 +00:00
2007-12-06 07:38:52 +00:00
if ( aOptions - > SortingMode ( ) ! = aSortMode )
2007-07-26 21:39:06 +00:00
return PR_FALSE ;
if ( aOptions - > MaxResults ( ) < = 0 )
return PR_FALSE ;
PRUint32 groupCount ;
const PRUint16 * groupings = aOptions - > GroupingMode ( & groupCount ) ;
if ( groupings | | groupCount )
return PR_FALSE ;
if ( aOptions - > ExcludeItems ( ) )
return PR_FALSE ;
if ( aOptions - > IncludeHidden ( ) )
return PR_FALSE ;
if ( aQuery - > MinVisits ( ) ! = - 1 | | aQuery - > MaxVisits ( ) ! = - 1 )
return PR_FALSE ;
if ( aQuery - > BeginTime ( ) | | aQuery - > BeginTimeReference ( ) )
return PR_FALSE ;
if ( aQuery - > EndTime ( ) | | aQuery - > EndTimeReference ( ) )
return PR_FALSE ;
if ( ! aQuery - > SearchTerms ( ) . IsEmpty ( ) )
return PR_FALSE ;
if ( aQuery - > OnlyBookmarked ( ) )
return PR_FALSE ;
if ( aQuery - > DomainIsHost ( ) | | ! aQuery - > Domain ( ) . IsEmpty ( ) )
return PR_FALSE ;
if ( aQuery - > AnnotationIsNot ( ) | | ! aQuery - > Annotation ( ) . IsEmpty ( ) )
return PR_FALSE ;
2006-07-18 16:19:04 +00:00
2007-07-26 21:39:06 +00:00
if ( aQuery - > UriIsPrefix ( ) | | aQuery - > Uri ( ) )
return PR_FALSE ;
if ( aQuery - > Folders ( ) . Length ( ) > 0 )
return PR_FALSE ;
return PR_TRUE ;
}
2007-10-01 17:36:18 +00:00
static
PRBool NeedToFilterResultSet ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsNavHistoryQueryOptions * aOptions )
{
// optimize the case where we just want a list with no grouping: this
// directly fills in the results and we avoid a copy of the whole list
PRUint32 groupCount ;
const PRUint16 * groupings = aOptions - > GroupingMode ( & groupCount ) ;
2007-11-20 02:01:53 +00:00
// Always filter bookmarks queries to avoid the inclusion of query nodes
if ( groupCount ! = 0 | |
aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS )
2007-10-01 17:36:18 +00:00
return PR_TRUE ;
2007-10-11 06:42:38 +00:00
nsCString parentAnnotationToExclude ;
nsresult rv = aOptions - > GetExcludeItemIfParentHasAnnotation ( parentAnnotationToExclude ) ;
NS_ENSURE_SUCCESS ( rv , PR_TRUE ) ;
if ( ! parentAnnotationToExclude . IsEmpty ( ) )
return PR_TRUE ;
2007-10-01 17:36:18 +00:00
PRInt32 i ;
for ( i = 0 ; i < aQueries . Count ( ) ; i + + ) {
if ( aQueries [ i ] - > Folders ( ) . Length ( ) ! = 0 ) {
return PR_TRUE ;
} else {
PRBool hasSearchTerms ;
nsresult rv = aQueries [ i ] - > GetHasSearchTerms ( & hasSearchTerms ) ;
if ( NS_FAILED ( rv ) | | hasSearchTerms )
return PR_TRUE ;
}
}
return PR_FALSE ;
}
2007-07-26 21:39:06 +00:00
nsresult
nsNavHistory : : ConstructQueryString ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsNavHistoryQueryOptions * aOptions ,
nsCString & queryString )
{
2006-07-18 16:19:04 +00:00
PRInt32 sortingMode = aOptions - > SortingMode ( ) ;
2006-07-18 15:59:28 +00:00
if ( sortingMode < 0 | |
2007-04-22 22:20:25 +00:00
sortingMode > nsINavHistoryQueryOptions : : SORT_BY_ANNOTATION_DESCENDING ) {
2006-07-18 15:56:56 +00:00
return NS_ERROR_INVALID_ARG ;
2006-07-18 15:59:28 +00:00
}
2007-07-26 21:39:06 +00:00
// for the very special query for the history menu
// we generate a super-optimized SQL query
2007-12-06 07:38:52 +00:00
if ( IsHistoryMenuQuery ( aQueries , aOptions ,
nsINavHistoryQueryOptions : : SORT_BY_DATE_DESCENDING ) ) {
2007-08-02 17:34:55 +00:00
// visit_type <> 4 == TRANSITION_EMBED
// visit_type <> 0 == undefined (see bug #375777 for details)
2007-07-26 21:39:06 +00:00
queryString = NS_LITERAL_CSTRING (
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, "
2008-01-05 05:16:27 +00:00
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " h.id " )
" , f.url, null, null "
2007-07-26 21:39:06 +00:00
" FROM moz_places h "
2008-01-05 05:16:27 +00:00
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id "
" WHERE h.id IN ( "
" SELECT DISTINCT p.id "
" FROM moz_places p "
" JOIN moz_historyvisits ON place_id = p.id "
" WHERE hidden <> 1 AND visit_type NOT IN(0,4) "
" ORDER BY visit_date DESC "
" LIMIT " ) ;
2007-09-29 01:04:31 +00:00
queryString . AppendInt ( aOptions - > MaxResults ( ) ) ;
2008-01-05 05:16:27 +00:00
queryString + = NS_LITERAL_CSTRING ( " ) ORDER BY 6 DESC " ) ; // v.visit_date
2007-07-26 21:39:06 +00:00
return NS_OK ;
}
2007-12-06 07:38:52 +00:00
// for the most visited menu query
// we generate a super-optimized SQL query
if ( IsHistoryMenuQuery ( aQueries , aOptions ,
nsINavHistoryQueryOptions : : SORT_BY_VISITCOUNT_DESCENDING ) ) {
queryString = NS_LITERAL_CSTRING (
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, "
2008-01-05 05:16:27 +00:00
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " h.id " )
" , f.url, null, null "
2007-12-06 07:38:52 +00:00
" FROM moz_places h "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id WHERE "
" h.id IN (SELECT id FROM moz_places WHERE hidden <> 1 "
" ORDER BY visit_count DESC LIMIT " ) ;
queryString . AppendInt ( aOptions - > MaxResults ( ) ) ;
queryString + = NS_LITERAL_CSTRING ( " ) ORDER BY h.visit_count DESC " ) ;
return NS_OK ;
}
2006-07-18 16:19:04 +00:00
PRBool asVisits =
( aOptions - > ResultType ( ) = = nsINavHistoryQueryOptions : : RESULTS_AS_VISIT | |
aOptions - > ResultType ( ) = = nsINavHistoryQueryOptions : : RESULTS_AS_FULL_VISIT ) ;
2006-07-18 15:56:56 +00:00
2007-04-25 21:03:29 +00:00
nsCAutoString commonConditions ;
2007-02-16 22:55:10 +00:00
2007-04-25 21:03:29 +00:00
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS ) {
// only look at bookmarks nodes
commonConditions . AssignLiteral ( " b.type = 1 " ) ;
} else if ( ! aOptions - > IncludeHidden ( ) ) {
2006-07-18 16:34:38 +00:00
// The hiding code here must match the notification behavior in AddVisit
// Some items are unhidden but are subframe navigations that we shouldn't
// show. This happens especially on imported profiles because the previous
// history system didn't hide as many things as we do now. Some sites,
// especially Javascript-heavy ones, load things in frames to display them,
// resulting in a lot of these entries. This filters those visits out.
2007-08-02 17:34:55 +00:00
// 4 == TRANSITION_EMBED
// 0 == undefined (see bug #375777 for details)
commonConditions . AssignLiteral (
2008-01-05 05:16:27 +00:00
" h.hidden <> 1 AND v.visit_type NOT IN(0,4) " ) ;
2006-07-18 16:34:38 +00:00
}
2006-07-18 15:56:56 +00:00
// Query string: Output parameters should be in order of kGetInfoIndex_*
2006-07-18 16:10:01 +00:00
// WATCH OUT: nsNavBookmarks::Init also creates some statements that share
// these same indices for passing to RowToResult. If you add something to
// this, you also need to update the bookmark statements to keep them in
// sync!
2007-07-26 21:39:06 +00:00
2006-07-18 15:56:56 +00:00
nsCAutoString groupBy ;
2006-07-18 15:59:28 +00:00
if ( asVisits ) {
2006-07-18 15:56:56 +00:00
// if we want visits, this is easy, just combine all possible matches
// between the history and visits table and do our query.
2006-07-18 16:19:04 +00:00
// FIXME(brettw) Add full visit info
2006-07-18 16:10:01 +00:00
queryString = NS_LITERAL_CSTRING (
2007-04-25 21:03:29 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, "
" v.visit_date, f.url, v.session, null "
2006-12-08 19:57:12 +00:00
" FROM moz_places h "
" JOIN moz_historyvisits v ON h.id = v.place_id "
2007-04-25 21:03:29 +00:00
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id " ) ;
2006-07-18 15:56:56 +00:00
} else {
2006-07-18 16:12:09 +00:00
// For URLs, it is more complicated, because we want each URL once. The
// GROUP BY clause gives us this. To get the max visit time, we populate
2006-07-18 16:17:02 +00:00
// one column by using a nested SELECT on the visit table. Also, ignore
// session information.
2006-07-18 16:19:04 +00:00
// FIXME(brettw) add nulls for full visit info
2007-04-25 21:03:29 +00:00
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_HISTORY ) {
queryString = NS_LITERAL_CSTRING (
2007-07-26 21:39:06 +00:00
" SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, MAX(visit_date), "
" f.url, null, null "
2007-04-25 21:03:29 +00:00
" FROM moz_places h "
" LEFT OUTER JOIN moz_historyvisits v ON h.id = v.place_id "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id " ) ;
groupBy = NS_LITERAL_CSTRING ( " GROUP BY h.id " ) ;
} else if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS ) {
2007-11-20 02:01:53 +00:00
queryString = NS_LITERAL_CSTRING ( " SELECT b.fk, h.url, COALESCE(b.title, h.title), " ) ;
2007-10-11 06:42:38 +00:00
queryString + = NS_LITERAL_CSTRING (
" h.rev_host, h.visit_count, "
2008-01-05 05:16:27 +00:00
SQL_STR_FRAGMENT_MAX_VISIT_DATE ( " b.fk " )
" , f.url, null, b.id, b.dateAdded, b.lastModified "
2007-10-11 06:42:38 +00:00
" FROM moz_bookmarks b "
" JOIN moz_places h ON b.fk = h.id "
" LEFT OUTER JOIN moz_historyvisits v ON b.fk = v.place_id "
" LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id " ) ;
2007-04-25 21:03:29 +00:00
groupBy = NS_LITERAL_CSTRING ( " GROUP BY b.id " ) ;
2007-10-01 17:36:18 +00:00
} else {
2007-06-19 16:25:11 +00:00
// XXX: implement support for nsINavHistoryQueryOptions::QUERY_TYPE_UNIFIED
2007-04-25 21:03:29 +00:00
return NS_ERROR_NOT_IMPLEMENTED ;
}
2006-07-18 15:56:56 +00:00
}
PRInt32 numParameters = 0 ;
nsCAutoString conditions ;
2006-07-18 16:19:04 +00:00
PRInt32 i ;
for ( i = 0 ; i < aQueries . Count ( ) ; i + + ) {
2006-07-18 15:56:56 +00:00
nsCString queryClause ;
2006-07-18 15:59:03 +00:00
PRInt32 clauseParameters = 0 ;
2007-07-26 21:39:06 +00:00
nsresult rv = QueryToSelectClause ( aQueries [ i ] , aOptions , numParameters ,
2006-07-18 16:19:29 +00:00
& queryClause , & clauseParameters ,
2007-04-25 21:03:29 +00:00
commonConditions ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! queryClause . IsEmpty ( ) ) {
if ( ! conditions . IsEmpty ( ) ) // exists previous clause: multiple ones are ORed
conditions + = NS_LITERAL_CSTRING ( " OR " ) ;
conditions + = NS_LITERAL_CSTRING ( " ( " ) + queryClause +
NS_LITERAL_CSTRING ( " ) " ) ;
numParameters + = clauseParameters ;
}
}
2006-07-18 16:19:29 +00:00
// in cases where there were no queries, we need to use the common conditions
// (normally these are appended to each clause that are not annotation-based)
2007-04-25 21:03:29 +00:00
if ( ! conditions . IsEmpty ( ) ) {
queryString + = " WHERE " ;
2006-07-18 16:19:29 +00:00
queryString + = conditions ;
2007-04-25 21:03:29 +00:00
} else if ( ! commonConditions . IsEmpty ( ) ) {
queryString + = " WHERE " ;
queryString + = commonConditions ;
2006-07-18 15:56:56 +00:00
}
queryString + = groupBy ;
// Sort clause: we will sort later, but if it comes out of the DB sorted,
// our later sort will be basically free. The DB can sort these for free
// most of the time anyway, because it has indices over these items.
2006-07-18 16:19:04 +00:00
//
// FIXME: do some performance tests, I'm not sure that the indices are getting
// used, in which case we should just remove this except when there are max
// results.
2006-07-18 15:59:28 +00:00
switch ( sortingMode ) {
case nsINavHistoryQueryOptions : : SORT_BY_NONE :
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_TITLE_ASCENDING :
case nsINavHistoryQueryOptions : : SORT_BY_TITLE_DESCENDING :
2006-07-18 15:56:56 +00:00
// the DB doesn't have indices on titles, and we need to do special
// sorting for locales. This type of sorting is done only at the end.
2006-07-18 16:14:36 +00:00
//
// If the user wants few results, we limit them by date, necessitating
// a sort by date here (see the IDL definition for maxResults). We'll
// still do the official sort by title later.
2006-07-18 16:19:04 +00:00
if ( aOptions - > MaxResults ( ) > 0 )
2007-05-19 00:40:55 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 6 DESC " ) ; // v.visit_date
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_DATE_ASCENDING :
2007-05-19 00:40:55 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 6 ASC " ) ; // v.visit_date
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_DATE_DESCENDING :
2007-05-19 00:40:55 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 6 DESC " ) ; // v.visit_date
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 16:19:04 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_URI_ASCENDING :
2007-04-25 21:03:29 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 2 ASC " ) ; // h.url
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 16:19:04 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_URI_DESCENDING :
2007-04-25 21:03:29 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 2 DESC " ) ; // h.url
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_VISITCOUNT_ASCENDING :
2007-04-25 21:03:29 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 5 ASC " ) ; // h.visit_count
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_VISITCOUNT_DESCENDING :
2007-04-25 21:03:29 +00:00
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 5 DESC " ) ; // h.visit_count
2006-07-18 15:56:56 +00:00
break ;
2007-07-17 01:13:50 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_DATEADDED_ASCENDING :
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS )
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 10 ASC " ) ; // dateAdded
break ;
case nsINavHistoryQueryOptions : : SORT_BY_DATEADDED_DESCENDING :
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS )
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 10 DESC " ) ; // dateAdded
break ;
case nsINavHistoryQueryOptions : : SORT_BY_LASTMODIFIED_ASCENDING :
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS )
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 11 ASC " ) ; // b.lastModified
break ;
case nsINavHistoryQueryOptions : : SORT_BY_LASTMODIFIED_DESCENDING :
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS )
queryString + = NS_LITERAL_CSTRING ( " ORDER BY 11 DESC " ) ; // b.lastModified
break ;
2007-10-11 06:42:38 +00:00
case nsINavHistoryQueryOptions : : SORT_BY_COUNT_ASCENDING :
case nsINavHistoryQueryOptions : : SORT_BY_COUNT_DESCENDING :
// "count" of the items in a folder is not something we have in the database
// sorting is done at nsNavHistoryQueryResultNode::FillChildren
if ( aOptions - > QueryType ( ) = = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS )
break ;
2006-07-18 15:56:56 +00:00
default :
NS_NOTREACHED ( " Invalid sorting mode " ) ;
}
2006-07-18 16:14:36 +00:00
2007-10-01 17:36:18 +00:00
// determine whether we can push maxResults constraints
// into the queries as LIMIT, or if we need to do result count clamping later
// using FilterResultSet()
if ( ! NeedToFilterResultSet ( aQueries , aOptions ) & & aOptions - > MaxResults ( ) > 0 ) {
2006-07-18 16:14:36 +00:00
queryString + = NS_LITERAL_CSTRING ( " LIMIT " ) ;
2006-07-18 16:19:04 +00:00
queryString . AppendInt ( aOptions - > MaxResults ( ) ) ;
2006-07-18 16:14:36 +00:00
queryString . AppendLiteral ( " " ) ;
}
2007-07-26 21:39:06 +00:00
return NS_OK ;
}
// nsNavHistory::GetQueryResults
//
// Call this to get the results from a complex query. This is used by
// nsNavHistoryQueryResultNode to populate its children. For simple bookmark
// queries, use nsNavBookmarks::QueryFolderChildren.
//
// THIS DOES NOT DO SORTING. You will need to sort the container yourself
// when you get the results. This is because sorting depends on tree
// statistics that will be built from the perspective of the tree. See
// nsNavHistoryQueryResultNode::FillChildren
//
// FIXME: This only does keyword searching for the first query, and does
// it ANDed with the all the rest of the queries.
nsresult
nsNavHistory : : GetQueryResults ( nsNavHistoryQueryResultNode * aResultNode ,
const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsNavHistoryQueryOptions * aOptions ,
nsCOMArray < nsNavHistoryResultNode > * aResults )
{
NS_ENSURE_ARG_POINTER ( aOptions ) ;
NS_ASSERTION ( aResults - > Count ( ) = = 0 , " Initial result array must be empty " ) ;
if ( ! aQueries . Count ( ) )
return NS_ERROR_INVALID_ARG ;
nsCString queryString ;
nsresult rv = ConstructQueryString ( aQueries , aOptions , queryString ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-02-16 22:55:10 +00:00
# ifdef DEBUG_thunder
printf ( " Constructed the query: %s \n " , PromiseFlatCString ( queryString ) . get ( ) ) ;
# endif
2006-07-18 15:56:56 +00:00
// Put this in a transaction. Even though we are only reading, this will
// speed up the grouped queries to the annotation service for titles and
// full text searching.
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
// create statement
nsCOMPtr < mozIStorageStatement > statement ;
rv = mDBConn - > CreateStatement ( queryString , getter_AddRefs ( statement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// bind parameters
2007-07-26 21:39:06 +00:00
PRInt32 numParameters = 0 ;
2007-09-29 01:04:31 +00:00
PRInt32 i ;
2007-10-01 17:36:18 +00:00
for ( i = 0 ; i < aQueries . Count ( ) ; i + + ) {
2007-09-29 01:04:31 +00:00
PRInt32 clauseParameters = 0 ;
rv = BindQueryClauseParameters ( statement , numParameters ,
aQueries [ i ] , aOptions , & clauseParameters ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
numParameters + = clauseParameters ;
}
2007-10-01 17:36:18 +00:00
// optimize the case where we just use the results as is
// and we don't need to do any post-query filtering
if ( NeedToFilterResultSet ( aQueries , aOptions ) ) {
2006-07-18 15:56:56 +00:00
// generate the toplevel results
nsCOMArray < nsNavHistoryResultNode > toplevel ;
2006-07-18 16:19:04 +00:00
rv = ResultsAsList ( statement , aOptions , & toplevel ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-10-01 17:36:18 +00:00
PRUint32 groupCount ;
const PRUint16 * groupings = aOptions - > GroupingMode ( & groupCount ) ;
2007-09-15 20:07:02 +00:00
if ( groupCount = = 0 ) {
2007-10-01 17:36:18 +00:00
FilterResultSet ( aResultNode , toplevel , aResults , aQueries , aOptions ) ;
2006-07-18 16:20:08 +00:00
} else {
2007-09-15 20:07:02 +00:00
nsCOMArray < nsNavHistoryResultNode > filteredResults ;
2007-10-01 17:36:18 +00:00
FilterResultSet ( aResultNode , toplevel , & filteredResults , aQueries , aOptions ) ;
2007-09-15 20:07:02 +00:00
rv = RecursiveGroup ( aResultNode , filteredResults , groupings , groupCount ,
aResults ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-10-01 17:36:18 +00:00
} else {
rv = ResultsAsList ( statement , aOptions , aResults ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::AddObserver
NS_IMETHODIMP
2006-07-18 16:08:23 +00:00
nsNavHistory : : AddObserver ( nsINavHistoryObserver * aObserver , PRBool aOwnsWeak )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:08:23 +00:00
return mObservers . AppendWeakElement ( aObserver , aOwnsWeak ) ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::RemoveObserver
NS_IMETHODIMP
nsNavHistory : : RemoveObserver ( nsINavHistoryObserver * aObserver )
{
2006-07-18 16:08:23 +00:00
return mObservers . RemoveWeakElement ( aObserver ) ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::BeginUpdateBatch
2007-08-14 14:20:42 +00:00
// See RunInBatchMode, mLock _must_ be set when batching
2007-06-01 00:44:43 +00:00
nsresult
2006-07-18 15:56:56 +00:00
nsNavHistory : : BeginUpdateBatch ( )
{
2007-08-14 14:20:42 +00:00
if ( mBatchLevel + + = = 0 ) {
PRBool transactionInProgress = PR_TRUE ; // default to no transaction on err
mDBConn - > GetTransactionInProgress ( & transactionInProgress ) ;
mBatchHasTransaction = ! transactionInProgress ;
if ( mBatchHasTransaction )
mDBConn - > BeginTransaction ( ) ;
2006-07-18 16:08:48 +00:00
ENUMERATE_WEAKARRAY ( mObservers , nsINavHistoryObserver ,
OnBeginUpdateBatch ( ) )
2006-07-18 15:56:56 +00:00
}
2007-08-14 14:20:42 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::EndUpdateBatch
2007-06-01 00:44:43 +00:00
nsresult
2006-07-18 15:56:56 +00:00
nsNavHistory : : EndUpdateBatch ( )
{
2007-08-14 14:20:42 +00:00
if ( - - mBatchLevel = = 0 ) {
if ( mBatchHasTransaction )
mDBConn - > CommitTransaction ( ) ;
mBatchHasTransaction = PR_FALSE ;
2006-07-18 16:08:23 +00:00
ENUMERATE_WEAKARRAY ( mObservers , nsINavHistoryObserver , OnEndUpdateBatch ( ) )
2006-07-18 15:56:56 +00:00
}
return NS_OK ;
}
2007-06-01 00:44:43 +00:00
NS_IMETHODIMP
nsNavHistory : : RunInBatchMode ( nsINavHistoryBatchCallback * aCallback ,
2007-08-14 14:20:42 +00:00
nsISupports * aUserData )
{
NS_ENSURE_STATE ( mLock ) ;
2007-06-01 00:44:43 +00:00
NS_ENSURE_ARG_POINTER ( aCallback ) ;
2007-08-14 14:20:42 +00:00
nsAutoLock lock ( mLock ) ;
2007-06-01 00:44:43 +00:00
UpdateBatchScoper batch ( * this ) ;
nsresult rv = aCallback - > RunBatched ( aUserData ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
2007-01-05 19:58:56 +00:00
NS_IMETHODIMP
nsNavHistory : : GetHistoryDisabled ( PRBool * _retval )
{
NS_ENSURE_ARG_POINTER ( _retval ) ;
* _retval = IsHistoryDisabled ( ) ;
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
// Browser history *************************************************************
// nsNavHistory::AddPageWithDetails
//
// This function is used by the migration components to import history.
//
// Note that this always adds the page with one visit and no parent, which
// is appropriate for imported URIs.
NS_IMETHODIMP
nsNavHistory : : AddPageWithDetails ( nsIURI * aURI , const PRUnichar * aTitle ,
PRInt64 aLastVisited )
{
2006-07-18 16:23:51 +00:00
PRInt64 visitID ;
nsresult rv = AddVisit ( aURI , aLastVisited , 0 , TRANSITION_LINK , PR_FALSE ,
0 , & visitID ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:23:51 +00:00
2007-07-26 16:23:11 +00:00
return SetPageTitleInternal ( aURI , nsString ( aTitle ) ) ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::GetLastPageVisited
2006-07-18 16:28:49 +00:00
//
// This was once used when the new window is set to "previous page." It
// doesn't seem to be used anymore, so we don't spend any time precompiling
// the statement.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : GetLastPageVisited ( nsACString & aLastPageVisited )
{
2006-07-18 16:28:49 +00:00
nsCOMPtr < mozIStorageStatement > statement ;
nsresult rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" SELECT h.url "
2006-12-08 19:57:12 +00:00
" FROM moz_places h LEFT OUTER JOIN moz_historyvisits v ON h.id = v.place_id "
2006-07-18 16:28:49 +00:00
" WHERE v.visit_date IN "
" (SELECT MAX(visit_date) "
2006-12-08 19:57:12 +00:00
" FROM moz_historyvisits v2 LEFT JOIN moz_places h2 ON v2.place_id = h2.id "
2006-07-18 16:28:49 +00:00
" WHERE h2.hidden != 1) " ) ,
getter_AddRefs ( statement ) ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:28:49 +00:00
PRBool hasMatch = PR_FALSE ;
if ( NS_SUCCEEDED ( statement - > ExecuteStep ( & hasMatch ) ) & & hasMatch ) {
return statement - > GetUTF8String ( 0 , aLastPageVisited ) ;
}
aLastPageVisited . Truncate ( 0 ) ;
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::GetCount
//
2006-07-18 16:20:57 +00:00
// This function is used in legacy code to see if there is any history to
// clear. Counting the actual number of history entries is very slow, so
// we just see if there are any and return 0 or 1, which is enough to make
// all the code that uses this function happy.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : GetCount ( PRUint32 * aCount )
{
2006-07-18 16:20:57 +00:00
PRBool hasEntries = PR_FALSE ;
nsresult rv = GetHasHistoryEntries ( & hasEntries ) ;
if ( hasEntries )
* aCount = 1 ;
2006-07-18 15:56:56 +00:00
else
2006-07-18 16:20:57 +00:00
* aCount = 0 ;
return rv ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::RemovePage
//
// Removes all visits and the main history entry for the given URI.
// Silently fails if we have no knowledge of the page.
NS_IMETHODIMP
nsNavHistory : : RemovePage ( nsIURI * aURI )
{
2007-07-26 16:23:11 +00:00
PRInt64 placeId ;
nsresult rv = GetUrlIdFor ( aURI , & placeId , PR_TRUE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
nsCOMPtr < mozIStorageStatement > statement ;
2006-07-18 16:10:55 +00:00
// delete all visits for this page
2006-07-18 15:56:56 +00:00
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-07-26 16:23:11 +00:00
" DELETE FROM moz_historyvisits WHERE place_id = ?1 " ) ,
2006-07-18 15:56:56 +00:00
getter_AddRefs ( statement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
rv = statement - > BindInt64Parameter ( 0 , placeId ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
// now that visits have been removed, run annotation expiration.
// this will remove all expire-able annotations for this URI.
( void ) mExpire . OnDeleteURI ( ) ;
// does the uri have un-expirable annotations?
nsAnnotationService * annosvc = nsAnnotationService : : GetAnnotationService ( ) ;
NS_ENSURE_STATE ( annosvc ) ;
nsTArray < nsCString > annoNames ;
rv = annosvc - > GetAnnotationNamesTArray ( placeId , & annoNames , PR_FALSE ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// is the uri bookmarked?
2006-07-18 16:10:55 +00:00
nsNavBookmarks * bookmarksService = nsNavBookmarks : : GetBookmarksService ( ) ;
2007-07-26 16:23:11 +00:00
NS_ENSURE_STATE ( bookmarksService ) ;
2006-07-18 16:10:55 +00:00
PRBool bookmarked = PR_FALSE ;
rv = bookmarksService - > IsBookmarked ( aURI , & bookmarked ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
// if there are no more annotations, and the entry is not bookmarked
// then we can remove the moz_places entry.
if ( annoNames . Length ( ) = = 0 & & ! bookmarked ) {
// Note that we do NOT delete favicons. Any unreferenced favicons will be
// deleted next time the browser is shut down.
2006-07-18 16:10:55 +00:00
// delete main history entries
2006-07-18 16:11:19 +00:00
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
2007-07-26 16:23:11 +00:00
" DELETE FROM moz_places WHERE id = ?1 " ) ,
2006-07-18 16:11:19 +00:00
getter_AddRefs ( statement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
rv = statement - > BindInt64Parameter ( 0 , placeId ) ;
2006-07-18 16:11:19 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:59:28 +00:00
}
2008-01-25 17:11:14 +00:00
else {
// because the moz_place was annotated (or it was a bookmark),
// we didn't delete it, but we did delete the moz_visits
// so we need to reset the frecency. Note, we don't
// reset the visit_count, as we use that in our "on idle"
// query to figure out which places to recalcuate frecency first.
rv = mDBConn - > CreateStatement ( NS_LITERAL_CSTRING (
" UPDATE moz_places SET frecency = -1 WHERE id = ?1 " ) ,
getter_AddRefs ( statement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > BindInt64Parameter ( 0 , placeId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// placeId could have a livemark item, so setting the frecency to -1
// would cause it to show up in the url bar autocomplete
// call FixInvalidFrecenciesForExcludedPlaces() to handle that scenario
// XXX this might be dog slow, further degrading delete perf.
rv = FixInvalidFrecenciesForExcludedPlaces ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// XXX todo
// forcibly call the "on idle" timer here to do a little work
// but the rest will happen on idle.
}
2006-07-18 15:56:56 +00:00
// Observers: Be sure to finish transaction before calling observers. Note also
// that we always call the observers even though we aren't sure something
// actually got deleted.
transaction . Commit ( ) ;
2006-07-18 16:08:23 +00:00
ENUMERATE_WEAKARRAY ( mObservers , nsINavHistoryObserver , OnDeleteURI ( aURI ) )
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::RemovePagesFromHost
//
// This function will delete all history information about pages from a
// given host. If aEntireDomain is set, we will also delete pages from
// sub hosts (so if we are passed in "microsoft.com" we delete
// "www.microsoft.com", "msdn.microsoft.com", etc.). An empty host name
2006-07-18 16:10:55 +00:00
// means local files and anything else with no host name. You can also pass
// in the localized "(local files)" title given to you from a history query.
2006-07-18 15:56:56 +00:00
//
2007-07-26 16:23:11 +00:00
// Silently fails if we have no knowledge of the host.
2006-07-18 15:56:56 +00:00
//
// This function is actually pretty simple, it just boils down to a DELETE
2007-07-26 16:23:11 +00:00
// statement, but is made complex due to the observers and the two types of
// similar delete operations that we need to support.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : RemovePagesFromHost ( const nsACString & aHost , PRBool aEntireDomain )
{
nsresult rv ;
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
// Local files don't have any host name. We don't want to delete all files in
// history when we get passed an empty string, so force to exact match
if ( aHost . IsEmpty ( ) )
aEntireDomain = PR_FALSE ;
// translate "(local files)" to an empty host name
// be sure to use the TitleForDomain to get the localized name
2006-07-18 16:19:04 +00:00
nsCString localFiles ;
TitleForDomain ( EmptyCString ( ) , localFiles ) ;
nsAutoString host16 ;
2007-07-26 16:23:11 +00:00
if ( ! aHost . Equals ( localFiles ) )
2006-07-18 16:19:04 +00:00
host16 = NS_ConvertUTF8toUTF16 ( aHost ) ;
2006-07-18 15:56:56 +00:00
// nsISupports version of the host string for passing to observers
nsCOMPtr < nsISupportsString > hostSupports ( do_CreateInstance ( NS_SUPPORTS_STRING_CONTRACTID , & rv ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = hostSupports - > SetData ( host16 ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// see BindQueryClauseParameters for how this host selection works
nsAutoString revHostDot ;
GetReversedHostname ( host16 , revHostDot ) ;
NS_ASSERTION ( revHostDot [ revHostDot . Length ( ) - 1 ] = = ' . ' , " Invalid rev. host " ) ;
nsAutoString revHostSlash ( revHostDot ) ;
revHostSlash . Truncate ( revHostSlash . Length ( ) - 1 ) ;
revHostSlash . Append ( NS_LITERAL_STRING ( " / " ) ) ;
2006-07-18 16:10:55 +00:00
// how we are selecting host names
nsCAutoString conditionString ;
if ( aEntireDomain )
2007-07-26 16:23:11 +00:00
conditionString . AssignLiteral ( " h.rev_host >= ?1 AND h.rev_host < ?2 " ) ;
2006-07-18 16:10:55 +00:00
else
2007-07-26 16:23:11 +00:00
conditionString . AssignLiteral ( " h.rev_host = ?1 " ) ;
2006-07-18 16:10:55 +00:00
// Tell the observers about the non-hidden items we are about to delete.
// Since this is a two-step process, if we get an error, we may tell them we
// will delete something but then not actually delete it. Too bad.
//
// Note also that we *include* bookmarked items here. We generally want to
// send out delete notifications for bookmarked items since in general,
// deleting the visits (like we always do) will cause the item to disappear
2006-07-18 16:14:36 +00:00
// from history views. This will also cause all visit dates to be deleted,
// which affects many bookmark views
2007-07-26 16:23:11 +00:00
// is bookmarked?
conditionString . AppendLiteral ( " AND (b.type = ?3 OR b.id IS NULL) " ) ;
// has EXPIRES_NEVER annotations?
conditionString . AppendLiteral ( " AND (a.expiration = ?4 OR a.id IS NULL) " ) ;
2006-07-18 16:14:36 +00:00
// create statement depending on delete type
2007-07-26 16:23:11 +00:00
nsCAutoString getURIsForDeletion = NS_LITERAL_CSTRING (
" SELECT h.id, h.url, b.id, a.id FROM moz_places h "
" LEFT OUTER JOIN moz_bookmarks b ON h.id = b.fk "
" LEFT OUTER JOIN moz_annos a ON h.id = a.place_id WHERE " ) +
conditionString ;
nsCOMPtr < mozIStorageStatement > statement ;
rv = mDBConn - > CreateStatement ( getURIsForDeletion , getter_AddRefs ( statement ) ) ;
2006-07-18 16:14:36 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > BindStringParameter ( 0 , revHostDot ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( aEntireDomain ) {
rv = statement - > BindStringParameter ( 1 , revHostSlash ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:14:36 +00:00
}
2007-07-26 16:23:11 +00:00
rv = statement - > BindInt32Parameter ( 2 , nsNavBookmarks : : TYPE_BOOKMARK ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = statement - > BindInt32Parameter ( 3 , nsIAnnotationService : : EXPIRE_NEVER ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCAutoString deletedPlaceIds ;
nsCAutoString deletedPlaceIdsBookmarked ;
nsCAutoString deletedPlaceIdsWithAnno ;
nsCStringArray deletedURIs ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:36 +00:00
PRBool hasMore = PR_FALSE ;
while ( ( statement - > ExecuteStep ( & hasMore ) = = NS_OK ) & & hasMore ) {
nsCAutoString thisURIString ;
2007-07-26 16:23:11 +00:00
if ( NS_FAILED ( statement - > GetUTF8String ( 1 , thisURIString ) ) | |
2006-07-18 16:14:36 +00:00
thisURIString . IsEmpty ( ) )
continue ; // no URI
2007-07-26 16:23:11 +00:00
if ( ! deletedURIs . AppendCString ( thisURIString ) )
2006-07-18 16:14:36 +00:00
return NS_ERROR_OUT_OF_MEMORY ;
2006-07-18 15:56:56 +00:00
2007-07-26 16:23:11 +00:00
if ( ! deletedPlaceIds . IsEmpty ( ) )
deletedPlaceIds . AppendLiteral ( " , " ) ;
PRInt64 placeId ;
rv = statement - > GetInt64 ( 0 , & placeId ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
deletedPlaceIds . AppendInt ( placeId ) ;
if ( statement - > AsInt64 ( 2 ) ) {
if ( ! deletedPlaceIdsBookmarked . IsEmpty ( ) )
deletedPlaceIdsBookmarked . AppendLiteral ( " , " ) ;
deletedPlaceIdsBookmarked . AppendInt ( placeId ) ;
}
if ( statement - > AsInt64 ( 3 ) ) {
if ( ! deletedPlaceIdsWithAnno . IsEmpty ( ) )
deletedPlaceIdsWithAnno . AppendLiteral ( " , " ) ;
deletedPlaceIdsWithAnno . AppendInt ( placeId ) ;
}
2006-07-18 16:10:55 +00:00
}
2006-07-18 15:56:56 +00:00
2007-07-26 16:23:11 +00:00
// first, delete all the visits
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" DELETE FROM moz_historyvisits WHERE place_id IN ( " ) +
deletedPlaceIds + NS_LITERAL_CSTRING ( " ) " ) ) ;
2006-07-18 16:10:55 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
// delete annotations (except EXPIRE_NEVER)
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" DELETE FROM moz_annos WHERE place_id NOT IN ( " ) +
2007-07-31 03:30:52 +00:00
deletedPlaceIdsWithAnno + NS_LITERAL_CSTRING ( " ) " ) ) ;
2007-04-04 17:21:03 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-07-26 16:23:11 +00:00
// finally, delete the actual moz_places records that are
// - not bookmarked
// - do not have EXPIRE_NEVER annotations
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" DELETE FROM moz_places WHERE id IN ( " ) + deletedPlaceIds +
NS_LITERAL_CSTRING ( " ) AND id NOT IN ( " ) + deletedPlaceIdsBookmarked +
NS_LITERAL_CSTRING ( " ) AND id NOT IN ( " ) + deletedPlaceIdsWithAnno +
NS_LITERAL_CSTRING ( " ) " ) ) ;
2006-07-18 16:10:55 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2008-01-25 17:11:14 +00:00
// reset the frecency for the places we did not delete. Note, we don't
// reset the visit_count, as we use that in our "on idle"
// query to figure out which places to recalcuate frecency first.
if ( ! deletedPlaceIdsBookmarked . IsEmpty ( ) | |
! deletedPlaceIdsWithAnno . IsEmpty ( ) ) {
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING (
" UPDATE moz_places SET frecency = -1 WHERE id IN ( " ) +
deletedPlaceIdsBookmarked +
NS_LITERAL_CSTRING ( " ) OR id IN ( " ) + deletedPlaceIdsWithAnno +
NS_LITERAL_CSTRING ( " ) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// the places could be livemark items, so setting the frecency to -1
// would cause them show up in the url bar autocomplete
// call FixInvalidFrecenciesForExcludedPlaces() to handle that scenario
rv = FixInvalidFrecenciesForExcludedPlaces ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// XXX todo
// forcibly call the "on idle" timer here to do a little work
// but the rest will happen on idle.
}
2006-07-18 15:56:56 +00:00
transaction . Commit ( ) ;
2006-07-18 16:10:55 +00:00
// notify observers
UpdateBatchScoper batch ( * this ) ; // sends Begin/EndUpdateBatch to obsrvrs.
if ( deletedURIs . Count ( ) ) {
nsCOMPtr < nsIURI > thisURI ;
for ( PRUint32 observerIndex = 0 ; observerIndex < mObservers . Length ( ) ;
observerIndex + + ) {
const nsCOMPtr < nsINavHistoryObserver > & obs = mObservers . ElementAt ( observerIndex ) ;
if ( ! obs )
continue ;
// send it all the URIs
for ( PRInt32 i = 0 ; i < deletedURIs . Count ( ) ; i + + ) {
if ( NS_FAILED ( NS_NewURI ( getter_AddRefs ( thisURI ) , * deletedURIs [ i ] ,
nsnull , nsnull ) ) )
continue ; // bad URI
obs - > OnDeleteURI ( thisURI ) ;
}
}
}
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::RemoveAllPages
//
2006-07-18 16:10:55 +00:00
// This function is used to clear history.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : RemoveAllPages ( )
{
2006-07-18 16:31:43 +00:00
// expire everything
2006-07-18 16:35:58 +00:00
mExpire . ClearHistory ( ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:35:58 +00:00
// Compress DB. Currently commented out because compression is very slow.
2007-06-23 16:11:33 +00:00
// Deleted data will be overwritten with 0s by sqlite.
2006-07-18 16:35:58 +00:00
#if 0
2006-07-18 16:31:43 +00:00
nsresult rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " VACUUM " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:35:58 +00:00
# endif
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::HidePage
//
2006-07-18 16:14:12 +00:00
// Sets the 'hidden' column to true. If we've not heard of the page, we
// succeed and do nothing.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : HidePage ( nsIURI * aURI )
{
2006-07-18 16:14:36 +00:00
return NS_ERROR_NOT_IMPLEMENTED ;
/*
2006-07-18 15:56:56 +00:00
// for speed to save disk accesses
mozStorageTransaction transaction ( mDBConn , PR_FALSE ,
mozIStorageConnection : : TRANSACTION_EXCLUSIVE ) ;
// We need to do a query anyway to see if this URL is already in the DB.
// Might as well ask for the hidden column to save updates in some cases.
nsCOMPtr < mozIStorageStatement > dbSelectStatement ;
nsresult rv = mDBConn - > CreateStatement (
2006-12-08 19:57:12 +00:00
NS_LITERAL_CSTRING ( " SELECT id,hidden FROM moz_places WHERE url = ?1 " ) ,
2006-07-18 15:56:56 +00:00
getter_AddRefs ( dbSelectStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:58:11 +00:00
rv = BindStatementURI ( dbSelectStatement , 0 , aURI ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool alreadyVisited = PR_TRUE ;
rv = dbSelectStatement - > ExecuteStep ( & alreadyVisited ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:14:12 +00:00
// don't need to do anything if we've never heard of this page
if ( ! alreadyVisited )
return NS_OK ;
// modify the existing page if necessary
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
PRInt32 oldHiddenState = 0 ;
rv = dbSelectStatement - > GetInt32 ( 1 , & oldHiddenState ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
if ( ! oldHiddenState )
return NS_OK ; // already marked as hidden, we're done
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
// find the old ID, which can be found faster than long URLs
PRInt32 entryid = 0 ;
rv = dbSelectStatement - > GetInt32 ( 0 , & entryid ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
// need to clear the old statement before we create a new one
dbSelectStatement = nsnull ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
nsCOMPtr < mozIStorageStatement > dbModStatement ;
rv = mDBConn - > CreateStatement (
2006-12-08 19:57:12 +00:00
NS_LITERAL_CSTRING ( " UPDATE moz_places SET hidden = 1 WHERE id = ?1 " ) ,
2006-07-18 16:14:12 +00:00
getter_AddRefs ( dbModStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
dbModStatement - > BindInt32Parameter ( 0 , entryid ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
rv = dbModStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
// notify observers, finish transaction first
transaction . Commit ( ) ;
2006-07-18 16:08:23 +00:00
ENUMERATE_WEAKARRAY ( mObservers , nsINavHistoryObserver ,
OnPageChanged ( aURI ,
nsINavHistoryObserver : : ATTRIBUTE_HIDDEN ,
EmptyString ( ) ) )
2006-07-18 15:56:56 +00:00
return NS_OK ;
2006-07-18 16:14:36 +00:00
*/
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::MarkPageAsTyped
//
2007-12-28 01:53:59 +00:00
// We call MarkPageAsTyped() before visiting a URL in order to
// help determine the transition type of the visit.
// We keep track of the URL so that later, in AddVisitChain()
// we can use TRANSITION_TYPED as the transition.
// Note, AddVisitChain() is not called immediately when we are doing LAZY_ADDs
2006-07-18 16:14:12 +00:00
//
2007-12-28 01:53:59 +00:00
// @see MarkPageAsFollowedBookmark
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : MarkPageAsTyped ( nsIURI * aURI )
{
2006-07-18 16:36:48 +00:00
// don't add when history is disabled
if ( IsHistoryDisabled ( ) )
2006-07-18 16:32:58 +00:00
return NS_OK ;
2006-07-18 16:14:12 +00:00
nsCAutoString uriString ;
2008-01-25 17:11:14 +00:00
nsresult rv = aURI - > GetSpec ( uriString ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
// if URL is already in the typed queue, then we need to remove the old one
PRInt64 unusedEventTime ;
if ( mRecentTyped . Get ( uriString , & unusedEventTime ) )
mRecentTyped . Remove ( uriString ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
if ( mRecentTyped . Count ( ) > RECENT_EVENT_QUEUE_MAX_LENGTH )
ExpireNonrecentEvents ( & mRecentTyped ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:12 +00:00
mRecentTyped . Put ( uriString , GetNow ( ) ) ;
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsGlobalHistory2 ************************************************************
// nsNavHistory::AddURI
2006-07-18 16:14:12 +00:00
//
2006-07-18 16:23:51 +00:00
// This is the main method of adding history entries.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : AddURI ( nsIURI * aURI , PRBool aRedirect ,
PRBool aToplevel , nsIURI * aReferrer )
{
2006-07-18 16:36:48 +00:00
// don't add when history is disabled
if ( IsHistoryDisabled ( ) )
2006-07-18 16:32:58 +00:00
return NS_OK ;
2007-12-19 17:33:50 +00:00
// filter out any unwanted URIs
PRBool canAdd = PR_FALSE ;
nsresult rv = CanAddURI ( aURI , & canAdd ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! canAdd )
return NS_OK ;
2006-07-18 16:35:58 +00:00
PRTime now = PR_Now ( ) ;
2006-07-18 16:34:13 +00:00
# ifdef LAZY_ADD
LazyMessage message ;
2006-07-18 16:35:58 +00:00
rv = message . Init ( LazyMessage : : Type_AddURI , aURI ) ;
2006-07-18 16:34:13 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
message . isRedirect = aRedirect ;
message . isToplevel = aToplevel ;
if ( aReferrer ) {
rv = aReferrer - > Clone ( getter_AddRefs ( message . referrer ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2006-07-18 16:35:58 +00:00
message . time = now ;
rv = AddLazyMessage ( message ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:34:13 +00:00
# else
2006-07-18 16:35:58 +00:00
rv = AddURIInternal ( aURI , now , aRedirect , aToplevel , aReferrer ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:34:13 +00:00
# endif
2006-07-18 16:35:58 +00:00
mExpire . OnAddURI ( now ) ;
return NS_OK ;
2006-07-18 16:34:13 +00:00
}
// nsNavHistory::AddURIInternal
//
// This does the work of AddURI so it can be done lazily.
nsresult
nsNavHistory : : AddURIInternal ( nsIURI * aURI , PRTime aTime , PRBool aRedirect ,
PRBool aToplevel , nsIURI * aReferrer )
{
2006-07-18 16:23:51 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
2006-07-18 16:27:59 +00:00
PRInt64 redirectBookmark = 0 ;
2006-07-18 16:23:51 +00:00
PRInt64 visitID , sessionID ;
2006-07-18 16:34:13 +00:00
nsresult rv = AddVisitChain ( aURI , aTime , aToplevel , aRedirect , aReferrer ,
2006-07-18 16:27:59 +00:00
& visitID , & sessionID , & redirectBookmark ) ;
2006-07-18 16:23:51 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:27:59 +00:00
// The bookmark cache of redirects may be out-of-date with this addition, so
// we need to update it. The issue here is if they bookmark "mozilla.org" by
// typing it in without ever having visited "www.mozilla.org". They will then
// get redirected to the latter, and we need to add mozilla.org ->
// www.mozilla.org to the bookmark hashtable.
//
// AddVisitChain will put the spec of a bookmarked URI if it encounters one
// into bookmarkURI. If this is non-empty, we know that something has happened
// with a bookmark and we should probably go update it.
if ( redirectBookmark ) {
nsNavBookmarks * bookmarkService = nsNavBookmarks : : GetBookmarksService ( ) ;
if ( bookmarkService ) {
PRTime now = GetNow ( ) ;
bookmarkService - > AddBookmarkToHash ( redirectBookmark ,
now - BOOKMARK_REDIRECT_TIME_THRESHOLD ) ;
}
}
2006-07-18 16:28:49 +00:00
return transaction . Commit ( ) ;
2006-07-18 16:23:51 +00:00
}
// nsNavHistory::AddVisitChain
//
// This function is sits between AddURI (which is called when a page is
// visited) and AddVisit (which creates the DB entries) to figure out what
// we should add and what are the detailed parameters that should be used
// (like referring visit ID and typed/bookmarked state).
//
// This function walks up the referring chain and recursively calls itself,
// each time calling InternalAdd to create a new history entry. (When we
// get notified of redirects, we don't actually add any history entries, just
// save them in mRecentRedirects. This function will add all of them for a
// given destination page when that page is actually visited.)
// See GetRedirectFor for more information about how redirects work.
2006-07-18 16:27:59 +00:00
//
// aRedirectBookmark should be empty when this function is first called. If
// there are any redirects that are bookmarks the specs will be placed in
// this buffer. The caller can then determine if any bookmarked items were
// visited so it knows whether to update the bookmark service's redirect
// hashtable.
2006-07-18 16:14:12 +00:00
2006-07-18 16:23:51 +00:00
nsresult
2006-07-18 16:34:13 +00:00
nsNavHistory : : AddVisitChain ( nsIURI * aURI , PRTime aTime ,
PRBool aToplevel , PRBool aIsRedirect ,
2006-07-18 16:23:51 +00:00
nsIURI * aReferrer , PRInt64 * aVisitID ,
2006-07-18 16:27:59 +00:00
PRInt64 * aSessionID , PRInt64 * aRedirectBookmark )
2006-07-18 16:23:51 +00:00
{
2006-07-18 16:14:12 +00:00
PRUint32 transitionType = 0 ;
2006-07-18 16:23:51 +00:00
PRInt64 referringVisit = 0 ;
PRTime visitTime = 0 ;
nsCAutoString spec ;
nsresult rv = aURI - > GetSpec ( spec ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCAutoString redirectSource ;
if ( GetRedirectFor ( spec , redirectSource , & visitTime , & transitionType ) ) {
// this was a redirect: See GetRedirectFor for info on how this works
nsCOMPtr < nsIURI > redirectURI ;
rv = NS_NewURI ( getter_AddRefs ( redirectURI ) , redirectSource ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:27:59 +00:00
// remember if any redirect sources were bookmarked
nsNavBookmarks * bookmarkService = nsNavBookmarks : : GetBookmarksService ( ) ;
PRBool isBookmarked ;
if ( bookmarkService & &
NS_SUCCEEDED ( bookmarkService - > IsBookmarked ( redirectURI , & isBookmarked ) )
& & isBookmarked ) {
GetUrlIdFor ( redirectURI , aRedirectBookmark , PR_FALSE ) ;
}
2006-07-18 16:34:13 +00:00
// Find the visit for the source. Note that we decrease the time counter,
// which will ensure that the referrer and this page will appear in history
// in the correct order. Since the times are in microseconds, it should not
// normally be possible to get two pages within one microsecond of each
// other so the referrer won't appear before a previous page viewed.
rv = AddVisitChain ( redirectURI , aTime - 1 , aToplevel , PR_TRUE , aReferrer ,
2006-07-18 16:27:59 +00:00
& referringVisit , aSessionID , aRedirectBookmark ) ;
2006-07-18 16:23:51 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-05-25 02:52:24 +00:00
// for redirects in frames, we don't want to see those items in history
// see bug #381453 for more details
if ( ! aToplevel ) {
transitionType = nsINavHistoryService : : TRANSITION_EMBED ;
}
2006-07-18 16:23:51 +00:00
} else if ( aReferrer ) {
2006-07-18 16:14:12 +00:00
// If there is a referrer, we know you came from somewhere, either manually
// or automatically. For toplevel windows, assume its manual and you want
// to see this in history. For other things, it's some kind of embedded
// navigation. This is true of images and other content the user doesn't
// want to see in their history, but also of embedded frames that the user
// navigated manually and probably DOES want to see in history.
// Unfortunately, there isn't any easy way to distinguish these.
//
// Generally, it boils down to the problem of detecting whether a frame
// content change is the result of a user action, which isn't well defined
// since script could change a frame's source as a result of user request,
// or just because it feels like loading a new ad. The "back" button will
// undo either of these actions.
if ( aToplevel )
transitionType = nsINavHistoryService : : TRANSITION_LINK ;
else
transitionType = nsINavHistoryService : : TRANSITION_EMBED ;
2006-07-18 16:23:51 +00:00
// Note that here we should NOT use the GetNow function. That function
// caches the value of "now" until next time the event loop runs. This
// gives better performance, but here we may get many notifications without
// running the event loop. We must preserve these events' ordering. This
// most commonly happens on redirects.
visitTime = PR_Now ( ) ;
// try to turn the referrer into a visit
if ( ! FindLastVisit ( aReferrer , & referringVisit , aSessionID ) ) {
// we couldn't find a visit for the referrer, don't set it
* aSessionID = GetNewSessionID ( ) ;
}
2006-07-18 16:14:12 +00:00
} else {
// When there is no referrer, we know the user must have gotten the link
// from somewhere, so check our sources to see if it was recently typed or
// has a bookmark selected. We don't handle drag-and-drop operations.
2007-08-02 17:34:55 +00:00
// note: the link may have also come from a new window (set to load a homepage)
// or on start up (if we've set to load the home page or restore tabs)
// we treat these as TRANSITION_LINK (if they are top level) or
// TRANSITION_EMBED (if not top level). We don't want to to add visits to
// history without a transition type.
2006-07-18 16:23:51 +00:00
if ( CheckIsRecentEvent ( & mRecentTyped , spec ) )
transitionType = nsINavHistoryService : : TRANSITION_TYPED ;
else if ( CheckIsRecentEvent ( & mRecentBookmark , spec ) )
transitionType = nsINavHistoryService : : TRANSITION_BOOKMARK ;
2007-08-02 17:34:55 +00:00
else if ( aToplevel )
transitionType = nsINavHistoryService : : TRANSITION_LINK ;
else
transitionType = nsINavHistoryService : : TRANSITION_EMBED ;
2006-07-18 16:23:51 +00:00
visitTime = PR_Now ( ) ;
* aSessionID = GetNewSessionID ( ) ;
2006-07-18 16:14:12 +00:00
}
2006-07-18 16:23:51 +00:00
// this call will create the visit and create/update the page entry
2007-12-21 19:37:18 +00:00
return AddVisit ( aURI , visitTime , aReferrer , transitionType ,
2006-07-18 16:23:51 +00:00
aIsRedirect , * aSessionID , aVisitID ) ;
2006-07-18 15:56:56 +00:00
}
// nsNavHistory::IsVisited
//
// Note that this ignores the "hidden" flag. This function just checks if the
// given page is in the DB for link coloring. The "hidden" flag affects
// the history list view and autocomplete.
NS_IMETHODIMP
nsNavHistory : : IsVisited ( nsIURI * aURI , PRBool * _retval )
{
2006-07-18 16:36:48 +00:00
// if history is disabled, we can optimize
if ( IsHistoryDisabled ( ) ) {
2006-07-18 16:32:58 +00:00
* _retval = PR_FALSE ;
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
nsCAutoString utf8URISpec ;
nsresult rv = aURI - > GetSpec ( utf8URISpec ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
* _retval = IsURIStringVisited ( utf8URISpec ) ;
return NS_OK ;
}
// nsNavHistory::SetPageTitle
2006-07-18 16:14:36 +00:00
//
2007-07-26 16:23:11 +00:00
// This sets the page title.
2006-07-18 16:29:14 +00:00
//
// Note that we do not allow empty real titles and will silently ignore such
// requests. When a URL is added we give it a default title based on the
// URL. Most pages provide a title and it gets replaced to something better.
// Some pages don't: some say <title></title>, and some don't have any title
// element. In BOTH cases, we get SetPageTitle(URI, ""), but in both cases,
// our default title is more useful to the user than "(no title)".
//
// User titles will accept empty strings so the user can still manually
// override it.
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : SetPageTitle ( nsIURI * aURI ,
const nsAString & aTitle )
{
2006-07-18 16:29:14 +00:00
if ( aTitle . IsEmpty ( ) )
return NS_OK ;
2006-07-18 16:34:13 +00:00
# ifdef LAZY_ADD
LazyMessage message ;
nsresult rv = message . Init ( LazyMessage : : Type_Title , aURI ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
message . title = aTitle ;
return AddLazyMessage ( message ) ;
# else
2007-07-26 16:23:11 +00:00
return SetPageTitleInternal ( aURI , aTitle ) ;
2006-07-18 16:34:13 +00:00
# endif
2006-07-18 16:14:36 +00:00
}
2006-07-18 15:56:56 +00:00
2007-02-26 00:54:36 +00:00
NS_IMETHODIMP
nsNavHistory : : GetPageTitle ( nsIURI * aURI , nsAString & aTitle )
{
aTitle . Truncate ( 0 ) ;
mozIStorageStatement * statement = DBGetURLPageInfo ( ) ;
mozStorageStatementScoper scope ( statement ) ;
nsresult rv = BindStatementURI ( statement , 0 , aURI ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool results ;
rv = statement - > ExecuteStep ( & results ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! results ) {
aTitle . SetIsVoid ( PR_TRUE ) ;
return NS_OK ; // not found: return void string
}
return statement - > GetString ( nsNavHistory : : kGetInfoIndex_Title , aTitle ) ;
}
2006-07-18 15:56:56 +00:00
2006-07-18 16:18:39 +00:00
# ifndef MOZILLA_1_8_BRANCH
2006-07-18 16:14:36 +00:00
// nsNavHistory::GetURIGeckoFlags
//
// FIXME: should we try to use annotations for this stuff?
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : GetURIGeckoFlags ( nsIURI * aURI , PRUint32 * aResult )
{
return NS_ERROR_NOT_IMPLEMENTED ;
}
2006-07-18 16:14:36 +00:00
// nsNavHistory::SetURIGeckoFlags
//
// FIXME: should we try to use annotations for this stuff?
2006-07-18 15:56:56 +00:00
NS_IMETHODIMP
nsNavHistory : : SetURIGeckoFlags ( nsIURI * aURI , PRUint32 aFlags )
{
return NS_ERROR_NOT_IMPLEMENTED ;
}
2006-07-18 16:18:39 +00:00
# endif
2006-07-18 15:56:56 +00:00
2006-07-18 16:23:51 +00:00
// nsIGlobalHistory3 ***********************************************************
2006-07-18 16:29:39 +00:00
// nsNavHistory::AddDocumentRedirect
2006-07-18 16:23:51 +00:00
//
// This adds a redirect mapping from the destination of the redirect to the
// source, time, and type. This mapping is used by GetRedirectFor when we
// get a page added to reconstruct the redirects that happened when a page
// is visited. See GetRedirectFor for more information
// this is the expiration callback function that deletes stale entries
PLDHashOperator PR_CALLBACK nsNavHistory : : ExpireNonrecentRedirects (
nsCStringHashKey : : KeyType aKey , RedirectInfo & aData , void * aUserArg )
{
2007-07-08 07:08:04 +00:00
PRInt64 * threshold = reinterpret_cast < PRInt64 * > ( aUserArg ) ;
2006-07-18 16:23:51 +00:00
if ( aData . mTimeCreated < * threshold )
return PL_DHASH_REMOVE ;
return PL_DHASH_NEXT ;
}
2007-07-26 21:39:06 +00:00
2006-07-18 16:23:51 +00:00
NS_IMETHODIMP
2006-07-18 16:29:39 +00:00
nsNavHistory : : AddDocumentRedirect ( nsIChannel * aOldChannel ,
nsIChannel * aNewChannel ,
PRInt32 aFlags ,
PRBool aTopLevel )
2006-07-18 16:23:51 +00:00
{
nsresult rv ;
nsCOMPtr < nsIURI > oldURI , newURI ;
rv = aOldChannel - > GetURI ( getter_AddRefs ( oldURI ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = aNewChannel - > GetURI ( getter_AddRefs ( newURI ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCString oldSpec , newSpec ;
rv = oldURI - > GetSpec ( oldSpec ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = newURI - > GetSpec ( newSpec ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( mRecentRedirects . Count ( ) > RECENT_EVENT_QUEUE_MAX_LENGTH ) {
// expire out-of-date ones
PRInt64 threshold = PR_Now ( ) - RECENT_EVENT_THRESHOLD ;
mRecentRedirects . Enumerate ( ExpireNonrecentRedirects ,
2007-07-08 07:08:04 +00:00
reinterpret_cast < void * > ( & threshold ) ) ;
2006-07-18 16:23:51 +00:00
}
RedirectInfo info ;
// remove any old entries for this redirect destination
if ( mRecentRedirects . Get ( newSpec , & info ) )
mRecentRedirects . Remove ( newSpec ) ;
// save the new redirect info
info . mSourceURI = oldSpec ;
info . mTimeCreated = PR_Now ( ) ;
if ( aFlags & nsIChannelEventSink : : REDIRECT_TEMPORARY )
info . mType = TRANSITION_REDIRECT_TEMPORARY ;
else
info . mType = TRANSITION_REDIRECT_PERMANENT ;
mRecentRedirects . Put ( newSpec , info ) ;
return NS_OK ;
}
2007-08-28 07:19:57 +00:00
nsresult
2007-11-02 17:46:41 +00:00
nsNavHistory : : OnIdle ( )
2007-08-28 07:19:57 +00:00
{
nsresult rv ;
nsCOMPtr < nsIIdleService > idleService =
do_GetService ( " @mozilla.org/widget/idleservice;1 " , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRUint32 idleTime ;
rv = idleService - > GetIdleTime ( & idleTime ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-25 17:11:14 +00:00
// If we've been idle for more than mFrecencyUpdateIdleTime
// recalculate some frecency values. A value of zero indicates that
// frecency recalculation on idle is disabled.
if ( mFrecencyUpdateIdleTime & & idleTime > mFrecencyUpdateIdleTime )
( void ) RecalculateFrecencies ( ) ;
2007-11-05 01:10:37 +00:00
// If we've been idle for more than EXPIRE_IDLE_TIME_IN_MSECS
// keep the expiration engine chugging along.
// Note: This is done prior to a possible vacuum, to optimize space reduction
// in the vacuum.
if ( idleTime > EXPIRE_IDLE_TIME_IN_MSECS ) {
PRBool dummy ;
( void ) mExpire . ExpireItems ( MAX_EXPIRE_RECORDS_ON_IDLE , & dummy ) ;
}
2007-11-20 23:27:41 +00:00
// If we've been idle for more than LONG_IDLE_TIME_IN_MSECS
2007-11-20 18:23:38 +00:00
// perform long-idle tasks.
2007-11-20 23:27:41 +00:00
if ( idleTime > LONG_IDLE_TIME_IN_MSECS ) {
2007-11-20 18:23:38 +00:00
// Do a one-time re-creation of the moz_places.url index (bug 381795)
// XXX REMOVE ME AFTER BETA2.
PRBool oldIndexExists = PR_FALSE ;
rv = mDBConn - > IndexExists ( NS_LITERAL_CSTRING ( " moz_places_urlindex " ) , & oldIndexExists ) ;
2008-01-05 05:16:27 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
if ( oldIndexExists ) {
2007-11-20 23:27:41 +00:00
// wrap in a transaction for safety and performance
mozStorageTransaction urlindexTransaction ( mDBConn , PR_FALSE ) ;
2007-11-20 18:23:38 +00:00
// drop old index
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " DROP INDEX IF EXISTS moz_places_urlindex " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// remove any duplicates
rv = RemoveDuplicateURIs ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// create new index
rv = mDBConn - > ExecuteSimpleSQL (
NS_LITERAL_CSTRING ( " CREATE UNIQUE INDEX moz_places_url_uniqueindex ON moz_places (url) " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = urlindexTransaction . Commit ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-09-06 19:25:50 +00:00
#if 0
2007-11-02 17:48:08 +00:00
// Currently commented out because vacuum is very slow
// see bug #390244 for more details.
2007-11-02 17:46:41 +00:00
rv = mDBConn - > ExecuteSimpleSQL ( NS_LITERAL_CSTRING ( " VACUUM; " ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-09-06 19:25:50 +00:00
# endif
2007-08-28 07:19:57 +00:00
}
return NS_OK ;
}
void // static
2007-11-02 17:46:41 +00:00
nsNavHistory : : IdleTimerCallback ( nsITimer * aTimer , void * aClosure )
2007-08-28 07:19:57 +00:00
{
nsNavHistory * history = static_cast < nsNavHistory * > ( aClosure ) ;
2007-11-02 17:46:41 +00:00
( void ) history - > OnIdle ( ) ;
2007-08-28 07:19:57 +00:00
}
2006-07-18 16:23:51 +00:00
2007-12-22 02:27:11 +00:00
// nsIDownloadHistory **********************************************************
NS_IMETHODIMP
nsNavHistory : : AddDownload ( nsIURI * aSource , nsIURI * aReferrer ,
PRTime aStartTime )
{
2007-12-27 01:48:19 +00:00
PRInt64 visitID ;
2007-12-22 02:27:11 +00:00
return AddVisit ( aSource , aStartTime , aReferrer , TRANSITION_DOWNLOAD , PR_FALSE ,
2007-12-27 01:48:19 +00:00
0 , & visitID ) ;
2007-12-22 02:27:11 +00:00
}
2006-07-18 15:56:56 +00:00
// nsIObserver *****************************************************************
NS_IMETHODIMP
nsNavHistory : : Observe ( nsISupports * aSubject , const char * aTopic ,
const PRUnichar * aData )
{
if ( nsCRT : : strcmp ( aTopic , gQuitApplicationMessage ) = = 0 ) {
2007-11-02 17:46:41 +00:00
if ( mIdleTimer ) {
mIdleTimer - > Cancel ( ) ;
mIdleTimer = nsnull ;
2007-08-28 07:19:57 +00:00
}
if ( mAutoCompleteTimer ) {
mAutoCompleteTimer - > Cancel ( ) ;
mAutoCompleteTimer = nsnull ;
}
2006-07-18 15:56:56 +00:00
if ( gTldTypes ) {
2006-07-18 15:59:53 +00:00
delete gTldTypes ;
2006-07-18 15:56:56 +00:00
gTldTypes = nsnull ;
}
2006-07-18 16:26:20 +00:00
nsresult rv ;
nsCOMPtr < nsIPrefService > prefService =
do_GetService ( NS_PREFSERVICE_CONTRACTID , & rv ) ;
if ( NS_SUCCEEDED ( rv ) )
prefService - > SavePrefFile ( nsnull ) ;
2006-07-18 16:10:55 +00:00
2006-07-18 16:35:58 +00:00
// notify expiring system that we're quitting, it may want to do stuff
mExpire . OnQuit ( ) ;
2007-08-02 17:34:55 +00:00
2007-07-31 02:16:02 +00:00
// run post-run migration
// NOTE: This must run after expiration. It causes expiration to take a
// very long time if run first.
( void ) CleanUpOnQuit ( ) ;
2007-03-25 20:14:28 +00:00
// notify the bookmarks service we're quitting
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
NS_ENSURE_TRUE ( bookmarks , NS_ERROR_OUT_OF_MEMORY ) ;
( void ) bookmarks - > OnQuit ( ) ;
2006-07-18 16:27:09 +00:00
} else if ( nsCRT : : strcmp ( aTopic , gXpcomShutdown ) = = 0 ) {
nsresult rv ;
nsCOMPtr < nsIObserverService > observerService =
do_GetService ( " @mozilla.org/observer-service;1 " , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
observerService - > RemoveObserver ( this , gXpcomShutdown ) ;
observerService - > RemoveObserver ( this , gQuitApplicationMessage ) ;
2006-07-18 15:56:56 +00:00
} else if ( nsCRT : : strcmp ( aTopic , " nsPref:changed " ) = = 0 ) {
2007-12-05 08:30:07 +00:00
PRInt32 oldDaysMin = mExpireDaysMin ;
PRInt32 oldDaysMax = mExpireDaysMax ;
PRInt32 oldVisits = mExpireSites ;
2008-01-25 17:11:14 +00:00
LoadPrefs ( PR_FALSE ) ;
2007-12-05 08:30:07 +00:00
if ( oldDaysMin ! = mExpireDaysMin | | oldDaysMax ! = mExpireDaysMax | |
oldVisits ! = mExpireSites )
2006-07-18 16:35:58 +00:00
mExpire . OnExpirationChanged ( ) ;
2006-07-18 15:56:56 +00:00
}
return NS_OK ;
}
2006-07-18 16:34:13 +00:00
// Lazy stuff ******************************************************************
# ifdef LAZY_ADD
// nsNavHistory::AddLazyLoadFaviconMessage
nsresult
nsNavHistory : : AddLazyLoadFaviconMessage ( nsIURI * aPage , nsIURI * aFavicon ,
PRBool aForceReload )
{
LazyMessage message ;
nsresult rv = message . Init ( LazyMessage : : Type_Favicon , aPage ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = aFavicon - > Clone ( getter_AddRefs ( message . favicon ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
message . alwaysLoadFavicon = aForceReload ;
return AddLazyMessage ( message ) ;
}
// nsNavHistory::StartLazyTimer
//
// This schedules flushing of the lazy message queue for the future.
//
// If we already have timer set, we canel it and schedule a new timer in
// the future. This saves you from having to wait if you open a bunch of
// pages in a row. However, we don't want to defer too long, so we'll only
// push it back MAX_LAZY_TIMER_DEFERMENTS times. After that we always
// let the timer go the next time.
nsresult
nsNavHistory : : StartLazyTimer ( )
{
if ( ! mLazyTimer ) {
mLazyTimer = do_CreateInstance ( " @mozilla.org/timer;1 " ) ;
if ( ! mLazyTimer )
return NS_ERROR_OUT_OF_MEMORY ;
} else {
if ( mLazyTimerSet ) {
if ( mLazyTimerDeferments > = MAX_LAZY_TIMER_DEFERMENTS ) {
// already set and we don't want to push it back any later, use that one
return NS_OK ;
} else {
// push back the active timer
mLazyTimer - > Cancel ( ) ;
mLazyTimerDeferments + + ;
}
}
}
nsresult rv = mLazyTimer - > InitWithFuncCallback ( LazyTimerCallback , this ,
LAZY_MESSAGE_TIMEOUT ,
nsITimer : : TYPE_ONE_SHOT ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
mLazyTimerSet = PR_TRUE ;
return NS_OK ;
}
// nsNavHistory::AddLazyMessage
nsresult
nsNavHistory : : AddLazyMessage ( const LazyMessage & aMessage )
{
if ( ! mLazyMessages . AppendElement ( aMessage ) )
return NS_ERROR_OUT_OF_MEMORY ;
return StartLazyTimer ( ) ;
}
// nsNavHistory::LazyTimerCallback
void // static
nsNavHistory : : LazyTimerCallback ( nsITimer * aTimer , void * aClosure )
{
2007-07-08 07:08:04 +00:00
nsNavHistory * that = static_cast < nsNavHistory * > ( aClosure ) ;
2006-07-18 16:34:13 +00:00
that - > mLazyTimerSet = PR_FALSE ;
that - > mLazyTimerDeferments = 0 ;
that - > CommitLazyMessages ( ) ;
}
// nsNavHistory::CommitLazyMessages
void
nsNavHistory : : CommitLazyMessages ( )
{
2006-07-18 16:38:03 +00:00
mozStorageTransaction transaction ( mDBConn , PR_TRUE ) ;
2006-07-18 16:34:13 +00:00
for ( PRUint32 i = 0 ; i < mLazyMessages . Length ( ) ; i + + ) {
LazyMessage & message = mLazyMessages [ i ] ;
switch ( message . type ) {
case LazyMessage : : Type_AddURI :
AddURIInternal ( message . uri , message . time , message . isRedirect ,
message . isToplevel , message . referrer ) ;
break ;
case LazyMessage : : Type_Title :
2007-07-26 16:23:11 +00:00
SetPageTitleInternal ( message . uri , message . title ) ;
2006-07-18 16:34:13 +00:00
break ;
case LazyMessage : : Type_Favicon : {
nsFaviconService * faviconService = nsFaviconService : : GetFaviconService ( ) ;
if ( faviconService ) {
nsCString spec ;
message . uri - > GetSpec ( spec ) ;
faviconService - > DoSetAndLoadFaviconForPage ( message . uri ,
message . favicon ,
message . alwaysLoadFavicon ) ;
}
break ;
}
default :
NS_NOTREACHED ( " Invalid lazy message type " ) ;
}
}
mLazyMessages . Clear ( ) ;
}
# endif // LAZY_ADD
2006-07-18 15:56:56 +00:00
// Query stuff *****************************************************************
// nsNavHistory::QueryToSelectClause
//
// THE ORDER AND BEHAVIOR SHOULD BE IN SYNC WITH BindQueryClauseParameters
//
// I don't check return values from the query object getters because there's
// no way for those to fail.
nsresult
2006-07-18 16:20:08 +00:00
nsNavHistory : : QueryToSelectClause ( nsNavHistoryQuery * aQuery , // const
2007-04-25 21:03:29 +00:00
nsNavHistoryQueryOptions * aOptions ,
2006-07-18 15:59:03 +00:00
PRInt32 aStartParameter ,
2006-07-18 15:56:56 +00:00
nsCString * aClause ,
2006-07-18 16:19:29 +00:00
PRInt32 * aParamCount ,
const nsACString & aCommonConditions )
2006-07-18 15:56:56 +00:00
{
PRBool hasIt ;
aClause - > Truncate ( ) ;
* aParamCount = 0 ;
nsCAutoString paramString ;
// begin time
if ( NS_SUCCEEDED ( aQuery - > GetHasBeginTime ( & hasIt ) ) & & hasIt ) {
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " v.visit_date >= " ) + paramString ;
( * aParamCount ) + + ;
}
// end time
if ( NS_SUCCEEDED ( aQuery - > GetHasEndTime ( & hasIt ) ) & & hasIt ) {
if ( ! aClause - > IsEmpty ( ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " v.visit_date <= " ) + paramString ;
( * aParamCount ) + + ;
}
// search terms FIXME
2007-02-16 22:55:10 +00:00
// min and max visit count
if ( aQuery - > MinVisits ( ) > = 0 ) {
if ( ! aClause - > IsEmpty ( ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " h.visit_count >= " ) + paramString ;
( * aParamCount ) + + ;
}
if ( aQuery - > MaxVisits ( ) > = 0 ) {
if ( ! aClause - > IsEmpty ( ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " h.visit_count <= " ) + paramString ;
( * aParamCount ) + + ;
}
2007-06-12 05:18:50 +00:00
2007-09-15 20:07:02 +00:00
if ( aOptions - > QueryType ( ) ! = nsINavHistoryQueryOptions : : QUERY_TYPE_BOOKMARKS & &
aQuery - > OnlyBookmarked ( ) ) {
2007-06-12 05:18:50 +00:00
// only bookmarked, has no affect on bookmarks-only queries
2007-04-25 21:03:29 +00:00
if ( ! aClause - > IsEmpty ( ) )
2006-07-18 15:59:28 +00:00
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
2007-04-04 17:21:03 +00:00
* aClause + = NS_LITERAL_CSTRING ( " EXISTS (SELECT b.fk FROM moz_bookmarks b WHERE b.type = " ) +
nsPrintfCString ( " %d " , nsNavBookmarks : : TYPE_BOOKMARK ) +
NS_LITERAL_CSTRING ( " AND b.fk = h.id) " ) ;
2006-07-18 15:59:28 +00:00
}
2006-07-18 15:56:56 +00:00
// domain
if ( NS_SUCCEEDED ( aQuery - > GetHasDomain ( & hasIt ) ) & & hasIt ) {
if ( ! aClause - > IsEmpty ( ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
PRBool domainIsHost = PR_FALSE ;
aQuery - > GetDomainIsHost ( & domainIsHost ) ;
if ( domainIsHost ) {
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " h.rev_host = " ) + paramString ;
2006-07-18 16:16:38 +00:00
aClause - > Append ( ' ' ) ;
2006-07-18 15:56:56 +00:00
( * aParamCount ) + + ;
} else {
// see domain setting in BindQueryClauseParameters for why we do this
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " h.rev_host >= " ) + paramString ;
( * aParamCount ) + + ;
parameterString ( aStartParameter + * aParamCount , paramString ) ;
* aClause + = NS_LITERAL_CSTRING ( " AND h.rev_host < " ) + paramString ;
2006-07-18 16:16:38 +00:00
aClause - > Append ( ' ' ) ;
2006-07-18 15:56:56 +00:00
( * aParamCount ) + + ;
}
}
2006-07-18 16:16:38 +00:00
// URI
//
// Performance improvement: Selecting URI by prefixes this way is slow because
// sqlite will not use indices when you use substring. Currently, there is
// not really any use for URI queries, so this isn't worth optimizing a lot.
// In the future, we could do a >=,<= thing like we do for domain names to
// make it use the index.
if ( NS_SUCCEEDED ( aQuery - > GetHasUri ( & hasIt ) ) & & hasIt ) {
if ( ! aClause - > IsEmpty ( ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
nsCAutoString paramString ;
parameterString ( aStartParameter + * aParamCount , paramString ) ;
( * aParamCount ) + + ;
nsCAutoString match ;
2006-07-18 16:20:08 +00:00
if ( aQuery - > UriIsPrefix ( ) ) {
2006-07-18 16:16:38 +00:00
// Prefix: want something of the form SUBSTR(h.url, 0, length(?1)) = ?1
* aClause + = NS_LITERAL_CSTRING ( " SUBSTR(h.url, 0, LENGTH( " ) +
paramString + NS_LITERAL_CSTRING ( " )) = " ) + paramString ;
} else {
* aClause + = NS_LITERAL_CSTRING ( " h.url = " ) + paramString ;
}
aClause - > Append ( ' ' ) ;
}
2006-07-18 16:19:29 +00:00
// annotation
aQuery - > GetHasAnnotation ( & hasIt ) ;
if ( hasIt ) {
2007-02-16 22:55:10 +00:00
if ( ! aClause - > IsEmpty ( ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
2006-07-18 16:19:29 +00:00
nsCAutoString paramString ;
parameterString ( aStartParameter + * aParamCount , paramString ) ;
( * aParamCount ) + + ;
2006-07-18 16:20:08 +00:00
if ( aQuery - > AnnotationIsNot ( ) )
2006-07-18 16:19:29 +00:00
aClause - > AppendLiteral ( " NOT " ) ;
2007-04-10 00:24:37 +00:00
aClause - > AppendLiteral ( " EXISTS (SELECT h.id FROM moz_annos anno JOIN moz_anno_attributes annoname ON anno.anno_attribute_id = annoname.id WHERE anno.place_id = h.id AND annoname.name = " ) ;
2006-07-18 16:19:29 +00:00
aClause - > Append ( paramString ) ;
aClause - > AppendLiteral ( " ) " ) ;
// annotation-based queries don't get the common conditions, so you get
// all URLs with that annotation
} else {
2007-02-16 22:55:10 +00:00
if ( ! ( aClause - > IsEmpty ( ) | | aCommonConditions . IsEmpty ( ) ) )
* aClause + = NS_LITERAL_CSTRING ( " AND " ) ;
2006-07-18 16:19:29 +00:00
aClause - > Append ( aCommonConditions ) ;
}
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::BindQueryClauseParameters
//
// THE ORDER AND BEHAVIOR SHOULD BE IN SYNC WITH QueryToSelectClause
nsresult
nsNavHistory : : BindQueryClauseParameters ( mozIStorageStatement * statement ,
2006-07-18 15:59:03 +00:00
PRInt32 aStartParameter ,
2006-07-18 16:20:08 +00:00
nsNavHistoryQuery * aQuery , // const
2007-06-12 05:18:50 +00:00
nsNavHistoryQueryOptions * aOptions ,
2006-07-18 15:59:03 +00:00
PRInt32 * aParamCount )
2006-07-18 15:56:56 +00:00
{
nsresult rv ;
( * aParamCount ) = 0 ;
PRBool hasIt ;
// begin time
if ( NS_SUCCEEDED ( aQuery - > GetHasBeginTime ( & hasIt ) ) & & hasIt ) {
2006-07-18 16:20:08 +00:00
PRTime time = NormalizeTime ( aQuery - > BeginTimeReference ( ) ,
aQuery - > BeginTime ( ) ) ;
2006-07-18 15:56:56 +00:00
rv = statement - > BindInt64Parameter ( aStartParameter + * aParamCount , time ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
}
// end time
if ( NS_SUCCEEDED ( aQuery - > GetHasEndTime ( & hasIt ) ) & & hasIt ) {
2006-07-18 16:20:08 +00:00
PRTime time = NormalizeTime ( aQuery - > EndTimeReference ( ) ,
aQuery - > EndTime ( ) ) ;
2006-07-18 15:56:56 +00:00
rv = statement - > BindInt64Parameter ( aStartParameter + * aParamCount , time ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
}
// search terms FIXME
2007-02-16 22:55:10 +00:00
// min and max visit count
PRInt32 visits = aQuery - > MinVisits ( ) ;
if ( visits > = 0 ) {
rv = statement - > BindInt32Parameter ( aStartParameter + * aParamCount , visits ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
}
visits = aQuery - > MaxVisits ( ) ;
if ( visits > = 0 ) {
rv = statement - > BindInt32Parameter ( aStartParameter + * aParamCount , visits ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
}
2006-07-18 15:56:56 +00:00
// domain (see GetReversedHostname for more info on reversed host names)
if ( NS_SUCCEEDED ( aQuery - > GetHasDomain ( & hasIt ) ) & & hasIt ) {
nsString revDomain ;
2006-07-18 16:20:08 +00:00
GetReversedHostname ( NS_ConvertUTF8toUTF16 ( aQuery - > Domain ( ) ) , revDomain ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:20:08 +00:00
if ( aQuery - > DomainIsHost ( ) ) {
2006-07-18 15:56:56 +00:00
rv = statement - > BindStringParameter ( aStartParameter + * aParamCount , revDomain ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
} else {
// for "mozilla.org" do query >= "gro.allizom." AND < "gro.allizom/"
// which will get everything starting with "gro.allizom." while using the
// index (using SUBSTRING() causes indexes to be discarded).
NS_ASSERTION ( revDomain [ revDomain . Length ( ) - 1 ] = = ' . ' , " Invalid rev. host " ) ;
rv = statement - > BindStringParameter ( aStartParameter + * aParamCount , revDomain ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
revDomain . Truncate ( revDomain . Length ( ) - 1 ) ;
revDomain . Append ( PRUnichar ( ' / ' ) ) ;
rv = statement - > BindStringParameter ( aStartParameter + * aParamCount , revDomain ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
( * aParamCount ) + + ;
}
}
2006-07-18 16:16:38 +00:00
// URI
if ( NS_SUCCEEDED ( aQuery - > GetHasUri ( & hasIt ) ) & & hasIt ) {
2006-07-18 16:20:08 +00:00
BindStatementURI ( statement , aStartParameter + * aParamCount , aQuery - > Uri ( ) ) ;
2006-07-18 16:16:38 +00:00
( * aParamCount ) + + ;
}
2006-07-18 16:19:29 +00:00
// annotation
aQuery - > GetHasAnnotation ( & hasIt ) ;
if ( hasIt ) {
rv = statement - > BindUTF8StringParameter ( aStartParameter + * aParamCount ,
2006-07-18 16:20:08 +00:00
aQuery - > Annotation ( ) ) ;
2006-07-18 16:19:29 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
// nsNavHistory::ResultsAsList
//
nsresult
2006-07-18 16:12:34 +00:00
nsNavHistory : : ResultsAsList ( mozIStorageStatement * statement ,
nsNavHistoryQueryOptions * aOptions ,
2006-07-18 15:56:56 +00:00
nsCOMArray < nsNavHistoryResultNode > * aResults )
{
nsresult rv ;
nsCOMPtr < mozIStorageValueArray > row = do_QueryInterface ( statement , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasMore = PR_FALSE ;
while ( NS_SUCCEEDED ( statement - > ExecuteStep ( & hasMore ) ) & & hasMore ) {
2007-10-19 06:26:34 +00:00
nsRefPtr < nsNavHistoryResultNode > result ;
2006-07-18 16:12:34 +00:00
rv = RowToResult ( row , aOptions , getter_AddRefs ( result ) ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
aResults - > AppendObject ( result ) ;
}
return NS_OK ;
}
// nsNavHistory::RecursiveGroup
//
// aSource and aDest must be different!
2006-07-18 16:19:04 +00:00
//
// This just calls the correct grouping subroutine. These will generate the
// grouping and return to us a list of nodes that are the groups.
//
// If we need to do another level of grouping, we go in and replace those
// container's children with another level of grouping. This is less
// efficient because we need to copy the lists around. However, multilevel
// grouping will be very uncommon so we are more interested in an optimized
// single level of grouping.
2006-07-18 15:56:56 +00:00
nsresult
2007-06-20 05:19:06 +00:00
nsNavHistory : : RecursiveGroup ( nsNavHistoryQueryResultNode * aResultNode ,
const nsCOMArray < nsNavHistoryResultNode > & aSource ,
2007-04-25 21:03:29 +00:00
const PRUint16 * aGroupingMode , PRUint32 aGroupCount ,
2006-07-18 15:56:56 +00:00
nsCOMArray < nsNavHistoryResultNode > * aDest )
{
NS_ASSERTION ( aGroupCount > 0 , " Invalid group count " ) ;
NS_ASSERTION ( aDest - > Count ( ) = = 0 , " Destination array is not empty " ) ;
NS_ASSERTION ( & aSource ! = aDest , " Source and dest must be different for grouping " ) ;
nsresult rv ;
switch ( aGroupingMode [ 0 ] ) {
2006-11-21 23:02:16 +00:00
case nsINavHistoryQueryOptions : : GROUP_BY_DAY :
2007-06-20 05:19:06 +00:00
rv = GroupByDay ( aResultNode , aSource , aDest ) ;
2006-11-21 23:02:16 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : GROUP_BY_HOST :
2007-06-20 05:19:06 +00:00
rv = GroupByHost ( aResultNode , aSource , aDest , PR_FALSE ) ;
2006-07-18 15:56:56 +00:00
break ;
2006-07-18 15:59:28 +00:00
case nsINavHistoryQueryOptions : : GROUP_BY_DOMAIN :
2007-06-20 05:19:06 +00:00
rv = GroupByHost ( aResultNode , aSource , aDest , PR_TRUE ) ;
2006-07-18 15:56:56 +00:00
break ;
2007-04-25 21:03:29 +00:00
case nsINavHistoryQueryOptions : : GROUP_BY_FOLDER :
2007-10-11 06:42:38 +00:00
rv = GroupByFolder ( aResultNode , aSource , aDest ) ;
break ;
2006-07-18 15:56:56 +00:00
default :
// unknown grouping mode
return NS_ERROR_INVALID_ARG ;
}
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( aGroupCount > 1 ) {
// Sort another level: We need to copy the array since we want the output
2007-07-12 23:49:58 +00:00
// to be our level's destination arrays.
2006-07-18 15:59:03 +00:00
for ( PRInt32 i = 0 ; i < aDest - > Count ( ) ; i + + ) {
2006-07-18 15:56:56 +00:00
nsNavHistoryResultNode * curNode = ( * aDest ) [ i ] ;
2006-07-18 16:19:04 +00:00
if ( curNode - > IsContainer ( ) ) {
nsNavHistoryContainerResultNode * container = curNode - > GetAsContainer ( ) ;
nsCOMArray < nsNavHistoryResultNode > temp ( container - > mChildren ) ;
container - > mChildren . Clear ( ) ;
2007-06-20 05:19:06 +00:00
rv = RecursiveGroup ( aResultNode , temp , & aGroupingMode [ 1 ] , aGroupCount - 1 ,
2006-07-18 16:19:04 +00:00
& container - > mChildren ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
}
}
return NS_OK ;
}
2006-11-21 23:02:16 +00:00
// code borrowed from mozilla/xpfe/components/history/src/nsGlobalHistory.cpp
// pass in a pre-normalized now and a date, and we'll find
// the difference since midnight on each of the days.
//
// USECS_PER_DAY == PR_USEC_PER_SEC * 60 * 60 * 24;
static const PRInt64 USECS_PER_DAY = LL_INIT ( 20 , 500654080 ) ;
static PRInt64
GetAgeInDays ( PRTime aNormalizedNow , PRTime aDate )
{
PRTime dateMidnight = NormalizeTimeRelativeToday ( aDate ) ;
2007-06-26 15:42:07 +00:00
// if the visit time is in the future
// treat as "today" see bug #385867
if ( dateMidnight > aNormalizedNow )
return 0 ;
else
return ( ( aNormalizedNow - dateMidnight ) / USECS_PER_DAY ) ;
2006-11-21 23:02:16 +00:00
}
2007-10-11 06:42:38 +00:00
const PRInt64 UNDEFINED_URN_VALUE = - 1 ;
2007-06-20 05:19:06 +00:00
// Create a urn (like
// urn:places-persist:place:group=0&group=1&sort=1&type=1,,%28local%20files%29)
// to be used to persist the open state of this container in localstore.rdf
nsresult
CreatePlacesPersistURN ( nsNavHistoryQueryResultNode * aResultNode ,
2007-10-11 06:42:38 +00:00
PRInt64 aValue , const nsCString & aTitle , nsCString & aURN )
2007-06-20 05:19:06 +00:00
{
nsCAutoString uri ;
nsresult rv = aResultNode - > GetUri ( uri ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
aURN . Assign ( NS_LITERAL_CSTRING ( " urn:places-persist: " ) ) ;
aURN . Append ( uri ) ;
aURN . Append ( NS_LITERAL_CSTRING ( " , " ) ) ;
2007-10-11 06:42:38 +00:00
if ( aValue ! = UNDEFINED_URN_VALUE )
aURN . AppendInt ( aValue ) ;
2007-06-20 05:19:06 +00:00
aURN . Append ( NS_LITERAL_CSTRING ( " , " ) ) ;
if ( ! aTitle . IsEmpty ( ) ) {
nsCAutoString escapedTitle ;
PRBool success = NS_Escape ( aTitle , escapedTitle , url_XAlphas ) ;
NS_ENSURE_TRUE ( success , NS_ERROR_OUT_OF_MEMORY ) ;
aURN . Append ( escapedTitle ) ;
}
return NS_OK ;
}
2006-11-21 23:02:16 +00:00
// XXX todo
// we should make "group by date" more flexible and extensible, in order
// to allow extension developers to write better history sidebars and viewers
// see bug #359346
// nsNavHistory::GroupByDay
nsresult
2007-06-20 05:19:06 +00:00
nsNavHistory : : GroupByDay ( nsNavHistoryQueryResultNode * aResultNode ,
const nsCOMArray < nsNavHistoryResultNode > & aSource ,
2006-11-21 23:02:16 +00:00
nsCOMArray < nsNavHistoryResultNode > * aDest )
{
// 8 == today, yesterday, 2 ago, 3 ago, 4 ago, 5 ago, 6 ago, older than 6
const PRInt32 numDays = 8 ;
nsNavHistoryContainerResultNode * dates [ numDays ] ;
for ( PRInt32 i = 0 ; i < numDays ; i + + )
dates [ i ] = nsnull ;
nsCAutoString dateNames [ numDays ] ;
// special case: Today
GetStringFromName ( NS_LITERAL_STRING ( " finduri-AgeInDays-is-0 " ) . get ( ) ,
dateNames [ 0 ] ) ;
// special case: Yesterday
GetStringFromName ( NS_LITERAL_STRING ( " finduri-AgeInDays-is-1 " ) . get ( ) ,
dateNames [ 1 ] ) ;
for ( PRInt32 curDay = 2 ; curDay < = numDays - 2 ; curDay + + ) {
// common case: "<curDay> days ago"
GetAgeInDaysString ( curDay , NS_LITERAL_STRING ( " finduri-AgeInDays-is " ) . get ( ) ,
dateNames [ curDay ] ) ;
}
// special case: "Older than <numDays-2> days"
GetAgeInDaysString ( numDays - 2 ,
NS_LITERAL_STRING ( " finduri-AgeInDays-isgreater " ) . get ( ) ,
dateNames [ numDays - 1 ] ) ;
PRTime normalizedNow = NormalizeTimeRelativeToday ( PR_Now ( ) ) ;
for ( PRInt32 i = 0 ; i < aSource . Count ( ) ; i + + ) {
if ( ! aSource [ i ] - > IsURI ( ) ) {
// what do we do with non-URLs? I'll just dump them into the top level
aDest - > AppendObject ( aSource [ i ] ) ;
continue ;
}
// get the date from aSource[i]
nsCAutoString curDateName ;
PRInt64 ageInDays = GetAgeInDays ( normalizedNow , aSource [ i ] - > mTime ) ;
if ( ageInDays > ( numDays - 1 ) )
ageInDays = numDays - 1 ;
curDateName = dateNames [ ageInDays ] ;
if ( ! dates [ ageInDays ] ) {
2007-06-20 05:19:06 +00:00
nsCAutoString urn ;
nsresult rv = CreatePlacesPersistURN ( aResultNode , ageInDays , EmptyCString ( ) , urn ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-11-21 23:02:16 +00:00
// need to create an entry for this date
2007-06-20 05:19:06 +00:00
dates [ ageInDays ] = new nsNavHistoryContainerResultNode ( urn ,
2006-11-21 23:02:16 +00:00
curDateName ,
EmptyCString ( ) ,
nsNavHistoryResultNode : : RESULT_TYPE_DAY ,
PR_TRUE ,
2007-08-02 20:19:44 +00:00
EmptyCString ( ) ,
nsnull ) ;
2006-11-21 23:02:16 +00:00
if ( ! dates [ ageInDays ] )
return NS_ERROR_OUT_OF_MEMORY ;
}
if ( ! dates [ ageInDays ] - > mChildren . AppendObject ( aSource [ i ] ) )
return NS_ERROR_OUT_OF_MEMORY ;
}
for ( PRInt32 i = 0 ; i < numDays ; i + + ) {
if ( dates [ i ] ) {
nsresult rv = aDest - > AppendObject ( dates [ i ] ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
}
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::GroupByHost
//
2006-07-18 16:19:04 +00:00
// OPTIMIZATION: This parses the URI of each node that is coming in. This
// makes it kind of slow. A previous version of this code used a host on
// each result node. This host name is populated from the database, so it
// doesn't have to be populated at query time. This is faster, but takes up
// a lot of space in result nodes that isn't very helpful.
//
// One option would be to store the host names in the nodes only if we will
// be grouping by host later. Once we group by host, we could set it to the
// empty string to free that heap data. Then this code would be fast but
// we would only be charged the overhead of an empty string on each node.
2006-07-18 15:56:56 +00:00
nsresult
2007-06-20 05:19:06 +00:00
nsNavHistory : : GroupByHost ( nsNavHistoryQueryResultNode * aResultNode ,
const nsCOMArray < nsNavHistoryResultNode > & aSource ,
2006-07-18 16:19:04 +00:00
nsCOMArray < nsNavHistoryResultNode > * aDest ,
PRBool aIsDomain )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:19:04 +00:00
nsDataHashtable < nsCStringHashKey , nsNavHistoryContainerResultNode * > hosts ;
if ( ! hosts . Init ( 512 ) )
2006-07-18 15:56:56 +00:00
return NS_ERROR_OUT_OF_MEMORY ;
2006-07-18 16:19:04 +00:00
for ( PRInt32 i = 0 ; i < aSource . Count ( ) ; i + + ) {
if ( ! aSource [ i ] - > IsURI ( ) ) {
// what do we do with non-URLs? I'll just dump them into the top level
aDest - > AppendObject ( aSource [ i ] ) ;
continue ;
2006-07-18 15:56:56 +00:00
}
2006-07-18 16:19:04 +00:00
// get the host name
nsCOMPtr < nsIURI > uri ;
nsCAutoString fullHostName ;
2006-07-18 16:22:38 +00:00
if ( NS_FAILED ( NS_NewURI ( getter_AddRefs ( uri ) , aSource [ i ] - > mURI ) ) | |
2006-07-18 16:19:04 +00:00
NS_FAILED ( uri - > GetHost ( fullHostName ) ) ) {
// invalid host name, just drop it in the top level
aDest - > AppendObject ( aSource [ i ] ) ;
continue ;
}
2006-07-18 15:56:56 +00:00
2006-07-18 16:19:04 +00:00
nsCAutoString curHostName ;
if ( aIsDomain ) {
DomainNameFromHostName ( fullHostName , curHostName ) ;
2006-07-18 15:56:56 +00:00
} else {
2006-07-18 16:19:04 +00:00
// just use the full host name
curHostName = fullHostName ;
2006-07-18 15:56:56 +00:00
}
2006-07-18 16:19:04 +00:00
nsNavHistoryContainerResultNode * curTopGroup = nsnull ;
if ( ! hosts . Get ( curHostName , & curTopGroup ) ) {
2006-07-18 15:56:56 +00:00
// need to create an entry for this host
2006-07-18 16:19:04 +00:00
nsCAutoString title ;
TitleForDomain ( curHostName , title ) ;
2007-06-20 05:19:06 +00:00
nsCAutoString urn ;
2007-10-11 06:42:38 +00:00
nsresult rv = CreatePlacesPersistURN ( aResultNode , UNDEFINED_URN_VALUE , title , urn ) ;
2007-06-20 05:19:06 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
curTopGroup = new nsNavHistoryContainerResultNode ( urn , title ,
2006-07-18 16:22:38 +00:00
EmptyCString ( ) , nsNavHistoryResultNode : : RESULT_TYPE_HOST , PR_TRUE ,
2007-08-02 20:19:44 +00:00
EmptyCString ( ) , nsnull ) ;
2006-07-18 15:56:56 +00:00
if ( ! curTopGroup )
return NS_ERROR_OUT_OF_MEMORY ;
2006-07-18 16:19:04 +00:00
if ( ! hosts . Put ( curHostName , curTopGroup ) )
2006-07-18 15:56:56 +00:00
return NS_ERROR_OUT_OF_MEMORY ;
2007-06-20 05:19:06 +00:00
rv = aDest - > AppendObject ( curTopGroup ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2006-07-18 16:19:04 +00:00
if ( ! curTopGroup - > mChildren . AppendObject ( aSource [ i ] ) )
return NS_ERROR_OUT_OF_MEMORY ;
2006-07-18 15:56:56 +00:00
}
return NS_OK ;
}
2007-10-11 06:42:38 +00:00
PRInt64
2007-11-20 02:01:53 +00:00
nsNavHistory : : GetTagsFolder ( )
2007-10-11 06:42:38 +00:00
{
2007-11-20 02:01:53 +00:00
// cache our tags folder
2007-10-11 06:42:38 +00:00
// note, we can't do this in nsNavHistory::Init(),
// as getting the bookmarks service would initialize it.
2007-11-20 02:01:53 +00:00
if ( mTagsFolder = = - 1 ) {
2007-10-11 06:42:38 +00:00
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
NS_ENSURE_TRUE ( bookmarks , - 1 ) ;
2007-11-20 02:01:53 +00:00
nsresult rv = bookmarks - > GetTagsFolder ( & mTagsFolder ) ;
2007-10-11 06:42:38 +00:00
NS_ENSURE_SUCCESS ( rv , - 1 ) ;
}
2007-11-20 02:01:53 +00:00
return mTagsFolder ;
2007-10-11 06:42:38 +00:00
}
// nsNavHistory::GroupByFolder
nsresult
nsNavHistory : : GroupByFolder ( nsNavHistoryQueryResultNode * aResultNode ,
const nsCOMArray < nsNavHistoryResultNode > & aSource ,
nsCOMArray < nsNavHistoryResultNode > * aDest )
{
nsresult rv ;
nsDataHashtable < nsTrimInt64HashKey , nsNavHistoryContainerResultNode * > folders ;
if ( ! folders . Init ( 512 ) )
return NS_ERROR_OUT_OF_MEMORY ;
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
if ( ! bookmarks )
return NS_ERROR_OUT_OF_MEMORY ;
// iterate over source, creating container nodes for each
// entry's parent folder, adding to hash
for ( PRInt32 i = 0 ; i < aSource . Count ( ) ; i + + ) {
if ( aSource [ i ] - > mItemId = = - 1 )
continue ; // only bookmark nodes can be grouped by folder
PRInt64 parentId ;
rv = bookmarks - > GetFolderIdForItem ( aSource [ i ] - > mItemId , & parentId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// if parent id is not in the hash, add it
nsNavHistoryContainerResultNode * folderNode = nsnull ;
if ( ! folders . Get ( parentId , & folderNode ) ) {
// get parent folder title
2008-01-09 03:54:37 +00:00
nsCAutoString title ;
2007-10-11 06:42:38 +00:00
rv = bookmarks - > GetItemTitle ( parentId , title ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
nsCAutoString urn ;
2008-01-09 03:54:37 +00:00
rv = CreatePlacesPersistURN ( aResultNode , parentId , title , urn ) ;
2007-10-11 06:42:38 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// create parent node
folderNode = new nsNavHistoryContainerResultNode ( urn ,
2008-01-09 03:54:37 +00:00
title , EmptyCString ( ) , nsNavHistoryResultNode : : RESULT_TYPE_FOLDER ,
2007-10-11 06:42:38 +00:00
PR_TRUE , EmptyCString ( ) , aResultNode - > mOptions ) ;
if ( ! folders . Put ( parentId , folderNode ) )
return NS_ERROR_OUT_OF_MEMORY ;
2007-11-20 02:01:53 +00:00
folderNode - > mItemId = parentId ;
2007-10-11 06:42:38 +00:00
rv = aDest - > AppendObject ( folderNode ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
// add self to parent node
if ( ! folderNode - > mChildren . AppendObject ( aSource [ i ] ) )
return NS_ERROR_OUT_OF_MEMORY ;
// when grouping by folders, we create new nsNavHistoryContainerResultNode
// nodes. set the date added and last modified values for that node
// to be the greatest value from the children in that group.
if ( aSource [ i ] - > mDateAdded > folderNode - > mDateAdded )
folderNode - > mDateAdded = aSource [ i ] - > mDateAdded ;
if ( aSource [ i ] - > mLastModified > folderNode - > mLastModified )
folderNode - > mLastModified = aSource [ i ] - > mLastModified ;
}
return NS_OK ;
}
2007-09-15 19:53:43 +00:00
PRBool
2007-11-25 02:38:17 +00:00
nsNavHistory : : URIHasTag ( const nsACString & aURISpec , const nsAString & aTag )
2007-09-15 19:53:43 +00:00
{
2007-10-09 19:14:48 +00:00
mozStorageStatementScoper scoper ( mDBURIHasTag ) ;
2007-09-15 19:53:43 +00:00
2007-11-25 02:38:17 +00:00
nsresult rv = mDBURIHasTag - > BindUTF8StringParameter ( 0 , aURISpec ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-09-15 19:53:43 +00:00
2007-10-09 19:14:48 +00:00
rv = mDBURIHasTag - > BindStringParameter ( 1 , aTag ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-09-15 19:53:43 +00:00
2007-11-25 02:38:17 +00:00
rv = mDBURIHasTag - > BindInt64Parameter ( 2 , GetTagsFolder ( ) ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-09-15 19:53:43 +00:00
2007-10-09 19:14:48 +00:00
PRBool hasTag = PR_FALSE ;
rv = mDBURIHasTag - > ExecuteStep ( & hasTag ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-10-09 19:14:48 +00:00
return hasTag ;
2007-09-15 19:53:43 +00:00
}
2007-11-25 02:38:17 +00:00
void
nsNavHistory : : CreateTermsFromTokens ( const nsStringArray & aTagTokens , nsStringArray & aTerms )
{
PRUint32 tagTokensCount = aTagTokens . Count ( ) ;
// from our tokens, build up all possible tags
// for example: ("a b c") -> ("a","b","c","a b","b c","a b c")
for ( PRUint32 numCon = 1 ; numCon < = tagTokensCount ; numCon + + ) {
for ( PRUint32 i = 0 ; i < tagTokensCount ; i + + ) {
if ( i + numCon > tagTokensCount )
continue ;
// after certain number of tokens (30 if SQLITE_MAX_EXPR_DEPTH is the default of 1000)
// we'll generate a query with an expression tree that is too large.
// if we exceed this limit, CreateStatement() will fail.
// 30 tokens == 465 terms
if ( aTerms . Count ( ) = = 465 ) {
NS_WARNING ( " hitting SQLITE_MAX_EXPR_DEPTH, not generating any more terms " ) ;
return ;
}
nsAutoString currentValue ;
for ( PRUint32 j = i ; j < i + numCon ; j + + ) {
if ( ! currentValue . IsEmpty ( ) )
currentValue + = NS_LITERAL_STRING ( " " ) ;
currentValue + = * ( aTagTokens . StringAt ( j ) ) ;
}
aTerms . AppendString ( currentValue ) ;
}
}
}
PRBool
nsNavHistory : : URIHasAnyTagFromTerms ( const nsACString & aURISpec , const nsStringArray & aTerms )
{
PRUint32 termsCount = aTerms . Count ( ) ;
if ( termsCount = = 1 )
return URIHasTag ( aURISpec , * ( aTerms . StringAt ( 0 ) ) ) ;
nsCString tagQuery = NS_LITERAL_CSTRING (
" SELECT b.id FROM moz_bookmarks b "
" JOIN moz_places p ON b.fk = p.id "
" WHERE p.url = ?1 "
" AND (SELECT b1.parent FROM moz_bookmarks b1 WHERE "
" b1.id = b.parent AND ( " ) ;
for ( PRUint32 i = 0 ; i < termsCount ; i + + ) {
if ( i )
tagQuery + = NS_LITERAL_CSTRING ( " OR " ) ;
// +3 to skip over the "?2", which is the tag root parameter
tagQuery + = NS_LITERAL_CSTRING ( " LOWER(b1.title) = " ) +
nsPrintfCString ( " LOWER(?%d) " , i + 3 ) ;
}
tagQuery + = NS_LITERAL_CSTRING ( " )) = ?2 LIMIT 1 " ) ;
nsCOMPtr < mozIStorageStatement > uriHasAnyTagQuery ;
nsresult rv = mDBConn - > CreateStatement ( tagQuery , getter_AddRefs ( uriHasAnyTagQuery ) ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-11-25 02:38:17 +00:00
rv = uriHasAnyTagQuery - > BindUTF8StringParameter ( 0 , aURISpec ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-11-25 02:38:17 +00:00
rv = uriHasAnyTagQuery - > BindInt64Parameter ( 1 , GetTagsFolder ( ) ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-11-25 02:38:17 +00:00
for ( PRUint32 i = 0 ; i < termsCount ; i + + ) {
// +2 to skip over the "?2", which is the tag root parameter
rv = uriHasAnyTagQuery - > BindStringParameter ( i + 2 , * ( aTerms . StringAt ( i ) ) ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-11-25 02:38:17 +00:00
}
PRBool hasAnyTag = PR_FALSE ;
rv = uriHasAnyTagQuery - > ExecuteStep ( & hasAnyTag ) ;
2008-01-30 05:04:55 +00:00
NS_ENSURE_SUCCESS ( rv , PR_FALSE ) ;
2007-11-25 02:38:17 +00:00
return hasAnyTag ;
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::FilterResultSet
//
2007-10-01 17:36:18 +00:00
// This does some post-query-execution filtering:
// - searching on title & url
// - parent folder (recursively)
// - excludeQueries
// - tags
// - limit count
2007-10-11 06:42:38 +00:00
// - excludingLivemarkItems
2007-10-01 17:36:18 +00:00
//
// Note: changes to filtering in FilterResultSet()
// may require changes to NeedToFilterResultSet()
2006-07-18 15:56:56 +00:00
nsresult
2007-09-15 20:07:02 +00:00
nsNavHistory : : FilterResultSet ( nsNavHistoryQueryResultNode * aQueryNode ,
2007-09-01 21:23:36 +00:00
const nsCOMArray < nsNavHistoryResultNode > & aSet ,
2006-07-18 15:56:56 +00:00
nsCOMArray < nsNavHistoryResultNode > * aFiltered ,
2007-10-01 17:36:18 +00:00
const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsNavHistoryQueryOptions * aOptions )
2006-07-18 15:56:56 +00:00
{
2007-09-01 21:23:36 +00:00
nsresult rv ;
2007-09-15 20:07:02 +00:00
// get the bookmarks service
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
NS_ENSURE_TRUE ( bookmarks , NS_ERROR_OUT_OF_MEMORY ) ;
// parse the search terms
nsTArray < nsStringArray * > terms ;
ParseSearchTermsFromQueries ( aQueries , & terms ) ;
PRUint32 queryIndex ;
// The includeFolders array for each query is initialized with its
// query's folders array. We add sub-folders as we check items.
nsTArray < nsTArray < PRInt64 > * > includeFolders ;
nsTArray < nsTArray < PRInt64 > * > excludeFolders ;
for ( queryIndex = 0 ;
queryIndex < aQueries . Count ( ) ; queryIndex + + ) {
includeFolders . AppendElement ( new nsTArray < PRInt64 > ( aQueries [ queryIndex ] - > Folders ( ) ) ) ;
excludeFolders . AppendElement ( new nsTArray < PRInt64 > ( ) ) ;
}
2006-07-18 15:56:56 +00:00
2007-09-01 21:23:36 +00:00
// filter against query options
2007-10-11 06:42:38 +00:00
// XXX only excludeQueries and excludeItemIfParentHasAnnotation are supported at the moment
2007-09-01 21:23:36 +00:00
PRBool excludeQueries = PR_FALSE ;
2007-09-15 20:07:02 +00:00
if ( aQueryNode ) {
rv = aQueryNode - > mOptions - > GetExcludeQueries ( & excludeQueries ) ;
2007-09-01 21:23:36 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
}
2007-10-11 06:42:38 +00:00
nsCString parentAnnotationToExclude ;
nsTArray < PRInt64 > parentFoldersToExclude ;
if ( aQueryNode ) {
rv = aQueryNode - > mOptions - > GetExcludeItemIfParentHasAnnotation ( parentAnnotationToExclude ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
if ( ! parentAnnotationToExclude . IsEmpty ( ) ) {
// find all the folders that have the annotation we are excluding
// and save off their item ids. when doing filtering,
// if a result's parent item id matches a saved item id,
// the result should be excluded
mozStorageStatementScoper scope ( mFoldersWithAnnotationQuery ) ;
rv = mFoldersWithAnnotationQuery - > BindUTF8StringParameter ( 0 , parentAnnotationToExclude ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasMore = PR_FALSE ;
while ( NS_SUCCEEDED ( mFoldersWithAnnotationQuery - > ExecuteStep ( & hasMore ) ) & & hasMore ) {
PRInt64 folderId = 0 ;
rv = mFoldersWithAnnotationQuery - > GetInt64 ( 0 , & folderId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
parentFoldersToExclude . AppendElement ( folderId ) ;
}
}
2006-07-18 15:59:03 +00:00
for ( PRInt32 nodeIndex = 0 ; nodeIndex < aSet . Count ( ) ; nodeIndex + + ) {
2007-10-30 23:53:53 +00:00
// exclude-queries is implicit when searching, we're only looking at
// plan URI nodes
if ( ! aSet [ nodeIndex ] - > IsURI ( ) )
2007-09-01 21:23:36 +00:00
continue ;
2007-09-15 20:07:02 +00:00
PRInt64 parentId = - 1 ;
if ( aSet [ nodeIndex ] - > mItemId ! = - 1 ) {
if ( aQueryNode - > mItemId = = aSet [ nodeIndex ] - > mItemId )
continue ;
rv = bookmarks - > GetFolderIdForItem ( aSet [ nodeIndex ] - > mItemId , & parentId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 15:56:56 +00:00
}
2007-10-11 06:42:38 +00:00
// if we are excluding items by parent annotation,
// exclude items who's parent is a folder with that annotation
if ( ! parentAnnotationToExclude . IsEmpty ( ) & & ( parentFoldersToExclude . IndexOf ( parentId ) ! = - 1 ) )
continue ;
2007-09-15 20:07:02 +00:00
// Append the node if it matches one of the queries
PRBool appendNode = PR_FALSE ;
for ( queryIndex = 0 ;
queryIndex < aQueries . Count ( ) & & ! appendNode ; queryIndex + + ) {
// parent folder
if ( includeFolders [ queryIndex ] - > Length ( ) ! = 0 ) {
// filter out simple history nodes from bookmark queries
if ( aSet [ nodeIndex ] - > mItemId = = - 1 )
continue ;
// filter out the node of which their parent is in the exclude-folders
// cache
if ( excludeFolders [ queryIndex ] - > IndexOf ( parentId ) ! = - 1 )
continue ;
if ( includeFolders [ queryIndex ] - > IndexOf ( parentId ) = = - 1 ) {
// check ancestors
PRInt64 ancestor = parentId , lastAncestor ;
PRBool belongs = PR_FALSE ;
2007-09-27 17:08:15 +00:00
nsTArray < PRInt64 > ancestorFolders ;
2007-09-15 20:07:02 +00:00
while ( ! belongs ) {
// Avoid using |ancestor| itself if GetFolderIdForItem failed.
lastAncestor = ancestor ;
2007-09-27 17:08:15 +00:00
ancestorFolders . AppendElement ( ancestor ) ;
2007-09-15 20:07:02 +00:00
// GetFolderIdForItems throws when called for the places-root
if ( NS_FAILED ( bookmarks - > GetFolderIdForItem ( ancestor , & ancestor ) ) ) {
break ;
2007-09-27 17:08:15 +00:00
} else if ( excludeFolders [ queryIndex ] - > IndexOf ( ancestor ) ! = - 1 ) {
break ;
2007-09-15 20:07:02 +00:00
} else if ( includeFolders [ queryIndex ] - > IndexOf ( ancestor ) ! = - 1 ) {
belongs = PR_TRUE ;
}
}
2007-09-27 17:08:15 +00:00
// if the parentId or any of its ancestors "belong",
// include all of them. otherwise, exclude all of them.
2007-09-15 20:07:02 +00:00
if ( belongs ) {
2007-09-27 17:08:15 +00:00
includeFolders [ queryIndex ] - > AppendElements ( ancestorFolders ) ;
2007-09-15 20:07:02 +00:00
} else {
2007-09-27 17:08:15 +00:00
excludeFolders [ queryIndex ] - > AppendElements ( ancestorFolders ) ;
2007-09-15 20:07:02 +00:00
continue ;
}
}
}
2007-09-15 19:53:43 +00:00
2007-11-25 02:38:17 +00:00
nsStringArray tagTerms ;
CreateTermsFromTokens ( * terms [ queryIndex ] , tagTerms ) ;
2007-09-15 20:07:02 +00:00
// search terms
// XXXmano/dietrich: when bug 331487 is fixed, bookmark queries can group
// by folder or not regardless of specified folders or search terms.
PRBool allTermsFound = PR_TRUE ;
for ( PRInt32 termIndex = 0 ; termIndex < terms [ queryIndex ] - > Count ( ) & &
allTermsFound ; termIndex + + ) {
// search terms should match title, url or tags
2007-09-01 21:23:36 +00:00
PRBool termFound = PR_FALSE ;
// title and URL
2007-09-15 20:07:02 +00:00
if ( CaseInsensitiveFindInReadable ( * terms [ queryIndex ] - > StringAt ( termIndex ) ,
2007-09-01 21:23:36 +00:00
NS_ConvertUTF8toUTF16 ( aSet [ nodeIndex ] - > mTitle ) ) | |
2007-10-30 23:53:53 +00:00
( CaseInsensitiveFindInReadable ( * terms [ queryIndex ] - > StringAt ( termIndex ) ,
2007-09-15 20:07:02 +00:00
NS_ConvertUTF8toUTF16 ( aSet [ nodeIndex ] - > mURI ) ) ) )
2007-09-01 21:23:36 +00:00
termFound = PR_TRUE ;
2007-09-15 19:53:43 +00:00
2007-09-15 20:07:02 +00:00
if ( ! termFound )
2007-09-01 21:23:36 +00:00
allTermsFound = PR_FALSE ;
2006-07-18 15:56:56 +00:00
}
2007-11-25 02:38:17 +00:00
if ( ! allTermsFound & & ! URIHasAnyTagFromTerms ( aSet [ nodeIndex ] - > mURI , tagTerms ) )
continue ;
2007-09-01 21:23:36 +00:00
2007-09-15 20:07:02 +00:00
appendNode = PR_TRUE ;
}
if ( appendNode )
2006-07-18 15:56:56 +00:00
aFiltered - > AppendObject ( aSet [ nodeIndex ] ) ;
2007-10-01 17:36:18 +00:00
2007-10-11 06:42:38 +00:00
// stop once we've seen max results
// unless our options apply to containers, in which case we need to
// handle max results after sorting, see FillChildren()
if ( ! aOptions - > ApplyOptionsToContainers ( ) & &
aOptions - > MaxResults ( ) > 0 & &
aFiltered - > Count ( ) > = aOptions - > MaxResults ( ) )
2007-10-01 17:36:18 +00:00
break ;
2006-07-18 15:56:56 +00:00
}
2007-09-15 20:07:02 +00:00
// de-allocate the matrixes
for ( PRUint32 i = 0 ; i < aQueries . Count ( ) ; i + + ) {
delete terms [ i ] ;
delete includeFolders [ i ] ;
delete excludeFolders [ i ] ;
}
2006-07-18 15:56:56 +00:00
return NS_OK ;
}
2006-07-18 16:14:12 +00:00
// nsNavHistory::CheckIsRecentEvent
//
// Sees if this URL happened "recently."
//
// It is always removed from our recent list no matter what. It only counts
2007-12-05 08:30:07 +00:00
// as "recent" if the event happened more recently than our event
2006-07-18 16:14:12 +00:00
// threshold ago.
PRBool
nsNavHistory : : CheckIsRecentEvent ( RecentEventHash * hashTable ,
const nsACString & url )
{
PRTime eventTime ;
if ( hashTable - > Get ( url , & eventTime ) ) {
hashTable - > Remove ( url ) ;
if ( eventTime > GetNow ( ) - RECENT_EVENT_THRESHOLD )
return PR_TRUE ;
return PR_FALSE ;
}
return PR_FALSE ;
}
// nsNavHistory::ExpireNonrecentEvents
//
// This goes through our
PR_STATIC_CALLBACK ( PLDHashOperator )
ExpireNonrecentEventsCallback ( nsCStringHashKey : : KeyType aKey ,
PRInt64 & aData ,
void * userArg )
{
2007-07-08 07:08:04 +00:00
PRInt64 * threshold = reinterpret_cast < PRInt64 * > ( userArg ) ;
2006-07-18 16:23:51 +00:00
if ( aData < * threshold )
2006-07-18 16:14:12 +00:00
return PL_DHASH_REMOVE ;
return PL_DHASH_NEXT ;
}
void
nsNavHistory : : ExpireNonrecentEvents ( RecentEventHash * hashTable )
{
2006-07-18 16:23:51 +00:00
PRInt64 threshold = GetNow ( ) - RECENT_EVENT_THRESHOLD ;
2006-07-18 16:14:12 +00:00
hashTable - > Enumerate ( ExpireNonrecentEventsCallback ,
2007-07-08 07:08:04 +00:00
reinterpret_cast < void * > ( & threshold ) ) ;
2006-07-18 16:23:51 +00:00
}
// nsNavHistory::GetRedirectFor
//
// Given a destination URI, this finds a recent redirect that resulted in
// this URI. If it finds one, it will put the redirect source info into
// the out params and return true. If there is no matching redirect, it will
// return false.
//
// @param aDestination The destination URI spec of the redirect to look for.
// @param aSource Will be filled with the redirect source URI when a
// redirect is found.
// @param aTime Will be filled with the time the redirect happened
// when a redirect is found.
// @param aRedirectType Will be filled with the redirect type when a redirect
// is found. Will be either
// TRANSITION_REDIRECT_PERMANENT or
// TRANSITION_REDIRECT_TEMPORARY
// @returns True if the redirect is found.
//
// HOW REDIRECT TRACKING WORKS
// ---------------------------
2006-07-18 16:29:39 +00:00
// When we get an AddDocumentRedirect message, we store the redirect in
2006-07-18 16:23:51 +00:00
// our mRecentRedirects which maps the destination URI to a source,time pair.
// When we get a new URI, we see if there were any redirects to this page
// in the hash table. If found, we know that the page came through the given
// redirect and add it.
//
// Example: Page S redirects throught R1, then R2, to give page D. Page S
// will have been already added to history.
2006-07-18 16:29:39 +00:00
// - AddDocumentRedirect(R1, R2)
// - AddDocumentRedirect(R2, D)
2006-07-18 16:23:51 +00:00
// - AddURI(uri=D, referrer=S)
//
// When we get the AddURI(D), we see the hash table has a value for D from R2.
// We have to recursively check that source since there could be more than
// one redirect, as in this case. Here we see there was a redirect to R2 from
// R1. The referrer for D is S, so we know S->R1->R2->D.
//
// Alternatively, the user could have typed or followed a bookmark from S.
// In this case, with two redirects we'll get:
// - MarkPageAsTyped(S)
2006-07-18 16:29:39 +00:00
// - AddDocumentRedirect(S, R)
// - AddDocumentRedirect(R, D)
2006-07-18 16:23:51 +00:00
// - AddURI(uri=D, referrer=null)
// We need to be careful to add a visit to S in this case with an incoming
// transition of typed and an outgoing transition of redirect.
//
// Note that this can get confused in some cases where you have a page
// open in more than one window loading at the same time. This should be rare,
// however, and should not affect much.
PRBool
nsNavHistory : : GetRedirectFor ( const nsACString & aDestination ,
nsACString & aSource , PRTime * aTime ,
PRUint32 * aRedirectType )
{
RedirectInfo info ;
if ( mRecentRedirects . Get ( aDestination , & info ) ) {
mRecentRedirects . Remove ( aDestination ) ;
if ( info . mTimeCreated < GetNow ( ) - RECENT_EVENT_THRESHOLD )
return PR_FALSE ; // too long ago, probably invalid
aSource = info . mSourceURI ;
* aTime = info . mTimeCreated ;
* aRedirectType = info . mType ;
return PR_TRUE ;
}
return PR_FALSE ;
2006-07-18 16:14:12 +00:00
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::RowToResult
//
2006-07-18 16:19:04 +00:00
// Here, we just have a generic row. It could be a query, URL, visit,
// or full visit.
2006-07-18 15:56:56 +00:00
nsresult
2006-07-18 16:12:34 +00:00
nsNavHistory : : RowToResult ( mozIStorageValueArray * aRow ,
nsNavHistoryQueryOptions * aOptions ,
2006-07-18 15:56:56 +00:00
nsNavHistoryResultNode * * aResult )
{
* aResult = nsnull ;
2006-07-18 16:17:26 +00:00
NS_ASSERTION ( aRow & & aOptions & & aResult , " Null pointer in RowToResult " ) ;
2006-07-18 15:56:56 +00:00
2006-07-18 16:19:04 +00:00
// URL
nsCAutoString url ;
nsresult rv = aRow - > GetUTF8String ( kGetInfoIndex_URL , url ) ;
2006-07-18 16:02:41 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
// title
nsCAutoString title ;
2007-04-25 21:03:29 +00:00
rv = aRow - > GetUTF8String ( kGetInfoIndex_Title , title ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
PRUint32 accessCount = aRow - > AsInt32 ( kGetInfoIndex_VisitCount ) ;
PRTime time = aRow - > AsInt64 ( kGetInfoIndex_VisitDate ) ;
// favicon
nsCAutoString favicon ;
rv = aRow - > GetUTF8String ( kGetInfoIndex_FaviconURL , favicon ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-09 03:54:37 +00:00
// itemId, if any
PRInt64 itemId = - 1 ;
PRBool isNull ;
rv = aRow - > GetIsNull ( kGetInfoIndex_ItemId , & isNull ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! isNull ) {
itemId = aRow - > AsInt64 ( kGetInfoIndex_ItemId ) ;
}
2006-07-18 16:19:04 +00:00
if ( IsQueryURI ( url ) ) {
2006-07-18 16:20:08 +00:00
// special case "place:" URIs: turn them into containers
2008-01-09 03:54:37 +00:00
// We should never expose the history title for query nodes if the
// bookmark-item's title is set to null (the history title may be the
// query string without the place: prefix). Thus we call getItemTitle
// explicitly. Unfortunately, query-bookmarks cannot be distinguished in the
// sql query.
if ( itemId ! = - 1 ) {
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
NS_ENSURE_TRUE ( bookmarks , NS_ERROR_OUT_OF_MEMORY ) ;
rv = bookmarks - > GetItemTitle ( itemId , title ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-11-20 02:01:53 +00:00
rv = QueryRowToResult ( itemId , url , title , accessCount , time , favicon , aResult ) ;
// If it's a simple folder node (i.e. a shortcut to another folder), apply
// our options for it.
if ( * aResult & & ( * aResult ) - > IsFolder ( ) ) {
( * aResult ) - > GetAsContainer ( ) - > mOptions = aOptions ;
}
return rv ;
2006-07-18 16:19:04 +00:00
} else if ( aOptions - > ResultType ( ) = = nsNavHistoryQueryOptions : : RESULTS_AS_URI ) {
2006-07-18 16:22:38 +00:00
* aResult = new nsNavHistoryResultNode ( url , title , accessCount , time ,
favicon ) ;
2008-01-09 03:54:37 +00:00
if ( ! * aResult )
2006-07-18 16:12:34 +00:00
return NS_ERROR_OUT_OF_MEMORY ;
2007-04-25 21:03:29 +00:00
2008-01-09 03:54:37 +00:00
if ( itemId ! = - 1 ) {
( * aResult ) - > mItemId = itemId ;
2007-05-19 00:40:55 +00:00
( * aResult ) - > mDateAdded = aRow - > AsInt64 ( kGetInfoIndex_ItemDateAdded ) ;
( * aResult ) - > mLastModified = aRow - > AsInt64 ( kGetInfoIndex_ItemLastModified ) ;
2007-04-25 21:03:29 +00:00
}
2006-07-18 16:19:04 +00:00
NS_ADDREF ( * aResult ) ;
return NS_OK ;
}
// now we know the result type is some kind of visit (regular or full)
2006-07-18 16:12:34 +00:00
2006-07-18 16:19:04 +00:00
// session
PRInt64 session = aRow - > AsInt64 ( kGetInfoIndex_SessionId ) ;
if ( aOptions - > ResultType ( ) = = nsNavHistoryQueryOptions : : RESULTS_AS_VISIT ) {
2006-07-18 16:22:38 +00:00
* aResult = new nsNavHistoryVisitResultNode ( url , title , accessCount , time ,
favicon , session ) ;
2006-07-18 16:19:04 +00:00
if ( ! * aResult )
return NS_ERROR_OUT_OF_MEMORY ;
NS_ADDREF ( * aResult ) ;
return NS_OK ;
2006-07-18 16:02:41 +00:00
}
2006-07-18 16:19:04 +00:00
// now it had better be a full visit
NS_ASSERTION ( aOptions - > ResultType ( ) = = nsNavHistoryQueryOptions : : RESULTS_AS_FULL_VISIT ,
" Invalid result type in RowToResult " ) ;
NS_NOTREACHED ( " Full visits not supported yet. " ) ;
// visit ID
/*PRUint32 accessCount;
rv = aRow - > GetInt32 ( kGetInfoIndex_VisitCount , & accessCount ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ; */
2006-07-18 15:56:56 +00:00
2006-07-18 16:19:04 +00:00
// referring visit ID
/*PRUint32 accessCount;
rv = aRow - > GetInt32 ( kGetInfoIndex_VisitCount , & accessCount ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ; */
// transition
/*PRUint32 transition;
rv = aRow - > GetInt32 ( kGetInfoIndex_VisitCount , & transition ) ;
2006-07-18 16:08:48 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
* aResult = new nsNavHistoryFullVisitResultNode ( title , accessCount , time ,
favicon , url , session , visitId ,
referring , transition ) ;
if ( ! * aResult )
return NS_ERROR_OUT_OF_MEMORY ;
NS_ADDREF ( * aResult ) ; */
2006-07-18 16:08:48 +00:00
return NS_OK ;
}
2006-07-18 16:12:34 +00:00
2006-07-18 16:20:08 +00:00
// nsNavHistory::QueryRowToResult
//
// Called by RowToResult when the URI is a place: URI to generate the proper
// folder or query node.
nsresult
2007-09-01 21:23:36 +00:00
nsNavHistory : : QueryRowToResult ( PRInt64 itemId , const nsACString & aURI ,
const nsACString & aTitle ,
2006-07-18 16:20:08 +00:00
PRUint32 aAccessCount , PRTime aTime ,
const nsACString & aFavicon ,
nsNavHistoryResultNode * * aNode )
{
nsCOMArray < nsNavHistoryQuery > queries ;
nsCOMPtr < nsNavHistoryQueryOptions > options ;
nsresult rv = QueryStringToQueryArray ( aURI , & queries ,
getter_AddRefs ( options ) ) ;
if ( NS_FAILED ( rv ) ) {
// This was a query that did not parse, what do we do? We don't want to
// return failure since that will kill the whole query process. Instead
// make a query node with the query as a string. This way we have a valid
// node for the user to manipulate that will look like a query, but it will
// never populate since the query string is invalid.
2006-07-18 16:22:38 +00:00
* aNode = new nsNavHistoryQueryResultNode ( aURI , aTitle , aFavicon ) ;
2006-07-18 16:20:08 +00:00
if ( ! * aNode )
return NS_ERROR_OUT_OF_MEMORY ;
NS_ADDREF ( * aNode ) ;
} else {
2007-04-25 21:03:29 +00:00
PRInt64 folderId = GetSimpleBookmarksQueryFolder ( queries , options ) ;
2006-07-18 16:20:08 +00:00
if ( folderId ) {
// simple bookmarks folder, magically generate a bookmarks folder node
nsNavBookmarks * bookmarks = nsNavBookmarks : : GetBookmarksService ( ) ;
NS_ENSURE_TRUE ( bookmarks , NS_ERROR_OUT_OF_MEMORY ) ;
// this addrefs for us
2007-08-02 20:19:44 +00:00
rv = bookmarks - > ResultNodeForContainer ( folderId , options , aNode ) ;
2006-07-18 16:20:08 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2008-01-09 03:54:37 +00:00
// this is the query item-Id, and is what is exposed by node.itemId
( * aNode ) - > GetAsFolder ( ) - > mQueryItemId = itemId ;
// Use the query item title, unless it's void (in that case,
// we keep the concrete folder title set)
if ( ! aTitle . IsVoid ( ) )
( * aNode ) - > mTitle = aTitle ;
2006-07-18 16:20:08 +00:00
} else {
// regular query
* aNode = new nsNavHistoryQueryResultNode ( aTitle , EmptyCString ( ) ,
queries , options ) ;
if ( ! * aNode )
return NS_ERROR_OUT_OF_MEMORY ;
2007-09-01 21:23:36 +00:00
( * aNode ) - > mItemId = itemId ;
2006-07-18 16:20:08 +00:00
NS_ADDREF ( * aNode ) ;
}
}
return NS_OK ;
}
2006-07-18 16:19:04 +00:00
// nsNavHistory::VisitIdToResultNode
//
// Used by the query results to create new nodes on the fly when
// notifications come in. This just creates a node for the given visit ID.
2006-07-18 16:12:34 +00:00
2006-07-18 16:08:48 +00:00
nsresult
2006-07-18 16:19:04 +00:00
nsNavHistory : : VisitIdToResultNode ( PRInt64 visitId ,
nsNavHistoryQueryOptions * aOptions ,
nsNavHistoryResultNode * * aResult )
{
mozIStorageStatement * statement ; // non-owning!
if ( aOptions - > ResultType ( ) = = nsNavHistoryQueryOptions : : RESULTS_AS_VISIT | |
aOptions - > ResultType ( ) = = nsNavHistoryQueryOptions : : RESULTS_AS_FULL_VISIT ) {
// visit query - want exact visit time
statement = mDBVisitToVisitResult ;
} else {
// URL results - want last visit time
statement = mDBVisitToURLResult ;
2006-07-18 16:12:34 +00:00
}
2006-07-18 15:56:56 +00:00
2006-07-18 16:19:04 +00:00
mozStorageStatementScoper scoper ( statement ) ;
nsresult rv = statement - > BindInt64Parameter ( 0 , visitId ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
PRBool hasMore = PR_FALSE ;
rv = statement - > ExecuteStep ( & hasMore ) ;
2006-07-18 15:56:56 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:19:04 +00:00
if ( ! hasMore ) {
NS_NOTREACHED ( " Trying to get a result node for an invalid visit " ) ;
return NS_ERROR_INVALID_ARG ;
2006-07-18 15:56:56 +00:00
}
2006-07-18 16:10:01 +00:00
2006-07-18 16:19:04 +00:00
return RowToResult ( statement , aOptions , aResult ) ;
}
2007-04-25 21:03:29 +00:00
nsresult
nsNavHistory : : BookmarkIdToResultNode ( PRInt64 aBookmarkId , nsNavHistoryQueryOptions * aOptions ,
nsNavHistoryResultNode * * aResult )
{
mozStorageStatementScoper scoper ( mDBBookmarkToUrlResult ) ;
nsresult rv = mDBBookmarkToUrlResult - > BindInt64Parameter ( 0 , aBookmarkId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasMore = PR_FALSE ;
rv = mDBBookmarkToUrlResult - > ExecuteStep ( & hasMore ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! hasMore ) {
NS_NOTREACHED ( " Trying to get a result node for an invalid bookmark identifier " ) ;
return NS_ERROR_INVALID_ARG ;
}
return RowToResult ( mDBBookmarkToUrlResult , aOptions , aResult ) ;
}
2006-07-18 15:56:56 +00:00
// nsNavHistory::TitleForDomain
//
// This computes the title for a given domain. Normally, this is just the
// domain name, but we specially handle empty cases to give you a nice
// localized string.
void
2006-07-18 16:19:04 +00:00
nsNavHistory : : TitleForDomain ( const nsCString & domain , nsACString & aTitle )
2006-07-18 15:56:56 +00:00
{
if ( ! domain . IsEmpty ( ) ) {
aTitle = domain ;
return ;
}
// use the localized one instead
2006-11-21 23:02:16 +00:00
GetStringFromName ( NS_LITERAL_STRING ( " localhost " ) . get ( ) , aTitle ) ;
}
void
nsNavHistory : : GetAgeInDaysString ( PRInt32 aInt , const PRUnichar * aName , nsACString & aResult )
{
nsAutoString intString ;
intString . AppendInt ( aInt ) ;
const PRUnichar * strings [ 1 ] = { intString . get ( ) } ;
2006-07-18 15:56:56 +00:00
nsXPIDLString value ;
2006-11-21 23:02:16 +00:00
nsresult rv = mBundle - > FormatStringFromName ( aName , strings ,
1 , getter_Copies ( value ) ) ;
2006-07-18 15:56:56 +00:00
if ( NS_SUCCEEDED ( rv ) )
2006-11-21 23:02:16 +00:00
CopyUTF16toUTF8 ( value , aResult ) ;
2006-07-18 15:56:56 +00:00
else
2006-11-21 23:02:16 +00:00
aResult . Truncate ( 0 ) ;
2006-07-18 15:56:56 +00:00
}
2006-11-21 23:02:16 +00:00
void
nsNavHistory : : GetStringFromName ( const PRUnichar * aName , nsACString & aResult )
{
nsXPIDLString value ;
nsresult rv = mBundle - > GetStringFromName ( aName , getter_Copies ( value ) ) ;
if ( NS_SUCCEEDED ( rv ) )
CopyUTF16toUTF8 ( value , aResult ) ;
else
aResult . Truncate ( 0 ) ;
}
2006-07-18 15:56:56 +00:00
2006-07-18 16:14:36 +00:00
// nsNavHistory::SetPageTitleInternal
//
2007-07-26 16:23:11 +00:00
// Called to set the title for the given URI. Used as a
// backend for SetTitle.
2006-07-18 16:14:36 +00:00
//
// Will fail for pages that are not in the DB. To clear the corresponding
// title, use aTitle.SetIsVoid(). Sending an empty string will save an
// empty string instead of clearing it.
nsresult
2007-07-26 16:23:11 +00:00
nsNavHistory : : SetPageTitleInternal ( nsIURI * aURI , const nsAString & aTitle )
2006-07-18 16:14:36 +00:00
{
nsresult rv ;
mozStorageTransaction transaction ( mDBConn , PR_TRUE ) ;
// first, make sure the page exists, and fetch the old title (we need the one
// that isn't changing to send notifications)
nsAutoString title ;
{ // scope for statement
mozStorageStatementScoper infoScoper ( mDBGetURLPageInfo ) ;
rv = BindStatementURI ( mDBGetURLPageInfo , 0 , aURI ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasURL = PR_FALSE ;
rv = mDBGetURLPageInfo - > ExecuteStep ( & hasURL ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! hasURL ) {
// we don't have the URL, give up
return NS_ERROR_NOT_AVAILABLE ;
}
// page title
2006-07-18 16:23:27 +00:00
rv = mDBGetURLPageInfo - > GetString ( kGetInfoIndex_Title , title ) ;
2006-07-18 16:14:36 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
// It is actually common to set the title to be the same thing it used to
// be. For example, going to any web page will always cause a title to be set,
// even though it will often be unchanged since the last visit. In these
// cases, we can avoid DB writing and (most significantly) observer overhead.
2007-07-26 16:23:11 +00:00
if ( aTitle . IsVoid ( ) = = title . IsVoid ( ) & &
2006-07-18 16:14:36 +00:00
aTitle = = title )
return NS_OK ;
nsCOMPtr < mozIStorageStatement > dbModStatement ;
2007-07-26 16:23:11 +00:00
title = aTitle ;
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " UPDATE moz_places SET title = ?1 WHERE url = ?2 " ) ,
getter_AddRefs ( dbModStatement ) ) ;
2006-07-18 16:14:36 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// title
if ( aTitle . IsVoid ( ) )
dbModStatement - > BindNullParameter ( 0 ) ;
else
2006-07-18 16:29:14 +00:00
dbModStatement - > BindStringParameter ( 0 , StringHead ( aTitle , HISTORY_TITLE_LENGTH_MAX ) ) ;
2006-07-18 16:14:36 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
// url
rv = BindStatementURI ( dbModStatement , 1 , aURI ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = dbModStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 18:23:38 +00:00
rv = transaction . Commit ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:14:36 +00:00
// observers (have to check first if it's bookmarked)
ENUMERATE_WEAKARRAY ( mObservers , nsINavHistoryObserver ,
2007-07-26 16:23:11 +00:00
OnTitleChanged ( aURI , title ) )
2006-07-18 16:14:36 +00:00
return NS_OK ;
}
2006-07-18 16:28:24 +00:00
nsresult
nsNavHistory : : AddPageWithVisit ( nsIURI * aURI ,
const nsString & aTitle ,
PRBool aHidden , PRBool aTyped ,
PRInt32 aVisitCount ,
PRInt32 aLastVisitTransition ,
PRTime aLastVisitDate )
{
PRBool canAdd = PR_FALSE ;
nsresult rv = CanAddURI ( aURI , & canAdd ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! canAdd ) {
return NS_OK ;
}
PRInt64 pageID ;
2008-01-25 17:11:14 +00:00
// we don't want to calculate frecency here for each uri we import
rv = InternalAddNewPage ( aURI , aTitle , aHidden , aTyped , aVisitCount , PR_FALSE , & pageID ) ;
2006-07-18 16:28:24 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( aLastVisitDate ! = - 1 ) {
PRInt64 visitID ;
rv = InternalAddVisit ( pageID , 0 , 0 ,
aLastVisitDate , aLastVisitTransition , & visitID ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
2007-07-12 23:49:58 +00:00
return NS_OK ;
2006-07-18 16:28:24 +00:00
}
nsresult
nsNavHistory : : RemoveDuplicateURIs ( )
{
2007-11-20 05:14:57 +00:00
// this must be in a transaction because we do related queries
2007-07-12 23:49:58 +00:00
mozStorageTransaction transaction ( mDBConn , PR_FALSE ) ;
2006-07-18 16:28:24 +00:00
2007-11-20 05:14:57 +00:00
// this query chooses an id for every duplicate uris
// this id will be retained while duplicates will be discarded
// total_visit_count is the sum of all duplicate uris visit_count
nsCOMPtr < mozIStorageStatement > selectStatement ;
nsresult rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " SELECT "
" (SELECT h.id FROM moz_places h WHERE h.url=url ORDER BY h.visit_count DESC LIMIT 1), "
" url, SUM(visit_count) "
" FROM moz_places "
" GROUP BY url HAVING( COUNT(url) > 1) " ) ,
getter_AddRefs ( selectStatement ) ) ;
2006-07-18 16:28:24 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 05:14:57 +00:00
// this query remaps history visits to the retained place_id
nsCOMPtr < mozIStorageStatement > updateStatement ;
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING (
" UPDATE moz_historyvisits "
" SET place_id = ?1 "
" WHERE place_id IN (SELECT id FROM moz_places WHERE id <> ?1 AND url = ?2) " ) ,
getter_AddRefs ( updateStatement ) ) ;
2006-07-18 16:28:24 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
2007-11-20 05:14:57 +00:00
// this query remaps bookmarks to the retained place_id
nsCOMPtr < mozIStorageStatement > bookmarkStatement ;
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING (
" UPDATE moz_bookmarks "
" SET fk = ?1 "
" WHERE fk IN (SELECT id FROM moz_places WHERE id <> ?1 AND url = ?2) " ) ,
getter_AddRefs ( bookmarkStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// this query remaps annotations to the retained place_id
nsCOMPtr < mozIStorageStatement > annoStatement ;
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING (
" UPDATE moz_annos "
" SET place_id = ?1 "
" WHERE place_id IN (SELECT id FROM moz_places WHERE id <> ?1 AND url = ?2) " ) ,
getter_AddRefs ( annoStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// this query deletes all duplicate uris except the choosen id
nsCOMPtr < mozIStorageStatement > deleteStatement ;
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " DELETE FROM moz_places WHERE url = ?1 AND id <> ?2 " ) ,
getter_AddRefs ( deleteStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// this query updates visit_count to the sum of all visits
nsCOMPtr < mozIStorageStatement > countStatement ;
rv = mDBConn - > CreateStatement (
NS_LITERAL_CSTRING ( " UPDATE moz_places SET visit_count = ?1 WHERE id = ?2 " ) ,
getter_AddRefs ( countStatement ) ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// for each duplicate uri we update historyvisit and visit_count
PRBool hasMore ;
while ( NS_SUCCEEDED ( selectStatement - > ExecuteStep ( & hasMore ) ) & & hasMore ) {
PRUint64 id = selectStatement - > AsInt64 ( 0 ) ;
nsCAutoString url ;
rv = selectStatement - > GetUTF8String ( 1 , url ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRUint64 visit_count = selectStatement - > AsInt64 ( 2 ) ;
// update historyvisits so they are remapped to the retained uri
rv = updateStatement - > BindInt64Parameter ( 0 , id ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = updateStatement - > BindUTF8StringParameter ( 1 , url ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = updateStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// remap bookmarks to the retained id
rv = bookmarkStatement - > BindInt64Parameter ( 0 , id ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = bookmarkStatement - > BindUTF8StringParameter ( 1 , url ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = bookmarkStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// remap annotations to the retained id
rv = annoStatement - > BindInt64Parameter ( 0 , id ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = annoStatement - > BindUTF8StringParameter ( 1 , url ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = annoStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// remove duplicate uris from moz_places
rv = deleteStatement - > BindUTF8StringParameter ( 0 , url ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = deleteStatement - > BindInt64Parameter ( 1 , id ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = deleteStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// update visit_count to the sum of all visit_count
rv = countStatement - > BindInt64Parameter ( 0 , visit_count ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = countStatement - > BindInt64Parameter ( 1 , id ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = countStatement - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
rv = transaction . Commit ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
2006-07-18 16:28:24 +00:00
}
2006-07-18 15:56:56 +00:00
// Local function **************************************************************
// GetReversedHostname
//
// 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).
nsresult
GetReversedHostname ( nsIURI * aURI , nsAString & aRevHost )
{
nsCString forward8 ;
nsresult rv = aURI - > GetHost ( forward8 ) ;
2006-07-18 16:02:41 +00:00
if ( NS_FAILED ( rv ) ) {
return rv ;
}
2006-07-18 15:56:56 +00:00
// can't do reversing in UTF8, better use 16-bit chars
nsAutoString forward = NS_ConvertUTF8toUTF16 ( forward8 ) ;
GetReversedHostname ( forward , aRevHost ) ;
return NS_OK ;
}
// GetReversedHostname
//
// Same as previous but for strings
void
GetReversedHostname ( const nsString & aForward , nsAString & aRevHost )
{
ReverseString ( aForward , aRevHost ) ;
aRevHost . Append ( PRUnichar ( ' . ' ) ) ;
}
// IsNumericHostName
//
// For host-based groupings, numeric IPs should not be collapsed by the
// last part of the domain name, but should stand alone. This code determines
// if this is the case so we don't collapse "10.1.2.3" to "3". It would be
// nice to use the URL parsing code, but it doesn't give us this information,
// this is usually done by the OS in response to DNS lookups.
//
// This implementation is not perfect, we just check for all numbers and
// digits, and three periods. You could come up with crazy internal host
// names that would fool this logic, but I bet there are no real examples.
2006-07-18 16:19:04 +00:00
PRBool IsNumericHostName ( const nsCString & aHost )
2006-07-18 15:56:56 +00:00
{
2006-07-18 15:59:03 +00:00
PRInt32 periodCount = 0 ;
for ( PRUint32 i = 0 ; i < aHost . Length ( ) ; i + + ) {
2006-07-18 16:04:19 +00:00
PRUnichar cur = aHost [ i ] ;
2006-07-18 16:19:04 +00:00
if ( cur = = ' . ' )
2006-07-18 15:56:56 +00:00
periodCount + + ;
2006-07-18 16:19:04 +00:00
else if ( cur < ' 0 ' | | cur > ' 9 ' )
2006-07-18 15:56:56 +00:00
return PR_FALSE ;
}
return ( periodCount = = 3 ) ;
}
2006-07-18 16:19:04 +00:00
// GetSimpleBookmarksQueryFolder
2006-07-18 16:15:01 +00:00
//
// Determines if this set of queries is a simple bookmarks query for a
// folder with no other constraints. In these common cases, we can more
// efficiently compute the results.
2006-07-18 16:19:04 +00:00
//
2007-10-11 06:42:38 +00:00
// A simple bookmarks query will result in a hierarchical tree of
// bookmark items, folders and separators.
//
2006-07-18 16:19:04 +00:00
// Returns the folder ID if it is a simple folder query, 0 if not.
static PRInt64
2007-04-25 21:03:29 +00:00
GetSimpleBookmarksQueryFolder ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsNavHistoryQueryOptions * aOptions )
2006-07-18 16:15:01 +00:00
{
2006-07-18 16:20:08 +00:00
if ( aQueries . Count ( ) ! = 1 )
2006-07-18 16:19:04 +00:00
return 0 ;
2007-10-11 06:42:38 +00:00
// if there are any groupings, including GROUP_BY_FOLDER, this is not a simple
// bookmarks query
PRUint32 groupCount ;
const PRUint16 * groupings = aOptions - > GroupingMode ( & groupCount ) ;
if ( groupings )
return 0 ;
2006-07-18 16:15:01 +00:00
2006-07-18 16:20:08 +00:00
nsNavHistoryQuery * query = aQueries [ 0 ] ;
2006-07-18 16:19:04 +00:00
if ( query - > Folders ( ) . Length ( ) ! = 1 )
return 0 ;
2006-07-18 16:15:01 +00:00
PRBool hasIt ;
query - > GetHasBeginTime ( & hasIt ) ;
if ( hasIt )
2006-07-18 16:19:04 +00:00
return 0 ;
2006-07-18 16:15:01 +00:00
query - > GetHasEndTime ( & hasIt ) ;
if ( hasIt )
2006-07-18 16:19:04 +00:00
return 0 ;
2006-07-18 16:15:01 +00:00
query - > GetHasDomain ( & hasIt ) ;
if ( hasIt )
2006-07-18 16:19:04 +00:00
return 0 ;
query - > GetHasUri ( & hasIt ) ;
2007-07-21 09:34:22 +00:00
if ( hasIt )
return 0 ;
( void ) query - > GetHasSearchTerms ( & hasIt ) ;
2006-07-18 16:19:04 +00:00
if ( hasIt )
return 0 ;
2007-04-25 21:03:29 +00:00
if ( aOptions - > MaxResults ( ) > 0 )
return 0 ;
2006-07-18 16:19:04 +00:00
// Note that we don't care about the onlyBookmarked flag, if you specify a bookmark
// folder, onlyBookmarked is inferred.
2007-07-21 09:34:22 +00:00
NS_ASSERTION ( query - > Folders ( ) [ 0 ] > 0 , " bad folder id " ) ;
2006-07-18 16:19:04 +00:00
return query - > Folders ( ) [ 0 ] ;
2006-07-18 16:15:01 +00:00
}
2007-09-15 20:07:02 +00:00
// ParseSearchTermsFromQueries
2006-07-18 15:56:56 +00:00
//
2007-09-15 20:07:02 +00:00
// Construct a matrix of search terms from the given queries array.
// All of the query objects are ORed together. Within a query, all the terms
2007-12-05 08:30:07 +00:00
// are ANDed together. See nsINavHistoryService.idl.
2007-09-15 20:07:02 +00:00
//
2007-12-05 08:30:07 +00:00
// This just breaks the query up into words. We don't do anything fancy,
2006-07-18 15:56:56 +00:00
// not even quoting. We do, however, strip quotes, because people might
// try to input quotes expecting them to do something and get no results
// back.
inline PRBool isQueryWhitespace ( PRUnichar ch )
{
return ch = = ' ' ;
}
2007-09-15 20:07:02 +00:00
void ParseSearchTermsFromQueries ( const nsCOMArray < nsNavHistoryQuery > & aQueries ,
nsTArray < nsStringArray * > * aTerms )
2006-07-18 15:56:56 +00:00
{
2006-07-18 15:59:03 +00:00
PRInt32 lastBegin = - 1 ;
2007-09-15 20:07:02 +00:00
for ( PRUint32 i = 0 ; i < aQueries . Count ( ) ; i + + ) {
nsStringArray * queryTerms = new nsStringArray ( ) ;
PRBool hasSearchTerms ;
if ( NS_SUCCEEDED ( aQueries [ i ] - > GetHasSearchTerms ( & hasSearchTerms ) ) & &
hasSearchTerms ) {
const nsString & searchTerms = aQueries [ i ] - > SearchTerms ( ) ;
for ( PRUint32 j = 0 ; j < searchTerms . Length ( ) ; j + + ) {
if ( isQueryWhitespace ( searchTerms [ j ] ) | |
searchTerms [ j ] = = ' " ' ) {
if ( lastBegin > = 0 ) {
// found the end of a word
queryTerms - > AppendString ( Substring ( searchTerms , lastBegin ,
j - lastBegin ) ) ;
lastBegin = - 1 ;
}
} else {
if ( lastBegin < 0 ) {
// found the beginning of a word
lastBegin = j ;
}
}
2006-07-18 15:56:56 +00:00
}
2007-09-15 20:07:02 +00:00
// last word
if ( lastBegin > = 0 )
queryTerms - > AppendString ( Substring ( searchTerms , lastBegin ) ) ;
2006-07-18 15:56:56 +00:00
}
2007-09-15 20:07:02 +00:00
aTerms - > AppendElement ( queryTerms ) ;
2006-07-18 15:56:56 +00:00
}
}
2006-07-18 16:29:14 +00:00
// GenerateTitleFromURI
//
// Given a URL, we try to get a reasonable title for this page. We try
// to use a filename out of the URI, then fall back on the path, then fall
// back on the whole hostname.
nsresult // static
GenerateTitleFromURI ( nsIURI * aURI , nsAString & aTitle )
{
nsCAutoString name ;
nsCOMPtr < nsIURL > url ( do_QueryInterface ( aURI ) ) ;
if ( url )
url - > GetFileName ( name ) ;
if ( name . IsEmpty ( ) ) {
// path
nsresult rv = aURI - > GetPath ( name ) ;
if ( NS_FAILED ( rv ) | | ( name . Length ( ) = = 1 & & name [ 0 ] = = ' / ' ) ) {
// empty path name, use hostname
rv = aURI - > GetHost ( name ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
}
aTitle = NS_ConvertUTF8toUTF16 ( name ) ;
return NS_OK ;
}
2006-07-18 15:56:56 +00:00
// GetTLDCharCount
//
// Given a normal, forward host name ("bugzilla.mozilla.org")
2006-07-18 16:19:04 +00:00
// returns the number of 8-bit characters that the TLD occupies, NOT
// including the trailing dot: bugzilla.mozilla.org -> 3
2006-07-18 15:56:56 +00:00
// theregister.co.uk -> 5
// mysite.us -> 2
PRInt32
2006-07-18 16:19:04 +00:00
GetTLDCharCount ( const nsCString & aHost )
2006-07-18 15:56:56 +00:00
{
2006-07-18 16:19:04 +00:00
nsCAutoString trailing ;
2006-07-18 15:56:56 +00:00
GetSubstringFromNthDot ( aHost , aHost . Length ( ) - 1 , 1 ,
PR_FALSE , trailing ) ;
switch ( GetTLDType ( trailing ) ) {
case 0 :
// not a known TLD
return 0 ;
case 1 :
// first-level TLD
return trailing . Length ( ) ;
case 2 : {
// need to check second level and trim it too (if valid)
2006-07-18 16:19:04 +00:00
nsCAutoString trailingMore ;
2006-07-18 15:56:56 +00:00
GetSubstringFromNthDot ( aHost , aHost . Length ( ) - 1 ,
2 , PR_FALSE , trailingMore ) ;
if ( GetTLDType ( trailingMore ) )
return trailingMore . Length ( ) ;
else
return trailing . Length ( ) ;
}
default :
NS_NOTREACHED ( " Invalid TLD type " ) ;
return 0 ;
}
}
// GetTLDType
//
// Given the last part of a host name, tells you whether this is a known TLD.
// 0 -> not known
// 1 -> known 1st or second level TLD ("com", "co.uk")
// 2 -> end of a two-part TLD ("uk")
//
// If this returns 2, you should probably re-call the function including
// the next level of name. For example ("uk" -> 2, then you call with
// "co.uk" and know that the last two pars of this domain name are
// "toplevel".
//
// This should be moved somewhere else (like cookies) and made easier to
// update.
PRInt32
2006-07-18 16:19:04 +00:00
GetTLDType ( const nsCString & aHostTail )
2006-07-18 15:56:56 +00:00
{
//static nsDataHashtable<nsStringHashKey, int> tldTypes;
if ( ! gTldTypes ) {
// need to populate table
2006-07-18 16:19:04 +00:00
gTldTypes = new nsDataHashtable < nsCStringHashKey , int > ( ) ;
2006-07-18 15:56:56 +00:00
if ( ! gTldTypes )
return 0 ;
gTldTypes - > Init ( 256 ) ;
2006-07-18 16:19:04 +00:00
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " com " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " org " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " net " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " edu " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " gov " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " mil " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " uk " ) , 2 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " co.uk " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " kr " ) , 2 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " co.kr " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " hu " ) , 1 ) ;
gTldTypes - > Put ( NS_LITERAL_CSTRING ( " us " ) , 1 ) ;
2006-07-18 15:56:56 +00:00
// FIXME: add the rest
}
2006-07-18 15:59:03 +00:00
PRInt32 type = 0 ;
2006-07-18 15:56:56 +00:00
if ( gTldTypes - > Get ( aHostTail , & type ) )
return type ;
else
return 0 ;
}
// GetSubstringFromNthDot
//
// Similar to GetSubstringToNthDot except searches backward
// GetSubstringFromNthDot("foo.bar", length, 1, PR_FALSE) -> "bar"
//
// It is legal to pass in a starting position < 0 so you can just
// use Length()-1 as the starting position even if the length is 0.
2006-07-18 16:19:04 +00:00
void GetSubstringFromNthDot ( const nsCString & aInput , PRInt32 aStartingSpot ,
PRInt32 aN , PRBool aIncludeDot , nsACString & aSubstr )
2006-07-18 15:56:56 +00:00
{
2006-07-18 15:59:03 +00:00
PRInt32 dotsFound = 0 ;
for ( PRInt32 i = aStartingSpot ; i > = 0 ; i - - ) {
2006-07-18 15:56:56 +00:00
if ( aInput [ i ] = = ' . ' ) {
dotsFound + + ;
if ( dotsFound = = aN ) {
if ( aIncludeDot )
aSubstr = Substring ( aInput , i , aInput . Length ( ) - i ) ;
else
aSubstr = Substring ( aInput , i + 1 , aInput . Length ( ) - i - 1 ) ;
return ;
}
}
}
aSubstr = aInput ; // no dot found
}
2006-07-18 15:58:11 +00:00
// BindStatementURI
//
// Binds the specified URI as the parameter 'index' for the statment.
// URIs are always bound as UTF8
2006-07-18 15:59:03 +00:00
nsresult BindStatementURI ( mozIStorageStatement * statement , PRInt32 index ,
2006-07-18 15:58:11 +00:00
nsIURI * aURI )
{
2007-06-19 16:34:32 +00:00
NS_ENSURE_ARG_POINTER ( aURI ) ;
2006-07-18 15:58:11 +00:00
nsCAutoString utf8URISpec ;
nsresult rv = aURI - > GetSpec ( utf8URISpec ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
2006-07-18 16:29:14 +00:00
rv = statement - > BindUTF8StringParameter ( index ,
StringHead ( utf8URISpec , HISTORY_URI_LENGTH_MAX ) ) ;
2006-07-18 15:58:11 +00:00
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
2008-01-25 17:11:14 +00:00
nsresult
nsNavHistory : : UpdateFrecency ( PRInt64 aPlaceId , PRBool aIsBookmarked )
{
mozStorageStatementScoper statsScoper ( mDBGetPlaceVisitStats ) ;
nsresult rv = mDBGetPlaceVisitStats - > BindInt64Parameter ( 0 , aPlaceId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool hasResults = PR_FALSE ;
rv = mDBGetPlaceVisitStats - > ExecuteStep ( & hasResults ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
if ( ! hasResults ) {
NS_WARNING ( " attempting to update frecency for a bogus place " ) ;
// before I added the check for itemType == TYPE_BOOKMARK
// I hit this with aPlaceId of 0 (on import)
return NS_OK ;
}
PRInt32 typed = 0 ;
rv = mDBGetPlaceVisitStats - > GetInt32 ( 0 , & typed ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 hidden = 0 ;
rv = mDBGetPlaceVisitStats - > GetInt32 ( 1 , & hidden ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 oldFrecency = 0 ;
rv = mDBGetPlaceVisitStats - > GetInt32 ( 2 , & oldFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 visitCountForFrecency = 0 ;
// because visit_count excludes visit with visit_type NOT IN(0,4)
// we can't use it for calculating frecency, so we must
// calculate it.
rv = CalculateVisitCount ( aPlaceId , PR_TRUE /* for frecency */ ,
& visitCountForFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRInt32 newFrecency = 0 ;
rv = CalculateFrecencyInternal ( aPlaceId , typed , visitCountForFrecency ,
aIsBookmarked , & newFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// save ourselves the UPDATE if the frecency hasn't changed
// One way this can happen is with livemarks.
// when we added the livemark, the frecency was 0.
// On refresh, when we remove and then add the livemark items,
// the frecency (for a given moz_places) will not have changed
// (if we've never visited that place).
if ( newFrecency = = oldFrecency )
return NS_OK ;
mozStorageStatementScoper updateScoper ( mDBUpdateFrecencyAndHidden ) ;
rv = mDBUpdateFrecencyAndHidden - > BindInt64Parameter ( 0 , aPlaceId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBUpdateFrecencyAndHidden - > BindInt32Parameter ( 1 , newFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// if we calculated a non-zero frecency we should unhide this place
// so that previously hidden (non-livebookmark item) bookmarks
// will now appear in autocomplete
// if we calculated a zero frecency, we re-use the old hidden value.
rv = mDBUpdateFrecencyAndHidden - > BindInt32Parameter ( 2 ,
newFrecency ? 0 /* not hidden */ : hidden ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBUpdateFrecencyAndHidden - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
nsresult
nsNavHistory : : CalculateFrecencyInternal ( PRInt64 aPlaceId , PRInt32 aTyped , PRInt32 aVisitCount , PRBool aIsBookmarked , PRInt32 * aFrecency )
{
PRTime normalizedNow = NormalizeTimeRelativeToday ( GetNow ( ) ) ;
float pointsForSampledVisits = 0.0 ;
if ( aPlaceId ! = - 1 ) {
PRInt32 numSampledVisits = 0 ;
mozStorageStatementScoper scoper ( mDBVisitsForFrecency ) ;
nsresult rv = mDBVisitsForFrecency - > BindInt64Parameter ( 0 , aPlaceId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// mDBVisitsForFrecency is limited by the browser.frecency.numVisits pref
PRBool hasMore = PR_FALSE ;
while ( NS_SUCCEEDED ( mDBVisitsForFrecency - > ExecuteStep ( & hasMore ) )
& & hasMore ) {
numSampledVisits + + ;
PRInt32 visitType = mDBVisitsForFrecency - > AsInt32 ( 1 ) ;
PRInt32 bonus = 0 ;
switch ( visitType ) {
case nsINavHistoryService : : TRANSITION_EMBED :
bonus = mEmbedVisitBonus ;
break ;
case nsINavHistoryService : : TRANSITION_LINK :
bonus = mLinkVisitBonus ;
break ;
case nsINavHistoryService : : TRANSITION_TYPED :
bonus = mTypedVisitBonus ;
break ;
case nsINavHistoryService : : TRANSITION_BOOKMARK :
bonus = mBookmarkVisitBonus ;
break ;
case nsINavHistoryService : : TRANSITION_DOWNLOAD :
bonus = mDownloadVisitBonus ;
break ;
case nsINavHistoryService : : TRANSITION_REDIRECT_PERMANENT :
bonus = mPermRedirectVisitBonus ;
break ;
case nsINavHistoryService : : TRANSITION_REDIRECT_TEMPORARY :
bonus = mTempRedirectVisitBonus ;
break ;
default :
// 0 == undefined (see bug #375777 for details)
if ( visitType )
NS_WARNING ( " new transition but no weight for frecency " ) ;
bonus = mDefaultVisitBonus ;
break ;
}
// if bonus was zero, we can skip the work to determine the weight
if ( bonus ) {
PRTime visitDate = mDBVisitsForFrecency - > AsInt64 ( 0 ) ;
PRInt64 ageInDays = GetAgeInDays ( normalizedNow , visitDate ) ;
PRInt32 weight = 0 ;
if ( ageInDays < = mFirstBucketCutoffInDays )
weight = mFirstBucketWeight ;
else if ( ageInDays < = mSecondBucketCutoffInDays )
weight = mSecondBucketWeight ;
else if ( ageInDays < = mThirdBucketCutoffInDays )
weight = mThirdBucketWeight ;
else if ( ageInDays < = mFourthBucketCutoffInDays )
weight = mFourthBucketWeight ;
else
weight = mDefaultWeight ;
pointsForSampledVisits + = weight * ( bonus / 100.0 ) ;
}
}
if ( numSampledVisits ) {
// fix for bug #412219
if ( ! pointsForSampledVisits ) {
// For URIs with zero points in the sampled recent visits
// but "browsing" type visits outside the sampling range,
// set frecency to -1, so that they're still shown in autocomplete.
PRInt32 trueVisitCount = 0 ;
rv = CalculateVisitCount ( aPlaceId , PR_FALSE /* not for frecency */ ,
& trueVisitCount ) ;
if ( NS_SUCCEEDED ( rv ) & & trueVisitCount )
* aFrecency = - 1 ;
else
* aFrecency = 0 ;
}
else {
// Estimate frecency using the last few visits.
// Use NS_ceilf() so that we don't round down to 0, which
// would cause us to completely ignore the place during autocomplete.
* aFrecency = ( PRInt32 ) NS_ceilf ( aVisitCount * NS_ceilf ( pointsForSampledVisits ) / numSampledVisits ) ;
}
# ifdef DEBUG_FRECENCY
printf ( " CalculateFrecency() for place %lld: %d = %d * %f / %d \n " , aPlaceId , * aFrecency , aVisitCount , pointsForSampledVisits , numSampledVisits ) ;
# endif
return NS_OK ;
}
}
// XXX the code below works well for guessing the frecency on import, and we'll correct later once we have
// visits.
// what if we don't have visits and we never visit? we could end up with a really high value
// that keeps coming up in ac results? only do this on import? something to figure out.
PRInt32 bonus = 0 ;
// not the same logic above, as a single visit could not be both
// a bookmark visit and a typed visit. but when estimating a frecency
// for a place that doesn't have any visits, this will make it so
// something bookmarked and typed will have a higher frecency than
// something just typed or just bookmarked.
if ( aIsBookmarked )
bonus + = mUnvisitedBookmarkBonus ;
if ( aTyped )
bonus + = mUnvisitedTypedBonus ;
// assume "now" as our ageInDays, so use the first bucket.
// note, when we recalculate "old frecencies" (see mDBOldFrecencies)
// this frecency value could be off by an order of
// (mFirstBucketWeight / mDefaultBucketWeight)
pointsForSampledVisits = mFirstBucketWeight * ( bonus / ( float ) 100.0 ) ;
// for a unvisited bookmark, produce a non-zero frecency
// so that unvisited bookmarks show up in URL bar autocomplete
if ( ! aVisitCount & & aIsBookmarked )
aVisitCount = 1 ;
// use NS_ceilf() so that we don't round down to 0, which
// would cause us to completely ignore the place during autocomplete
* aFrecency = ( PRInt32 ) NS_ceilf ( aVisitCount * NS_ceilf ( pointsForSampledVisits ) ) ;
# ifdef DEBUG_FRECENCY
printf ( " CalculateFrecency() for unvisited: frecency %d = %f points (b: %d, t: %d) * visit count %d \n " , * aFrecency , pointsForSampledVisits , aIsBookmarked , aTyped , aVisitCount ) ;
# endif
return NS_OK ;
}
nsresult
nsNavHistory : : CalculateFrecency ( PRInt64 aPlaceId , PRInt32 aTyped , PRInt32 aVisitCount , nsCAutoString & aURL , PRInt32 * aFrecency )
{
* aFrecency = 0 ;
nsresult rv ;
nsCOMPtr < nsILivemarkService > lms =
do_GetService ( NS_LIVEMARKSERVICE_CONTRACTID , & rv ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool isBookmark = PR_FALSE ;
// determine if the place is a (non-livemark item) bookmark and prevent
// place: queries from showing up in the URL bar autocomplete results
if ( ! IsQueryURI ( aURL ) & & aPlaceId ! = - 1 ) {
mozStorageStatementScoper scope ( mDBGetBookmarkParentsForPlace ) ;
rv = mDBGetBookmarkParentsForPlace - > BindInt64Parameter ( 0 , aPlaceId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// this query can return multiple parent folders because
// it is possible for the user to bookmark something that
// is also a livemark item
PRBool hasMore = PR_FALSE ;
while ( NS_SUCCEEDED ( mDBGetBookmarkParentsForPlace - > ExecuteStep ( & hasMore ) )
& & hasMore ) {
PRInt64 folderId ;
rv = mDBGetBookmarkParentsForPlace - > GetInt64 ( 0 , & folderId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
PRBool parentIsLivemark ;
rv = lms - > IsLivemark ( folderId , & parentIsLivemark ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// we found one parent that is not a livemark feed, so we can stop
if ( ! parentIsLivemark ) {
isBookmark = PR_TRUE ;
break ;
}
}
}
rv = CalculateFrecencyInternal ( aPlaceId , aTyped , aVisitCount ,
isBookmark , aFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
nsresult
nsNavHistory : : RecalculateFrecencies ( )
{
mozStorageTransaction transaction ( mDBConn , PR_TRUE ) ;
nsresult rv = RecalculateFrecenciesInternal ( mDBInvalidFrecencies , - 1 ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// if the last visit date was "recent" (in the top bucket)
// don't bother recalculating as it was recalculated recently
// and we are looking for "older" places.
PRTime startOfFirstBucket = GetNow ( ) -
( USECS_PER_DAY * ( mFirstBucketCutoffInDays + 1 ) ) ;
rv = RecalculateFrecenciesInternal ( mDBOldFrecencies , startOfFirstBucket ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
return NS_OK ;
}
nsresult
nsNavHistory : : RecalculateFrecenciesInternal ( mozIStorageStatement * aStatement , PRInt64 aBindParameter )
{
nsresult rv ;
mozStorageStatementScoper scoper ( aStatement ) ;
if ( aBindParameter ! = - 1 ) {
rv = aStatement - > BindInt64Parameter ( 0 , aBindParameter ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
PRBool hasMore = PR_FALSE ;
while ( NS_SUCCEEDED ( aStatement - > ExecuteStep ( & hasMore ) ) & & hasMore ) {
PRInt64 placeId = aStatement - > AsInt64 ( 0 ) ;
// for frecency, we don't use visit_count, aStatement->AsInt32(1)
PRInt32 hidden = aStatement - > AsInt32 ( 2 ) ;
PRInt32 typed = aStatement - > AsInt32 ( 3 ) ;
PRInt32 oldFrecency = aStatement - > AsInt32 ( 4 ) ;
nsCAutoString url ;
aStatement - > GetUTF8String ( 5 , url ) ;
PRInt32 newFrecency = 0 ;
PRInt32 visitCountForFrecency = 0 ;
// because visit_count excludes visit with visit_type NOT IN(0,4)
// we can't use it for calculating frecency so we must calculate it.
rv = CalculateVisitCount ( placeId , PR_TRUE /* for frecency */ ,
& visitCountForFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = CalculateFrecency ( placeId , typed , visitCountForFrecency ,
url , & newFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// save ourselves the UPDATE if the frecency hasn't changed
if ( newFrecency = = oldFrecency )
continue ;
mozStorageStatementScoper updateScoper ( mDBUpdateFrecencyAndHidden ) ;
rv = mDBUpdateFrecencyAndHidden - > BindInt64Parameter ( 0 , placeId ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBUpdateFrecencyAndHidden - > BindInt32Parameter ( 1 , newFrecency ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
// if we calculated a non-zero frecency we should unhide this place
// so that previously hidden (non-livebookmark items) bookmarks
// will now appear in autocomplete. if we calculated a zero frecency,
// we re-use the old hidden value.
rv = mDBUpdateFrecencyAndHidden - > BindInt32Parameter ( 2 ,
newFrecency ? 0 /* not hidden */ : hidden ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
rv = mDBUpdateFrecencyAndHidden - > Execute ( ) ;
NS_ENSURE_SUCCESS ( rv , rv ) ;
}
return NS_OK ;
}