2009-08-20 11:56:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Places code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2010-12-02 14:09:52 -08:00
|
|
|
* the Mozilla Foundation.
|
2009-08-20 11:56:10 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2010-07-02 08:50:41 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#endif
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
#include "History.h"
|
|
|
|
#include "nsNavHistory.h"
|
2010-07-13 18:00:33 -07:00
|
|
|
#include "nsNavBookmarks.h"
|
2010-04-27 04:24:56 -07:00
|
|
|
#include "Helpers.h"
|
2009-08-20 11:56:10 -07:00
|
|
|
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
#include "mozilla/dom/Link.h"
|
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
#include "nsIEventStateManager.h"
|
2010-04-27 04:24:56 -07:00
|
|
|
#include "mozilla/Services.h"
|
2010-11-12 14:24:25 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2010-12-16 00:10:27 -08:00
|
|
|
#include "nsNetUtil.h"
|
2010-11-12 14:24:25 -08:00
|
|
|
|
|
|
|
// Initial size for the cache holding visited status observers.
|
|
|
|
#define VISIT_OBSERVERS_INITIAL_CACHE_SIZE 128
|
2009-08-20 11:56:10 -07:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace places {
|
|
|
|
|
2010-02-24 08:37:02 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Global Defines
|
|
|
|
|
|
|
|
#define URI_VISITED "visited"
|
|
|
|
#define URI_NOT_VISITED "not visited"
|
|
|
|
#define URI_VISITED_RESOLUTION_TOPIC "visited-status-resolution"
|
2010-07-13 18:00:33 -07:00
|
|
|
// Observer event fired after a visit has been registered in the DB.
|
|
|
|
#define URI_VISIT_SAVED "uri-visit-saved"
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Anonymous Helpers
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2010-10-11 05:53:42 -07:00
|
|
|
class VisitedQuery : public AsyncStatementCallback
|
2009-08-20 11:56:10 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static nsresult Start(nsIURI* aURI)
|
|
|
|
{
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_PRECONDITION(aURI, "Null URI");
|
2009-08-20 11:56:10 -07:00
|
|
|
|
2010-07-02 08:50:41 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
// If we are a content process, always remote the request to the
|
|
|
|
// parent process.
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
2010-10-06 08:21:31 -07:00
|
|
|
mozilla::dom::ContentChild* cpc =
|
2010-07-02 08:50:41 -07:00
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
NS_ASSERTION(cpc, "Content Protocol is NULL!");
|
2010-07-26 12:59:34 -07:00
|
|
|
(void)cpc->SendStartVisitedQuery(aURI);
|
2010-07-02 08:50:41 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
|
|
|
NS_ENSURE_STATE(navHistory);
|
|
|
|
if (navHistory->hasEmbedVisit(aURI)) {
|
|
|
|
nsRefPtr<VisitedQuery> callback = new VisitedQuery(aURI, true);
|
|
|
|
NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
// As per IHistory contract, we must notify asynchronously.
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(callback, &VisitedQuery::NotifyVisitedStatus);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-06 08:21:31 -07:00
|
|
|
mozIStorageAsyncStatement* stmt =
|
|
|
|
History::GetService()->GetIsVisitedStatement();
|
2009-08-20 11:56:10 -07:00
|
|
|
NS_ENSURE_STATE(stmt);
|
|
|
|
|
2010-04-27 04:24:56 -07:00
|
|
|
// Bind by index for performance.
|
|
|
|
nsresult rv = URIBinder::Bind(stmt, 0, aURI);
|
2009-08-20 11:56:10 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<VisitedQuery> callback = new VisitedQuery(aURI);
|
|
|
|
NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStoragePendingStatement> handle;
|
|
|
|
return stmt->ExecuteAsync(callback, getter_AddRefs(handle));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HandleResult(mozIStorageResultSet* aResults)
|
|
|
|
{
|
|
|
|
// If this method is called, we've gotten results, which means we have a
|
|
|
|
// visit.
|
|
|
|
mIsVisited = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HandleError(mozIStorageError* aError)
|
|
|
|
{
|
|
|
|
// mIsVisited is already set to false, and that's the assumption we will
|
|
|
|
// make if an error occurred.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HandleCompletion(PRUint16 aReason)
|
|
|
|
{
|
2010-08-06 08:42:53 -07:00
|
|
|
if (aReason != mozIStorageStatementCallback::REASON_FINISHED) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsresult rv = NotifyVisitedStatus();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NotifyVisitedStatus()
|
|
|
|
{
|
2009-08-20 11:56:10 -07:00
|
|
|
if (mIsVisited) {
|
|
|
|
History::GetService()->NotifyVisited(mURI);
|
|
|
|
}
|
2010-02-24 08:37:02 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
2010-04-27 04:24:56 -07:00
|
|
|
mozilla::services::GetObserverService();
|
2010-02-24 08:37:02 -08:00
|
|
|
if (observerService) {
|
|
|
|
nsAutoString status;
|
|
|
|
if (mIsVisited) {
|
|
|
|
status.AssignLiteral(URI_VISITED);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.AssignLiteral(URI_NOT_VISITED);
|
|
|
|
}
|
|
|
|
(void)observerService->NotifyObservers(mURI,
|
|
|
|
URI_VISITED_RESOLUTION_TOPIC,
|
|
|
|
status.get());
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-12 14:24:25 -08:00
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
private:
|
2010-11-12 14:24:25 -08:00
|
|
|
VisitedQuery(nsIURI* aURI, bool aIsVisited=false)
|
2009-08-20 11:56:10 -07:00
|
|
|
: mURI(aURI)
|
2010-11-12 14:24:25 -08:00
|
|
|
, mIsVisited(aIsVisited)
|
2009-08-20 11:56:10 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
bool mIsVisited;
|
|
|
|
};
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
struct VisitData {
|
|
|
|
VisitData()
|
|
|
|
: placeId(0)
|
|
|
|
, visitId(0)
|
|
|
|
, sessionId(0)
|
|
|
|
, hidden(false)
|
|
|
|
, typed(false)
|
|
|
|
, transitionType(-1)
|
|
|
|
, visitTime(0)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt64 placeId;
|
2010-11-08 11:42:08 -08:00
|
|
|
PRInt64 visitId;
|
|
|
|
PRInt64 sessionId;
|
|
|
|
nsCString spec;
|
2010-11-17 16:15:49 -08:00
|
|
|
nsString revHost;
|
2010-11-08 11:42:08 -08:00
|
|
|
bool hidden;
|
|
|
|
bool typed;
|
2010-07-13 18:00:33 -07:00
|
|
|
PRInt32 transitionType;
|
2010-11-08 11:42:08 -08:00
|
|
|
PRTime visitTime;
|
2010-07-13 18:00:33 -07:00
|
|
|
};
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
|
2010-07-13 18:00:33 -07:00
|
|
|
/**
|
2010-11-08 11:42:08 -08:00
|
|
|
* Notifies observers about a visit.
|
2010-07-13 18:00:33 -07:00
|
|
|
*/
|
2010-11-08 11:42:08 -08:00
|
|
|
class NotifyVisitObservers : public nsRunnable
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-08 11:42:08 -08:00
|
|
|
NotifyVisitObservers(VisitData& aPlace,
|
|
|
|
VisitData& aReferrer)
|
|
|
|
: mPlace(aPlace)
|
|
|
|
, mReferrer(aReferrer)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_IMETHOD Run()
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-12 14:24:25 -08:00
|
|
|
NS_PRECONDITION(NS_IsMainThread(),
|
|
|
|
"This should be called on the main thread");
|
|
|
|
|
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
|
|
|
if (!navHistory) {
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_WARNING("Trying to notify about a visit but cannot get the history service!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-17 16:15:49 -08:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
(void)NS_NewURI(getter_AddRefs(uri), mPlace.spec);
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
// Notify nsNavHistory observers of visit, but only for certain types of
|
|
|
|
// visits to maintain consistency with nsNavHistory::GetQueryResults.
|
|
|
|
if (!mPlace.hidden &&
|
|
|
|
mPlace.transitionType != nsINavHistoryService::TRANSITION_EMBED &&
|
|
|
|
mPlace.transitionType != nsINavHistoryService::TRANSITION_FRAMED_LINK) {
|
2010-11-17 16:15:49 -08:00
|
|
|
navHistory->NotifyOnVisit(uri, mPlace.visitId, mPlace.visitTime,
|
2010-11-12 14:24:25 -08:00
|
|
|
mPlace.sessionId, mReferrer.visitId,
|
|
|
|
mPlace.transitionType);
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obsService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (obsService) {
|
2010-11-17 16:15:49 -08:00
|
|
|
nsresult rv = obsService->NotifyObservers(uri, URI_VISIT_SAVED, nsnull);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Could not notify observers");
|
|
|
|
}
|
|
|
|
|
2010-11-17 16:15:49 -08:00
|
|
|
History::GetService()->NotifyVisited(uri);
|
2010-11-08 11:47:07 -08:00
|
|
|
|
2010-07-13 18:00:33 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-08 11:42:08 -08:00
|
|
|
private:
|
2010-11-08 11:47:07 -08:00
|
|
|
VisitData mPlace;
|
|
|
|
VisitData mReferrer;
|
2010-07-13 18:00:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-11-08 11:42:08 -08:00
|
|
|
* Adds a visit to the database.
|
2010-07-13 18:00:33 -07:00
|
|
|
*/
|
2010-11-08 11:42:08 -08:00
|
|
|
class InsertVisitedURI : public nsRunnable
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-08 11:42:08 -08:00
|
|
|
/**
|
|
|
|
* Adds a visit to the database asynchronously.
|
|
|
|
*
|
2010-11-08 11:43:46 -08:00
|
|
|
* @param aConnection
|
|
|
|
* The database connection to use for these operations.
|
2010-11-08 11:42:08 -08:00
|
|
|
* @param aPlace
|
|
|
|
* The location to record a visit.
|
|
|
|
* @param [optional] aReferrer
|
|
|
|
* The page that "referred" us to aPlace.
|
|
|
|
*/
|
2010-11-08 11:43:46 -08:00
|
|
|
static nsresult Start(mozIStorageConnection* aConnection,
|
|
|
|
VisitData& aPlace,
|
2010-11-08 11:42:08 -08:00
|
|
|
nsIURI* aReferrer = nsnull)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_PRECONDITION(NS_IsMainThread(),
|
|
|
|
"This should be called on the main thread");
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
nsRefPtr<InsertVisitedURI> event =
|
|
|
|
new InsertVisitedURI(aConnection, aPlace, aReferrer);
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
// Speculatively get a new session id for our visit. While it is true that
|
|
|
|
// we will use the session id from the referrer if the visit was "recent"
|
|
|
|
// enough, we cannot call this method off of the main thread, so we have to
|
|
|
|
// consume an id now.
|
2010-11-12 14:24:25 -08:00
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
|
|
|
NS_ENSURE_TRUE(navHistory, NS_ERROR_UNEXPECTED);
|
|
|
|
event->mPlace.sessionId = navHistory->GetNewSessionID();
|
2010-11-08 11:42:08 -08:00
|
|
|
|
|
|
|
// Get the target thread, and then start the work!
|
2010-11-08 11:43:46 -08:00
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetInterface(aConnection);
|
|
|
|
NS_ENSURE_TRUE(target, NS_ERROR_UNEXPECTED);
|
|
|
|
nsresult rv = target->Dispatch(event, NS_DISPATCH_NORMAL);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_IMETHOD Run()
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_PRECONDITION(!NS_IsMainThread(),
|
|
|
|
"This should not be called on the main thread");
|
|
|
|
|
|
|
|
bool known = FetchPageInfo(mPlace);
|
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
// If we had a referrer, we want to know about its last visit to put this
|
|
|
|
// new visit into the same session.
|
2010-11-17 16:15:49 -08:00
|
|
|
if (!mReferrer.spec.IsEmpty()) {
|
2010-11-16 04:44:05 -08:00
|
|
|
bool recentVisit = FetchVisitInfo(mReferrer, mPlace.visitTime);
|
|
|
|
// At this point, we know the referrer's session id, which this new visit
|
|
|
|
// should also share.
|
|
|
|
if (recentVisit) {
|
|
|
|
mPlace.sessionId = mReferrer.sessionId;
|
|
|
|
}
|
|
|
|
// However, if it isn't recent enough, we don't care to log anything about
|
|
|
|
// the referrer and we'll start a new session.
|
|
|
|
else {
|
|
|
|
// This is sufficient to ignore our referrer. This behavior has test
|
|
|
|
// coverage, so if this invariant changes, we'll know.
|
|
|
|
mReferrer.visitId = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mozStorageTransaction transaction(mDBConn, PR_FALSE,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
2010-11-08 11:42:08 -08:00
|
|
|
// If the page was in moz_places, we need to update the entry.
|
|
|
|
if (known) {
|
|
|
|
NS_ASSERTION(mPlace.placeId > 0, "must have a valid place id!");
|
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
2010-11-08 11:43:46 -08:00
|
|
|
"UPDATE moz_places "
|
|
|
|
"SET hidden = :hidden, typed = :typed "
|
|
|
|
"WHERE id = :page_id "
|
|
|
|
);
|
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-11-08 11:42:08 -08:00
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), mPlace.placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Otherwise, the page was not in moz_places, so now we have to add it.
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(mPlace.placeId == 0, "should not have a valid place id!");
|
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
2010-11-08 11:43:46 -08:00
|
|
|
"INSERT INTO moz_places "
|
2010-12-02 14:09:52 -08:00
|
|
|
"(url, rev_host, hidden, typed, guid) "
|
|
|
|
"VALUES (:page_url, :rev_host, :hidden, :typed, GENERATE_GUID()) "
|
2010-11-08 11:43:46 -08:00
|
|
|
);
|
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-11-17 16:15:49 -08:00
|
|
|
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("rev_host"),
|
|
|
|
mPlace.revHost);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-17 16:15:49 -08:00
|
|
|
rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), mPlace.spec);
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-11-16 04:44:05 -08:00
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("typed"), mPlace.typed);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("hidden"), mPlace.hidden);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-08 11:42:08 -08:00
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
rv = AddVisit(mPlace, mReferrer);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-08 11:42:08 -08:00
|
|
|
|
|
|
|
rv = UpdateFrecency(mPlace);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-08 11:42:08 -08:00
|
|
|
|
|
|
|
rv = transaction.Commit();
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
// Finally, dispatch an event to the main thread to notify observers.
|
|
|
|
nsCOMPtr<nsIRunnable> event = new NotifyVisitObservers(mPlace, mReferrer);
|
|
|
|
rv = NS_DispatchToMainThread(event);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-08 11:42:08 -08:00
|
|
|
private:
|
2010-11-08 11:43:46 -08:00
|
|
|
InsertVisitedURI(mozIStorageConnection* aConnection,
|
|
|
|
VisitData& aPlace,
|
2010-11-08 11:42:08 -08:00
|
|
|
nsIURI* aReferrer)
|
2010-11-08 11:43:46 -08:00
|
|
|
: mDBConn(aConnection)
|
|
|
|
, mPlace(aPlace)
|
|
|
|
, mHistory(History::GetService())
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-17 16:15:49 -08:00
|
|
|
if (aReferrer) {
|
|
|
|
(void)aReferrer->GetSpec(mReferrer.spec);
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
/**
|
|
|
|
* Loads information about the page into _place from moz_places.
|
|
|
|
*
|
|
|
|
* @param _place
|
|
|
|
* The VisitData for the place we need to know information about.
|
|
|
|
* @return true if the page was recorded in moz_places, false otherwise.
|
|
|
|
*/
|
|
|
|
bool FetchPageInfo(VisitData& _place)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-17 16:15:49 -08:00
|
|
|
NS_PRECONDITION(!_place.spec.IsEmpty(), "must have a non-empty spec!");
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"SELECT id, typed, hidden "
|
|
|
|
"FROM moz_places "
|
|
|
|
"WHERE url = :page_url "
|
|
|
|
);
|
|
|
|
NS_ENSURE_TRUE(stmt, false);
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-11-08 11:42:08 -08:00
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"),
|
2010-11-17 16:15:49 -08:00
|
|
|
_place.spec);
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
if (!hasResult) {
|
|
|
|
return false;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
rv = stmt->GetInt64(0, &_place.placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
if (!_place.typed) {
|
|
|
|
// If this transition wasn't typed, others might have been. If database
|
|
|
|
// has location as typed, reflect that in our data structure.
|
|
|
|
PRInt32 typed;
|
|
|
|
rv = stmt->GetInt32(1, &typed);
|
|
|
|
_place.typed = !!typed;
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
2010-11-08 11:42:08 -08:00
|
|
|
if (_place.hidden) {
|
|
|
|
// If this transition was hidden, it is possible that others were not.
|
|
|
|
// Any one visible transition makes this location visible. If database
|
|
|
|
// has location as visible, reflect that in our data structure.
|
|
|
|
PRInt32 hidden;
|
|
|
|
rv = stmt->GetInt32(2, &hidden);
|
|
|
|
_place.hidden = !!hidden;
|
|
|
|
NS_ENSURE_SUCCESS(rv, true);
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
return true;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
/**
|
|
|
|
* Loads visit information about the page into _place.
|
|
|
|
*
|
|
|
|
* @param _place
|
|
|
|
* The VisitData for the place we need to know visit information about.
|
|
|
|
* @param [optional] aThresholdStart
|
|
|
|
* The timestamp of a new visit (not represented by _place) used to
|
|
|
|
* determine if the page was recently visited or not.
|
|
|
|
* @return true if the page was recently (determined with aThresholdStart)
|
|
|
|
* visited, false otherwise.
|
|
|
|
*/
|
|
|
|
bool FetchVisitInfo(VisitData& _place,
|
|
|
|
PRTime aThresholdStart = 0)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-17 16:15:49 -08:00
|
|
|
NS_PRECONDITION(!_place.spec.IsEmpty(), "must have a non-empty spec!");
|
2010-11-08 11:42:08 -08:00
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"SELECT id, session, visit_date "
|
|
|
|
"FROM moz_historyvisits "
|
|
|
|
"WHERE place_id = (SELECT id FROM moz_places WHERE url = :page_url) "
|
|
|
|
"ORDER BY visit_date DESC "
|
|
|
|
);
|
|
|
|
NS_ENSURE_TRUE(stmt, false);
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-11-08 11:42:08 -08:00
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"),
|
2010-11-17 16:15:49 -08:00
|
|
|
_place.spec);
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
if (!hasResult) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = stmt->GetInt64(0, &_place.visitId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
rv = stmt->GetInt64(1, &_place.sessionId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
rv = stmt->GetInt64(2, &_place.visitTime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
// If we have been given a visit threshold start time, go ahead and
|
|
|
|
// calculate if we have been recently visited.
|
|
|
|
if (aThresholdStart &&
|
|
|
|
aThresholdStart - _place.visitTime <= RECENT_EVENT_THRESHOLD) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
/**
|
|
|
|
* Adds a visit for _place and updates it with the right visit id.
|
|
|
|
*
|
|
|
|
* @param _place
|
|
|
|
* The VisitData for the place we need to know visit information about.
|
|
|
|
* @param aReferrer
|
|
|
|
* A reference to the referrer's visit data.
|
|
|
|
*/
|
|
|
|
nsresult AddVisit(VisitData& _place,
|
|
|
|
const VisitData& aReferrer)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-16 04:44:05 -08:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
if (_place.placeId) {
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
2010-11-08 11:43:46 -08:00
|
|
|
"INSERT INTO moz_historyvisits "
|
|
|
|
"(from_visit, place_id, visit_date, visit_type, session) "
|
|
|
|
"VALUES (:from_visit, :page_id, :visit_date, :visit_type, :session) "
|
|
|
|
);
|
2010-11-16 04:44:05 -08:00
|
|
|
NS_ENSURE_STATE(stmt);
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), mPlace.placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"INSERT INTO moz_historyvisits "
|
|
|
|
"(from_visit, place_id, visit_date, visit_type, session) "
|
2010-11-17 16:15:49 -08:00
|
|
|
"VALUES (:from_visit, (SELECT id FROM moz_places WHERE url = :page_url), :visit_date, :visit_type, :session) "
|
2010-11-16 04:44:05 -08:00
|
|
|
);
|
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-11-17 16:15:49 -08:00
|
|
|
rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), _place.spec);
|
2010-11-16 04:44:05 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("from_visit"),
|
|
|
|
aReferrer.visitId);
|
2010-11-08 11:42:08 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("visit_date"),
|
|
|
|
_place.visitTime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRInt32 transitionType = _place.transitionType;
|
|
|
|
NS_ASSERTION(transitionType >= nsINavHistoryService::TRANSITION_LINK &&
|
|
|
|
transitionType <= nsINavHistoryService::TRANSITION_FRAMED_LINK,
|
|
|
|
"Invalid transition type!");
|
|
|
|
rv = stmt->BindInt32ByName(NS_LITERAL_CSTRING("visit_type"),
|
|
|
|
transitionType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("session"),
|
|
|
|
_place.sessionId);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-11-08 11:42:08 -08:00
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
// Now that it should be in the database, we need to obtain the id of the
|
2010-12-21 15:03:01 -08:00
|
|
|
// place we just added.
|
2010-11-08 11:42:08 -08:00
|
|
|
bool visited = FetchVisitInfo(_place);
|
2010-12-21 15:03:01 -08:00
|
|
|
if (visited) {
|
|
|
|
NS_NOTREACHED("Not visited after adding a visit!");
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
return NS_OK;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
/**
|
|
|
|
* Updates the frecency, and possibly the hidden-ness of aPlace.
|
|
|
|
*
|
|
|
|
* @param aPlace
|
|
|
|
* The VisitData for the place we want to update.
|
|
|
|
*/
|
|
|
|
nsresult UpdateFrecency(const VisitData& aPlace)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-16 04:44:05 -08:00
|
|
|
nsresult rv;
|
2010-11-08 11:43:46 -08:00
|
|
|
{ // First, set our frecency to the proper value.
|
2010-11-16 04:44:05 -08:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
if (aPlace.placeId) {
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
2010-11-08 11:43:46 -08:00
|
|
|
"UPDATE moz_places "
|
|
|
|
"SET frecency = CALCULATE_FRECENCY(:page_id) "
|
|
|
|
"WHERE id = :page_id"
|
|
|
|
);
|
2010-11-16 04:44:05 -08:00
|
|
|
NS_ENSURE_STATE(stmt);
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), mPlace.placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"UPDATE moz_places "
|
|
|
|
"SET frecency = CALCULATE_FRECENCY(id) "
|
|
|
|
"WHERE url = :page_url"
|
|
|
|
);
|
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-11-17 16:15:49 -08:00
|
|
|
rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), aPlace.spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-16 04:44:05 -08:00
|
|
|
}
|
2010-11-08 11:43:46 -08:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
{ // Now, we need to mark the page as not hidden if the frecency is now
|
|
|
|
// nonzero.
|
2010-11-16 04:44:05 -08:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
if (aPlace.placeId) {
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
2010-11-08 11:43:46 -08:00
|
|
|
"UPDATE moz_places "
|
|
|
|
"SET hidden = 0 "
|
|
|
|
"WHERE id = :page_id AND frecency <> 0"
|
|
|
|
);
|
2010-11-16 04:44:05 -08:00
|
|
|
NS_ENSURE_STATE(stmt);
|
|
|
|
rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"), mPlace.placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"UPDATE moz_places "
|
|
|
|
"SET hidden = 0 "
|
|
|
|
"WHERE url = :page_url AND frecency <> 0"
|
|
|
|
);
|
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-11-17 16:15:49 -08:00
|
|
|
rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"), aPlace.spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-16 04:44:05 -08:00
|
|
|
}
|
2010-11-08 11:43:46 -08:00
|
|
|
|
2010-11-16 04:44:05 -08:00
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-11-08 11:43:46 -08:00
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
mozIStorageConnection* mDBConn;
|
2010-11-08 11:42:08 -08:00
|
|
|
|
|
|
|
VisitData mPlace;
|
|
|
|
VisitData mReferrer;
|
2010-11-08 11:43:46 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Strong reference to the History object because we do not want it to
|
|
|
|
* disappear out from under us.
|
|
|
|
*/
|
|
|
|
nsRefPtr<History> mHistory;
|
2010-07-13 18:00:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-11-08 11:45:46 -08:00
|
|
|
* Notifies observers about a pages title changing.
|
2010-07-13 18:00:33 -07:00
|
|
|
*/
|
2010-11-08 11:45:46 -08:00
|
|
|
class NotifyTitleObservers : public nsRunnable
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-08 11:45:46 -08:00
|
|
|
/**
|
2010-11-17 16:15:49 -08:00
|
|
|
* Notifies observers on the main thread.
|
2010-11-08 11:45:46 -08:00
|
|
|
*
|
2010-11-17 16:15:49 -08:00
|
|
|
* @param aSpec
|
|
|
|
* The spec of the URI to notify about.
|
2010-11-08 11:45:46 -08:00
|
|
|
* @param aTitle
|
|
|
|
* The new title to notify about.
|
|
|
|
*/
|
2010-11-17 16:15:49 -08:00
|
|
|
NotifyTitleObservers(const nsCString& aSpec,
|
2010-11-08 11:45:46 -08:00
|
|
|
const nsString& aTitle)
|
2010-11-17 16:15:49 -08:00
|
|
|
: mSpec(aSpec)
|
2010-11-08 11:45:46 -08:00
|
|
|
, mTitle(aTitle)
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_PRECONDITION(!NS_IsMainThread(),
|
|
|
|
"This should not be called on the main thread");
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_IMETHOD Run()
|
2010-07-13 18:00:33 -07:00
|
|
|
{
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_PRECONDITION(NS_IsMainThread(),
|
|
|
|
"This should be called on the main thread");
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
|
|
|
NS_ENSURE_TRUE(navHistory, NS_ERROR_OUT_OF_MEMORY);
|
2010-11-17 16:15:49 -08:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
(void)NS_NewURI(getter_AddRefs(uri), mSpec);
|
|
|
|
navHistory->NotifyTitleChange(uri, mTitle);
|
2010-11-08 11:47:07 -08:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-11-08 11:42:08 -08:00
|
|
|
private:
|
2010-11-17 16:15:49 -08:00
|
|
|
const nsCString mSpec;
|
2010-11-08 11:45:46 -08:00
|
|
|
const nsString mTitle;
|
2010-07-13 18:00:33 -07:00
|
|
|
};
|
2010-07-13 18:00:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-11-08 11:45:46 -08:00
|
|
|
* Sets the page title for a page in moz_places (if necessary).
|
2010-07-13 18:00:41 -07:00
|
|
|
*/
|
2010-11-08 11:45:46 -08:00
|
|
|
class SetPageTitle : public nsRunnable
|
2010-07-13 18:00:41 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-08 11:45:46 -08:00
|
|
|
/**
|
|
|
|
* Sets a pages title in the database asynchronously.
|
|
|
|
*
|
|
|
|
* @param aConnection
|
|
|
|
* The database connection to use for this operation.
|
|
|
|
* @param aURI
|
|
|
|
* The URI to set the page title on.
|
|
|
|
* @param aTitle
|
|
|
|
* The title to set for the page, if the page exists.
|
|
|
|
*/
|
|
|
|
static nsresult Start(mozIStorageConnection* aConnection,
|
|
|
|
nsIURI* aURI,
|
|
|
|
const nsString& aTitle)
|
2010-07-13 18:00:41 -07:00
|
|
|
{
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_PRECONDITION(NS_IsMainThread(),
|
|
|
|
"This should be called on the main thread");
|
2010-11-17 16:15:49 -08:00
|
|
|
NS_PRECONDITION(aURI, "Must pass a non-null URI object!");
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-17 16:15:49 -08:00
|
|
|
nsCString spec;
|
|
|
|
nsresult rv = aURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<SetPageTitle> event = new SetPageTitle(spec, aTitle);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
// Get the target thread, and then start the work!
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetInterface(aConnection);
|
|
|
|
NS_ENSURE_TRUE(target, NS_ERROR_UNEXPECTED);
|
2010-11-17 16:15:49 -08:00
|
|
|
rv = target->Dispatch(event, NS_DISPATCH_NORMAL);
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
return NS_OK;
|
2010-07-13 18:00:41 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_IMETHOD Run()
|
2010-07-13 18:00:41 -07:00
|
|
|
{
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_PRECONDITION(!NS_IsMainThread(),
|
|
|
|
"This should not be called on the main thread");
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
// First, see if the page exists in the database (we'll need its id later).
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt =
|
|
|
|
mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"SELECT id, title "
|
|
|
|
"FROM moz_places "
|
|
|
|
"WHERE url = :page_url "
|
|
|
|
);
|
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
PRInt64 placeId = 0;
|
2010-07-13 18:00:41 -07:00
|
|
|
nsAutoString title;
|
2010-11-08 11:45:46 -08:00
|
|
|
{
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
2010-11-17 16:15:49 -08:00
|
|
|
nsresult rv = URIBinder::Bind(stmt, NS_LITERAL_CSTRING("page_url"),
|
|
|
|
mSpec);
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmt->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!hasResult) {
|
|
|
|
// We have no record of this page, so there is no need to do any further
|
|
|
|
// work.
|
2010-11-17 16:15:49 -08:00
|
|
|
return NS_OK;
|
2010-11-08 11:45:46 -08:00
|
|
|
}
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
rv = stmt->GetInt64(0, &placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
rv = stmt->GetString(1, title);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(placeId > 0, "We somehow have an invalid place id here!");
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
// Also, if we have the same title, there is no reason to do another write
|
|
|
|
// or notify our observers, so bail early.
|
|
|
|
if (mTitle.Equals(title) || (mTitle.IsVoid() && title.IsVoid())) {
|
2010-11-17 16:15:49 -08:00
|
|
|
return NS_OK;
|
2010-07-13 18:00:41 -07:00
|
|
|
}
|
2010-11-08 11:45:46 -08:00
|
|
|
|
|
|
|
// Now we can update our database record.
|
|
|
|
stmt = mHistory->syncStatements.GetCachedStatement(
|
|
|
|
"UPDATE moz_places "
|
|
|
|
"SET title = :page_title "
|
|
|
|
"WHERE id = :page_id "
|
2010-07-13 18:00:41 -07:00
|
|
|
);
|
2010-11-08 11:45:46 -08:00
|
|
|
NS_ENSURE_STATE(stmt);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
{
|
|
|
|
mozStorageStatementScoper scoper(stmt);
|
|
|
|
nsresult rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("page_id"),
|
|
|
|
placeId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (mTitle.IsVoid()) {
|
|
|
|
rv = stmt->BindNullByName(NS_LITERAL_CSTRING("page_title"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = stmt->BindStringByName(NS_LITERAL_CSTRING("page_title"),
|
|
|
|
StringHead(mTitle, TITLE_LENGTH_MAX));
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-07-13 18:00:41 -07:00
|
|
|
|
2010-11-17 16:15:49 -08:00
|
|
|
nsCOMPtr<nsIRunnable> event = new NotifyTitleObservers(mSpec, mTitle);
|
|
|
|
nsresult rv = NS_DispatchToMainThread(event);
|
2010-07-13 18:00:41 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
private:
|
2010-11-17 16:15:49 -08:00
|
|
|
SetPageTitle(const nsCString& aSpec,
|
2010-11-08 11:45:46 -08:00
|
|
|
const nsString& aTitle)
|
2010-11-17 16:15:49 -08:00
|
|
|
: mSpec(aSpec)
|
2010-11-08 11:45:46 -08:00
|
|
|
, mTitle(aTitle)
|
|
|
|
, mHistory(History::GetService())
|
2010-07-13 18:00:41 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-17 16:15:49 -08:00
|
|
|
const nsCString mSpec;
|
2010-11-08 11:45:46 -08:00
|
|
|
const nsString mTitle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strong reference to the History object because we do not want it to
|
|
|
|
* disappear out from under us.
|
|
|
|
*/
|
|
|
|
nsRefPtr<History> mHistory;
|
2010-07-13 18:00:41 -07:00
|
|
|
};
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// History
|
|
|
|
|
2010-07-02 19:58:25 -07:00
|
|
|
History* History::gService = NULL;
|
2009-08-20 11:56:10 -07:00
|
|
|
|
|
|
|
History::History()
|
2010-07-13 18:00:33 -07:00
|
|
|
: mShuttingDown(false)
|
2010-11-08 11:43:46 -08:00
|
|
|
, syncStatements(mDBConn)
|
2009-08-20 11:56:10 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!gService, "Ruh-roh! This service has already been created!");
|
|
|
|
gService = this;
|
2010-07-13 18:00:33 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
NS_WARN_IF_FALSE(os, "Observer service was not found!");
|
|
|
|
if (os) {
|
|
|
|
(void)os->AddObserver(this, TOPIC_PLACES_SHUTDOWN, PR_FALSE);
|
|
|
|
}
|
2009-08-20 11:56:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
History::~History()
|
|
|
|
{
|
2010-07-02 19:58:25 -07:00
|
|
|
gService = NULL;
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-03-03 12:55:37 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (mObservers.IsInitialized()) {
|
|
|
|
NS_ASSERTION(mObservers.Count() == 0,
|
|
|
|
"Not all Links were removed before we disappear!");
|
|
|
|
}
|
|
|
|
#endif
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
void
|
|
|
|
History::NotifyVisited(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "Ruh-roh! A NULL URI was passed to us!");
|
|
|
|
|
2010-07-02 08:50:41 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
mozilla::dom::ContentParent* cpp =
|
|
|
|
mozilla::dom::ContentParent::GetSingleton(PR_FALSE);
|
|
|
|
if (cpp)
|
2010-07-26 12:59:34 -07:00
|
|
|
(void)cpp->SendNotifyVisited(aURI);
|
2010-07-02 08:50:41 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
// If the hash table has not been initialized, then we have nothing to notify
|
|
|
|
// about.
|
|
|
|
if (!mObservers.IsInitialized()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additionally, if we have no observers for this URI, we have nothing to
|
|
|
|
// notify about.
|
|
|
|
KeyClass* key = mObservers.GetEntry(aURI);
|
|
|
|
if (!key) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk through the array, and update each Link node.
|
|
|
|
const ObserverArray& observers = key->array;
|
|
|
|
ObserverArray::index_type len = observers.Length();
|
|
|
|
for (ObserverArray::index_type i = 0; i < len; i++) {
|
|
|
|
Link* link = observers[i];
|
|
|
|
link->SetLinkState(eLinkState_Visited);
|
|
|
|
NS_ASSERTION(len == observers.Length(),
|
|
|
|
"Calling SetLinkState added or removed an observer!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// All the registered nodes can now be removed for this URI.
|
|
|
|
mObservers.RemoveEntry(aURI);
|
|
|
|
}
|
|
|
|
|
2010-10-06 08:21:31 -07:00
|
|
|
mozIStorageAsyncStatement*
|
|
|
|
History::GetIsVisitedStatement()
|
|
|
|
{
|
|
|
|
if (mIsVisitedStatement) {
|
|
|
|
return mIsVisitedStatement;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't yet have a database connection, go ahead and clone it now.
|
|
|
|
if (!mReadOnlyDBConn) {
|
2010-11-08 11:43:46 -08:00
|
|
|
mozIStorageConnection* dbConn = GetDBConn();
|
2010-10-06 08:21:31 -07:00
|
|
|
NS_ENSURE_TRUE(dbConn, nsnull);
|
|
|
|
|
|
|
|
(void)dbConn->Clone(PR_TRUE, getter_AddRefs(mReadOnlyDBConn));
|
|
|
|
NS_ENSURE_TRUE(mReadOnlyDBConn, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can create our cached statement.
|
|
|
|
nsresult rv = mReadOnlyDBConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT h.id "
|
|
|
|
"FROM moz_places h "
|
|
|
|
"WHERE url = ?1 "
|
|
|
|
"AND EXISTS(SELECT id FROM moz_historyvisits WHERE place_id = h.id LIMIT 1) "
|
|
|
|
), getter_AddRefs(mIsVisitedStatement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
return mIsVisitedStatement;
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
/* static */
|
|
|
|
History*
|
|
|
|
History::GetService()
|
|
|
|
{
|
|
|
|
if (gService) {
|
|
|
|
return gService;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<IHistory> service(do_GetService(NS_IHISTORY_CONTRACTID));
|
|
|
|
NS_ABORT_IF_FALSE(service, "Cannot obtain IHistory service!");
|
|
|
|
NS_ASSERTION(gService, "Our constructor was not run?!");
|
|
|
|
|
|
|
|
return gService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
History*
|
|
|
|
History::GetSingleton()
|
|
|
|
{
|
|
|
|
if (!gService) {
|
|
|
|
gService = new History();
|
|
|
|
NS_ENSURE_TRUE(gService, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(gService);
|
|
|
|
return gService;
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
mozIStorageConnection*
|
|
|
|
History::GetDBConn()
|
|
|
|
{
|
|
|
|
if (mDBConn) {
|
|
|
|
return mDBConn;
|
|
|
|
}
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
|
|
|
NS_ENSURE_TRUE(navHistory, nsnull);
|
2010-11-08 11:43:46 -08:00
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsresult rv = navHistory->GetDBConnection(getter_AddRefs(mDBConn));
|
2010-11-08 11:43:46 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
|
|
|
return mDBConn;
|
|
|
|
}
|
|
|
|
|
2010-07-13 18:00:33 -07:00
|
|
|
void
|
|
|
|
History::Shutdown()
|
|
|
|
{
|
2010-11-17 17:23:29 -08:00
|
|
|
NS_ASSERTION(!mShuttingDown, "Shutdown was called more than once!");
|
|
|
|
|
2010-07-13 18:00:33 -07:00
|
|
|
mShuttingDown = true;
|
|
|
|
|
2010-10-13 12:02:22 -07:00
|
|
|
// Clean up our statements and connection.
|
2010-11-17 17:23:29 -08:00
|
|
|
nsISupports* obj = static_cast<IHistory*>(this);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
new FinalizeStatementCacheProxy<mozIStorageStatement>(syncStatements, obj);
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_GetInterface(mDBConn);
|
|
|
|
if (target) {
|
|
|
|
(void)target->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2010-11-08 11:43:46 -08:00
|
|
|
|
2010-10-06 08:21:31 -07:00
|
|
|
if (mReadOnlyDBConn) {
|
2010-10-13 12:02:22 -07:00
|
|
|
if (mIsVisitedStatement) {
|
|
|
|
(void)mIsVisitedStatement->Finalize();
|
|
|
|
}
|
2010-10-06 08:21:31 -07:00
|
|
|
(void)mReadOnlyDBConn->AsyncClose(nsnull);
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// IHistory
|
|
|
|
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
History::VisitURI(nsIURI* aURI,
|
|
|
|
nsIURI* aLastVisitedURI,
|
|
|
|
PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aURI, "URI should not be NULL.");
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-02 08:50:24 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
2010-11-08 11:42:08 -08:00
|
|
|
mozilla::dom::ContentChild* cpc =
|
2010-07-02 08:50:24 -07:00
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
NS_ASSERTION(cpc, "Content Protocol is NULL!");
|
2010-07-26 12:59:34 -07:00
|
|
|
(void)cpc->SendVisitURI(aURI, aLastVisitedURI, aFlags);
|
2010-07-02 08:50:24 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif /* MOZ_IPC */
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
|
|
|
NS_ENSURE_TRUE(navHistory, NS_ERROR_OUT_OF_MEMORY);
|
2010-07-13 18:00:33 -07:00
|
|
|
|
|
|
|
// Silently return if URI is something we shouldn't add to DB.
|
|
|
|
PRBool canAdd;
|
2010-11-12 14:24:25 -08:00
|
|
|
nsresult rv = navHistory->CanAddURI(aURI, &canAdd);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!canAdd) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aLastVisitedURI) {
|
2010-11-08 11:42:08 -08:00
|
|
|
PRBool same;
|
|
|
|
rv = aURI->Equals(aLastVisitedURI, &same);
|
2010-07-13 18:00:33 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-08 11:42:08 -08:00
|
|
|
if (same) {
|
|
|
|
// Do not save refresh-page visits.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
VisitData place;
|
|
|
|
rv = aURI->GetSpec(place.spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-17 16:15:49 -08:00
|
|
|
(void)GetReversedHostname(aURI, place.revHost);
|
2010-07-13 18:00:33 -07:00
|
|
|
|
|
|
|
// Assigns a type to the edge in the visit linked list. Each type will be
|
|
|
|
// considered differently when weighting the frecency of a location.
|
2010-11-12 14:24:25 -08:00
|
|
|
PRUint32 recentFlags = navHistory->GetRecentFlags(aURI);
|
2010-07-13 18:00:33 -07:00
|
|
|
bool redirected = false;
|
2010-11-12 14:24:25 -08:00
|
|
|
bool isFollowedLink = recentFlags & nsNavHistory::RECENT_ACTIVATED;
|
|
|
|
|
|
|
|
// Embed visits should never be added to the database, and the same is valid
|
|
|
|
// for redirects across frames.
|
|
|
|
// For the above reasoning non-toplevel transitions are handled at first.
|
|
|
|
// if the visit is toplevel or a non-toplevel followed link, then it can be
|
|
|
|
// handled as usual and stored on disk.
|
|
|
|
|
|
|
|
if (!(aFlags & IHistory::TOP_LEVEL) && !isFollowedLink) {
|
|
|
|
// A frame redirected to a new site without user interaction.
|
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_EMBED;
|
|
|
|
}
|
|
|
|
else if (aFlags & IHistory::REDIRECT_TEMPORARY) {
|
2010-11-08 11:42:08 -08:00
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_REDIRECT_TEMPORARY;
|
2010-07-13 18:00:33 -07:00
|
|
|
redirected = true;
|
|
|
|
}
|
|
|
|
else if (aFlags & IHistory::REDIRECT_PERMANENT) {
|
2010-11-08 11:42:08 -08:00
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT;
|
2010-07-13 18:00:33 -07:00
|
|
|
redirected = true;
|
|
|
|
}
|
|
|
|
else if (recentFlags & nsNavHistory::RECENT_TYPED) {
|
2010-11-08 11:42:08 -08:00
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_TYPED;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
else if (recentFlags & nsNavHistory::RECENT_BOOKMARKED) {
|
2010-11-08 11:42:08 -08:00
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_BOOKMARK;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
2010-11-12 14:24:25 -08:00
|
|
|
else if (!(aFlags & IHistory::TOP_LEVEL) && isFollowedLink) {
|
2010-07-13 18:00:33 -07:00
|
|
|
// User activated a link in a frame.
|
2010-11-08 11:42:08 -08:00
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_FRAMED_LINK;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-11-12 14:24:25 -08:00
|
|
|
// User was redirected or link was clicked in the main window.
|
|
|
|
place.transitionType = nsINavHistoryService::TRANSITION_LINK;
|
2010-07-13 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
place.typed = place.transitionType == nsINavHistoryService::TRANSITION_TYPED;
|
|
|
|
place.hidden =
|
|
|
|
place.transitionType == nsINavHistoryService::TRANSITION_FRAMED_LINK ||
|
|
|
|
place.transitionType == nsINavHistoryService::TRANSITION_EMBED ||
|
|
|
|
redirected;
|
|
|
|
place.visitTime = PR_Now();
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
// EMBED visits are session-persistent and should not go through the database.
|
|
|
|
// They exist only to keep track of isVisited status during the session.
|
|
|
|
if (place.transitionType == nsINavHistoryService::TRANSITION_EMBED) {
|
2010-11-17 16:15:49 -08:00
|
|
|
navHistory->registerEmbedVisit(aURI, place.visitTime);
|
2010-11-12 14:24:25 -08:00
|
|
|
// Finally, enqueue an event to notify observers.
|
|
|
|
VisitData noReferrer;
|
|
|
|
nsCOMPtr<nsIRunnable> event = new NotifyVisitObservers(place, noReferrer);
|
|
|
|
rv = NS_DispatchToMainThread(event);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mozIStorageConnection* dbConn = GetDBConn();
|
|
|
|
NS_ENSURE_STATE(dbConn);
|
2010-11-08 11:43:46 -08:00
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
rv = InsertVisitedURI::Start(dbConn, place, aLastVisitedURI);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-07-13 18:00:33 -07:00
|
|
|
|
2010-11-08 11:42:08 -08:00
|
|
|
// Finally, notify that we've been visited.
|
2010-07-13 18:00:33 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obsService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (obsService) {
|
|
|
|
obsService->NotifyObservers(aURI, NS_LINK_VISITED_EVENT_TOPIC, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
History::RegisterVisitedCallback(nsIURI* aURI,
|
|
|
|
Link* aLink)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "Must pass a non-null URI!");
|
2010-07-02 08:50:41 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
2010-08-01 23:38:10 -07:00
|
|
|
NS_PRECONDITION(aLink, "Must pass a non-null Link!");
|
2010-07-02 08:50:41 -07:00
|
|
|
}
|
|
|
|
#else
|
2010-08-01 23:38:10 -07:00
|
|
|
NS_PRECONDITION(aLink, "Must pass a non-null Link!");
|
2010-07-02 08:50:41 -07:00
|
|
|
#endif
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
// First, ensure that our hash table is setup.
|
|
|
|
if (!mObservers.IsInitialized()) {
|
2010-11-12 14:24:25 -08:00
|
|
|
NS_ENSURE_TRUE(mObservers.Init(VISIT_OBSERVERS_INITIAL_CACHE_SIZE),
|
|
|
|
NS_ERROR_OUT_OF_MEMORY);
|
2009-08-20 11:56:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain our array of observers for this URI.
|
2010-03-03 12:55:37 -08:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool keyAlreadyExists = !!mObservers.GetEntry(aURI);
|
|
|
|
#endif
|
2009-08-20 11:56:10 -07:00
|
|
|
KeyClass* key = mObservers.PutEntry(aURI);
|
|
|
|
NS_ENSURE_TRUE(key, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
ObserverArray& observers = key->array;
|
|
|
|
|
|
|
|
if (observers.IsEmpty()) {
|
2010-03-03 12:55:37 -08:00
|
|
|
NS_ASSERTION(!keyAlreadyExists,
|
|
|
|
"An empty key was kept around in our hashtable!");
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
// We are the first Link node to ask about this URI, or there are no pending
|
|
|
|
// Links wanting to know about this URI. Therefore, we should query the
|
|
|
|
// database now.
|
|
|
|
nsresult rv = VisitedQuery::Start(aURI);
|
2010-10-27 13:14:16 -07:00
|
|
|
|
|
|
|
// In IPC builds, we are passed a NULL Link from
|
|
|
|
// ContentParent::RecvStartVisitedQuery. Since we won't be adding a NULL
|
|
|
|
// entry to our list of observers, and the code after this point assumes
|
|
|
|
// that aLink is non-NULL, we will need to return now.
|
2010-07-02 08:50:41 -07:00
|
|
|
if (NS_FAILED(rv) || !aLink) {
|
2010-03-03 12:55:37 -08:00
|
|
|
// Remove our array from the hashtable so we don't keep it around.
|
|
|
|
mObservers.RemoveEntry(aURI);
|
2009-08-20 11:56:10 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2010-10-27 13:14:16 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
// In IPC builds, we are passed a NULL Link from
|
|
|
|
// ContentParent::RecvStartVisitedQuery. All of our code after this point
|
|
|
|
// assumes aLink is non-NULL, so we have to return now.
|
|
|
|
else if (!aLink) {
|
|
|
|
NS_ASSERTION(XRE_GetProcessType() == GeckoProcessType_Default,
|
|
|
|
"We should only ever get a null Link in the default process!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
2009-08-20 11:56:10 -07:00
|
|
|
|
|
|
|
// Sanity check that Links are not registered more than once for a given URI.
|
|
|
|
// This will not catch a case where it is registered for two different URIs.
|
|
|
|
NS_ASSERTION(!observers.Contains(aLink),
|
|
|
|
"Already tracking this Link object!");
|
|
|
|
|
|
|
|
// Start tracking our Link.
|
|
|
|
if (!observers.AppendElement(aLink)) {
|
|
|
|
// Curses - unregister and return failure.
|
|
|
|
(void)UnregisterVisitedCallback(aURI, aLink);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
History::UnregisterVisitedCallback(nsIURI* aURI,
|
|
|
|
Link* aLink)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aURI, "Must pass a non-null URI!");
|
|
|
|
NS_ASSERTION(aLink, "Must pass a non-null Link object!");
|
|
|
|
|
|
|
|
// Get the array, and remove the item from it.
|
|
|
|
KeyClass* key = mObservers.GetEntry(aURI);
|
|
|
|
if (!key) {
|
|
|
|
NS_ERROR("Trying to unregister for a URI that wasn't registered!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
ObserverArray& observers = key->array;
|
|
|
|
if (!observers.RemoveElement(aLink)) {
|
|
|
|
NS_ERROR("Trying to unregister a node that wasn't registered!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the array is now empty, we should remove it from the hashtable.
|
|
|
|
if (observers.IsEmpty()) {
|
|
|
|
mObservers.RemoveEntry(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-13 18:00:41 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
History::SetURITitle(nsIURI* aURI, const nsAString& aTitle)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aURI, "Must pass a non-null URI!");
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-02 08:53:42 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Content) {
|
|
|
|
mozilla::dom::ContentChild * cpc =
|
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
NS_ASSERTION(cpc, "Content Protocol is NULL!");
|
2010-07-26 12:59:34 -07:00
|
|
|
(void)cpc->SendSetURITitle(aURI, nsDependentString(aTitle));
|
2010-07-02 08:53:42 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif /* MOZ_IPC */
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
nsNavHistory* navHistory = nsNavHistory::GetHistoryService();
|
2010-07-13 18:00:41 -07:00
|
|
|
|
|
|
|
// At first, it seems like nav history should always be available here, no
|
|
|
|
// matter what.
|
|
|
|
//
|
|
|
|
// nsNavHistory fails to register as a service if there is no profile in
|
|
|
|
// place (for instance, if user is choosing a profile).
|
|
|
|
//
|
|
|
|
// Maybe the correct thing to do is to not register this service if no
|
|
|
|
// profile has been selected?
|
|
|
|
//
|
2010-11-12 14:24:25 -08:00
|
|
|
NS_ENSURE_TRUE(navHistory, NS_ERROR_FAILURE);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
|
|
|
PRBool canAdd;
|
2010-11-12 14:24:25 -08:00
|
|
|
nsresult rv = navHistory->CanAddURI(aURI, &canAdd);
|
2010-07-13 18:00:41 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!canAdd) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-12 14:24:25 -08:00
|
|
|
// Embed visits don't have a database entry, thus don't set a title on them.
|
|
|
|
if (navHistory->hasEmbedVisit(aURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
nsAutoString title;
|
2010-07-13 18:00:41 -07:00
|
|
|
if (aTitle.IsEmpty()) {
|
2010-11-08 11:45:46 -08:00
|
|
|
title.SetIsVoid(PR_TRUE);
|
2010-07-13 18:00:41 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-11-08 11:45:46 -08:00
|
|
|
title.Assign(aTitle);
|
2010-07-13 18:00:41 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 11:45:46 -08:00
|
|
|
mozIStorageConnection* dbConn = GetDBConn();
|
|
|
|
NS_ENSURE_STATE(dbConn);
|
|
|
|
|
|
|
|
rv = SetPageTitle::Start(dbConn, aURI, title);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-13 18:00:41 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-13 18:00:33 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
History::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
if (strcmp(aTopic, TOPIC_PLACES_SHUTDOWN) == 0) {
|
|
|
|
Shutdown();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
(void)os->RemoveObserver(this, TOPIC_PLACES_SHUTDOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-08-20 11:56:10 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsISupports
|
|
|
|
|
2010-11-08 11:43:46 -08:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(
|
2010-07-13 18:00:33 -07:00
|
|
|
History
|
|
|
|
, IHistory
|
|
|
|
, nsIObserver
|
2009-08-20 11:56:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
} // namespace places
|
|
|
|
} // namespace mozilla
|