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"
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
),
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_
);
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);
58 bool PlatformKeysCertificateSelector::Accept() {
59 DCHECK(!callback_
.is_null());
60 scoped_refptr
<net::X509Certificate
> cert
= GetSelectedCert();
63 base::ResetAndReturn(&callback_
).Run(cert
);
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
>&)>&
73 PlatformKeysCertificateSelector
* selector
=
74 new PlatformKeysCertificateSelector(certificates
, extension_name
,
75 callback
, web_contents
);
80 } // namespace chromeos