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->activation_state() != ACTIVATION_STATE_ACTIVATED && | |
351 cell->activation_state() != ACTIVATION_STATE_UNKNOWN) || | |
352 cell->needs_new_plan()) { | |
stevenjb
2012/03/12 18:21:29
As long as we're cleaning this up, can you wrap th
sadrul
2012/03/12 18:32:25
Done.
| |
353 ActivateCellular(cell); | |
354 } else if (cell->connecting_or_connected()) { | |
355 // Cellular network is connecting or connected, | |
356 // so we show the config settings for the cellular network. | |
357 ShowTabbedNetworkSettings(cell); | |
358 } else { | |
359 // Clicked on a disconnected cellular network, so connect to it. | |
360 cros->ConnectToCellularNetwork(cell); | |
361 } | |
362 break; | |
363 } | |
364 | |
365 case TYPE_VPN: { | |
366 VirtualNetwork* vpn = static_cast<VirtualNetwork*>(network); | |
367 // Connect or reconnect. | |
368 if (vpn->connecting_or_connected()) { | |
369 // Show the config settings for the connected network. | |
370 if (cros->connected_network()) | |
371 ShowTabbedNetworkSettings(cros->connected_network()); | |
stevenjb
2012/03/12 18:21:29
Can you put the cros->connected_network() bit into
sadrul
2012/03/12 18:32:25
Done.
| |
372 } else { | |
373 vpn->AttemptConnection(base::Bind(&NetworkMenu::DoConnect, | |
374 weak_pointer_factory_.GetWeakPtr(), | |
375 vpn)); | |
376 } | |
377 break; | |
378 } | |
379 | |
380 default: | |
381 break; | |
382 } | |
383 } | |
384 | |
350 //////////////////////////////////////////////////////////////////////////////// | 385 //////////////////////////////////////////////////////////////////////////////// |
351 // NetworkMenuModel, ui::MenuModel implementation: | 386 // NetworkMenuModel, ui::MenuModel implementation: |
352 | 387 |
353 bool NetworkMenuModel::HasIcons() const { | 388 bool NetworkMenuModel::HasIcons() const { |
354 return true; | 389 return true; |
355 } | 390 } |
356 | 391 |
357 int NetworkMenuModel::GetItemCount() const { | 392 int NetworkMenuModel::GetItemCount() const { |
358 return static_cast<int>(menu_items_.size()); | 393 return static_cast<int>(menu_items_.size()); |
359 } | 394 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 } else if (cellular->sim_lock_state() == SIM_UNLOCKED || | 479 } else if (cellular->sim_lock_state() == SIM_UNLOCKED || |
445 cellular->sim_lock_state() == SIM_UNKNOWN) { | 480 cellular->sim_lock_state() == SIM_UNKNOWN) { |
446 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); | 481 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); |
447 } else { | 482 } else { |
448 SimDialogDelegate::ShowDialog(owner_->delegate()->GetNativeWindow(), | 483 SimDialogDelegate::ShowDialog(owner_->delegate()->GetNativeWindow(), |
449 SimDialogDelegate::SIM_DIALOG_UNLOCK); | 484 SimDialogDelegate::SIM_DIALOG_UNLOCK); |
450 } | 485 } |
451 } else if (flags & FLAG_TOGGLE_OFFLINE) { | 486 } else if (flags & FLAG_TOGGLE_OFFLINE) { |
452 cros->EnableOfflineMode(!cros->offline_mode()); | 487 cros->EnableOfflineMode(!cros->offline_mode()); |
453 } else if (flags & FLAG_ETHERNET) { | 488 } else if (flags & FLAG_ETHERNET) { |
454 if (cros->ethernet_connected()) { | 489 if (cros->ethernet_connected()) |
455 owner_->ShowTabbedNetworkSettings(cros->ethernet_network()); | 490 owner_->ShowTabbedNetworkSettings(cros->ethernet_network()); |
456 } | |
457 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | | 491 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | |
458 FLAG_CELLULAR | FLAG_ADD_CELLULAR | | 492 FLAG_CELLULAR | FLAG_ADD_CELLULAR | |
459 FLAG_VPN | FLAG_ADD_VPN)) { | 493 FLAG_VPN | FLAG_ADD_VPN)) { |
460 ConnectToNetworkAt(index, std::string(), std::string(), -1); | 494 ConnectToNetworkAt(index, std::string(), std::string(), -1); |
461 } else if (flags & FLAG_DISCONNECT_VPN) { | 495 } else if (flags & FLAG_DISCONNECT_VPN) { |
462 const VirtualNetwork* active_vpn = cros->virtual_network(); | 496 const VirtualNetwork* active_vpn = cros->virtual_network(); |
463 if (active_vpn) | 497 if (active_vpn) |
464 cros->DisconnectFromNetwork(active_vpn); | 498 cros->DisconnectFromNetwork(active_vpn); |
465 } else if (flags & FLAG_VIEW_ACCOUNT) { | 499 } else if (flags & FLAG_VIEW_ACCOUNT) { |
466 Browser* browser = BrowserList::GetLastActive(); | 500 Browser* browser = BrowserList::GetLastActive(); |
467 if (browser) | 501 if (browser) |
468 browser->ShowSingletonTab(GURL(top_up_url_)); | 502 browser->ShowSingletonTab(GURL(top_up_url_)); |
469 } | 503 } |
470 } | 504 } |
471 | 505 |
472 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) { | 506 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) { |
473 } | 507 } |
474 | 508 |
475 //////////////////////////////////////////////////////////////////////////////// | 509 //////////////////////////////////////////////////////////////////////////////// |
476 // NetworkMenuModel, private methods: | 510 // NetworkMenuModel, private methods: |
477 | 511 |
478 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { | 512 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { |
479 views::Widget* window = browser::CreateViewsWindow( | 513 views::Widget* window = browser::CreateViewsWindow( |
480 owner_->delegate()->GetNativeWindow(), view, STYLE_GENERIC); | 514 owner_->delegate()->GetNativeWindow(), view, STYLE_GENERIC); |
481 window->SetAlwaysOnTop(true); | 515 window->SetAlwaysOnTop(true); |
482 window->Show(); | 516 window->Show(); |
483 } | 517 } |
484 | 518 |
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 { | 519 void NetworkMenuModel::ShowOther(ConnectionType type) const { |
494 if (type == TYPE_CELLULAR) { | 520 if (type == TYPE_CELLULAR) { |
495 ChooseMobileNetworkDialog::ShowDialog( | 521 ChooseMobileNetworkDialog::ShowDialog( |
496 owner_->delegate()->GetNativeWindow()); | 522 owner_->delegate()->GetNativeWindow()); |
497 } else { | 523 } else { |
498 ShowNetworkConfigView(new NetworkConfigView(type)); | 524 ShowNetworkConfigView(new NetworkConfigView(type)); |
499 } | 525 } |
500 } | 526 } |
501 | 527 |
502 //////////////////////////////////////////////////////////////////////////////// | 528 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1082 window->Show(); | 1108 window->Show(); |
1083 } else { | 1109 } else { |
1084 cros->ConnectToWifiNetwork(wifi); | 1110 cros->ConnectToWifiNetwork(wifi); |
1085 // Connection failures are responsible for updating the UI, including | 1111 // Connection failures are responsible for updating the UI, including |
1086 // reopening dialogs. | 1112 // reopening dialogs. |
1087 } | 1113 } |
1088 } | 1114 } |
1089 } | 1115 } |
1090 | 1116 |
1091 } // namespace chromeos | 1117 } // namespace chromeos |
OLD | NEW |