Bug 494940 - Open mixed content dialog asynchronously, r=bz
authorOlli Pettay <Olli.Pettay@helsinki.fi>
Thu, 28 May 2009 11:25:09 +0300
changeset 28803 2ad7177c98bda1039e72f6165764bbe6d8b94a02
parent 28802 c569f6c33f0589a6bc9e48588f0c82f37e04f52f
child 28804 ddd616e583093f3079a7a6c29f55ef39bdf27fff
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbz
bugs494940
milestone1.9.2a1pre
Bug 494940 - Open mixed content dialog asynchronously, r=bz
security/manager/boot/src/nsSecurityWarningDialogs.cpp
security/manager/boot/src/nsSecurityWarningDialogs.h
--- a/security/manager/boot/src/nsSecurityWarningDialogs.cpp
+++ b/security/manager/boot/src/nsSecurityWarningDialogs.cpp
@@ -43,16 +43,18 @@
 #include "nsIServiceManager.h"
 #include "nsReadableUtils.h"
 #include "nsString.h"
 #include "nsIPrompt.h"
 #include "nsIInterfaceRequestor.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsIPrefService.h"
 #include "nsIPrefBranch.h"
+#include "nsThreadUtils.h"
+#include "nsAutoPtr.h"
 
 NS_IMPL_THREADSAFE_ISUPPORTS1(nsSecurityWarningDialogs, nsISecurityWarningDialogs)
 
 #define STRING_BUNDLE_URL    "chrome://pipnss/locale/security.properties"
 
 #define ENTER_SITE_PREF      "security.warn_entering_secure"
 #define WEAK_SITE_PREF       "security.warn_entering_weak"
 #define LEAVE_SITE_PREF      "security.warn_leaving_secure"
@@ -86,119 +88,164 @@ nsSecurityWarningDialogs::Init()
 
 NS_IMETHODIMP 
 nsSecurityWarningDialogs::ConfirmEnteringSecure(nsIInterfaceRequestor *ctx, PRBool *_retval)
 {
   nsresult rv;
 
   rv = AlertDialog(ctx, ENTER_SITE_PREF, 
                    NS_LITERAL_STRING("EnterSecureMessage").get(),
-                   NS_LITERAL_STRING("EnterSecureShowAgain").get());
+                   NS_LITERAL_STRING("EnterSecureShowAgain").get(),
+                   PR_FALSE);
 
   *_retval = PR_TRUE;
   return rv;
 }
 
 NS_IMETHODIMP 
 nsSecurityWarningDialogs::ConfirmEnteringWeak(nsIInterfaceRequestor *ctx, PRBool *_retval)
 {
   nsresult rv;
 
   rv = AlertDialog(ctx, WEAK_SITE_PREF,
                    NS_LITERAL_STRING("WeakSecureMessage").get(),
-                   NS_LITERAL_STRING("WeakSecureShowAgain").get());
+                   NS_LITERAL_STRING("WeakSecureShowAgain").get(),
+                   PR_FALSE);
 
   *_retval = PR_TRUE;
   return rv;
 }
 
 NS_IMETHODIMP 
 nsSecurityWarningDialogs::ConfirmLeavingSecure(nsIInterfaceRequestor *ctx, PRBool *_retval)
 {
   nsresult rv;
 
   rv = AlertDialog(ctx, LEAVE_SITE_PREF, 
                    NS_LITERAL_STRING("LeaveSecureMessage").get(),
-                   NS_LITERAL_STRING("LeaveSecureShowAgain").get());
+                   NS_LITERAL_STRING("LeaveSecureShowAgain").get(),
+                   PR_FALSE);
 
   *_retval = PR_TRUE;
   return rv;
 }
 
 
 NS_IMETHODIMP 
 nsSecurityWarningDialogs::ConfirmMixedMode(nsIInterfaceRequestor *ctx, PRBool *_retval)
 {
   nsresult rv;
 
   rv = AlertDialog(ctx, MIXEDCONTENT_PREF, 
                    NS_LITERAL_STRING("MixedContentMessage").get(),
-                   NS_LITERAL_STRING("MixedContentShowAgain").get());
+                   NS_LITERAL_STRING("MixedContentShowAgain").get(),
+                   PR_TRUE);
 
   *_retval = PR_TRUE;
   return rv;
 }
 
