Thunderbird:ShutdownService

From MozillaWiki
Jump to: navigation, search

Mailnews Shutdown Service

A new shutdown interface is being developed for core mailnews. The new service should make it easier for developers and extension writers to do some things at shutdown time.

Information about the implementation of the service is available at bug 397498.

Any class that wants to perform a shutdown task can simply inherit from nsIObserver and register for the task named: "msg-shutdown":

 ...
 nsCOMPtr<nsIObserverService> observerService;
 observerService = do_GetService("@mozilla.org/observer-service;1");
 if (observerService)
   observerService->AddObserver(this, "msg-shutdown", PR_FALSE);
 ...

When the shutdown service detects a notification that the entire application wants to shutdown, it builds a to-do list of tasks by iterating through the "msg-shutdown" observers. Each observer is asked if it still needs to perform the task by calling NeedsToRunTask(). Setting the in-param in the nsIMsgShutdownTask instance will determine if the task will be queued or not.

When a nsIMsgShutdownTask instances turn is up to run, the shutdown service will call DoShutdownTask(). It is up to the nsIMsgShutdownTask instance to call OnStopRunningUrl() on the passed in nsIUrlListener in DoShutdownTask()

 NS_IMETHODIMP ShutdownTask::DoShutdownTask(nsIUrlListener *inUrlListener, 
                                            nsIMsgWindow *inMsgWindow)
 {
   ...
   mUrlListener = inUrlListener;
 }
 
 ...
 
 nsresult ShutdownTask::ImDoneWithMyTask()
 {
   ...
   // Tell the shutdown service we are done running.
   mUrlListener->OnStopRunningUrl(nsnull, NS_OK);
 }

During the span of time when a task instance is running progress text can be modified by calling nsMsgShutdownService::SetStatusText().

 ...
 nsCOMPtr<nsIMsgShutdownService> shutdownService;
 ...
 shutdownService->SetStatusText(NS_LITERAL_STRING("Hi Mom!"));
 ..

Note: Be sure to use a string-bundle for task title and progress strings. A initial .properties file has already been created (shutdownWindow.properties).