Bug 1855758 - Implement better progress notifications from the parser to the front end. r=BenC,mkmelin

So that we can trigger actions as soon as the message body is ready, we need to know when the
parser has finished outputting it.

Unfortunately my original plan of using web progress notifications is inadequate as it's not
possible to pass useful information with the notifications, so this patch implements a specialist
notification. (Thanks to nsBrowserStatusFilter for this, it discards almost all the data it
receives.) The "mark message as read" action is then connected to the notification.

Differential Revision: https://phabricator.services.mozilla.com/D191039

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Geoff Lankow 2023-10-19 22:33:42 +00:00
Родитель 4ae1e23f28
Коммит 83e27447ce
6 изменённых файлов: 98 добавлений и 74 удалений

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

@ -454,7 +454,7 @@ async function OnLoadMsgHeaderPane() {
getMessagePaneBrowser().addProgressListener(
messageProgressListener,
Ci.nsIWebProgress.NOTIFY_STATUS | Ci.nsIWebProgress.NOTIFY_STATE_ALL
Ci.nsIWebProgress.NOTIFY_STATE_ALL
);
gHeaderCustomize.init();
@ -523,38 +523,31 @@ var MsgHdrViewObserver = {
* Receives a message's headers as we display the message through our mime converter.
*
* @see {nsIMailChannel}
* @implements {nsIMailProgressListener}
* @implements {nsIWebProgressListener}
* @implements {nsISupportsWeakReference}
*/
var messageProgressListener = {
QueryInterface: ChromeUtils.generateQI([
"nsIMailProgressListener",
"nsIWebProgressListener",
"nsISupportsWeakReference",
]),
/**
* @param {nsIWebProgress} webProgress
* @param {nsIRequest} request
* @param {nsresult} status
* @param {wstring} message
* @see {nsIWebProgressListener}
* @param {nsIMailChannel} mailChannel
* @see {nsIMailProgressListener}
*/
onStatusChange(webProgress, request, status, message) {
// We use status change events as a notification from the parser that the
// headers are ready to use. Though this event could be fired for any
// of the resources in the message, the first three arguments aren't given
// so there's no way to know. We'll assume that the first time we get here
// after STATE_START is the notification from the parser, since at that
// point there should be no other requests in progress.
const docRequest = getMessagePaneBrowser().webProgress?.documentRequest;
if (
!this._sawHeaders &&
docRequest &&
docRequest instanceof Ci.nsIMailChannel
) {
this._sawHeaders = true;
this.processHeaders(docRequest.headerNames, docRequest.headerValues);
}
onHeadersComplete(mailChannel) {
this.processHeaders(mailChannel.headerNames, mailChannel.headerValues);
},
/**
* @param {nsIMailChannel} mailChannel
* @see {nsIMailProgressListener}
*/
onBodyComplete(mailChannel) {
autoMarkAsRead();
},
/**
@ -578,17 +571,12 @@ var messageProgressListener = {
ClearAttachmentList();
gMessageNotificationBar.clearMsgNotifications();
this._sawHeaders = false;
request.listener = this;
request.smimeHeaderSink = smimeHeaderSink;
this.onStartHeaders();
} else if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
currentCharacterSet = request.mailCharacterSet;
request.smimeHeaderSink = null;
if (!this._sawHeaders) {
// This shouldn't happen, but it's here as a backup plan.
this._sawHeaders = true;
this.processHeaders(request.headerNames, request.headerValues);
}
if (request.imipItem) {
calImipBar.showImipBar(request.imipItem, request.imipMethod);
}
@ -4191,14 +4179,22 @@ function OnMsgLoaded(aUrl) {
return;
}
let win =
location.href == "about:message"
? window.browsingContext.topChromeWindow
: window;
let wintype = win.document.documentElement.getAttribute("windowtype");
gMessageNotificationBar.setJunkMsg(gMessage);
// See if MDN was requested but has not been sent.
HandleMDNResponse(aUrl);
}
/**
* Marks the message as read, optionally after a delay, if the preferences say
* we should do so.
*/
function autoMarkAsRead() {
if (!gMessage?.folder) {
// The message can't be marked read or unread.
return;
}
let markReadAutoMode = Services.prefs.getBoolPref(
"mailnews.mark_message_read.auto"
);
@ -4206,14 +4202,15 @@ function OnMsgLoaded(aUrl) {
// We just finished loading a message. If messages are to be marked as read
// automatically, set a timer to mark the message is read after n seconds
// where n can be configured by the user.
if (gMessage && !gMessage.isRead && markReadAutoMode) {
if (!gMessage.isRead && markReadAutoMode) {
let markReadOnADelay = Services.prefs.getBoolPref(
"mailnews.mark_message_read.delay"
);
let winType = top.document.documentElement.getAttribute("windowtype");
// Only use the timer if viewing using the 3-pane preview pane and the
// user has set the pref.
if (markReadOnADelay && wintype == "mail:3pane") {
if (markReadOnADelay && winType == "mail:3pane") {
// 3-pane window
ClearPendingReadTimer();
let markReadDelayTime = Services.prefs.getIntPref(
@ -4233,9 +4230,6 @@ function OnMsgLoaded(aUrl) {
MarkMessageAsRead(gMessage);
}
}
// See if MDN was requested but has not been sent.
HandleMDNResponse(aUrl);
}
/**

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

@ -7,6 +7,10 @@
#include "nsIPropertyBag2.idl"
#include "nsIMsgSMIMEHeaderSink.idl"
interface nsIMailProgressListener;
interface nsIWebProgress;
interface nsIRequest;
/**
* An interface that email-streaming channels can use to provide access to
* parsed message headers, message attachment info, and other metadata.
@ -75,4 +79,25 @@ interface nsIMailChannel : nsISupports {
* Set this in onStartRequest to receive security status notifications.
*/
attribute nsIMsgSMIMEHeaderSink smimeHeaderSink;
/**
* A listener for progress events. This object must also implement
* nsISupportsWeakReference.
*/
attribute nsIMailProgressListener listener;
};
[scriptable, uuid(1286f969-1c20-422e-8247-233fe0d26ba5)]
interface nsIMailProgressListener : nsISupports {
/**
* Receive a notification from the parser that it has finished outputting
* the headers to the channel.
*/
void onHeadersComplete(in nsIMailChannel mailChannel);
/**
* Receive a notification from the parser that it has finished outputting
* the message body to the channel.
*/
void onBodyComplete(in nsIMailChannel mailChannel);
};

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

@ -10,6 +10,7 @@ export class MailChannel {
_headerValues = [];
_attachments = [];
_mailCharacterSet = null;
_progressListener = null;
addHeaderFromMIME(name, value) {
this._headerNames.push(name);
@ -59,4 +60,12 @@ export class MailChannel {
imipMethod = null;
imipItem = null;
smimeHeaderSink = null;
get listener() {
return this._progressListener?.get();
}
set listener(listener) {
this._progressListener = Cu.getWeakReference(listener);
}
}

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

@ -118,3 +118,21 @@ nsMailChannel::SetSmimeHeaderSink(nsIMsgSMIMEHeaderSink* aSmimeHeaderSink) {
mSmimeHeaderSink = aSmimeHeaderSink;
return NS_OK;
}
NS_IMETHODIMP
nsMailChannel::GetListener(nsIMailProgressListener** aListener) {
nsCOMPtr<nsIMailProgressListener> listener = do_QueryReferent(mListener);
if (listener) {
NS_IF_ADDREF(*aListener = listener);
} else {
*aListener = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP
nsMailChannel::SetListener(nsIMailProgressListener* aListener) {
nsresult rv;
mListener = do_GetWeakReference(aListener, &rv);
return rv;
}

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

@ -10,6 +10,7 @@
#include "nsTArray.h"
#include "nsTString.h"
#include "calIItipItem.h"
#include "nsIWeakReferenceUtils.h"
class nsMailChannel : public nsIMailChannel {
public:
@ -23,6 +24,7 @@ class nsMailChannel : public nsIMailChannel {
nsCString mImipMethod;
nsCOMPtr<calIItipItem> mImipItem;
nsCOMPtr<nsIMsgSMIMEHeaderSink> mSmimeHeaderSink;
nsWeakPtr mListener;
};
#endif /* nsMailChannel_h__ */

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

@ -196,23 +196,10 @@ nsresult nsMimeHtmlDisplayEmitter::BroadcastHeaders(int32_t aHeaderMode) {
}
// Notify the front end that the headers are ready on `mailChannel`.
nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks;
mChannel->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks));
if (NS_SUCCEEDED(rv) && notificationCallbacks) {
nsCOMPtr<nsIProgressEventSink> sink;
rv = notificationCallbacks->GetInterface(NS_GET_IID(nsIProgressEventSink),
getter_AddRefs(sink));
if (NS_SUCCEEDED(rv) && sink) {
nsCOMPtr<nsIURI> uri;
mChannel->GetURI(getter_AddRefs(uri));
nsCString host;
uri->GetHost(host);
// The listener gets the localized string "Transferring data from {host}…"
// because we use NS_NET_STATUS_RECEIVING_FROM. The channel and status
// code itself do not get passed to the listener.
sink->OnStatus(mChannel, NS_NET_STATUS_RECEIVING_FROM,
NS_ConvertUTF8toUTF16(host).get());
}
nsCOMPtr<nsIMailProgressListener> listener;
mailChannel->GetListener(getter_AddRefs(listener));
if (listener) {
listener->OnHeadersComplete(mailChannel);
}
return rv;
@ -440,24 +427,13 @@ nsresult nsMimeHtmlDisplayEmitter::EndBody() {
}
// Notify the front end that we've finished reading the body.
nsCOMPtr<nsIInterfaceRequestor> notificationCallbacks;
nsresult rv =
mChannel->GetNotificationCallbacks(getter_AddRefs(notificationCallbacks));
if (NS_SUCCEEDED(rv) && notificationCallbacks) {
nsCOMPtr<nsIProgressEventSink> sink;
rv = notificationCallbacks->GetInterface(NS_GET_IID(nsIProgressEventSink),
getter_AddRefs(sink));
if (NS_SUCCEEDED(rv) && sink) {
nsCOMPtr<nsIURI> uri;
mChannel->GetURI(getter_AddRefs(uri));
nsCString host;
uri->GetHost(host);
// The listener gets the localized string "Read {host}" because we use
// NS_NET_STATUS_READING. The channel and status code itself do not get
// passed to the listener.
sink->OnStatus(mChannel, NS_NET_STATUS_READING,
NS_ConvertUTF8toUTF16(host).get());
}
nsresult rv;
nsCOMPtr<nsIMailChannel> mailChannel = do_QueryInterface(mChannel, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMailProgressListener> listener;
mailChannel->GetListener(getter_AddRefs(listener));
if (listener) {
listener->OnBodyComplete(mailChannel);
}
return NS_OK;