Chromium Code Reviews| Index: chrome/browser/gtk/pk11_password_dialog.cc |
| diff --git a/chrome/browser/gtk/pk11_password_dialog.cc b/chrome/browser/gtk/pk11_password_dialog.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b4f0e7f09a5182b62c998e70ba235b24ab3cb7b7 |
| --- /dev/null |
| +++ b/chrome/browser/gtk/pk11_password_dialog.cc |
| @@ -0,0 +1,210 @@ |
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/pk11_password_dialog.h" |
| + |
| +#include <gtk/gtk.h> |
| + |
| +#include "app/gtk_signal.h" |
| +#include "app/l10n_util.h" |
| +#include "base/basictypes.h" |
| +#include "base/crypto/pk11_blocking_password_delegate.h" |
| +#include "base/task.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/waitable_event.h" |
| +#include "chrome/browser/browser_thread.h" |
| +#include "chrome/browser/gtk/gtk_util.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "grit/generated_resources.h" |
| + |
| +namespace { |
| + |
| +class PK11BlockingDialogDelegate : public base::PK11BlockingPasswordDelegate { |
| + public: |
| + PK11BlockingDialogDelegate(browser::PK11PasswordReason reason, |
| + const std::string& host) |
| + : event_(false, false), |
| + reason_(reason), |
| + host_(host), |
| + password_(), |
| + cancelled_(false) { |
| + } |
| + |
| + // base::PK11BlockingDialogDelegate implementation. |
| + virtual std::string RequestPassword(const std::string& slot_name, bool retry, |
| + bool* cancelled) { |
| + DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(!event_.IsSignaled()); |
| + event_.Reset(); |
| + if (BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + NewRunnableMethod(this, &PK11BlockingDialogDelegate::ShowDialog, |
| + slot_name, retry))) { |
| + event_.Wait(); |
| + } |
| + *cancelled = cancelled_; |
| + return password_; |
| + } |
| + |
| + private: |
| + void ShowDialog(const std::string& slot_name, bool retry) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + ShowPK11PasswordDialog( |
| + slot_name, retry, reason_, host_, |
| + NewCallback(this, &PK11BlockingDialogDelegate::GotPassword)); |
| + } |
| + void GotPassword(const char* password) { |
| + if (password) |
| + password_ = password; |
| + else |
| + cancelled_ = true; |
| + event_.Signal(); |
| + } |
| + base::WaitableEvent event_; |
| + browser::PK11PasswordReason reason_; |
| + std::string host_; |
| + std::string password_; |
| + bool cancelled_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PK11BlockingDialogDelegate); |
| +}; |
| + |
| +// TODO(mattm): change into a constrained dialog. |
| +class PK11PasswordDialog { |
|
Evan Stade
2010/12/10 04:17:04
it's confusing that this class is defined here whe
mattm
2011/01/12 01:22:07
I thought this was a somewhat common pattern to ha
|
| + public: |
| + PK11PasswordDialog(const std::string& slot_name, |
| + bool retry, |
| + browser::PK11PasswordReason reason, |
| + const std::string& host, |
| + browser::PK11PasswordCallback* callback); |
| + |
| + void Show(); |
| + |
| + private: |
| + CHROMEGTK_CALLBACK_1(PK11PasswordDialog, void, OnResponse, int); |
| + CHROMEGTK_CALLBACK_0(PK11PasswordDialog, void, OnWindowDestroy); |
| + |
| + scoped_ptr<browser::PK11PasswordCallback> callback_; |
| + |
| + GtkWidget* dialog_; |
| + GtkWidget* password_entry_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PK11PasswordDialog); |
| +}; |
| + |
| +PK11PasswordDialog::PK11PasswordDialog(const std::string& slot_name, |
| + bool retry, |
| + browser::PK11PasswordReason reason, |
| + const std::string& host, |
| + browser::PK11PasswordCallback* callback) |
| + : callback_(callback) { |
| + dialog_ = gtk_dialog_new_with_buttons( |
| + l10n_util::GetStringUTF8(IDS_PK11_AUTH_DIALOG_TITLE).c_str(), |
| + NULL, |
| + GTK_DIALOG_NO_SEPARATOR, |
| + NULL); // Populate the buttons later, for control over the OK button. |
| + gtk_dialog_add_button(GTK_DIALOG(dialog_), |
| + GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT); |
| + GtkWidget* ok_button = gtk_util::AddButtonToDialog( |
| + dialog_, |
| + l10n_util::GetStringUTF8(IDS_PK11_AUTH_DIALOG_OK_BUTTON_LABEL).c_str(), |
| + GTK_STOCK_OK, |
| + GTK_RESPONSE_ACCEPT); |
| + GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT); |
| + gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); |
| + |
| + // Select an appropriate text for the reason. |
| + std::string text; |
| + const string16& host16 = UTF8ToUTF16(host); |
| + const string16& slot16 = UTF8ToUTF16(slot_name); |
| + switch (reason) { |
| + case browser::kPK11PasswordKeygen: |
| + text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_KEYGEN, |
| + slot16, host16); |
| + break; |
| + case browser::kPK11PasswordCertEnrollment: |
| + text = l10n_util::GetStringFUTF8( |
| + IDS_PK11_AUTH_DIALOG_TEXT_CERT_ENROLLMENT, slot16, host16); |
| + break; |
| + case browser::kPK11PasswordClientAuth: |
| + text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_CLIENT_AUTH, |
| + slot16, host16); |
| + break; |
| + case browser::kPK11PasswordCertImport: |
| + text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_CERT_IMPORT, |
| + slot16); |
| + break; |
| + case browser::kPK11PasswordCertExport: |
| + text = l10n_util::GetStringFUTF8(IDS_PK11_AUTH_DIALOG_TEXT_CERT_EXPORT, |
| + slot16); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + GtkWidget* label = gtk_label_new(text.c_str()); |
| + gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); |
| + gtk_util::LeftAlignMisc(label); |
| + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), label, |
| + FALSE, FALSE, 0); |
| + |
| + password_entry_ = gtk_entry_new(); |
| + gtk_entry_set_activates_default(GTK_ENTRY(password_entry_), TRUE); |
| + gtk_entry_set_visibility(GTK_ENTRY(password_entry_), FALSE); |
| + |
| + GtkWidget* password_box = gtk_hbox_new(FALSE, gtk_util::kLabelSpacing); |
| + gtk_box_pack_start(GTK_BOX(password_box), |
| + gtk_label_new(l10n_util::GetStringUTF8( |
| + IDS_PK11_AUTH_DIALOG_PASSWORD_FIELD).c_str()), |
| + FALSE, FALSE, 0); |
| + gtk_box_pack_start(GTK_BOX(password_box), password_entry_, |
| + TRUE, TRUE, 0); |
| + |
| + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_)->vbox), password_box, |
| + FALSE, FALSE, 0); |
| + |
| + g_signal_connect(dialog_, "response", |
| + G_CALLBACK(OnResponseThunk), this); |
| + g_signal_connect(dialog_, "destroy", |
| + G_CALLBACK(OnWindowDestroyThunk), this); |
| +} |
| + |
| +void PK11PasswordDialog::Show() { |
| + gtk_util::ShowDialog(dialog_); |
| +} |
| + |
| +void PK11PasswordDialog::OnResponse(GtkWidget* dialog, int response_id) { |
| + if (response_id == GTK_RESPONSE_ACCEPT) { |
|
Evan Stade
2010/12/10 04:17:04
no {}
mattm
2011/01/12 01:22:07
Done.
|
| + callback_->Run(gtk_entry_get_text(GTK_ENTRY(password_entry_))); |
| + } else { |
| + callback_->Run(static_cast<const char*>(NULL)); |
| + } |
| + gtk_widget_destroy(GTK_WIDGET(dialog_)); |
| +} |
| + |
| +void PK11PasswordDialog::OnWindowDestroy(GtkWidget* widget) { |
| + delete this; |
| +} |
| + |
| +} // namespace |
| + |
| +// Every post-task we do blocks, so there's no need to ref-count. |
| +DISABLE_RUNNABLE_METHOD_REFCOUNT(PK11BlockingDialogDelegate); |
| + |
| +namespace browser { |
| + |
| +void ShowPK11PasswordDialog(const std::string& slot_name, |
| + bool retry, |
| + PK11PasswordReason reason, |
| + const std::string& host, |
| + PK11PasswordCallback* callback) { |
| + (new PK11PasswordDialog(slot_name, retry, reason, host, callback))->Show(); |
| +} |
| + |
| +base::PK11BlockingPasswordDelegate* NewPK11BlockingDialogDelegate( |
| + PK11PasswordReason reason, |
| + const std::string& host) { |
| + return new PK11BlockingDialogDelegate(reason, host); |
| +} |
| + |
| +} // namespace browser |