Back to index

lightning-sunbird  0.9+nobinonly
nsSecurityWarningDialogs.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Terry Hayes <thayes@netscape.com>
00025  *   Javier Delgadillo <javi@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsSecurityWarningDialogs.h"
00042 #include "nsIComponentManager.h"
00043 #include "nsIServiceManager.h"
00044 #include "nsReadableUtils.h"
00045 #include "nsString.h"
00046 #include "nsIPrompt.h"
00047 #include "nsIInterfaceRequestor.h"
00048 #include "nsIInterfaceRequestorUtils.h"
00049 #include "nsIPrefService.h"
00050 #include "nsIPrefBranch.h"
00051 
00052 NS_IMPL_THREADSAFE_ISUPPORTS1(nsSecurityWarningDialogs, nsISecurityWarningDialogs)
00053 
00054 static NS_DEFINE_CID(kCStringBundleServiceCID,  NS_STRINGBUNDLESERVICE_CID);
00055 
00056 #define STRING_BUNDLE_URL    "chrome://pipnss/locale/security.properties"
00057 
00058 #define ENTER_SITE_PREF      "security.warn_entering_secure"
00059 #define WEAK_SITE_PREF       "security.warn_entering_weak"
00060 #define LEAVE_SITE_PREF      "security.warn_leaving_secure"
00061 #define MIXEDCONTENT_PREF    "security.warn_viewing_mixed"
00062 #define INSECURE_SUBMIT_PREF "security.warn_submit_insecure"
00063 
00064 nsSecurityWarningDialogs::nsSecurityWarningDialogs()
00065 {
00066 }
00067 
00068 nsSecurityWarningDialogs::~nsSecurityWarningDialogs()
00069 {
00070 }
00071 
00072 nsresult
00073 nsSecurityWarningDialogs::Init()
00074 {
00075   nsresult rv;
00076 
00077   mPrefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
00078   if (NS_FAILED(rv)) return rv;
00079 
00080   nsCOMPtr<nsIStringBundleService> service = do_GetService(kCStringBundleServiceCID, &rv);
00081   if (NS_FAILED(rv)) return rv;
00082   
00083   rv = service->CreateBundle(STRING_BUNDLE_URL,
00084                              getter_AddRefs(mStringBundle));
00085   return rv;
00086 }
00087 
00088 NS_IMETHODIMP 
00089 nsSecurityWarningDialogs::ConfirmEnteringSecure(nsIInterfaceRequestor *ctx, PRBool *_retval)
00090 {
00091   nsresult rv;
00092 
00093   rv = AlertDialog(ctx, ENTER_SITE_PREF, 
00094                    NS_LITERAL_STRING("EnterSecureMessage").get(),
00095                    NS_LITERAL_STRING("EnterSecureShowAgain").get());
00096 
00097   *_retval = PR_TRUE;
00098   return rv;
00099 }
00100 
00101 NS_IMETHODIMP 
00102 nsSecurityWarningDialogs::ConfirmEnteringWeak(nsIInterfaceRequestor *ctx, PRBool *_retval)
00103 {
00104   nsresult rv;
00105 
00106   rv = AlertDialog(ctx, WEAK_SITE_PREF,
00107                    NS_LITERAL_STRING("WeakSecureMessage").get(),
00108                    NS_LITERAL_STRING("WeakSecureShowAgain").get());
00109 
00110   *_retval = PR_TRUE;
00111   return rv;
00112 }
00113 
00114 NS_IMETHODIMP 
00115 nsSecurityWarningDialogs::ConfirmLeavingSecure(nsIInterfaceRequestor *ctx, PRBool *_retval)
00116 {
00117   nsresult rv;
00118 
00119   rv = AlertDialog(ctx, LEAVE_SITE_PREF, 
00120                    NS_LITERAL_STRING("LeaveSecureMessage").get(),
00121                    NS_LITERAL_STRING("LeaveSecureShowAgain").get());
00122 
00123   *_retval = PR_TRUE;
00124   return rv;
00125 }
00126 
00127 
00128 NS_IMETHODIMP 
00129 nsSecurityWarningDialogs::ConfirmMixedMode(nsIInterfaceRequestor *ctx, PRBool *_retval)
00130 {
00131   nsresult rv;
00132 
00133   rv = AlertDialog(ctx, MIXEDCONTENT_PREF, 
00134                    NS_LITERAL_STRING("MixedContentMessage").get(),
00135                    NS_LITERAL_STRING("MixedContentShowAgain").get());
00136 
00137   *_retval = PR_TRUE;
00138   return rv;
00139 }
00140 
00141 
00142 nsresult
00143 nsSecurityWarningDialogs::AlertDialog(nsIInterfaceRequestor *ctx, const char *prefName,
00144                           const PRUnichar *dialogMessageName,
00145                           const PRUnichar *showAgainName)
00146 {
00147   nsresult rv;
00148 
00149   // Get user's preference for this alert
00150   PRBool prefValue;
00151   rv = mPrefBranch->GetBoolPref(prefName, &prefValue);
00152   if (NS_FAILED(rv)) prefValue = PR_TRUE;
00153 
00154   // Stop if alert is not requested
00155   if (!prefValue) return NS_OK;
00156 
00157   // Check for a show-once pref for this dialog.
00158   // If the show-once pref is set to true:
00159   //   - The default value of the "show every time" checkbox is unchecked
00160   //   - If the user checks the checkbox, we clear the show-once pref.
00161 
00162   nsCAutoString showOncePref(prefName);
00163   showOncePref += ".show_once";
00164 
00165   PRBool showOnce = PR_FALSE;
00166   mPrefBranch->GetBoolPref(showOncePref.get(), &showOnce);
00167 
00168   if (showOnce)
00169     prefValue = PR_FALSE;
00170 
00171   // Get Prompt to use
00172   nsCOMPtr<nsIPrompt> prompt = do_GetInterface(ctx);
00173   if (!prompt) return NS_ERROR_FAILURE;
00174 
00175   // Get messages strings from localization file
00176   nsXPIDLString windowTitle, message, dontShowAgain;
00177 
00178   mStringBundle->GetStringFromName(NS_LITERAL_STRING("Title").get(),
00179                                    getter_Copies(windowTitle));
00180   mStringBundle->GetStringFromName(dialogMessageName,
00181                                    getter_Copies(message));
00182   mStringBundle->GetStringFromName(showAgainName,
00183                                    getter_Copies(dontShowAgain));
00184   if (!windowTitle || !message || !dontShowAgain) return NS_ERROR_FAILURE;
00185 
00186   rv = prompt->AlertCheck(windowTitle, message, dontShowAgain, &prefValue);
00187   if (NS_FAILED(rv)) return rv;
00188       
00189   if (!prefValue) {
00190     mPrefBranch->SetBoolPref(prefName, PR_FALSE);
00191   } else if (showOnce) {
00192     mPrefBranch->SetBoolPref(showOncePref.get(), PR_FALSE);
00193   }
00194 
00195   return rv;
00196 }
00197 
00198 NS_IMETHODIMP 
00199 nsSecurityWarningDialogs::ConfirmPostToInsecure(nsIInterfaceRequestor *ctx, PRBool* _result)
00200 {
00201   nsresult rv;
00202 
00203   rv = ConfirmDialog(ctx, INSECURE_SUBMIT_PREF,
00204                      NS_LITERAL_STRING("PostToInsecureFromInsecureMessage").get(),
00205                      NS_LITERAL_STRING("PostToInsecureFromInsecureShowAgain").get(),
00206                      _result);
00207 
00208   return rv;
00209 }
00210 
00211 NS_IMETHODIMP 
00212 nsSecurityWarningDialogs::ConfirmPostToInsecureFromSecure(nsIInterfaceRequestor *ctx, PRBool* _result)
00213 {
00214   nsresult rv;
00215 
00216   rv = ConfirmDialog(ctx, nsnull, // No preference for this one - it's too important
00217                      NS_LITERAL_STRING("PostToInsecureFromSecureMessage").get(),
00218                      nsnull, 
00219                      _result);
00220 
00221   return rv;
00222 }
00223 
00224 nsresult
00225 nsSecurityWarningDialogs::ConfirmDialog(nsIInterfaceRequestor *ctx, const char *prefName,
00226                             const PRUnichar *messageName, 
00227                             const PRUnichar *showAgainName, 
00228                             PRBool* _result)
00229 {
00230   nsresult rv;
00231 
00232   // Get user's preference for this alert
00233   // prefName, showAgainName are null if there is no preference for this dialog
00234   PRBool prefValue = PR_TRUE;
00235   
00236   if (prefName != nsnull) {
00237     rv = mPrefBranch->GetBoolPref(prefName, &prefValue);
00238     if (NS_FAILED(rv)) prefValue = PR_TRUE;
00239   }
00240   
00241   // Stop if confirm is not requested
00242   if (!prefValue) {
00243     *_result = PR_TRUE;
00244     return NS_OK;
00245   }
00246   
00247   // See AlertDialog() for a description of how showOnce works.
00248   nsCAutoString showOncePref(prefName);
00249   showOncePref += ".show_once";
00250 
00251   PRBool showOnce = PR_FALSE;
00252   mPrefBranch->GetBoolPref(showOncePref.get(), &showOnce);
00253 
00254   if (showOnce)
00255     prefValue = PR_FALSE;
00256 
00257   // Get Prompt to use
00258   nsCOMPtr<nsIPrompt> prompt = do_GetInterface(ctx);
00259   if (!prompt) return NS_ERROR_FAILURE;
00260 
00261   // Get messages strings from localization file
00262   nsXPIDLString windowTitle, message, alertMe, cont;
00263 
00264   mStringBundle->GetStringFromName(NS_LITERAL_STRING("Title").get(),
00265                                    getter_Copies(windowTitle));
00266   mStringBundle->GetStringFromName(messageName,
00267                                    getter_Copies(message));
00268   if (showAgainName != nsnull) {
00269     mStringBundle->GetStringFromName(showAgainName,
00270                                      getter_Copies(alertMe));
00271   }
00272   mStringBundle->GetStringFromName(NS_LITERAL_STRING("Continue").get(),
00273                                    getter_Copies(cont));
00274   // alertMe is allowed to be null
00275   if (!windowTitle || !message || !cont) return NS_ERROR_FAILURE;
00276       
00277   // Replace # characters with newlines to lay out the dialog.
00278   PRUnichar* msgchars = message.BeginWriting();
00279   
00280   PRUint32 i = 0;
00281   for (i = 0; msgchars[i] != '\0'; i++) {
00282     if (msgchars[i] == '#') {
00283       msgchars[i] = '\n';
00284     }
00285   }  
00286 
00287   PRInt32 buttonPressed;
00288 
00289   rv  = prompt->ConfirmEx(windowTitle, 
00290                           message, 
00291                           (nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_0) +
00292                           (nsIPrompt::BUTTON_TITLE_CANCEL * nsIPrompt::BUTTON_POS_1),
00293                           cont,
00294                           nsnull,
00295                           nsnull,
00296                           alertMe, 
00297                           &prefValue, 
00298                           &buttonPressed);
00299 
00300   if (NS_FAILED(rv)) return rv;
00301 
00302   *_result = (buttonPressed != 1);
00303 
00304   if (!prefValue && prefName != nsnull) {
00305     mPrefBranch->SetBoolPref(prefName, PR_FALSE);
00306   } else if (prefValue && showOnce) {
00307     mPrefBranch->SetBoolPref(showOncePref.get(), PR_FALSE);
00308   }
00309 
00310   return rv;
00311 }
00312