Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / options / wimax_config_view.cc
blob2fe342f6a2d37d2e5a7b84c56e924be4f7033143
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 "ash/system/chromeos/network/network_connect.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 "chromeos/login/login_state.h"
16 #include "chromeos/network/network_configuration_handler.h"
17 #include "chromeos/network/network_event_log.h"
18 #include "chromeos/network/network_profile.h"
19 #include "chromeos/network/network_profile_handler.h"
20 #include "chromeos/network/network_state.h"
21 #include "chromeos/network/network_state_handler.h"
22 #include "components/onc/onc_constants.h"
23 #include "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "grit/locale_settings.h"
26 #include "grit/theme_resources.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/events/event.h"
31 #include "ui/views/controls/button/checkbox.h"
32 #include "ui/views/controls/button/image_button.h"
33 #include "ui/views/controls/label.h"
34 #include "ui/views/controls/textfield/textfield.h"
35 #include "ui/views/layout/grid_layout.h"
36 #include "ui/views/layout/layout_constants.h"
37 #include "ui/views/widget/widget.h"
38 #include "ui/views/window/dialog_client_view.h"
40 namespace chromeos {
42 namespace {
44 void ShillError(const std::string& function,
45 const std::string& error_name,
46 scoped_ptr<base::DictionaryValue> error_data) {
47 NET_LOG_ERROR("Shill Error from WimaxConfigView: " + error_name, function);
50 } // namespace
52 WimaxConfigView::WimaxConfigView(NetworkConfigView* parent,
53 const std::string& service_path)
54 : ChildNetworkConfigView(parent, service_path),
55 identity_label_(NULL),
56 identity_textfield_(NULL),
57 save_credentials_checkbox_(NULL),
58 share_network_checkbox_(NULL),
59 shared_network_label_(NULL),
60 passphrase_label_(NULL),
61 passphrase_textfield_(NULL),
62 passphrase_visible_button_(NULL),
63 error_label_(NULL),
64 weak_ptr_factory_(this) {
65 Init();
68 WimaxConfigView::~WimaxConfigView() {
69 RemoveAllChildViews(true); // Destroy children before models
72 base::string16 WimaxConfigView::GetTitle() const {
73 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_JOIN_WIMAX_NETWORKS);
76 views::View* WimaxConfigView::GetInitiallyFocusedView() {
77 if (identity_textfield_ && identity_textfield_->enabled())
78 return identity_textfield_;
79 if (passphrase_textfield_ && passphrase_textfield_->enabled())
80 return passphrase_textfield_;
81 return NULL;
84 bool WimaxConfigView::CanLogin() {
85 // In OOBE it may be valid to log in with no credentials (crbug.com/137776).
86 if (!chromeos::StartupUtils::IsOobeCompleted())
87 return true;
89 // TODO(benchan): Update this with the correct minimum length (don't just
90 // check if empty).
91 // If the network requires a passphrase, make sure it is the right length.
92 return passphrase_textfield_ && !passphrase_textfield_->text().empty();
95 void WimaxConfigView::UpdateDialogButtons() {
96 parent_->GetDialogClientView()->UpdateDialogButtons();
99 void WimaxConfigView::UpdateErrorLabel() {
100 base::string16 error_msg;
101 if (!service_path_.empty()) {
102 const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
103 GetNetworkState(service_path_);
104 if (wimax && wimax->connection_state() == shill::kStateFailure)
105 error_msg = ash::network_connect::ErrorString(
106 wimax->error(), wimax->path());
108 if (!error_msg.empty()) {
109 error_label_->SetText(error_msg);
110 error_label_->SetVisible(true);
111 } else {
112 error_label_->SetVisible(false);
116 void WimaxConfigView::ContentsChanged(views::Textfield* sender,
117 const base::string16& new_contents) {
118 UpdateDialogButtons();
121 bool WimaxConfigView::HandleKeyEvent(views::Textfield* sender,
122 const ui::KeyEvent& key_event) {
123 if (sender == passphrase_textfield_ &&
124 key_event.key_code() == ui::VKEY_RETURN) {
125 parent_->GetDialogClientView()->AcceptWindow();
127 return false;
130 void WimaxConfigView::ButtonPressed(views::Button* sender,
131 const ui::Event& event) {
132 if (sender == passphrase_visible_button_ && passphrase_textfield_) {
133 if (passphrase_textfield_->GetTextInputType() == ui::TEXT_INPUT_TYPE_TEXT) {
134 passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
135 passphrase_visible_button_->SetToggled(false);
136 } else {
137 passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_TEXT);
138 passphrase_visible_button_->SetToggled(true);
140 } else {
141 NOTREACHED();
145 bool WimaxConfigView::Login() {
146 const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
147 GetNetworkState(service_path_);
148 if (!wimax) {
149 // Shill no longer knows about this network (edge case).
150 // TODO(stevenjb): Add notification for this.
151 NET_LOG_ERROR("Network not found", service_path_);
152 return true; // Close dialog
154 base::DictionaryValue properties;
155 properties.SetStringWithoutPathExpansion(
156 shill::kEapIdentityProperty, GetEapIdentity());
157 properties.SetStringWithoutPathExpansion(
158 shill::kEapPasswordProperty, GetEapPassphrase());
159 properties.SetBooleanWithoutPathExpansion(
160 shill::kSaveCredentialsProperty, GetSaveCredentials());
162 const bool share_default = true;
163 bool share_network = GetShareNetwork(share_default);
165 bool only_policy_autoconnect =
166 onc::PolicyAllowsOnlyPolicyNetworksToAutoconnect(!share_network);
167 if (only_policy_autoconnect) {
168 properties.SetBooleanWithoutPathExpansion(shill::kAutoConnectProperty,
169 false);
172 ash::network_connect::ConfigureNetworkAndConnect(
173 service_path_, properties, share_network);
174 return true; // dialog will be closed
177 std::string WimaxConfigView::GetEapIdentity() const {
178 DCHECK(identity_textfield_);
179 return base::UTF16ToUTF8(identity_textfield_->text());
182 std::string WimaxConfigView::GetEapPassphrase() const {
183 return passphrase_textfield_ ? base::UTF16ToUTF8(
184 passphrase_textfield_->text()) :
185 std::string();
188 bool WimaxConfigView::GetSaveCredentials() const {
189 return save_credentials_checkbox_ ? save_credentials_checkbox_->checked() :
190 false;
193 bool WimaxConfigView::GetShareNetwork(bool share_default) const {
194 return share_network_checkbox_ ? share_network_checkbox_->checked() :
195 share_default;
198 void WimaxConfigView::Cancel() {
201 void WimaxConfigView::Init() {
202 const NetworkState* wimax = NetworkHandler::Get()->network_state_handler()->
203 GetNetworkState(service_path_);
204 DCHECK(wimax && wimax->type() == shill::kTypeWimax);
206 WifiConfigView::ParseWiFiEAPUIProperty(
207 &save_credentials_ui_data_, wimax, ::onc::eap::kSaveCredentials);
208 WifiConfigView::ParseWiFiEAPUIProperty(
209 &identity_ui_data_, wimax, ::onc::eap::kIdentity);
210 WifiConfigView::ParseWiFiUIProperty(
211 &passphrase_ui_data_, wimax, ::onc::wifi::kPassphrase);
213 views::GridLayout* layout = views::GridLayout::CreatePanel(this);
214 SetLayoutManager(layout);
216 const int column_view_set_id = 0;
217 views::ColumnSet* column_set = layout->AddColumnSet(column_view_set_id);
218 const int kPasswordVisibleWidth = 20;
219 // Label
220 column_set->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 1,
221 views::GridLayout::USE_PREF, 0, 0);
222 column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
223 // Textfield, combobox.
224 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
225 views::GridLayout::USE_PREF, 0,
226 ChildNetworkConfigView::kInputFieldMinWidth);
227 column_set->AddPaddingColumn(0, views::kRelatedControlSmallHorizontalSpacing);
228 // Password visible button / policy indicator.
229 column_set->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL, 1,
230 views::GridLayout::USE_PREF, 0, kPasswordVisibleWidth);
232 // Network name
233 layout->StartRow(0, column_view_set_id);
234 layout->AddView(new views::Label(l10n_util::GetStringUTF16(
235 IDS_OPTIONS_SETTINGS_INTERNET_TAB_NETWORK)));
236 views::Label* label = new views::Label(base::UTF8ToUTF16(wimax->name()));
237 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
238 layout->AddView(label);
239 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
241 // Identity
242 layout->StartRow(0, column_view_set_id);
243 base::string16 identity_label_text = l10n_util::GetStringUTF16(
244 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_CERT_IDENTITY);
245 identity_label_ = new views::Label(identity_label_text);
246 layout->AddView(identity_label_);
247 identity_textfield_ = new views::Textfield();
248 identity_textfield_->SetAccessibleName(identity_label_text);
249 identity_textfield_->set_controller(this);
250 identity_textfield_->SetEnabled(identity_ui_data_.IsEditable());
251 layout->AddView(identity_textfield_);
252 layout->AddView(new ControlledSettingIndicatorView(identity_ui_data_));
253 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
255 // Passphrase input
256 layout->StartRow(0, column_view_set_id);
257 base::string16 passphrase_label_text = l10n_util::GetStringUTF16(
258 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE);
259 passphrase_label_ = new views::Label(passphrase_label_text);
260 layout->AddView(passphrase_label_);
261 passphrase_textfield_ = new views::Textfield();
262 passphrase_textfield_->SetTextInputType(ui::TEXT_INPUT_TYPE_PASSWORD);
263 passphrase_textfield_->set_controller(this);
264 passphrase_label_->SetEnabled(true);
265 passphrase_textfield_->SetEnabled(passphrase_ui_data_.IsEditable());
266 passphrase_textfield_->SetAccessibleName(passphrase_label_text);
267 layout->AddView(passphrase_textfield_);
269 if (passphrase_ui_data_.IsManaged()) {
270 layout->AddView(new ControlledSettingIndicatorView(passphrase_ui_data_));
271 } else {
272 // Password visible button.
273 passphrase_visible_button_ = new views::ToggleImageButton(this);
274 passphrase_visible_button_->SetFocusable(true);
275 passphrase_visible_button_->SetTooltipText(
276 l10n_util::GetStringUTF16(
277 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_SHOW));
278 passphrase_visible_button_->SetToggledTooltipText(
279 l10n_util::GetStringUTF16(
280 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PASSPHRASE_HIDE));
281 passphrase_visible_button_->SetImage(
282 views::ImageButton::STATE_NORMAL,
283 ResourceBundle::GetSharedInstance().
284 GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD));
285 passphrase_visible_button_->SetImage(
286 views::ImageButton::STATE_HOVERED,
287 ResourceBundle::GetSharedInstance().
288 GetImageSkiaNamed(IDR_NETWORK_SHOW_PASSWORD_HOVER));
289 passphrase_visible_button_->SetToggledImage(
290 views::ImageButton::STATE_NORMAL,
291 ResourceBundle::GetSharedInstance().
292 GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD));
293 passphrase_visible_button_->SetToggledImage(
294 views::ImageButton::STATE_HOVERED,
295 ResourceBundle::GetSharedInstance().
296 GetImageSkiaNamed(IDR_NETWORK_HIDE_PASSWORD_HOVER));
297 passphrase_visible_button_->SetImageAlignment(
298 views::ImageButton::ALIGN_CENTER, views::ImageButton::ALIGN_MIDDLE);
299 layout->AddView(passphrase_visible_button_);
302 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
304 // Checkboxes.
306 if (LoginState::Get()->IsUserAuthenticated()) {
307 // Save credentials
308 layout->StartRow(0, column_view_set_id);
309 save_credentials_checkbox_ = new views::Checkbox(
310 l10n_util::GetStringUTF16(
311 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SAVE_CREDENTIALS));
312 save_credentials_checkbox_->SetEnabled(
313 save_credentials_ui_data_.IsEditable());
314 layout->SkipColumns(1);
315 layout->AddView(save_credentials_checkbox_);
316 layout->AddView(
317 new ControlledSettingIndicatorView(save_credentials_ui_data_));
320 // Share network
321 if (wimax->profile_path().empty()) {
322 layout->StartRow(0, column_view_set_id);
323 share_network_checkbox_ = new views::Checkbox(
324 l10n_util::GetStringUTF16(
325 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_SHARE_NETWORK));
326 if (LoginState::Get()->IsUserAuthenticated()) {
327 share_network_checkbox_->SetChecked(false); // Default to unshared
328 share_network_checkbox_->SetEnabled(true);
329 } else {
330 // Not logged in, must be shared
331 share_network_checkbox_->SetChecked(true);
332 share_network_checkbox_->SetEnabled(false);
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()->GetProperties(
351 service_path_,
352 base::Bind(&WimaxConfigView::InitFromProperties,
353 weak_ptr_factory_.GetWeakPtr()),
354 base::Bind(&ShillError, "GetProperties"));
358 void WimaxConfigView::InitFromProperties(
359 const std::string& service_path,
360 const base::DictionaryValue& properties) {
361 // EapIdentity
362 std::string eap_identity;
363 properties.GetStringWithoutPathExpansion(
364 shill::kEapIdentityProperty, &eap_identity);
365 identity_textfield_->SetText(base::UTF8ToUTF16(eap_identity));
367 // Save credentials
368 if (save_credentials_checkbox_) {
369 bool save_credentials = false;
370 properties.GetBooleanWithoutPathExpansion(
371 shill::kSaveCredentialsProperty, &save_credentials);
372 save_credentials_checkbox_->SetChecked(save_credentials);
376 void WimaxConfigView::InitFocus() {
377 views::View* view_to_focus = GetInitiallyFocusedView();
378 if (view_to_focus)
379 view_to_focus->RequestFocus();
382 } // namespace chromeos