certificate_viewer_win.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
1// Copyright (c) 2011 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#include "chrome/browser/certificate_viewer.h"
6
7#include <windows.h>
8#include <cryptuiapi.h>
9#pragma comment(lib, "cryptui.lib")
10
11#include "base/logging.h"
12#include "net/cert/x509_certificate.h"
13
14#if defined(USE_AURA)
15#include "chrome/browser/ui/host_desktop.h"
16#include "ui/aura/root_window.h"
17#include "ui/aura/window.h"
18#endif
19
20namespace {
21
22void ShowCertificateViewerImpl(content::WebContents* web_contents,
23                               HWND parent,
24                               net::X509Certificate* cert) {
25  // Create a new cert context and store containing just the certificate
26  // and its intermediate certificates.
27  PCCERT_CONTEXT cert_list = cert->CreateOSCertChainForCert();
28  CHECK(cert_list);
29
30  CRYPTUI_VIEWCERTIFICATE_STRUCT view_info = { 0 };
31  view_info.dwSize = sizeof(view_info);
32  // We set our parent to the tab window. This makes the cert dialog created
33  // in CryptUIDlgViewCertificate modal to the browser.
34  view_info.hwndParent = parent;
35  view_info.dwFlags = CRYPTUI_DISABLE_EDITPROPERTIES |
36                      CRYPTUI_DISABLE_ADDTOSTORE;
37  view_info.pCertContext = cert_list;
38  HCERTSTORE cert_store = cert_list->hCertStore;
39  view_info.cStores = 1;
40  view_info.rghStores = &cert_store;
41  BOOL properties_changed;
42
43  // This next call blocks but keeps processing windows messages, making it
44  // modal to the browser window.
45  BOOL rv = ::CryptUIDlgViewCertificate(&view_info, &properties_changed);
46
47  CertFreeCertificateContext(cert_list);
48}
49
50}  // namespace
51
52#if defined(USE_AURA)
53void ShowCertificateViewer(content::WebContents* web_contents,
54                           gfx::NativeWindow parent,
55                           net::X509Certificate* cert) {
56  if (chrome::GetHostDesktopTypeForNativeWindow(parent) !=
57      chrome::HOST_DESKTOP_TYPE_ASH) {
58    ShowCertificateViewerImpl(
59        web_contents, parent->GetDispatcher()->GetAcceleratedWidget(), cert);
60  } else {
61    NOTIMPLEMENTED();
62  }
63}
64#else
65void ShowCertificateViewer(content::WebContents* web_contents,
66                           gfx::NativeWindow parent,
67                           net::X509Certificate* cert) {
68  ShowCertificateViewerImpl(web_contents, parent, cert);
69}
70#endif
71