| 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()); |
| 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::Init(const base::string16& text) { |
| 85 views::GridLayout* const layout = views::GridLayout::CreatePanel(this); |
| 86 SetLayoutManager(layout); |
| 87 |
| 88 const int kColumnSetId = 0; |
| 89 views::ColumnSet* const column_set = layout->AddColumnSet(kColumnSetId); |
| 90 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 91 views::GridLayout::USE_PREF, 0, 0); |
| 92 |
| 93 layout->StartRow(0, kColumnSetId); |
| 94 scoped_ptr<views::Label> label(new views::Label(text)); |
| 95 label->SetMultiLine(true); |
| 96 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 97 label->SetAllowCharacterBreak(true); |
| 98 layout->AddView(label.release()); |
| 99 |
| 100 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 101 |
| 102 // The dimensions of the certificate selector table view, in pixels. |
| 103 const int kTableViewWidth = 400; |
| 104 const int kTableViewHeight = 100; |
| 105 |
| 106 std::vector<ui::TableColumn> columns; |
| 107 columns.push_back(ui::TableColumn()); |
| 108 table_ = new views::TableView(model_.get(), columns, views::TEXT_ONLY, |
| 109 true /* single_selection */); |
| 110 table_->SetObserver(this); |
| 111 layout->StartRow(1, kColumnSetId); |
| 112 layout->AddView(table_->CreateParentIfNecessary(), 1, 1, |
| 113 views::GridLayout::FILL, views::GridLayout::FILL, |
| 114 kTableViewWidth, kTableViewHeight); |
| 115 |
| 116 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 117 |
| 118 constrained_window::ShowWebModalDialogViews(this, web_contents_); |
| 119 |
| 120 // Select the first row automatically. This must be done after the dialog has |
| 121 // been created. |
| 122 table_->Select(0); |
| 123 } |
| 124 |
| 125 net::X509Certificate* CertificateSelector::GetSelectedCert() const { |
| 126 const int selected = table_->FirstSelectedRow(); |
| 127 if (selected < 0) |
| 128 return nullptr; |
| 129 CHECK_LT(static_cast<size_t>(selected), certificates_.size()); |
| 130 return certificates_[selected].get(); |
| 131 } |
| 132 |
| 133 bool CertificateSelector::CanResize() const { |
| 134 return true; |
| 135 } |
| 136 |
| 137 base::string16 CertificateSelector::GetWindowTitle() const { |
| 138 return l10n_util::GetStringUTF16(IDS_CLIENT_CERT_DIALOG_TITLE); |
| 139 } |
| 140 |
| 141 bool CertificateSelector::IsDialogButtonEnabled(ui::DialogButton button) const { |
| 142 return button != ui::DIALOG_BUTTON_OK || GetSelectedCert() != nullptr; |
| 143 } |
| 144 |
| 145 views::View* CertificateSelector::GetInitiallyFocusedView() { |
| 146 DCHECK(table_); |
| 147 return table_; |
| 148 } |
| 149 |
| 150 views::View* CertificateSelector::CreateExtraView() { |
| 151 DCHECK(!view_cert_button_); |
| 152 scoped_ptr<views::LabelButton> button(new views::LabelButton( |
| 153 this, l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON))); |
| 154 button->SetStyle(views::Button::STYLE_BUTTON); |
| 155 view_cert_button_ = button.get(); |
| 156 return button.release(); |
| 157 } |
| 158 |
| 159 ui::ModalType CertificateSelector::GetModalType() const { |
| 160 return ui::MODAL_TYPE_CHILD; |
| 161 } |
| 162 |
| 163 void CertificateSelector::ButtonPressed(views::Button* sender, |
| 164 const ui::Event& event) { |
| 165 if (sender == view_cert_button_) { |
| 166 net::X509Certificate* const cert = GetSelectedCert(); |
| 167 if (cert) |
| 168 ShowCertificateViewer(web_contents_, |
| 169 web_contents_->GetTopLevelNativeWindow(), cert); |
| 170 } |
| 171 } |
| 172 |
| 173 void CertificateSelector::OnSelectionChanged() { |
| 174 GetDialogClientView()->ok_button()->SetEnabled(GetSelectedCert() != nullptr); |
| 175 } |
| 176 |
| 177 void CertificateSelector::OnDoubleClick() { |
| 178 if (Accept()) |
| 179 GetWidget()->Close(); |
| 180 } |
| 181 |
| 182 } // namespace chrome |
| OLD | NEW |