File: certificate_viewer_win.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (99 lines) | stat: -rw-r--r-- 3,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/certificate_viewer.h"

#include <windows.h>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/task_runner.h"
#include "base/threading/thread.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/ui/cryptuiapi_shim.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util_win.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/shell_dialogs/base_shell_dialog_win.h"

namespace {

// Shows a Windows certificate viewer dialog on a background thread to avoid
// nested run loops.
class CertificateViewerDialog : public ui::BaseShellDialogImpl {
 public:
  CertificateViewerDialog() {}

  // Shows the dialog and calls |callback| when the dialog closes. The caller
  // must ensure the CertificateViewerDialog remains valid until then.
  void Show(HWND parent,
            net::X509Certificate* cert,
            const base::Closure& callback) {
    if (IsRunningDialogForOwner(parent)) {
      base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
      return;
    }

    RunState run_state = BeginRun(parent);
    run_state.dialog_thread->task_runner()->PostTaskAndReply(
        FROM_HERE,
        base::Bind(&CertificateViewerDialog::ShowOnDialogThread,
                   base::Unretained(this), run_state,
                   base::WrapRefCounted(cert)),
        base::Bind(&CertificateViewerDialog::OnDialogClosed,
                   base::Unretained(this), run_state, callback));
  }

 private:
  void ShowOnDialogThread(const RunState& run_state,
                          const scoped_refptr<net::X509Certificate>& cert) {
    // Create a new cert context and store containing just the certificate
    // and its intermediate certificates.
    net::ScopedPCCERT_CONTEXT cert_list(
        net::x509_util::CreateCertContextWithChain(cert.get()));
    // Perhaps this should show an error instead of silently failing, but it's
    // probably not even possible to get here with a cert that can't be
    // converted to a CERT_CONTEXT.
    if (!cert_list)
      return;

    CRYPTUI_VIEWCERTIFICATE_STRUCT view_info = {0};
    view_info.dwSize = sizeof(view_info);
    view_info.hwndParent = run_state.owner;
    view_info.dwFlags =
        CRYPTUI_DISABLE_EDITPROPERTIES | CRYPTUI_DISABLE_ADDTOSTORE;
    view_info.pCertContext = cert_list.get();
    HCERTSTORE cert_store = cert_list->hCertStore;
    view_info.cStores = 1;
    view_info.rghStores = &cert_store;

    BOOL properties_changed;
    ::CryptUIDlgViewCertificate(&view_info, &properties_changed);
  }

  void OnDialogClosed(const RunState& run_state,
                      const base::Closure& callback) {
    EndRun(run_state);
    // May delete |this|.
    callback.Run();
  }

  DISALLOW_COPY_AND_ASSIGN(CertificateViewerDialog);
};

}  // namespace

void ShowCertificateViewer(content::WebContents* web_contents,
                           gfx::NativeWindow parent,
                           net::X509Certificate* cert) {
  CertificateViewerDialog* dialog = new CertificateViewerDialog;
  dialog->Show(
      parent->GetHost()->GetAcceleratedWidget(), cert,
      base::Bind(&base::DeletePointer<CertificateViewerDialog>, dialog));
}