• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_WEBUI_H_
6 #define CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_WEBUI_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/values.h"
13 #include "components/web_modal/native_web_contents_modal_dialog.h"
14 #include "content/public/browser/web_ui_message_handler.h"
15 #include "net/cert/x509_certificate.h"
16 #include "ui/gfx/native_widget_types.h"
17 #include "ui/web_dialogs/web_dialog_delegate.h"
18 
19 namespace content {
20 class WebContents;
21 }
22 
23 class ConstrainedWebDialogDelegate;
24 
25 // Modal dialog for displaying detailed certificate information. This is used in
26 // chromeos builds to display detailed information in a modal dialog when the
27 // user clicks on "View" from the Certificate Manager dialog.
28 class CertificateViewerModalDialog : public ui::WebDialogDelegate {
29  public:
30   // Construct a certificate viewer for the passed in certificate. A reference
31   // to the certificate pointer is added for the lifetime of the certificate
32   // viewer.
33   explicit CertificateViewerModalDialog(
34       net::X509Certificate* cert);
35   virtual ~CertificateViewerModalDialog();
36 
37   virtual void Show(content::WebContents* web_contents,
38                     gfx::NativeWindow parent);
39   virtual web_modal::NativeWebContentsModalDialog
40   GetNativeWebContentsModalDialog();
GetWebUI()41   const content::WebUI* GetWebUI() const { return webui_; }
42 
43  protected:
44   // Overridden from ui::WebDialogDelegate:
45   virtual ui::ModalType GetDialogModalType() const OVERRIDE;
46   virtual base::string16 GetDialogTitle() const OVERRIDE;
47   virtual GURL GetDialogContentURL() const OVERRIDE;
48   virtual void GetWebUIMessageHandlers(
49       std::vector<content::WebUIMessageHandler*>* handlers) const OVERRIDE;
50   virtual void GetDialogSize(gfx::Size* size) const OVERRIDE;
51   virtual std::string GetDialogArgs() const OVERRIDE;
52   virtual void OnDialogShown(
53       content::WebUI* webui,
54       content::RenderViewHost* render_view_host) OVERRIDE;
55   virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE;
56   virtual void OnCloseContents(
57       content::WebContents* source, bool* out_close_dialog) OVERRIDE;
58   virtual bool ShouldShowDialogTitle() const OVERRIDE;
59 
60   // The certificate being viewed.
61   scoped_refptr<net::X509Certificate> cert_;
62 
63   // The title of the certificate viewer dialog, Certificate Viewer: CN.
64   base::string16 title_;
65 
66  private:
67   content::WebUI* webui_;
68   gfx::NativeWindow window_;
69   DISALLOW_COPY_AND_ASSIGN(CertificateViewerModalDialog);
70 };
71 
72 // Dialog for displaying detailed certificate information. This is used in linux
73 // and chromeos builds to display detailed information in a floating dialog when
74 // the user clicks on "Certificate Information" from the lock icon of a web site
75 // or "View" from the Certificate Manager.
76 class CertificateViewerDialog : public CertificateViewerModalDialog {
77  public:
78   // Construct a certificate viewer for the passed in certificate. A reference
79   // to the certificate pointer is added for the lifetime of the certificate
80   // viewer.
81   explicit CertificateViewerDialog(net::X509Certificate* cert);
82   virtual ~CertificateViewerDialog();
83 
84   // CertificateViewerModalDialog overrides.
85   virtual void Show(content::WebContents* web_contents,
86                     gfx::NativeWindow parent) OVERRIDE;
87   virtual web_modal::NativeWebContentsModalDialog
88   GetNativeWebContentsModalDialog() OVERRIDE;
89 
90  protected:
91   // Overridden from ui::WebDialogDelegate:
92   virtual GURL GetDialogContentURL() const OVERRIDE;
93   virtual ui::ModalType GetDialogModalType() const OVERRIDE;
94 
95  private:
96   ConstrainedWebDialogDelegate* dialog_;
97 
98   DISALLOW_COPY_AND_ASSIGN(CertificateViewerDialog);
99 };
100 
101 // Dialog handler which handles calls from the JS WebUI code to view certificate
102 // details and export the certificate.
103 class CertificateViewerDialogHandler : public content::WebUIMessageHandler {
104  public:
105   CertificateViewerDialogHandler(CertificateViewerModalDialog* dialog,
106                                  net::X509Certificate* cert);
107   virtual ~CertificateViewerDialogHandler();
108 
109   // Overridden from WebUIMessageHandler
110   virtual void RegisterMessages() OVERRIDE;
111 
112  private:
113   // Brings up the export certificate dialog for the chosen certificate in the
114   // chain.
115   //
116   // The input is an integer index to the certificate in the chain to export.
117   void ExportCertificate(const base::ListValue* args);
118 
119   // Gets the details for a specific certificate in the certificate chain. Calls
120   // the javascript function cert_viewer.getCertificateFields with a tree
121   // structure containing the fields and values for certain nodes.
122   //
123   // The input is an integer index to the certificate in the chain to view.
124   void RequestCertificateFields(const base::ListValue* args);
125 
126   // Helper function to get the certificate index from |args|. Returns -1 if
127   // the index is out of range.
128   int GetCertificateIndex(const base::ListValue* args) const;
129 
130   // The certificate being viewed.
131   scoped_refptr<net::X509Certificate> cert_;
132 
133   // The dialog.
134   CertificateViewerModalDialog* dialog_;
135 
136   // The certificate chain. Does not take references, so only valid as long as
137   // |cert_| is.
138   net::X509Certificate::OSCertHandles cert_chain_;
139 
140   DISALLOW_COPY_AND_ASSIGN(CertificateViewerDialogHandler);
141 };
142 
143 #endif  // CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_WEBUI_H_
144