Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / certificate_viewer_win.cc
blob39843f975f2747f0361a58d3c3e798904c4b9f19
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.
5 #include "chrome/browser/certificate_viewer.h"
7 #include <windows.h>
8 #include <cryptuiapi.h>
9 #pragma comment(lib, "cryptui.lib")
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/task_runner.h"
17 #include "base/threading/thread.h"
18 #include "chrome/browser/ui/host_desktop.h"
19 #include "net/cert/x509_certificate.h"
20 #include "ui/aura/window.h"
21 #include "ui/aura/window_tree_host.h"
22 #include "ui/shell_dialogs/base_shell_dialog_win.h"
24 namespace {
26 // Shows a Windows certificate viewer dialog on a background thread to avoid
27 // nested message loops.
28 class CertificateViewerDialog : public ui::BaseShellDialogImpl {
29 public:
30 CertificateViewerDialog() {}
32 // Shows the dialog and calls |callback| when the dialog closes. The caller
33 // must ensure the CertificateViewerDialog remains valid until then.
34 void Show(HWND parent,
35 net::X509Certificate* cert,
36 const base::Closure& callback) {
37 if (IsRunningDialogForOwner(parent)) {
38 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
39 return;
42 RunState run_state = BeginRun(parent);
43 run_state.dialog_thread->task_runner()->PostTaskAndReply(
44 FROM_HERE,
45 base::Bind(&CertificateViewerDialog::ShowOnDialogThread,
46 base::Unretained(this), run_state, make_scoped_refptr(cert)),
47 base::Bind(&CertificateViewerDialog::OnDialogClosed,
48 base::Unretained(this), run_state, callback));
51 private:
52 void ShowOnDialogThread(const RunState& run_state,
53 const scoped_refptr<net::X509Certificate>& cert) {
54 // Create a new cert context and store containing just the certificate
55 // and its intermediate certificates.
56 PCCERT_CONTEXT cert_list = cert->CreateOSCertChainForCert();
57 CHECK(cert_list);
59 CRYPTUI_VIEWCERTIFICATE_STRUCT view_info = {0};
60 view_info.dwSize = sizeof(view_info);
61 view_info.hwndParent = run_state.owner;
62 view_info.dwFlags =
63 CRYPTUI_DISABLE_EDITPROPERTIES | CRYPTUI_DISABLE_ADDTOSTORE;
64 view_info.pCertContext = cert_list;
65 HCERTSTORE cert_store = cert_list->hCertStore;
66 view_info.cStores = 1;
67 view_info.rghStores = &cert_store;
69 BOOL properties_changed;
70 ::CryptUIDlgViewCertificate(&view_info, &properties_changed);
72 CertFreeCertificateContext(cert_list);
75 void OnDialogClosed(const RunState& run_state,
76 const base::Closure& callback) {
77 EndRun(run_state);
78 // May delete |this|.
79 callback.Run();
82 DISALLOW_COPY_AND_ASSIGN(CertificateViewerDialog);
85 } // namespace
87 void ShowCertificateViewer(content::WebContents* web_contents,
88 gfx::NativeWindow parent,
89 net::X509Certificate* cert) {
90 if (chrome::GetHostDesktopTypeForNativeWindow(parent) !=
91 chrome::HOST_DESKTOP_TYPE_ASH) {
92 CertificateViewerDialog* dialog = new CertificateViewerDialog;
93 dialog->Show(
94 parent->GetHost()->GetAcceleratedWidget(), cert,
95 base::Bind(&base::DeletePointer<CertificateViewerDialog>, dialog));
96 } else {
97 NOTIMPLEMENTED();