bug 343033: 5-10 second delay or hang or crash when quitting Cocoa Firefox

patch: cancel background download as soon as user quits
r=darin
This commit is contained in:
tony%ponderer.org 2006-11-20 18:24:01 +00:00
Родитель 6727dd009a
Коммит 3bf63ef255
3 изменённых файлов: 81 добавлений и 16 удалений

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

@ -105,7 +105,7 @@ Rot13Line(nsCString &line)
line.BeginWriting(start); line.BeginWriting(start);
line.EndWriting(end); line.EndWriting(end);
while (start != end) { while (start != end) {
*start = kRot13Table[*start]; *start = kRot13Table[NS_STATIC_CAST(PRInt32, *start)];
++start; ++start;
} }
} }
@ -973,6 +973,7 @@ nsUrlClassifierDBService::Observe(nsISupports *aSubject, const char *aTopic,
const PRUnichar *aData) const PRUnichar *aData)
{ {
if (nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) { if (nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
LOG(("shutting down db service\n"));
Shutdown(); Shutdown();
} }
return NS_OK; return NS_OK;

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

@ -36,14 +36,17 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#include "nsCRT.h"
#include "nsIObserverService.h"
#include "nsIURI.h" #include "nsIURI.h"
#include "nsIUrlClassifierDBService.h" #include "nsIUrlClassifierDBService.h"
#include "nsNetUtil.h"
#include "nsStreamUtils.h" #include "nsStreamUtils.h"
#include "nsToolkitCompsCID.h" #include "nsToolkitCompsCID.h"
#include "nsUrlClassifierStreamUpdater.h" #include "nsUrlClassifierStreamUpdater.h"
#include "prlog.h" #include "prlog.h"
static const char* gQuitApplicationMessage = "quit-application";
// NSPR_LOG_MODULES=UrlClassifierStreamUpdater:5 // NSPR_LOG_MODULES=UrlClassifierStreamUpdater:5
#if defined(PR_LOGGING) #if defined(PR_LOGGING)
static const PRLogModuleInfo *gUrlClassifierStreamUpdaterLog = nsnull; static const PRLogModuleInfo *gUrlClassifierStreamUpdaterLog = nsnull;
@ -61,7 +64,8 @@ class TableUpdateListener : public nsIStreamListener
{ {
public: public:
TableUpdateListener(nsIUrlClassifierCallback *aTableCallback, TableUpdateListener(nsIUrlClassifierCallback *aTableCallback,
nsIUrlClassifierCallback *aErrorCallback); nsIUrlClassifierCallback *aErrorCallback,
nsUrlClassifierStreamUpdater* aStreamUpdater);
nsCOMPtr<nsIUrlClassifierDBService> mDBService; nsCOMPtr<nsIUrlClassifierDBService> mDBService;
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
@ -74,14 +78,19 @@ private:
// Callback when table updates complete. // Callback when table updates complete.
nsCOMPtr<nsIUrlClassifierCallback> mTableCallback; nsCOMPtr<nsIUrlClassifierCallback> mTableCallback;
nsCOMPtr<nsIUrlClassifierCallback> mErrorCallback; nsCOMPtr<nsIUrlClassifierCallback> mErrorCallback;
// Reference to the stream updater that created this.
nsUrlClassifierStreamUpdater *mStreamUpdater;
}; };
TableUpdateListener::TableUpdateListener( TableUpdateListener::TableUpdateListener(
nsIUrlClassifierCallback *aTableCallback, nsIUrlClassifierCallback *aTableCallback,
nsIUrlClassifierCallback *aErrorCallback) nsIUrlClassifierCallback *aErrorCallback,
nsUrlClassifierStreamUpdater* aStreamUpdater)
{ {
mTableCallback = aTableCallback; mTableCallback = aTableCallback;
mErrorCallback = aErrorCallback; mErrorCallback = aErrorCallback;
mStreamUpdater = aStreamUpdater;
} }
NS_IMPL_ISUPPORTS2(TableUpdateListener, nsIStreamListener, nsIRequestObserver) NS_IMPL_ISUPPORTS2(TableUpdateListener, nsIStreamListener, nsIRequestObserver)
@ -175,10 +184,7 @@ TableUpdateListener::OnStopRequest(nsIRequest *request, nsISupports* context,
else else
mDBService->CancelStream(); mDBService->CancelStream();
nsUrlClassifierStreamUpdater* updater = mStreamUpdater->DownloadDone();
NS_STATIC_CAST(nsUrlClassifierStreamUpdater*, context);
NS_ASSERTION(updater != nsnull, "failed to cast context");
updater->mIsUpdating = PR_FALSE;
return NS_OK; return NS_OK;
} }
@ -188,15 +194,32 @@ TableUpdateListener::OnStopRequest(nsIRequest *request, nsISupports* context,
// Handles creating/running the stream listener // Handles creating/running the stream listener
nsUrlClassifierStreamUpdater::nsUrlClassifierStreamUpdater() nsUrlClassifierStreamUpdater::nsUrlClassifierStreamUpdater()
: mIsUpdating(PR_FALSE), mUpdateUrl(nsnull) : mIsUpdating(PR_FALSE), mInitialized(PR_FALSE), mUpdateUrl(nsnull),
mListener(nsnull), mChannel(nsnull)
{ {
#if defined(PR_LOGGING) #if defined(PR_LOGGING)
if (!gUrlClassifierStreamUpdaterLog) if (!gUrlClassifierStreamUpdaterLog)
gUrlClassifierStreamUpdaterLog = PR_NewLogModule("UrlClassifierStreamUpdater"); gUrlClassifierStreamUpdaterLog = PR_NewLogModule("UrlClassifierStreamUpdater");
#endif #endif
} }
NS_IMPL_ISUPPORTS1(nsUrlClassifierStreamUpdater, nsIUrlClassifierStreamUpdater) NS_IMPL_ISUPPORTS2(nsUrlClassifierStreamUpdater,
nsIUrlClassifierStreamUpdater,
nsIObserver)
/**
* Drop our reference to mChannel if we have one.
*/
void
nsUrlClassifierStreamUpdater::DownloadDone()
{
mIsUpdating = PR_FALSE;
mChannel = nsnull;
}
///////////////////////////////////////////////////////////////////////////////
// nsIUrlClassifierStreamUpdater implementation
NS_IMETHODIMP NS_IMETHODIMP
nsUrlClassifierStreamUpdater::GetUpdateUrl(nsACString & aUpdateUrl) nsUrlClassifierStreamUpdater::GetUpdateUrl(nsACString & aUpdateUrl)
@ -235,17 +258,29 @@ nsUrlClassifierStreamUpdater::DownloadUpdates(
return NS_ERROR_NOT_INITIALIZED; return NS_ERROR_NOT_INITIALIZED;
} }
if (!mInitialized) {
// Add an observer for shutdown so we can cancel any pending list
// downloads. quit-application is the same event that the download
// manager listens for and uses to cancel pending downloads.
nsCOMPtr<nsIObserverService> observerService =
do_GetService("@mozilla.org/observer-service;1");
if (!observerService)
return NS_ERROR_FAILURE;
observerService->AddObserver(this, gQuitApplicationMessage, PR_FALSE);
mInitialized = PR_TRUE;
}
// Ok, try to create the download channel. // Ok, try to create the download channel.
nsresult rv; nsresult rv;
nsCOMPtr<nsIChannel> channel; rv = NS_NewChannel(getter_AddRefs(mChannel), mUpdateUrl);
rv = NS_NewChannel(getter_AddRefs(channel), mUpdateUrl);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
// Bind to a different callback each time we invoke this method. // Bind to a different callback each time we invoke this method.
mListener = new TableUpdateListener(aTableCallback, aErrorCallback); mListener = new TableUpdateListener(aTableCallback, aErrorCallback, this);
// Make the request // Make the request
rv = channel->AsyncOpen(mListener.get(), this); rv = mChannel->AsyncOpen(mListener.get(), nsnull);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
mIsUpdating = PR_TRUE; mIsUpdating = PR_TRUE;
@ -253,3 +288,23 @@ nsUrlClassifierStreamUpdater::DownloadUpdates(
return NS_OK; return NS_OK;
} }
///////////////////////////////////////////////////////////////////////////////
// nsIObserver implementation
NS_IMETHODIMP
nsUrlClassifierStreamUpdater::Observe(nsISupports *aSubject, const char *aTopic,
const PRUnichar *aData)
{
if (nsCRT::strcmp(aTopic, gQuitApplicationMessage) == 0) {
if (mIsUpdating && mChannel) {
LOG(("Cancel download"));
nsresult rv;
rv = mChannel->Cancel(NS_ERROR_ABORT);
NS_ENSURE_SUCCESS(rv, rv);
mIsUpdating = PR_FALSE;
mChannel = nsnull;
}
}
return NS_OK;
}

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

