1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/manager/boot/src/nsSecurityWarningDialogs.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsSecurityWarningDialogs.h" 1.11 +#include "nsIComponentManager.h" 1.12 +#include "nsIServiceManager.h" 1.13 +#include "nsReadableUtils.h" 1.14 +#include "nsString.h" 1.15 +#include "nsIPrompt.h" 1.16 +#include "nsIInterfaceRequestor.h" 1.17 +#include "nsIInterfaceRequestorUtils.h" 1.18 +#include "nsIPrefService.h" 1.19 +#include "nsIPrefBranch.h" 1.20 +#include "nsThreadUtils.h" 1.21 + 1.22 +#include "mozilla/Telemetry.h" 1.23 +#include "nsISecurityUITelemetry.h" 1.24 + 1.25 +NS_IMPL_ISUPPORTS(nsSecurityWarningDialogs, nsISecurityWarningDialogs) 1.26 + 1.27 +#define STRING_BUNDLE_URL "chrome://pipnss/locale/security.properties" 1.28 + 1.29 +nsSecurityWarningDialogs::nsSecurityWarningDialogs() 1.30 +{ 1.31 +} 1.32 + 1.33 +nsSecurityWarningDialogs::~nsSecurityWarningDialogs() 1.34 +{ 1.35 +} 1.36 + 1.37 +nsresult 1.38 +nsSecurityWarningDialogs::Init() 1.39 +{ 1.40 + nsresult rv; 1.41 + 1.42 + mPrefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); 1.43 + if (NS_FAILED(rv)) return rv; 1.44 + 1.45 + nsCOMPtr<nsIStringBundleService> service = 1.46 + do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); 1.47 + if (NS_FAILED(rv)) return rv; 1.48 + 1.49 + rv = service->CreateBundle(STRING_BUNDLE_URL, 1.50 + getter_AddRefs(mStringBundle)); 1.51 + return rv; 1.52 +} 1.53 + 1.54 +NS_IMETHODIMP 1.55 +nsSecurityWarningDialogs::ConfirmPostToInsecureFromSecure(nsIInterfaceRequestor *ctx, bool* _result) 1.56 +{ 1.57 + nsresult rv; 1.58 + 1.59 + // The Telemetry clickthrough constant is 1 more than the constant for the dialog. 1.60 + rv = ConfirmDialog(ctx, nullptr, // No preference for this one - it's too important 1.61 + MOZ_UTF16("PostToInsecureFromSecureMessage"), 1.62 + nullptr, 1.63 + nsISecurityUITelemetry::WARNING_CONFIRM_POST_TO_INSECURE_FROM_SECURE, 1.64 + _result); 1.65 + 1.66 + return rv; 1.67 +} 1.68 + 1.69 +nsresult 1.70 +nsSecurityWarningDialogs::ConfirmDialog(nsIInterfaceRequestor *ctx, const char *prefName, 1.71 + const char16_t *messageName, 1.72 + const char16_t *showAgainName, 1.73 + const uint32_t aBucket, 1.74 + bool* _result) 1.75 +{ 1.76 + nsresult rv; 1.77 + 1.78 + // Get user's preference for this alert 1.79 + // prefName, showAgainName are null if there is no preference for this dialog 1.80 + bool prefValue = true; 1.81 + 1.82 + if (prefName) { 1.83 + rv = mPrefBranch->GetBoolPref(prefName, &prefValue); 1.84 + if (NS_FAILED(rv)) prefValue = true; 1.85 + } 1.86 + 1.87 + // Stop if confirm is not requested 1.88 + if (!prefValue) { 1.89 + *_result = true; 1.90 + return NS_OK; 1.91 + } 1.92 + 1.93 + MOZ_ASSERT(NS_IsMainThread()); 1.94 + mozilla::Telemetry::Accumulate(mozilla::Telemetry::SECURITY_UI, aBucket); 1.95 + // See AlertDialog() for a description of how showOnce works. 1.96 + nsAutoCString showOncePref(prefName); 1.97 + showOncePref += ".show_once"; 1.98 + 1.99 + bool showOnce = false; 1.100 + mPrefBranch->GetBoolPref(showOncePref.get(), &showOnce); 1.101 + 1.102 + if (showOnce) 1.103 + prefValue = false; 1.104 + 1.105 + // Get Prompt to use 1.106 + nsCOMPtr<nsIPrompt> prompt = do_GetInterface(ctx); 1.107 + if (!prompt) return NS_ERROR_FAILURE; 1.108 + 1.109 + // Get messages strings from localization file 1.110 + nsXPIDLString windowTitle, message, alertMe, cont; 1.111 + 1.112 + mStringBundle->GetStringFromName(MOZ_UTF16("Title"), 1.113 + getter_Copies(windowTitle)); 1.114 + mStringBundle->GetStringFromName(messageName, 1.115 + getter_Copies(message)); 1.116 + if (showAgainName) { 1.117 + mStringBundle->GetStringFromName(showAgainName, 1.118 + getter_Copies(alertMe)); 1.119 + } 1.120 + mStringBundle->GetStringFromName(MOZ_UTF16("Continue"), 1.121 + getter_Copies(cont)); 1.122 + // alertMe is allowed to be null 1.123 + if (!windowTitle || !message || !cont) return NS_ERROR_FAILURE; 1.124 + 1.125 + // Replace # characters with newlines to lay out the dialog. 1.126 + char16_t* msgchars = message.BeginWriting(); 1.127 + 1.128 + uint32_t i = 0; 1.129 + for (i = 0; msgchars[i] != '\0'; i++) { 1.130 + if (msgchars[i] == '#') { 1.131 + msgchars[i] = '\n'; 1.132 + } 1.133 + } 1.134 + 1.135 + int32_t buttonPressed; 1.136 + 1.137 + rv = prompt->ConfirmEx(windowTitle, 1.138 + message, 1.139 + (nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_0) + 1.140 + (nsIPrompt::BUTTON_TITLE_CANCEL * nsIPrompt::BUTTON_POS_1), 1.141 + cont, 1.142 + nullptr, 1.143 + nullptr, 1.144 + alertMe, 1.145 + &prefValue, 1.146 + &buttonPressed); 1.147 + 1.148 + if (NS_FAILED(rv)) return rv; 1.149 + 1.150 + *_result = (buttonPressed != 1); 1.151 + if (*_result) { 1.152 + // For confirmation dialogs, the clickthrough constant is 1 more 1.153 + // than the constant for the dialog. 1.154 + mozilla::Telemetry::Accumulate(mozilla::Telemetry::SECURITY_UI, aBucket + 1); 1.155 + } 1.156 + 1.157 + if (!prefValue && prefName) { 1.158 + mPrefBranch->SetBoolPref(prefName, false); 1.159 + } else if (prefValue && showOnce) { 1.160 + mPrefBranch->SetBoolPref(showOncePref.get(), false); 1.161 + } 1.162 + 1.163 + return rv; 1.164 +} 1.165 +