| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/ssl_client_certificate_selector.h" | 5 #include "chrome/browser/ui/views/ssl_client_certificate_selector.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/i18n/time_formatting.h" | |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/certificate_viewer.h" | 10 #include "chrome/browser/certificate_viewer.h" |
| 12 #include "components/web_modal/web_contents_modal_dialog_host.h" | 11 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 13 #include "components/web_modal/web_contents_modal_dialog_manager.h" | |
| 14 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h" | |
| 15 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 17 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
| 18 #include "net/cert/x509_certificate.h" | 15 #include "net/cert/x509_certificate.h" |
| 19 #include "net/ssl/ssl_cert_request_info.h" | 16 #include "net/ssl/ssl_cert_request_info.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/models/table_model.h" | 18 #include "ui/base/models/table_model.h" |
| 22 #include "ui/base/models/table_model_observer.h" | 19 #include "ui/base/models/table_model_observer.h" |
| 23 #include "ui/gfx/native_widget_types.h" | |
| 24 #include "ui/views/controls/button/label_button.h" | 20 #include "ui/views/controls/button/label_button.h" |
| 25 #include "ui/views/controls/label.h" | 21 #include "ui/views/controls/label.h" |
| 26 #include "ui/views/controls/table/table_view.h" | 22 #include "ui/views/controls/table/table_view.h" |
| 27 #include "ui/views/layout/grid_layout.h" | 23 #include "ui/views/layout/grid_layout.h" |
| 28 #include "ui/views/layout/layout_constants.h" | 24 #include "ui/views/layout/layout_constants.h" |
| 29 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
| 30 #include "ui/views/window/dialog_client_view.h" | 26 #include "ui/views/window/dialog_client_view.h" |
| 31 | 27 |
| 32 #if defined(USE_NSS) | 28 #if defined(USE_NSS) |
| 33 #include "chrome/browser/ui/crypto_module_password_dialog_nss.h" | 29 #include "chrome/browser/ui/crypto_module_password_dialog_nss.h" |
| 34 #endif | 30 #endif |
| 35 | 31 |
| 36 using content::BrowserThread; | |
| 37 using content::WebContents; | |
| 38 using web_modal::WebContentsModalDialogManager; | |
| 39 using web_modal::WebContentsModalDialogManagerDelegate; | |
| 40 | |
| 41 namespace { | |
| 42 | |
| 43 // The dimensions of the certificate selector table view, in pixels. | |
| 44 static const int kTableViewWidth = 400; | |
| 45 static const int kTableViewHeight = 100; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
| 50 // CertificateSelectorTableModel: | 33 // CertificateSelectorTableModel: |
| 51 | 34 |
| 52 class CertificateSelectorTableModel : public ui::TableModel { | 35 class CertificateSelectorTableModel : public ui::TableModel { |
| 53 public: | 36 public: |
| 54 explicit CertificateSelectorTableModel( | 37 explicit CertificateSelectorTableModel( |
| 55 net::SSLCertRequestInfo* cert_request_info); | 38 net::SSLCertRequestInfo* cert_request_info); |
| 56 | 39 |
| 57 // ui::TableModel implementation: | 40 // ui::TableModel implementation: |
| 58 virtual int RowCount() OVERRIDE; | 41 virtual int RowCount() OVERRIDE; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 74 } |
| 92 | 75 |
| 93 void CertificateSelectorTableModel::SetObserver( | 76 void CertificateSelectorTableModel::SetObserver( |
| 94 ui::TableModelObserver* observer) { | 77 ui::TableModelObserver* observer) { |
| 95 } | 78 } |
| 96 | 79 |
| 97 /////////////////////////////////////////////////////////////////////////////// | 80 /////////////////////////////////////////////////////////////////////////////// |
| 98 // SSLClientCertificateSelector: | 81 // SSLClientCertificateSelector: |
| 99 | 82 |
| 100 SSLClientCertificateSelector::SSLClientCertificateSelector( | 83 SSLClientCertificateSelector::SSLClientCertificateSelector( |
| 101 WebContents* web_contents, | 84 content::WebContents* web_contents, |
| 102 const net::HttpNetworkSession* network_session, | 85 const net::HttpNetworkSession* network_session, |
| 103 net::SSLCertRequestInfo* cert_request_info, | 86 net::SSLCertRequestInfo* cert_request_info, |
| 104 const chrome::SelectCertificateCallback& callback) | 87 const chrome::SelectCertificateCallback& callback) |
| 105 : SSLClientAuthObserver(network_session, cert_request_info, callback), | 88 : SSLClientAuthObserver(network_session, cert_request_info, callback), |
| 106 model_(new CertificateSelectorTableModel(cert_request_info)), | 89 model_(new CertificateSelectorTableModel(cert_request_info)), |
| 107 web_contents_(web_contents), | 90 web_contents_(web_contents), |
| 108 window_(NULL), | |
| 109 table_(NULL), | 91 table_(NULL), |
| 110 view_cert_button_(NULL) { | 92 view_cert_button_(NULL) { |
| 111 DVLOG(1) << __FUNCTION__; | 93 DVLOG(1) << __FUNCTION__; |
| 112 } | 94 } |
| 113 | 95 |
| 114 SSLClientCertificateSelector::~SSLClientCertificateSelector() { | 96 SSLClientCertificateSelector::~SSLClientCertificateSelector() { |
| 115 table_->SetModel(NULL); | 97 table_->SetModel(NULL); |
| 116 } | 98 } |
| 117 | 99 |
| 118 void SSLClientCertificateSelector::Init() { | 100 void SSLClientCertificateSelector::Init() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 130 IDS_CLIENT_CERT_DIALOG_TEXT, | 112 IDS_CLIENT_CERT_DIALOG_TEXT, |
| 131 base::ASCIIToUTF16(cert_request_info()->host_and_port.ToString())); | 113 base::ASCIIToUTF16(cert_request_info()->host_and_port.ToString())); |
| 132 views::Label* label = new views::Label(text); | 114 views::Label* label = new views::Label(text); |
| 133 label->SetMultiLine(true); | 115 label->SetMultiLine(true); |
| 134 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 116 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 135 label->SetAllowCharacterBreak(true); | 117 label->SetAllowCharacterBreak(true); |
| 136 layout->AddView(label); | 118 layout->AddView(label); |
| 137 | 119 |
| 138 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 120 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 139 | 121 |
| 122 // The dimensions of the certificate selector table view, in pixels. |
| 123 static const int kTableViewWidth = 400; |
| 124 static const int kTableViewHeight = 100; |
| 125 |
| 140 CreateCertTable(); | 126 CreateCertTable(); |
| 141 layout->StartRow(1, column_set_id); | 127 layout->StartRow(1, column_set_id); |
| 142 layout->AddView(table_->CreateParentIfNecessary(), 1, 1, | 128 layout->AddView(table_->CreateParentIfNecessary(), 1, 1, |
| 143 views::GridLayout::FILL, | 129 views::GridLayout::FILL, |
| 144 views::GridLayout::FILL, kTableViewWidth, kTableViewHeight); | 130 views::GridLayout::FILL, kTableViewWidth, kTableViewHeight); |
| 145 | 131 |
| 146 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | 132 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 147 | 133 |
| 148 StartObserving(); | 134 StartObserving(); |
| 149 | 135 |
| 150 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 136 ShowWebModalDialogViews(this, web_contents_); |
| 151 WebContentsModalDialogManager::FromWebContents(web_contents_); | |
| 152 WebContentsModalDialogManagerDelegate* modal_delegate = | |
| 153 web_contents_modal_dialog_manager->delegate(); | |
| 154 DCHECK(modal_delegate); | |
| 155 window_ = views::Widget::CreateWindowAsFramelessChild( | |
| 156 this, modal_delegate->GetWebContentsModalDialogHost()->GetHostView()); | |
| 157 web_contents_modal_dialog_manager->ShowModalDialog( | |
| 158 window_->GetNativeView()); | |
| 159 | 137 |
| 160 // Select the first row automatically. This must be done after the dialog has | 138 // Select the first row automatically. This must be done after the dialog has |
| 161 // been created. | 139 // been created. |
| 162 table_->Select(0); | 140 table_->Select(0); |
| 163 } | 141 } |
| 164 | 142 |
| 165 net::X509Certificate* SSLClientCertificateSelector::GetSelectedCert() const { | 143 net::X509Certificate* SSLClientCertificateSelector::GetSelectedCert() const { |
| 166 int selected = table_->FirstSelectedRow(); | 144 int selected = table_->FirstSelectedRow(); |
| 167 if (selected >= 0 && | 145 if (selected >= 0 && |
| 168 selected < static_cast<int>( | 146 selected < static_cast<int>(cert_request_info()->client_certs.size())) |
| 169 cert_request_info()->client_certs.size())) | |
| 170 return cert_request_info()->client_certs[selected].get(); | 147 return cert_request_info()->client_certs[selected].get(); |
| 171 return NULL; | 148 return NULL; |
| 172 } | 149 } |
| 173 | 150 |
| 174 /////////////////////////////////////////////////////////////////////////////// | 151 /////////////////////////////////////////////////////////////////////////////// |
| 175 // SSLClientAuthObserver implementation: | 152 // SSLClientAuthObserver implementation: |
| 176 | 153 |
| 177 void SSLClientCertificateSelector::OnCertSelectedByNotification() { | 154 void SSLClientCertificateSelector::OnCertSelectedByNotification() { |
| 178 DVLOG(1) << __FUNCTION__; | 155 DVLOG(1) << __FUNCTION__; |
| 179 DCHECK(window_); | 156 GetWidget()->Close(); |
| 180 window_->Close(); | |
| 181 } | 157 } |
| 182 | 158 |
| 183 /////////////////////////////////////////////////////////////////////////////// | 159 /////////////////////////////////////////////////////////////////////////////// |
| 184 // DialogDelegateView implementation: | 160 // DialogDelegateView implementation: |
| 185 | 161 |
| 186 bool SSLClientCertificateSelector::CanResize() const { | 162 bool SSLClientCertificateSelector::CanResize() const { |
| 187 return true; | 163 return true; |
| 188 } | 164 } |
| 189 | 165 |
| 190 base::string16 SSLClientCertificateSelector::GetWindowTitle() const { | 166 base::string16 SSLClientCertificateSelector::GetWindowTitle() const { |
| 191 return l10n_util::GetStringUTF16(IDS_CLIENT_CERT_DIALOG_TITLE); | 167 return l10n_util::GetStringUTF16(IDS_CLIENT_CERT_DIALOG_TITLE); |
| 192 } | 168 } |
| 193 | 169 |
| 194 void SSLClientCertificateSelector::DeleteDelegate() { | 170 void SSLClientCertificateSelector::DeleteDelegate() { |
| 195 DVLOG(1) << __FUNCTION__; | 171 DVLOG(1) << __FUNCTION__; |
| 196 delete this; | 172 delete this; |
| 197 } | 173 } |
| 198 | 174 |
| 199 bool SSLClientCertificateSelector::IsDialogButtonEnabled( | 175 bool SSLClientCertificateSelector::IsDialogButtonEnabled( |
| 200 ui::DialogButton button) const { | 176 ui::DialogButton button) const { |
| 201 if (button == ui::DIALOG_BUTTON_OK) | 177 if (button == ui::DIALOG_BUTTON_OK) |
| 202 return !!GetSelectedCert(); | 178 return !!GetSelectedCert(); |
| 203 return true; | 179 return true; |
| 204 } | 180 } |
| 205 | 181 |
| 206 bool SSLClientCertificateSelector::Cancel() { | 182 bool SSLClientCertificateSelector::Cancel() { |
| 207 DVLOG(1) << __FUNCTION__; | 183 DVLOG(1) << __FUNCTION__; |
| 208 StopObserving(); | 184 StopObserving(); |
| 209 CertificateSelected(NULL); | 185 CertificateSelected(NULL); |
| 210 | |
| 211 return true; | 186 return true; |
| 212 } | 187 } |
| 213 | 188 |
| 214 bool SSLClientCertificateSelector::Accept() { | 189 bool SSLClientCertificateSelector::Accept() { |
| 215 DVLOG(1) << __FUNCTION__; | 190 DVLOG(1) << __FUNCTION__; |
| 216 scoped_refptr<net::X509Certificate> cert = GetSelectedCert(); | 191 scoped_refptr<net::X509Certificate> cert = GetSelectedCert(); |
| 217 if (cert) { | 192 if (cert) { |
| 218 // Remove the observer before we try unlocking, otherwise we might act on a | 193 // Remove the observer before we try unlocking, otherwise we might act on a |
| 219 // notification while waiting for the unlock dialog, causing us to delete | 194 // notification while waiting for the unlock dialog, causing us to delete |
| 220 // ourself before the Unlocked callback gets called. | 195 // ourself before the Unlocked callback gets called. |
| 221 StopObserving(); | 196 StopObserving(); |
| 222 #if defined(USE_NSS) | 197 #if defined(USE_NSS) |
| 223 chrome::UnlockCertSlotIfNecessary( | 198 chrome::UnlockCertSlotIfNecessary( |
| 224 cert, | 199 cert, |
| 225 chrome::kCryptoModulePasswordClientAuth, | 200 chrome::kCryptoModulePasswordClientAuth, |
| 226 cert_request_info()->host_and_port, | 201 cert_request_info()->host_and_port, |
| 227 window_->GetNativeView(), | 202 GetWidget()->GetNativeView(), |
| 228 base::Bind(&SSLClientCertificateSelector::Unlocked, | 203 base::Bind(&SSLClientCertificateSelector::Unlocked, |
| 229 base::Unretained(this), | 204 base::Unretained(this), |
| 230 cert)); | 205 cert)); |
| 231 #else | 206 #else |
| 232 Unlocked(cert); | 207 Unlocked(cert); |
| 233 #endif | 208 #endif |
| 234 return false; // Unlocked() will close the dialog. | 209 return false; // Unlocked() will close the dialog. |
| 235 } | 210 } |
| 236 | 211 |
| 237 return false; | 212 return false; |
| 238 } | 213 } |
| 239 | 214 |
| 240 views::View* SSLClientCertificateSelector::GetInitiallyFocusedView() { | 215 views::View* SSLClientCertificateSelector::GetInitiallyFocusedView() { |
| 241 return table_; | 216 return table_; |
| 242 } | 217 } |
| 243 | 218 |
| 244 views::View* SSLClientCertificateSelector::CreateExtraView() { | 219 views::View* SSLClientCertificateSelector::CreateExtraView() { |
| 245 DCHECK(!view_cert_button_); | 220 DCHECK(!view_cert_button_); |
| 246 view_cert_button_ = new views::LabelButton(this, | 221 view_cert_button_ = new views::LabelButton(this, |
| 247 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON)); | 222 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON)); |
| 248 view_cert_button_->SetStyle(views::Button::STYLE_BUTTON); | 223 view_cert_button_->SetStyle(views::Button::STYLE_BUTTON); |
| 249 return view_cert_button_; | 224 return view_cert_button_; |
| 250 } | 225 } |
| 251 | 226 |
| 252 ui::ModalType SSLClientCertificateSelector::GetModalType() const { | 227 ui::ModalType SSLClientCertificateSelector::GetModalType() const { |
| 253 #if defined(USE_ASH) | |
| 254 return ui::MODAL_TYPE_CHILD; | 228 return ui::MODAL_TYPE_CHILD; |
| 255 #else | |
| 256 return views::WidgetDelegate::GetModalType(); | |
| 257 #endif | |
| 258 } | 229 } |
| 259 | 230 |
| 260 /////////////////////////////////////////////////////////////////////////////// | 231 /////////////////////////////////////////////////////////////////////////////// |
| 261 // views::ButtonListener implementation: | 232 // views::ButtonListener implementation: |
| 262 | 233 |
| 263 void SSLClientCertificateSelector::ButtonPressed( | 234 void SSLClientCertificateSelector::ButtonPressed( |
| 264 views::Button* sender, const ui::Event& event) { | 235 views::Button* sender, const ui::Event& event) { |
| 265 if (sender == view_cert_button_) { | 236 if (sender == view_cert_button_) { |
| 266 net::X509Certificate* cert = GetSelectedCert(); | 237 net::X509Certificate* cert = GetSelectedCert(); |
| 267 if (cert) | 238 if (cert) |
| 268 ShowCertificateViewer(web_contents_, | 239 ShowCertificateViewer(web_contents_, |
| 269 web_contents_->GetTopLevelNativeWindow(), | 240 web_contents_->GetTopLevelNativeWindow(), |
| 270 cert); | 241 cert); |
| 271 } | 242 } |
| 272 } | 243 } |
| 273 | 244 |
| 274 /////////////////////////////////////////////////////////////////////////////// | 245 /////////////////////////////////////////////////////////////////////////////// |
| 275 // views::TableViewObserver implementation: | 246 // views::TableViewObserver implementation: |
| 276 void SSLClientCertificateSelector::OnSelectionChanged() { | 247 void SSLClientCertificateSelector::OnSelectionChanged() { |
| 277 GetDialogClientView()->ok_button()->SetEnabled(!!GetSelectedCert()); | 248 GetDialogClientView()->ok_button()->SetEnabled(!!GetSelectedCert()); |
| 278 } | 249 } |
| 279 | 250 |
| 280 void SSLClientCertificateSelector::OnDoubleClick() { | 251 void SSLClientCertificateSelector::OnDoubleClick() { |
| 281 if (Accept()) | 252 if (Accept()) |
| 282 window_->Close(); | 253 GetWidget()->Close(); |
| 283 } | 254 } |
| 284 | 255 |
| 285 /////////////////////////////////////////////////////////////////////////////// | 256 /////////////////////////////////////////////////////////////////////////////// |
| 286 // SSLClientCertificateSelector private methods: | 257 // SSLClientCertificateSelector private methods: |
| 287 | 258 |
| 288 void SSLClientCertificateSelector::CreateCertTable() { | 259 void SSLClientCertificateSelector::CreateCertTable() { |
| 289 std::vector<ui::TableColumn> columns; | 260 std::vector<ui::TableColumn> columns; |
| 290 columns.push_back(ui::TableColumn()); | 261 columns.push_back(ui::TableColumn()); |
| 291 table_ = new views::TableView(model_.get(), | 262 table_ = new views::TableView(model_.get(), columns, views::TEXT_ONLY, |
| 292 columns, | |
| 293 views::TEXT_ONLY, | |
| 294 true /* single_selection */); | 263 true /* single_selection */); |
| 295 table_->SetObserver(this); | 264 table_->SetObserver(this); |
| 296 } | 265 } |
| 297 | 266 |
| 298 void SSLClientCertificateSelector::Unlocked(net::X509Certificate* cert) { | 267 void SSLClientCertificateSelector::Unlocked(net::X509Certificate* cert) { |
| 299 DVLOG(1) << __FUNCTION__; | 268 DVLOG(1) << __FUNCTION__; |
| 300 CertificateSelected(cert); | 269 CertificateSelected(cert); |
| 301 window_->Close(); | 270 GetWidget()->Close(); |
| 302 } | 271 } |
| 303 | 272 |
| 304 namespace chrome { | 273 namespace chrome { |
| 305 | 274 |
| 306 void ShowSSLClientCertificateSelector( | 275 void ShowSSLClientCertificateSelector( |
| 307 content::WebContents* contents, | 276 content::WebContents* contents, |
| 308 const net::HttpNetworkSession* network_session, | 277 const net::HttpNetworkSession* network_session, |
| 309 net::SSLCertRequestInfo* cert_request_info, | 278 net::SSLCertRequestInfo* cert_request_info, |
| 310 const chrome::SelectCertificateCallback& callback) { | 279 const chrome::SelectCertificateCallback& callback) { |
| 311 DVLOG(1) << __FUNCTION__ << " " << contents; | 280 DVLOG(1) << __FUNCTION__ << " " << contents; |
| 312 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 313 (new SSLClientCertificateSelector( | 282 (new SSLClientCertificateSelector( |
| 314 contents, network_session, cert_request_info, callback))->Init(); | 283 contents, network_session, cert_request_info, callback))->Init(); |
| 315 } | 284 } |
| 316 | 285 |
| 317 } // namespace chrome | 286 } // namespace chrome |
| OLD | NEW |