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