Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/ui/request_pin_view.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/strings/utf_string_conversions.h" | |
| 14 #include "chrome/browser/chromeos/net/onc_utils.h" | |
| 15 #include "chrome/browser/chromeos/options/passphrase_textfield.h" | |
| 16 #include "chrome/grit/generated_resources.h" | |
| 17 #include "chrome/grit/theme_resources.h" | |
| 18 #include "chromeos/login/login_state.h" | |
| 19 #include "components/onc/onc_constants.h" | |
| 20 #include "ui/base/l10n/l10n_util.h" | |
| 21 #include "ui/base/resource/resource_bundle.h" | |
| 22 #include "ui/events/event.h" | |
| 23 #include "ui/views/controls/label.h" | |
| 24 #include "ui/views/controls/textfield/textfield.h" | |
| 25 #include "ui/views/layout/grid_layout.h" | |
| 26 #include "ui/views/layout/layout_constants.h" | |
| 27 #include "ui/views/widget/widget.h" | |
| 28 #include "ui/views/window/dialog_client_view.h" | |
| 29 | |
| 30 namespace chromeos { | |
| 31 | |
| 32 RequestPinView::RequestPinView(const std::string& extension_name, | |
| 33 const std::string& dialog_type, | |
| 34 const base::string16& error_message, | |
| 35 bool accept_input, | |
| 36 const RequestPinCallback& callback) | |
| 37 : callback_(callback), weak_ptr_factory_(this) { | |
| 38 Init(); | |
| 39 SetExtensionName(extension_name); | |
| 40 SetDialogParameters(dialog_type, error_message, accept_input); | |
| 41 } | |
| 42 | |
| 43 RequestPinView::~RequestPinView() {} | |
| 44 | |
| 45 void RequestPinView::ContentsChanged(views::Textfield* sender, | |
| 46 const base::string16& new_contents) { | |
| 47 GetDialogClientView()->UpdateDialogButtons(); | |
| 48 } | |
| 49 | |
| 50 bool RequestPinView::Cancel() { | |
| 51 locked_ = false; | |
| 52 // This is null when the dialog is closed by the service with window->Close() | |
| 53 // function. | |
| 54 if (!callback_.is_null()) { | |
| 55 callback_.Run(base::string16()); | |
| 56 callback_.Reset(); | |
| 57 } | |
| 58 | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 bool RequestPinView::Accept() { | |
| 63 DCHECK(!callback_.is_null()); | |
| 64 | |
| 65 if (!textfield_->enabled()) { | |
| 66 callback_.Run(base::string16()); | |
| 67 callback_.Reset(); | |
| 68 locked_ = false; | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 error_label_->SetVisible(true); | |
| 73 error_label_->SetText( | |
| 74 l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PROCESSING)); | |
| 75 error_label_->SetTooltipText(error_label_->text()); | |
| 76 error_label_->SetEnabledColor(SK_ColorGRAY); | |
| 77 error_label_->SizeToPreferredSize(); | |
| 78 SetAcceptInput(false); | |
| 79 locked_ = true; | |
| 80 GetDialogClientView()->UpdateDialogButtons(); | |
| 81 callback_.Run(textfield_->text()); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 base::string16 RequestPinView::GetWindowTitle() const { | |
| 86 return window_title_; | |
| 87 } | |
| 88 | |
| 89 ui::ModalType RequestPinView::GetModalType() const { | |
| 90 return ui::MODAL_TYPE_NONE; | |
| 91 } | |
| 92 | |
| 93 views::View* RequestPinView::GetInitiallyFocusedView() { | |
| 94 return textfield_; | |
| 95 } | |
| 96 | |
| 97 bool RequestPinView::IsDialogButtonEnabled(ui::DialogButton button) const { | |
| 98 switch (button) { | |
| 99 case ui::DialogButton::DIALOG_BUTTON_CANCEL: | |
| 100 return true; | |
| 101 case ui::DialogButton::DIALOG_BUTTON_OK: | |
| 102 if (locked_) { | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 // Not locked but the |textfield_| is not enabled. It's just a | |
| 107 // notification to the user and [OK] button can be used to close the | |
| 108 // dialog. | |
| 109 if (!textfield_->enabled()) { | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 return textfield_->text().size() > 0; | |
| 114 case ui::DialogButton::DIALOG_BUTTON_NONE: | |
| 115 return true; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 bool RequestPinView::IsLocked() { | |
| 120 return locked_; | |
| 121 } | |
| 122 | |
| 123 void RequestPinView::SetCallback(const RequestPinCallback& callback) { | |
| 124 // It is acceptable to drop the old |callback_| if the dialog is locked, | |
| 125 // meaning the callback is a temporary one pointing to | |
| 126 // CertificateProviderService::OnFlowInterrupted. It is suposed to be used | |
| 127 // to notify the service if the dialog is closed while it is locked. | |
| 128 DCHECK(locked_ || callback_.is_null()); | |
| 129 callback_ = callback; | |
| 130 } | |
| 131 | |
| 132 void RequestPinView::SetDialogParameters(const base::string16& error_message, | |
| 133 bool accept_input) { | |
| 134 locked_ = false; | |
| 135 | |
| 136 SetErrorMessage(error_message); | |
| 137 SetAcceptInput(accept_input); | |
| 138 } | |
| 139 | |
| 140 void RequestPinView::SetDialogParameters(const std::string& dialog_type, | |
| 141 const base::string16& error_message, | |
| 142 bool accept_input) { | |
| 143 SetDialogParameters(error_message, accept_input); | |
| 144 | |
| 145 textfield_label_->SetText(base::ASCIIToUTF16(dialog_type)); | |
|
stevenjb
2016/08/09 21:04:39
Where does |dialog_type| come from? It would be be
igorcov1
2016/08/10 18:05:03
Done.
| |
| 146 textfield_label_->SizeToPreferredSize(); | |
| 147 } | |
| 148 | |
| 149 void RequestPinView::SetExtensionName(const std::string& extension_name) { | |
| 150 window_title_ = base::ASCIIToUTF16(extension_name); | |
| 151 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; | |
| 152 base::string16 label_text = l10n_util::GetStringFUTF16( | |
| 153 label_text_id, base::ASCIIToUTF16(extension_name)); | |
| 154 header_label_->SetText(label_text); | |
| 155 header_label_->SizeToPreferredSize(); | |
| 156 } | |
| 157 | |
| 158 void RequestPinView::Init() { | |
| 159 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
| 160 SetLayoutManager(layout); | |
| 161 | |
| 162 int column_view_set_id = 0; | |
| 163 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id); | |
| 164 | |
| 165 // Infomation label. | |
| 166 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
| 167 views::GridLayout::USE_PREF, 0, 0); | |
| 168 layout->StartRow(0, column_view_set_id); | |
| 169 | |
| 170 int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; | |
| 171 base::string16 label_text = l10n_util::GetStringUTF16(label_text_id); | |
| 172 header_label_ = new views::Label(label_text); | |
| 173 header_label_->SetEnabled(true); | |
| 174 layout->AddView(header_label_); | |
| 175 | |
| 176 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 177 | |
| 178 column_view_set_id++; | |
| 179 column_set = layout->AddColumnSet(column_view_set_id); | |
| 180 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
| 181 views::GridLayout::USE_PREF, 0, 0); | |
| 182 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 100, | |
| 183 views::GridLayout::USE_PREF, 0, 0); | |
| 184 | |
| 185 layout->StartRow(0, column_view_set_id); | |
| 186 textfield_label_ = new views::Label(); | |
| 187 textfield_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 188 textfield_label_->SetEnabled(true); | |
| 189 layout->AddView(textfield_label_); | |
| 190 | |
| 191 textfield_ = new PassphraseTextfield(); | |
| 192 textfield_->set_controller(this); | |
| 193 textfield_->SetEnabled(true); | |
| 194 textfield_->SetAccessibleName(textfield_label_->text()); | |
| 195 layout->AddView(textfield_); | |
| 196 | |
| 197 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 198 | |
| 199 column_view_set_id++; | |
| 200 column_set = layout->AddColumnSet(column_view_set_id); | |
| 201 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, | |
| 202 views::GridLayout::USE_PREF, 0, 0); | |
| 203 | |
| 204 // Create an error label. | |
| 205 layout->StartRow(0, column_view_set_id); | |
| 206 error_label_ = new views::Label(); | |
| 207 error_label_->SetVisible(false); | |
| 208 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 209 layout->AddView(error_label_); | |
| 210 } | |
| 211 | |
| 212 void RequestPinView::SetAcceptInput(bool accept_input) { | |
| 213 if (accept_input) { | |
| 214 textfield_->SetEnabled(true); | |
| 215 textfield_->SetBackgroundColor(SK_ColorWHITE); | |
| 216 textfield_->RequestFocus(); | |
| 217 } else { | |
| 218 textfield_->SetEnabled(false); | |
| 219 textfield_->SetBackgroundColor(SK_ColorGRAY); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 void RequestPinView::SetErrorMessage(const base::string16& error_message) { | |
| 224 if (error_message.empty()) { | |
| 225 error_label_->SetVisible(false); | |
|
stevenjb
2016/08/09 21:04:39
return; no else
igorcov1
2016/08/10 18:05:03
Done.
| |
| 226 } else { | |
| 227 error_label_->SetVisible(true); | |
| 228 error_label_->SetText(error_message); | |
| 229 error_label_->SetTooltipText(error_message); | |
| 230 error_label_->SetEnabledColor(SK_ColorRED); | |
| 231 error_label_->SizeToPreferredSize(); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 } // namespace chromeos | |
| OLD | NEW |