michael@0: //* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: michael@0: #include "nsNavHistory.h" michael@0: michael@0: #include "mozIPlacesAutoComplete.h" michael@0: #include "nsNavBookmarks.h" michael@0: #include "nsAnnotationService.h" michael@0: #include "nsFaviconService.h" michael@0: #include "nsPlacesMacros.h" michael@0: #include "History.h" michael@0: #include "Helpers.h" michael@0: michael@0: #include "nsTArray.h" michael@0: #include "nsCollationCID.h" michael@0: #include "nsILocaleService.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsPrintfCString.h" michael@0: #include "nsPromiseFlatString.h" michael@0: #include "nsString.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "prsystem.h" michael@0: #include "prtime.h" michael@0: #include "nsEscape.h" michael@0: #include "nsIEffectiveTLDService.h" michael@0: #include "nsIClassInfoImpl.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsAppDirectoryServiceDefs.h" michael@0: #include "nsMathUtils.h" michael@0: #include "mozilla/storage.h" michael@0: #include "mozilla/Preferences.h" michael@0: #include michael@0: michael@0: #ifdef MOZ_XUL michael@0: #include "nsIAutoCompleteInput.h" michael@0: #include "nsIAutoCompletePopup.h" michael@0: #endif michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::places; michael@0: michael@0: // The maximum number of things that we will store in the recent events list michael@0: // before calling ExpireNonrecentEvents. This number should be big enough so it michael@0: // is very difficult to get that many unconsumed events (for example, typed but michael@0: // never visited) in the RECENT_EVENT_THRESHOLD. Otherwise, we'll start michael@0: // checking each one for every page visit, which will be somewhat slower. michael@0: #define RECENT_EVENT_QUEUE_MAX_LENGTH 128 michael@0: michael@0: // preference ID strings michael@0: #define PREF_HISTORY_ENABLED "places.history.enabled" michael@0: michael@0: #define PREF_FREC_NUM_VISITS "places.frecency.numVisits" michael@0: #define PREF_FREC_NUM_VISITS_DEF 10 michael@0: #define PREF_FREC_FIRST_BUCKET_CUTOFF "places.frecency.firstBucketCutoff" michael@0: #define PREF_FREC_FIRST_BUCKET_CUTOFF_DEF 4 michael@0: #define PREF_FREC_SECOND_BUCKET_CUTOFF "places.frecency.secondBucketCutoff" michael@0: #define PREF_FREC_SECOND_BUCKET_CUTOFF_DEF 14 michael@0: #define PREF_FREC_THIRD_BUCKET_CUTOFF "places.frecency.thirdBucketCutoff" michael@0: #define PREF_FREC_THIRD_BUCKET_CUTOFF_DEF 31 michael@0: #define PREF_FREC_FOURTH_BUCKET_CUTOFF "places.frecency.fourthBucketCutoff" michael@0: #define PREF_FREC_FOURTH_BUCKET_CUTOFF_DEF 90 michael@0: #define PREF_FREC_FIRST_BUCKET_WEIGHT "places.frecency.firstBucketWeight" michael@0: #define PREF_FREC_FIRST_BUCKET_WEIGHT_DEF 100 michael@0: #define PREF_FREC_SECOND_BUCKET_WEIGHT "places.frecency.secondBucketWeight" michael@0: #define PREF_FREC_SECOND_BUCKET_WEIGHT_DEF 70 michael@0: #define PREF_FREC_THIRD_BUCKET_WEIGHT "places.frecency.thirdBucketWeight" michael@0: #define PREF_FREC_THIRD_BUCKET_WEIGHT_DEF 50 michael@0: #define PREF_FREC_FOURTH_BUCKET_WEIGHT "places.frecency.fourthBucketWeight" michael@0: #define PREF_FREC_FOURTH_BUCKET_WEIGHT_DEF 30 michael@0: #define PREF_FREC_DEFAULT_BUCKET_WEIGHT "places.frecency.defaultBucketWeight" michael@0: #define PREF_FREC_DEFAULT_BUCKET_WEIGHT_DEF 10 michael@0: #define PREF_FREC_EMBED_VISIT_BONUS "places.frecency.embedVisitBonus" michael@0: #define PREF_FREC_EMBED_VISIT_BONUS_DEF 0 michael@0: #define PREF_FREC_FRAMED_LINK_VISIT_BONUS "places.frecency.framedLinkVisitBonus" michael@0: #define PREF_FREC_FRAMED_LINK_VISIT_BONUS_DEF 0 michael@0: #define PREF_FREC_LINK_VISIT_BONUS "places.frecency.linkVisitBonus" michael@0: #define PREF_FREC_LINK_VISIT_BONUS_DEF 100 michael@0: #define PREF_FREC_TYPED_VISIT_BONUS "places.frecency.typedVisitBonus" michael@0: #define PREF_FREC_TYPED_VISIT_BONUS_DEF 2000 michael@0: #define PREF_FREC_BOOKMARK_VISIT_BONUS "places.frecency.bookmarkVisitBonus" michael@0: #define PREF_FREC_BOOKMARK_VISIT_BONUS_DEF 75 michael@0: #define PREF_FREC_DOWNLOAD_VISIT_BONUS "places.frecency.downloadVisitBonus" michael@0: #define PREF_FREC_DOWNLOAD_VISIT_BONUS_DEF 0 michael@0: #define PREF_FREC_PERM_REDIRECT_VISIT_BONUS "places.frecency.permRedirectVisitBonus" michael@0: #define PREF_FREC_PERM_REDIRECT_VISIT_BONUS_DEF 0 michael@0: #define PREF_FREC_TEMP_REDIRECT_VISIT_BONUS "places.frecency.tempRedirectVisitBonus" michael@0: #define PREF_FREC_TEMP_REDIRECT_VISIT_BONUS_DEF 0 michael@0: #define PREF_FREC_DEFAULT_VISIT_BONUS "places.frecency.defaultVisitBonus" michael@0: #define PREF_FREC_DEFAULT_VISIT_BONUS_DEF 0 michael@0: #define PREF_FREC_UNVISITED_BOOKMARK_BONUS "places.frecency.unvisitedBookmarkBonus" michael@0: #define PREF_FREC_UNVISITED_BOOKMARK_BONUS_DEF 140 michael@0: #define PREF_FREC_UNVISITED_TYPED_BONUS "places.frecency.unvisitedTypedBonus" michael@0: #define PREF_FREC_UNVISITED_TYPED_BONUS_DEF 200 michael@0: michael@0: // In order to avoid calling PR_now() too often we use a cached "now" value michael@0: // for repeating stuff. These are milliseconds between "now" cache refreshes. michael@0: #define RENEW_CACHED_NOW_TIMEOUT ((int32_t)3 * PR_MSEC_PER_SEC) michael@0: michael@0: static const int64_t USECS_PER_DAY = (int64_t)PR_USEC_PER_SEC * 60 * 60 * 24; michael@0: michael@0: // character-set annotation michael@0: #define CHARSET_ANNO NS_LITERAL_CSTRING("URIProperties/characterSet") michael@0: michael@0: // These macros are used when splitting history by date. michael@0: // These are the day containers and catch-all final container. michael@0: #define HISTORY_ADDITIONAL_DATE_CONT_NUM 3 michael@0: // We use a guess of the number of months considering all of them 30 days michael@0: // long, but we split only the last 6 months. michael@0: #define HISTORY_DATE_CONT_NUM(_daysFromOldestVisit) \ michael@0: (HISTORY_ADDITIONAL_DATE_CONT_NUM + \ michael@0: std::min(6, (int32_t)ceilf((float)_daysFromOldestVisit/30))) michael@0: // Max number of containers, used to initialize the params hash. michael@0: #define HISTORY_DATE_CONT_MAX 10 michael@0: michael@0: // Initial size of the embed visits cache. michael@0: #define EMBED_VISITS_INITIAL_CACHE_SIZE 128 michael@0: michael@0: // Initial size of the recent events caches. michael@0: #define RECENT_EVENTS_INITIAL_CACHE_SIZE 128 michael@0: michael@0: // Observed topics. michael@0: #ifdef MOZ_XUL michael@0: #define TOPIC_AUTOCOMPLETE_FEEDBACK_INCOMING "autocomplete-will-enter-text" michael@0: #endif michael@0: #define TOPIC_IDLE_DAILY "idle-daily" michael@0: #define TOPIC_PREF_CHANGED "nsPref:changed" michael@0: #define TOPIC_PROFILE_TEARDOWN "profile-change-teardown" michael@0: #define TOPIC_PROFILE_CHANGE "profile-before-change" michael@0: michael@0: static const char* kObservedPrefs[] = { michael@0: PREF_HISTORY_ENABLED michael@0: , PREF_FREC_NUM_VISITS michael@0: , PREF_FREC_FIRST_BUCKET_CUTOFF michael@0: , PREF_FREC_SECOND_BUCKET_CUTOFF michael@0: , PREF_FREC_THIRD_BUCKET_CUTOFF michael@0: , PREF_FREC_FOURTH_BUCKET_CUTOFF michael@0: , PREF_FREC_FIRST_BUCKET_WEIGHT michael@0: , PREF_FREC_SECOND_BUCKET_WEIGHT michael@0: , PREF_FREC_THIRD_BUCKET_WEIGHT michael@0: , PREF_FREC_FOURTH_BUCKET_WEIGHT michael@0: , PREF_FREC_DEFAULT_BUCKET_WEIGHT michael@0: , PREF_FREC_EMBED_VISIT_BONUS michael@0: , PREF_FREC_FRAMED_LINK_VISIT_BONUS michael@0: , PREF_FREC_LINK_VISIT_BONUS michael@0: , PREF_FREC_TYPED_VISIT_BONUS michael@0: , PREF_FREC_BOOKMARK_VISIT_BONUS michael@0: , PREF_FREC_DOWNLOAD_VISIT_BONUS michael@0: , PREF_FREC_PERM_REDIRECT_VISIT_BONUS michael@0: , PREF_FREC_TEMP_REDIRECT_VISIT_BONUS michael@0: , PREF_FREC_DEFAULT_VISIT_BONUS michael@0: , PREF_FREC_UNVISITED_BOOKMARK_BONUS michael@0: , PREF_FREC_UNVISITED_TYPED_BONUS michael@0: , nullptr michael@0: }; michael@0: michael@0: NS_IMPL_ADDREF(nsNavHistory) michael@0: NS_IMPL_RELEASE(nsNavHistory) michael@0: michael@0: NS_IMPL_CLASSINFO(nsNavHistory, nullptr, nsIClassInfo::SINGLETON, michael@0: NS_NAVHISTORYSERVICE_CID) michael@0: NS_INTERFACE_MAP_BEGIN(nsNavHistory) michael@0: NS_INTERFACE_MAP_ENTRY(nsINavHistoryService) michael@0: NS_INTERFACE_MAP_ENTRY(nsIBrowserHistory) michael@0: NS_INTERFACE_MAP_ENTRY(nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_ENTRY(nsPIPlacesDatabase) michael@0: NS_INTERFACE_MAP_ENTRY(nsPIPlacesHistoryListenersNotifier) michael@0: NS_INTERFACE_MAP_ENTRY(mozIStorageVacuumParticipant) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsINavHistoryService) michael@0: NS_IMPL_QUERY_CLASSINFO(nsNavHistory) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: // We don't care about flattening everything michael@0: NS_IMPL_CI_INTERFACE_GETTER(nsNavHistory, michael@0: nsINavHistoryService, michael@0: nsIBrowserHistory) michael@0: michael@0: namespace { michael@0: michael@0: static int64_t GetSimpleBookmarksQueryFolder( michael@0: const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions* aOptions); michael@0: static void ParseSearchTermsFromQueries(const nsCOMArray& aQueries, michael@0: nsTArray*>* aTerms); michael@0: michael@0: void GetTagsSqlFragment(int64_t aTagsFolder, michael@0: const nsACString& aRelation, michael@0: bool aHasSearchTerms, michael@0: nsACString& _sqlFragment) { michael@0: if (!aHasSearchTerms) michael@0: _sqlFragment.AssignLiteral("null"); michael@0: else { michael@0: // This subquery DOES NOT order tags for performance reasons. michael@0: _sqlFragment.Assign(NS_LITERAL_CSTRING( michael@0: "(SELECT GROUP_CONCAT(t_t.title, ',') " michael@0: "FROM moz_bookmarks b_t " michael@0: "JOIN moz_bookmarks t_t ON t_t.id = +b_t.parent " michael@0: "WHERE b_t.fk = ") + aRelation + NS_LITERAL_CSTRING(" " michael@0: "AND t_t.parent = ") + michael@0: nsPrintfCString("%lld", aTagsFolder) + NS_LITERAL_CSTRING(" " michael@0: ")")); michael@0: } michael@0: michael@0: _sqlFragment.AppendLiteral(" AS tags "); michael@0: } michael@0: michael@0: /** michael@0: * This class sets begin/end of batch updates to correspond to C++ scopes so michael@0: * we can be sure end always gets called. michael@0: */ michael@0: class UpdateBatchScoper michael@0: { michael@0: public: michael@0: UpdateBatchScoper(nsNavHistory& aNavHistory) : mNavHistory(aNavHistory) michael@0: { michael@0: mNavHistory.BeginUpdateBatch(); michael@0: } michael@0: ~UpdateBatchScoper() michael@0: { michael@0: mNavHistory.EndUpdateBatch(); michael@0: } michael@0: protected: michael@0: nsNavHistory& mNavHistory; michael@0: }; michael@0: michael@0: } // anonymouse namespace michael@0: michael@0: michael@0: // Queries rows indexes to bind or get values, if adding a new one, be sure to michael@0: // update nsNavBookmarks statements and its kGetChildrenIndex_* constants michael@0: const int32_t nsNavHistory::kGetInfoIndex_PageID = 0; michael@0: const int32_t nsNavHistory::kGetInfoIndex_URL = 1; michael@0: const int32_t nsNavHistory::kGetInfoIndex_Title = 2; michael@0: const int32_t nsNavHistory::kGetInfoIndex_RevHost = 3; michael@0: const int32_t nsNavHistory::kGetInfoIndex_VisitCount = 4; michael@0: const int32_t nsNavHistory::kGetInfoIndex_VisitDate = 5; michael@0: const int32_t nsNavHistory::kGetInfoIndex_FaviconURL = 6; michael@0: const int32_t nsNavHistory::kGetInfoIndex_ItemId = 7; michael@0: const int32_t nsNavHistory::kGetInfoIndex_ItemDateAdded = 8; michael@0: const int32_t nsNavHistory::kGetInfoIndex_ItemLastModified = 9; michael@0: const int32_t nsNavHistory::kGetInfoIndex_ItemParentId = 10; michael@0: const int32_t nsNavHistory::kGetInfoIndex_ItemTags = 11; michael@0: const int32_t nsNavHistory::kGetInfoIndex_Frecency = 12; michael@0: const int32_t nsNavHistory::kGetInfoIndex_Hidden = 13; michael@0: const int32_t nsNavHistory::kGetInfoIndex_Guid = 14; michael@0: michael@0: PLACES_FACTORY_SINGLETON_IMPLEMENTATION(nsNavHistory, gHistoryService) michael@0: michael@0: michael@0: nsNavHistory::nsNavHistory() michael@0: : mBatchLevel(0) michael@0: , mBatchDBTransaction(nullptr) michael@0: , mCachedNow(0) michael@0: , mRecentTyped(RECENT_EVENTS_INITIAL_CACHE_SIZE) michael@0: , mRecentLink(RECENT_EVENTS_INITIAL_CACHE_SIZE) michael@0: , mRecentBookmark(RECENT_EVENTS_INITIAL_CACHE_SIZE) michael@0: , mEmbedVisits(EMBED_VISITS_INITIAL_CACHE_SIZE) michael@0: , mHistoryEnabled(true) michael@0: , mNumVisitsForFrecency(10) michael@0: , mTagsFolder(-1) michael@0: , mDaysOfHistory(-1) michael@0: , mLastCachedStartOfDay(INT64_MAX) michael@0: , mLastCachedEndOfDay(0) michael@0: , mCanNotify(true) michael@0: , mCacheObservers("history-observers") michael@0: { michael@0: NS_ASSERTION(!gHistoryService, michael@0: "Attempting to create two instances of the service!"); michael@0: gHistoryService = this; michael@0: } michael@0: michael@0: michael@0: nsNavHistory::~nsNavHistory() michael@0: { michael@0: // remove the static reference to the service. Check to make sure its us michael@0: // in case somebody creates an extra instance of the service. michael@0: NS_ASSERTION(gHistoryService == this, michael@0: "Deleting a non-singleton instance of the service"); michael@0: if (gHistoryService == this) michael@0: gHistoryService = nullptr; michael@0: } michael@0: michael@0: michael@0: nsresult michael@0: nsNavHistory::Init() michael@0: { michael@0: LoadPrefs(); michael@0: michael@0: mDB = Database::GetDatabase(); michael@0: NS_ENSURE_STATE(mDB); michael@0: michael@0: /***************************************************************************** michael@0: *** IMPORTANT NOTICE! michael@0: *** michael@0: *** Nothing after these add observer calls should return anything but NS_OK. michael@0: *** If a failure code is returned, this nsNavHistory object will be held onto michael@0: *** by the observer service and the preference service. michael@0: ****************************************************************************/ michael@0: michael@0: // Observe preferences changes. michael@0: Preferences::AddWeakObservers(this, kObservedPrefs); michael@0: michael@0: nsCOMPtr obsSvc = services::GetObserverService(); michael@0: if (obsSvc) { michael@0: (void)obsSvc->AddObserver(this, TOPIC_PLACES_CONNECTION_CLOSED, true); michael@0: (void)obsSvc->AddObserver(this, TOPIC_IDLE_DAILY, true); michael@0: #ifdef MOZ_XUL michael@0: (void)obsSvc->AddObserver(this, TOPIC_AUTOCOMPLETE_FEEDBACK_INCOMING, true); michael@0: #endif michael@0: } michael@0: michael@0: // Don't add code that can fail here! Do it up above, before we add our michael@0: // observers. michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetDatabaseStatus(uint16_t *aDatabaseStatus) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aDatabaseStatus); michael@0: *aDatabaseStatus = mDB->GetDatabaseStatus(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: uint32_t michael@0: nsNavHistory::GetRecentFlags(nsIURI *aURI) michael@0: { michael@0: uint32_t result = 0; michael@0: nsAutoCString spec; michael@0: nsresult rv = aURI->GetSpec(spec); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Unable to get aURI's spec"); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: if (CheckIsRecentEvent(&mRecentTyped, spec)) michael@0: result |= RECENT_TYPED; michael@0: if (CheckIsRecentEvent(&mRecentLink, spec)) michael@0: result |= RECENT_ACTIVATED; michael@0: if (CheckIsRecentEvent(&mRecentBookmark, spec)) michael@0: result |= RECENT_BOOKMARKED; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsresult michael@0: nsNavHistory::GetIdForPage(nsIURI* aURI, michael@0: int64_t* _pageId, michael@0: nsCString& _GUID) michael@0: { michael@0: *_pageId = 0; michael@0: michael@0: nsCOMPtr stmt = mDB->GetStatement( michael@0: "SELECT id, url, title, rev_host, visit_count, guid " michael@0: "FROM moz_places " michael@0: "WHERE url = :page_url " michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), aURI); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasEntry = false; michael@0: rv = stmt->ExecuteStep(&hasEntry); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (hasEntry) { michael@0: rv = stmt->GetInt64(0, _pageId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = stmt->GetUTF8String(5, _GUID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsNavHistory::GetOrCreateIdForPage(nsIURI* aURI, michael@0: int64_t* _pageId, michael@0: nsCString& _GUID) michael@0: { michael@0: nsresult rv = GetIdForPage(aURI, _pageId, _GUID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (*_pageId != 0) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Create a new hidden, untyped and unvisited entry. michael@0: nsCOMPtr stmt = mDB->GetStatement( michael@0: "INSERT OR IGNORE INTO moz_places (url, rev_host, hidden, frecency, guid) " michael@0: "VALUES (:page_url, :rev_host, :hidden, :frecency, GENERATE_GUID()) " michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), aURI); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: // host (reversed with trailing period) michael@0: nsAutoString revHost; michael@0: rv = GetReversedHostname(aURI, revHost); michael@0: // Not all URI types have hostnames, so this is optional. michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = stmt->BindStringByName(NS_LITERAL_CSTRING("rev_host"), revHost); michael@0: } else { michael@0: rv = stmt->BindNullByName(NS_LITERAL_CSTRING("rev_host")); michael@0: } michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("hidden"), 1); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nsAutoCString spec; michael@0: rv = aURI->GetSpec(spec); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("frecency"), michael@0: IsQueryURI(spec) ? 0 : -1); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = stmt->Execute(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: { michael@0: nsCOMPtr getIdStmt = mDB->GetStatement( michael@0: "SELECT id, guid FROM moz_places WHERE url = :page_url " michael@0: ); michael@0: NS_ENSURE_STATE(getIdStmt); michael@0: mozStorageStatementScoper getIdScoper(getIdStmt); michael@0: michael@0: rv = URIBinder::Bind(getIdStmt, NS_LITERAL_CSTRING("page_url"), aURI); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasResult = false; michael@0: rv = getIdStmt->ExecuteStep(&hasResult); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: NS_ASSERTION(hasResult, "hasResult is false but the call succeeded?"); michael@0: *_pageId = getIdStmt->AsInt64(0); michael@0: rv = getIdStmt->GetUTF8String(1, _GUID); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: void michael@0: nsNavHistory::LoadPrefs() michael@0: { michael@0: // History preferences. michael@0: mHistoryEnabled = Preferences::GetBool(PREF_HISTORY_ENABLED, true); michael@0: michael@0: // Frecency preferences. michael@0: #define FRECENCY_PREF(_prop, _pref) \ michael@0: _prop = Preferences::GetInt(_pref, _pref##_DEF) michael@0: michael@0: FRECENCY_PREF(mNumVisitsForFrecency, PREF_FREC_NUM_VISITS); michael@0: FRECENCY_PREF(mFirstBucketCutoffInDays, PREF_FREC_FIRST_BUCKET_CUTOFF); michael@0: FRECENCY_PREF(mSecondBucketCutoffInDays, PREF_FREC_SECOND_BUCKET_CUTOFF); michael@0: FRECENCY_PREF(mThirdBucketCutoffInDays, PREF_FREC_THIRD_BUCKET_CUTOFF); michael@0: FRECENCY_PREF(mFourthBucketCutoffInDays, PREF_FREC_FOURTH_BUCKET_CUTOFF); michael@0: FRECENCY_PREF(mEmbedVisitBonus, PREF_FREC_EMBED_VISIT_BONUS); michael@0: FRECENCY_PREF(mFramedLinkVisitBonus, PREF_FREC_FRAMED_LINK_VISIT_BONUS); michael@0: FRECENCY_PREF(mLinkVisitBonus, PREF_FREC_LINK_VISIT_BONUS); michael@0: FRECENCY_PREF(mTypedVisitBonus, PREF_FREC_TYPED_VISIT_BONUS); michael@0: FRECENCY_PREF(mBookmarkVisitBonus, PREF_FREC_BOOKMARK_VISIT_BONUS); michael@0: FRECENCY_PREF(mDownloadVisitBonus, PREF_FREC_DOWNLOAD_VISIT_BONUS); michael@0: FRECENCY_PREF(mPermRedirectVisitBonus, PREF_FREC_PERM_REDIRECT_VISIT_BONUS); michael@0: FRECENCY_PREF(mTempRedirectVisitBonus, PREF_FREC_TEMP_REDIRECT_VISIT_BONUS); michael@0: FRECENCY_PREF(mDefaultVisitBonus, PREF_FREC_DEFAULT_VISIT_BONUS); michael@0: FRECENCY_PREF(mUnvisitedBookmarkBonus, PREF_FREC_UNVISITED_BOOKMARK_BONUS); michael@0: FRECENCY_PREF(mUnvisitedTypedBonus, PREF_FREC_UNVISITED_TYPED_BONUS); michael@0: FRECENCY_PREF(mFirstBucketWeight, PREF_FREC_FIRST_BUCKET_WEIGHT); michael@0: FRECENCY_PREF(mSecondBucketWeight, PREF_FREC_SECOND_BUCKET_WEIGHT); michael@0: FRECENCY_PREF(mThirdBucketWeight, PREF_FREC_THIRD_BUCKET_WEIGHT); michael@0: FRECENCY_PREF(mFourthBucketWeight, PREF_FREC_FOURTH_BUCKET_WEIGHT); michael@0: FRECENCY_PREF(mDefaultWeight, PREF_FREC_DEFAULT_BUCKET_WEIGHT); michael@0: michael@0: #undef FRECENCY_PREF michael@0: } michael@0: michael@0: michael@0: void michael@0: nsNavHistory::NotifyOnVisit(nsIURI* aURI, michael@0: int64_t aVisitID, michael@0: PRTime aTime, michael@0: int64_t referringVisitID, michael@0: int32_t aTransitionType, michael@0: const nsACString& aGUID, michael@0: bool aHidden) michael@0: { michael@0: MOZ_ASSERT(!aGUID.IsEmpty()); michael@0: // If there's no history, this visit will surely add a day. If the visit is michael@0: // added before or after the last cached day, the day count may have changed. michael@0: // Otherwise adding multiple visits in the same day should not invalidate michael@0: // the cache. michael@0: if (mDaysOfHistory == 0) { michael@0: mDaysOfHistory = 1; michael@0: } else if (aTime > mLastCachedEndOfDay || aTime < mLastCachedStartOfDay) { michael@0: mDaysOfHistory = -1; michael@0: } michael@0: michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnVisit(aURI, aVisitID, aTime, 0, michael@0: referringVisitID, aTransitionType, aGUID, aHidden)); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::NotifyTitleChange(nsIURI* aURI, michael@0: const nsString& aTitle, michael@0: const nsACString& aGUID) michael@0: { michael@0: MOZ_ASSERT(!aGUID.IsEmpty()); michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, OnTitleChanged(aURI, aTitle, aGUID)); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::NotifyFrecencyChanged(nsIURI* aURI, michael@0: int32_t aNewFrecency, michael@0: const nsACString& aGUID, michael@0: bool aHidden, michael@0: PRTime aLastVisitDate) michael@0: { michael@0: MOZ_ASSERT(!aGUID.IsEmpty()); michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnFrecencyChanged(aURI, aNewFrecency, aGUID, aHidden, michael@0: aLastVisitDate)); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::NotifyManyFrecenciesChanged() michael@0: { michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnManyFrecenciesChanged()); michael@0: } michael@0: michael@0: namespace { michael@0: michael@0: class FrecencyNotification : public nsRunnable michael@0: { michael@0: public: michael@0: FrecencyNotification(const nsACString& aSpec, michael@0: int32_t aNewFrecency, michael@0: const nsACString& aGUID, michael@0: bool aHidden, michael@0: PRTime aLastVisitDate) michael@0: : mSpec(aSpec) michael@0: , mNewFrecency(aNewFrecency) michael@0: , mGUID(aGUID) michael@0: , mHidden(aHidden) michael@0: , mLastVisitDate(aLastVisitDate) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD Run() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Must be called on the main thread"); michael@0: nsNavHistory* navHistory = nsNavHistory::GetHistoryService(); michael@0: if (navHistory) { michael@0: nsCOMPtr uri; michael@0: (void)NS_NewURI(getter_AddRefs(uri), mSpec); michael@0: navHistory->NotifyFrecencyChanged(uri, mNewFrecency, mGUID, mHidden, michael@0: mLastVisitDate); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsCString mSpec; michael@0: int32_t mNewFrecency; michael@0: nsCString mGUID; michael@0: bool mHidden; michael@0: PRTime mLastVisitDate; michael@0: }; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: void michael@0: nsNavHistory::DispatchFrecencyChangedNotification(const nsACString& aSpec, michael@0: int32_t aNewFrecency, michael@0: const nsACString& aGUID, michael@0: bool aHidden, michael@0: PRTime aLastVisitDate) const michael@0: { michael@0: nsCOMPtr notif = new FrecencyNotification(aSpec, aNewFrecency, michael@0: aGUID, aHidden, michael@0: aLastVisitDate); michael@0: (void)NS_DispatchToMainThread(notif); michael@0: } michael@0: michael@0: int32_t michael@0: nsNavHistory::GetDaysOfHistory() { michael@0: MOZ_ASSERT(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: if (mDaysOfHistory != -1) michael@0: return mDaysOfHistory; michael@0: michael@0: // SQLite doesn't have a CEIL() function, so we must do that later. michael@0: // We should also take into account timers resolution, that may be as bad as michael@0: // 16ms on Windows, so in some cases the difference may be 0, if the michael@0: // check is done near the visit. Thus remember to check for NULL separately. michael@0: nsCOMPtr stmt = mDB->GetStatement( michael@0: "SELECT CAST(( " michael@0: "strftime('%s','now','localtime','utc') - " michael@0: "(SELECT MIN(visit_date)/1000000 FROM moz_historyvisits) " michael@0: ") AS DOUBLE) " michael@0: "/86400, " michael@0: "strftime('%s','now','localtime','+1 day','start of day','utc') * 1000000" michael@0: ); michael@0: NS_ENSURE_TRUE(stmt, 0); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: bool hasResult; michael@0: if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) { michael@0: // If we get NULL, then there are no visits, otherwise there must always be michael@0: // at least 1 day of history. michael@0: bool hasNoVisits; michael@0: (void)stmt->GetIsNull(0, &hasNoVisits); michael@0: mDaysOfHistory = hasNoVisits ? michael@0: 0 : std::max(1, static_cast(ceil(stmt->AsDouble(0)))); michael@0: mLastCachedStartOfDay = michael@0: NormalizeTime(nsINavHistoryQuery::TIME_RELATIVE_TODAY, 0); michael@0: mLastCachedEndOfDay = stmt->AsInt64(1) - 1; // Start of tomorrow - 1. michael@0: } michael@0: michael@0: return mDaysOfHistory; michael@0: } michael@0: michael@0: PRTime michael@0: nsNavHistory::GetNow() michael@0: { michael@0: if (!mCachedNow) { michael@0: mCachedNow = PR_Now(); michael@0: if (!mExpireNowTimer) michael@0: mExpireNowTimer = do_CreateInstance("@mozilla.org/timer;1"); michael@0: if (mExpireNowTimer) michael@0: mExpireNowTimer->InitWithFuncCallback(expireNowTimerCallback, this, michael@0: RENEW_CACHED_NOW_TIMEOUT, michael@0: nsITimer::TYPE_ONE_SHOT); michael@0: } michael@0: return mCachedNow; michael@0: } michael@0: michael@0: michael@0: void nsNavHistory::expireNowTimerCallback(nsITimer* aTimer, void* aClosure) michael@0: { michael@0: nsNavHistory *history = static_cast(aClosure); michael@0: if (history) { michael@0: history->mCachedNow = 0; michael@0: history->mExpireNowTimer = 0; michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Code borrowed from mozilla/xpfe/components/history/src/nsGlobalHistory.cpp michael@0: * Pass in a pre-normalized now and a date, and we'll find the difference since michael@0: * midnight on each of the days. michael@0: */ michael@0: static PRTime michael@0: NormalizeTimeRelativeToday(PRTime aTime) michael@0: { michael@0: // round to midnight this morning michael@0: PRExplodedTime explodedTime; michael@0: PR_ExplodeTime(aTime, PR_LocalTimeParameters, &explodedTime); michael@0: michael@0: // set to midnight (0:00) michael@0: explodedTime.tm_min = michael@0: explodedTime.tm_hour = michael@0: explodedTime.tm_sec = michael@0: explodedTime.tm_usec = 0; michael@0: michael@0: return PR_ImplodeTime(&explodedTime); michael@0: } michael@0: michael@0: // nsNavHistory::NormalizeTime michael@0: // michael@0: // Converts a nsINavHistoryQuery reference+offset time into a PRTime michael@0: // relative to the epoch. michael@0: // michael@0: // It is important that this function NOT use the current time optimization. michael@0: // It is called to update queries, and we really need to know what right michael@0: // now is because those incoming values will also have current times that michael@0: // we will have to compare against. michael@0: michael@0: PRTime // static michael@0: nsNavHistory::NormalizeTime(uint32_t aRelative, PRTime aOffset) michael@0: { michael@0: PRTime ref; michael@0: switch (aRelative) michael@0: { michael@0: case nsINavHistoryQuery::TIME_RELATIVE_EPOCH: michael@0: return aOffset; michael@0: case nsINavHistoryQuery::TIME_RELATIVE_TODAY: michael@0: ref = NormalizeTimeRelativeToday(PR_Now()); michael@0: break; michael@0: case nsINavHistoryQuery::TIME_RELATIVE_NOW: michael@0: ref = PR_Now(); michael@0: break; michael@0: default: michael@0: NS_NOTREACHED("Invalid relative time"); michael@0: return 0; michael@0: } michael@0: return ref + aOffset; michael@0: } michael@0: michael@0: // nsNavHistory::GetUpdateRequirements michael@0: // michael@0: // Returns conditions for query update. michael@0: // michael@0: // QUERYUPDATE_TIME: michael@0: // This query is only limited by an inclusive time range on the first michael@0: // query object. The caller can quickly evaluate the time itself if it michael@0: // chooses. This is even simpler than "simple" below. michael@0: // QUERYUPDATE_SIMPLE: michael@0: // This query is evaluatable using EvaluateQueryForNode to do live michael@0: // updating. michael@0: // QUERYUPDATE_COMPLEX: michael@0: // This query is not evaluatable using EvaluateQueryForNode. When something michael@0: // happens that this query updates, you will need to re-run the query. michael@0: // QUERYUPDATE_COMPLEX_WITH_BOOKMARKS: michael@0: // A complex query that additionally has dependence on bookmarks. All michael@0: // bookmark-dependent queries fall under this category. michael@0: // michael@0: // aHasSearchTerms will be set to true if the query has any dependence on michael@0: // keywords. When there is no dependence on keywords, we can handle title michael@0: // change operations as simple instead of complex. michael@0: michael@0: uint32_t michael@0: nsNavHistory::GetUpdateRequirements(const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: bool* aHasSearchTerms) michael@0: { michael@0: NS_ASSERTION(aQueries.Count() > 0, "Must have at least one query"); michael@0: michael@0: // first check if there are search terms michael@0: *aHasSearchTerms = false; michael@0: int32_t i; michael@0: for (i = 0; i < aQueries.Count(); i ++) { michael@0: aQueries[i]->GetHasSearchTerms(aHasSearchTerms); michael@0: if (*aHasSearchTerms) michael@0: break; michael@0: } michael@0: michael@0: bool nonTimeBasedItems = false; michael@0: bool domainBasedItems = false; michael@0: michael@0: for (i = 0; i < aQueries.Count(); i ++) { michael@0: nsNavHistoryQuery* query = aQueries[i]; michael@0: michael@0: if (query->Folders().Length() > 0 || michael@0: query->OnlyBookmarked() || michael@0: query->Tags().Length() > 0) { michael@0: return QUERYUPDATE_COMPLEX_WITH_BOOKMARKS; michael@0: } michael@0: michael@0: // Note: we don't currently have any complex non-bookmarked items, but these michael@0: // are expected to be added. Put detection of these items here. michael@0: if (!query->SearchTerms().IsEmpty() || michael@0: !query->Domain().IsVoid() || michael@0: query->Uri() != nullptr) michael@0: nonTimeBasedItems = true; michael@0: michael@0: if (! query->Domain().IsVoid()) michael@0: domainBasedItems = true; michael@0: } michael@0: michael@0: if (aOptions->ResultType() == michael@0: nsINavHistoryQueryOptions::RESULTS_AS_TAG_QUERY) michael@0: return QUERYUPDATE_COMPLEX_WITH_BOOKMARKS; michael@0: michael@0: // Whenever there is a maximum number of results, michael@0: // and we are not a bookmark query we must requery. This michael@0: // is because we can't generally know if any given addition/change causes michael@0: // the item to be in the top N items in the database. michael@0: if (aOptions->MaxResults() > 0) michael@0: return QUERYUPDATE_COMPLEX; michael@0: michael@0: if (aQueries.Count() == 1 && domainBasedItems) michael@0: return QUERYUPDATE_HOST; michael@0: if (aQueries.Count() == 1 && !nonTimeBasedItems) michael@0: return QUERYUPDATE_TIME; michael@0: michael@0: return QUERYUPDATE_SIMPLE; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::EvaluateQueryForNode michael@0: // michael@0: // This runs the node through the given queries to see if satisfies the michael@0: // query conditions. Not every query parameters are handled by this code, michael@0: // but we handle the most common ones so that performance is better. michael@0: // michael@0: // We assume that the time on the node is the time that we want to compare. michael@0: // This is not necessarily true because URL nodes have the last access time, michael@0: // which is not necessarily the same. However, since this is being called michael@0: // to update the list, we assume that the last access time is the current michael@0: // access time that we are being asked to compare so it works out. michael@0: // michael@0: // Returns true if node matches the query, false if not. michael@0: michael@0: bool michael@0: nsNavHistory::EvaluateQueryForNode(const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: nsNavHistoryResultNode* aNode) michael@0: { michael@0: // lazily created from the node's string when we need to match URIs michael@0: nsCOMPtr nodeUri; michael@0: michael@0: // --- hidden --- michael@0: if (aNode->mHidden && !aOptions->IncludeHidden()) michael@0: return false; michael@0: michael@0: for (int32_t i = 0; i < aQueries.Count(); i ++) { michael@0: bool hasIt; michael@0: nsCOMPtr query = aQueries[i]; michael@0: michael@0: // --- begin time --- michael@0: query->GetHasBeginTime(&hasIt); michael@0: if (hasIt) { michael@0: PRTime beginTime = NormalizeTime(query->BeginTimeReference(), michael@0: query->BeginTime()); michael@0: if (aNode->mTime < beginTime) michael@0: continue; // before our time range michael@0: } michael@0: michael@0: // --- end time --- michael@0: query->GetHasEndTime(&hasIt); michael@0: if (hasIt) { michael@0: PRTime endTime = NormalizeTime(query->EndTimeReference(), michael@0: query->EndTime()); michael@0: if (aNode->mTime > endTime) michael@0: continue; // after our time range michael@0: } michael@0: michael@0: // --- search terms --- michael@0: if (! query->SearchTerms().IsEmpty()) { michael@0: // we can use the existing filtering code, just give it our one object in michael@0: // an array. michael@0: nsCOMArray inputSet; michael@0: inputSet.AppendObject(aNode); michael@0: nsCOMArray queries; michael@0: queries.AppendObject(query); michael@0: nsCOMArray filteredSet; michael@0: nsresult rv = FilterResultSet(nullptr, inputSet, &filteredSet, queries, aOptions); michael@0: if (NS_FAILED(rv)) michael@0: continue; michael@0: if (! filteredSet.Count()) michael@0: continue; // did not make it through the filter, doesn't match michael@0: } michael@0: michael@0: // --- domain/host matching --- michael@0: query->GetHasDomain(&hasIt); michael@0: if (hasIt) { michael@0: if (! nodeUri) { michael@0: // lazy creation of nodeUri, which might be checked for multiple queries michael@0: if (NS_FAILED(NS_NewURI(getter_AddRefs(nodeUri), aNode->mURI))) michael@0: continue; michael@0: } michael@0: nsAutoCString asciiRequest; michael@0: if (NS_FAILED(AsciiHostNameFromHostString(query->Domain(), asciiRequest))) michael@0: continue; michael@0: michael@0: if (query->DomainIsHost()) { michael@0: nsAutoCString host; michael@0: if (NS_FAILED(nodeUri->GetAsciiHost(host))) michael@0: continue; michael@0: michael@0: if (! asciiRequest.Equals(host)) michael@0: continue; // host names don't match michael@0: } michael@0: // check domain names michael@0: nsAutoCString domain; michael@0: DomainNameFromURI(nodeUri, domain); michael@0: if (! asciiRequest.Equals(domain)) michael@0: continue; // domain names don't match michael@0: } michael@0: michael@0: // --- URI matching --- michael@0: if (query->Uri()) { michael@0: if (! nodeUri) { // lazy creation of nodeUri michael@0: if (NS_FAILED(NS_NewURI(getter_AddRefs(nodeUri), aNode->mURI))) michael@0: continue; michael@0: } michael@0: if (! query->UriIsPrefix()) { michael@0: // easy case: the URI is an exact match michael@0: bool equals; michael@0: nsresult rv = query->Uri()->Equals(nodeUri, &equals); michael@0: NS_ENSURE_SUCCESS(rv, false); michael@0: if (! equals) michael@0: continue; michael@0: } else { michael@0: // harder case: match prefix, note that we need to get the ASCII string michael@0: // from the node's parsed URI instead of using the node's mUrl string, michael@0: // because that might not be normalized michael@0: nsAutoCString nodeUriString; michael@0: nodeUri->GetAsciiSpec(nodeUriString); michael@0: nsAutoCString queryUriString; michael@0: query->Uri()->GetAsciiSpec(queryUriString); michael@0: if (queryUriString.Length() > nodeUriString.Length()) michael@0: continue; // not long enough to match as prefix michael@0: nodeUriString.SetLength(queryUriString.Length()); michael@0: if (! nodeUriString.Equals(queryUriString)) michael@0: continue; // prefixes don't match michael@0: } michael@0: } michael@0: michael@0: // Transitions matching. michael@0: const nsTArray& transitions = query->Transitions(); michael@0: if (aNode->mTransitionType > 0 && michael@0: transitions.Length() && michael@0: !transitions.Contains(aNode->mTransitionType)) { michael@0: continue; // transition doesn't match. michael@0: } michael@0: michael@0: // If we ever make it to the bottom of this loop, that means it passed all michael@0: // tests for the given query. Since queries are ORed together, that means michael@0: // it passed everything and we are done. michael@0: return true; michael@0: } michael@0: michael@0: // didn't match any query michael@0: return false; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::AsciiHostNameFromHostString michael@0: // michael@0: // We might have interesting encodings and different case in the host name. michael@0: // This will convert that host name into an ASCII host name by sending it michael@0: // through the URI canonicalization. The result can be used for comparison michael@0: // with other ASCII host name strings. michael@0: nsresult // static michael@0: nsNavHistory::AsciiHostNameFromHostString(const nsACString& aHostName, michael@0: nsACString& aAscii) michael@0: { michael@0: // To properly generate a uri we must provide a protocol. michael@0: nsAutoCString fakeURL("http://"); michael@0: fakeURL.Append(aHostName); michael@0: nsCOMPtr uri; michael@0: nsresult rv = NS_NewURI(getter_AddRefs(uri), fakeURL); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = uri->GetAsciiHost(aAscii); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::DomainNameFromURI michael@0: // michael@0: // This does the www.mozilla.org -> mozilla.org and michael@0: // foo.theregister.co.uk -> theregister.co.uk conversion michael@0: void michael@0: nsNavHistory::DomainNameFromURI(nsIURI *aURI, michael@0: nsACString& aDomainName) michael@0: { michael@0: // lazily get the effective tld service michael@0: if (!mTLDService) michael@0: mTLDService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID); michael@0: michael@0: if (mTLDService) { michael@0: // get the base domain for a given hostname. michael@0: // e.g. for "images.bbc.co.uk", this would be "bbc.co.uk". michael@0: nsresult rv = mTLDService->GetBaseDomain(aURI, 0, aDomainName); michael@0: if (NS_SUCCEEDED(rv)) michael@0: return; michael@0: } michael@0: michael@0: // just return the original hostname michael@0: // (it's also possible the host is an IP address) michael@0: aURI->GetAsciiHost(aDomainName); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetHasHistoryEntries(bool* aHasEntries) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aHasEntries); michael@0: *aHasEntries = GetDaysOfHistory() > 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: namespace { michael@0: michael@0: class InvalidateAllFrecenciesCallback : public AsyncStatementCallback michael@0: { michael@0: public: michael@0: InvalidateAllFrecenciesCallback() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD HandleCompletion(uint16_t aReason) michael@0: { michael@0: if (aReason == REASON_FINISHED) { michael@0: nsNavHistory *navHistory = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_STATE(navHistory); michael@0: navHistory->NotifyManyFrecenciesChanged(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: nsresult michael@0: nsNavHistory::invalidateFrecencies(const nsCString& aPlaceIdsQueryString) michael@0: { michael@0: // Exclude place: queries by setting their frecency to zero. michael@0: nsCString invalidFrecenciesSQLFragment( michael@0: "UPDATE moz_places SET frecency = " michael@0: ); michael@0: if (!aPlaceIdsQueryString.IsEmpty()) michael@0: invalidFrecenciesSQLFragment.AppendLiteral("NOTIFY_FRECENCY("); michael@0: invalidFrecenciesSQLFragment.AppendLiteral( michael@0: "(CASE " michael@0: "WHEN url BETWEEN 'place:' AND 'place;' " michael@0: "THEN 0 " michael@0: "ELSE -1 " michael@0: "END) " michael@0: ); michael@0: if (!aPlaceIdsQueryString.IsEmpty()) { michael@0: invalidFrecenciesSQLFragment.AppendLiteral( michael@0: ", url, guid, hidden, last_visit_date) " michael@0: ); michael@0: } michael@0: invalidFrecenciesSQLFragment.AppendLiteral( michael@0: "WHERE frecency > 0 " michael@0: ); michael@0: if (!aPlaceIdsQueryString.IsEmpty()) { michael@0: invalidFrecenciesSQLFragment.AppendLiteral("AND id IN("); michael@0: invalidFrecenciesSQLFragment.Append(aPlaceIdsQueryString); michael@0: invalidFrecenciesSQLFragment.AppendLiteral(")"); michael@0: } michael@0: nsRefPtr cb = michael@0: aPlaceIdsQueryString.IsEmpty() ? new InvalidateAllFrecenciesCallback() michael@0: : nullptr; michael@0: michael@0: nsCOMPtr stmt = mDB->GetAsyncStatement( michael@0: invalidFrecenciesSQLFragment michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: michael@0: nsCOMPtr ps; michael@0: nsresult rv = stmt->ExecuteAsync(cb, getter_AddRefs(ps)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // Call this method before visiting a URL in order to help determine the michael@0: // transition type of the visit. michael@0: // michael@0: // @see MarkPageAsTyped michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::MarkPageAsFollowedBookmark(nsIURI* aURI) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: // don't add when history is disabled michael@0: if (IsHistoryDisabled()) michael@0: return NS_OK; michael@0: michael@0: nsAutoCString uriString; michael@0: nsresult rv = aURI->GetSpec(uriString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // if URL is already in the bookmark queue, then we need to remove the old one michael@0: int64_t unusedEventTime; michael@0: if (mRecentBookmark.Get(uriString, &unusedEventTime)) michael@0: mRecentBookmark.Remove(uriString); michael@0: michael@0: if (mRecentBookmark.Count() > RECENT_EVENT_QUEUE_MAX_LENGTH) michael@0: ExpireNonrecentEvents(&mRecentBookmark); michael@0: michael@0: mRecentBookmark.Put(uriString, GetNow()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::CanAddURI michael@0: // michael@0: // Filter out unwanted URIs such as "chrome:", "mailbox:", etc. michael@0: // michael@0: // The model is if we don't know differently then add which basically means michael@0: // we are suppose to try all the things we know not to allow in and then if michael@0: // we don't bail go on and allow it in. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::CanAddURI(nsIURI* aURI, bool* canAdd) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: NS_ENSURE_ARG_POINTER(canAdd); michael@0: michael@0: // If history is disabled, don't add any entry. michael@0: if (IsHistoryDisabled()) { michael@0: *canAdd = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsAutoCString scheme; michael@0: nsresult rv = aURI->GetScheme(scheme); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // first check the most common cases (HTTP, HTTPS) to allow in to avoid most michael@0: // of the work michael@0: if (scheme.EqualsLiteral("http")) { michael@0: *canAdd = true; michael@0: return NS_OK; michael@0: } michael@0: if (scheme.EqualsLiteral("https")) { michael@0: *canAdd = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // now check for all bad things michael@0: if (scheme.EqualsLiteral("about") || michael@0: scheme.EqualsLiteral("imap") || michael@0: scheme.EqualsLiteral("news") || michael@0: scheme.EqualsLiteral("mailbox") || michael@0: scheme.EqualsLiteral("moz-anno") || michael@0: scheme.EqualsLiteral("view-source") || michael@0: scheme.EqualsLiteral("chrome") || michael@0: scheme.EqualsLiteral("resource") || michael@0: scheme.EqualsLiteral("data") || michael@0: scheme.EqualsLiteral("wyciwyg") || michael@0: scheme.EqualsLiteral("javascript") || michael@0: scheme.EqualsLiteral("blob")) { michael@0: *canAdd = false; michael@0: return NS_OK; michael@0: } michael@0: *canAdd = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsNavHistory::GetNewQuery michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetNewQuery(nsINavHistoryQuery **_retval) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: michael@0: nsRefPtr query = new nsNavHistoryQuery(); michael@0: query.forget(_retval); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsNavHistory::GetNewQueryOptions michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetNewQueryOptions(nsINavHistoryQueryOptions **_retval) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: michael@0: nsRefPtr queryOptions = new nsNavHistoryQueryOptions(); michael@0: queryOptions.forget(_retval); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsNavHistory::ExecuteQuery michael@0: // michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::ExecuteQuery(nsINavHistoryQuery *aQuery, nsINavHistoryQueryOptions *aOptions, michael@0: nsINavHistoryResult** _retval) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aQuery); michael@0: NS_ENSURE_ARG(aOptions); michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: michael@0: return ExecuteQueries(&aQuery, 1, aOptions, _retval); michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::ExecuteQueries michael@0: // michael@0: // This function is actually very simple, we just create the proper root node (either michael@0: // a bookmark folder or a complex query node) and assign it to the result. The node michael@0: // will then populate itself accordingly. michael@0: // michael@0: // Quick overview of query operation: When you call this function, we will construct michael@0: // the correct container node and set the options you give it. This node will then michael@0: // fill itself. Folder nodes will call nsNavBookmarks::QueryFolderChildren, and michael@0: // all other queries will call GetQueryResults. If these results contain other michael@0: // queries, those will be populated when the container is opened. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::ExecuteQueries(nsINavHistoryQuery** aQueries, uint32_t aQueryCount, michael@0: nsINavHistoryQueryOptions *aOptions, michael@0: nsINavHistoryResult** _retval) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aQueries); michael@0: NS_ENSURE_ARG(aOptions); michael@0: NS_ENSURE_ARG(aQueryCount); michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: michael@0: nsresult rv; michael@0: // concrete options michael@0: nsCOMPtr options = do_QueryInterface(aOptions); michael@0: NS_ENSURE_TRUE(options, NS_ERROR_INVALID_ARG); michael@0: michael@0: // concrete queries array michael@0: nsCOMArray queries; michael@0: for (uint32_t i = 0; i < aQueryCount; i ++) { michael@0: nsCOMPtr query = do_QueryInterface(aQueries[i], &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: queries.AppendObject(query); michael@0: } michael@0: michael@0: // Create the root node. michael@0: nsRefPtr rootNode; michael@0: int64_t folderId = GetSimpleBookmarksQueryFolder(queries, options); michael@0: if (folderId) { michael@0: // In the simple case where we're just querying children of a single michael@0: // bookmark folder, we can more efficiently generate results. michael@0: nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService(); michael@0: NS_ENSURE_TRUE(bookmarks, NS_ERROR_OUT_OF_MEMORY); michael@0: nsRefPtr tempRootNode; michael@0: rv = bookmarks->ResultNodeForContainer(folderId, options, michael@0: getter_AddRefs(tempRootNode)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rootNode = tempRootNode->GetAsContainer(); michael@0: } michael@0: else { michael@0: NS_WARNING("Generating a generic empty node for a broken query!"); michael@0: // This is a perf hack to generate an empty query that skips filtering. michael@0: options->SetExcludeItems(true); michael@0: } michael@0: } michael@0: michael@0: if (!rootNode) { michael@0: // Either this is not a folder shortcut, or is a broken one. In both cases michael@0: // just generate a query node. michael@0: rootNode = new nsNavHistoryQueryResultNode(EmptyCString(), EmptyCString(), michael@0: queries, options); michael@0: } michael@0: michael@0: // Create the result that will hold nodes. Inject batching status into it. michael@0: nsRefPtr result; michael@0: rv = nsNavHistoryResult::NewHistoryResult(aQueries, aQueryCount, options, michael@0: rootNode, isBatching(), michael@0: getter_AddRefs(result)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: result.forget(_retval); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // determine from our nsNavHistoryQuery array and nsNavHistoryQueryOptions michael@0: // if this is the place query from the history menu. michael@0: // from browser-menubar.inc, our history menu query is: michael@0: // place:sort=4&maxResults=10 michael@0: // note, any maxResult > 0 will still be considered a history menu query michael@0: // or if this is the place query from the "Most Visited" item in the michael@0: // "Smart Bookmarks" folder: place:sort=8&maxResults=10 michael@0: // note, any maxResult > 0 will still be considered a Most Visited menu query michael@0: static michael@0: bool IsOptimizableHistoryQuery(const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions *aOptions, michael@0: uint16_t aSortMode) michael@0: { michael@0: if (aQueries.Count() != 1) michael@0: return false; michael@0: michael@0: nsNavHistoryQuery *aQuery = aQueries[0]; michael@0: michael@0: if (aOptions->QueryType() != nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY) michael@0: return false; michael@0: michael@0: if (aOptions->ResultType() != nsINavHistoryQueryOptions::RESULTS_AS_URI) michael@0: return false; michael@0: michael@0: if (aOptions->SortingMode() != aSortMode) michael@0: return false; michael@0: michael@0: if (aOptions->MaxResults() <= 0) michael@0: return false; michael@0: michael@0: if (aOptions->ExcludeItems()) michael@0: return false; michael@0: michael@0: if (aOptions->IncludeHidden()) michael@0: return false; michael@0: michael@0: if (aQuery->MinVisits() != -1 || aQuery->MaxVisits() != -1) michael@0: return false; michael@0: michael@0: if (aQuery->BeginTime() || aQuery->BeginTimeReference()) michael@0: return false; michael@0: michael@0: if (aQuery->EndTime() || aQuery->EndTimeReference()) michael@0: return false; michael@0: michael@0: if (!aQuery->SearchTerms().IsEmpty()) michael@0: return false; michael@0: michael@0: if (aQuery->OnlyBookmarked()) michael@0: return false; michael@0: michael@0: if (aQuery->DomainIsHost() || !aQuery->Domain().IsEmpty()) michael@0: return false; michael@0: michael@0: if (aQuery->AnnotationIsNot() || !aQuery->Annotation().IsEmpty()) michael@0: return false; michael@0: michael@0: if (aQuery->UriIsPrefix() || aQuery->Uri()) michael@0: return false; michael@0: michael@0: if (aQuery->Folders().Length() > 0) michael@0: return false; michael@0: michael@0: if (aQuery->Tags().Length() > 0) michael@0: return false; michael@0: michael@0: if (aQuery->Transitions().Length() > 0) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static michael@0: bool NeedToFilterResultSet(const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions *aOptions) michael@0: { michael@0: uint16_t resultType = aOptions->ResultType(); michael@0: return resultType == nsINavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS; michael@0: } michael@0: michael@0: // ** Helper class for ConstructQueryString **/ michael@0: michael@0: class PlacesSQLQueryBuilder michael@0: { michael@0: public: michael@0: PlacesSQLQueryBuilder(const nsCString& aConditions, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: bool aUseLimit, michael@0: nsNavHistory::StringHash& aAddParams, michael@0: bool aHasSearchTerms); michael@0: michael@0: nsresult GetQueryString(nsCString& aQueryString); michael@0: michael@0: private: michael@0: nsresult Select(); michael@0: michael@0: nsresult SelectAsURI(); michael@0: nsresult SelectAsVisit(); michael@0: nsresult SelectAsDay(); michael@0: nsresult SelectAsSite(); michael@0: nsresult SelectAsTag(); michael@0: michael@0: nsresult Where(); michael@0: nsresult GroupBy(); michael@0: nsresult OrderBy(); michael@0: nsresult Limit(); michael@0: michael@0: void OrderByColumnIndexAsc(int32_t aIndex); michael@0: void OrderByColumnIndexDesc(int32_t aIndex); michael@0: // Use these if you want a case insensitive sorting. michael@0: void OrderByTextColumnIndexAsc(int32_t aIndex); michael@0: void OrderByTextColumnIndexDesc(int32_t aIndex); michael@0: michael@0: const nsCString& mConditions; michael@0: bool mUseLimit; michael@0: bool mHasSearchTerms; michael@0: michael@0: uint16_t mResultType; michael@0: uint16_t mQueryType; michael@0: bool mIncludeHidden; michael@0: uint16_t mSortingMode; michael@0: uint32_t mMaxResults; michael@0: michael@0: nsCString mQueryString; michael@0: nsCString mGroupBy; michael@0: bool mHasDateColumns; michael@0: bool mSkipOrderBy; michael@0: nsNavHistory::StringHash& mAddParams; michael@0: }; michael@0: michael@0: PlacesSQLQueryBuilder::PlacesSQLQueryBuilder( michael@0: const nsCString& aConditions, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: bool aUseLimit, michael@0: nsNavHistory::StringHash& aAddParams, michael@0: bool aHasSearchTerms) michael@0: : mConditions(aConditions) michael@0: , mUseLimit(aUseLimit) michael@0: , mHasSearchTerms(aHasSearchTerms) michael@0: , mResultType(aOptions->ResultType()) michael@0: , mQueryType(aOptions->QueryType()) michael@0: , mIncludeHidden(aOptions->IncludeHidden()) michael@0: , mSortingMode(aOptions->SortingMode()) michael@0: , mMaxResults(aOptions->MaxResults()) michael@0: , mSkipOrderBy(false) michael@0: , mAddParams(aAddParams) michael@0: { michael@0: mHasDateColumns = (mQueryType == nsINavHistoryQueryOptions::QUERY_TYPE_BOOKMARKS); michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::GetQueryString(nsCString& aQueryString) michael@0: { michael@0: nsresult rv = Select(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = Where(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = GroupBy(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = OrderBy(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = Limit(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: aQueryString = mQueryString; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::Select() michael@0: { michael@0: nsresult rv; michael@0: michael@0: switch (mResultType) michael@0: { michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_URI: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS: michael@0: rv = SelectAsURI(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: break; michael@0: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_VISIT: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_FULL_VISIT: michael@0: rv = SelectAsVisit(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: break; michael@0: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_DATE_QUERY: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_DATE_SITE_QUERY: michael@0: rv = SelectAsDay(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: break; michael@0: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_SITE_QUERY: michael@0: rv = SelectAsSite(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: break; michael@0: michael@0: case nsINavHistoryQueryOptions::RESULTS_AS_TAG_QUERY: michael@0: rv = SelectAsTag(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: break; michael@0: michael@0: default: michael@0: NS_NOTREACHED("Invalid result type"); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::SelectAsURI() michael@0: { michael@0: nsNavHistory *history = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_TRUE(history, NS_ERROR_OUT_OF_MEMORY); michael@0: nsAutoCString tagsSqlFragment; michael@0: michael@0: switch (mQueryType) { michael@0: case nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY: michael@0: GetTagsSqlFragment(history->GetTagsFolder(), michael@0: NS_LITERAL_CSTRING("h.id"), michael@0: mHasSearchTerms, michael@0: tagsSqlFragment); michael@0: michael@0: mQueryString = NS_LITERAL_CSTRING( michael@0: "SELECT h.id, h.url, h.title AS page_title, h.rev_host, h.visit_count, " michael@0: "h.last_visit_date, f.url, null, null, null, null, ") + michael@0: tagsSqlFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_places h " michael@0: "LEFT JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: // WHERE 1 is a no-op since additonal conditions will start with AND. michael@0: "WHERE 1 " michael@0: "{QUERY_OPTIONS_VISITS} {QUERY_OPTIONS_PLACES} " michael@0: "{ADDITIONAL_CONDITIONS} "); michael@0: break; michael@0: michael@0: case nsINavHistoryQueryOptions::QUERY_TYPE_BOOKMARKS: michael@0: if (mResultType == nsINavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS) { michael@0: // Order-by clause is hardcoded because we need to discard duplicates michael@0: // in FilterResultSet. We will retain only the last modified item, michael@0: // so we are ordering by place id and last modified to do a faster michael@0: // filtering. michael@0: mSkipOrderBy = true; michael@0: michael@0: GetTagsSqlFragment(history->GetTagsFolder(), michael@0: NS_LITERAL_CSTRING("b2.fk"), michael@0: mHasSearchTerms, michael@0: tagsSqlFragment); michael@0: michael@0: mQueryString = NS_LITERAL_CSTRING( michael@0: "SELECT b2.fk, h.url, COALESCE(b2.title, h.title) AS page_title, " michael@0: "h.rev_host, h.visit_count, h.last_visit_date, f.url, b2.id, " michael@0: "b2.dateAdded, b2.lastModified, b2.parent, ") + michael@0: tagsSqlFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_bookmarks b2 " michael@0: "JOIN (SELECT b.fk " michael@0: "FROM moz_bookmarks b " michael@0: // ADDITIONAL_CONDITIONS will filter on parent. michael@0: "WHERE b.type = 1 {ADDITIONAL_CONDITIONS} " michael@0: ") AS seed ON b2.fk = seed.fk " michael@0: "JOIN moz_places h ON h.id = b2.fk " michael@0: "LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE NOT EXISTS ( " michael@0: "SELECT id FROM moz_bookmarks WHERE id = b2.parent AND parent = ") + michael@0: nsPrintfCString("%lld", history->GetTagsFolder()) + michael@0: NS_LITERAL_CSTRING(") " michael@0: "ORDER BY b2.fk DESC, b2.lastModified DESC"); michael@0: } michael@0: else { michael@0: GetTagsSqlFragment(history->GetTagsFolder(), michael@0: NS_LITERAL_CSTRING("b.fk"), michael@0: mHasSearchTerms, michael@0: tagsSqlFragment); michael@0: mQueryString = NS_LITERAL_CSTRING( michael@0: "SELECT b.fk, h.url, COALESCE(b.title, h.title) AS page_title, " michael@0: "h.rev_host, h.visit_count, h.last_visit_date, f.url, b.id, " michael@0: "b.dateAdded, b.lastModified, b.parent, ") + michael@0: tagsSqlFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_bookmarks b " michael@0: "JOIN moz_places h ON b.fk = h.id " michael@0: "LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE NOT EXISTS " michael@0: "(SELECT id FROM moz_bookmarks " michael@0: "WHERE id = b.parent AND parent = ") + michael@0: nsPrintfCString("%lld", history->GetTagsFolder()) + michael@0: NS_LITERAL_CSTRING(") " michael@0: "{ADDITIONAL_CONDITIONS}"); michael@0: } michael@0: break; michael@0: michael@0: default: michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::SelectAsVisit() michael@0: { michael@0: nsNavHistory *history = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_TRUE(history, NS_ERROR_OUT_OF_MEMORY); michael@0: nsAutoCString tagsSqlFragment; michael@0: GetTagsSqlFragment(history->GetTagsFolder(), michael@0: NS_LITERAL_CSTRING("h.id"), michael@0: mHasSearchTerms, michael@0: tagsSqlFragment); michael@0: mQueryString = NS_LITERAL_CSTRING( michael@0: "SELECT h.id, h.url, h.title AS page_title, h.rev_host, h.visit_count, " michael@0: "v.visit_date, f.url, null, null, null, null, ") + michael@0: tagsSqlFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_places h " michael@0: "JOIN moz_historyvisits v ON h.id = v.place_id " michael@0: "LEFT JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: // WHERE 1 is a no-op since additonal conditions will start with AND. michael@0: "WHERE 1 " michael@0: "{QUERY_OPTIONS_VISITS} {QUERY_OPTIONS_PLACES} " michael@0: "{ADDITIONAL_CONDITIONS} "); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::SelectAsDay() michael@0: { michael@0: mSkipOrderBy = true; michael@0: michael@0: // Sort child queries based on sorting mode if it's provided, otherwise michael@0: // fallback to default sort by title ascending. michael@0: uint16_t sortingMode = nsINavHistoryQueryOptions::SORT_BY_TITLE_ASCENDING; michael@0: if (mSortingMode != nsINavHistoryQueryOptions::SORT_BY_NONE && michael@0: mResultType == nsINavHistoryQueryOptions::RESULTS_AS_DATE_QUERY) michael@0: sortingMode = mSortingMode; michael@0: michael@0: uint16_t resultType = michael@0: mResultType == nsINavHistoryQueryOptions::RESULTS_AS_DATE_QUERY ? michael@0: (uint16_t)nsINavHistoryQueryOptions::RESULTS_AS_URI : michael@0: (uint16_t)nsINavHistoryQueryOptions::RESULTS_AS_SITE_QUERY; michael@0: michael@0: // beginTime will become the node's time property, we don't use endTime michael@0: // because it could overlap, and we use time to sort containers and find michael@0: // insert position in a result. michael@0: mQueryString = nsPrintfCString( michael@0: "SELECT null, " michael@0: "'place:type=%ld&sort=%ld&beginTime='||beginTime||'&endTime='||endTime, " michael@0: "dayTitle, null, null, beginTime, null, null, null, null, null, null " michael@0: "FROM (", // TOUTER BEGIN michael@0: resultType, michael@0: sortingMode); michael@0: michael@0: nsNavHistory *history = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_STATE(history); michael@0: michael@0: int32_t daysOfHistory = history->GetDaysOfHistory(); michael@0: for (int32_t i = 0; i <= HISTORY_DATE_CONT_NUM(daysOfHistory); i++) { michael@0: nsAutoCString dateName; michael@0: // Timeframes are calculated as BeginTime <= container < EndTime. michael@0: // Notice times can't be relative to now, since to recognize a query we michael@0: // must ensure it won't change based on the time it is built. michael@0: // So, to select till now, we really select till start of tomorrow, that is michael@0: // a fixed timestamp. michael@0: // These are used as limits for the inside containers. michael@0: nsAutoCString sqlFragmentContainerBeginTime, sqlFragmentContainerEndTime; michael@0: // These are used to query if the container should be visible. michael@0: nsAutoCString sqlFragmentSearchBeginTime, sqlFragmentSearchEndTime; michael@0: switch(i) { michael@0: case 0: michael@0: // Today michael@0: history->GetStringFromName( michael@0: MOZ_UTF16("finduri-AgeInDays-is-0"), dateName); michael@0: // From start of today michael@0: sqlFragmentContainerBeginTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','utc')*1000000)"); michael@0: // To now (tomorrow) michael@0: sqlFragmentContainerEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','+1 day','utc')*1000000)"); michael@0: // Search for the same timeframe. michael@0: sqlFragmentSearchBeginTime = sqlFragmentContainerBeginTime; michael@0: sqlFragmentSearchEndTime = sqlFragmentContainerEndTime; michael@0: break; michael@0: case 1: michael@0: // Yesterday michael@0: history->GetStringFromName( michael@0: MOZ_UTF16("finduri-AgeInDays-is-1"), dateName); michael@0: // From start of yesterday michael@0: sqlFragmentContainerBeginTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','-1 day','utc')*1000000)"); michael@0: // To start of today michael@0: sqlFragmentContainerEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','utc')*1000000)"); michael@0: // Search for the same timeframe. michael@0: sqlFragmentSearchBeginTime = sqlFragmentContainerBeginTime; michael@0: sqlFragmentSearchEndTime = sqlFragmentContainerEndTime; michael@0: break; michael@0: case 2: michael@0: // Last 7 days michael@0: history->GetAgeInDaysString(7, michael@0: MOZ_UTF16("finduri-AgeInDays-last-is"), dateName); michael@0: // From start of 7 days ago michael@0: sqlFragmentContainerBeginTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','-7 days','utc')*1000000)"); michael@0: // To now (tomorrow) michael@0: sqlFragmentContainerEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','+1 day','utc')*1000000)"); michael@0: // This is an overlapped container, but we show it only if there are michael@0: // visits older than yesterday. michael@0: sqlFragmentSearchBeginTime = sqlFragmentContainerBeginTime; michael@0: sqlFragmentSearchEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','-2 days','utc')*1000000)"); michael@0: break; michael@0: case 3: michael@0: // This month michael@0: history->GetStringFromName( michael@0: MOZ_UTF16("finduri-AgeInMonths-is-0"), dateName); michael@0: // From start of this month michael@0: sqlFragmentContainerBeginTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of month','utc')*1000000)"); michael@0: // To now (tomorrow) michael@0: sqlFragmentContainerEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','+1 day','utc')*1000000)"); michael@0: // This is an overlapped container, but we show it only if there are michael@0: // visits older than 7 days ago. michael@0: sqlFragmentSearchBeginTime = sqlFragmentContainerBeginTime; michael@0: sqlFragmentSearchEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of day','-7 days','utc')*1000000)"); michael@0: break; michael@0: default: michael@0: if (i == HISTORY_ADDITIONAL_DATE_CONT_NUM + 6) { michael@0: // Older than 6 months michael@0: history->GetAgeInDaysString(6, michael@0: MOZ_UTF16("finduri-AgeInMonths-isgreater"), dateName); michael@0: // From start of epoch michael@0: sqlFragmentContainerBeginTime = NS_LITERAL_CSTRING( michael@0: "(datetime(0, 'unixepoch')*1000000)"); michael@0: // To start of 6 months ago ( 5 months + this month). michael@0: sqlFragmentContainerEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of month','-5 months','utc')*1000000)"); michael@0: // Search for the same timeframe. michael@0: sqlFragmentSearchBeginTime = sqlFragmentContainerBeginTime; michael@0: sqlFragmentSearchEndTime = sqlFragmentContainerEndTime; michael@0: break; michael@0: } michael@0: int32_t MonthIndex = i - HISTORY_ADDITIONAL_DATE_CONT_NUM; michael@0: // Previous months' titles are month's name if inside this year, michael@0: // month's name and year for previous years. michael@0: PRExplodedTime tm; michael@0: PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &tm); michael@0: uint16_t currentYear = tm.tm_year; michael@0: // Set day before month, setting month without day could cause issues. michael@0: // For example setting month to February when today is 30, since michael@0: // February has not 30 days, will return March instead. michael@0: // Also, we use day 2 instead of day 1, so that the GMT month is always michael@0: // the same as the local month. (Bug 603002) michael@0: tm.tm_mday = 2; michael@0: tm.tm_month -= MonthIndex; michael@0: // Notice we use GMTParameters because we just want to get the first michael@0: // day of each month. Using LocalTimeParameters would instead force us michael@0: // to apply a DST correction that we don't really need here. michael@0: PR_NormalizeTime(&tm, PR_GMTParameters); michael@0: // If the container is for a past year, add the year to its title, michael@0: // otherwise just show the month name. michael@0: // Note that tm_month starts from 0, while we need a 1-based index. michael@0: if (tm.tm_year < currentYear) { michael@0: history->GetMonthYear(tm.tm_month + 1, tm.tm_year, dateName); michael@0: } michael@0: else { michael@0: history->GetMonthName(tm.tm_month + 1, dateName); michael@0: } michael@0: michael@0: // From start of MonthIndex + 1 months ago michael@0: sqlFragmentContainerBeginTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of month','-"); michael@0: sqlFragmentContainerBeginTime.AppendInt(MonthIndex); michael@0: sqlFragmentContainerBeginTime.Append(NS_LITERAL_CSTRING( michael@0: " months','utc')*1000000)")); michael@0: // To start of MonthIndex months ago michael@0: sqlFragmentContainerEndTime = NS_LITERAL_CSTRING( michael@0: "(strftime('%s','now','localtime','start of month','-"); michael@0: sqlFragmentContainerEndTime.AppendInt(MonthIndex - 1); michael@0: sqlFragmentContainerEndTime.Append(NS_LITERAL_CSTRING( michael@0: " months','utc')*1000000)")); michael@0: // Search for the same timeframe. michael@0: sqlFragmentSearchBeginTime = sqlFragmentContainerBeginTime; michael@0: sqlFragmentSearchEndTime = sqlFragmentContainerEndTime; michael@0: break; michael@0: } michael@0: michael@0: nsPrintfCString dateParam("dayTitle%d", i); michael@0: mAddParams.Put(dateParam, dateName); michael@0: michael@0: nsPrintfCString dayRange( michael@0: "SELECT :%s AS dayTitle, " michael@0: "%s AS beginTime, " michael@0: "%s AS endTime " michael@0: "WHERE EXISTS ( " michael@0: "SELECT id FROM moz_historyvisits " michael@0: "WHERE visit_date >= %s " michael@0: "AND visit_date < %s " michael@0: "AND visit_type NOT IN (0,%d,%d) " michael@0: "{QUERY_OPTIONS_VISITS} " michael@0: "LIMIT 1 " michael@0: ") ", michael@0: dateParam.get(), michael@0: sqlFragmentContainerBeginTime.get(), michael@0: sqlFragmentContainerEndTime.get(), michael@0: sqlFragmentSearchBeginTime.get(), michael@0: sqlFragmentSearchEndTime.get(), michael@0: nsINavHistoryService::TRANSITION_EMBED, michael@0: nsINavHistoryService::TRANSITION_FRAMED_LINK michael@0: ); michael@0: michael@0: mQueryString.Append(dayRange); michael@0: michael@0: if (i < HISTORY_DATE_CONT_NUM(daysOfHistory)) michael@0: mQueryString.Append(NS_LITERAL_CSTRING(" UNION ALL ")); michael@0: } michael@0: michael@0: mQueryString.Append(NS_LITERAL_CSTRING(") ")); // TOUTER END michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::SelectAsSite() michael@0: { michael@0: nsAutoCString localFiles; michael@0: michael@0: nsNavHistory *history = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_STATE(history); michael@0: michael@0: history->GetStringFromName(MOZ_UTF16("localhost"), localFiles); michael@0: mAddParams.Put(NS_LITERAL_CSTRING("localhost"), localFiles); michael@0: michael@0: // If there are additional conditions the query has to join on visits too. michael@0: nsAutoCString visitsJoin; michael@0: nsAutoCString additionalConditions; michael@0: nsAutoCString timeConstraints; michael@0: if (!mConditions.IsEmpty()) { michael@0: visitsJoin.AssignLiteral("JOIN moz_historyvisits v ON v.place_id = h.id "); michael@0: additionalConditions.AssignLiteral("{QUERY_OPTIONS_VISITS} " michael@0: "{QUERY_OPTIONS_PLACES} " michael@0: "{ADDITIONAL_CONDITIONS} "); michael@0: timeConstraints.AssignLiteral("||'&beginTime='||:begin_time||" michael@0: "'&endTime='||:end_time"); michael@0: } michael@0: michael@0: mQueryString = nsPrintfCString( michael@0: "SELECT null, 'place:type=%ld&sort=%ld&domain=&domainIsHost=true'%s, " michael@0: ":localhost, :localhost, null, null, null, null, null, null, null " michael@0: "WHERE EXISTS ( " michael@0: "SELECT h.id FROM moz_places h " michael@0: "%s " michael@0: "WHERE h.hidden = 0 " michael@0: "AND h.visit_count > 0 " michael@0: "AND h.url BETWEEN 'file://' AND 'file:/~' " michael@0: "%s " michael@0: "LIMIT 1 " michael@0: ") " michael@0: "UNION ALL " michael@0: "SELECT null, " michael@0: "'place:type=%ld&sort=%ld&domain='||host||'&domainIsHost=true'%s, " michael@0: "host, host, null, null, null, null, null, null, null " michael@0: "FROM ( " michael@0: "SELECT get_unreversed_host(h.rev_host) AS host " michael@0: "FROM moz_places h " michael@0: "%s " michael@0: "WHERE h.hidden = 0 " michael@0: "AND h.rev_host <> '.' " michael@0: "AND h.visit_count > 0 " michael@0: "%s " michael@0: "GROUP BY h.rev_host " michael@0: "ORDER BY host ASC " michael@0: ") ", michael@0: nsINavHistoryQueryOptions::RESULTS_AS_URI, michael@0: mSortingMode, michael@0: timeConstraints.get(), michael@0: visitsJoin.get(), michael@0: additionalConditions.get(), michael@0: nsINavHistoryQueryOptions::RESULTS_AS_URI, michael@0: mSortingMode, michael@0: timeConstraints.get(), michael@0: visitsJoin.get(), michael@0: additionalConditions.get() michael@0: ); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::SelectAsTag() michael@0: { michael@0: nsNavHistory *history = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_STATE(history); michael@0: michael@0: // This allows sorting by date fields what is not possible with michael@0: // other history queries. michael@0: mHasDateColumns = true; michael@0: michael@0: mQueryString = nsPrintfCString( michael@0: "SELECT null, 'place:folder=' || id || '&queryType=%d&type=%ld', " michael@0: "title, null, null, null, null, null, dateAdded, " michael@0: "lastModified, null, null, null " michael@0: "FROM moz_bookmarks " michael@0: "WHERE parent = %lld", michael@0: nsINavHistoryQueryOptions::QUERY_TYPE_BOOKMARKS, michael@0: nsINavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS, michael@0: history->GetTagsFolder() michael@0: ); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::Where() michael@0: { michael@0: michael@0: // Set query options michael@0: nsAutoCString additionalVisitsConditions; michael@0: nsAutoCString additionalPlacesConditions; michael@0: michael@0: if (!mIncludeHidden) { michael@0: additionalPlacesConditions += NS_LITERAL_CSTRING("AND hidden = 0 "); michael@0: } michael@0: michael@0: if (mQueryType == nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY) { michael@0: // last_visit_date is updated for any kind of visit, so it's a good michael@0: // indicator whether the page has visits. michael@0: additionalPlacesConditions += NS_LITERAL_CSTRING( michael@0: "AND last_visit_date NOTNULL " michael@0: ); michael@0: } michael@0: michael@0: if (mResultType == nsINavHistoryQueryOptions::RESULTS_AS_URI && michael@0: !additionalVisitsConditions.IsEmpty()) { michael@0: // URI results don't join on visits. michael@0: nsAutoCString tmp = additionalVisitsConditions; michael@0: additionalVisitsConditions = "AND EXISTS (SELECT 1 FROM moz_historyvisits WHERE place_id = h.id "; michael@0: additionalVisitsConditions.Append(tmp); michael@0: additionalVisitsConditions.Append("LIMIT 1)"); michael@0: } michael@0: michael@0: mQueryString.ReplaceSubstring("{QUERY_OPTIONS_VISITS}", michael@0: additionalVisitsConditions.get()); michael@0: mQueryString.ReplaceSubstring("{QUERY_OPTIONS_PLACES}", michael@0: additionalPlacesConditions.get()); michael@0: michael@0: // If we used WHERE already, we inject the conditions michael@0: // in place of {ADDITIONAL_CONDITIONS} michael@0: if (mQueryString.Find("{ADDITIONAL_CONDITIONS}", 0) != kNotFound) { michael@0: nsAutoCString innerCondition; michael@0: // If we have condition AND it michael@0: if (!mConditions.IsEmpty()) { michael@0: innerCondition = " AND ("; michael@0: innerCondition += mConditions; michael@0: innerCondition += ")"; michael@0: } michael@0: mQueryString.ReplaceSubstring("{ADDITIONAL_CONDITIONS}", michael@0: innerCondition.get()); michael@0: michael@0: } else if (!mConditions.IsEmpty()) { michael@0: michael@0: mQueryString += "WHERE "; michael@0: mQueryString += mConditions; michael@0: michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::GroupBy() michael@0: { michael@0: mQueryString += mGroupBy; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::OrderBy() michael@0: { michael@0: if (mSkipOrderBy) michael@0: return NS_OK; michael@0: michael@0: // Sort clause: we will sort later, but if it comes out of the DB sorted, michael@0: // our later sort will be basically free. The DB can sort these for free michael@0: // most of the time anyway, because it has indices over these items. michael@0: switch(mSortingMode) michael@0: { michael@0: case nsINavHistoryQueryOptions::SORT_BY_NONE: michael@0: // Ensure sorting does not change based on tables status. michael@0: if (mResultType == nsINavHistoryQueryOptions::RESULTS_AS_URI) { michael@0: if (mQueryType == nsINavHistoryQueryOptions::QUERY_TYPE_BOOKMARKS) michael@0: mQueryString += NS_LITERAL_CSTRING(" ORDER BY b.id ASC "); michael@0: else if (mQueryType == nsINavHistoryQueryOptions::QUERY_TYPE_HISTORY) michael@0: mQueryString += NS_LITERAL_CSTRING(" ORDER BY h.id ASC "); michael@0: } michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_TITLE_ASCENDING: michael@0: case nsINavHistoryQueryOptions::SORT_BY_TITLE_DESCENDING: michael@0: // If the user wants few results, we limit them by date, necessitating michael@0: // a sort by date here (see the IDL definition for maxResults). michael@0: // Otherwise we will do actual sorting by title, but since we could need michael@0: // to special sort for some locale we will repeat a second sorting at the michael@0: // end in nsNavHistoryResult, that should be faster since the list will be michael@0: // almost ordered. michael@0: if (mMaxResults > 0) michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_VisitDate); michael@0: else if (mSortingMode == nsINavHistoryQueryOptions::SORT_BY_TITLE_ASCENDING) michael@0: OrderByTextColumnIndexAsc(nsNavHistory::kGetInfoIndex_Title); michael@0: else michael@0: OrderByTextColumnIndexDesc(nsNavHistory::kGetInfoIndex_Title); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_DATE_ASCENDING: michael@0: OrderByColumnIndexAsc(nsNavHistory::kGetInfoIndex_VisitDate); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_DATE_DESCENDING: michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_VisitDate); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_URI_ASCENDING: michael@0: OrderByColumnIndexAsc(nsNavHistory::kGetInfoIndex_URL); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_URI_DESCENDING: michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_URL); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_VISITCOUNT_ASCENDING: michael@0: OrderByColumnIndexAsc(nsNavHistory::kGetInfoIndex_VisitCount); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_VISITCOUNT_DESCENDING: michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_VisitCount); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_DATEADDED_ASCENDING: michael@0: if (mHasDateColumns) michael@0: OrderByColumnIndexAsc(nsNavHistory::kGetInfoIndex_ItemDateAdded); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_DATEADDED_DESCENDING: michael@0: if (mHasDateColumns) michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_ItemDateAdded); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_LASTMODIFIED_ASCENDING: michael@0: if (mHasDateColumns) michael@0: OrderByColumnIndexAsc(nsNavHistory::kGetInfoIndex_ItemLastModified); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_LASTMODIFIED_DESCENDING: michael@0: if (mHasDateColumns) michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_ItemLastModified); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_TAGS_ASCENDING: michael@0: case nsINavHistoryQueryOptions::SORT_BY_TAGS_DESCENDING: michael@0: case nsINavHistoryQueryOptions::SORT_BY_ANNOTATION_ASCENDING: michael@0: case nsINavHistoryQueryOptions::SORT_BY_ANNOTATION_DESCENDING: michael@0: break; // Sort later in nsNavHistoryQueryResultNode::FillChildren() michael@0: case nsINavHistoryQueryOptions::SORT_BY_FRECENCY_ASCENDING: michael@0: OrderByColumnIndexAsc(nsNavHistory::kGetInfoIndex_Frecency); michael@0: break; michael@0: case nsINavHistoryQueryOptions::SORT_BY_FRECENCY_DESCENDING: michael@0: OrderByColumnIndexDesc(nsNavHistory::kGetInfoIndex_Frecency); michael@0: break; michael@0: default: michael@0: NS_NOTREACHED("Invalid sorting mode"); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void PlacesSQLQueryBuilder::OrderByColumnIndexAsc(int32_t aIndex) michael@0: { michael@0: mQueryString += nsPrintfCString(" ORDER BY %d ASC", aIndex+1); michael@0: } michael@0: michael@0: void PlacesSQLQueryBuilder::OrderByColumnIndexDesc(int32_t aIndex) michael@0: { michael@0: mQueryString += nsPrintfCString(" ORDER BY %d DESC", aIndex+1); michael@0: } michael@0: michael@0: void PlacesSQLQueryBuilder::OrderByTextColumnIndexAsc(int32_t aIndex) michael@0: { michael@0: mQueryString += nsPrintfCString(" ORDER BY %d COLLATE NOCASE ASC", michael@0: aIndex+1); michael@0: } michael@0: michael@0: void PlacesSQLQueryBuilder::OrderByTextColumnIndexDesc(int32_t aIndex) michael@0: { michael@0: mQueryString += nsPrintfCString(" ORDER BY %d COLLATE NOCASE DESC", michael@0: aIndex+1); michael@0: } michael@0: michael@0: nsresult michael@0: PlacesSQLQueryBuilder::Limit() michael@0: { michael@0: if (mUseLimit && mMaxResults > 0) { michael@0: mQueryString += NS_LITERAL_CSTRING(" LIMIT "); michael@0: mQueryString.AppendInt(mMaxResults); michael@0: mQueryString.AppendLiteral(" "); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsNavHistory::ConstructQueryString( michael@0: const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: nsCString& queryString, michael@0: bool& aParamsPresent, michael@0: nsNavHistory::StringHash& aAddParams) michael@0: { michael@0: // For information about visit_type see nsINavHistoryService.idl. michael@0: // visitType == 0 is undefined (see bug #375777 for details). michael@0: // Some sites, especially Javascript-heavy ones, load things in frames to michael@0: // display them, resulting in a lot of these entries. This is the reason michael@0: // why such visits are filtered out. michael@0: nsresult rv; michael@0: aParamsPresent = false; michael@0: michael@0: int32_t sortingMode = aOptions->SortingMode(); michael@0: NS_ASSERTION(sortingMode >= nsINavHistoryQueryOptions::SORT_BY_NONE && michael@0: sortingMode <= nsINavHistoryQueryOptions::SORT_BY_FRECENCY_DESCENDING, michael@0: "Invalid sortingMode found while building query!"); michael@0: michael@0: bool hasSearchTerms = false; michael@0: for (int32_t i = 0; i < aQueries.Count() && !hasSearchTerms; i++) { michael@0: aQueries[i]->GetHasSearchTerms(&hasSearchTerms); michael@0: } michael@0: michael@0: nsAutoCString tagsSqlFragment; michael@0: GetTagsSqlFragment(GetTagsFolder(), michael@0: NS_LITERAL_CSTRING("h.id"), michael@0: hasSearchTerms, michael@0: tagsSqlFragment); michael@0: michael@0: if (IsOptimizableHistoryQuery(aQueries, aOptions, michael@0: nsINavHistoryQueryOptions::SORT_BY_DATE_DESCENDING) || michael@0: IsOptimizableHistoryQuery(aQueries, aOptions, michael@0: nsINavHistoryQueryOptions::SORT_BY_VISITCOUNT_DESCENDING)) { michael@0: // Generate an optimized query for the history menu and most visited michael@0: // smart bookmark. michael@0: queryString = NS_LITERAL_CSTRING( michael@0: "SELECT h.id, h.url, h.title AS page_title, h.rev_host, h.visit_count, h.last_visit_date, " michael@0: "f.url, null, null, null, null, ") + michael@0: tagsSqlFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_places h " michael@0: "LEFT OUTER JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE h.hidden = 0 " michael@0: "AND EXISTS (SELECT id FROM moz_historyvisits WHERE place_id = h.id " michael@0: "AND visit_type NOT IN ") + michael@0: nsPrintfCString("(0,%d,%d) ", michael@0: nsINavHistoryService::TRANSITION_EMBED, michael@0: nsINavHistoryService::TRANSITION_FRAMED_LINK) + michael@0: NS_LITERAL_CSTRING("LIMIT 1) " michael@0: "{QUERY_OPTIONS} " michael@0: ); michael@0: michael@0: queryString.Append(NS_LITERAL_CSTRING("ORDER BY ")); michael@0: if (sortingMode == nsINavHistoryQueryOptions::SORT_BY_DATE_DESCENDING) michael@0: queryString.Append(NS_LITERAL_CSTRING("last_visit_date DESC ")); michael@0: else michael@0: queryString.Append(NS_LITERAL_CSTRING("visit_count DESC ")); michael@0: michael@0: queryString.Append(NS_LITERAL_CSTRING("LIMIT ")); michael@0: queryString.AppendInt(aOptions->MaxResults()); michael@0: michael@0: nsAutoCString additionalQueryOptions; michael@0: michael@0: queryString.ReplaceSubstring("{QUERY_OPTIONS}", michael@0: additionalQueryOptions.get()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsAutoCString conditions; michael@0: for (int32_t i = 0; i < aQueries.Count(); i++) { michael@0: nsCString queryClause; michael@0: rv = QueryToSelectClause(aQueries[i], aOptions, i, &queryClause); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (! queryClause.IsEmpty()) { michael@0: aParamsPresent = true; michael@0: if (! conditions.IsEmpty()) // exists previous clause: multiple ones are ORed michael@0: conditions += NS_LITERAL_CSTRING(" OR "); michael@0: conditions += NS_LITERAL_CSTRING("(") + queryClause + michael@0: NS_LITERAL_CSTRING(")"); michael@0: } michael@0: } michael@0: michael@0: // Determine whether we can push maxResults constraints into the queries michael@0: // as LIMIT, or if we need to do result count clamping later michael@0: // using FilterResultSet() michael@0: bool useLimitClause = !NeedToFilterResultSet(aQueries, aOptions); michael@0: michael@0: PlacesSQLQueryBuilder queryStringBuilder(conditions, aOptions, michael@0: useLimitClause, aAddParams, michael@0: hasSearchTerms); michael@0: rv = queryStringBuilder.GetQueryString(queryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: PLDHashOperator BindAdditionalParameter(nsNavHistory::StringHash::KeyType aParamName, michael@0: nsCString aParamValue, michael@0: void* aStatement) michael@0: { michael@0: mozIStorageStatement* stmt = static_cast(aStatement); michael@0: michael@0: nsresult rv = stmt->BindUTF8StringByName(aParamName, aParamValue); michael@0: if (NS_FAILED(rv)) michael@0: return PL_DHASH_STOP; michael@0: michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: michael@0: // nsNavHistory::GetQueryResults michael@0: // michael@0: // Call this to get the results from a complex query. This is used by michael@0: // nsNavHistoryQueryResultNode to populate its children. For simple bookmark michael@0: // queries, use nsNavBookmarks::QueryFolderChildren. michael@0: // michael@0: // THIS DOES NOT DO SORTING. You will need to sort the container yourself michael@0: // when you get the results. This is because sorting depends on tree michael@0: // statistics that will be built from the perspective of the tree. See michael@0: // nsNavHistoryQueryResultNode::FillChildren michael@0: // michael@0: // FIXME: This only does keyword searching for the first query, and does michael@0: // it ANDed with the all the rest of the queries. michael@0: michael@0: nsresult michael@0: nsNavHistory::GetQueryResults(nsNavHistoryQueryResultNode *aResultNode, michael@0: const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions *aOptions, michael@0: nsCOMArray* aResults) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aOptions); michael@0: NS_ASSERTION(aResults->Count() == 0, "Initial result array must be empty"); michael@0: if (! aQueries.Count()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsCString queryString; michael@0: bool paramsPresent = false; michael@0: nsNavHistory::StringHash addParams(HISTORY_DATE_CONT_MAX); michael@0: nsresult rv = ConstructQueryString(aQueries, aOptions, queryString, michael@0: paramsPresent, addParams); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: // create statement michael@0: nsCOMPtr statement = mDB->GetStatement(queryString); michael@0: #ifdef DEBUG michael@0: if (!statement) { michael@0: nsAutoCString lastErrorString; michael@0: (void)mDB->MainConn()->GetLastErrorString(lastErrorString); michael@0: int32_t lastError = 0; michael@0: (void)mDB->MainConn()->GetLastError(&lastError); michael@0: printf("Places failed to create a statement from this query:\n%s\nStorage error (%d): %s\n", michael@0: queryString.get(), lastError, lastErrorString.get()); michael@0: } michael@0: #endif michael@0: NS_ENSURE_STATE(statement); michael@0: mozStorageStatementScoper scoper(statement); michael@0: michael@0: if (paramsPresent) { michael@0: // bind parameters michael@0: int32_t i; michael@0: for (i = 0; i < aQueries.Count(); i++) { michael@0: rv = BindQueryClauseParameters(statement, i, aQueries[i], aOptions); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: addParams.EnumerateRead(BindAdditionalParameter, statement.get()); michael@0: michael@0: // Optimize the case where there is no need for any post-query filtering. michael@0: if (NeedToFilterResultSet(aQueries, aOptions)) { michael@0: // Generate the top-level results. michael@0: nsCOMArray toplevel; michael@0: rv = ResultsAsList(statement, aOptions, &toplevel); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: FilterResultSet(aResultNode, toplevel, aResults, aQueries, aOptions); michael@0: } else { michael@0: rv = ResultsAsList(statement, aOptions, aResults); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::AddObserver michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::AddObserver(nsINavHistoryObserver* aObserver, bool aOwnsWeak) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aObserver); michael@0: michael@0: return mObservers.AppendWeakElement(aObserver, aOwnsWeak); michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RemoveObserver michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemoveObserver(nsINavHistoryObserver* aObserver) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aObserver); michael@0: michael@0: return mObservers.RemoveWeakElement(aObserver); michael@0: } michael@0: michael@0: // nsNavHistory::BeginUpdateBatch michael@0: // See RunInBatchMode michael@0: nsresult michael@0: nsNavHistory::BeginUpdateBatch() michael@0: { michael@0: if (mBatchLevel++ == 0) { michael@0: mBatchDBTransaction = new mozStorageTransaction(mDB->MainConn(), false); michael@0: michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, OnBeginUpdateBatch()); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsNavHistory::EndUpdateBatch michael@0: nsresult michael@0: nsNavHistory::EndUpdateBatch() michael@0: { michael@0: if (--mBatchLevel == 0) { michael@0: if (mBatchDBTransaction) { michael@0: DebugOnly rv = mBatchDBTransaction->Commit(); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Batch failed to commit transaction"); michael@0: delete mBatchDBTransaction; michael@0: mBatchDBTransaction = nullptr; michael@0: } michael@0: michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, OnEndUpdateBatch()); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RunInBatchMode(nsINavHistoryBatchCallback* aCallback, michael@0: nsISupports* aUserData) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aCallback); michael@0: michael@0: UpdateBatchScoper batch(*this); michael@0: return aCallback->RunBatched(aUserData); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetHistoryDisabled(bool *_retval) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG_POINTER(_retval); michael@0: michael@0: *_retval = IsHistoryDisabled(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Browser history ************************************************************* michael@0: michael@0: michael@0: // nsNavHistory::RemovePagesInternal michael@0: // michael@0: // Deletes a list of placeIds from history. michael@0: // This is an internal method used by RemovePages, RemovePagesFromHost and michael@0: // RemovePagesByTimeframe. michael@0: // Takes a comma separated list of place ids. michael@0: // This method does not do any observer notification. michael@0: michael@0: nsresult michael@0: nsNavHistory::RemovePagesInternal(const nsCString& aPlaceIdsQueryString) michael@0: { michael@0: // Return early if there is nothing to delete. michael@0: if (aPlaceIdsQueryString.IsEmpty()) michael@0: return NS_OK; michael@0: michael@0: mozStorageTransaction transaction(mDB->MainConn(), false); michael@0: michael@0: // Delete all visits for the specified place ids. michael@0: nsresult rv = mDB->MainConn()->ExecuteSimpleSQL( michael@0: NS_LITERAL_CSTRING( michael@0: "DELETE FROM moz_historyvisits WHERE place_id IN (") + michael@0: aPlaceIdsQueryString + michael@0: NS_LITERAL_CSTRING(")") michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = CleanupPlacesOnVisitsDelete(aPlaceIdsQueryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Invalidate the cached value for whether there's history or not. michael@0: mDaysOfHistory = -1; michael@0: michael@0: return transaction.Commit(); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Performs cleanup on places that just had all their visits removed, including michael@0: * deletion of those places. This is an internal method used by michael@0: * RemovePagesInternal and RemoveVisitsByTimeframe. This method does not michael@0: * execute in a transaction, so callers should make sure they begin one if michael@0: * needed. michael@0: * michael@0: * @param aPlaceIdsQueryString michael@0: * A comma-separated list of place IDs, each of which just had all its michael@0: * visits removed michael@0: */ michael@0: nsresult michael@0: nsNavHistory::CleanupPlacesOnVisitsDelete(const nsCString& aPlaceIdsQueryString) michael@0: { michael@0: // Return early if there is nothing to delete. michael@0: if (aPlaceIdsQueryString.IsEmpty()) michael@0: return NS_OK; michael@0: michael@0: // Collect about-to-be-deleted URIs to notify onDeleteURI. michael@0: nsCOMPtr stmt = mDB->GetStatement(NS_LITERAL_CSTRING( michael@0: "SELECT h.id, h.url, h.guid, " michael@0: "(SUBSTR(h.url, 1, 6) <> 'place:' " michael@0: " AND NOT EXISTS (SELECT b.id FROM moz_bookmarks b " michael@0: "WHERE b.fk = h.id LIMIT 1)) as whole_entry " michael@0: "FROM moz_places h " michael@0: "WHERE h.id IN ( ") + aPlaceIdsQueryString + NS_LITERAL_CSTRING(")") michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: nsCString filteredPlaceIds; michael@0: nsCOMArray URIs; michael@0: nsTArray GUIDs; michael@0: bool hasMore; michael@0: while (NS_SUCCEEDED(stmt->ExecuteStep(&hasMore)) && hasMore) { michael@0: int64_t placeId; michael@0: nsresult rv = stmt->GetInt64(0, &placeId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nsAutoCString URLString; michael@0: rv = stmt->GetUTF8String(1, URLString); michael@0: nsCString guid; michael@0: rv = stmt->GetUTF8String(2, guid); michael@0: int32_t wholeEntry; michael@0: rv = stmt->GetInt32(3, &wholeEntry); michael@0: nsCOMPtr uri; michael@0: rv = NS_NewURI(getter_AddRefs(uri), URLString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (wholeEntry) { michael@0: if (!filteredPlaceIds.IsEmpty()) { michael@0: filteredPlaceIds.AppendLiteral(","); michael@0: } michael@0: filteredPlaceIds.AppendInt(placeId); michael@0: URIs.AppendObject(uri); michael@0: GUIDs.AppendElement(guid); michael@0: } michael@0: else { michael@0: // Notify that we will delete all visits for this page, but not the page michael@0: // itself, since it's bookmarked or a place: query. michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnDeleteVisits(uri, 0, guid, nsINavHistoryObserver::REASON_DELETED, 0)); michael@0: } michael@0: } michael@0: michael@0: // if the entry is not bookmarked and is not a place: uri michael@0: // then we can remove it from moz_places. michael@0: // Note that we do NOT delete favicons. Any unreferenced favicons will be michael@0: // deleted next time the browser is shut down. michael@0: nsresult rv = mDB->MainConn()->ExecuteSimpleSQL( michael@0: NS_LITERAL_CSTRING( michael@0: "DELETE FROM moz_places WHERE id IN ( " michael@0: ) + filteredPlaceIds + NS_LITERAL_CSTRING( michael@0: ") " michael@0: ) michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Invalidate frecencies of touched places, since they need recalculation. michael@0: rv = invalidateFrecencies(aPlaceIdsQueryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Finally notify about the removed URIs. michael@0: for (int32_t i = 0; i < URIs.Count(); ++i) { michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnDeleteURI(URIs[i], GUIDs[i], nsINavHistoryObserver::REASON_DELETED)); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RemovePages michael@0: // michael@0: // Removes a bunch of uris from history. michael@0: // Has better performance than RemovePage when deleting a lot of history. michael@0: // We don't do duplicates removal, URIs array should be cleaned-up before. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemovePages(nsIURI **aURIs, uint32_t aLength) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURIs); michael@0: michael@0: nsresult rv; michael@0: // build a list of place ids to delete michael@0: nsCString deletePlaceIdsQueryString; michael@0: for (uint32_t i = 0; i < aLength; i++) { michael@0: int64_t placeId; michael@0: nsAutoCString guid; michael@0: rv = GetIdForPage(aURIs[i], &placeId, guid); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (placeId != 0) { michael@0: if (!deletePlaceIdsQueryString.IsEmpty()) michael@0: deletePlaceIdsQueryString.AppendLiteral(","); michael@0: deletePlaceIdsQueryString.AppendInt(placeId); michael@0: } michael@0: } michael@0: michael@0: UpdateBatchScoper batch(*this); // sends Begin/EndUpdateBatch to observers michael@0: michael@0: rv = RemovePagesInternal(deletePlaceIdsQueryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Clear the registered embed visits. michael@0: clearEmbedVisits(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RemovePage michael@0: // michael@0: // Removes all visits and the main history entry for the given URI. michael@0: // Silently fails if we have no knowledge of the page. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemovePage(nsIURI *aURI) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: // Build a list of place ids to delete. michael@0: int64_t placeId; michael@0: nsAutoCString guid; michael@0: nsresult rv = GetIdForPage(aURI, &placeId, guid); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (placeId == 0) { michael@0: return NS_OK; michael@0: } michael@0: nsAutoCString deletePlaceIdQueryString; michael@0: deletePlaceIdQueryString.AppendInt(placeId); michael@0: michael@0: rv = RemovePagesInternal(deletePlaceIdQueryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Clear the registered embed visits. michael@0: clearEmbedVisits(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RemovePagesFromHost michael@0: // michael@0: // This function will delete all history information about pages from a michael@0: // given host. If aEntireDomain is set, we will also delete pages from michael@0: // sub hosts (so if we are passed in "microsoft.com" we delete michael@0: // "www.microsoft.com", "msdn.microsoft.com", etc.). An empty host name michael@0: // means local files and anything else with no host name. You can also pass michael@0: // in the localized "(local files)" title given to you from a history query. michael@0: // michael@0: // Silently fails if we have no knowledge of the host. michael@0: // michael@0: // This sends onBeginUpdateBatch/onEndUpdateBatch to observers michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemovePagesFromHost(const nsACString& aHost, bool aEntireDomain) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: nsresult rv; michael@0: // Local files don't have any host name. We don't want to delete all files in michael@0: // history when we get passed an empty string, so force to exact match michael@0: if (aHost.IsEmpty()) michael@0: aEntireDomain = false; michael@0: michael@0: // translate "(local files)" to an empty host name michael@0: // be sure to use the TitleForDomain to get the localized name michael@0: nsCString localFiles; michael@0: TitleForDomain(EmptyCString(), localFiles); michael@0: nsAutoString host16; michael@0: if (!aHost.Equals(localFiles)) michael@0: CopyUTF8toUTF16(aHost, host16); michael@0: michael@0: // nsISupports version of the host string for passing to observers michael@0: nsCOMPtr hostSupports(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = hostSupports->SetData(host16); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // see BindQueryClauseParameters for how this host selection works michael@0: nsAutoString revHostDot; michael@0: GetReversedHostname(host16, revHostDot); michael@0: NS_ASSERTION(revHostDot[revHostDot.Length() - 1] == '.', "Invalid rev. host"); michael@0: nsAutoString revHostSlash(revHostDot); michael@0: revHostSlash.Truncate(revHostSlash.Length() - 1); michael@0: revHostSlash.Append(NS_LITERAL_STRING("/")); michael@0: michael@0: // build condition string based on host selection type michael@0: nsAutoCString conditionString; michael@0: if (aEntireDomain) michael@0: conditionString.AssignLiteral("rev_host >= ?1 AND rev_host < ?2 "); michael@0: else michael@0: conditionString.AssignLiteral("rev_host = ?1 "); michael@0: michael@0: // create statement depending on delete type michael@0: nsCOMPtr statement = mDB->GetStatement( michael@0: NS_LITERAL_CSTRING("SELECT id FROM moz_places WHERE ") + conditionString michael@0: ); michael@0: NS_ENSURE_STATE(statement); michael@0: mozStorageStatementScoper scoper(statement); michael@0: michael@0: rv = statement->BindStringByIndex(0, revHostDot); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (aEntireDomain) { michael@0: rv = statement->BindStringByIndex(1, revHostSlash); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: nsCString hostPlaceIds; michael@0: bool hasMore = false; michael@0: while (NS_SUCCEEDED(statement->ExecuteStep(&hasMore)) && hasMore) { michael@0: if (!hostPlaceIds.IsEmpty()) michael@0: hostPlaceIds.AppendLiteral(","); michael@0: int64_t placeId; michael@0: rv = statement->GetInt64(0, &placeId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: hostPlaceIds.AppendInt(placeId); michael@0: } michael@0: michael@0: // force a full refresh calling onEndUpdateBatch (will call Refresh()) michael@0: UpdateBatchScoper batch(*this); // sends Begin/EndUpdateBatch to observers michael@0: michael@0: rv = RemovePagesInternal(hostPlaceIds); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Clear the registered embed visits. michael@0: clearEmbedVisits(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RemovePagesByTimeframe michael@0: // michael@0: // This function will delete all history information about michael@0: // pages for a given timeframe. michael@0: // Limits are included: aBeginTime <= timeframe <= aEndTime michael@0: // michael@0: // This method sends onBeginUpdateBatch/onEndUpdateBatch to observers michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemovePagesByTimeframe(PRTime aBeginTime, PRTime aEndTime) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: nsresult rv; michael@0: // build a list of place ids to delete michael@0: nsCString deletePlaceIdsQueryString; michael@0: michael@0: // we only need to know if a place has a visit into the given timeframe michael@0: // this query is faster than actually selecting in moz_historyvisits michael@0: nsCOMPtr selectByTime = mDB->GetStatement( michael@0: "SELECT h.id FROM moz_places h WHERE " michael@0: "EXISTS " michael@0: "(SELECT id FROM moz_historyvisits v WHERE v.place_id = h.id " michael@0: "AND v.visit_date >= :from_date AND v.visit_date <= :to_date LIMIT 1)" michael@0: ); michael@0: NS_ENSURE_STATE(selectByTime); michael@0: mozStorageStatementScoper selectByTimeScoper(selectByTime); michael@0: michael@0: rv = selectByTime->BindInt64ByName(NS_LITERAL_CSTRING("from_date"), aBeginTime); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = selectByTime->BindInt64ByName(NS_LITERAL_CSTRING("to_date"), aEndTime); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasMore = false; michael@0: while (NS_SUCCEEDED(selectByTime->ExecuteStep(&hasMore)) && hasMore) { michael@0: int64_t placeId; michael@0: rv = selectByTime->GetInt64(0, &placeId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (placeId != 0) { michael@0: if (!deletePlaceIdsQueryString.IsEmpty()) michael@0: deletePlaceIdsQueryString.AppendLiteral(","); michael@0: deletePlaceIdsQueryString.AppendInt(placeId); michael@0: } michael@0: } michael@0: michael@0: // force a full refresh calling onEndUpdateBatch (will call Refresh()) michael@0: UpdateBatchScoper batch(*this); // sends Begin/EndUpdateBatch to observers michael@0: michael@0: rv = RemovePagesInternal(deletePlaceIdsQueryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Clear the registered embed visits. michael@0: clearEmbedVisits(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Removes all visits in a given timeframe. Limits are included: michael@0: * aBeginTime <= timeframe <= aEndTime. Any place that becomes unvisited michael@0: * as a result will also be deleted. michael@0: * michael@0: * Note that removal is performed in batch, so observers will not be michael@0: * notified of individual places that are deleted. Instead they will be michael@0: * notified onBeginUpdateBatch and onEndUpdateBatch. michael@0: * michael@0: * @param aBeginTime michael@0: * The start of the timeframe, inclusive michael@0: * @param aEndTime michael@0: * The end of the timeframe, inclusive michael@0: */ michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemoveVisitsByTimeframe(PRTime aBeginTime, PRTime aEndTime) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: nsresult rv; michael@0: michael@0: // Build a list of place IDs whose visits fall entirely within the timespan. michael@0: // These places will be deleted by the call to CleanupPlacesOnVisitsDelete michael@0: // below. michael@0: nsCString deletePlaceIdsQueryString; michael@0: { michael@0: nsCOMPtr selectByTime = mDB->GetStatement( michael@0: "SELECT place_id " michael@0: "FROM moz_historyvisits " michael@0: "WHERE :from_date <= visit_date AND visit_date <= :to_date " michael@0: "EXCEPT " michael@0: "SELECT place_id " michael@0: "FROM moz_historyvisits " michael@0: "WHERE visit_date < :from_date OR :to_date < visit_date" michael@0: ); michael@0: NS_ENSURE_STATE(selectByTime); michael@0: mozStorageStatementScoper selectByTimeScoper(selectByTime); michael@0: rv = selectByTime->BindInt64ByName(NS_LITERAL_CSTRING("from_date"), aBeginTime); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = selectByTime->BindInt64ByName(NS_LITERAL_CSTRING("to_date"), aEndTime); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasMore = false; michael@0: while (NS_SUCCEEDED(selectByTime->ExecuteStep(&hasMore)) && hasMore) { michael@0: int64_t placeId; michael@0: rv = selectByTime->GetInt64(0, &placeId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: // placeId should not be <= 0, but be defensive. michael@0: if (placeId > 0) { michael@0: if (!deletePlaceIdsQueryString.IsEmpty()) michael@0: deletePlaceIdsQueryString.AppendLiteral(","); michael@0: deletePlaceIdsQueryString.AppendInt(placeId); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // force a full refresh calling onEndUpdateBatch (will call Refresh()) michael@0: UpdateBatchScoper batch(*this); // sends Begin/EndUpdateBatch to observers michael@0: michael@0: mozStorageTransaction transaction(mDB->MainConn(), false); michael@0: michael@0: // Delete all visits within the timeframe. michael@0: nsCOMPtr deleteVisitsStmt = mDB->GetStatement( michael@0: "DELETE FROM moz_historyvisits " michael@0: "WHERE :from_date <= visit_date AND visit_date <= :to_date" michael@0: ); michael@0: NS_ENSURE_STATE(deleteVisitsStmt); michael@0: mozStorageStatementScoper deletevisitsScoper(deleteVisitsStmt); michael@0: michael@0: rv = deleteVisitsStmt->BindInt64ByName(NS_LITERAL_CSTRING("from_date"), aBeginTime); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = deleteVisitsStmt->BindInt64ByName(NS_LITERAL_CSTRING("to_date"), aEndTime); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = deleteVisitsStmt->Execute(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = CleanupPlacesOnVisitsDelete(deletePlaceIdsQueryString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = transaction.Commit(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Clear the registered embed visits. michael@0: clearEmbedVisits(); michael@0: michael@0: // Invalidate the cached value for whether there's history or not. michael@0: mDaysOfHistory = -1; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RemoveAllPages michael@0: // michael@0: // This function is used to clear history. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::RemoveAllPages() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: nsresult rv = mDB->MainConn()->ExecuteSimpleSQL(NS_LITERAL_CSTRING( michael@0: "DELETE FROM moz_historyvisits" michael@0: )); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Clear the registered embed visits. michael@0: clearEmbedVisits(); michael@0: michael@0: // Update the cached value for whether there's history or not. michael@0: mDaysOfHistory = 0; michael@0: michael@0: // Expiration will take care of orphans. michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, OnClearHistory()); michael@0: michael@0: // Invalidate frecencies for the remaining places. This must happen michael@0: // after the notification to ensure it runs enqueued to expiration. michael@0: rv = invalidateFrecencies(EmptyCString()); michael@0: NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "failed to fix invalid frecencies"); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // Call this method before visiting a URL in order to help determine the michael@0: // transition type of the visit. michael@0: // michael@0: // @see MarkPageAsFollowedBookmark michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::MarkPageAsTyped(nsIURI *aURI) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: // don't add when history is disabled michael@0: if (IsHistoryDisabled()) michael@0: return NS_OK; michael@0: michael@0: nsAutoCString uriString; michael@0: nsresult rv = aURI->GetSpec(uriString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // if URL is already in the typed queue, then we need to remove the old one michael@0: int64_t unusedEventTime; michael@0: if (mRecentTyped.Get(uriString, &unusedEventTime)) michael@0: mRecentTyped.Remove(uriString); michael@0: michael@0: if (mRecentTyped.Count() > RECENT_EVENT_QUEUE_MAX_LENGTH) michael@0: ExpireNonrecentEvents(&mRecentTyped); michael@0: michael@0: mRecentTyped.Put(uriString, GetNow()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // Call this method before visiting a URL in order to help determine the michael@0: // transition type of the visit. michael@0: // michael@0: // @see MarkPageAsTyped michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::MarkPageAsFollowedLink(nsIURI *aURI) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: // don't add when history is disabled michael@0: if (IsHistoryDisabled()) michael@0: return NS_OK; michael@0: michael@0: nsAutoCString uriString; michael@0: nsresult rv = aURI->GetSpec(uriString); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // if URL is already in the links queue, then we need to remove the old one michael@0: int64_t unusedEventTime; michael@0: if (mRecentLink.Get(uriString, &unusedEventTime)) michael@0: mRecentLink.Remove(uriString); michael@0: michael@0: if (mRecentLink.Count() > RECENT_EVENT_QUEUE_MAX_LENGTH) michael@0: ExpireNonrecentEvents(&mRecentLink); michael@0: michael@0: mRecentLink.Put(uriString, GetNow()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::SetCharsetForURI michael@0: // michael@0: // Sets the character-set for a URI. michael@0: // If aCharset is empty remove character-set annotation for aURI. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::SetCharsetForURI(nsIURI* aURI, michael@0: const nsAString& aCharset) michael@0: { michael@0: PLACES_WARN_DEPRECATED(); michael@0: michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService(); michael@0: NS_ENSURE_TRUE(annosvc, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: if (aCharset.IsEmpty()) { michael@0: // remove the current page character-set annotation michael@0: nsresult rv = annosvc->RemovePageAnnotation(aURI, CHARSET_ANNO); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: else { michael@0: // Set page character-set annotation, silently overwrite if already exists michael@0: nsresult rv = annosvc->SetPageAnnotationString(aURI, CHARSET_ANNO, michael@0: aCharset, 0, michael@0: nsAnnotationService::EXPIRE_NEVER); michael@0: if (rv == NS_ERROR_INVALID_ARG) { michael@0: // We don't have this page. Silently fail. michael@0: return NS_OK; michael@0: } michael@0: else if (NS_FAILED(rv)) michael@0: return rv; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::GetCharsetForURI michael@0: // michael@0: // Get the last saved character-set for a URI. michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetCharsetForURI(nsIURI* aURI, michael@0: nsAString& aCharset) michael@0: { michael@0: PLACES_WARN_DEPRECATED(); michael@0: michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: nsAnnotationService* annosvc = nsAnnotationService::GetAnnotationService(); michael@0: NS_ENSURE_TRUE(annosvc, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: nsAutoString charset; michael@0: nsresult rv = annosvc->GetPageAnnotationString(aURI, CHARSET_ANNO, aCharset); michael@0: if (NS_FAILED(rv)) { michael@0: // be sure to return an empty string if character-set is not found michael@0: aCharset.Truncate(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetPageTitle(nsIURI* aURI, nsAString& aTitle) michael@0: { michael@0: PLACES_WARN_DEPRECATED(); michael@0: michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aURI); michael@0: michael@0: aTitle.Truncate(0); michael@0: michael@0: nsCOMPtr stmt = mDB->GetStatement( michael@0: "SELECT id, url, title, rev_host, visit_count, guid " michael@0: "FROM moz_places " michael@0: "WHERE url = :page_url " michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), aURI); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasResults = false; michael@0: rv = stmt->ExecuteStep(&hasResults); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!hasResults) { michael@0: aTitle.SetIsVoid(true); michael@0: return NS_OK; // Not found, return a void string. michael@0: } michael@0: michael@0: rv = stmt->GetString(2, aTitle); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// mozIStorageVacuumParticipant michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetDatabaseConnection(mozIStorageConnection** _DBConnection) michael@0: { michael@0: return GetDBConnection(_DBConnection); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetExpectedDatabasePageSize(int32_t* _expectedPageSize) michael@0: { michael@0: NS_ENSURE_STATE(mDB); michael@0: NS_ENSURE_STATE(mDB->MainConn()); michael@0: return mDB->MainConn()->GetDefaultPageSize(_expectedPageSize); michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::OnBeginVacuum(bool* _vacuumGranted) michael@0: { michael@0: // TODO: Check if we have to deny the vacuum in some heavy-load case. michael@0: // We could maybe want to do that during batches? michael@0: *_vacuumGranted = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::OnEndVacuum(bool aSucceeded) michael@0: { michael@0: NS_WARN_IF_FALSE(aSucceeded, "Places.sqlite vacuum failed."); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// nsPIPlacesDatabase michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::GetDBConnection(mozIStorageConnection **_DBConnection) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(_DBConnection); michael@0: nsRefPtr connection = mDB->MainConn(); michael@0: connection.forget(_DBConnection); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::AsyncExecuteLegacyQueries(nsINavHistoryQuery** aQueries, michael@0: uint32_t aQueryCount, michael@0: nsINavHistoryQueryOptions* aOptions, michael@0: mozIStorageStatementCallback* aCallback, michael@0: mozIStoragePendingStatement** _stmt) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: NS_ENSURE_ARG(aQueries); michael@0: NS_ENSURE_ARG(aOptions); michael@0: NS_ENSURE_ARG(aCallback); michael@0: NS_ENSURE_ARG_POINTER(_stmt); michael@0: michael@0: nsCOMArray queries; michael@0: for (uint32_t i = 0; i < aQueryCount; i ++) { michael@0: nsCOMPtr query = do_QueryInterface(aQueries[i]); michael@0: NS_ENSURE_STATE(query); michael@0: queries.AppendObject(query); michael@0: } michael@0: NS_ENSURE_ARG_MIN(queries.Count(), 1); michael@0: michael@0: nsCOMPtr options = do_QueryInterface(aOptions); michael@0: NS_ENSURE_ARG(options); michael@0: michael@0: nsCString queryString; michael@0: bool paramsPresent = false; michael@0: nsNavHistory::StringHash addParams(HISTORY_DATE_CONT_MAX); michael@0: nsresult rv = ConstructQueryString(queries, options, queryString, michael@0: paramsPresent, addParams); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: nsCOMPtr statement = michael@0: mDB->GetAsyncStatement(queryString); michael@0: NS_ENSURE_STATE(statement); michael@0: michael@0: #ifdef DEBUG michael@0: if (NS_FAILED(rv)) { michael@0: nsAutoCString lastErrorString; michael@0: (void)mDB->MainConn()->GetLastErrorString(lastErrorString); michael@0: int32_t lastError = 0; michael@0: (void)mDB->MainConn()->GetLastError(&lastError); michael@0: printf("Places failed to create a statement from this query:\n%s\nStorage error (%d): %s\n", michael@0: queryString.get(), lastError, lastErrorString.get()); michael@0: } michael@0: #endif michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (paramsPresent) { michael@0: // bind parameters michael@0: int32_t i; michael@0: for (i = 0; i < queries.Count(); i++) { michael@0: rv = BindQueryClauseParameters(statement, i, queries[i], options); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: addParams.EnumerateRead(BindAdditionalParameter, statement.get()); michael@0: michael@0: rv = statement->ExecuteAsync(aCallback, _stmt); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsPIPlacesHistoryListenersNotifier ****************************************** michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::NotifyOnPageExpired(nsIURI *aURI, PRTime aVisitTime, michael@0: bool aWholeEntry, const nsACString& aGUID, michael@0: uint16_t aReason, uint32_t aTransitionType) michael@0: { michael@0: // Invalidate the cached value for whether there's history or not. michael@0: mDaysOfHistory = -1; michael@0: michael@0: MOZ_ASSERT(!aGUID.IsEmpty()); michael@0: if (aWholeEntry) { michael@0: // Notify our observers that the page has been removed. michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, OnDeleteURI(aURI, aGUID, aReason)); michael@0: } michael@0: else { michael@0: // Notify our observers that some visits for the page have been removed. michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnDeleteVisits(aURI, aVisitTime, aGUID, aReason, michael@0: aTransitionType)); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// nsIObserver michael@0: michael@0: NS_IMETHODIMP michael@0: nsNavHistory::Observe(nsISupports *aSubject, const char *aTopic, michael@0: const char16_t *aData) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: if (strcmp(aTopic, TOPIC_PROFILE_TEARDOWN) == 0 || michael@0: strcmp(aTopic, TOPIC_PROFILE_CHANGE) == 0) { michael@0: // These notifications are used by tests to simulate a Places shutdown. michael@0: // They should just be forwarded to the Database handle. michael@0: mDB->Observe(aSubject, aTopic, aData); michael@0: } michael@0: michael@0: else if (strcmp(aTopic, TOPIC_PLACES_CONNECTION_CLOSED) == 0) { michael@0: // Don't even try to notify observers from this point on, the category michael@0: // cache would init services that could try to use our APIs. michael@0: mCanNotify = false; michael@0: } michael@0: michael@0: #ifdef MOZ_XUL michael@0: else if (strcmp(aTopic, TOPIC_AUTOCOMPLETE_FEEDBACK_INCOMING) == 0) { michael@0: nsCOMPtr input = do_QueryInterface(aSubject); michael@0: if (!input) michael@0: return NS_OK; michael@0: michael@0: // If the source is a private window, don't add any input history. michael@0: bool isPrivate; michael@0: nsresult rv = input->GetInPrivateContext(&isPrivate); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (isPrivate) michael@0: return NS_OK; michael@0: michael@0: nsCOMPtr popup; michael@0: input->GetPopup(getter_AddRefs(popup)); michael@0: if (!popup) michael@0: return NS_OK; michael@0: michael@0: nsCOMPtr controller; michael@0: input->GetController(getter_AddRefs(controller)); michael@0: if (!controller) michael@0: return NS_OK; michael@0: michael@0: // Don't bother if the popup is closed michael@0: bool open; michael@0: rv = popup->GetPopupOpen(&open); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!open) michael@0: return NS_OK; michael@0: michael@0: // Ignore if nothing selected from the popup michael@0: int32_t selectedIndex; michael@0: rv = popup->GetSelectedIndex(&selectedIndex); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (selectedIndex == -1) michael@0: return NS_OK; michael@0: michael@0: rv = AutoCompleteFeedback(selectedIndex, controller); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: #endif michael@0: else if (strcmp(aTopic, TOPIC_PREF_CHANGED) == 0) { michael@0: LoadPrefs(); michael@0: } michael@0: michael@0: else if (strcmp(aTopic, TOPIC_IDLE_DAILY) == 0) { michael@0: (void)DecayFrecency(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: namespace { michael@0: michael@0: class DecayFrecencyCallback : public AsyncStatementTelemetryTimer michael@0: { michael@0: public: michael@0: DecayFrecencyCallback() michael@0: : AsyncStatementTelemetryTimer(Telemetry::PLACES_IDLE_FRECENCY_DECAY_TIME_MS) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD HandleCompletion(uint16_t aReason) michael@0: { michael@0: (void)AsyncStatementTelemetryTimer::HandleCompletion(aReason); michael@0: if (aReason == REASON_FINISHED) { michael@0: nsNavHistory *navHistory = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_STATE(navHistory); michael@0: navHistory->NotifyManyFrecenciesChanged(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: nsresult michael@0: nsNavHistory::DecayFrecency() michael@0: { michael@0: nsresult rv = FixInvalidFrecencies(); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Globally decay places frecency rankings to estimate reduced frecency michael@0: // values of pages that haven't been visited for a while, i.e., they do michael@0: // not get an updated frecency. A scaling factor of .975 results in .5 the michael@0: // original value after 28 days. michael@0: // When changing the scaling factor, ensure that the barrier in michael@0: // moz_places_afterupdate_frecency_trigger still ignores these changes. michael@0: nsCOMPtr decayFrecency = mDB->GetAsyncStatement( michael@0: "UPDATE moz_places SET frecency = ROUND(frecency * .975) " michael@0: "WHERE frecency > 0" michael@0: ); michael@0: NS_ENSURE_STATE(decayFrecency); michael@0: michael@0: // Decay potentially unused adaptive entries (e.g. those that are at 1) michael@0: // to allow better chances for new entries that will start at 1. michael@0: nsCOMPtr decayAdaptive = mDB->GetAsyncStatement( michael@0: "UPDATE moz_inputhistory SET use_count = use_count * .975" michael@0: ); michael@0: NS_ENSURE_STATE(decayAdaptive); michael@0: michael@0: // Delete any adaptive entries that won't help in ordering anymore. michael@0: nsCOMPtr deleteAdaptive = mDB->GetAsyncStatement( michael@0: "DELETE FROM moz_inputhistory WHERE use_count < .01" michael@0: ); michael@0: NS_ENSURE_STATE(deleteAdaptive); michael@0: michael@0: mozIStorageBaseStatement *stmts[] = { michael@0: decayFrecency.get(), michael@0: decayAdaptive.get(), michael@0: deleteAdaptive.get() michael@0: }; michael@0: nsCOMPtr ps; michael@0: nsRefPtr cb = new DecayFrecencyCallback(); michael@0: rv = mDB->MainConn()->ExecuteAsync(stmts, ArrayLength(stmts), cb, michael@0: getter_AddRefs(ps)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // Query stuff ***************************************************************** michael@0: michael@0: // Helper class for QueryToSelectClause michael@0: // michael@0: // This class helps to build part of the WHERE clause. It supports michael@0: // multiple queries by appending the query index to the parameter name. michael@0: // For the query with index 0 the parameter name is not altered what michael@0: // allows using this parameter in other situations (see SelectAsSite). michael@0: michael@0: class ConditionBuilder michael@0: { michael@0: public: michael@0: michael@0: ConditionBuilder(int32_t aQueryIndex): mQueryIndex(aQueryIndex) michael@0: { } michael@0: michael@0: ConditionBuilder& Condition(const char* aStr) michael@0: { michael@0: if (!mClause.IsEmpty()) michael@0: mClause.AppendLiteral(" AND "); michael@0: Str(aStr); michael@0: return *this; michael@0: } michael@0: michael@0: ConditionBuilder& Str(const char* aStr) michael@0: { michael@0: mClause.Append(' '); michael@0: mClause.Append(aStr); michael@0: mClause.Append(' '); michael@0: return *this; michael@0: } michael@0: michael@0: ConditionBuilder& Param(const char* aParam) michael@0: { michael@0: mClause.Append(' '); michael@0: if (!mQueryIndex) michael@0: mClause.Append(aParam); michael@0: else michael@0: mClause += nsPrintfCString("%s%d", aParam, mQueryIndex); michael@0: michael@0: mClause.Append(' '); michael@0: return *this; michael@0: } michael@0: michael@0: void GetClauseString(nsCString& aResult) michael@0: { michael@0: aResult = mClause; michael@0: } michael@0: michael@0: private: michael@0: michael@0: int32_t mQueryIndex; michael@0: nsCString mClause; michael@0: }; michael@0: michael@0: michael@0: // nsNavHistory::QueryToSelectClause michael@0: // michael@0: // THE BEHAVIOR SHOULD BE IN SYNC WITH BindQueryClauseParameters michael@0: // michael@0: // I don't check return values from the query object getters because there's michael@0: // no way for those to fail. michael@0: michael@0: nsresult michael@0: nsNavHistory::QueryToSelectClause(nsNavHistoryQuery* aQuery, // const michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: int32_t aQueryIndex, michael@0: nsCString* aClause) michael@0: { michael@0: bool hasIt; michael@0: bool excludeQueries = aOptions->ExcludeQueries(); michael@0: michael@0: ConditionBuilder clause(aQueryIndex); michael@0: michael@0: if ((NS_SUCCEEDED(aQuery->GetHasBeginTime(&hasIt)) && hasIt) || michael@0: (NS_SUCCEEDED(aQuery->GetHasEndTime(&hasIt)) && hasIt)) { michael@0: clause.Condition("EXISTS (SELECT 1 FROM moz_historyvisits " michael@0: "WHERE place_id = h.id"); michael@0: // begin time michael@0: if (NS_SUCCEEDED(aQuery->GetHasBeginTime(&hasIt)) && hasIt) michael@0: clause.Condition("visit_date >=").Param(":begin_time"); michael@0: // end time michael@0: if (NS_SUCCEEDED(aQuery->GetHasEndTime(&hasIt)) && hasIt) michael@0: clause.Condition("visit_date <=").Param(":end_time"); michael@0: clause.Str(" LIMIT 1)"); michael@0: } michael@0: michael@0: // search terms michael@0: bool hasSearchTerms; michael@0: if (NS_SUCCEEDED(aQuery->GetHasSearchTerms(&hasSearchTerms)) && hasSearchTerms) { michael@0: // Re-use the autocomplete_match function. Setting the behavior to 0 michael@0: // it can match everything and work as a nice case insensitive comparator. michael@0: clause.Condition("AUTOCOMPLETE_MATCH(").Param(":search_string") michael@0: .Str(", h.url, page_title, tags, ") michael@0: .Str(nsPrintfCString("0, 0, 0, 0, %d, 0)", michael@0: mozIPlacesAutoComplete::MATCH_ANYWHERE_UNMODIFIED).get()); michael@0: // Serching by terms implicitly exclude queries. michael@0: excludeQueries = true; michael@0: } michael@0: michael@0: // min and max visit count michael@0: if (aQuery->MinVisits() >= 0) michael@0: clause.Condition("h.visit_count >=").Param(":min_visits"); michael@0: michael@0: if (aQuery->MaxVisits() >= 0) michael@0: clause.Condition("h.visit_count <=").Param(":max_visits"); michael@0: michael@0: // only bookmarked, has no affect on bookmarks-only queries michael@0: if (aOptions->QueryType() != nsINavHistoryQueryOptions::QUERY_TYPE_BOOKMARKS && michael@0: aQuery->OnlyBookmarked()) michael@0: clause.Condition("EXISTS (SELECT b.fk FROM moz_bookmarks b WHERE b.type = ") michael@0: .Str(nsPrintfCString("%d", nsNavBookmarks::TYPE_BOOKMARK).get()) michael@0: .Str("AND b.fk = h.id)"); michael@0: michael@0: // domain michael@0: if (NS_SUCCEEDED(aQuery->GetHasDomain(&hasIt)) && hasIt) { michael@0: bool domainIsHost = false; michael@0: aQuery->GetDomainIsHost(&domainIsHost); michael@0: if (domainIsHost) michael@0: clause.Condition("h.rev_host =").Param(":domain_lower"); michael@0: else michael@0: // see domain setting in BindQueryClauseParameters for why we do this michael@0: clause.Condition("h.rev_host >=").Param(":domain_lower") michael@0: .Condition("h.rev_host <").Param(":domain_upper"); michael@0: } michael@0: michael@0: // URI michael@0: if (NS_SUCCEEDED(aQuery->GetHasUri(&hasIt)) && hasIt) { michael@0: if (aQuery->UriIsPrefix()) { michael@0: clause.Condition("h.url >= ").Param(":uri") michael@0: .Condition("h.url <= ").Param(":uri_upper"); michael@0: } michael@0: else michael@0: clause.Condition("h.url =").Param(":uri"); michael@0: } michael@0: michael@0: // annotation michael@0: aQuery->GetHasAnnotation(&hasIt); michael@0: if (hasIt) { michael@0: clause.Condition(""); michael@0: if (aQuery->AnnotationIsNot()) michael@0: clause.Str("NOT"); michael@0: clause.Str( michael@0: "EXISTS " michael@0: "(SELECT h.id " michael@0: "FROM moz_annos anno " michael@0: "JOIN moz_anno_attributes annoname " michael@0: "ON anno.anno_attribute_id = annoname.id " michael@0: "WHERE anno.place_id = h.id " michael@0: "AND annoname.name = ").Param(":anno").Str(")"); michael@0: // annotation-based queries don't get the common conditions, so you get michael@0: // all URLs with that annotation michael@0: } michael@0: michael@0: // tags michael@0: const nsTArray &tags = aQuery->Tags(); michael@0: if (tags.Length() > 0) { michael@0: clause.Condition("h.id"); michael@0: if (aQuery->TagsAreNot()) michael@0: clause.Str("NOT"); michael@0: clause.Str( michael@0: "IN " michael@0: "(SELECT bms.fk " michael@0: "FROM moz_bookmarks bms " michael@0: "JOIN moz_bookmarks tags ON bms.parent = tags.id " michael@0: "WHERE tags.parent ="). michael@0: Param(":tags_folder"). michael@0: Str("AND tags.title IN ("); michael@0: for (uint32_t i = 0; i < tags.Length(); ++i) { michael@0: nsPrintfCString param(":tag%d_", i); michael@0: clause.Param(param.get()); michael@0: if (i < tags.Length() - 1) michael@0: clause.Str(","); michael@0: } michael@0: clause.Str(")"); michael@0: if (!aQuery->TagsAreNot()) michael@0: clause.Str("GROUP BY bms.fk HAVING count(*) >=").Param(":tag_count"); michael@0: clause.Str(")"); michael@0: } michael@0: michael@0: // transitions michael@0: const nsTArray& transitions = aQuery->Transitions(); michael@0: for (uint32_t i = 0; i < transitions.Length(); ++i) { michael@0: nsPrintfCString param(":transition%d_", i); michael@0: clause.Condition("h.id IN (SELECT place_id FROM moz_historyvisits " michael@0: "WHERE visit_type = ") michael@0: .Param(param.get()) michael@0: .Str(")"); michael@0: } michael@0: michael@0: // folders michael@0: const nsTArray& folders = aQuery->Folders(); michael@0: if (folders.Length() > 0) { michael@0: nsTArray includeFolders; michael@0: includeFolders.AppendElements(folders); michael@0: michael@0: nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService(); michael@0: NS_ENSURE_STATE(bookmarks); michael@0: michael@0: for (nsTArray::size_type i = 0; i < folders.Length(); ++i) { michael@0: nsTArray subFolders; michael@0: if (NS_FAILED(bookmarks->GetDescendantFolders(folders[i], subFolders))) michael@0: continue; michael@0: includeFolders.AppendElements(subFolders); michael@0: } michael@0: michael@0: clause.Condition("b.parent IN("); michael@0: for (nsTArray::size_type i = 0; i < includeFolders.Length(); ++i) { michael@0: clause.Str(nsPrintfCString("%lld", includeFolders[i]).get()); michael@0: if (i < includeFolders.Length() - 1) { michael@0: clause.Str(","); michael@0: } michael@0: } michael@0: clause.Str(")"); michael@0: } michael@0: michael@0: if (excludeQueries) { michael@0: // Serching by terms implicitly exclude queries. michael@0: clause.Condition("NOT h.url BETWEEN 'place:' AND 'place;'"); michael@0: } michael@0: michael@0: clause.GetClauseString(*aClause); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::BindQueryClauseParameters michael@0: // michael@0: // THE BEHAVIOR SHOULD BE IN SYNC WITH QueryToSelectClause michael@0: michael@0: nsresult michael@0: nsNavHistory::BindQueryClauseParameters(mozIStorageBaseStatement* statement, michael@0: int32_t aQueryIndex, michael@0: nsNavHistoryQuery* aQuery, // const michael@0: nsNavHistoryQueryOptions* aOptions) michael@0: { michael@0: nsresult rv; michael@0: michael@0: bool hasIt; michael@0: // Append numbered index to param names, to replace them correctly in michael@0: // case of multiple queries. If we have just one query we don't change the michael@0: // param name though. michael@0: nsAutoCString qIndex; michael@0: if (aQueryIndex > 0) michael@0: qIndex.AppendInt(aQueryIndex); michael@0: michael@0: // begin time michael@0: if (NS_SUCCEEDED(aQuery->GetHasBeginTime(&hasIt)) && hasIt) { michael@0: PRTime time = NormalizeTime(aQuery->BeginTimeReference(), michael@0: aQuery->BeginTime()); michael@0: rv = statement->BindInt64ByName( michael@0: NS_LITERAL_CSTRING("begin_time") + qIndex, time); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // end time michael@0: if (NS_SUCCEEDED(aQuery->GetHasEndTime(&hasIt)) && hasIt) { michael@0: PRTime time = NormalizeTime(aQuery->EndTimeReference(), michael@0: aQuery->EndTime()); michael@0: rv = statement->BindInt64ByName( michael@0: NS_LITERAL_CSTRING("end_time") + qIndex, time michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // search terms michael@0: if (NS_SUCCEEDED(aQuery->GetHasSearchTerms(&hasIt)) && hasIt) { michael@0: rv = statement->BindStringByName( michael@0: NS_LITERAL_CSTRING("search_string") + qIndex, michael@0: aQuery->SearchTerms() michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // min and max visit count michael@0: int32_t visits = aQuery->MinVisits(); michael@0: if (visits >= 0) { michael@0: rv = statement->BindInt32ByName( michael@0: NS_LITERAL_CSTRING("min_visits") + qIndex, visits michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: visits = aQuery->MaxVisits(); michael@0: if (visits >= 0) { michael@0: rv = statement->BindInt32ByName( michael@0: NS_LITERAL_CSTRING("max_visits") + qIndex, visits michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // domain (see GetReversedHostname for more info on reversed host names) michael@0: if (NS_SUCCEEDED(aQuery->GetHasDomain(&hasIt)) && hasIt) { michael@0: nsString revDomain; michael@0: GetReversedHostname(NS_ConvertUTF8toUTF16(aQuery->Domain()), revDomain); michael@0: michael@0: if (aQuery->DomainIsHost()) { michael@0: rv = statement->BindStringByName( michael@0: NS_LITERAL_CSTRING("domain_lower") + qIndex, revDomain michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } else { michael@0: // for "mozilla.org" do query >= "gro.allizom." AND < "gro.allizom/" michael@0: // which will get everything starting with "gro.allizom." while using the michael@0: // index (using SUBSTRING() causes indexes to be discarded). michael@0: NS_ASSERTION(revDomain[revDomain.Length() - 1] == '.', "Invalid rev. host"); michael@0: rv = statement->BindStringByName( michael@0: NS_LITERAL_CSTRING("domain_lower") + qIndex, revDomain michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: revDomain.Truncate(revDomain.Length() - 1); michael@0: revDomain.Append(char16_t('/')); michael@0: rv = statement->BindStringByName( michael@0: NS_LITERAL_CSTRING("domain_upper") + qIndex, revDomain michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: // URI michael@0: if (aQuery->Uri()) { michael@0: rv = URIBinder::Bind( michael@0: statement, NS_LITERAL_CSTRING("uri") + qIndex, aQuery->Uri() michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (aQuery->UriIsPrefix()) { michael@0: nsAutoCString uriString; michael@0: aQuery->Uri()->GetSpec(uriString); michael@0: uriString.Append(char(0x7F)); // MAX_UTF8 michael@0: rv = URIBinder::Bind( michael@0: statement, NS_LITERAL_CSTRING("uri_upper") + qIndex, uriString michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: // annotation michael@0: if (!aQuery->Annotation().IsEmpty()) { michael@0: rv = statement->BindUTF8StringByName( michael@0: NS_LITERAL_CSTRING("anno") + qIndex, aQuery->Annotation() michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: // tags michael@0: const nsTArray &tags = aQuery->Tags(); michael@0: if (tags.Length() > 0) { michael@0: for (uint32_t i = 0; i < tags.Length(); ++i) { michael@0: nsPrintfCString paramName("tag%d_", i); michael@0: NS_ConvertUTF16toUTF8 tag(tags[i]); michael@0: rv = statement->BindUTF8StringByName(paramName + qIndex, tag); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: int64_t tagsFolder = GetTagsFolder(); michael@0: rv = statement->BindInt64ByName( michael@0: NS_LITERAL_CSTRING("tags_folder") + qIndex, tagsFolder michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!aQuery->TagsAreNot()) { michael@0: rv = statement->BindInt32ByName( michael@0: NS_LITERAL_CSTRING("tag_count") + qIndex, tags.Length() michael@0: ); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: // transitions michael@0: const nsTArray& transitions = aQuery->Transitions(); michael@0: if (transitions.Length() > 0) { michael@0: for (uint32_t i = 0; i < transitions.Length(); ++i) { michael@0: nsPrintfCString paramName("transition%d_", i); michael@0: rv = statement->BindInt64ByName(paramName + qIndex, transitions[i]); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::ResultsAsList michael@0: // michael@0: michael@0: nsresult michael@0: nsNavHistory::ResultsAsList(mozIStorageStatement* statement, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: nsCOMArray* aResults) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr row = do_QueryInterface(statement, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasMore = false; michael@0: while (NS_SUCCEEDED(statement->ExecuteStep(&hasMore)) && hasMore) { michael@0: nsRefPtr result; michael@0: rv = RowToResult(row, aOptions, getter_AddRefs(result)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: aResults->AppendObject(result); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: const int64_t UNDEFINED_URN_VALUE = -1; michael@0: michael@0: // Create a urn (like michael@0: // urn:places-persist:place:group=0&group=1&sort=1&type=1,,%28local%20files%29) michael@0: // to be used to persist the open state of this container in localstore.rdf michael@0: nsresult michael@0: CreatePlacesPersistURN(nsNavHistoryQueryResultNode *aResultNode, michael@0: int64_t aValue, const nsCString& aTitle, nsCString& aURN) michael@0: { michael@0: nsAutoCString uri; michael@0: nsresult rv = aResultNode->GetUri(uri); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: aURN.Assign(NS_LITERAL_CSTRING("urn:places-persist:")); michael@0: aURN.Append(uri); michael@0: michael@0: aURN.Append(NS_LITERAL_CSTRING(",")); michael@0: if (aValue != UNDEFINED_URN_VALUE) michael@0: aURN.AppendInt(aValue); michael@0: michael@0: aURN.Append(NS_LITERAL_CSTRING(",")); michael@0: if (!aTitle.IsEmpty()) { michael@0: nsAutoCString escapedTitle; michael@0: bool success = NS_Escape(aTitle, escapedTitle, url_XAlphas); michael@0: NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY); michael@0: aURN.Append(escapedTitle); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: int64_t michael@0: nsNavHistory::GetTagsFolder() michael@0: { michael@0: // cache our tags folder michael@0: // note, we can't do this in nsNavHistory::Init(), michael@0: // as getting the bookmarks service would initialize it. michael@0: if (mTagsFolder == -1) { michael@0: nsNavBookmarks *bookmarks = nsNavBookmarks::GetBookmarksService(); michael@0: NS_ENSURE_TRUE(bookmarks, -1); michael@0: michael@0: nsresult rv = bookmarks->GetTagsFolder(&mTagsFolder); michael@0: NS_ENSURE_SUCCESS(rv, -1); michael@0: } michael@0: return mTagsFolder; michael@0: } michael@0: michael@0: // nsNavHistory::FilterResultSet michael@0: // michael@0: // This does some post-query-execution filtering: michael@0: // - searching on title, url and tags michael@0: // - limit count michael@0: // michael@0: // Note: changes to filtering in FilterResultSet() michael@0: // may require changes to NeedToFilterResultSet() michael@0: michael@0: nsresult michael@0: nsNavHistory::FilterResultSet(nsNavHistoryQueryResultNode* aQueryNode, michael@0: const nsCOMArray& aSet, michael@0: nsCOMArray* aFiltered, michael@0: const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions *aOptions) michael@0: { michael@0: // get the bookmarks service michael@0: nsNavBookmarks *bookmarks = nsNavBookmarks::GetBookmarksService(); michael@0: NS_ENSURE_TRUE(bookmarks, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: // parse the search terms michael@0: nsTArray*> terms; michael@0: ParseSearchTermsFromQueries(aQueries, &terms); michael@0: michael@0: uint16_t resultType = aOptions->ResultType(); michael@0: for (int32_t nodeIndex = 0; nodeIndex < aSet.Count(); nodeIndex++) { michael@0: // exclude-queries is implicit when searching, we're only looking at michael@0: // plan URI nodes michael@0: if (!aSet[nodeIndex]->IsURI()) michael@0: continue; michael@0: michael@0: // RESULTS_AS_TAG_CONTENTS returns a set ordered by place_id and michael@0: // lastModified. So, to remove duplicates, we can retain the first result michael@0: // for each uri. michael@0: if (resultType == nsINavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS && michael@0: nodeIndex > 0 && aSet[nodeIndex]->mURI == aSet[nodeIndex-1]->mURI) michael@0: continue; michael@0: michael@0: if (aSet[nodeIndex]->mItemId != -1 && aQueryNode && michael@0: aQueryNode->mItemId == aSet[nodeIndex]->mItemId) { michael@0: continue; michael@0: } michael@0: michael@0: // Append the node only if it matches one of the queries. michael@0: bool appendNode = false; michael@0: for (int32_t queryIndex = 0; michael@0: queryIndex < aQueries.Count() && !appendNode; queryIndex++) { michael@0: michael@0: if (terms[queryIndex]->Length()) { michael@0: // Filter based on search terms. michael@0: // Convert title and url for the current node to UTF16 strings. michael@0: NS_ConvertUTF8toUTF16 nodeTitle(aSet[nodeIndex]->mTitle); michael@0: // Unescape the URL for search terms matching. michael@0: nsAutoCString cNodeURL(aSet[nodeIndex]->mURI); michael@0: NS_ConvertUTF8toUTF16 nodeURL(NS_UnescapeURL(cNodeURL)); michael@0: michael@0: // Determine if every search term matches anywhere in the title, url or michael@0: // tag. michael@0: bool matchAll = true; michael@0: for (int32_t termIndex = terms[queryIndex]->Length() - 1; michael@0: termIndex >= 0 && matchAll; michael@0: termIndex--) { michael@0: nsString& term = terms[queryIndex]->ElementAt(termIndex); michael@0: michael@0: // True if any of them match; false makes us quit the loop michael@0: matchAll = CaseInsensitiveFindInReadable(term, nodeTitle) || michael@0: CaseInsensitiveFindInReadable(term, nodeURL) || michael@0: CaseInsensitiveFindInReadable(term, aSet[nodeIndex]->mTags); michael@0: } michael@0: michael@0: // Skip the node if we don't match all terms in the title, url or tag michael@0: if (!matchAll) michael@0: continue; michael@0: } michael@0: michael@0: // We passed all filters, so we can append the node to filtered results. michael@0: appendNode = true; michael@0: } michael@0: michael@0: if (appendNode) michael@0: aFiltered->AppendObject(aSet[nodeIndex]); michael@0: michael@0: // Stop once we have reached max results. michael@0: if (aOptions->MaxResults() > 0 && michael@0: (uint32_t)aFiltered->Count() >= aOptions->MaxResults()) michael@0: break; michael@0: } michael@0: michael@0: // De-allocate the temporary matrixes. michael@0: for (int32_t i = 0; i < aQueries.Count(); i++) { michael@0: delete terms[i]; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::registerEmbedVisit(nsIURI* aURI, michael@0: int64_t aTime) michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: VisitHashKey* visit = mEmbedVisits.PutEntry(aURI); michael@0: if (!visit) { michael@0: NS_WARNING("Unable to register a EMBED visit."); michael@0: return; michael@0: } michael@0: visit->visitTime = aTime; michael@0: } michael@0: michael@0: bool michael@0: nsNavHistory::hasEmbedVisit(nsIURI* aURI) { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: return !!mEmbedVisits.GetEntry(aURI); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::clearEmbedVisits() { michael@0: NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread"); michael@0: michael@0: mEmbedVisits.Clear(); michael@0: } michael@0: michael@0: // nsNavHistory::CheckIsRecentEvent michael@0: // michael@0: // Sees if this URL happened "recently." michael@0: // michael@0: // It is always removed from our recent list no matter what. It only counts michael@0: // as "recent" if the event happened more recently than our event michael@0: // threshold ago. michael@0: michael@0: bool michael@0: nsNavHistory::CheckIsRecentEvent(RecentEventHash* hashTable, michael@0: const nsACString& url) michael@0: { michael@0: PRTime eventTime; michael@0: if (hashTable->Get(url, reinterpret_cast(&eventTime))) { michael@0: hashTable->Remove(url); michael@0: if (eventTime > GetNow() - RECENT_EVENT_THRESHOLD) michael@0: return true; michael@0: return false; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::ExpireNonrecentEvents michael@0: // michael@0: // This goes through our michael@0: michael@0: static PLDHashOperator michael@0: ExpireNonrecentEventsCallback(nsCStringHashKey::KeyType aKey, michael@0: int64_t& aData, michael@0: void* userArg) michael@0: { michael@0: int64_t* threshold = reinterpret_cast(userArg); michael@0: if (aData < *threshold) michael@0: return PL_DHASH_REMOVE; michael@0: return PL_DHASH_NEXT; michael@0: } michael@0: void michael@0: nsNavHistory::ExpireNonrecentEvents(RecentEventHash* hashTable) michael@0: { michael@0: int64_t threshold = GetNow() - RECENT_EVENT_THRESHOLD; michael@0: hashTable->Enumerate(ExpireNonrecentEventsCallback, michael@0: reinterpret_cast(&threshold)); michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::RowToResult michael@0: // michael@0: // Here, we just have a generic row. It could be a query, URL, visit, michael@0: // or full visit. michael@0: michael@0: nsresult michael@0: nsNavHistory::RowToResult(mozIStorageValueArray* aRow, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: nsNavHistoryResultNode** aResult) michael@0: { michael@0: NS_ASSERTION(aRow && aOptions && aResult, "Null pointer in RowToResult"); michael@0: michael@0: // URL michael@0: nsAutoCString url; michael@0: nsresult rv = aRow->GetUTF8String(kGetInfoIndex_URL, url); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // title michael@0: nsAutoCString title; michael@0: rv = aRow->GetUTF8String(kGetInfoIndex_Title, title); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: uint32_t accessCount = aRow->AsInt32(kGetInfoIndex_VisitCount); michael@0: PRTime time = aRow->AsInt64(kGetInfoIndex_VisitDate); michael@0: michael@0: // favicon michael@0: nsAutoCString favicon; michael@0: rv = aRow->GetUTF8String(kGetInfoIndex_FaviconURL, favicon); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // itemId michael@0: int64_t itemId = aRow->AsInt64(kGetInfoIndex_ItemId); michael@0: int64_t parentId = -1; michael@0: if (itemId == 0) { michael@0: // This is not a bookmark. For non-bookmarks we use a -1 itemId value. michael@0: // Notice ids in sqlite tables start from 1, so itemId cannot ever be 0. michael@0: itemId = -1; michael@0: } michael@0: else { michael@0: // This is a bookmark, so it has a parent. michael@0: int64_t itemParentId = aRow->AsInt64(kGetInfoIndex_ItemParentId); michael@0: if (itemParentId > 0) { michael@0: // The Places root has parent == 0, but that item id does not really michael@0: // exist. We want to set the parent only if it's a real one. michael@0: parentId = itemParentId; michael@0: } michael@0: } michael@0: michael@0: if (IsQueryURI(url)) { michael@0: // special case "place:" URIs: turn them into containers michael@0: michael@0: // We should never expose the history title for query nodes if the michael@0: // bookmark-item's title is set to null (the history title may be the michael@0: // query string without the place: prefix). Thus we call getItemTitle michael@0: // explicitly. Doing this in the SQL query would be less performant since michael@0: // it should be done for all results rather than only for queries. michael@0: if (itemId != -1) { michael@0: nsNavBookmarks *bookmarks = nsNavBookmarks::GetBookmarksService(); michael@0: NS_ENSURE_TRUE(bookmarks, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: rv = bookmarks->GetItemTitle(itemId, title); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: nsRefPtr resultNode; michael@0: rv = QueryRowToResult(itemId, url, title, accessCount, time, favicon, michael@0: getter_AddRefs(resultNode)); michael@0: NS_ENSURE_SUCCESS(rv,rv); michael@0: michael@0: if (aOptions->ResultType() == nsNavHistoryQueryOptions::RESULTS_AS_TAG_QUERY) { michael@0: // RESULTS_AS_TAG_QUERY has date columns michael@0: resultNode->mDateAdded = aRow->AsInt64(kGetInfoIndex_ItemDateAdded); michael@0: resultNode->mLastModified = aRow->AsInt64(kGetInfoIndex_ItemLastModified); michael@0: } michael@0: else if (resultNode->IsFolder()) { michael@0: // If it's a simple folder node (i.e. a shortcut to another folder), apply michael@0: // our options for it. However, if the parent type was tag query, we do not michael@0: // apply them, because it would not yield any results. michael@0: resultNode->GetAsContainer()->mOptions = aOptions; michael@0: } michael@0: michael@0: resultNode.forget(aResult); michael@0: return rv; michael@0: } else if (aOptions->ResultType() == nsNavHistoryQueryOptions::RESULTS_AS_URI || michael@0: aOptions->ResultType() == nsNavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS) { michael@0: nsRefPtr resultNode = michael@0: new nsNavHistoryResultNode(url, title, accessCount, time, favicon); michael@0: michael@0: if (itemId != -1) { michael@0: resultNode->mItemId = itemId; michael@0: resultNode->mFolderId = parentId; michael@0: resultNode->mDateAdded = aRow->AsInt64(kGetInfoIndex_ItemDateAdded); michael@0: resultNode->mLastModified = aRow->AsInt64(kGetInfoIndex_ItemLastModified); michael@0: } michael@0: michael@0: resultNode->mFrecency = aRow->AsInt32(kGetInfoIndex_Frecency); michael@0: resultNode->mHidden = !!aRow->AsInt32(kGetInfoIndex_Hidden); michael@0: michael@0: nsAutoString tags; michael@0: rv = aRow->GetString(kGetInfoIndex_ItemTags, tags); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!tags.IsVoid()) { michael@0: resultNode->mTags.Assign(tags); michael@0: } michael@0: michael@0: rv = aRow->GetUTF8String(kGetInfoIndex_Guid, resultNode->mPageGuid); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: resultNode.forget(aResult); michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (aOptions->ResultType() == nsNavHistoryQueryOptions::RESULTS_AS_VISIT) { michael@0: nsRefPtr resultNode = michael@0: new nsNavHistoryResultNode(url, title, accessCount, time, favicon); michael@0: michael@0: nsAutoString tags; michael@0: rv = aRow->GetString(kGetInfoIndex_ItemTags, tags); michael@0: if (!tags.IsVoid()) michael@0: resultNode->mTags.Assign(tags); michael@0: michael@0: rv = aRow->GetUTF8String(kGetInfoIndex_Guid, resultNode->mPageGuid); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: resultNode.forget(aResult); michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::QueryRowToResult michael@0: // michael@0: // Called by RowToResult when the URI is a place: URI to generate the proper michael@0: // folder or query node. michael@0: michael@0: nsresult michael@0: nsNavHistory::QueryRowToResult(int64_t itemId, const nsACString& aURI, michael@0: const nsACString& aTitle, michael@0: uint32_t aAccessCount, PRTime aTime, michael@0: const nsACString& aFavicon, michael@0: nsNavHistoryResultNode** aNode) michael@0: { michael@0: nsCOMArray queries; michael@0: nsCOMPtr options; michael@0: nsresult rv = QueryStringToQueryArray(aURI, &queries, michael@0: getter_AddRefs(options)); michael@0: michael@0: nsRefPtr resultNode; michael@0: // If this failed the query does not parse correctly, let the error pass and michael@0: // handle it later. michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Check if this is a folder shortcut, so we can take a faster path. michael@0: int64_t folderId = GetSimpleBookmarksQueryFolder(queries, options); michael@0: if (folderId) { michael@0: nsNavBookmarks *bookmarks = nsNavBookmarks::GetBookmarksService(); michael@0: NS_ENSURE_TRUE(bookmarks, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: rv = bookmarks->ResultNodeForContainer(folderId, options, michael@0: getter_AddRefs(resultNode)); michael@0: // If this failed the shortcut is pointing to nowhere, let the error pass michael@0: // and handle it later. michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // This is the query itemId, and is what is exposed by node.itemId. michael@0: resultNode->GetAsFolder()->mQueryItemId = itemId; michael@0: michael@0: // Use the query item title, unless it's void (in that case use the michael@0: // concrete folder title). michael@0: if (!aTitle.IsVoid()) { michael@0: resultNode->mTitle = aTitle; michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: // This is a regular query. michael@0: resultNode = new nsNavHistoryQueryResultNode(aTitle, EmptyCString(), michael@0: aTime, queries, options); michael@0: resultNode->mItemId = itemId; michael@0: } michael@0: } michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: NS_WARNING("Generating a generic empty node for a broken query!"); michael@0: // This is a broken query, that either did not parse or points to not michael@0: // existing data. We don't want to return failure since that will kill the michael@0: // whole result. Instead make a generic empty query node. michael@0: resultNode = new nsNavHistoryQueryResultNode(aTitle, aFavicon, aURI); michael@0: resultNode->mItemId = itemId; michael@0: // This is a perf hack to generate an empty query that skips filtering. michael@0: resultNode->GetAsQuery()->Options()->SetExcludeItems(true); michael@0: } michael@0: michael@0: resultNode.forget(aNode); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: // nsNavHistory::VisitIdToResultNode michael@0: // michael@0: // Used by the query results to create new nodes on the fly when michael@0: // notifications come in. This just creates a node for the given visit ID. michael@0: michael@0: nsresult michael@0: nsNavHistory::VisitIdToResultNode(int64_t visitId, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: nsNavHistoryResultNode** aResult) michael@0: { michael@0: nsAutoCString tagsFragment; michael@0: GetTagsSqlFragment(GetTagsFolder(), NS_LITERAL_CSTRING("h.id"), michael@0: true, tagsFragment); michael@0: michael@0: nsCOMPtr statement; michael@0: switch (aOptions->ResultType()) michael@0: { michael@0: case nsNavHistoryQueryOptions::RESULTS_AS_VISIT: michael@0: case nsNavHistoryQueryOptions::RESULTS_AS_FULL_VISIT: michael@0: // visit query - want exact visit time michael@0: // Should match kGetInfoIndex_* (see GetQueryResults) michael@0: statement = mDB->GetStatement(NS_LITERAL_CSTRING( michael@0: "SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, " michael@0: "v.visit_date, f.url, null, null, null, null, " michael@0: ) + tagsFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_places h " michael@0: "JOIN moz_historyvisits v ON h.id = v.place_id " michael@0: "LEFT JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE v.id = :visit_id ") michael@0: ); michael@0: break; michael@0: michael@0: case nsNavHistoryQueryOptions::RESULTS_AS_URI: michael@0: // URL results - want last visit time michael@0: // Should match kGetInfoIndex_* (see GetQueryResults) michael@0: statement = mDB->GetStatement(NS_LITERAL_CSTRING( michael@0: "SELECT h.id, h.url, h.title, h.rev_host, h.visit_count, " michael@0: "h.last_visit_date, f.url, null, null, null, null, " michael@0: ) + tagsFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_places h " michael@0: "JOIN moz_historyvisits v ON h.id = v.place_id " michael@0: "LEFT JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE v.id = :visit_id ") michael@0: ); michael@0: break; michael@0: michael@0: default: michael@0: // Query base types like RESULTS_AS_*_QUERY handle additions michael@0: // by registering their own observers when they are expanded. michael@0: return NS_OK; michael@0: } michael@0: NS_ENSURE_STATE(statement); michael@0: mozStorageStatementScoper scoper(statement); michael@0: michael@0: nsresult rv = statement->BindInt64ByName(NS_LITERAL_CSTRING("visit_id"), michael@0: visitId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasMore = false; michael@0: rv = statement->ExecuteStep(&hasMore); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (! hasMore) { michael@0: NS_NOTREACHED("Trying to get a result node for an invalid visit"); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: nsCOMPtr row = do_QueryInterface(statement, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return RowToResult(row, aOptions, aResult); michael@0: } michael@0: michael@0: nsresult michael@0: nsNavHistory::BookmarkIdToResultNode(int64_t aBookmarkId, nsNavHistoryQueryOptions* aOptions, michael@0: nsNavHistoryResultNode** aResult) michael@0: { michael@0: nsAutoCString tagsFragment; michael@0: GetTagsSqlFragment(GetTagsFolder(), NS_LITERAL_CSTRING("h.id"), michael@0: true, tagsFragment); michael@0: // Should match kGetInfoIndex_* michael@0: nsCOMPtr stmt = mDB->GetStatement(NS_LITERAL_CSTRING( michael@0: "SELECT b.fk, h.url, COALESCE(b.title, h.title), " michael@0: "h.rev_host, h.visit_count, h.last_visit_date, f.url, b.id, " michael@0: "b.dateAdded, b.lastModified, b.parent, " michael@0: ) + tagsFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_bookmarks b " michael@0: "JOIN moz_places h ON b.fk = h.id " michael@0: "LEFT JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE b.id = :item_id ") michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("item_id"), michael@0: aBookmarkId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasMore = false; michael@0: rv = stmt->ExecuteStep(&hasMore); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!hasMore) { michael@0: NS_NOTREACHED("Trying to get a result node for an invalid bookmark identifier"); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: nsCOMPtr row = do_QueryInterface(stmt, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return RowToResult(row, aOptions, aResult); michael@0: } michael@0: michael@0: nsresult michael@0: nsNavHistory::URIToResultNode(nsIURI* aURI, michael@0: nsNavHistoryQueryOptions* aOptions, michael@0: nsNavHistoryResultNode** aResult) michael@0: { michael@0: nsAutoCString tagsFragment; michael@0: GetTagsSqlFragment(GetTagsFolder(), NS_LITERAL_CSTRING("h.id"), michael@0: true, tagsFragment); michael@0: // Should match kGetInfoIndex_* michael@0: nsCOMPtr stmt = mDB->GetStatement(NS_LITERAL_CSTRING( michael@0: "SELECT h.id, :page_url, h.title, h.rev_host, h.visit_count, " michael@0: "h.last_visit_date, f.url, null, null, null, null, " michael@0: ) + tagsFragment + NS_LITERAL_CSTRING(", h.frecency, h.hidden, h.guid " michael@0: "FROM moz_places h " michael@0: "LEFT JOIN moz_favicons f ON h.favicon_id = f.id " michael@0: "WHERE h.url = :page_url ") michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: mozStorageStatementScoper scoper(stmt); michael@0: michael@0: nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), aURI); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool hasMore = false; michael@0: rv = stmt->ExecuteStep(&hasMore); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (!hasMore) { michael@0: NS_NOTREACHED("Trying to get a result node for an invalid url"); michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: nsCOMPtr row = do_QueryInterface(stmt, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return RowToResult(row, aOptions, aResult); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::SendPageChangedNotification(nsIURI* aURI, michael@0: uint32_t aChangedAttribute, michael@0: const nsAString& aNewValue, michael@0: const nsACString& aGUID) michael@0: { michael@0: MOZ_ASSERT(!aGUID.IsEmpty()); michael@0: NOTIFY_OBSERVERS(mCanNotify, mCacheObservers, mObservers, michael@0: nsINavHistoryObserver, michael@0: OnPageChanged(aURI, aChangedAttribute, aNewValue, aGUID)); michael@0: } michael@0: michael@0: // nsNavHistory::TitleForDomain michael@0: // michael@0: // This computes the title for a given domain. Normally, this is just the michael@0: // domain name, but we specially handle empty cases to give you a nice michael@0: // localized string. michael@0: michael@0: void michael@0: nsNavHistory::TitleForDomain(const nsCString& domain, nsACString& aTitle) michael@0: { michael@0: if (! domain.IsEmpty()) { michael@0: aTitle = domain; michael@0: return; michael@0: } michael@0: michael@0: // use the localized one instead michael@0: GetStringFromName(MOZ_UTF16("localhost"), aTitle); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::GetAgeInDaysString(int32_t aInt, const char16_t *aName, michael@0: nsACString& aResult) michael@0: { michael@0: nsIStringBundle *bundle = GetBundle(); michael@0: if (bundle) { michael@0: nsAutoString intString; michael@0: intString.AppendInt(aInt); michael@0: const char16_t* strings[1] = { intString.get() }; michael@0: nsXPIDLString value; michael@0: nsresult rv = bundle->FormatStringFromName(aName, strings, michael@0: 1, getter_Copies(value)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: CopyUTF16toUTF8(value, aResult); michael@0: return; michael@0: } michael@0: } michael@0: CopyUTF16toUTF8(nsDependentString(aName), aResult); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::GetStringFromName(const char16_t *aName, nsACString& aResult) michael@0: { michael@0: nsIStringBundle *bundle = GetBundle(); michael@0: if (bundle) { michael@0: nsXPIDLString value; michael@0: nsresult rv = bundle->GetStringFromName(aName, getter_Copies(value)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: CopyUTF16toUTF8(value, aResult); michael@0: return; michael@0: } michael@0: } michael@0: CopyUTF16toUTF8(nsDependentString(aName), aResult); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::GetMonthName(int32_t aIndex, nsACString& aResult) michael@0: { michael@0: nsIStringBundle *bundle = GetDateFormatBundle(); michael@0: if (bundle) { michael@0: nsCString name = nsPrintfCString("month.%d.name", aIndex); michael@0: nsXPIDLString value; michael@0: nsresult rv = bundle->GetStringFromName(NS_ConvertUTF8toUTF16(name).get(), michael@0: getter_Copies(value)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: CopyUTF16toUTF8(value, aResult); michael@0: return; michael@0: } michael@0: } michael@0: aResult = nsPrintfCString("[%d]", aIndex); michael@0: } michael@0: michael@0: void michael@0: nsNavHistory::GetMonthYear(int32_t aMonth, int32_t aYear, nsACString& aResult) michael@0: { michael@0: nsIStringBundle *bundle = GetBundle(); michael@0: if (bundle) { michael@0: nsAutoCString monthName; michael@0: GetMonthName(aMonth, monthName); michael@0: nsAutoString yearString; michael@0: yearString.AppendInt(aYear); michael@0: const char16_t* strings[2] = { michael@0: NS_ConvertUTF8toUTF16(monthName).get() michael@0: , yearString.get() michael@0: }; michael@0: nsXPIDLString value; michael@0: if (NS_SUCCEEDED(bundle->FormatStringFromName( michael@0: MOZ_UTF16("finduri-MonthYear"), strings, 2, michael@0: getter_Copies(value) michael@0: ))) { michael@0: CopyUTF16toUTF8(value, aResult); michael@0: return; michael@0: } michael@0: } michael@0: aResult.AppendLiteral("finduri-MonthYear"); michael@0: } michael@0: michael@0: michael@0: namespace { michael@0: michael@0: // GetSimpleBookmarksQueryFolder michael@0: // michael@0: // Determines if this set of queries is a simple bookmarks query for a michael@0: // folder with no other constraints. In these common cases, we can more michael@0: // efficiently compute the results. michael@0: // michael@0: // A simple bookmarks query will result in a hierarchical tree of michael@0: // bookmark items, folders and separators. michael@0: // michael@0: // Returns the folder ID if it is a simple folder query, 0 if not. michael@0: static int64_t michael@0: GetSimpleBookmarksQueryFolder(const nsCOMArray& aQueries, michael@0: nsNavHistoryQueryOptions* aOptions) michael@0: { michael@0: if (aQueries.Count() != 1) michael@0: return 0; michael@0: michael@0: nsNavHistoryQuery* query = aQueries[0]; michael@0: if (query->Folders().Length() != 1) michael@0: return 0; michael@0: michael@0: bool hasIt; michael@0: query->GetHasBeginTime(&hasIt); michael@0: if (hasIt) michael@0: return 0; michael@0: query->GetHasEndTime(&hasIt); michael@0: if (hasIt) michael@0: return 0; michael@0: query->GetHasDomain(&hasIt); michael@0: if (hasIt) michael@0: return 0; michael@0: query->GetHasUri(&hasIt); michael@0: if (hasIt) michael@0: return 0; michael@0: (void)query->GetHasSearchTerms(&hasIt); michael@0: if (hasIt) michael@0: return 0; michael@0: if (query->Tags().Length() > 0) michael@0: return 0; michael@0: if (aOptions->MaxResults() > 0) michael@0: return 0; michael@0: michael@0: // RESULTS_AS_TAG_CONTENTS is quite similar to a folder shortcut, but it must michael@0: // not be treated like that, since it needs all query options. michael@0: if(aOptions->ResultType() == nsINavHistoryQueryOptions::RESULTS_AS_TAG_CONTENTS) michael@0: return 0; michael@0: michael@0: // Don't care about onlyBookmarked flag, since specifying a bookmark michael@0: // folder is inferring onlyBookmarked. michael@0: michael@0: return query->Folders()[0]; michael@0: } michael@0: michael@0: michael@0: // ParseSearchTermsFromQueries michael@0: // michael@0: // Construct a matrix of search terms from the given queries array. michael@0: // All of the query objects are ORed together. Within a query, all the terms michael@0: // are ANDed together. See nsINavHistoryService.idl. michael@0: // michael@0: // This just breaks the query up into words. We don't do anything fancy, michael@0: // not even quoting. We do, however, strip quotes, because people might michael@0: // try to input quotes expecting them to do something and get no results michael@0: // back. michael@0: michael@0: inline bool isQueryWhitespace(char16_t ch) michael@0: { michael@0: return ch == ' '; michael@0: } michael@0: michael@0: void ParseSearchTermsFromQueries(const nsCOMArray& aQueries, michael@0: nsTArray*>* aTerms) michael@0: { michael@0: int32_t lastBegin = -1; michael@0: for (int32_t i = 0; i < aQueries.Count(); i++) { michael@0: nsTArray *queryTerms = new nsTArray(); michael@0: bool hasSearchTerms; michael@0: if (NS_SUCCEEDED(aQueries[i]->GetHasSearchTerms(&hasSearchTerms)) && michael@0: hasSearchTerms) { michael@0: const nsString& searchTerms = aQueries[i]->SearchTerms(); michael@0: for (uint32_t j = 0; j < searchTerms.Length(); j++) { michael@0: if (isQueryWhitespace(searchTerms[j]) || michael@0: searchTerms[j] == '"') { michael@0: if (lastBegin >= 0) { michael@0: // found the end of a word michael@0: queryTerms->AppendElement(Substring(searchTerms, lastBegin, michael@0: j - lastBegin)); michael@0: lastBegin = -1; michael@0: } michael@0: } else { michael@0: if (lastBegin < 0) { michael@0: // found the beginning of a word michael@0: lastBegin = j; michael@0: } michael@0: } michael@0: } michael@0: // last word michael@0: if (lastBegin >= 0) michael@0: queryTerms->AppendElement(Substring(searchTerms, lastBegin)); michael@0: } michael@0: aTerms->AppendElement(queryTerms); michael@0: } michael@0: } michael@0: michael@0: } // anonymous namespace michael@0: michael@0: michael@0: nsresult michael@0: nsNavHistory::UpdateFrecency(int64_t aPlaceId) michael@0: { michael@0: nsCOMPtr updateFrecencyStmt = mDB->GetAsyncStatement( michael@0: "UPDATE moz_places " michael@0: "SET frecency = NOTIFY_FRECENCY(" michael@0: "CALCULATE_FRECENCY(:page_id), url, guid, hidden, last_visit_date" michael@0: ") " michael@0: "WHERE id = :page_id" michael@0: ); michael@0: NS_ENSURE_STATE(updateFrecencyStmt); michael@0: nsresult rv = updateFrecencyStmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), michael@0: aPlaceId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nsCOMPtr updateHiddenStmt = mDB->GetAsyncStatement( michael@0: "UPDATE moz_places " michael@0: "SET hidden = 0 " michael@0: "WHERE id = :page_id AND frecency <> 0" michael@0: ); michael@0: NS_ENSURE_STATE(updateHiddenStmt); michael@0: rv = updateHiddenStmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), michael@0: aPlaceId); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mozIStorageBaseStatement *stmts[] = { michael@0: updateFrecencyStmt.get() michael@0: , updateHiddenStmt.get() michael@0: }; michael@0: michael@0: nsRefPtr cb = michael@0: new AsyncStatementCallbackNotifier(TOPIC_FRECENCY_UPDATED); michael@0: nsCOMPtr ps; michael@0: rv = mDB->MainConn()->ExecuteAsync(stmts, ArrayLength(stmts), cb, michael@0: getter_AddRefs(ps)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: namespace { michael@0: michael@0: class FixInvalidFrecenciesCallback : public AsyncStatementCallbackNotifier michael@0: { michael@0: public: michael@0: FixInvalidFrecenciesCallback() michael@0: : AsyncStatementCallbackNotifier(TOPIC_FRECENCY_UPDATED) michael@0: { michael@0: } michael@0: michael@0: NS_IMETHOD HandleCompletion(uint16_t aReason) michael@0: { michael@0: nsresult rv = AsyncStatementCallbackNotifier::HandleCompletion(aReason); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (aReason == REASON_FINISHED) { michael@0: nsNavHistory *navHistory = nsNavHistory::GetHistoryService(); michael@0: NS_ENSURE_STATE(navHistory); michael@0: navHistory->NotifyManyFrecenciesChanged(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: nsresult michael@0: nsNavHistory::FixInvalidFrecencies() michael@0: { michael@0: nsCOMPtr stmt = mDB->GetAsyncStatement( michael@0: "UPDATE moz_places " michael@0: "SET frecency = CALCULATE_FRECENCY(id) " michael@0: "WHERE frecency < 0" michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: michael@0: nsRefPtr callback = michael@0: new FixInvalidFrecenciesCallback(); michael@0: nsCOMPtr ps; michael@0: (void)stmt->ExecuteAsync(callback, getter_AddRefs(ps)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: #ifdef MOZ_XUL michael@0: michael@0: nsresult michael@0: nsNavHistory::AutoCompleteFeedback(int32_t aIndex, michael@0: nsIAutoCompleteController *aController) michael@0: { michael@0: nsCOMPtr stmt = mDB->GetAsyncStatement( michael@0: "INSERT OR REPLACE INTO moz_inputhistory " michael@0: // use_count will asymptotically approach the max of 10. michael@0: "SELECT h.id, IFNULL(i.input, :input_text), IFNULL(i.use_count, 0) * .9 + 1 " michael@0: "FROM moz_places h " michael@0: "LEFT JOIN moz_inputhistory i ON i.place_id = h.id AND i.input = :input_text " michael@0: "WHERE url = :page_url " michael@0: ); michael@0: NS_ENSURE_STATE(stmt); michael@0: michael@0: nsAutoString input; michael@0: nsresult rv = aController->GetSearchString(input); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = stmt->BindStringByName(NS_LITERAL_CSTRING("input_text"), input); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoString url; michael@0: rv = aController->GetValueAt(aIndex, url); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), michael@0: NS_ConvertUTF16toUTF8(url)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // We do the update asynchronously and we do not care about failures. michael@0: nsRefPtr callback = michael@0: new AsyncStatementCallbackNotifier(TOPIC_AUTOCOMPLETE_FEEDBACK_UPDATED); michael@0: nsCOMPtr canceler; michael@0: rv = stmt->ExecuteAsync(callback, getter_AddRefs(canceler)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: #endif michael@0: michael@0: michael@0: nsICollation * michael@0: nsNavHistory::GetCollation() michael@0: { michael@0: if (mCollation) michael@0: return mCollation; michael@0: michael@0: // locale michael@0: nsCOMPtr locale; michael@0: nsCOMPtr ls(do_GetService(NS_LOCALESERVICE_CONTRACTID)); michael@0: NS_ENSURE_TRUE(ls, nullptr); michael@0: nsresult rv = ls->GetApplicationLocale(getter_AddRefs(locale)); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: michael@0: // collation michael@0: nsCOMPtr cfact = michael@0: do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID); michael@0: NS_ENSURE_TRUE(cfact, nullptr); michael@0: rv = cfact->CreateCollation(locale, getter_AddRefs(mCollation)); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: michael@0: return mCollation; michael@0: } michael@0: michael@0: nsIStringBundle * michael@0: nsNavHistory::GetBundle() michael@0: { michael@0: if (!mBundle) { michael@0: nsCOMPtr bundleService = michael@0: services::GetStringBundleService(); michael@0: NS_ENSURE_TRUE(bundleService, nullptr); michael@0: nsresult rv = bundleService->CreateBundle( michael@0: "chrome://places/locale/places.properties", michael@0: getter_AddRefs(mBundle)); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: } michael@0: return mBundle; michael@0: } michael@0: michael@0: nsIStringBundle * michael@0: nsNavHistory::GetDateFormatBundle() michael@0: { michael@0: if (!mDateFormatBundle) { michael@0: nsCOMPtr bundleService = michael@0: services::GetStringBundleService(); michael@0: NS_ENSURE_TRUE(bundleService, nullptr); michael@0: nsresult rv = bundleService->CreateBundle( michael@0: "chrome://global/locale/dateFormat.properties", michael@0: getter_AddRefs(mDateFormatBundle)); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: } michael@0: return mDateFormatBundle; michael@0: }