+class nsAsyncAlert : public nsRunnable
+{
+public:
+  nsAsyncAlert(nsIPrompt*       aPrompt,
+               const char*      aPrefName,
+               const PRUnichar* aDialogMessageName,
+               const PRUnichar* aShowAgainName,
+               nsIPrefBranch*   aPrefBranch,
+               nsIStringBundle* aStringBundle)
+  : mPrompt(aPrompt), mPrefName(aPrefName),
+    mDialogMessageName(aDialogMessageName),
+    mShowAgainName(aShowAgainName), mPrefBranch(aPrefBranch),
+    mStringBundle(aStringBundle) {}
+  NS_IMETHOD Run();
 
-nsresult
-nsSecurityWarningDialogs::AlertDialog(nsIInterfaceRequestor *ctx, const char *prefName,
-                          const PRUnichar *dialogMessageName,
-                          const PRUnichar *showAgainName)
+protected:
+  nsCOMPtr<nsIPrompt>       mPrompt;
+  nsCString                 mPrefName;
+  nsString                  mDialogMessageName;
+  nsString                  mShowAgainName;
+  nsCOMPtr<nsIPrefBranch>   mPrefBranch;
+  nsCOMPtr<nsIStringBundle> mStringBundle;
+};
+
+NS_IMETHODIMP
+nsAsyncAlert::Run()
 {
   nsresult rv;
 
   // Get user's preference for this alert
   PRBool prefValue;
-  rv = mPrefBranch->GetBoolPref(prefName, &prefValue);
+  rv = mPrefBranch->GetBoolPref(mPrefName.get(), &prefValue);
   if (NS_FAILED(rv)) prefValue = PR_TRUE;
 
   // Stop if alert is not requested
   if (!prefValue) return NS_OK;
 
   // Check for a show-once pref for this dialog.
   // If the show-once pref is set to true:
   //   - The default value of the "show every time" checkbox is unchecked
   //   - If the user checks the checkbox, we clear the show-once pref.
 
-  nsCAutoString showOncePref(prefName);
+  nsCAutoString showOncePref(mPrefName);
   showOncePref += ".show_once";
 
   PRBool showOnce = PR_FALSE;
   mPrefBranch->GetBoolPref(showOncePref.get(), &showOnce);
 
   if (showOnce)
     prefValue = PR_FALSE;
 
-  // Get Prompt to use
-  nsCOMPtr<nsIPrompt> prompt = do_GetInterface(ctx);
-  if (!prompt) return NS_ERROR_FAILURE;
-
   // Get messages strings from localization file
   nsXPIDLString windowTitle, message, dontShowAgain;
 
   mStringBundle->GetStringFromName(NS_LITERAL_STRING("Title").get(),
                                    getter_Copies(windowTitle));
-  mStringBundle->GetStringFromName(dialogMessageName,
+  mStringBundle->GetStringFromName(mDialogMessageName.get(),
                                    getter_Copies(message));
-  mStringBundle->GetStringFromName(showAgainName,
+  mStringBundle->GetStringFromName(mShowAgainName.get(),
                                    getter_Copies(dontShowAgain));
   if (!windowTitle || !message || !dontShowAgain) return NS_ERROR_FAILURE;
 
-  rv = prompt->AlertCheck(windowTitle, message, dontShowAgain, &prefValue);
+  rv = mPrompt->AlertCheck(windowTitle, message, dontShowAgain, &prefValue);
   if (NS_FAILED(rv)) return rv;
       
   if (!prefValue) {
-    mPrefBranch->SetBoolPref(prefName, PR_FALSE);
+    mPrefBranch->SetBoolPref(mPrefName.get(), PR_FALSE);
   } else if (showOnce) {
     mPrefBranch->SetBoolPref(showOncePref.get(), PR_FALSE);
   }
 
   return rv;
 }
 
+
+nsresult
+nsSecurityWarningDialogs::AlertDialog(nsIInterfaceRequestor* aCtx,
+                                      const char* aPrefName,
+                                      const PRUnichar* aDialogMessageName,
+                                      const PRUnichar* aShowAgainName,
+                                      PRBool aAsync)
+{
+  // Get Prompt to use
+  nsCOMPtr<nsIPrompt> prompt = do_GetInterface(aCtx);
+  if (!prompt) return NS_ERROR_FAILURE;
+
+  nsRefPtr<nsAsyncAlert> alert = new nsAsyncAlert(prompt,
+                                                  aPrefName,
+                                                  aDialogMessageName,
+                                                  aShowAgainName,
+                                                  mPrefBranch,
+                                                  mStringBundle);
+  NS_ENSURE_TRUE(alert, NS_ERROR_OUT_OF_MEMORY);
+  return aAsync ? NS_DispatchToCurrentThread(alert) : alert->Run();
+}
+
+
+
 NS_IMETHODIMP 
 nsSecurityWarningDialogs::ConfirmPostToInsecure(nsIInterfaceRequestor *ctx, PRBool* _result)
 {
   nsresult rv;
 
   rv = ConfirmDialog(ctx, INSECURE_SUBMIT_PREF,
                      NS_LITERAL_STRING("PostToInsecureFromInsecureMessage").get(),
                      NS_LITERAL_STRING("PostToInsecureFromInsecureShowAgain").get(),
--- a/security/manager/boot/src/nsSecurityWarningDialogs.h
+++ b/security/manager/boot/src/nsSecurityWarningDialogs.h
@@ -54,17 +54,18 @@ public:
   nsSecurityWarningDialogs();
   virtual ~nsSecurityWarningDialogs();
 
   nsresult Init();
 
 protected:
   nsresult AlertDialog(nsIInterfaceRequestor *ctx, const char *prefName,
                    const PRUnichar *messageName,
-                   const PRUnichar *showAgainName);
+                   const PRUnichar *showAgainName,
+                   PRBool aAsync);
   nsresult ConfirmDialog(nsIInterfaceRequestor *ctx, const char *prefName,
                    const PRUnichar *messageName, 
                    const PRUnichar *showAgainName, PRBool* _result);
   nsCOMPtr<nsIStringBundle> mStringBundle;
   nsCOMPtr<nsIPrefBranch> mPrefBranch;
 };
 
 #define NS_SECURITYWARNINGDIALOGS_CID \