Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / options / wimax_config_view.cc
blobf5ea282834f765a16d5a2914cdf0ca022d7981ff
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/options/wimax_config_view.h"
7 #include "base/bind.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/enrollment_dialog_view.h"
12 #include "chrome/browser/chromeos/login/startup_utils.h"
13 #include "chrome/browser/chromeos/net/onc_utils.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "chrome/grit/theme_resources.h"
17 #include "chromeos/login/login_state.h"
18 #include "chromeos/network/network_configuration_handler.h"
19 #include "chromeos/network/network_event_log.h"
20 #include "chromeos/network/network_profile.h"
21 #include "chromeos/network/network_profile_handler.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
24 #include "components/onc/onc_constants.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/chromeos/network/network_connect.h"
29 #include "ui/events/event.h"
30 #include "ui/views/controls/button/checkbox.h"
31 #include "ui/views/controls/button/image_button.h"
32 #include "ui/views/controls/label.h"
33 #include "ui/views/controls/textfield/textfield.h"
34 #include "ui/views/layout/grid_layout.h"
35 #include "ui/views/layout/layout_constants.h"
36 #include "ui/views/widget/widget.h"
37 #include "ui/views/window/dialog_client_view.h"
39 namespace chromeos {
41 namespace {
43 void ShillError(const std::string& function,
44 const std::string& error_name,
45 scoped_ptr<base::DictionaryValue> error_data) {
46 NET_LOG_ERROR("Shill Error from WimaxConfigView: " + error_name, function);
49 } // namespace
51 WimaxConfigView::WimaxConfigView(NetworkConfigView* parent,
52 const std::string& service_path)
53 : ChildNetworkConfigView(parent, service_path),
54 identity_label_(NULL),
55 identity_textfield_(NULL),
56 save_credentials_checkbox_(NULL),
57 share_network_checkbox_(NULL),
58 passphrase_label_(NULL),
59 passphrase_textfield_(NULL),
60 passphrase_visible_button_(NULL),
61 error_label_(NULL),
62 weak_ptr_factory_(this) {
63 Init();
66 WimaxConfigView::~WimaxConfigView() {
67 RemoveAllChildViews(true); // Destroy children before models
70 base::string16 WimaxConfigView::GetTitle() const {
71 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_JOIN_WIMAX_NETWORKS);
74 views::View* WimaxConfigView::GetInitiallyFocusedView() {
75 if (identity_textfield_ && identity_textfield_->enabled())
76 return identity_textfield_;
77 if (passphrase_textfield_ && passphrase_textfield_->enabled())
78 return passphrase_textfield_;
79 return NULL;
82 bool WimaxConfigView::CanLogin() {
83 // In OOBE it may be valid to log in with no credentials (crbug.com/137776).
84 if (!chromeos::StartupUtils::IsOobeCompleted())
85 return true;
87 // TODO(benchan): Update this with the correct minimum length (don't just
88 // check if empty).
89 // If the network requires a passphrase, make sure it is the right length.
90 return passphrase_textfield_ && !passphrase_textfield_->text().empty();
93 void WimaxConfigView::UpdateDialogButtons() {
94 parent_->GetDialogClientView()->UpdateDialogButtons();
97 void WimaxConfigView::UpdateErrorLabel() {
98 base::string16 error_msg;
99 if (!service_path_.empty()) {
100 const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
101 GetNetworkState(service_path_);
102 if (wimax && wimax->connection_state() == shill::kStateFailure)
103 error_msg = ui::NetworkConnect::Get()->GetShillErrorString(
104 wimax->last_error(), wimax->path());
106 if (!error_msg.empty()) {
107 error_label_->SetText(error_msg);
108 error_label_->SetVisible(true);
109 } else {
110 error_label_->SetVisible(false);
114 void WimaxConfigView::ContentsChanged(views::Textfield* sender,
115 const base::string16& new_contents) {
116 UpdateDialogButtons();
119 bool WimaxConfigView::HandleKeyEvent(views::Textfield* sender,
120 const ui::KeyEvent& key_event) {
121 if (sender == passphrase_textfield_ &&
122 key_event.key_code() == ui::VKEY_RETURN) {
123 parent_->GetDialogClientView()->AcceptWindow();
125 return false;
128 void WimaxConfigView::ButtonPressed(views::Button* sender,
129 const ui::Event& event) {
130 if (sender == passphrase_visible_button_ && passphrase_textfield_) {
131 if (passphrase_textfield_->GetTextInputType() == ui::TEXT_INPUT_TYPE_TEXT) {
132 passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
133 passphrase_visible_button_->SetToggled(false);
134 } else {
135 passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
136 passphrase_visible_button_->SetToggled(true);
138 } else {
139 NOTREACHED();
143 bool WimaxConfigView::Login() {
144 const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
145 GetNetworkState(service_path_);
146 if (!wimax) {
147 // Shill no longer knows about this network (edge case).
148 // TODO(stevenjb): Add notification for this.
149 NET_LOG_ERROR("Network not found", service_path_);
150 return true; // Close dialog
152 base::DictionaryValue properties;
153 properties.SetStringWithoutPathExpansion(
154 shill::kEapIdentityProperty, GetEapIdentity());
155 properties.SetStringWithoutPathExpansion(
156 shill::kEapPasswordProperty, GetEapPassphrase());
157 properties.SetBooleanWithoutPathExpansion(
158 shill::kSaveCredentialsProperty, GetSaveCredentials());
160 const bool share_default = true;
161 bool share_network = GetShareNetwork(share_default);
163 bool only_policy_autoconnect =
164 onc::PolicyAllowsOnlyPolicyNetworksToAutoconnect(!share_network);
165 if (only_policy_autoconnect) {
166 properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty,
167 false);
170 ui::NetworkConnect::Get()->ConfigureNetworkAndConnect(
171 service_path_, properties, share_network);
172 return true; // dialog will be closed
175 std::string WimaxConfigView::GetEapIdentity() const {
176 DCHECK(identity_textfield_);
177 return base::UTF16ToUTF8(identity_textfield_->text());
180 std::string WimaxConfigView::GetEapPassphrase() const {
181 return passphrase_textfield_ ? base::UTF16ToUTF8(
182 passphrase_textfield_->text()) :
183 std::string();
186 bool WimaxConfigView::GetSaveCredentials() const {
187 return save_credentials_checkbox_ ? save_credentials_checkbox_->checked() :
188 false;
191 bool WimaxConfigView::GetShareNetwork(bool share_default) const {
192 return share_network_checkbox_ ? share_network_checkbox_->checked() :
193 share_default;
196 void WimaxConfigView::Cancel() {
199 void WimaxConfigView::Init() {
200 const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
201 GetNetworkState(service_path_);
202 DCHECK(wimax && wimax->type() == shill::kTypeWimax);
204 WifiConfigView::ParseEAPUIProperty(
205 &save_credentials_ui_data_, wimax, ::onc::eap::kSaveCredentials);
206 WifiConfigView::ParseEAPUIProperty(
207 &identity_ui_data_, wimax, ::onc::eap::kIdentity);
208 WifiConfigView::ParseUIProperty(
209 &passphrase_ui_data_, wimax, ::onc::wifi::kPassphrase);
211 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
212 SetLayoutManager(layout);
214 const int column_view_set_id = 0;
215 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
216 const int kPasswordVisibleWidth = 20;
217 // Label
218 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
219 views::GridLayout::USE_PREF, 0, 0);
220 column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
221 // Textfield, combobox.
222 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
223 views::GridLayout::USE_PREF, 0,
224 ChildNetworkConfigView::kInputFieldMinWidth);
225 column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
226 // Password visible button / policy indicator.
227 column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1,
228 views::GridLayout::USE_PREF, 0, kPasswordVisibleWidth);
230 // Network name
231 layout->StartRow(0, column_view_set_id);
232 layout->AddView(new views::Label(l10n_util::GetStringUTF16(
233 IDS_OPTIONS_SETTINGS_INTERNET_TAB_NETWORK)));
234 views::Label* label = new views::Label(base::UTF8ToUTF16(wimax->name()));
235 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
236 layout->AddView(label);
237 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
239 // Identity
240 layout->StartRow(0, column_view_set_id);
241 base::string16 identity_label_text = l10n_util::GetStringUTF16(
242 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_IDENTITY);
243 identity_label_ = new views::Label(identity_label_text);
244 layout->AddView(identity_label_);
245 identity_textfield_ = new views::Textfield();
246 identity_textfield_->SetAccessibleName(identity_label_text);
247 identity_textfield_->set_controller(this);
248 identity_textfield_->SetEnabled(identity_ui_data_.IsEditable());
249 layout->AddView(identity_textfield_);
250 layout->AddView(new ControlledSettingIndicatorView(identity_ui_data_));
251 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
253 // Passphrase input
254 layout->StartRow(0, column_view_set_id);
255 base::string16 passphrase_label_text = l10n_util::GetStringUTF16(
256 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE);
257 passphrase_label_ = new views::Label(passphrase_label_text);
258 layout->AddView(passphrase_label_);
259 passphrase_textfield_ = new views::Textfield();
260 passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
261 passphrase_textfield_->set_controller(this);
262 passphrase_label_->SetEnabled(true);
263 passphrase_textfield_->SetEnabled(passphrase_ui_data_.IsEditable());
264 passphrase_textfield_->SetAccessibleName(passphrase_label_text);
265 layout->AddView(passphrase_textfield_);
267 if (passphrase_ui_data_.IsManaged()) {
268 layout->AddView(new ControlledSettingIndicatorView(passphrase_ui_data_));
269 } else {
270 // Password visible button.
271 passphrase_visible_button_ = new views::ToggleImageButton(this);
272 passphrase_visible_button_->SetFocusable(true);
273 passphrase_visible_button_->SetTooltipText(
274 l10n_util::GetStringUTF16(
275 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_SHOW));
276 passphrase_visible_button_->SetToggledTooltipText(
277 l10n_util::GetStringUTF16(
278 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_HIDE));
279 passphrase_visible_button_->SetImage(
280 views::ImageButton::STATE_NORMAL,
281 ResourceBundle::GetSharedInstance().
282 GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD));
283 passphrase_visible_button_->SetImage(
284 views::ImageButton::STATE_HOVERED,
285 ResourceBundle::GetSharedInstance().
286 GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD_HOVER));
287 passphrase_visible_button_->SetToggledImage(
288 views::ImageButton::STATE_NORMAL,
289 ResourceBundle::GetSharedInstance().
290 GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD));
291 passphrase_visible_button_->SetToggledImage(
292 views::ImageButton::STATE_HOVERED,
293 ResourceBundle::GetSharedInstance().
294 GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD_HOVER));
295 passphrase_visible_button_->SetImageAlignment(
296 views::ImageButton::ALIGN_CENTER, views::ImageButton::ALIGN_MIDDLE);
297 layout->AddView(passphrase_visible_button_);
300 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
302 // Checkboxes.
304 if (LoginState::Get()->IsUserAuthenticated()) {
305 // Save credentials
306 layout->StartRow(0, column_view_set_id);
307 save_credentials_checkbox_ = new views::Checkbox(
308 l10n_util::GetStringUTF16(
309 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SAVE_CREDENTIALS));
310 save_credentials_checkbox_->SetEnabled(
311 save_credentials_ui_data_.IsEditable());
312 layout->SkipColumns(1);
313 layout->AddView(save_credentials_checkbox_);
314 layout->AddView(
315 new ControlledSettingIndicatorView(save_credentials_ui_data_));
318 // Share network
319 if (wimax->profile_path().empty()) {
320 layout->StartRow(0, column_view_set_id);
321 share_network_checkbox_ = new views::Checkbox(
322 l10n_util::GetStringUTF16(
323 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHARE_NETWORK));
325 bool share_network_checkbox_value = false;
326 bool share_network_checkbox_enabled = false;
327 ChildNetworkConfigView::GetShareStateForLoginState(
328 &share_network_checkbox_value,
329 &share_network_checkbox_enabled);
331 share_network_checkbox_->SetChecked(share_network_checkbox_value);
332 share_network_checkbox_->SetEnabled(share_network_checkbox_enabled);
334 layout->SkipColumns(1);
335 layout->AddView(share_network_checkbox_);
337 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
339 // Create an error label.
340 layout->StartRow(0, column_view_set_id);
341 layout->SkipColumns(1);
342 error_label_ = new views::Label();
343 error_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
344 error_label_->SetEnabledColor(SK_ColorRED);
345 layout->AddView(error_label_);
347 UpdateErrorLabel();
349 if (wimax) {
350 NetworkHandler::Get()->network_configuration_handler()->GetShillProperties(
351 service_path_, base::Bind(&WimaxConfigView::InitFromProperties,
352 weak_ptr_factory_.GetWeakPtr()),
353 base::Bind(&ShillError, "GetProperties"));
357 void WimaxConfigView::InitFromProperties(
358 const std::string& service_path,
359 const base::DictionaryValue& properties) {
360 // EapIdentity
361 std::string eap_identity;
362 properties.GetStringWithoutPathExpansion(
363 shill::kEapIdentityProperty, &eap_identity);
364 identity_textfield_->SetText(base::UTF8ToUTF16(eap_identity));
366 // Save credentials
367 if (save_credentials_checkbox_) {
368 bool save_credentials = false;
369 properties.GetBooleanWithoutPathExpansion(
370 shill::kSaveCredentialsProperty, &save_credentials);
371 save_credentials_checkbox_->SetChecked(save_credentials);
375 void WimaxConfigView::InitFocus() {
376 views::View* view_to_focus = GetInitiallyFocusedView();
377 if (view_to_focus)
378 view_to_focus->RequestFocus();
381 } // namespace chromeos