From 7658cbf8e15ddb2c055b4da2011327ec67cac953 Mon Sep 17 00:00:00 2001 From: Cykesiopka Date: Fri, 21 Oct 2016 00:33:36 +0800 Subject: [PATCH] Bug 1308888 - Simplify passing handle to the cert to view in the cert viewer. r=keeler The cert viewer currently supports two ways to pass a handle to the cert: 1. Passing the nickname of the cert via window.name. 2. Via an nsIDialogParamBlock, which is itself accessed through window.arguments. Method 1 is unused and unnecessary. Method 2 is overly complex: the relevant nsIX509Cert can just be passed directly. This patch does the following: 1. Makes it so that there is only a single, straightforward way to pass a handle to the cert. 2. Makes the cert viewer title localisable while we're nearby. 3. Renames viewCertDetails.js to better reflect the current use of the file. MozReview-Commit-ID: pqtfNgvImT --HG-- rename : security/manager/pki/resources/content/viewCertDetails.js => security/manager/pki/resources/content/certViewer.js extra : rebase_source : 776a27111ab26cdcdc91b002890c43a3fe4f48e8 --- .../en-US/chrome/pippki/pippki.properties | 4 +- security/manager/pki/nsNSSDialogHelper.h | 23 ++++++++--- security/manager/pki/nsNSSDialogs.cpp | 38 ++++--------------- .../{viewCertDetails.js => certViewer.js} | 25 ++++++------ .../pki/resources/content/certViewer.xul | 3 +- security/manager/pki/resources/jar.mn | 2 +- .../mochitest/browser/browser_certViewer.js | 36 +++++++++--------- 7 files changed, 60 insertions(+), 71 deletions(-) rename security/manager/pki/resources/content/{viewCertDetails.js => certViewer.js} (95%) diff --git a/security/manager/locales/en-US/chrome/pippki/pippki.properties b/security/manager/locales/en-US/chrome/pippki/pippki.properties index fe7bc2e0797e..056f609af1fd 100644 --- a/security/manager/locales/en-US/chrome/pippki/pippki.properties +++ b/security/manager/locales/en-US/chrome/pippki/pippki.properties @@ -111,7 +111,9 @@ pageInfo_MixedContent2=Parts of the page you are viewing were not encrypted befo pageInfo_WeakCipher=Your connection to this website uses weak encryption and is not private. Other people can view your information or modify the website’s behavior. # Cert Viewer -certDetails=Certificate Viewer: +# LOCALIZATION NOTE(certViewerTitle): Title used for the Certificate Viewer. +# %1$S is a string representative of the certificate being viewed. +certViewerTitle=Certificate Viewer: “%1$S” notPresent= # Token Manager diff --git a/security/manager/pki/nsNSSDialogHelper.h b/security/manager/pki/nsNSSDialogHelper.h index d35646ee9793..87605da5f1d3 100644 --- a/security/manager/pki/nsNSSDialogHelper.h +++ b/security/manager/pki/nsNSSDialogHelper.h @@ -11,17 +11,28 @@ class mozIDOMWindowProxy; class nsISupports; /** - * Common class that uses the window watcher service to open a - * standard dialog, with or without a parent context. The params - * parameter can be an nsISupportsArray so any number of additional - * arguments can be used. + * Helper class that uses the window watcher service to open a standard dialog, + * with or without a parent context. */ class nsNSSDialogHelper { public: - // params is a nsIDialogParamBlock or a nsIKeygenThread + /** + * Opens a XUL dialog. + * + * @param window + * Parent window of the dialog, or nullptr to signal no parent. + * @param url + * URL to the XUL dialog. + * @param params + * Parameters to pass to the dialog. Same semantics as the + * nsIWindowWatcher.openWindow() |aArguments| parameter. + * @param modal + * true if the dialog should be modal, false otherwise. + * @return The result of opening the dialog. + */ static nsresult openDialog(mozIDOMWindowProxy* window, const char* url, nsISupports* params, bool modal = true); }; -#endif +#endif // nsNSSDialogHelper_h diff --git a/security/manager/pki/nsNSSDialogs.cpp b/security/manager/pki/nsNSSDialogs.cpp index fa9bc41e3707..1e80cc4d285c 100644 --- a/security/manager/pki/nsNSSDialogs.cpp +++ b/security/manager/pki/nsNSSDialogs.cpp @@ -7,35 +7,28 @@ /* * Dialog services for PIP. */ + +#include "nsNSSDialogs.h" + #include "mozIDOMWindow.h" #include "mozilla/Assertions.h" #include "mozilla/Casting.h" #include "nsArray.h" -#include "nsDateTimeFormatCID.h" #include "nsEmbedCID.h" -#include "nsIComponentManager.h" -#include "nsIDateTimeFormat.h" #include "nsIDialogParamBlock.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" #include "nsIKeygenThread.h" #include "nsIPromptService.h" #include "nsIProtectedAuthThread.h" -#include "nsIServiceManager.h" #include "nsIWindowWatcher.h" #include "nsIX509CertDB.h" #include "nsIX509Cert.h" -#include "nsIX509CertValidity.h" #include "nsNSSDialogHelper.h" -#include "nsNSSDialogs.h" -#include "nsPromiseFlatString.h" -#include "nsReadableUtils.h" #include "nsString.h" #define PIPSTRING_BUNDLE_URL "chrome://pippki/locale/pippki.properties" -/* ==== */ - nsNSSDialogs::nsNSSDialogs() { } @@ -329,33 +322,18 @@ nsNSSDialogs::GetPKCS12FilePassword(nsIInterfaceRequestor* ctx, return NS_OK; } -NS_IMETHODIMP +NS_IMETHODIMP nsNSSDialogs::ViewCert(nsIInterfaceRequestor* ctx, nsIX509Cert* cert) { - nsCOMPtr dlgArray = nsArrayBase::Create(); - if (!dlgArray) { - return NS_ERROR_FAILURE; - } - nsresult rv = dlgArray->AppendElement(cert, false); - if (NS_FAILED(rv)) { - return rv; - } - nsCOMPtr dlgParamBlock( - do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID)); - if (!dlgParamBlock) { - return NS_ERROR_FAILURE; - } - rv = dlgParamBlock->SetObjects(dlgArray); - if (NS_FAILED(rv)) { - return rv; - } + // |ctx| is allowed to be null. + NS_ENSURE_ARG(cert); // Get the parent window for the dialog nsCOMPtr parent = do_GetInterface(ctx); return nsNSSDialogHelper::openDialog(parent, "chrome://pippki/content/certViewer.xul", - dlgParamBlock, - false); + cert, + false /*modal*/); } NS_IMETHODIMP diff --git a/security/manager/pki/resources/content/viewCertDetails.js b/security/manager/pki/resources/content/certViewer.js similarity index 95% rename from security/manager/pki/resources/content/viewCertDetails.js rename to security/manager/pki/resources/content/certViewer.js index 5a5650a1ebd9..c625076943cf 100644 --- a/security/manager/pki/resources/content/viewCertDetails.js +++ b/security/manager/pki/resources/content/certViewer.js @@ -3,6 +3,14 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; +/** + * @file Implements functionality for certViewer.xul and its tabs certDump.xul + * and viewCertDetails.xul: a dialog that allows various attributes of a + * certificate to be viewed. + * @argument {nsISupports} window.arguments[0] + * The cert to view, queryable to nsIX509Cert. + */ + const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; const { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); @@ -16,7 +24,6 @@ const nsIASN1Sequence = Ci.nsIASN1Sequence; const nsIASN1PrintableItem = Ci.nsIASN1PrintableItem; const nsIASN1Tree = Ci.nsIASN1Tree; const nsASN1Tree = "@mozilla.org/security/nsASN1Tree;1"; -const nsIDialogParamBlock = Ci.nsIDialogParamBlock; var bundle; @@ -71,21 +78,11 @@ function AddUsage(usage) function setWindowName() { - // Get the cert from the cert database - var certdb = Components.classes[nsX509CertDB].getService(nsIX509CertDB); - var myName = self.name; bundle = document.getElementById("pippki_bundle"); - var cert; - var certDetails = bundle.getString('certDetails'); - if (myName != "") { - document.title = certDetails + '"' + myName + '"'; // XXX l10n? - cert = certdb.findCertByNickname(myName); - } else { - var params = window.arguments[0].QueryInterface(nsIDialogParamBlock); - cert = params.objects.queryElementAt(0, nsIX509Cert); - document.title = certDetails + '"' + cert.windowTitle + '"'; // XXX l10n? - } + let cert = window.arguments[0].QueryInterface(Ci.nsIX509Cert); + document.title = bundle.getFormattedString("certViewerTitle", + [cert.windowTitle]); // // Set the cert attributes for viewing diff --git a/security/manager/pki/resources/content/certViewer.xul b/security/manager/pki/resources/content/certViewer.xul index e9a1e2b77fcc..3f2cd3bde04f 100644 --- a/security/manager/pki/resources/content/certViewer.xul +++ b/security/manager/pki/resources/content/certViewer.xul @@ -20,7 +20,8 @@ -