Backed out 2 changesets (bug 1908320) for causing wr failures on align-items-baseline...
[gecko.git] / security / manager / pki / nsNSSDialogs.cpp
blob159c873d340ee7e12e9df515ba7dc4c9a3c10d6b
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /*
8 * Dialog services for PIP.
9 */
11 #include "nsNSSDialogs.h"
13 #include "mozIDOMWindow.h"
14 #include "nsArray.h"
15 #include "nsComponentManagerUtils.h"
16 #include "nsEmbedCID.h"
17 #include "nsHashPropertyBag.h"
18 #include "nsIDialogParamBlock.h"
19 #include "nsIInterfaceRequestor.h"
20 #include "nsIInterfaceRequestorUtils.h"
21 #include "nsIPK11Token.h"
22 #include "nsIPromptService.h"
23 #include "nsIWindowWatcher.h"
24 #include "nsIX509CertDB.h"
25 #include "nsIX509Cert.h"
26 #include "nsNSSDialogHelper.h"
27 #include "nsPromiseFlatString.h"
28 #include "nsServiceManagerUtils.h"
29 #include "nsString.h"
30 #include "nsVariant.h"
32 #define PIPSTRING_BUNDLE_URL "chrome://pippki/locale/pippki.properties"
34 nsNSSDialogs::nsNSSDialogs() = default;
36 nsNSSDialogs::~nsNSSDialogs() = default;
38 NS_IMPL_ISUPPORTS(nsNSSDialogs, nsITokenPasswordDialogs, nsICertificateDialogs)
40 nsresult nsNSSDialogs::Init() {
41 nsresult rv;
43 nsCOMPtr<nsIStringBundleService> service =
44 do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
45 if (NS_FAILED(rv)) return rv;
47 rv = service->CreateBundle(PIPSTRING_BUNDLE_URL,
48 getter_AddRefs(mPIPStringBundle));
49 return rv;
52 NS_IMETHODIMP
53 nsNSSDialogs::SetPassword(nsIInterfaceRequestor* ctx, nsIPK11Token* token,
54 /*out*/ bool* canceled) {
55 // |ctx| is allowed to be null.
56 NS_ENSURE_ARG(canceled);
58 *canceled = false;
60 // Get the parent window for the dialog
61 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
63 nsCOMPtr<nsIDialogParamBlock> block =
64 do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID);
65 if (!block) return NS_ERROR_FAILURE;
67 nsCOMPtr<nsIMutableArray> objects = nsArrayBase::Create();
68 if (!objects) {
69 return NS_ERROR_FAILURE;
71 nsresult rv = objects->AppendElement(token);
72 if (NS_FAILED(rv)) {
73 return rv;
75 rv = block->SetObjects(objects);
76 if (NS_FAILED(rv)) {
77 return rv;
80 rv = nsNSSDialogHelper::openDialog(
81 parent, "chrome://pippki/content/changepassword.xhtml", block);
83 if (NS_FAILED(rv)) return rv;
85 int32_t status;
87 rv = block->GetInt(1, &status);
88 if (NS_FAILED(rv)) return rv;
90 *canceled = (status == 0);
92 return rv;
95 NS_IMETHODIMP
96 nsNSSDialogs::ConfirmDownloadCACert(nsIInterfaceRequestor* ctx,
97 nsIX509Cert* cert,
98 /*out*/ uint32_t* trust,
99 /*out*/ bool* importConfirmed) {
100 // |ctx| is allowed to be null.
101 NS_ENSURE_ARG(cert);
102 NS_ENSURE_ARG(trust);
103 NS_ENSURE_ARG(importConfirmed);
105 nsCOMPtr<nsIMutableArray> argArray = nsArrayBase::Create();
106 if (!argArray) {
107 return NS_ERROR_FAILURE;
110 nsresult rv = argArray->AppendElement(cert);
111 if (NS_FAILED(rv)) {
112 return rv;
115 nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag();
116 rv = argArray->AppendElement(retVals);
117 if (NS_FAILED(rv)) {
118 return rv;
121 // Get the parent window for the dialog
122 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
123 rv = nsNSSDialogHelper::openDialog(
124 parent, "chrome://pippki/content/downloadcert.xhtml", argArray);
125 if (NS_FAILED(rv)) {
126 return rv;
129 rv = retVals->GetPropertyAsBool(u"importConfirmed"_ns, importConfirmed);
130 if (NS_FAILED(rv)) {
131 return rv;
134 *trust = nsIX509CertDB::UNTRUSTED;
135 if (!*importConfirmed) {
136 return NS_OK;
139 bool trustForSSL = false;
140 rv = retVals->GetPropertyAsBool(u"trustForSSL"_ns, &trustForSSL);
141 if (NS_FAILED(rv)) {
142 return rv;
144 bool trustForEmail = false;
145 rv = retVals->GetPropertyAsBool(u"trustForEmail"_ns, &trustForEmail);
146 if (NS_FAILED(rv)) {
147 return rv;
150 *trust |= trustForSSL ? nsIX509CertDB::TRUSTED_SSL : 0;
151 *trust |= trustForEmail ? nsIX509CertDB::TRUSTED_EMAIL : 0;
153 return NS_OK;
156 NS_IMETHODIMP
157 nsNSSDialogs::SetPKCS12FilePassword(nsIInterfaceRequestor* ctx,
158 /*out*/ nsAString& password,
159 /*out*/ bool* confirmedPassword) {
160 // |ctx| is allowed to be null.
161 NS_ENSURE_ARG(confirmedPassword);
163 // Get the parent window for the dialog
164 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
165 nsCOMPtr<nsIWritablePropertyBag2> retVals = new nsHashPropertyBag();
166 nsresult rv = nsNSSDialogHelper::openDialog(
167 parent, "chrome://pippki/content/setp12password.xhtml", retVals);
168 if (NS_FAILED(rv)) {
169 return rv;
172 rv = retVals->GetPropertyAsBool(u"confirmedPassword"_ns, confirmedPassword);
173 if (NS_FAILED(rv)) {
174 return rv;
177 if (!*confirmedPassword) {
178 return NS_OK;
181 return retVals->GetPropertyAsAString(u"password"_ns, password);
184 NS_IMETHODIMP
185 nsNSSDialogs::GetPKCS12FilePassword(nsIInterfaceRequestor* ctx,
186 nsAString& _password, bool* _retval) {
187 *_retval = false;
189 nsCOMPtr<nsIPromptService> promptSvc(
190 do_GetService(NS_PROMPTSERVICE_CONTRACTID));
191 if (!promptSvc) {
192 return NS_ERROR_FAILURE;
195 nsAutoString msg;
196 nsresult rv =
197 mPIPStringBundle->GetStringFromName("getPKCS12FilePasswordMessage", msg);
198 if (NS_FAILED(rv)) {
199 return rv;
202 // Get the parent window for the dialog
203 nsCOMPtr<mozIDOMWindowProxy> parent = do_GetInterface(ctx);
204 char16_t* pwTemp = nullptr;
205 rv = promptSvc->PromptPassword(parent, nullptr, msg.get(), &pwTemp, _retval);
206 if (NS_FAILED(rv)) {
207 return rv;
210 if (*_retval) {
211 _password.Assign(pwTemp);
212 free(pwTemp);
215 return NS_OK;