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. 4 5 #include "chrome/browser/chromeos/options/network_config_view.h" 6 7 #include <algorithm> 8 9 #include "ash/shell.h" 10 #include "base/strings/string_util.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "chrome/browser/chromeos/cros/network_property_ui_data.h" 13 #include "chrome/browser/chromeos/login/login_display_host_impl.h" 14 #include "chrome/browser/chromeos/login/user.h" 15 #include "chrome/browser/chromeos/options/vpn_config_view.h" 16 #include "chrome/browser/chromeos/options/wifi_config_view.h" 17 #include "chrome/browser/chromeos/options/wimax_config_view.h" 18 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser_finder.h" 21 #include "chrome/browser/ui/browser_window.h" 22 #include "chrome/browser/ui/host_desktop.h" 23 #include "chromeos/network/network_state.h" 24 #include "chromeos/network/network_state_handler.h" 25 #include "grit/chromium_strings.h" 26 #include "grit/generated_resources.h" 27 #include "grit/locale_settings.h" 28 #include "grit/theme_resources.h" 29 #include "ui/aura/root_window.h" 30 #include "ui/base/accessibility/accessible_view_state.h" 31 #include "ui/base/l10n/l10n_util.h" 32 #include "ui/base/resource/resource_bundle.h" 33 #include "ui/gfx/image/image.h" 34 #include "ui/gfx/rect.h" 35 #include "ui/views/controls/button/label_button.h" 36 #include "ui/views/controls/image_view.h" 37 #include "ui/views/layout/layout_constants.h" 38 #include "ui/views/widget/widget.h" 39 40 using views::Widget; 41 42 namespace { 43 44 gfx::NativeWindow GetDialogParent() { 45 if (chromeos::LoginDisplayHostImpl::default_host()) { 46 return chromeos::LoginDisplayHostImpl::default_host()->GetNativeWindow(); 47 } else { 48 Browser* browser = chrome::FindTabbedBrowser( 49 ProfileManager::GetDefaultProfileOrOffTheRecord(), 50 true, 51 chrome::HOST_DESKTOP_TYPE_ASH); 52 if (browser) 53 return browser->window()->GetNativeWindow(); 54 } 55 return NULL; 56 } 57 58 // Avoid global static initializer. 59 chromeos::NetworkConfigView** GetActiveDialogPointer() { 60 static chromeos::NetworkConfigView* active_dialog = NULL; 61 return &active_dialog; 62 } 63 64 chromeos::NetworkConfigView* GetActiveDialog() { 65 return *(GetActiveDialogPointer()); 66 } 67 68 void SetActiveDialog(chromeos::NetworkConfigView* dialog) { 69 *(GetActiveDialogPointer()) = dialog; 70 } 71 72 } // namespace 73 74 namespace chromeos { 75 76 // static 77 const int ChildNetworkConfigView::kInputFieldMinWidth = 270; 78 79 NetworkConfigView::NetworkConfigView() 80 : child_config_view_(NULL), 81 delegate_(NULL), 82 advanced_button_(NULL) { 83 DCHECK(GetActiveDialog() == NULL); 84 SetActiveDialog(this); 85 } 86 87 void NetworkConfigView::InitWithNetworkState(const NetworkState* network) { 88 DCHECK(network); 89 std::string service_path = network->path(); 90 if (network->type() == flimflam::kTypeWifi) 91 child_config_view_ = new WifiConfigView(this, service_path, false); 92 else if (network->type() == flimflam::kTypeWimax) 93 child_config_view_ = new WimaxConfigView(this, service_path); 94 else if (network->type() == flimflam::kTypeVPN) 95 child_config_view_ = new VPNConfigView(this, service_path); 96 else 97 NOTREACHED(); 98 } 99 100 void NetworkConfigView::InitWithType(const std::string& type) { 101 if (type == flimflam::kTypeWifi) { 102 child_config_view_ = new WifiConfigView(this, 103 "" /* service_path */, 104 false /* show_8021x */); 105 advanced_button_ = new views::LabelButton(this, l10n_util::GetStringUTF16( 106 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_ADVANCED_BUTTON)); 107 advanced_button_->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 108 } else if (type == flimflam::kTypeVPN) { 109 child_config_view_ = new VPNConfigView(this, 110 "" /* service_path */); 111 } else { 112 NOTREACHED(); 113 } 114 } 115 116 NetworkConfigView::~NetworkConfigView() { 117 DCHECK(GetActiveDialog() == this); 118 SetActiveDialog(NULL); 119 } 120 121 // static 122 void NetworkConfigView::Show(const std::string& service_path, 123 gfx::NativeWindow parent) { 124 if (GetActiveDialog() != NULL) 125 return; 126 NetworkConfigView* view = new NetworkConfigView(); 127 const NetworkState* network = NetworkHandler::Get()->network_state_handler()-> 128 GetNetworkState(service_path); 129 if (!network) { 130 LOG(ERROR) << "NetworkConfigView::Show called with invalid service_path"; 131 return; 132 } 133 view->InitWithNetworkState(network); 134 view->ShowDialog(parent); 135 } 136 137 // static 138 void NetworkConfigView::ShowForType(const std::string& type, 139 gfx::NativeWindow parent) { 140 if (GetActiveDialog() != NULL) 141 return; 142 NetworkConfigView* view = new NetworkConfigView(); 143 view->InitWithType(type); 144 view->ShowDialog(parent); 145 } 146 147 gfx::NativeWindow NetworkConfigView::GetNativeWindow() const { 148 return GetWidget()->GetNativeWindow(); 149 } 150 151 string16 NetworkConfigView::GetDialogButtonLabel( 152 ui::DialogButton button) const { 153 if (button == ui::DIALOG_BUTTON_OK) 154 return l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_CONNECT); 155 return views::DialogDelegateView::GetDialogButtonLabel(button); 156 } 157 158 bool NetworkConfigView::IsDialogButtonEnabled(ui::DialogButton button) const { 159 // Disable connect button if cannot login. 160 if (button == ui::DIALOG_BUTTON_OK) 161 return child_config_view_->CanLogin(); 162 return true; 163 } 164 165 bool NetworkConfigView::Cancel() { 166 if (delegate_) 167 delegate_->OnDialogCancelled(); 168 child_config_view_->Cancel(); 169 return true; 170 } 171 172 bool NetworkConfigView::Accept() { 173 // Do not attempt login if it is guaranteed to fail, keep the dialog open. 174 if (!child_config_view_->CanLogin()) 175 return false; 176 bool result = child_config_view_->Login(); 177 if (result && delegate_) 178 delegate_->OnDialogAccepted(); 179 return result; 180 } 181 182 views::View* NetworkConfigView::CreateExtraView() { 183 return advanced_button_; 184 } 185 186 views::View* NetworkConfigView::GetInitiallyFocusedView() { 187 return child_config_view_->GetInitiallyFocusedView(); 188 } 189 190 string16 NetworkConfigView::GetWindowTitle() const { 191 DCHECK(!child_config_view_->GetTitle().empty()); 192 return child_config_view_->GetTitle(); 193 } 194 195 ui::ModalType NetworkConfigView::GetModalType() const { 196 return ui::MODAL_TYPE_SYSTEM; 197 } 198 199 void NetworkConfigView::GetAccessibleState(ui::AccessibleViewState* state) { 200 state->name = 201 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_OTHER_WIFI_NETWORKS); 202 state->role = ui::AccessibilityTypes::ROLE_DIALOG; 203 } 204 205 void NetworkConfigView::ButtonPressed(views::Button* sender, 206 const ui::Event& event) { 207 if (advanced_button_ && sender == advanced_button_) { 208 advanced_button_->SetVisible(false); 209 ShowAdvancedView(); 210 } 211 } 212 213 void NetworkConfigView::ShowAdvancedView() { 214 // Clear out the old widgets and build new ones. 215 RemoveChildView(child_config_view_); 216 delete child_config_view_; 217 // For now, there is only an advanced view for Wi-Fi 802.1X. 218 child_config_view_ = new WifiConfigView(this, 219 "" /* service_path */, 220 true /* show_8021x */); 221 AddChildView(child_config_view_); 222 // Resize the window to be able to hold the new widgets. 223 gfx::Size size = views::Widget::GetLocalizedContentsSize( 224 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_WIDTH_CHARS, 225 IDS_JOIN_WIFI_NETWORK_DIALOG_ADVANCED_MINIMUM_HEIGHT_LINES); 226 // Get the new bounds with desired size at the same center point. 227 gfx::Rect bounds = GetWidget()->GetWindowBoundsInScreen(); 228 int horiz_padding = bounds.width() - size.width(); 229 int vert_padding = bounds.height() - size.height(); 230 bounds.Inset(horiz_padding / 2, vert_padding / 2, 231 horiz_padding / 2, vert_padding / 2); 232 GetWidget()->SetBoundsConstrained(bounds); 233 Layout(); 234 child_config_view_->InitFocus(); 235 } 236 237 void NetworkConfigView::Layout() { 238 child_config_view_->SetBounds(0, 0, width(), height()); 239 } 240 241 gfx::Size NetworkConfigView::GetPreferredSize() { 242 gfx::Size result(views::Widget::GetLocalizedContentsSize( 243 IDS_JOIN_WIFI_NETWORK_DIALOG_WIDTH_CHARS, 244 IDS_JOIN_WIFI_NETWORK_DIALOG_MINIMUM_HEIGHT_LINES)); 245 gfx::Size size = child_config_view_->GetPreferredSize(); 246 result.set_height(size.height()); 247 if (size.width() > result.width()) 248 result.set_width(size.width()); 249 return result; 250 } 251 252 void NetworkConfigView::ViewHierarchyChanged( 253 const ViewHierarchyChangedDetails& details) { 254 // Can't init before we're inserted into a Container, because we require 255 // a HWND to parent native child controls to. 256 if (details.is_add && details.child == this) { 257 AddChildView(child_config_view_); 258 } 259 } 260 261 void NetworkConfigView::ShowDialog(gfx::NativeWindow parent) { 262 if (parent == NULL) 263 parent = GetDialogParent(); 264 // Failed connections may result in a pop-up with no natural parent window, 265 // so provide a fallback context on the active display. 266 gfx::NativeWindow context = parent ? NULL : ash::Shell::GetActiveRootWindow(); 267 Widget* window = DialogDelegate::CreateDialogWidget(this, context, parent); 268 window->SetAlwaysOnTop(true); 269 window->Show(); 270 } 271 272 // ChildNetworkConfigView 273 274 ChildNetworkConfigView::ChildNetworkConfigView( 275 NetworkConfigView* parent, 276 const std::string& service_path) 277 : parent_(parent), 278 service_path_(service_path) { 279 } 280 281 ChildNetworkConfigView::~ChildNetworkConfigView() { 282 } 283 284 // ControlledSettingIndicatorView 285 286 ControlledSettingIndicatorView::ControlledSettingIndicatorView() 287 : managed_(false), 288 image_view_(NULL) { 289 Init(); 290 } 291 292 ControlledSettingIndicatorView::ControlledSettingIndicatorView( 293 const NetworkPropertyUIData& ui_data) 294 : managed_(false), 295 image_view_(NULL) { 296 Init(); 297 Update(ui_data); 298 } 299 300 ControlledSettingIndicatorView::~ControlledSettingIndicatorView() {} 301 302 void ControlledSettingIndicatorView::Update( 303 const NetworkPropertyUIData& ui_data) { 304 if (managed_ == ui_data.IsManaged()) 305 return; 306 307 managed_ = ui_data.IsManaged(); 308 PreferredSizeChanged(); 309 } 310 311 gfx::Size ControlledSettingIndicatorView::GetPreferredSize() { 312 return (managed_ && visible()) ? image_view_->GetPreferredSize() 313 : gfx::Size(); 314 } 315 316 void ControlledSettingIndicatorView::Layout() { 317 image_view_->SetBounds(0, 0, width(), height()); 318 } 319 320 void ControlledSettingIndicatorView::OnMouseEntered( 321 const ui::MouseEvent& event) { 322 image_view_->SetImage(color_image_); 323 } 324 325 void ControlledSettingIndicatorView::OnMouseExited( 326 const ui::MouseEvent& event) { 327 image_view_->SetImage(gray_image_); 328 } 329 330 void ControlledSettingIndicatorView::Init() { 331 color_image_ = ResourceBundle::GetSharedInstance().GetImageNamed( 332 IDR_CONTROLLED_SETTING_MANDATORY).ToImageSkia(); 333 gray_image_ = ResourceBundle::GetSharedInstance().GetImageNamed( 334 IDR_CONTROLLED_SETTING_MANDATORY_GRAY).ToImageSkia(); 335 image_view_ = new views::ImageView(); 336 // Disable |image_view_| so mouse events propagate to the parent. 337 image_view_->SetEnabled(false); 338 image_view_->SetImage(gray_image_); 339 image_view_->SetTooltipText( 340 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_POLICY)); 341 AddChildView(image_view_); 342 } 343 344 } // namespace chromeos 345