summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/pdf_password_dialog.cc
blob: 0d5e6565bd4e19c61d9dcf3648c667a3088f840e (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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/browser/ui/pdf/pdf_tab_helper.h"

#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
#include "content/public/browser/web_contents.h"
#include "grit/generated_resources.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 {

// PDFPasswordDialogViews 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 PasswordDialogClosedCallback& callback);
  virtual ~PDFPasswordDialogViews();

  // views::DialogDelegate:
  virtual base::string16 GetWindowTitle() const OVERRIDE;
  virtual base::string16 GetDialogButtonLabel(
      ui::DialogButton button) const OVERRIDE;
  virtual bool Cancel() OVERRIDE;
  virtual bool Accept() OVERRIDE;

  // views::WidgetDelegate:
  virtual views::View* GetInitiallyFocusedView() OVERRIDE;
  virtual views::View* GetContentsView() OVERRIDE;
  virtual views::Widget* GetWidget() OVERRIDE;
  virtual const views::Widget* GetWidget() const OVERRIDE;
  virtual void DeleteDelegate() OVERRIDE;
  virtual ui::ModalType GetModalType() const OVERRIDE;

 private:
  // The message box view whose commands we handle.
  views::MessageBoxView* message_box_view_;

  views::Widget* dialog_;

  PasswordDialogClosedCallback callback_;

  DISALLOW_COPY_AND_ASSIGN(PDFPasswordDialogViews);
};

PDFPasswordDialogViews::PDFPasswordDialogViews(
    content::WebContents* web_contents,
    const base::string16& prompt,
    const PasswordDialogClosedCallback& callback)
    : message_box_view_(NULL),
      dialog_(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);

  web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
      web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
  web_modal::WebContentsModalDialogManagerDelegate* modal_delegate =
      web_contents_modal_dialog_manager->delegate();
  DCHECK(modal_delegate);
  dialog_ = views::Widget::CreateWindowAsFramelessChild(
      this, modal_delegate->GetWebContentsModalDialogHost()->GetHostView());
  web_contents_modal_dialog_manager->ShowDialog(dialog_->GetNativeView());
}

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 {
#if defined(USE_ASH)
  return ui::MODAL_TYPE_CHILD;
#else
  return views::WidgetDelegate::GetModalType();
#endif
}

}  // namespace

void ShowPDFPasswordDialog(content::WebContents* web_contents,
                           const base::string16& prompt,
                           const PasswordDialogClosedCallback& callback) {
  new PDFPasswordDialogViews(web_contents, prompt, callback);
}