1 // Copyright 2014 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/signed_certificate_timestamps_views.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/ui/views/signed_certificate_timestamp_info_view.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "components/constrained_window/constrained_window_views.h"
14 #include "content/public/browser/notification_source.h"
15 #include "content/public/browser/signed_certificate_timestamp_store.h"
16 #include "content/public/common/signed_certificate_timestamp_id_and_status.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/models/combobox_model.h"
19 #include "ui/views/controls/combobox/combobox.h"
20 #include "ui/views/layout/grid_layout.h"
21 #include "ui/views/layout/layout_constants.h"
22 #include "ui/views/widget/widget.h"
26 void SignedCertificateTimestampIDsToList(
27 const content::SignedCertificateTimestampIDStatusList
& sct_ids_list
,
28 net::SignedCertificateTimestampAndStatusList
* sct_list
) {
29 for (content::SignedCertificateTimestampIDStatusList::const_iterator it
=
31 it
!= sct_ids_list
.end();
33 scoped_refptr
<net::ct::SignedCertificateTimestamp
> sct
;
34 content::SignedCertificateTimestampStore::GetInstance()->Retrieve(it
->id
,
37 net::SignedCertificateTimestampAndStatus(sct
, it
->status
));
45 void ShowSignedCertificateTimestampsViewer(
46 content::WebContents
* web_contents
,
47 const content::SignedCertificateTimestampIDStatusList
& sct_ids_list
) {
48 net::SignedCertificateTimestampAndStatusList sct_list
;
49 SignedCertificateTimestampIDsToList(sct_ids_list
, &sct_list
);
50 new SignedCertificateTimestampsViews(web_contents
, sct_list
);
55 class SCTListModel
: public ui::ComboboxModel
{
57 explicit SCTListModel(
58 const net::SignedCertificateTimestampAndStatusList
& sct_list
);
59 ~SCTListModel() override
;
61 // Overridden from ui::ComboboxModel:
62 int GetItemCount() const override
;
63 base::string16
GetItemAt(int index
) override
;
66 net::SignedCertificateTimestampAndStatusList sct_list_
;
68 DISALLOW_COPY_AND_ASSIGN(SCTListModel
);
71 SCTListModel::SCTListModel(
72 const net::SignedCertificateTimestampAndStatusList
& sct_list
)
73 : sct_list_(sct_list
) {}
75 SCTListModel::~SCTListModel() {}
77 int SCTListModel::GetItemCount() const { return sct_list_
.size(); }
79 base::string16
SCTListModel::GetItemAt(int index
) {
80 DCHECK_LT(static_cast<size_t>(index
), sct_list_
.size());
81 std::string origin
= l10n_util::GetStringUTF8(
82 chrome::ct::SCTOriginToResourceID(*(sct_list_
[index
].sct
.get())));
84 std::string status
= l10n_util::GetStringUTF8(
85 chrome::ct::StatusToResourceID(sct_list_
[index
].status
));
87 // This formatting string may be internationalized for RTL, etc.
88 return l10n_util::GetStringFUTF16(IDS_SCT_CHOOSER_FORMAT
,
89 base::IntToString16(index
+ 1),
90 base::UTF8ToUTF16(origin
),
91 base::UTF8ToUTF16(status
));
94 SignedCertificateTimestampsViews::SignedCertificateTimestampsViews(
95 content::WebContents
* web_contents
,
96 const net::SignedCertificateTimestampAndStatusList
& sct_list
)
97 : sct_info_view_(NULL
),
99 constrained_window::ShowWebModalDialogViews(this, web_contents
);
102 SignedCertificateTimestampsViews::~SignedCertificateTimestampsViews() {}
104 base::string16
SignedCertificateTimestampsViews::GetWindowTitle() const {
105 return l10n_util::GetStringUTF16(IDS_SCT_VIEWER_TITLE
);
108 int SignedCertificateTimestampsViews::GetDialogButtons() const {
109 return ui::DIALOG_BUTTON_CANCEL
;
112 ui::ModalType
SignedCertificateTimestampsViews::GetModalType() const {
113 return ui::MODAL_TYPE_CHILD
;
116 void SignedCertificateTimestampsViews::OnPerformAction(
117 views::Combobox
* combobox
) {
118 DCHECK_EQ(combobox
, sct_selector_box_
.get());
119 DCHECK_LT(combobox
->selected_index(), sct_list_model_
->GetItemCount());
120 ShowSCTInfo(combobox
->selected_index());
123 void SignedCertificateTimestampsViews::ViewHierarchyChanged(
124 const ViewHierarchyChangedDetails
& details
) {
125 views::DialogDelegateView::ViewHierarchyChanged(details
);
126 if (details
.is_add
&& details
.child
== this)
130 void SignedCertificateTimestampsViews::Init() {
131 views::GridLayout
* layout
= views::GridLayout::CreatePanel(this);
132 SetLayoutManager(layout
);
134 const int kSelectorBoxLayoutId
= 0;
135 views::ColumnSet
* column_set
= layout
->AddColumnSet(kSelectorBoxLayoutId
);
136 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
, 1,
137 views::GridLayout::USE_PREF
, 0, 0);
139 layout
->StartRow(0, kSelectorBoxLayoutId
);
140 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
142 // Add SCT selector drop-down list.
143 layout
->StartRow(0, kSelectorBoxLayoutId
);
144 sct_list_model_
.reset(new SCTListModel(sct_list_
));
145 sct_selector_box_
.reset(new views::Combobox(sct_list_model_
.get()));
146 sct_selector_box_
->set_listener(this);
147 sct_selector_box_
->set_owned_by_client();
148 layout
->AddView(sct_selector_box_
.get());
149 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
151 // Add the SCT info view, displaying information about a specific SCT.
152 layout
->StartRow(0, kSelectorBoxLayoutId
);
153 sct_info_view_
= new SignedCertificateTimestampInfoView();
154 layout
->AddView(sct_info_view_
);
156 sct_info_view_
->SetSignedCertificateTimestamp(*(sct_list_
[0].sct
.get()),
157 sct_list_
[0].status
);
160 void SignedCertificateTimestampsViews::ShowSCTInfo(int sct_index
) {
161 if ((sct_index
< 0) || (static_cast<size_t>(sct_index
) > sct_list_
.size()))
164 sct_info_view_
->SetSignedCertificateTimestamp(
165 *(sct_list_
[sct_index
].sct
.get()), sct_list_
[sct_index
].status
);
168 void SignedCertificateTimestampsViews::Observe(
170 const content::NotificationSource
& source
,
171 const content::NotificationDetails
& details
) {
172 GetWidget()->Close();