| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/status/network_menu.h" | 5 #include "chrome/browser/chromeos/status/network_menu.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 views::View* item = submenu->child_at(i); | 80 views::View* item = submenu->child_at(i); |
| 81 if (item->id() == views::MenuItemView::kMenuItemViewID) { | 81 if (item->id() == views::MenuItemView::kMenuItemViewID) { |
| 82 views::MenuItemView* menu_item = | 82 views::MenuItemView* menu_item = |
| 83 static_cast<views::MenuItemView*>(item); | 83 static_cast<views::MenuItemView*>(item); |
| 84 SetMenuMargins(menu_item, top, bottom); | 84 SetMenuMargins(menu_item, top, bottom); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Activate a cellular network. |
| 91 void ActivateCellular(const chromeos::CellularNetwork* cellular) { |
| 92 DCHECK(cellular); |
| 93 Browser* browser = BrowserList::GetLastActive(); |
| 94 if (!browser) |
| 95 return; |
| 96 browser->OpenMobilePlanTabAndActivate(); |
| 97 } |
| 98 |
| 90 } // namespace | 99 } // namespace |
| 91 | 100 |
| 92 namespace chromeos { | 101 namespace chromeos { |
| 93 | 102 |
| 94 class NetworkMenuModel : public ui::MenuModel { | 103 class NetworkMenuModel : public ui::MenuModel { |
| 95 public: | 104 public: |
| 96 struct MenuItem { | 105 struct MenuItem { |
| 97 MenuItem() | 106 MenuItem() |
| 98 : type(ui::MenuModel::TYPE_SEPARATOR), | 107 : type(ui::MenuModel::TYPE_SEPARATOR), |
| 99 sub_menu_model(NULL), | 108 sub_menu_model(NULL), |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 // Top up URL of the current carrier on empty string if there's none. | 204 // Top up URL of the current carrier on empty string if there's none. |
| 196 std::string top_up_url_; | 205 std::string top_up_url_; |
| 197 | 206 |
| 198 // Carrier ID which top up URL is initialized for. | 207 // Carrier ID which top up URL is initialized for. |
| 199 // Used to update top up URL only when cellular carrier has changed. | 208 // Used to update top up URL only when cellular carrier has changed. |
| 200 std::string carrier_id_; | 209 std::string carrier_id_; |
| 201 | 210 |
| 202 private: | 211 private: |
| 203 // Show a NetworkConfigView modal dialog instance. | 212 // Show a NetworkConfigView modal dialog instance. |
| 204 void ShowNetworkConfigView(NetworkConfigView* view) const; | 213 void ShowNetworkConfigView(NetworkConfigView* view) const; |
| 205 // Activate a cellular network. | |
| 206 void ActivateCellular(const CellularNetwork* cellular) const; | |
| 207 // Open a dialog to set up and connect to a network. | 214 // Open a dialog to set up and connect to a network. |
| 208 void ShowOther(ConnectionType type) const; | 215 void ShowOther(ConnectionType type) const; |
| 209 | 216 |
| 210 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel); | 217 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel); |
| 211 }; | 218 }; |
| 212 | 219 |
| 213 | |
| 214 class MoreMenuModel : public NetworkMenuModel { | 220 class MoreMenuModel : public NetworkMenuModel { |
| 215 public: | 221 public: |
| 216 explicit MoreMenuModel(const base::WeakPtr<NetworkMenu> owner) | 222 explicit MoreMenuModel(const base::WeakPtr<NetworkMenu> owner) |
| 217 : NetworkMenuModel(owner) {} | 223 : NetworkMenuModel(owner) {} |
| 218 virtual ~MoreMenuModel() {} | 224 virtual ~MoreMenuModel() {} |
| 219 | 225 |
| 220 // NetworkMenuModel implementation. | 226 // NetworkMenuModel implementation. |
| 221 virtual void InitMenuItems(bool should_open_button_options) OVERRIDE; | 227 virtual void InitMenuItems(bool should_open_button_options) OVERRIDE; |
| 222 | 228 |
| 223 // ui::MenuModel implementation | 229 // ui::MenuModel implementation |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 int auto_connect) const { | 280 int auto_connect) const { |
| 275 int flags = menu_items_[index].flags; | 281 int flags = menu_items_[index].flags; |
| 276 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 282 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 277 const std::string& service_path = menu_items_[index].service_path; | 283 const std::string& service_path = menu_items_[index].service_path; |
| 278 if (flags & FLAG_WIFI) { | 284 if (flags & FLAG_WIFI) { |
| 279 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); | 285 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); |
| 280 if (wifi) { | 286 if (wifi) { |
| 281 // Connect or reconnect. | 287 // Connect or reconnect. |
| 282 if (auto_connect >= 0) | 288 if (auto_connect >= 0) |
| 283 wifi->SetAutoConnect(auto_connect ? true : false); | 289 wifi->SetAutoConnect(auto_connect ? true : false); |
| 284 if (wifi->connecting_or_connected()) { | 290 owner_->ConnectToNetwork(wifi); |
| 285 // Show the config settings for the active network. | |
| 286 owner_->ShowTabbedNetworkSettings(wifi); | |
| 287 } else { | |
| 288 wifi->AttemptConnection(base::Bind(&NetworkMenu::DoConnect, | |
| 289 owner_, | |
| 290 wifi)); | |
| 291 } | |
| 292 } else { | 291 } else { |
| 293 // If we are attempting to connect to a network that no longer exists, | 292 // If we are attempting to connect to a network that no longer exists, |
| 294 // display a notification. | 293 // display a notification. |
| 295 LOG(WARNING) << "Wi-fi network does not exist to connect to: " | 294 LOG(WARNING) << "Wi-fi network does not exist to connect to: " |
| 296 << service_path; | 295 << service_path; |
| 297 // TODO(stevenjb): Show notification. | 296 // TODO(stevenjb): Show notification. |
| 298 } | 297 } |
| 299 } else if (flags & FLAG_CELLULAR) { | 298 } else if (flags & FLAG_CELLULAR) { |
| 300 CellularNetwork* cellular = cros->FindCellularNetworkByPath( | 299 CellularNetwork* cellular = cros->FindCellularNetworkByPath( |
| 301 service_path); | 300 service_path); |
| 302 if (cellular) { | 301 if (cellular) { |
| 303 if ((cellular->activation_state() != ACTIVATION_STATE_ACTIVATED && | 302 owner_->ConnectToNetwork(cellular); |
| 304 cellular->activation_state() != ACTIVATION_STATE_UNKNOWN) || | |
| 305 cellular->needs_new_plan()) { | |
| 306 ActivateCellular(cellular); | |
| 307 } else if (cellular->connecting_or_connected()) { | |
| 308 // Cellular network is connecting or connected, | |
| 309 // so we show the config settings for the cellular network. | |
| 310 owner_->ShowTabbedNetworkSettings(cellular); | |
| 311 } else { | |
| 312 // Clicked on a disconnected cellular network, so connect to it. | |
| 313 cros->ConnectToCellularNetwork(cellular); | |
| 314 } | |
| 315 } else { | 303 } else { |
| 316 // If we are attempting to connect to a network that no longer exists, | 304 // If we are attempting to connect to a network that no longer exists, |
| 317 // display a notification. | 305 // display a notification. |
| 318 LOG(WARNING) << "Cellular network does not exist to connect to: " | 306 LOG(WARNING) << "Cellular network does not exist to connect to: " |
| 319 << service_path; | 307 << service_path; |
| 320 // TODO(stevenjb): Show notification. | 308 // TODO(stevenjb): Show notification. |
| 321 } | 309 } |
| 322 } else if (flags & FLAG_ADD_WIFI) { | 310 } else if (flags & FLAG_ADD_WIFI) { |
| 323 ShowOther(TYPE_WIFI); | 311 ShowOther(TYPE_WIFI); |
| 324 } else if (flags & FLAG_ADD_CELLULAR) { | 312 } else if (flags & FLAG_ADD_CELLULAR) { |
| 325 ShowOther(TYPE_CELLULAR); | 313 ShowOther(TYPE_CELLULAR); |
| 326 } else if (flags & FLAG_ADD_VPN) { | 314 } else if (flags & FLAG_ADD_VPN) { |
| 327 ShowOther(TYPE_VPN); | 315 ShowOther(TYPE_VPN); |
| 328 } else if (flags & FLAG_VPN) { | 316 } else if (flags & FLAG_VPN) { |
| 329 VirtualNetwork* vpn = cros->FindVirtualNetworkByPath(service_path); | 317 VirtualNetwork* vpn = cros->FindVirtualNetworkByPath(service_path); |
| 330 if (vpn) { | 318 if (vpn) { |
| 331 // Connect or reconnect. | 319 owner_->ConnectToNetwork(vpn); |
| 332 if (vpn->connecting_or_connected()) { | |
| 333 // Show the config settings for the connected network. | |
| 334 if (cros->connected_network()) | |
| 335 owner_->ShowTabbedNetworkSettings(cros->connected_network()); | |
| 336 } else { | |
| 337 vpn->AttemptConnection(base::Bind(&NetworkMenu::DoConnect, | |
| 338 owner_, | |
| 339 vpn)); | |
| 340 } | |
| 341 } else { | 320 } else { |
| 342 // If we are attempting to connect to a network that no longer exists, | 321 // If we are attempting to connect to a network that no longer exists, |
| 343 // display a notification. | 322 // display a notification. |
| 344 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; | 323 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; |
| 345 // TODO(stevenjb): Show notification. | 324 // TODO(stevenjb): Show notification. |
| 346 } | 325 } |
| 347 } | 326 } |
| 348 } | 327 } |
| 349 | 328 |
| 329 void NetworkMenu::ConnectToNetwork(Network* network) { |
| 330 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 331 switch (network->type()) { |
| 332 case TYPE_ETHERNET: { |
| 333 ShowTabbedNetworkSettings(network); |
| 334 break; |
| 335 } |
| 336 case TYPE_WIFI: { |
| 337 WifiNetwork* wifi = static_cast<WifiNetwork*>(network); |
| 338 if (wifi->connecting_or_connected()) { |
| 339 ShowTabbedNetworkSettings(wifi); |
| 340 } else { |
| 341 wifi->AttemptConnection(base::Bind(&NetworkMenu::DoConnect, |
| 342 weak_pointer_factory_.GetWeakPtr(), |
| 343 wifi)); |
| 344 } |
| 345 break; |
| 346 } |
| 347 |
| 348 case TYPE_CELLULAR: { |
| 349 CellularNetwork* cell = static_cast<CellularNetwork*>(network); |
| 350 if (cell->NeedsActivation()) { |
| 351 ActivateCellular(cell); |
| 352 } else if (cell->connecting_or_connected()) { |
| 353 // Cellular network is connecting or connected, |
| 354 // so we show the config settings for the cellular network. |
| 355 ShowTabbedNetworkSettings(cell); |
| 356 } else { |
| 357 // Clicked on a disconnected cellular network, so connect to it. |
| 358 cros->ConnectToCellularNetwork(cell); |
| 359 } |
| 360 break; |
| 361 } |
| 362 |
| 363 case TYPE_VPN: { |
| 364 VirtualNetwork* vpn = static_cast<VirtualNetwork*>(network); |
| 365 // Connect or reconnect. |
| 366 if (vpn->connecting_or_connected()) { |
| 367 ShowTabbedNetworkSettings(vpn); |
| 368 } else { |
| 369 vpn->AttemptConnection(base::Bind(&NetworkMenu::DoConnect, |
| 370 weak_pointer_factory_.GetWeakPtr(), |
| 371 vpn)); |
| 372 } |
| 373 break; |
| 374 } |
| 375 |
| 376 default: |
| 377 break; |
| 378 } |
| 379 } |
| 380 |
| 350 //////////////////////////////////////////////////////////////////////////////// | 381 //////////////////////////////////////////////////////////////////////////////// |
| 351 // NetworkMenuModel, ui::MenuModel implementation: | 382 // NetworkMenuModel, ui::MenuModel implementation: |
| 352 | 383 |
| 353 bool NetworkMenuModel::HasIcons() const { | 384 bool NetworkMenuModel::HasIcons() const { |
| 354 return true; | 385 return true; |
| 355 } | 386 } |
| 356 | 387 |
| 357 int NetworkMenuModel::GetItemCount() const { | 388 int NetworkMenuModel::GetItemCount() const { |
| 358 return static_cast<int>(menu_items_.size()); | 389 return static_cast<int>(menu_items_.size()); |
| 359 } | 390 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 } else if (cellular->sim_lock_state() == SIM_UNLOCKED || | 475 } else if (cellular->sim_lock_state() == SIM_UNLOCKED || |
| 445 cellular->sim_lock_state() == SIM_UNKNOWN) { | 476 cellular->sim_lock_state() == SIM_UNKNOWN) { |
| 446 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); | 477 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); |
| 447 } else { | 478 } else { |
| 448 SimDialogDelegate::ShowDialog(owner_->delegate()->GetNativeWindow(), | 479 SimDialogDelegate::ShowDialog(owner_->delegate()->GetNativeWindow(), |
| 449 SimDialogDelegate::SIM_DIALOG_UNLOCK); | 480 SimDialogDelegate::SIM_DIALOG_UNLOCK); |
| 450 } | 481 } |
| 451 } else if (flags & FLAG_TOGGLE_OFFLINE) { | 482 } else if (flags & FLAG_TOGGLE_OFFLINE) { |
| 452 cros->EnableOfflineMode(!cros->offline_mode()); | 483 cros->EnableOfflineMode(!cros->offline_mode()); |
| 453 } else if (flags & FLAG_ETHERNET) { | 484 } else if (flags & FLAG_ETHERNET) { |
| 454 if (cros->ethernet_connected()) { | 485 if (cros->ethernet_connected()) |
| 455 owner_->ShowTabbedNetworkSettings(cros->ethernet_network()); | 486 owner_->ShowTabbedNetworkSettings(cros->ethernet_network()); |
| 456 } | |
| 457 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | | 487 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | |
| 458 FLAG_CELLULAR | FLAG_ADD_CELLULAR | | 488 FLAG_CELLULAR | FLAG_ADD_CELLULAR | |
| 459 FLAG_VPN | FLAG_ADD_VPN)) { | 489 FLAG_VPN | FLAG_ADD_VPN)) { |
| 460 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 490 ConnectToNetworkAt(index, std::string(), std::string(), -1); |
| 461 } else if (flags & FLAG_DISCONNECT_VPN) { | 491 } else if (flags & FLAG_DISCONNECT_VPN) { |
| 462 const VirtualNetwork* active_vpn = cros->virtual_network(); | 492 const VirtualNetwork* active_vpn = cros->virtual_network(); |
| 463 if (active_vpn) | 493 if (active_vpn) |
| 464 cros->DisconnectFromNetwork(active_vpn); | 494 cros->DisconnectFromNetwork(active_vpn); |
| 465 } else if (flags & FLAG_VIEW_ACCOUNT) { | 495 } else if (flags & FLAG_VIEW_ACCOUNT) { |
| 466 Browser* browser = BrowserList::GetLastActive(); | 496 Browser* browser = BrowserList::GetLastActive(); |
| 467 if (browser) | 497 if (browser) |
| 468 browser->ShowSingletonTab(GURL(top_up_url_)); | 498 browser->ShowSingletonTab(GURL(top_up_url_)); |
| 469 } | 499 } |
| 470 } | 500 } |
| 471 | 501 |
| 472 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) { | 502 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) { |
| 473 } | 503 } |
| 474 | 504 |
| 475 //////////////////////////////////////////////////////////////////////////////// | 505 //////////////////////////////////////////////////////////////////////////////// |
| 476 // NetworkMenuModel, private methods: | 506 // NetworkMenuModel, private methods: |
| 477 | 507 |
| 478 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { | 508 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { |
| 479 views::Widget* window = browser::CreateViewsWindow( | 509 views::Widget* window = browser::CreateViewsWindow( |
| 480 owner_->delegate()->GetNativeWindow(), view, STYLE_GENERIC); | 510 owner_->delegate()->GetNativeWindow(), view, STYLE_GENERIC); |
| 481 window->SetAlwaysOnTop(true); | 511 window->SetAlwaysOnTop(true); |
| 482 window->Show(); | 512 window->Show(); |
| 483 } | 513 } |
| 484 | 514 |
| 485 void NetworkMenuModel::ActivateCellular(const CellularNetwork* cellular) const { | |
| 486 DCHECK(cellular); | |
| 487 Browser* browser = BrowserList::GetLastActive(); | |
| 488 if (!browser) | |
| 489 return; | |
| 490 browser->OpenMobilePlanTabAndActivate(); | |
| 491 } | |
| 492 | |
| 493 void NetworkMenuModel::ShowOther(ConnectionType type) const { | 515 void NetworkMenuModel::ShowOther(ConnectionType type) const { |
| 494 if (type == TYPE_CELLULAR) { | 516 if (type == TYPE_CELLULAR) { |
| 495 ChooseMobileNetworkDialog::ShowDialog( | 517 ChooseMobileNetworkDialog::ShowDialog( |
| 496 owner_->delegate()->GetNativeWindow()); | 518 owner_->delegate()->GetNativeWindow()); |
| 497 } else { | 519 } else { |
| 498 ShowNetworkConfigView(new NetworkConfigView(type)); | 520 ShowNetworkConfigView(new NetworkConfigView(type)); |
| 499 } | 521 } |
| 500 } | 522 } |
| 501 | 523 |
| 502 //////////////////////////////////////////////////////////////////////////////// | 524 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1033 delegate_->GetMenuButton(), bounds, views::MenuItemView::TOPRIGHT, | 1055 delegate_->GetMenuButton(), bounds, views::MenuItemView::TOPRIGHT, |
| 1034 views::MenuRunner::HAS_MNEMONICS) == views::MenuRunner::MENU_DELETED) | 1056 views::MenuRunner::HAS_MNEMONICS) == views::MenuRunner::MENU_DELETED) |
| 1035 return; | 1057 return; |
| 1036 } | 1058 } |
| 1037 | 1059 |
| 1038 void NetworkMenu::ShowTabbedNetworkSettings(const Network* network) const { | 1060 void NetworkMenu::ShowTabbedNetworkSettings(const Network* network) const { |
| 1039 DCHECK(network); | 1061 DCHECK(network); |
| 1040 Browser* browser = BrowserList::GetLastActive(); | 1062 Browser* browser = BrowserList::GetLastActive(); |
| 1041 if (!browser) | 1063 if (!browser) |
| 1042 return; | 1064 return; |
| 1065 |
| 1066 // In case of a VPN, show the config settings for the connected network. |
| 1067 if (network->type() == chromeos::TYPE_VPN) { |
| 1068 network = CrosLibrary::Get()->GetNetworkLibrary()->connected_network(); |
| 1069 if (!network) |
| 1070 return; |
| 1071 } |
| 1072 |
| 1043 std::string network_name(network->name()); | 1073 std::string network_name(network->name()); |
| 1044 if (network_name.empty() && network->type() == chromeos::TYPE_ETHERNET) { | 1074 if (network_name.empty() && network->type() == chromeos::TYPE_ETHERNET) { |
| 1045 network_name = l10n_util::GetStringUTF8( | 1075 network_name = l10n_util::GetStringUTF8( |
| 1046 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); | 1076 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); |
| 1047 } | 1077 } |
| 1048 std::string page = StringPrintf( | 1078 std::string page = StringPrintf( |
| 1049 "%s?servicePath=%s&networkType=%d&networkName=%s", | 1079 "%s?servicePath=%s&networkType=%d&networkName=%s", |
| 1050 chrome::kInternetOptionsSubPage, | 1080 chrome::kInternetOptionsSubPage, |
| 1051 net::EscapeUrlEncodedData(network->service_path(), true).c_str(), | 1081 net::EscapeUrlEncodedData(network->service_path(), true).c_str(), |
| 1052 network->type(), | 1082 network->type(), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1082 window->Show(); | 1112 window->Show(); |
| 1083 } else { | 1113 } else { |
| 1084 cros->ConnectToWifiNetwork(wifi); | 1114 cros->ConnectToWifiNetwork(wifi); |
| 1085 // Connection failures are responsible for updating the UI, including | 1115 // Connection failures are responsible for updating the UI, including |
| 1086 // reopening dialogs. | 1116 // reopening dialogs. |
| 1087 } | 1117 } |
| 1088 } | 1118 } |
| 1089 } | 1119 } |
| 1090 | 1120 |
| 1091 } // namespace chromeos | 1121 } // namespace chromeos |
| OLD | NEW |