Chromium Code Reviews| Index: chrome/browser/chromeos/ui/request_pin_view.cc |
| diff --git a/chrome/browser/chromeos/ui/request_pin_view.cc b/chrome/browser/chromeos/ui/request_pin_view.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cff77abf29d183ec72a01496fb6064ce2598e1f7 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/ui/request_pin_view.cc |
| @@ -0,0 +1,281 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
|
emaxx
2016/09/06 15:02:11
nit: The current style guide says not to use "(c)"
igorcov
2016/09/07 09:12:29
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/ui/request_pin_view.h" |
| + |
| +#include <stddef.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/macros.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/chromeos/net/onc_utils.h" |
| +#include "chrome/browser/chromeos/options/passphrase_textfield.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "chrome/grit/theme_resources.h" |
| +#include "chromeos/login/login_state.h" |
| +#include "components/onc/onc_constants.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/events/event.h" |
| +#include "ui/views/controls/label.h" |
| +#include "ui/views/controls/textfield/textfield.h" |
| +#include "ui/views/layout/grid_layout.h" |
| +#include "ui/views/layout/layout_constants.h" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/views/window/dialog_client_view.h" |
| + |
| +namespace chromeos { |
| + |
| +RequestPinView::RequestPinView(const std::string& extension_name, |
| + RequestPinCodeType code_type, |
| + const int attempts_left, |
| + const RequestPinCallback& callback) |
| + : callback_(callback), weak_ptr_factory_(this) { |
| + DCHECK(code_type != RequestPinCodeType::UNCHANGED); |
| + Init(); |
| + SetExtensionName(extension_name); |
| + const bool accept_input = (attempts_left != 0); |
| + SetDialogParameters(code_type, RequestPinErrorType::NONE, attempts_left, |
| + accept_input); |
| +} |
| + |
| +RequestPinView::~RequestPinView() { |
| + if (!callback_.is_null()) { |
| + callback_.Run(base::string16()); |
| + callback_.Reset(); |
| + } |
| + |
| + if (!locked_callback_.is_null()) { |
| + locked_callback_.Run(true); |
| + locked_callback_.Reset(); |
| + } |
| +} |
| + |
| +void RequestPinView::ContentsChanged(views::Textfield* sender, |
| + const base::string16& new_contents) { |
| + GetDialogClientView()->UpdateDialogButtons(); |
| +} |
| + |
| +bool RequestPinView::Cancel() { |
| + // This is null when the dialog is closed by the service with window->Close() |
| + // function. |
| + if (!callback_.is_null()) { |
| + callback_.Run(base::string16()); |
| + callback_.Reset(); |
| + } else { |
| + locked_callback_.Run(true); |
| + locked_callback_.Reset(); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool RequestPinView::Accept() { |
| + DCHECK(!callback_.is_null() && locked_callback_.is_null()); |
| + |
| + if (!textfield_->enabled()) { |
| + callback_.Run(base::string16()); |
| + callback_.Reset(); |
| + return true; |
| + } |
| + |
| + error_label_->SetVisible(true); |
| + error_label_->SetText( |
| + l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PROCESSING)); |
| + error_label_->SetTooltipText(error_label_->text()); |
| + error_label_->SetEnabledColor(SK_ColorGRAY); |
| + error_label_->SizeToPreferredSize(); |
| + SetAcceptInput(false); |
| + callback_.Run(textfield_->text()); |
| + callback_.Reset(); |
| + GetDialogClientView()->UpdateDialogButtons(); |
| + |
| + return false; |
| +} |
| + |
| +base::string16 RequestPinView::GetWindowTitle() const { |
| + return window_title_; |
| +} |
| + |
| +ui::ModalType RequestPinView::GetModalType() const { |
| + return ui::MODAL_TYPE_NONE; |
| +} |
| + |
| +views::View* RequestPinView::GetInitiallyFocusedView() { |
| + return textfield_; |
| +} |
| + |
| +bool RequestPinView::IsDialogButtonEnabled(ui::DialogButton button) const { |
| + switch (button) { |
| + case ui::DialogButton::DIALOG_BUTTON_CANCEL: |
| + return true; |
| + case ui::DialogButton::DIALOG_BUTTON_OK: |
| + if (!locked_callback_.is_null()) { |
| + return false; |
| + } |
| + // Not locked but the |textfield_| is not enabled. It's just a |
| + // notification to the user and [OK] button can be used to close the |
| + // dialog. |
| + if (!textfield_->enabled()) { |
| + return true; |
| + } |
| + return textfield_->text().size() > 0; |
| + case ui::DialogButton::DIALOG_BUTTON_NONE: |
| + return true; |
| + } |
| +} |
| + |
| +bool RequestPinView::IsLocked() { |
| + return !locked_callback_.is_null(); |
| +} |
| + |
| +void RequestPinView::SetCallback(const RequestPinCallback& callback) { |
| + if (!locked_callback_.is_null()) { |
| + locked_callback_.Run(false); |
| + locked_callback_.Reset(); |
| + } |
| + |
| + DCHECK(callback_.is_null()); |
| + callback_ = callback; |
| +} |
| + |
| +void RequestPinView::SetLockedCallback( |
| + const CloseLockedViewCallback& locked_callback) { |
| + DCHECK(locked_callback_.is_null()); |
| + locked_callback_ = locked_callback; |
| +} |
| + |
| +void RequestPinView::SetDialogParameters(RequestPinCodeType code_type, |
| + RequestPinErrorType error_type, |
| + const int attempts_left, |
| + const bool accept_input) { |
| + DCHECK(locked_callback_.is_null()); |
| + SetErrorMessage(error_type, attempts_left); |
| + SetAcceptInput(accept_input); |
| + |
| + switch (code_type) { |
| + case RequestPinCodeType::PIN: |
| + code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PIN); |
| + break; |
| + case RequestPinCodeType::PUK: |
| + code_type_ = l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PUK); |
| + break; |
| + case RequestPinCodeType::UNCHANGED: |
| + break; |
| + } |
| + |
| + UpdateHeaderText(); |
| +} |
| + |
| +void RequestPinView::SetExtensionName(const std::string& extension_name) { |
| + window_title_ = base::ASCIIToUTF16(extension_name); |
| + UpdateHeaderText(); |
| +} |
| + |
| +void RequestPinView::UpdateHeaderText() { |
| + int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; |
| + base::string16 label_text = |
| + l10n_util::GetStringFUTF16(label_text_id, window_title_, code_type_); |
| + header_label_->SetText(label_text); |
| + header_label_->SizeToPreferredSize(); |
| +} |
| + |
| +void RequestPinView::Init() { |
| + views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
| + SetLayoutManager(layout); |
| + |
| + int column_view_set_id = 0; |
| + views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id); |
| + |
| + // Infomation label. |
|
emaxx
2016/09/06 15:02:11
nit: If you have this comment, then please also ad
igorcov
2016/09/07 09:12:29
Done.
|
| + column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + layout->StartRow(0, column_view_set_id); |
| + |
| + int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; |
| + base::string16 label_text = l10n_util::GetStringUTF16(label_text_id); |
| + header_label_ = new views::Label(label_text); |
| + header_label_->SetEnabled(true); |
| + layout->AddView(header_label_); |
| + |
| + layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| + |
| + column_view_set_id++; |
| + column_set = layout->AddColumnSet(column_view_set_id); |
| + column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 100, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + |
| + layout->StartRow(0, column_view_set_id); |
| + textfield_ = new PassphraseTextfield(); |
| + textfield_->set_controller(this); |
| + textfield_->SetEnabled(true); |
| + layout->AddView(textfield_); |
| + |
| + layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| + |
| + column_view_set_id++; |
| + column_set = layout->AddColumnSet(column_view_set_id); |
| + column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
| + views::GridLayout::USE_PREF, 0, 0); |
| + |
| + // Create an error label. |
| + layout->StartRow(0, column_view_set_id); |
| + error_label_ = new views::Label(); |
| + error_label_->SetVisible(false); |
| + error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| + layout->AddView(error_label_); |
| +} |
| + |
| +void RequestPinView::SetAcceptInput(bool accept_input) { |
| + if (accept_input) { |
| + textfield_->SetEnabled(true); |
| + textfield_->SetBackgroundColor(SK_ColorWHITE); |
| + textfield_->RequestFocus(); |
| + } else { |
| + textfield_->SetEnabled(false); |
| + textfield_->SetBackgroundColor(SK_ColorGRAY); |
| + } |
| +} |
| + |
| +void RequestPinView::SetErrorMessage(RequestPinErrorType error_type, |
| + int attempts_left) { |
| + base::string16 error_message; |
| + switch (error_type) { |
| + case RequestPinErrorType::INVALID_PIN: |
| + error_message = |
| + l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PIN_ERROR); |
| + break; |
| + case RequestPinErrorType::INVALID_PUK: |
| + error_message = |
| + l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_INVALID_PUK_ERROR); |
| + break; |
| + case RequestPinErrorType::MAX_ATTEMPTS_EXCEEDED: |
| + error_message = l10n_util::GetStringUTF16( |
| + IDS_REQUEST_PIN_DIALOG_MAX_ATTEMPTS_EXCEEDED_ERROR); |
| + break; |
| + case RequestPinErrorType::UNKNOWN_ERROR: |
| + error_message = |
| + l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_UNKNOWN_ERROR); |
| + break; |
| + case RequestPinErrorType::NONE: |
| + error_label_->SetVisible(false); |
| + return; |
| + } |
| + |
| + if (attempts_left >= 0) { |
| + error_message.append(l10n_util::GetStringFUTF16( |
| + IDS_REQUEST_PIN_DIALOG_ATTEMPTS_LEFT, |
| + base::ASCIIToUTF16(std::to_string(attempts_left)))); |
| + } |
| + |
| + error_label_->SetVisible(true); |
| + error_label_->SetText(error_message); |
| + error_label_->SetTooltipText(error_message); |
| + error_label_->SetEnabledColor(SK_ColorRED); |
| + error_label_->SizeToPreferredSize(); |
| +} |
| + |
| +} // namespace chromeos |