Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(954)

Side by Side Diff: chrome/browser/ui/views/certificate_selector.cc

Issue 932553002: Refactor SSLClientCertificateSelector for reuse with platformKeys API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cert_perms
Patch Set: Fixed comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/views/certificate_selector.h"
6
7 #include <stddef.h> // For size_t.
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/certificate_viewer.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/constrained_window/constrained_window_views.h"
16 #include "content/public/browser/web_contents.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/models/table_model.h"
19 #include "ui/base/models/table_model_observer.h"
20 #include "ui/views/controls/button/label_button.h"
21 #include "ui/views/controls/label.h"
22 #include "ui/views/controls/table/table_view.h"
23 #include "ui/views/layout/grid_layout.h"
24 #include "ui/views/layout/layout_constants.h"
25 #include "ui/views/widget/widget.h"
26 #include "ui/views/window/dialog_client_view.h"
27
28 namespace chrome {
29
30 class CertificateSelector::CertificateTableModel : public ui::TableModel {
31 public:
32 explicit CertificateTableModel(const net::CertificateList& certificates);
33
34 // ui::TableModel:
35 int RowCount() override;
36 base::string16 GetText(int index, int column_id) override;
37 void SetObserver(ui::TableModelObserver* observer) override;
38
39 private:
40 std::vector<base::string16> items_;
41
42 DISALLOW_COPY_AND_ASSIGN(CertificateTableModel);
43 };
44
45 CertificateSelector::CertificateTableModel::CertificateTableModel(
46 const net::CertificateList& certificates) {
47 for (const scoped_refptr<net::X509Certificate>& cert : certificates) {
48 items_.push_back(l10n_util::GetStringFUTF16(
49 IDS_CERT_SELECTOR_TABLE_CERT_FORMAT,
50 base::UTF8ToUTF16(cert->subject().GetDisplayName()),
51 base::UTF8ToUTF16(cert->issuer().GetDisplayName())));
52 }
53 }
54
55 int CertificateSelector::CertificateTableModel::RowCount() {
56 return items_.size();
57 }
58
59 base::string16 CertificateSelector::CertificateTableModel::GetText(
60 int index,
61 int column_id) {
62 DCHECK_EQ(column_id, 0);
63 DCHECK_GE(index, 0);
64 DCHECK_LT(static_cast<size_t>(index), items_.size());
65
66 return items_[index];
67 }
68
69 void CertificateSelector::CertificateTableModel::SetObserver(
70 ui::TableModelObserver* observer) {
71 }
72
73 CertificateSelector::CertificateSelector(
74 const net::CertificateList& certificates,
75 content::WebContents* web_contents)
76 : certificates_(certificates),
77 model_(new CertificateTableModel(certificates)),
78 web_contents_(web_contents),
79 table_(nullptr),
80 view_cert_button_(nullptr) {
81 CHECK(web_contents_);
82 }
83
84 CertificateSelector::~CertificateSelector() {
85 table_->SetModel(nullptr);
86 }
87
88 void CertificateSelector::Show() {
89 constrained_window::ShowWebModalDialogViews(this, web_contents_);
90
91 // Select the first row automatically. This must be done after the dialog has
92 // been created.
93 table_->Select(0);
94 }
95
96 void CertificateSelector::InitWithText(const base::string16& text) {
97 views::GridLayout* const layout = views::GridLayout::CreatePanel(this);
98 SetLayoutManager(layout);
99
100 const int kColumnSetId = 0;
101 views::ColumnSet* const column_set = layout->AddColumnSet(kColumnSetId);
102 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
103 views::GridLayout::USE_PREF, 0, 0);
104
105 layout->StartRow(0, kColumnSetId);
106 scoped_ptr<views::Label> label(new views::Label(text));
107 label->SetMultiLine(true);
108 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
109 label->SetAllowCharacterBreak(true);
110 layout->AddView(label.release());
111
112 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
113
114 // The dimensions of the certificate selector table view, in pixels.
115 const int kTableViewWidth = 400;
116 const int kTableViewHeight = 100;
117
118 std::vector<ui::TableColumn> columns;
119 columns.push_back(ui::TableColumn());
120 table_ = new views::TableView(model_.get(), columns, views::TEXT_ONLY,
121 true /* single_selection */);
122 table_->SetObserver(this);
123 layout->StartRow(1, kColumnSetId);
124 layout->AddView(table_->CreateParentIfNecessary(), 1, 1,
125 views::GridLayout::FILL, views::GridLayout::FILL,
126 kTableViewWidth, kTableViewHeight);
127
128 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
129 }
130
131 net::X509Certificate* CertificateSelector::GetSelectedCert() const {
132 const int selected = table_->FirstSelectedRow();
133 if (selected < 0) // Nothing is selected in |table_|.
134 return nullptr;
135 CHECK_LT(static_cast<size_t>(selected), certificates_.size());
136 return certificates_[selected].get();
137 }
138
139 bool CertificateSelector::CanResize() const {
140 return true;
141 }
142
143 base::string16 CertificateSelector::GetWindowTitle() const {
144 return l10n_util::GetStringUTF16(IDS_CLIENT_CERT_DIALOG_TITLE);
145 }
146
147 bool CertificateSelector::IsDialogButtonEnabled(ui::DialogButton button) const {
148 return button != ui::DIALOG_BUTTON_OK || GetSelectedCert() != nullptr;
149 }
150
151 views::View* CertificateSelector::GetInitiallyFocusedView() {
152 DCHECK(table_);
153 return table_;
154 }
155
156 views::View* CertificateSelector::CreateExtraView() {
157 DCHECK(!view_cert_button_);
158 scoped_ptr<views::LabelButton> button(new views::LabelButton(
159 this, l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON)));
160 button->SetStyle(views::Button::STYLE_BUTTON);
161 view_cert_button_ = button.get();
162 return button.release();
163 }
164
165 ui::ModalType CertificateSelector::GetModalType() const {
166 return ui::MODAL_TYPE_CHILD;
167 }
168
169 void CertificateSelector::ButtonPressed(views::Button* sender,
170 const ui::Event& event) {
171 if (sender == view_cert_button_) {
172 net::X509Certificate* const cert = GetSelectedCert();
173 if (cert)
174 ShowCertificateViewer(web_contents_,
175 web_contents_->GetTopLevelNativeWindow(), cert);
176 }
177 }
178
179 void CertificateSelector::OnSelectionChanged() {
180 GetDialogClientView()->ok_button()->SetEnabled(GetSelectedCert() != nullptr);
181 }
182
183 void CertificateSelector::OnDoubleClick() {
184 if (Accept())
185 GetWidget()->Close();
186 }
187
188 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/certificate_selector.h ('k') | chrome/browser/ui/views/certificate_selector_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698