OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
25 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
26 #include "ui/gfx/canvas_skia.h" | 26 #include "ui/gfx/canvas_skia.h" |
27 #include "ui/gfx/skbitmap_operations.h" | 27 #include "ui/gfx/skbitmap_operations.h" |
28 #include "views/controls/menu/menu_2.h" | 28 #include "views/controls/menu/menu_2.h" |
29 #include "views/window/window.h" | 29 #include "views/window/window.h" |
30 | 30 |
31 namespace chromeos { | 31 namespace chromeos { |
32 | 32 |
33 class MainMenuModel : public NetworkMenuModel { | 33 class MoreMenuModel : public NetworkMenuModel { |
34 public: | 34 public: |
35 explicit MainMenuModel(NetworkMenu* owner); | 35 explicit MoreMenuModel(NetworkMenu* owner); |
36 virtual ~MainMenuModel() {} | 36 virtual ~MoreMenuModel() {} |
37 | 37 |
38 // NetworkMenuModel implementation. | 38 // NetworkMenuModel implementation. |
39 virtual void InitMenuItems(bool is_browser_mode, | 39 virtual void InitMenuItems(bool is_browser_mode, |
40 bool should_open_button_options); | 40 bool should_open_button_options); |
41 | 41 |
42 private: | 42 private: |
43 scoped_ptr<NetworkMenuModel> vpn_menu_model_; | 43 friend class MainMenuModel; |
44 | 44 DISALLOW_COPY_AND_ASSIGN(MoreMenuModel); |
45 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); | |
46 }; | 45 }; |
47 | 46 |
48 class VPNMenuModel : public NetworkMenuModel { | 47 class VPNMenuModel : public NetworkMenuModel { |
49 public: | 48 public: |
50 explicit VPNMenuModel(NetworkMenu* owner); | 49 explicit VPNMenuModel(NetworkMenu* owner); |
51 virtual ~VPNMenuModel() {} | 50 virtual ~VPNMenuModel() {} |
52 | 51 |
53 // NetworkMenuModel implementation. | 52 // NetworkMenuModel implementation. |
54 virtual void InitMenuItems(bool is_browser_mode, | 53 virtual void InitMenuItems(bool is_browser_mode, |
55 bool should_open_button_options); | 54 bool should_open_button_options); |
56 | 55 |
57 static SkBitmap IconForDisplay(const Network* network); | 56 static SkBitmap IconForDisplay(const Network* network); |
58 | 57 |
59 private: | 58 private: |
60 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); | 59 DISALLOW_COPY_AND_ASSIGN(VPNMenuModel); |
61 }; | 60 }; |
62 | 61 |
| 62 class MainMenuModel : public NetworkMenuModel { |
| 63 public: |
| 64 explicit MainMenuModel(NetworkMenu* owner); |
| 65 virtual ~MainMenuModel() {} |
| 66 |
| 67 // NetworkMenuModel implementation. |
| 68 virtual void InitMenuItems(bool is_browser_mode, |
| 69 bool should_open_button_options); |
| 70 |
| 71 private: |
| 72 scoped_ptr<NetworkMenuModel> vpn_menu_model_; |
| 73 scoped_ptr<MoreMenuModel> more_menu_model_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(MainMenuModel); |
| 76 }; |
| 77 |
63 //////////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////////// |
64 // NetworkMenuModel, public methods: | 79 // NetworkMenuModel, public methods: |
65 | 80 |
66 bool NetworkMenuModel::ConnectToNetworkAt(int index, | 81 bool NetworkMenuModel::ConnectToNetworkAt(int index, |
67 const std::string& passphrase, | 82 const std::string& passphrase, |
68 const std::string& ssid, | 83 const std::string& ssid, |
69 int auto_connect) const { | 84 int auto_connect) const { |
70 int flags = menu_items_[index].flags; | 85 int flags = menu_items_[index].flags; |
71 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 86 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
72 const std::string& service_path = menu_items_[index].service_path; | 87 const std::string& service_path = menu_items_[index].service_path; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 } | 297 } |
283 | 298 |
284 void NetworkMenuModel::ShowOtherCellular() const { | 299 void NetworkMenuModel::ShowOtherCellular() const { |
285 ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow()); | 300 ChooseMobileNetworkDialog::ShowDialog(owner_->GetNativeWindow()); |
286 } | 301 } |
287 | 302 |
288 //////////////////////////////////////////////////////////////////////////////// | 303 //////////////////////////////////////////////////////////////////////////////// |
289 // MainMenuModel | 304 // MainMenuModel |
290 | 305 |
291 MainMenuModel::MainMenuModel(NetworkMenu* owner) | 306 MainMenuModel::MainMenuModel(NetworkMenu* owner) |
292 : NetworkMenuModel(owner) { | 307 : NetworkMenuModel(owner), |
293 vpn_menu_model_.reset(new VPNMenuModel(owner)); | 308 vpn_menu_model_(new VPNMenuModel(owner)), |
| 309 more_menu_model_(new MoreMenuModel(owner)) { |
294 } | 310 } |
295 | 311 |
296 void MainMenuModel::InitMenuItems(bool is_browser_mode, | 312 void MainMenuModel::InitMenuItems(bool is_browser_mode, |
297 bool should_open_button_options) { | 313 bool should_open_button_options) { |
298 // This gets called on initialization, so any changes should be reflected | 314 // This gets called on initialization, so any changes should be reflected |
299 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). | 315 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
300 | 316 |
301 menu_items_.clear(); | 317 menu_items_.clear(); |
302 | 318 |
303 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); | 319 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 | 517 |
502 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableVPN)) { | 518 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableVPN)) { |
503 // If there's a connected network, add submenu for Private Networks. | 519 // If there's a connected network, add submenu for Private Networks. |
504 const Network* connected_network = cros->connected_network(); | 520 const Network* connected_network = cros->connected_network(); |
505 if (connected_network) { | 521 if (connected_network) { |
506 menu_items_.push_back(MenuItem()); // Separator | 522 menu_items_.push_back(MenuItem()); // Separator |
507 menu_items_.push_back(MenuItem( | 523 menu_items_.push_back(MenuItem( |
508 ui::MenuModel::TYPE_SUBMENU, | 524 ui::MenuModel::TYPE_SUBMENU, |
509 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_PRIVATE_NETWORKS), | 525 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_PRIVATE_NETWORKS), |
510 VPNMenuModel::IconForDisplay(connected_network), | 526 VPNMenuModel::IconForDisplay(connected_network), |
511 vpn_menu_model_.get(), FLAG_PRIVATE_NETWORKS)); | 527 vpn_menu_model_.get(), FLAG_NONE)); |
| 528 vpn_menu_model_->InitMenuItems( |
| 529 is_browser_mode, should_open_button_options); |
512 } | 530 } |
513 } | 531 } |
514 | 532 |
515 // Enable / disable wireless. | 533 // Enable / disable wireless. |
516 if (wifi_available || cellular_available) { | 534 if (wifi_available || cellular_available) { |
517 menu_items_.push_back(MenuItem()); // Separator | 535 menu_items_.push_back(MenuItem()); // Separator |
518 | 536 |
519 if (wifi_available) { | 537 if (wifi_available) { |
520 // Add 'Scanning...' | 538 // Add 'Scanning...' |
521 if (cros->wifi_scanning()) { | 539 if (cros->wifi_scanning()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 } | 572 } |
555 } | 573 } |
556 | 574 |
557 // Offline mode. | 575 // Offline mode. |
558 // TODO(chocobo): Uncomment once we figure out how to do offline mode. | 576 // TODO(chocobo): Uncomment once we figure out how to do offline mode. |
559 // menu_items_.push_back(MenuItem(cros->offline_mode() ? | 577 // menu_items_.push_back(MenuItem(cros->offline_mode() ? |
560 // ui::MenuModel::TYPE_CHECK : ui::MenuModel::TYPE_COMMAND, | 578 // ui::MenuModel::TYPE_CHECK : ui::MenuModel::TYPE_COMMAND, |
561 // l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OFFLINE_MODE), | 579 // l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_OFFLINE_MODE), |
562 // SkBitmap(), std::string(), FLAG_TOGGLE_OFFLINE)); | 580 // SkBitmap(), std::string(), FLAG_TOGGLE_OFFLINE)); |
563 | 581 |
564 bool connected = cros->Connected(); // always call for test expectations. | 582 // Additional links like: |
565 bool oobe = !should_open_button_options; // we don't show options for OOBE. | 583 // * Network settings; |
566 // Network settings. (And IP Address) | 584 // * IP Address on active interface; |
567 if (!oobe) { | 585 // * Hardware addresses for wifi and ethernet. |
568 menu_items_.push_back(MenuItem()); // Separator | 586 menu_items_.push_back(MenuItem()); // Separator |
569 | 587 more_menu_model_->InitMenuItems(is_browser_mode, should_open_button_options); |
570 if (connected) { | 588 if (is_browser_mode) { |
571 std::string ip_address = cros->IPAddress(); | 589 // In browser mode we do not want separate submenu, inline items. |
572 if (!ip_address.empty()) { | 590 menu_items_.insert( |
573 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, | 591 menu_items_.end(), |
574 ASCIIToUTF16(cros->IPAddress()), SkBitmap(), std::string(), | 592 more_menu_model_->menu_items_.begin(), |
575 FLAG_DISABLED)); | 593 more_menu_model_->menu_items_.end()); |
576 } | 594 } else { |
577 } | 595 if (!more_menu_model_->menu_items_.empty()) { |
578 | 596 menu_items_.push_back(MenuItem( |
579 label = l10n_util::GetStringUTF16(is_browser_mode ? | 597 ui::MenuModel::TYPE_SUBMENU, |
580 IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG : | 598 l10n_util::GetStringUTF16(IDS_STATUSBAR_NETWORK_MORE), |
581 IDS_STATUSBAR_NETWORK_OPEN_PROXY_SETTINGS_DIALOG); | 599 SkBitmap(), more_menu_model_.get(), FLAG_NONE)); |
582 menu_items_.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, label, | |
583 SkBitmap(), std::string(), FLAG_OPTIONS)); | |
584 } | |
585 | |
586 // Recursively call each submenu to populate its own menu items. | |
587 for (size_t i = 0; i < menu_items_.size(); ++i) { | |
588 if (menu_items_[i].type == ui::MenuModel::TYPE_SUBMENU && | |
589 menu_items_[i].sub_menu_model) { | |
590 menu_items_[i].sub_menu_model->InitMenuItems(is_browser_mode, | |
591 should_open_button_options); | |
592 } | 600 } |
593 } | 601 } |
594 } | 602 } |
595 | 603 |
596 //////////////////////////////////////////////////////////////////////////////// | 604 //////////////////////////////////////////////////////////////////////////////// |
597 // VPNMenuModel | 605 // VPNMenuModel |
598 | 606 |
599 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) | 607 VPNMenuModel::VPNMenuModel(NetworkMenu* owner) |
600 : NetworkMenuModel(owner) { | 608 : NetworkMenuModel(owner) { |
601 } | 609 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 default: | 703 default: |
696 LOG(WARNING) << "VPN not handled for connection type " << network->type(); | 704 LOG(WARNING) << "VPN not handled for connection type " << network->type(); |
697 return SkBitmap(); | 705 return SkBitmap(); |
698 } | 706 } |
699 | 707 |
700 return NetworkMenu::IconForDisplay(icon, bottom_right_badge, top_left_badge, | 708 return NetworkMenu::IconForDisplay(icon, bottom_right_badge, top_left_badge, |
701 bottom_left_badge); | 709 bottom_left_badge); |
702 } | 710 } |
703 | 711 |
704 //////////////////////////////////////////////////////////////////////////////// | 712 //////////////////////////////////////////////////////////////////////////////// |
| 713 // MoreMenuModel |
| 714 |
| 715 MoreMenuModel::MoreMenuModel(NetworkMenu* owner) |
| 716 : NetworkMenuModel(owner) { |
| 717 } |
| 718 |
| 719 void MoreMenuModel::InitMenuItems( |
| 720 bool is_browser_mode, bool should_open_button_options) { |
| 721 // This gets called on initialization, so any changes should be reflected |
| 722 // in CrosMock::SetNetworkLibraryStatusAreaExpectations(). |
| 723 |
| 724 menu_items_.clear(); |
| 725 MenuItemVector link_items; |
| 726 MenuItemVector address_items; |
| 727 |
| 728 NetworkLibrary* cros = CrosLibrary::Get()->GetNetworkLibrary(); |
| 729 bool oobe = !should_open_button_options; // we don't show options for OOBE. |
| 730 if (!oobe) { |
| 731 string16 label = l10n_util::GetStringUTF16(is_browser_mode ? |
| 732 IDS_STATUSBAR_NETWORK_OPEN_OPTIONS_DIALOG : |
| 733 IDS_STATUSBAR_NETWORK_OPEN_PROXY_SETTINGS_DIALOG); |
| 734 link_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, label, |
| 735 SkBitmap(), std::string(), FLAG_OPTIONS)); |
| 736 } |
| 737 |
| 738 bool connected = cros->Connected(); // always call for test expectations. |
| 739 if (connected) { |
| 740 std::string ip_address = cros->IPAddress(); |
| 741 if (!ip_address.empty()) { |
| 742 address_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, |
| 743 ASCIIToUTF16(cros->IPAddress()), SkBitmap(), std::string(), |
| 744 FLAG_DISABLED)); |
| 745 } |
| 746 } |
| 747 |
| 748 if (!is_browser_mode) { |
| 749 const NetworkDevice* ether = cros->FindEthernetDevice(); |
| 750 if (ether) { |
| 751 std::string hardware_address; |
| 752 cros->GetIPConfigs(ether->device_path(), &hardware_address, |
| 753 NetworkLibrary::FORMAT_COLON_SEPARATED_HEX); |
| 754 if (!hardware_address.empty()) { |
| 755 std::string label = l10n_util::GetStringUTF8( |
| 756 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET) + " " + hardware_address; |
| 757 address_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, |
| 758 UTF8ToUTF16(label), SkBitmap(), std::string(), FLAG_DISABLED)); |
| 759 } |
| 760 } |
| 761 |
| 762 if (cros->wifi_enabled()) { |
| 763 const NetworkDevice* wifi = cros->FindWifiDevice(); |
| 764 if (wifi) { |
| 765 std::string hardware_address; |
| 766 cros->GetIPConfigs(wifi->device_path(), |
| 767 &hardware_address, NetworkLibrary::FORMAT_COLON_SEPARATED_HEX); |
| 768 if (!hardware_address.empty()) { |
| 769 std::string label = l10n_util::GetStringUTF8( |
| 770 IDS_STATUSBAR_NETWORK_DEVICE_WIFI) + " " + hardware_address; |
| 771 address_items.push_back(MenuItem(ui::MenuModel::TYPE_COMMAND, |
| 772 UTF8ToUTF16(label), SkBitmap(), std::string(), FLAG_DISABLED)); |
| 773 } |
| 774 } |
| 775 } |
| 776 } |
| 777 |
| 778 menu_items_ = link_items; |
| 779 if (!menu_items_.empty() && address_items.size() > 1) |
| 780 menu_items_.push_back(MenuItem()); // Separator |
| 781 menu_items_.insert(menu_items_.end(), |
| 782 address_items.begin(), address_items.end()); |
| 783 } |
| 784 |
| 785 //////////////////////////////////////////////////////////////////////////////// |
705 // NetworkMenu | 786 // NetworkMenu |
706 | 787 |
707 // static | 788 // static |
708 const int NetworkMenu::kNumBarsImages = 4; | 789 const int NetworkMenu::kNumBarsImages = 4; |
709 | 790 |
710 // NOTE: Use an array rather than just calculating a resource number to avoid | 791 // NOTE: Use an array rather than just calculating a resource number to avoid |
711 // creating implicit ordering dependencies on the resource values. | 792 // creating implicit ordering dependencies on the resource values. |
712 // static | 793 // static |
713 const int NetworkMenu::kBarsImages[kNumBarsImages] = { | 794 const int NetworkMenu::kBarsImages[kNumBarsImages] = { |
714 IDR_STATUSBAR_NETWORK_BARS1, | 795 IDR_STATUSBAR_NETWORK_BARS1, |
(...skipping 27 matching lines...) Expand all Loading... |
742 | 823 |
743 // static | 824 // static |
744 const int NetworkMenu::kNumAnimatingImages = 10; | 825 const int NetworkMenu::kNumAnimatingImages = 10; |
745 | 826 |
746 // static | 827 // static |
747 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; | 828 SkBitmap NetworkMenu::kAnimatingImages[kNumAnimatingImages]; |
748 | 829 |
749 // static | 830 // static |
750 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; | 831 SkBitmap NetworkMenu::kAnimatingImagesBlack[kNumAnimatingImages]; |
751 | 832 |
752 NetworkMenu::NetworkMenu() | 833 NetworkMenu::NetworkMenu() : min_width_(-1) { |
753 : min_width_(-1) { | |
754 main_menu_model_.reset(new MainMenuModel(this)); | 834 main_menu_model_.reset(new MainMenuModel(this)); |
755 network_menu_.reset(new views::Menu2(main_menu_model_.get())); | 835 network_menu_.reset(new views::Menu2(main_menu_model_.get())); |
756 } | 836 } |
757 | 837 |
758 NetworkMenu::~NetworkMenu() { | 838 NetworkMenu::~NetworkMenu() { |
759 } | 839 } |
760 | 840 |
761 void NetworkMenu::SetFirstLevelMenuWidth(int width) { | 841 void NetworkMenu::SetFirstLevelMenuWidth(int width) { |
762 min_width_ = width; | 842 min_width_ = width; |
763 // This actually has no effect since menu is rebuilt before showing. | 843 // This actually has no effect since menu is rebuilt before showing. |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 // Restore menu width, if it was set up. | 1066 // Restore menu width, if it was set up. |
987 // NOTE: width isn't checked for correctness here since all width-related | 1067 // NOTE: width isn't checked for correctness here since all width-related |
988 // logic implemented inside |network_menu_|. | 1068 // logic implemented inside |network_menu_|. |
989 if (min_width_ != -1) | 1069 if (min_width_ != -1) |
990 network_menu_->SetMinimumWidth(min_width_); | 1070 network_menu_->SetMinimumWidth(min_width_); |
991 refreshing_menu_ = false; | 1071 refreshing_menu_ = false; |
992 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); | 1072 network_menu_->RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
993 } | 1073 } |
994 | 1074 |
995 } // namespace chromeos | 1075 } // namespace chromeos |
| 1076 |
| 1077 |
OLD | NEW |