This source file includes following definitions.
- dialog_
- OnAutofillDataAvailable
- OnLoginModelDestroying
- BuildViewForPasswordManager
- CloseDialog
- OnOKClicked
- OnCancelClicked
- OnPromptHierarchyChanged
- Create
- OnDestroy
#include "chrome/browser/ui/login/login_prompt.h"
#include <gtk/gtk.h>
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/ui/gtk/constrained_window_gtk.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "components/password_manager/core/browser/login_model.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "grit/generated_resources.h"
#include "net/url_request/url_request.h"
#include "ui/base/gtk/gtk_hig_constants.h"
#include "ui/base/gtk/gtk_signal.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/gtk_compat.h"
#include "ui/gfx/scoped_gobject.h"
using autofill::PasswordForm;
using content::BrowserThread;
using content::WebContents;
using web_modal::WebContentsModalDialogManager;
class LoginHandlerGtk : public LoginHandler {
public:
LoginHandlerGtk(net::AuthChallengeInfo* auth_info, net::URLRequest* request)
: LoginHandler(auth_info, request),
username_entry_(NULL),
password_entry_(NULL),
ok_(NULL),
dialog_(NULL) {
}
virtual void OnAutofillDataAvailable(
const base::string16& username,
const base::string16& password) OVERRIDE {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (strlen(gtk_entry_get_text(GTK_ENTRY(username_entry_))) == 0) {
gtk_entry_set_text(GTK_ENTRY(username_entry_),
base::UTF16ToUTF8(username).c_str());
gtk_entry_set_text(GTK_ENTRY(password_entry_),
base::UTF16ToUTF8(password).c_str());
gtk_editable_select_region(GTK_EDITABLE(username_entry_), 0, -1);
}
}
virtual void OnLoginModelDestroying() OVERRIDE {}
virtual void BuildViewForPasswordManager(
PasswordManager* manager,
const base::string16& explanation) OVERRIDE {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
root_.reset(gtk_vbox_new(FALSE, ui::kContentAreaBorder));
g_object_ref_sink(root_.get());
g_signal_connect(root_.get(), "destroy", G_CALLBACK(OnDestroyThunk), this);
GtkWidget* label = gtk_label_new(base::UTF16ToUTF8(explanation).c_str());
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_box_pack_start(GTK_BOX(root_.get()), label, FALSE, FALSE, 0);
username_entry_ = gtk_entry_new();
gtk_entry_set_activates_default(GTK_ENTRY(username_entry_), TRUE);
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* table = gtk_util::CreateLabeledControlsGroup(NULL,
l10n_util::GetStringUTF8(IDS_LOGIN_DIALOG_USERNAME_FIELD).c_str(),
username_entry_,
l10n_util::GetStringUTF8(IDS_LOGIN_DIALOG_PASSWORD_FIELD).c_str(),
password_entry_,
NULL);
gtk_box_pack_start(GTK_BOX(root_.get()), table, FALSE, FALSE, 0);
GtkWidget* hbox = gtk_hbox_new(FALSE, 12);
gtk_box_pack_start(GTK_BOX(root_.get()), hbox, FALSE, FALSE, 0);
ok_ = gtk_button_new_from_stock(GTK_STOCK_OK);
gtk_button_set_label(
GTK_BUTTON(ok_),
l10n_util::GetStringUTF8(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL).c_str());
g_signal_connect(ok_, "clicked", G_CALLBACK(OnOKClickedThunk), this);
gtk_box_pack_end(GTK_BOX(hbox), ok_, FALSE, FALSE, 0);
GtkWidget* cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
g_signal_connect(cancel, "clicked", G_CALLBACK(OnCancelClickedThunk), this);
gtk_box_pack_end(GTK_BOX(hbox), cancel, FALSE, FALSE, 0);
g_signal_connect(root_.get(), "hierarchy-changed",
G_CALLBACK(OnPromptHierarchyChangedThunk), this);
SetModel(manager);
WebContents* requesting_contents = GetWebContentsForLogin();
DCHECK(requesting_contents);
dialog_ = CreateWebContentsModalDialogGtk(root_.get(), username_entry_);
WebContentsModalDialogManager* web_contents_modal_dialog_manager =
WebContentsModalDialogManager::FromWebContents(requesting_contents);
web_contents_modal_dialog_manager->ShowDialog(dialog_);
NotifyAuthNeeded();
}
virtual void CloseDialog() OVERRIDE {
if (dialog_)
gtk_widget_destroy(dialog_);
}
protected:
virtual ~LoginHandlerGtk() {
}
private:
friend class LoginPrompt;
CHROMEGTK_CALLBACK_0(LoginHandlerGtk, void, OnOKClicked);
CHROMEGTK_CALLBACK_0(LoginHandlerGtk, void, OnCancelClicked);
CHROMEGTK_CALLBACK_1(LoginHandlerGtk, void, OnPromptHierarchyChanged,
GtkWidget*);
CHROMEGTK_CALLBACK_0(LoginHandlerGtk, void, OnDestroy);
ui::ScopedGObject<GtkWidget>::Type root_;
GtkWidget* username_entry_;
GtkWidget* password_entry_;
GtkWidget* ok_;
GtkWidget* dialog_;
DISALLOW_COPY_AND_ASSIGN(LoginHandlerGtk);
};
void LoginHandlerGtk::OnOKClicked(GtkWidget* sender) {
SetAuth(
base::UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(username_entry_))),
base::UTF8ToUTF16(gtk_entry_get_text(GTK_ENTRY(password_entry_))));
}
void LoginHandlerGtk::OnCancelClicked(GtkWidget* sender) {
CancelAuth();
}
void LoginHandlerGtk::OnPromptHierarchyChanged(GtkWidget* sender,
GtkWidget* previous_toplevel) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!gtk_widget_is_toplevel(gtk_widget_get_toplevel(ok_)))
return;
gtk_widget_set_can_default(ok_, TRUE);
gtk_widget_grab_default(ok_);
}
LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
net::URLRequest* request) {
return new LoginHandlerGtk(auth_info, request);
}
void LoginHandlerGtk::OnDestroy(GtkWidget* widget) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
dialog_ = NULL;
SetModel(NULL);
ReleaseSoon();
}