Support weak references for bookmark / history / result observers. Bug 319324, r=darin.

Original committer: bryner%brianryner.com
Original revision: 1.28
Original date: 2005/12/08 22:23:52
This commit is contained in:
benjamin%smedbergs.us 2006-07-18 16:08:23 +00:00
Родитель d26ee77525
Коммит 96aa446f98
1 изменённых файлов: 35 добавлений и 41 удалений

Просмотреть файл

@ -207,7 +207,6 @@ nsIAtom* nsNavHistory::sToolbarRootAtom = nsnull;
nsNavHistory* nsNavHistory::gHistoryService; nsNavHistory* nsNavHistory::gHistoryService;
// nsNavHistory::nsNavHistory // nsNavHistory::nsNavHistory
nsNavHistory::nsNavHistory() : mNowValid(PR_FALSE), nsNavHistory::nsNavHistory() : mNowValid(PR_FALSE),
@ -652,8 +651,8 @@ nsNavHistory::InternalAdd(nsIURI* aURI, PRInt64 aSessionID,
// Notify observers. Note that we finish the transaction before doing this in // Notify observers. Note that we finish the transaction before doing this in
// case they need to use the DB // case they need to use the DB
transaction.Commit(); transaction.Commit();
for (PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver,
mObservers[i]->OnAddURI(aURI, aVisitDate); OnAddURI(aURI, aVisitDate))
return NS_OK; return NS_OK;
} }
@ -1298,13 +1297,9 @@ nsNavHistory::ExecuteQueries(nsINavHistoryQuery** aQueries, PRUint32 aQueryCount
// nsNavHistory::AddObserver // nsNavHistory::AddObserver
NS_IMETHODIMP NS_IMETHODIMP
nsNavHistory::AddObserver(nsINavHistoryObserver* aObserver) nsNavHistory::AddObserver(nsINavHistoryObserver* aObserver, PRBool aOwnsWeak)
{ {
if (mObservers.IndexOfObject(aObserver) >= 0) return mObservers.AppendWeakElement(aObserver, aOwnsWeak);
return NS_ERROR_INVALID_ARG; // already registered
if (!mObservers.AppendObject(aObserver))
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
} }
@ -1313,9 +1308,7 @@ nsNavHistory::AddObserver(nsINavHistoryObserver* aObserver)
NS_IMETHODIMP NS_IMETHODIMP
nsNavHistory::RemoveObserver(nsINavHistoryObserver* aObserver) nsNavHistory::RemoveObserver(nsINavHistoryObserver* aObserver)
{ {
if (!mObservers.RemoveObject(aObserver)) return mObservers.RemoveWeakElement(aObserver);
return NS_ERROR_INVALID_ARG;
return NS_OK;
} }
@ -1326,8 +1319,7 @@ nsNavHistory::BeginUpdateBatch()
{ {
mBatchesInProgress ++; mBatchesInProgress ++;
if (mBatchesInProgress == 1) { if (mBatchesInProgress == 1) {
for(PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver, OnBeginUpdateBatch())
mObservers[i]->OnBeginUpdateBatch();
} }
return NS_OK; return NS_OK;
} }
@ -1340,10 +1332,8 @@ nsNavHistory::EndUpdateBatch()
{ {
if (mBatchesInProgress == 0) if (mBatchesInProgress == 0)
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
mBatchesInProgress --; if (--mBatchesInProgress == 0) {
if (mBatchesInProgress == 0) { ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver, OnEndUpdateBatch())
for(PRInt32 i = 0; i < mObservers.Count(); i ++)
mObservers[i]->OnEndUpdateBatch();
} }
return NS_OK; return NS_OK;
} }
@ -1485,9 +1475,7 @@ nsNavHistory::RemovePage(nsIURI *aURI)
// that we always call the observers even though we aren't sure something // that we always call the observers even though we aren't sure something
// actually got deleted. // actually got deleted.
transaction.Commit(); transaction.Commit();
for (PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver, OnDeleteURI(aURI))
mObservers[i]->OnDeleteURI(aURI);
return NS_OK; return NS_OK;
} }
@ -1544,19 +1532,23 @@ nsNavHistory::RemovePagesFromHost(const nsACString& aHost, PRBool aEntireDomain)
revHostSlash.Append(NS_LITERAL_STRING("/")); revHostSlash.Append(NS_LITERAL_STRING("/"));
// see if we have to pass all deletes to the observers // see if we have to pass all deletes to the observers
PRBool sendObserversEverything = PR_FALSE; nsCOMArray<nsINavHistoryObserver> detailObservers;
PRInt32 i; for (PRUint32 i = 0; i < mObservers.Length(); ++i) {
for (i = 0; i < mObservers.Count(); i ++) { const nsCOMPtr<nsINavHistoryObserver> &obs = mObservers[i];
PRBool thisObserver = PR_FALSE; if (obs) {
mObservers[i]->GetWantAllDetails(&thisObserver); PRBool allDetails = PR_FALSE;
sendObserversEverything |= thisObserver; obs->GetWantAllDetails(&allDetails);
if (allDetails && !detailObservers.AppendObject(obs)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
} }
// Tell the observers about everything we are about to delete. Since this is a // Tell the observers about everything we are about to delete. Since this is a
// two-step process, if we get an error, we may tell them we will delete // two-step process, if we get an error, we may tell them we will delete
// something but then not actually delete it. Too bad. // something but then not actually delete it. Too bad.
nsCOMPtr<mozIStorageStatement> statement; nsCOMPtr<mozIStorageStatement> statement;
if (sendObserversEverything) { if (detailObservers.Count() != 0) {
// create statement depending on delete type // create statement depending on delete type
if (aEntireDomain) { if (aEntireDomain) {
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING( rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
@ -1585,8 +1577,8 @@ nsNavHistory::RemovePagesFromHost(const nsACString& aHost, PRBool aEntireDomain)
if (NS_FAILED(NS_NewURI(getter_AddRefs(thisURI), thisURIString, if (NS_FAILED(NS_NewURI(getter_AddRefs(thisURI), thisURIString,
nsnull, nsnull))) nsnull, nsnull)))
continue; // bad URI continue; // bad URI
for (i = 0; i < mObservers.Count(); i ++) for (PRInt32 j = 0; j < detailObservers.Count(); ++j)
mObservers[i]->OnDeleteURI(thisURI); detailObservers[j]->OnDeleteURI(thisURI);
} }
} }
@ -1681,8 +1673,7 @@ nsNavHistory::RemoveAllPages()
transaction.Commit(); transaction.Commit();
// notify observers // notify observers
for (PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver, OnClearHistory())
mObservers[i]->OnClearHistory();
return NS_OK; return NS_OK;
} }
@ -1759,9 +1750,10 @@ nsNavHistory::HidePage(nsIURI *aURI)
// notify observers, finish transaction first // notify observers, finish transaction first
transaction.Commit(); transaction.Commit();
for (PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver,
mObservers[i]->OnPageChanged(aURI, nsINavHistoryObserver::ATTRIBUTE_HIDDEN, OnPageChanged(aURI,
NS_LITERAL_STRING("")); nsINavHistoryObserver::ATTRIBUTE_HIDDEN,
EmptyString()))
return NS_OK; return NS_OK;
} }
@ -1843,9 +1835,10 @@ nsNavHistory::MarkPageAsTyped(nsIURI *aURI)
// observers, be sure to finish transaction first // observers, be sure to finish transaction first
transaction.Commit(); transaction.Commit();
for (PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver,
mObservers[i]->OnPageChanged(aURI, nsINavHistoryObserver::ATTRIBUTE_TYPED, OnPageChanged(aURI,
NS_LITERAL_STRING("")); nsINavHistoryObserver::ATTRIBUTE_TYPED,
EmptyString()))
return NS_OK; return NS_OK;
} }
@ -1913,9 +1906,10 @@ nsNavHistory::SetPageTitle(nsIURI *aURI,
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
// observers // observers
for (PRInt32 i = 0; i < mObservers.Count(); i ++) ENUMERATE_WEAKARRAY(mObservers, nsINavHistoryObserver,
mObservers[i]->OnPageChanged(aURI, nsINavHistoryObserver::ATTRIBUTE_TITLE, OnPageChanged(aURI,
aTitle); nsINavHistoryObserver::ATTRIBUTE_TITLE,
aTitle))
return NS_OK; return NS_OK;
} }