Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsSecurityWarningDialogs.h" |
michael@0 | 8 | #include "nsIComponentManager.h" |
michael@0 | 9 | #include "nsIServiceManager.h" |
michael@0 | 10 | #include "nsReadableUtils.h" |
michael@0 | 11 | #include "nsString.h" |
michael@0 | 12 | #include "nsIPrompt.h" |
michael@0 | 13 | #include "nsIInterfaceRequestor.h" |
michael@0 | 14 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 15 | #include "nsIPrefService.h" |
michael@0 | 16 | #include "nsIPrefBranch.h" |
michael@0 | 17 | #include "nsThreadUtils.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "mozilla/Telemetry.h" |
michael@0 | 20 | #include "nsISecurityUITelemetry.h" |
michael@0 | 21 | |
michael@0 | 22 | NS_IMPL_ISUPPORTS(nsSecurityWarningDialogs, nsISecurityWarningDialogs) |
michael@0 | 23 | |
michael@0 | 24 | #define STRING_BUNDLE_URL "chrome://pipnss/locale/security.properties" |
michael@0 | 25 | |
michael@0 | 26 | nsSecurityWarningDialogs::nsSecurityWarningDialogs() |
michael@0 | 27 | { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | nsSecurityWarningDialogs::~nsSecurityWarningDialogs() |
michael@0 | 31 | { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | nsresult |
michael@0 | 35 | nsSecurityWarningDialogs::Init() |
michael@0 | 36 | { |
michael@0 | 37 | nsresult rv; |
michael@0 | 38 | |
michael@0 | 39 | mPrefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv); |
michael@0 | 40 | if (NS_FAILED(rv)) return rv; |
michael@0 | 41 | |
michael@0 | 42 | nsCOMPtr<nsIStringBundleService> service = |
michael@0 | 43 | do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); |
michael@0 | 44 | if (NS_FAILED(rv)) return rv; |
michael@0 | 45 | |
michael@0 | 46 | rv = service->CreateBundle(STRING_BUNDLE_URL, |
michael@0 | 47 | getter_AddRefs(mStringBundle)); |
michael@0 | 48 | return rv; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | NS_IMETHODIMP |
michael@0 | 52 | nsSecurityWarningDialogs::ConfirmPostToInsecureFromSecure(nsIInterfaceRequestor *ctx, bool* _result) |
michael@0 | 53 | { |
michael@0 | 54 | nsresult rv; |
michael@0 | 55 | |
michael@0 | 56 | // The Telemetry clickthrough constant is 1 more than the constant for the dialog. |
michael@0 | 57 | rv = ConfirmDialog(ctx, nullptr, // No preference for this one - it's too important |
michael@0 | 58 | MOZ_UTF16("PostToInsecureFromSecureMessage"), |
michael@0 | 59 | nullptr, |
michael@0 | 60 | nsISecurityUITelemetry::WARNING_CONFIRM_POST_TO_INSECURE_FROM_SECURE, |
michael@0 | 61 | _result); |
michael@0 | 62 | |
michael@0 | 63 | return rv; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsresult |
michael@0 | 67 | nsSecurityWarningDialogs::ConfirmDialog(nsIInterfaceRequestor *ctx, const char *prefName, |
michael@0 | 68 | const char16_t *messageName, |
michael@0 | 69 | const char16_t *showAgainName, |
michael@0 | 70 | const uint32_t aBucket, |
michael@0 | 71 | bool* _result) |
michael@0 | 72 | { |
michael@0 | 73 | nsresult rv; |
michael@0 | 74 | |
michael@0 | 75 | // Get user's preference for this alert |
michael@0 | 76 | // prefName, showAgainName are null if there is no preference for this dialog |
michael@0 | 77 | bool prefValue = true; |
michael@0 | 78 | |
michael@0 | 79 | if (prefName) { |
michael@0 | 80 | rv = mPrefBranch->GetBoolPref(prefName, &prefValue); |
michael@0 | 81 | if (NS_FAILED(rv)) prefValue = true; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | // Stop if confirm is not requested |
michael@0 | 85 | if (!prefValue) { |
michael@0 | 86 | *_result = true; |
michael@0 | 87 | return NS_OK; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 91 | mozilla::Telemetry::Accumulate(mozilla::Telemetry::SECURITY_UI, aBucket); |
michael@0 | 92 | // See AlertDialog() for a description of how showOnce works. |
michael@0 | 93 | nsAutoCString showOncePref(prefName); |
michael@0 | 94 | showOncePref += ".show_once"; |
michael@0 | 95 | |
michael@0 | 96 | bool showOnce = false; |
michael@0 | 97 | mPrefBranch->GetBoolPref(showOncePref.get(), &showOnce); |
michael@0 | 98 | |
michael@0 | 99 | if (showOnce) |
michael@0 | 100 | prefValue = false; |
michael@0 | 101 | |
michael@0 | 102 | // Get Prompt to use |
michael@0 | 103 | nsCOMPtr<nsIPrompt> prompt = do_GetInterface(ctx); |
michael@0 | 104 | if (!prompt) return NS_ERROR_FAILURE; |
michael@0 | 105 | |
michael@0 | 106 | // Get messages strings from localization file |
michael@0 | 107 | nsXPIDLString windowTitle, message, alertMe, cont; |
michael@0 | 108 | |
michael@0 | 109 | mStringBundle->GetStringFromName(MOZ_UTF16("Title"), |
michael@0 | 110 | getter_Copies(windowTitle)); |
michael@0 | 111 | mStringBundle->GetStringFromName(messageName, |
michael@0 | 112 | getter_Copies(message)); |
michael@0 | 113 | if (showAgainName) { |
michael@0 | 114 | mStringBundle->GetStringFromName(showAgainName, |
michael@0 | 115 | getter_Copies(alertMe)); |
michael@0 | 116 | } |
michael@0 | 117 | mStringBundle->GetStringFromName(MOZ_UTF16("Continue"), |
michael@0 | 118 | getter_Copies(cont)); |
michael@0 | 119 | // alertMe is allowed to be null |
michael@0 | 120 | if (!windowTitle || !message || !cont) return NS_ERROR_FAILURE; |
michael@0 | 121 | |
michael@0 | 122 | // Replace # characters with newlines to lay out the dialog. |
michael@0 | 123 | char16_t* msgchars = message.BeginWriting(); |
michael@0 | 124 | |
michael@0 | 125 | uint32_t i = 0; |
michael@0 | 126 | for (i = 0; msgchars[i] != '\0'; i++) { |
michael@0 | 127 | if (msgchars[i] == '#') { |
michael@0 | 128 | msgchars[i] = '\n'; |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | int32_t buttonPressed; |
michael@0 | 133 | |
michael@0 | 134 | rv = prompt->ConfirmEx(windowTitle, |
michael@0 | 135 | message, |
michael@0 | 136 | (nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_0) + |
michael@0 | 137 | (nsIPrompt::BUTTON_TITLE_CANCEL * nsIPrompt::BUTTON_POS_1), |
michael@0 | 138 | cont, |
michael@0 | 139 | nullptr, |
michael@0 | 140 | nullptr, |
michael@0 | 141 | alertMe, |
michael@0 | 142 | &prefValue, |
michael@0 | 143 | &buttonPressed); |
michael@0 | 144 | |
michael@0 | 145 | if (NS_FAILED(rv)) return rv; |
michael@0 | 146 | |
michael@0 | 147 | *_result = (buttonPressed != 1); |
michael@0 | 148 | if (*_result) { |
michael@0 | 149 | // For confirmation dialogs, the clickthrough constant is 1 more |
michael@0 | 150 | // than the constant for the dialog. |
michael@0 | 151 | mozilla::Telemetry::Accumulate(mozilla::Telemetry::SECURITY_UI, aBucket + 1); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | if (!prefValue && prefName) { |
michael@0 | 155 | mPrefBranch->SetBoolPref(prefName, false); |
michael@0 | 156 | } else if (prefValue && showOnce) { |
michael@0 | 157 | mPrefBranch->SetBoolPref(showOncePref.get(), false); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | return rv; |
michael@0 | 161 | } |
michael@0 | 162 |