Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: chrome/browser/chromeos/status/network_menu.cc

Issue 9690021: chromeos: Refactor some network related code in preparation for the uber tray. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/chromeos/status/network_menu.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
99 bool CellularNeedsActivation(const chromeos::CellularNetwork* cell) {
100 return (cell->activation_state() != chromeos::ACTIVATION_STATE_ACTIVATED &&
101 cell->activation_state() != chromeos::ACTIVATION_STATE_UNKNOWN) ||
102 cell->needs_new_plan();
stevenjb 2012/03/12 18:44:56 I was actually thinking this should a member of Ce
sadrul 2012/03/12 18:51:02 That would make better sense. I have moved the cod
103 }
104
90 } // namespace 105 } // namespace
91 106
92 namespace chromeos { 107 namespace chromeos {
93 108
94 class NetworkMenuModel : public ui::MenuModel { 109 class NetworkMenuModel : public ui::MenuModel {
95 public: 110 public:
96 struct MenuItem { 111 struct MenuItem {
97 MenuItem() 112 MenuItem()
98 : type(ui::MenuModel::TYPE_SEPARATOR), 113 : type(ui::MenuModel::TYPE_SEPARATOR),
99 sub_menu_model(NULL), 114 sub_menu_model(NULL),
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // Top up URL of the current carrier on empty string if there's none. 210 // Top up URL of the current carrier on empty string if there's none.
196 std::string top_up_url_; 211 std::string top_up_url_;
197 212
198 // Carrier ID which top up URL is initialized for. 213 // Carrier ID which top up URL is initialized for.
199 // Used to update top up URL only when cellular carrier has changed. 214 // Used to update top up URL only when cellular carrier has changed.
200 std::string carrier_id_; 215 std::string carrier_id_;
201 216
202 private: 217 private:
203 // Show a NetworkConfigView modal dialog instance. 218 // Show a NetworkConfigView modal dialog instance.
204 void ShowNetworkConfigView(NetworkConfigView* view) const; 219 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. 220 // Open a dialog to set up and connect to a network.
208 void ShowOther(ConnectionType type) const; 221 void ShowOther(ConnectionType type) const;
209 222
210 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel); 223 DISALLOW_COPY_AND_ASSIGN(NetworkMenuModel);
211 }; 224 };
212 225
213
214 class MoreMenuModel : public NetworkMenuModel { 226 class MoreMenuModel : public NetworkMenuModel {
215 public: 227 public:
216 explicit MoreMenuModel(const base::WeakPtr<NetworkMenu> owner) 228 explicit MoreMenuModel(const base::WeakPtr<NetworkMenu> owner)
217 : NetworkMenuModel(owner) {} 229 : NetworkMenuModel(owner) {}
218 virtual ~MoreMenuModel() {} 230 virtual ~MoreMenuModel() {}
219 231
220 // NetworkMenuModel implementation. 232 // NetworkMenuModel implementation.
221 virtual void InitMenuItems(bool should_open_button_options) OVERRIDE; 233 virtual void InitMenuItems(bool should_open_button_options) OVERRIDE;
222 234
223 // ui::MenuModel implementation 235 // ui::MenuModel implementation
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 int auto_connect) const { 286 int auto_connect) const {
275 int flags = menu_items_[index].flags; 287 int flags = menu_items_[index].flags;
276 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); 288 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
277 const std::string& service_path = menu_items_[index].service_path; 289 const std::string& service_path = menu_items_[index].service_path;
278 if (flags & FLAG_WIFI) { 290 if (flags & FLAG_WIFI) {
279 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path); 291 WifiNetwork* wifi = cros->FindWifiNetworkByPath(service_path);
280 if (wifi) { 292 if (wifi) {
281 // Connect or reconnect. 293 // Connect or reconnect.
282 if (auto_connect >= 0) 294 if (auto_connect >= 0)
283 wifi->SetAutoConnect(auto_connect ? true : false); 295 wifi->SetAutoConnect(auto_connect ? true : false);
284 if (wifi->connecting_or_connected()) { 296 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 { 297 } else {
293 // If we are attempting to connect to a network that no longer exists, 298 // If we are attempting to connect to a network that no longer exists,
294 // display a notification. 299 // display a notification.
295 LOG(WARNING) << "Wi-fi network does not exist to connect to: " 300 LOG(WARNING) << "Wi-fi network does not exist to connect to: "
296 << service_path; 301 << service_path;
297 // TODO(stevenjb): Show notification. 302 // TODO(stevenjb): Show notification.
298 } 303 }
299 } else if (flags & FLAG_CELLULAR) { 304 } else if (flags & FLAG_CELLULAR) {
300 CellularNetwork* cellular = cros->FindCellularNetworkByPath( 305 CellularNetwork* cellular = cros->FindCellularNetworkByPath(
301 service_path); 306 service_path);
302 if (cellular) { 307 if (cellular) {
303 if ((cellular->activation_state() != ACTIVATION_STATE_ACTIVATED && 308 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 { 309 } else {
316 // If we are attempting to connect to a network that no longer exists, 310 // If we are attempting to connect to a network that no longer exists,
317 // display a notification. 311 // display a notification.
318 LOG(WARNING) << "Cellular network does not exist to connect to: " 312 LOG(WARNING) << "Cellular network does not exist to connect to: "
319 << service_path; 313 << service_path;
320 // TODO(stevenjb): Show notification. 314 // TODO(stevenjb): Show notification.
321 } 315 }
322 } else if (flags & FLAG_ADD_WIFI) { 316 } else if (flags & FLAG_ADD_WIFI) {
323 ShowOther(TYPE_WIFI); 317 ShowOther(TYPE_WIFI);
324 } else if (flags & FLAG_ADD_CELLULAR) { 318 } else if (flags & FLAG_ADD_CELLULAR) {
325 ShowOther(TYPE_CELLULAR); 319 ShowOther(TYPE_CELLULAR);
326 } else if (flags & FLAG_ADD_VPN) { 320 } else if (flags & FLAG_ADD_VPN) {
327 ShowOther(TYPE_VPN); 321 ShowOther(TYPE_VPN);
328 } else if (flags & FLAG_VPN) { 322 } else if (flags & FLAG_VPN) {
329 VirtualNetwork* vpn = cros->FindVirtualNetworkByPath(service_path); 323 VirtualNetwork* vpn = cros->FindVirtualNetworkByPath(service_path);
330 if (vpn) { 324 if (vpn) {
331 // Connect or reconnect. 325 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 { 326 } else {
342 // If we are attempting to connect to a network that no longer exists, 327 // If we are attempting to connect to a network that no longer exists,
343 // display a notification. 328 // display a notification.
344 LOG(WARNING) << "VPN does not exist to connect to: " << service_path; 329 LOG(WARNING) << "VPN does not exist to connect to: " << service_path;
345 // TODO(stevenjb): Show notification. 330 // TODO(stevenjb): Show notification.
346 } 331 }
347 } 332 }
348 } 333 }
349 334
335 void NetworkMenu::ConnectToNetwork(Network* network) {
336 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary();
337 switch (network->type()) {
338 case TYPE_ETHERNET: {
339 ShowTabbedNetworkSettings(network);
340 break;
341 }
342 case TYPE_WIFI: {
343 WifiNetwork* wifi = static_cast<WifiNetwork*>(network);
344 if (wifi->connecting_or_connected()) {
345 ShowTabbedNetworkSettings(wifi);
346 } else {
347 wifi->AttemptConnection(base::Bind(&NetworkMenu::DoConnect,
348 weak_pointer_factory_.GetWeakPtr(),
349 wifi));
350 }
351 break;
352 }
353
354 case TYPE_CELLULAR: {
355 CellularNetwork* cell = static_cast<CellularNetwork*>(network);
356 if (CellularNeedsActivation(cell)) {
357 ActivateCellular(cell);
358 } else if (cell->connecting_or_connected()) {
359 // Cellular network is connecting or connected,
360 // so we show the config settings for the cellular network.
361 ShowTabbedNetworkSettings(cell);
362 } else {
363 // Clicked on a disconnected cellular network, so connect to it.
364 cros->ConnectToCellularNetwork(cell);
365 }
366 break;
367 }
368
369 case TYPE_VPN: {
370 VirtualNetwork* vpn = static_cast<VirtualNetwork*>(network);
371 // Connect or reconnect.
372 if (vpn->connecting_or_connected()) {
373 ShowTabbedNetworkSettings(vpn);
374 } else {
375 vpn->AttemptConnection(base::Bind(&NetworkMenu::DoConnect,
376 weak_pointer_factory_.GetWeakPtr(),
377 vpn));
378 }
379 break;
380 }
381
382 default:
383 break;
384 }
385 }
386
350 //////////////////////////////////////////////////////////////////////////////// 387 ////////////////////////////////////////////////////////////////////////////////
351 // NetworkMenuModel, ui::MenuModel implementation: 388 // NetworkMenuModel, ui::MenuModel implementation:
352 389
353 bool NetworkMenuModel::HasIcons() const { 390 bool NetworkMenuModel::HasIcons() const {
354 return true; 391 return true;
355 } 392 }
356 393
357 int NetworkMenuModel::GetItemCount() const { 394 int NetworkMenuModel::GetItemCount() const {
358 return static_cast<int>(menu_items_.size()); 395 return static_cast<int>(menu_items_.size());
359 } 396 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 } else if (cellular->sim_lock_state() == SIM_UNLOCKED || 481 } else if (cellular->sim_lock_state() == SIM_UNLOCKED ||
445 cellular->sim_lock_state() == SIM_UNKNOWN) { 482 cellular->sim_lock_state() == SIM_UNKNOWN) {
446 cros->EnableCellularNetworkDevice(!cros->cellular_enabled()); 483 cros->EnableCellularNetworkDevice(!cros->cellular_enabled());
447 } else { 484 } else {
448 SimDialogDelegate::ShowDialog(owner_->delegate()->GetNativeWindow(), 485 SimDialogDelegate::ShowDialog(owner_->delegate()->GetNativeWindow(),
449 SimDialogDelegate::SIM_DIALOG_UNLOCK); 486 SimDialogDelegate::SIM_DIALOG_UNLOCK);
450 } 487 }
451 } else if (flags & FLAG_TOGGLE_OFFLINE) { 488 } else if (flags & FLAG_TOGGLE_OFFLINE) {
452 cros->EnableOfflineMode(!cros->offline_mode()); 489 cros->EnableOfflineMode(!cros->offline_mode());
453 } else if (flags & FLAG_ETHERNET) { 490 } else if (flags & FLAG_ETHERNET) {
454 if (cros->ethernet_connected()) { 491 if (cros->ethernet_connected())
455 owner_->ShowTabbedNetworkSettings(cros->ethernet_network()); 492 owner_->ShowTabbedNetworkSettings(cros->ethernet_network());
456 }
457 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI | 493 } else if (flags & (FLAG_WIFI | FLAG_ADD_WIFI |
458 FLAG_CELLULAR | FLAG_ADD_CELLULAR | 494 FLAG_CELLULAR | FLAG_ADD_CELLULAR |
459 FLAG_VPN | FLAG_ADD_VPN)) { 495 FLAG_VPN | FLAG_ADD_VPN)) {
460 ConnectToNetworkAt(index, std::string(), std::string(), -1); 496 ConnectToNetworkAt(index, std::string(), std::string(), -1);
461 } else if (flags & FLAG_DISCONNECT_VPN) { 497 } else if (flags & FLAG_DISCONNECT_VPN) {
462 const VirtualNetwork* active_vpn = cros->virtual_network(); 498 const VirtualNetwork* active_vpn = cros->virtual_network();
463 if (active_vpn) 499 if (active_vpn)
464 cros->DisconnectFromNetwork(active_vpn); 500 cros->DisconnectFromNetwork(active_vpn);
465 } else if (flags & FLAG_VIEW_ACCOUNT) { 501 } else if (flags & FLAG_VIEW_ACCOUNT) {
466 Browser* browser = BrowserList::GetLastActive(); 502 Browser* browser = BrowserList::GetLastActive();
467 if (browser) 503 if (browser)
468 browser->ShowSingletonTab(GURL(top_up_url_)); 504 browser->ShowSingletonTab(GURL(top_up_url_));
469 } 505 }
470 } 506 }
471 507
472 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) { 508 void NetworkMenuModel::SetMenuModelDelegate(ui::MenuModelDelegate* delegate) {
473 } 509 }
474 510
475 //////////////////////////////////////////////////////////////////////////////// 511 ////////////////////////////////////////////////////////////////////////////////
476 // NetworkMenuModel, private methods: 512 // NetworkMenuModel, private methods:
477 513
478 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const { 514 void NetworkMenuModel::ShowNetworkConfigView(NetworkConfigView* view) const {
479 views::Widget* window = browser::CreateViewsWindow( 515 views::Widget* window = browser::CreateViewsWindow(
480 owner_->delegate()->GetNativeWindow(), view, STYLE_GENERIC); 516 owner_->delegate()->GetNativeWindow(), view, STYLE_GENERIC);
481 window->SetAlwaysOnTop(true); 517 window->SetAlwaysOnTop(true);
482 window->Show(); 518 window->Show();
483 } 519 }
484 520
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 { 521 void NetworkMenuModel::ShowOther(ConnectionType type) const {
494 if (type == TYPE_CELLULAR) { 522 if (type == TYPE_CELLULAR) {
495 ChooseMobileNetworkDialog::ShowDialog( 523 ChooseMobileNetworkDialog::ShowDialog(
496 owner_->delegate()->GetNativeWindow()); 524 owner_->delegate()->GetNativeWindow());
497 } else { 525 } else {
498 ShowNetworkConfigView(new NetworkConfigView(type)); 526 ShowNetworkConfigView(new NetworkConfigView(type));
499 } 527 }
500 } 528 }
501 529
502 //////////////////////////////////////////////////////////////////////////////// 530 ////////////////////////////////////////////////////////////////////////////////
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
1033 delegate_->GetMenuButton(), bounds, views::MenuItemView::TOPRIGHT, 1061 delegate_->GetMenuButton(), bounds, views::MenuItemView::TOPRIGHT,
1034 views::MenuRunner::HAS_MNEMONICS) == views::MenuRunner::MENU_DELETED) 1062 views::MenuRunner::HAS_MNEMONICS) == views::MenuRunner::MENU_DELETED)
1035 return; 1063 return;
1036 } 1064 }
1037 1065
1038 void NetworkMenu::ShowTabbedNetworkSettings(const Network* network) const { 1066 void NetworkMenu::ShowTabbedNetworkSettings(const Network* network) const {
1039 DCHECK(network); 1067 DCHECK(network);
1040 Browser* browser = BrowserList::GetLastActive(); 1068 Browser* browser = BrowserList::GetLastActive();
1041 if (!browser) 1069 if (!browser)
1042 return; 1070 return;
1071
1072 // In case of a VPN, show the config settings for the connected network.
1073 if (network->type() == chromeos::TYPE_VPN)
1074 network = CrosLibrary::Get()->GetNetworkLibrary()->connected_network();
stevenjb 2012/03/12 18:44:56 Need an early exit if |network| is NULL.
sadrul 2012/03/12 18:51:02 Done.
1075
1043 std::string network_name(network->name()); 1076 std::string network_name(network->name());
1044 if (network_name.empty() && network->type() == chromeos::TYPE_ETHERNET) { 1077 if (network_name.empty() && network->type() == chromeos::TYPE_ETHERNET) {
1045 network_name = l10n_util::GetStringUTF8( 1078 network_name = l10n_util::GetStringUTF8(
1046 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); 1079 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
1047 } 1080 }
1048 std::string page = StringPrintf( 1081 std::string page = StringPrintf(
1049 "%s?servicePath=%s&networkType=%d&networkName=%s", 1082 "%s?servicePath=%s&networkType=%d&networkName=%s",
1050 chrome::kInternetOptionsSubPage, 1083 chrome::kInternetOptionsSubPage,
1051 net::EscapeUrlEncodedData(network->service_path(), true).c_str(), 1084 net::EscapeUrlEncodedData(network->service_path(), true).c_str(),
1052 network->type(), 1085 network->type(),
(...skipping 29 matching lines...) Expand all
1082 window->Show(); 1115 window->Show();
1083 } else { 1116 } else {
1084 cros->ConnectToWifiNetwork(wifi); 1117 cros->ConnectToWifiNetwork(wifi);
1085 // Connection failures are responsible for updating the UI, including 1118 // Connection failures are responsible for updating the UI, including
1086 // reopening dialogs. 1119 // reopening dialogs.
1087 } 1120 }
1088 } 1121 }
1089 } 1122 }
1090 1123
1091 } // namespace chromeos 1124 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/status/network_menu.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698