AccessibilityManager must be deleted after ash Shell, but before InputMethodManager.
[chromium-blink-merge.git] / chrome / browser / ui / views / certificate_selector.cc
blob1e353735c37072040e2ba579bf9d786ad1b99041
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/certificate_selector.h"
7 #include <stddef.h> // For size_t.
8 #include <vector>
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 "components/guest_view/browser/guest_view_base.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager.h"
18 #include "content/public/browser/web_contents.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/models/table_model.h"
21 #include "ui/base/models/table_model_observer.h"
22 #include "ui/views/controls/button/label_button.h"
23 #include "ui/views/controls/table/table_view.h"
24 #include "ui/views/layout/grid_layout.h"
25 #include "ui/views/layout/layout_constants.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/window/dialog_client_view.h"
29 namespace chrome {
31 const int CertificateSelector::kTableViewWidth = 400;
32 const int CertificateSelector::kTableViewHeight = 100;
34 class CertificateSelector::CertificateTableModel : public ui::TableModel {
35 public:
36 explicit CertificateTableModel(const net::CertificateList& certificates);
38 // ui::TableModel:
39 int RowCount() override;
40 base::string16 GetText(int index, int column_id) override;
41 void SetObserver(ui::TableModelObserver* observer) override;
43 private:
44 std::vector<base::string16> items_;
46 DISALLOW_COPY_AND_ASSIGN(CertificateTableModel);
49 CertificateSelector::CertificateTableModel::CertificateTableModel(
50 const net::CertificateList& certificates) {
51 for (const scoped_refptr<net::X509Certificate>& cert : certificates) {
52 items_.push_back(l10n_util::GetStringFUTF16(
53 IDS_CERT_SELECTOR_TABLE_CERT_FORMAT,
54 base::UTF8ToUTF16(cert->subject().GetDisplayName()),
55 base::UTF8ToUTF16(cert->issuer().GetDisplayName())));
59 int CertificateSelector::CertificateTableModel::RowCount() {
60 return items_.size();
63 base::string16 CertificateSelector::CertificateTableModel::GetText(
64 int index,
65 int column_id) {
66 DCHECK_EQ(column_id, 0);
67 DCHECK_GE(index, 0);
68 DCHECK_LT(static_cast<size_t>(index), items_.size());
70 return items_[index];
73 void CertificateSelector::CertificateTableModel::SetObserver(
74 ui::TableModelObserver* observer) {
77 CertificateSelector::CertificateSelector(
78 const net::CertificateList& certificates,
79 content::WebContents* web_contents)
80 : certificates_(certificates),
81 model_(new CertificateTableModel(certificates)),
82 web_contents_(web_contents),
83 table_(nullptr),
84 view_cert_button_(nullptr) {
85 CHECK(web_contents_);
88 CertificateSelector::~CertificateSelector() {
89 table_->SetModel(nullptr);
92 // static
93 bool CertificateSelector::CanShow(content::WebContents* web_contents) {
94 // GetTopLevelWebContents returns |web_contents| if it is not a guest.
95 content::WebContents* top_level_web_contents =
96 guest_view::GuestViewBase::GetTopLevelWebContents(web_contents);
97 return web_modal::WebContentsModalDialogManager::FromWebContents(
98 top_level_web_contents) != nullptr;
101 void CertificateSelector::Show() {
102 constrained_window::ShowWebModalDialogViews(this, web_contents_);
104 // Select the first row automatically. This must be done after the dialog has
105 // been created.
106 table_->Select(0);
109 void CertificateSelector::InitWithText(scoped_ptr<views::View> text_label) {
110 views::GridLayout* const layout = views::GridLayout::CreatePanel(this);
111 SetLayoutManager(layout);
113 const int kColumnSetId = 0;
114 views::ColumnSet* const column_set = layout->AddColumnSet(kColumnSetId);
115 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
116 views::GridLayout::USE_PREF, 0, 0);
118 layout->StartRow(0, kColumnSetId);
119 layout->AddView(text_label.release());
121 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
123 std::vector<ui::TableColumn> columns;
124 columns.push_back(ui::TableColumn());
125 table_ = new views::TableView(model_.get(), columns, views::TEXT_ONLY,
126 true /* single_selection */);
127 table_->SetObserver(this);
128 layout->StartRow(1, kColumnSetId);
129 layout->AddView(table_->CreateParentIfNecessary(), 1, 1,
130 views::GridLayout::FILL, views::GridLayout::FILL,
131 kTableViewWidth, kTableViewHeight);
133 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
136 net::X509Certificate* CertificateSelector::GetSelectedCert() const {
137 const int selected = table_->FirstSelectedRow();
138 if (selected < 0) // Nothing is selected in |table_|.
139 return nullptr;
140 CHECK_LT(static_cast<size_t>(selected), certificates_.size());
141 return certificates_[selected].get();
144 bool CertificateSelector::CanResize() const {
145 return true;
148 base::string16 CertificateSelector::GetWindowTitle() const {
149 return l10n_util::GetStringUTF16(IDS_CLIENT_CERT_DIALOG_TITLE);
152 bool CertificateSelector::IsDialogButtonEnabled(ui::DialogButton button) const {
153 return button != ui::DIALOG_BUTTON_OK || GetSelectedCert() != nullptr;
156 views::View* CertificateSelector::GetInitiallyFocusedView() {
157 DCHECK(table_);
158 return table_;
161 views::View* CertificateSelector::CreateExtraView() {
162 DCHECK(!view_cert_button_);
163 scoped_ptr<views::LabelButton> button(new views::LabelButton(
164 this, l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON)));
165 button->SetStyle(views::Button::STYLE_BUTTON);
166 view_cert_button_ = button.get();
167 return button.release();
170 ui::ModalType CertificateSelector::GetModalType() const {
171 return ui::MODAL_TYPE_CHILD;
174 void CertificateSelector::ButtonPressed(views::Button* sender,
175 const ui::Event& event) {
176 if (sender == view_cert_button_) {
177 net::X509Certificate* const cert = GetSelectedCert();
178 if (cert)
179 ShowCertificateViewer(web_contents_,
180 web_contents_->GetTopLevelNativeWindow(), cert);
184 void CertificateSelector::OnSelectionChanged() {
185 GetDialogClientView()->ok_button()->SetEnabled(GetSelectedCert() != nullptr);
188 void CertificateSelector::OnDoubleClick() {
189 if (GetSelectedCert())
190 GetDialogClientView()->AcceptWindow();
193 } // namespace chrome