Bug 1461106 - Remove SMTP password from cache when deleted from password manager to prevent stale connection attempts. r=jorgk a=jorgk BETA_60_CONTINUATION
authorJosiah Bruner <jsbruner@umich.edu>
Fri, 25 May 2018 14:18:00 +0200
branchBETA_60_CONTINUATION
changeset 31117 074ed27e1c152136815eb6fa119bd07494e1500c
parent 31116 7e1066ec6885c3f403ed58bbc58a37e1b40ce984
child 31118 13b00eca25c3da67029e34b47d487434736172c9
push id2213
push usermozilla@jorgk.com
push dateMon, 04 Jun 2018 06:13:58 +0000
treeherdercomm-beta@074ed27e1c15 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorgk, jorgk
bugs1461106
Bug 1461106 - Remove SMTP password from cache when deleted from password manager to prevent stale connection attempts. r=jorgk a=jorgk
mailnews/build/nsMailModule.cpp
mailnews/compose/src/nsSmtpServer.cpp
mailnews/compose/src/nsSmtpServer.h
--- a/mailnews/build/nsMailModule.cpp
+++ b/mailnews/build/nsMailModule.cpp
@@ -533,17 +533,17 @@ NS_DEFINE_NAMED_CID(NS_ABLDIFSERVICE_CID
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsBayesianFilter, Init)
 
 NS_DEFINE_NAMED_CID(NS_BAYESIANFILTER_CID);
 
 ////////////////////////////////////////////////////////////////////////////////
 // compose factories
 ////////////////////////////////////////////////////////////////////////////////
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsSmtpService)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsSmtpServer)
+NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsSmtpServer, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgCompose)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgComposeParams)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgComposeSendListener)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgComposeProgressParams)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgCompFields)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgAttachment)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgAttachmentData)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMsgAttachedFile)
--- a/mailnews/compose/src/nsSmtpServer.cpp
+++ b/mailnews/compose/src/nsSmtpServer.cpp
@@ -13,36 +13,76 @@
 #include "nsMsgBaseCID.h"
 #include "nsISmtpService.h"
 #include "nsMsgCompCID.h"
 #include "nsILoginInfo.h"
 #include "nsILoginManager.h"
 #include "nsIArray.h"
 #include "nsArrayUtils.h"
 #include "nsMemory.h"
+#include "nsIObserverService.h"
 
 NS_IMPL_ADDREF(nsSmtpServer)
 NS_IMPL_RELEASE(nsSmtpServer)
 NS_INTERFACE_MAP_BEGIN(nsSmtpServer)
     NS_INTERFACE_MAP_ENTRY(nsISmtpServer)
     NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
+    NS_INTERFACE_MAP_ENTRY(nsIObserver)
     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISmtpServer)
 NS_INTERFACE_MAP_END
 
 nsSmtpServer::nsSmtpServer():
     mKey("")
 {
     m_logonFailed = false;
     getPrefs();
 }
 
 nsSmtpServer::~nsSmtpServer()
 {
 }
 
+nsresult nsSmtpServer::Init()
+{
+  // We need to know when the password manager changes.
+  nsCOMPtr<nsIObserverService> observerService =
+           mozilla::services::GetObserverService();
+  NS_ENSURE_TRUE(observerService, NS_ERROR_UNEXPECTED);
+
+  observerService->AddObserver(this, "passwordmgr-storage-changed", false);
+  observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsSmtpServer::Observe(nsISupports *aSubject, const char* aTopic,
+                             const char16_t *aData)
+{
+  // When the state of the password manager changes we need to clear the
+  // password from the cache in case the user just removed it.
+  if (strcmp(aTopic, "passwordmgr-storage-changed") == 0)
+  {
+    m_password.Truncate();
+  }
+  else if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0)
+  {
+    // Now remove ourselves from the observer service as well.
+    nsCOMPtr<nsIObserverService> observerService =
+      mozilla::services::GetObserverService();
+    NS_ENSURE_TRUE(observerService, NS_ERROR_UNEXPECTED);
+
+    observerService->RemoveObserver(this, "passwordmgr-storage-changed");
+    observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
+  }
+
+  return NS_OK;
+}
+
+
 NS_IMETHODIMP
 nsSmtpServer::GetKey(char * *aKey)
 {
     if (!aKey) return NS_ERROR_NULL_POINTER;
     if (mKey.IsEmpty())
         *aKey = nullptr;
     else
         *aKey = ToNewCString(mKey);
--- a/mailnews/compose/src/nsSmtpServer.h
+++ b/mailnews/compose/src/nsSmtpServer.h
@@ -6,25 +6,29 @@
 #ifndef __nsSmtpServer_h_
 #define __nsSmtpServer_h_
 
 
 #include "nsString.h"
 #include "nsISmtpServer.h"
 #include "nsIPrefBranch.h"
 #include "nsWeakReference.h"
+#include "nsIObserver.h"
 
 class nsSmtpServer : public nsISmtpServer,
-                     public nsSupportsWeakReference
+                     public nsSupportsWeakReference,
+                     public nsIObserver
 {
 public:
     nsSmtpServer();
+    nsresult Init();
 
     NS_DECL_ISUPPORTS
     NS_DECL_NSISMTPSERVER
+    NS_DECL_NSIOBSERVER
 
 private:
     virtual ~nsSmtpServer();
     nsCString mKey;
     nsCOMPtr<nsIPrefBranch> mPrefBranch;
     nsCOMPtr<nsIPrefBranch> mDefPrefBranch;
 
     nsresult getPrefs();