certificate_viewer_win.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 "base/message_loop/message_loop.h"
13#include "net/cert/x509_certificate.h"
14
15#if defined(USE_AURA)
16#include "chrome/browser/ui/host_desktop.h"
17#include "ui/aura/window.h"
18#include "ui/aura/window_event_dispatcher.h"
19#endif
20
21namespace {
22
23void ShowCertificateViewerImpl(content::WebContents* web_contents,
24                               HWND parent,
25                               net::X509Certificate* cert) {
26  // Create a new cert context and store containing just the certificate
27  // and its intermediate certificates.
28  PCCERT_CONTEXT cert_list = cert->CreateOSCertChainForCert();
29  CHECK(cert_list);
30
31  CRYPTUI_VIEWCERTIFICATE_STRUCT view_info = { 0 };
32  view_info.dwSize = sizeof(view_info);
33  // We set our parent to the tab window. This makes the cert dialog created
34  // in CryptUIDlgViewCertificate modal to the browser.
35  view_info.hwndParent = parent;
36  view_info.dwFlags = CRYPTUI_DISABLE_EDITPROPERTIES |
37                      CRYPTUI_DISABLE_ADDTOSTORE;
38  view_info.pCertContext = cert_list;
39  HCERTSTORE cert_store = cert_list->hCertStore;
40  view_info.cStores = 1;
41  view_info.rghStores = &cert_store;
42  BOOL properties_changed;
43
44  // We must allow nested tasks to dispatch so that, e.g. gpu tasks are
45  // processed for painting. This allows a second window to continue painting
46  // while the the certificate dialog is open.
47  base::MessageLoop::ScopedNestableTaskAllower allow(
48      base::MessageLoop::current());
49  // This next call blocks but keeps processing windows messages, making it
50  // modal to the browser window.
51  BOOL rv = ::CryptUIDlgViewCertificate(&view_info, &properties_changed);
52
53  CertFreeCertificateContext(cert_list);
54}
55
56}  // namespace
57
58#if defined(USE_AURA)
59void ShowCertificateViewer(content::WebContents* web_contents,
60                           gfx::NativeWindow parent,
61                           net::X509Certificate* cert) {
62  if (chrome::GetHostDesktopTypeForNativeWindow(parent) !=
63      chrome::HOST_DESKTOP_TYPE_ASH) {
64    ShowCertificateViewerImpl(
65        web_contents,
66        parent->GetHost()->GetAcceleratedWidget(), cert);
67  } else {
68    NOTIMPLEMENTED();
69  }
70}
71#else
72void ShowCertificateViewer(content::WebContents* web_contents,
73                           gfx::NativeWindow parent,
74                           net::X509Certificate* cert) {
75  ShowCertificateViewerImpl(web_contents, parent, cert);
76}
77#endif
78