1 // Copyright 2020 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_system_proxy_credentials_view.h"
6 
7 #include <string>
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/i18n/number_formatting.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/chromeos/ui/passphrase_textfield.h"
16 #include "chrome/browser/ui/browser_dialogs.h"
17 #include "chrome/browser/ui/views/chrome_layout_provider.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/vector_icons/vector_icons.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/gfx/color_palette.h"
24 #include "ui/gfx/paint_vector_icon.h"
25 #include "ui/native_theme/native_theme.h"
26 #include "ui/views/controls/image_view.h"
27 #include "ui/views/controls/label.h"
28 #include "ui/views/controls/textfield/textfield.h"
29 #include "ui/views/layout/grid_layout.h"
30 #include "ui/views/widget/widget.h"
31 
32 namespace chromeos {
33 
RequestSystemProxyCredentialsView(const std::string & proxy_server,bool show_error_label,base::OnceClosure view_destruction_callback)34 RequestSystemProxyCredentialsView::RequestSystemProxyCredentialsView(
35     const std::string& proxy_server,
36     bool show_error_label,
37     base::OnceClosure view_destruction_callback)
38     : window_title_(
39           l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_TITLE)),
40       proxy_server_(proxy_server),
41       show_error_label_(show_error_label),
42       view_destruction_callback_(std::move(view_destruction_callback)) {
43   Init();
44 }
45 
~RequestSystemProxyCredentialsView()46 RequestSystemProxyCredentialsView::~RequestSystemProxyCredentialsView() {
47   std::move(view_destruction_callback_).Run();
48 }
49 
GetInitiallyFocusedView()50 views::View* RequestSystemProxyCredentialsView::GetInitiallyFocusedView() {
51   return username_textfield_;
52 }
53 
GetWindowTitle() const54 base::string16 RequestSystemProxyCredentialsView::GetWindowTitle() const {
55   return window_title_;
56 }
57 
ShouldShowCloseButton() const58 bool RequestSystemProxyCredentialsView::ShouldShowCloseButton() const {
59   return false;
60 }
61 
GetProxyServer() const62 const std::string& RequestSystemProxyCredentialsView::GetProxyServer() const {
63   return proxy_server_;
64 }
65 
GetUsername() const66 base::string16 RequestSystemProxyCredentialsView::GetUsername() const {
67   return username_textfield_->GetText();
68 }
69 
GetPassword() const70 base::string16 RequestSystemProxyCredentialsView::GetPassword() const {
71   return password_textfield_->GetText();
72 }
73 
Init()74 void RequestSystemProxyCredentialsView::Init() {
75   const views::LayoutProvider* provider = views::LayoutProvider::Get();
76   SetBorder(views::CreateEmptyBorder(
77       provider->GetDialogInsetsForContentType(views::TEXT, views::TEXT)));
78   SetButtonLabel(
79       ui::DIALOG_BUTTON_OK,
80       l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_OK_BUTTON));
81 
82   views::GridLayout* layout =
83       SetLayoutManager(std::make_unique<views::GridLayout>());
84 
85   int column_view_set_id = 0;
86   views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
87 
88   column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
89                         100, views::GridLayout::ColumnSize::kUsePreferred, 0,
90                         0);
91 
92   layout->StartRow(0, column_view_set_id);
93   auto info_label = std::make_unique<views::Label>(l10n_util::GetStringFUTF16(
94       IDS_SYSTEM_PROXY_AUTH_DIALOG_TEXT, base::ASCIIToUTF16(GetProxyServer())));
95   info_label->SetEnabled(true);
96   info_label->SetTextStyle(views::style::STYLE_PRIMARY);
97   layout->AddView(std::move(info_label));
98 
99   layout->StartRow(0, column_view_set_id);
100   auto info_label_privacy = std::make_unique<views::Label>(
101       l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_PRIVACY_WARNING));
102   info_label_privacy->SetEnabled(true);
103   info_label_privacy->SetTextStyle(views::style::STYLE_SECONDARY);
104   layout->AddView(std::move(info_label_privacy));
105 
106   column_view_set_id++;
107   column_set = layout->AddColumnSet(column_view_set_id);
108   column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
109                         views::GridLayout::kFixedSize,
110                         views::GridLayout::ColumnSize::kUsePreferred, 0, 0);
111   const int label_padding =
112       provider->GetDistanceMetric(views::DISTANCE_RELATED_LABEL_HORIZONTAL);
113   column_set->AddPaddingColumn(0, label_padding);
114   column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1.0,
115                         views::GridLayout::ColumnSize::kUsePreferred, 0, 0);
116 
117   const int unrelated_vertical_spacing =
118       provider->GetDistanceMetric(views::DISTANCE_UNRELATED_CONTROL_VERTICAL);
119   layout->StartRowWithPadding(1.0, column_view_set_id, 0,
120                               unrelated_vertical_spacing);
121   auto username_label = std::make_unique<views::Label>(
122       l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_USERNAME_LABEL));
123   username_label->SetEnabled(true);
124 
125   auto username_textfield = std::make_unique<views::Textfield>();
126   username_textfield->SetEnabled(true);
127   username_textfield->SetAssociatedLabel(
128       layout->AddView(std::move(username_label)));
129   username_textfield_ = layout->AddView(std::move(username_textfield));
130 
131   const int related_vertical_spacing =
132       provider->GetDistanceMetric(views::DISTANCE_RELATED_CONTROL_VERTICAL);
133   layout->StartRowWithPadding(1.0, column_view_set_id, 0,
134                               related_vertical_spacing);
135   auto password_label = std::make_unique<views::Label>(
136       l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_PASSWORD_LABEL));
137   password_label->SetEnabled(true);
138   auto password_textfield = std::make_unique<PassphraseTextfield>();
139   password_textfield->SetEnabled(true);
140   password_textfield->SetAssociatedLabel(
141       layout->AddView(std::move(password_label)));
142   password_textfield_ = layout->AddView(std::move(password_textfield));
143 
144   column_view_set_id++;
145   column_set = layout->AddColumnSet(column_view_set_id);
146   column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
147                         views::GridLayout::kFixedSize,
148                         views::GridLayout::ColumnSize::kUsePreferred, 0, 0);
149   column_set->AddPaddingColumn(0, label_padding);
150   column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
151                         1.0, views::GridLayout::ColumnSize::kUsePreferred, 0,
152                         0);
153   layout->StartRowWithPadding(1.0, column_view_set_id, 0,
154                               related_vertical_spacing);
155   auto error_icon = std::make_unique<views::ImageView>();
156   const int kIconSize = 18;
157   error_icon->SetImage(
158       gfx::CreateVectorIcon(vector_icons::kInfoOutlineIcon, kIconSize,
159                             GetNativeTheme()->GetSystemColor(
160                                 ui::NativeTheme::kColorId_AlertSeverityHigh)));
161   error_icon->SetImageSize(gfx::Size(kIconSize, kIconSize));
162   error_icon->SetVisible(show_error_label_);
163   layout->AddView(std::move(error_icon));
164   auto error_label = std::make_unique<views::Label>(
165       l10n_util::GetStringUTF16(IDS_SYSTEM_PROXY_AUTH_DIALOG_ERROR_LABEL));
166   error_label->SetEnabled(true);
167   error_label->SetVisible(show_error_label_);
168   error_label->SetEnabledColor(error_label->GetNativeTheme()->GetSystemColor(
169       ui::NativeTheme::kColorId_AlertSeverityHigh));
170   error_label_ = layout->AddView(std::move(error_label));
171 }
172 
173 }  // namespace chromeos
174