Index: chrome/browser/chromeos/options/request_pin_view.cc |
diff --git a/chrome/browser/chromeos/options/request_pin_view.cc b/chrome/browser/chromeos/options/request_pin_view.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d9a0ea2a01d3483edf6e26bc467fd3c53deac194 |
--- /dev/null |
+++ b/chrome/browser/chromeos/options/request_pin_view.cc |
@@ -0,0 +1,231 @@ |
+// Copyright (c) 2016 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/chromeos/options/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, |
+ const std::string& dialog_type, |
+ const base::string16& error_message, |
+ const bool accept_input, |
+ const UserInputCallback& callback) |
+ : callback_(callback), weak_ptr_factory_(this) { |
emaxx
2016/07/08 02:00:14
It's a bit unsafe to keep all data members uniniti
|
+ Init(); |
+ SetExtensionName(extension_name); |
+ SetDialogType(dialog_type); |
+ SetErrorMessage(error_message); |
+ SetAcceptInput(accept_input); |
+} |
+ |
+RequestPinView::~RequestPinView() {} |
emaxx
2016/07/08 02:00:14
Looks like this can be omitted.
igorcov
2016/07/08 11:40:53
Compiler says it can't.
|
+ |
+void RequestPinView::ContentsChanged(views::Textfield* sender, |
+ const base::string16& new_contents) { |
+ GetDialogClientView()->UpdateDialogButtons(); |
+} |
+ |
+bool RequestPinView::Cancel() { |
+ waiting_for_request_ = false; |
+ // This is null when the dialog is closed by the service with window->Close() |
+ // function, or when the user input has been sent to extension and while |
+ // waiting for validation the user closes the dialog. |
+ if (!callback_.is_null()) { |
+ callback_.Run(base::string16()); |
+ callback_.Reset(); |
+ } |
+ |
+ return true; |
+} |
+ |
+bool RequestPinView::Accept() { |
+ if (callback_.is_null()) { |
emaxx
2016/07/08 02:00:14
Shouldn't this be a DCHECK?
igorcov
2016/07/08 11:40:53
Done.
|
+ LOG(ERROR) << "Something broke the flow, the dialog will be closed"; |
emaxx
2016/07/08 02:00:14
The message looks quite informal.
|
+ return true; |
+ } |
+ |
+ if (!accept_input_) { |
+ callback_.Run(base::string16()); |
+ callback_.Reset(); |
+ waiting_for_request_ = false; |
+ return true; |
+ } |
+ |
+ error_label_->SetVisible(true); |
+ error_label_->SetText( |
+ l10n_util::GetStringUTF16(IDS_REQUEST_PIN_DIALOG_PROCESSING)); |
+ error_label_->SetEnabledColor(SK_ColorGRAY); |
+ error_label_->SizeToPreferredSize(); |
+ passphrase_textfield_->SetEnabled(false); |
+ passphrase_textfield_->SetBackgroundColor(SK_ColorGRAY); |
+ GetDialogClientView()->UpdateDialogButtons(); |
+ |
+ // Set the flag specifying that Chrome is waiting for a notification request |
+ // from extension now. |
+ waiting_for_request_ = true; |
+ callback_.Run(passphrase_textfield_->text()); |
+ return false; |
+} |
+ |
+base::string16 RequestPinView::GetWindowTitle() const { |
+ return window_title_; |
+} |
+ |
+ui::ModalType RequestPinView::GetModalType() const { |
+ return ui::MODAL_TYPE_SYSTEM; |
+} |
+ |
+views::View* RequestPinView::GetInitiallyFocusedView() { |
+ return passphrase_textfield_; |
+} |
+ |
+bool RequestPinView::IsDialogButtonEnabled(ui::DialogButton button) const { |
+ switch (button) { |
+ case ui::DialogButton::DIALOG_BUTTON_CANCEL: |
+ return true; |
+ case ui::DialogButton::DIALOG_BUTTON_OK: |
+ // No more input accepted, user is just notified. |
+ if (!accept_input_) { |
+ return true; |
+ } |
+ |
+ // The text field is disabled, the button should be disabled too. |
+ if (!passphrase_textfield_->enabled()) { |
+ return false; |
+ } |
+ |
+ return passphrase_textfield_->text().size() > 0; |
+ case ui::DialogButton::DIALOG_BUTTON_NONE: |
+ return true; |
+ } |
+} |
+ |
+void RequestPinView::SetExtensionName(const std::string& extension_name) { |
+ window_title_ = base::ASCIIToUTF16(extension_name); |
+ int label_text_id = IDS_REQUEST_PIN_DIALOG_HEADER; |
+ base::string16 label_text = l10n_util::GetStringFUTF16( |
+ label_text_id, base::ASCIIToUTF16(extension_name)); |
+ header_label_->SetText(label_text); |
+ header_label_->SizeToPreferredSize(); |
+} |
+ |
+void RequestPinView::SetDialogType(const std::string& dialog_type) { |
+ dialog_type_label_->SetText(base::ASCIIToUTF16(dialog_type)); |
+ dialog_type_label_->SizeToPreferredSize(); |
+} |
+ |
+void RequestPinView::SetErrorMessage(const base::string16& error_message) { |
+ if (error_message.empty()) { |
+ error_label_->SetVisible(false); |
+ } else { |
+ error_label_->SetVisible(true); |
+ error_label_->SetText(error_message); |
+ error_label_->SetEnabledColor(SK_ColorRED); |
+ error_label_->SizeToPreferredSize(); |
+ } |
+} |
+ |
+void RequestPinView::SetAcceptInput(bool accept_input) { |
+ accept_input_ = accept_input; |
+ if (accept_input_) { |
+ passphrase_textfield_->SetEnabled(true); |
+ passphrase_textfield_->SetBackgroundColor(SK_ColorWHITE); |
+ passphrase_textfield_->RequestFocus(); |
+ } else { |
+ passphrase_textfield_->SetEnabled(false); |
+ passphrase_textfield_->SetBackgroundColor(SK_ColorGRAY); |
+ } |
+} |
+ |
+void RequestPinView::SetCallback(const UserInputCallback& callback) { |
+ if (waiting_for_request_) { |
+ callback_.Reset(); |
emaxx
2016/07/08 02:00:14
In which scenario is it fine to just drop the call
igorcov
2016/07/08 11:40:53
It's the case when user submitted some input and t
|
+ } else { |
+ // Make sure the old callback was used already before setting the new one. |
+ DCHECK(callback_.is_null()); |
+ } |
+ |
+ callback_ = callback; |
+} |
+ |
+void RequestPinView::Init() { |
+ views::GridLayout* layout = views::GridLayout::CreatePanel(this); |
+ SetLayoutManager(layout); |
+ |
+ const int kTextfieldWidth = 25; |
+ int column_view_set_id = 0; |
+ views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id); |
+ |
+ // Infomation label. |
+ 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::LEADING, views::GridLayout::FILL, 1, |
+ views::GridLayout::USE_PREF, 0, 0); |
+ column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1, |
+ views::GridLayout::USE_PREF, 0, 0); |
+ |
+ layout->StartRow(0, column_view_set_id); |
+ dialog_type_label_ = new views::Label(); |
+ dialog_type_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
+ dialog_type_label_->SetEnabled(true); |
+ layout->AddView(dialog_type_label_); |
+ |
+ passphrase_textfield_ = new PassphraseTextfield(); |
+ passphrase_textfield_->set_controller(this); |
+ passphrase_textfield_->SetEnabled(true); |
+ passphrase_textfield_->SetAccessibleName(dialog_type_label_->text()); |
+ passphrase_textfield_->set_default_width_in_chars(kTextfieldWidth); |
+ layout->AddView(passphrase_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_); |
+} |
+ |
+} // namespace chromeos |