@ -42,21 +42,27 @@
#include <nsISupportsUtils.h> #include <nsISupportsUtils.h>
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsIObserver.h"
#include "nsIUrlClassifierStreamUpdater.h" #include "nsIUrlClassifierStreamUpdater.h"
#include "nsIStreamListener.h" #include "nsIStreamListener.h"
#include "nsNetUtil.h"
// Forward declare pointers // Forward declare pointers
class nsIURI; class nsIURI;
class nsUrlClassifierStreamUpdater : public nsIUrlClassifierStreamUpdater class nsUrlClassifierStreamUpdater : public nsIUrlClassifierStreamUpdater,
public nsIObserver
{ {
public: public:
nsUrlClassifierStreamUpdater(); nsUrlClassifierStreamUpdater();
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSIURLCLASSIFIERSTREAMUPDATER NS_DECL_NSIURLCLASSIFIERSTREAMUPDATER
NS_DECL_NSIOBSERVER
PRBool mIsUpdating; // When the channel gets OnStopRequest, we call this
// to reset the stream updater.
void DownloadDone();
private: private:
// No subclassing // No subclassing
@ -65,8 +71,11 @@ private:
// Disallow copy constructor // Disallow copy constructor
nsUrlClassifierStreamUpdater(nsUrlClassifierStreamUpdater&); nsUrlClassifierStreamUpdater(nsUrlClassifierStreamUpdater&);
PRBool mIsUpdating;
PRBool mInitialized;
nsCOMPtr<nsIURI> mUpdateUrl; nsCOMPtr<nsIURI> mUpdateUrl;
nsCOMPtr<nsIStreamListener> mListener; nsCOMPtr<nsIStreamListener> mListener;
nsCOMPtr<nsIChannel> mChannel;
}; };
#endif // nsUrlClassifierStreamUpdater_h_ #endif // nsUrlClassifierStreamUpdater_h_