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/. */
8 * Dialog services for PIP.
11 #include "nsNSSDialogs.h"
13 #include "mozIDOMWindow.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"
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() {
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
));
53 nsNSSDialogs::SetPassword(nsIInterfaceRequestor
* ctx
, nsIPK11Token
* token
,
54 /*out*/ bool* canceled
) {
55 // |ctx| is allowed to be null.
56 NS_ENSURE_ARG(canceled
);
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();
69 return NS_ERROR_FAILURE
;
71 nsresult rv
= objects
->AppendElement(token
);
75 rv
= block
->SetObjects(objects
);
80 rv
= nsNSSDialogHelper::openDialog(
81 parent
, "chrome://pippki/content/changepassword.xhtml", block
);
83 if (NS_FAILED(rv
)) return rv
;
87 rv
= block
->GetInt(1, &status
);
88 if (NS_FAILED(rv
)) return rv
;
90 *canceled
= (status
== 0);
96 nsNSSDialogs::ConfirmDownloadCACert(nsIInterfaceRequestor
* ctx
,
98 /*out*/ uint32_t* trust
,
99 /*out*/ bool* importConfirmed
) {
100 // |ctx| is allowed to be null.
102 NS_ENSURE_ARG(trust
);
103 NS_ENSURE_ARG(importConfirmed
);
105 nsCOMPtr
<nsIMutableArray
> argArray
= nsArrayBase::Create();
107 return NS_ERROR_FAILURE
;
110 nsresult rv
= argArray
->AppendElement(cert
);
115 nsCOMPtr
<nsIWritablePropertyBag2
> retVals
= new nsHashPropertyBag();
116 rv
= argArray
->AppendElement(retVals
);
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
);
129 rv
= retVals
->GetPropertyAsBool(u
"importConfirmed"_ns
, importConfirmed
);
134 *trust
= nsIX509CertDB::UNTRUSTED
;
135 if (!*importConfirmed
) {
139 bool trustForSSL
= false;
140 rv
= retVals
->GetPropertyAsBool(u
"trustForSSL"_ns
, &trustForSSL
);
144 bool trustForEmail
= false;
145 rv
= retVals
->GetPropertyAsBool(u
"trustForEmail"_ns
, &trustForEmail
);
150 *trust
|= trustForSSL
? nsIX509CertDB::TRUSTED_SSL
: 0;
151 *trust
|= trustForEmail
? nsIX509CertDB::TRUSTED_EMAIL
: 0;
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
);
172 rv
= retVals
->GetPropertyAsBool(u
"confirmedPassword"_ns
, confirmedPassword
);
177 if (!*confirmedPassword
) {
181 return retVals
->GetPropertyAsAString(u
"password"_ns
, password
);
185 nsNSSDialogs::GetPKCS12FilePassword(nsIInterfaceRequestor
* ctx
,
186 nsAString
& _password
, bool* _retval
) {
189 nsCOMPtr
<nsIPromptService
> promptSvc(
190 do_GetService(NS_PROMPTSERVICE_CONTRACTID
));
192 return NS_ERROR_FAILURE
;
197 mPIPStringBundle
->GetStringFromName("getPKCS12FilePasswordMessage", msg
);
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
);
211 _password
.Assign(pwTemp
);