1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2013 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/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/pdf/browser/pdf_web_contents_helper_client.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/controls/message_box_view.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/dialog_delegate.h"
namespace {
// Runs a tab-modal dialog that asks the user for a password.
class PDFPasswordDialogViews : public views::DialogDelegate {
public:
PDFPasswordDialogViews(content::WebContents* web_contents,
const base::string16& prompt,
const pdf::PasswordDialogClosedCallback& callback);
~PDFPasswordDialogViews() override;
// views::DialogDelegate:
base::string16 GetWindowTitle() const override;
base::string16 GetDialogButtonLabel(ui::DialogButton button) const override;
bool Cancel() override;
bool Accept() override;
// views::WidgetDelegate:
views::View* GetInitiallyFocusedView() override;
views::View* GetContentsView() override;
views::Widget* GetWidget() override;
const views::Widget* GetWidget() const override;
void DeleteDelegate() override;
ui::ModalType GetModalType() const override;
private:
// The message box view whose commands we handle.
views::MessageBoxView* message_box_view_;
pdf::PasswordDialogClosedCallback callback_;
DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews);
};
PDFPasswordDialogViews::PDFPasswordDialogViews(
content::WebContents* web_contents,
const base::string16& prompt,
const pdf::PasswordDialogClosedCallback& callback)
: message_box_view_(NULL), callback_(callback) {
views::MessageBoxView::InitParams init_params(prompt);
init_params.options = views::MessageBoxView::HAS_PROMPT_FIELD;
init_params.inter_row_vertical_spacing =
views::kUnrelatedControlVerticalSpacing;
message_box_view_ = new views::MessageBoxView(init_params);
message_box_view_->text_box()->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
constrained_window::ShowWebModalDialogViews(this, web_contents);
}
PDFPasswordDialogViews::~PDFPasswordDialogViews() {
if (!callback_.is_null()) {
// This dialog was torn down without either OK or cancel being clicked; be
// considerate and at least do the callback.
callback_.Run(false, base::string16());
}
}
//////////////////////////////////////////////////////////////////////////////
// PDFPasswordDialogViews, views::DialogDelegate implementation:
base::string16 PDFPasswordDialogViews::GetWindowTitle() const {
return l10n_util::GetStringUTF16(IDS_PDF_PASSWORD_DIALOG_TITLE);
}
base::string16 PDFPasswordDialogViews::GetDialogButtonLabel(
ui::DialogButton button) const {
if (button == ui::DIALOG_BUTTON_OK)
return l10n_util::GetStringUTF16(IDS_OK);
if (button == ui::DIALOG_BUTTON_CANCEL)
return l10n_util::GetStringUTF16(IDS_CANCEL);
return base::string16();
}
bool PDFPasswordDialogViews::Cancel() {
callback_.Run(false, base::string16());
callback_.Reset();
return true;
}
bool PDFPasswordDialogViews::Accept() {
callback_.Run(true, message_box_view_->text_box()->text());
callback_.Reset();
return true;
}
///////////////////////////////////////////////////////////////////////////////
// PDFPasswordDialogViews, views::WidgetDelegate implementation:
views::View* PDFPasswordDialogViews::GetInitiallyFocusedView() {
return message_box_view_->text_box();
}
views::View* PDFPasswordDialogViews::GetContentsView() {
return message_box_view_;
}
views::Widget* PDFPasswordDialogViews::GetWidget() {
return message_box_view_->GetWidget();
}
const views::Widget* PDFPasswordDialogViews::GetWidget() const {
return message_box_view_->GetWidget();
}
void PDFPasswordDialogViews::DeleteDelegate() {
delete this;
}
ui::ModalType PDFPasswordDialogViews::GetModalType() const {
return ui::MODAL_TYPE_CHILD;
}
} // namespace
void ShowPDFPasswordDialog(content::WebContents* web_contents,
const base::string16& prompt,
const pdf::PasswordDialogClosedCallback& callback) {
new PDFPasswordDialogViews(web_contents, prompt, callback);
}
|