Infobar material design refresh: layout
[chromium-blink-merge.git] / chrome / browser / ui / views / platform_keys_certificate_selector_chromeos.cc
blob4719d975a570f40a1e10f67d44ceadf9b6c4f076
1 // Copyright 2015 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/ui/views/platform_keys_certificate_selector_chromeos.h"
7 #include "base/callback_helpers.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/gfx/font.h"
14 #include "ui/views/controls/styled_label.h"
16 namespace chromeos {
18 PlatformKeysCertificateSelector::PlatformKeysCertificateSelector(
19 const net::CertificateList& certificates,
20 const std::string& extension_name,
21 const CertificateSelectedCallback& callback,
22 content::WebContents* web_contents)
23 : CertificateSelector(certificates, web_contents),
24 extension_name_(extension_name),
25 callback_(callback) {
26 DCHECK(!callback_.is_null());
29 PlatformKeysCertificateSelector::~PlatformKeysCertificateSelector() {
30 // Ensure to call back even if the dialog was closed because of the views
31 // hierarchy being destroyed.
32 if (!callback_.is_null())
33 base::ResetAndReturn(&callback_).Run(nullptr);
36 void PlatformKeysCertificateSelector::Init() {
37 const base::string16 name = base::ASCIIToUTF16(extension_name_);
39 size_t offset;
40 const base::string16 text = l10n_util::GetStringFUTF16(
41 IDS_PLATFORM_KEYS_SELECT_CERT_DIALOG_TEXT, name, &offset);
43 scoped_ptr<views::StyledLabel> label(
44 new views::StyledLabel(text, nullptr /* no listener */));
46 views::StyledLabel::RangeStyleInfo bold_style;
47 bold_style.font_style = gfx::Font::BOLD;
48 label->AddStyleRange(gfx::Range(offset, offset + name.size()), bold_style);
49 CertificateSelector::InitWithText(label.Pass());
52 bool PlatformKeysCertificateSelector::Cancel() {
53 DCHECK(!callback_.is_null());
54 base::ResetAndReturn(&callback_).Run(nullptr);
55 return true;
58 bool PlatformKeysCertificateSelector::Accept() {
59 DCHECK(!callback_.is_null());
60 scoped_refptr<net::X509Certificate> cert = GetSelectedCert();
61 if (!cert)
62 return false;
63 base::ResetAndReturn(&callback_).Run(cert);
64 return true;
67 void ShowPlatformKeysCertificateSelector(
68 content::WebContents* web_contents,
69 const std::string& extension_name,
70 const net::CertificateList& certificates,
71 const base::Callback<void(const scoped_refptr<net::X509Certificate>&)>&
72 callback) {
73 PlatformKeysCertificateSelector* selector =
74 new PlatformKeysCertificateSelector(certificates, extension_name,
75 callback, web_contents);
76 selector->Init();
77 selector->Show();
80 } // namespace chromeos